-
Notifications
You must be signed in to change notification settings - Fork 2
/
IRDecoder.hpp
215 lines (181 loc) · 5.8 KB
/
IRDecoder.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#ifndef IRDecoder_hpp
#define IRDecoder_hpp
#include <Arduino.h>
#include "IRProtocols.hpp"
#include "IRData.hpp"
class IRDecoder
{
public:
enum Error : char
{
None = 0,
NotEnoughData,
HeaderMismatch,
DataOverflow,
MarkMismatch,
SpaceMismatch,
TrailMismatch
};
static uint16_t lastOffset;
static String errorToString(Error error)
{
switch(error)
{
case None: return "none";
case NotEnoughData: return "not enough data";
case HeaderMismatch: return "header mismatch";
case DataOverflow: return "data overflow";
case MarkMismatch: return "mark mismatch";
case SpaceMismatch: return "space mismatch";
case TrailMismatch: return "trail mismatch";
}
}
static Error tryDecodeIR(decode_results *results, IRData &irData,
IRProtocol *protocol);
};
uint16_t IRDecoder::lastOffset = 1; // defines the static member variable
/**
* Tries to decode the raw data by ckecking its timings against
* a certain protocol.
*
* @see IRProtocol class
*
* @param results obtained from IRremote library
* @param irData destination data packet
* @param protocol
*
* @return true if raw data match given protocol
*/
IRDecoder::Error IRDecoder::tryDecodeIR(
decode_results *results, IRData &irData, IRProtocol *protocol)
{
uint8_t nBits = 0; // # of bits received (mark-space pairs)
uint16_t rawLength = results->rawlen;
unsigned int rawValue = 0;
uint8_t iData = 0;
uint8_t iBit = 0;
char repeatReached = 0; // used on protocols that repeat its packet
lastOffset = 1;
// not sure if this could happen
if(rawLength <= 4) return NotEnoughData;
// checks initial mark and space - please notice lastOffset++
if( !MATCH_MARK(results->rawbuf[lastOffset++], protocol->HeaderMark())
|| !MATCH_SPACE(results->rawbuf[lastOffset++], protocol->HeaderSpace())
)
{
return HeaderMismatch;
}
// ignores start space, header mark and space, and last mark
nBits = (results->rawlen - 4)/2;
if(nBits > irData.MaxSize() * 8)
{
return DataOverflow;
}
// tries to decode each bit
for(iBit = 0; iBit < nBits; iBit++)
{
iData = iBit / 8;
rawValue = results->rawbuf[lastOffset];
// initialize data array
if(iBit % 8 == 0) irData.data[iData] = 0;
if(!MATCH_MARK(rawValue, protocol->BitMark()))
{
return MarkMismatch;
}
lastOffset++;
rawValue = results->rawbuf[lastOffset];
if(MATCH_SPACE(rawValue, protocol->BitOneSpace()))
{
irData.data[iData] = (irData.data[iData] << 1) | 1;
}
else if(MATCH_SPACE(rawValue, protocol->BitZeroSpace()))
{
irData.data[iData] = (irData.data[iData] << 1);
}
else if(protocol->IsRepeated() && MATCH_SPACE(rawValue, protocol->RepeatSpace()))
{
repeatReached = 1;
irData.isRepeated = true;
break;
}
else if(protocol->HasTrail() && (lastOffset == rawLength - 2 || lastOffset == rawLength - 1))
{
if( ( lastOffset == rawLength - 2 && !MATCH_SPACE(rawValue, protocol->TrailSpace()) )
|| (lastOffset == rawLength - 1 && !MATCH_MARK(rawValue, protocol->BitMark()))
)
{
return TrailMismatch;
}
}
else
{
return SpaceMismatch;
}
lastOffset++;
}
// If there is a repeat, nBits calculated previously is wrong.
// Here we update it to the real value, i.e. the number of
// bits processed so far, before reaching repeat space.
// Therefore, data is made of those first bits decoded.
if(repeatReached) nBits = iBit;
else irData.isRepeated = false;
// Align left last bits on last data byte
if(nBits % 8 > 0)
{
irData.data[(int)nBits/8] <<= 8 - (nBits % 8);
}
irData.nBits = nBits;
irData.protocol = protocol;
irData.isValid = true;
return None;
}
/**
* Tries to decode the raw data by ckecking its timings against all
* available protocols.
*
* @see IRProtocols class
*
* @param results raw data
* @param data destination data packet
* @param debug if 1, prints debug info
*
* @return true if a matching protocol was found
*/
bool decodeIR(decode_results *results, IRData &data, char debug)
{
IRProtocol *protocol = nullptr;
IRDecoder::Error error = IRDecoder::None;
data.isValid = false;
// iterates over all protocols
g_irProtocols.First();
while(!g_irProtocols.IsDone())
{
protocol = g_irProtocols.Current();
if(debug)
{
Serial.print("Trying ");
Serial.print(protocol->Name());
Serial.print(": ");
}
error = IRDecoder::tryDecodeIR(results, data, protocol);
if(error == IRDecoder::None)
{
if(debug) Serial.println("MATCH");
break;
}
else
{
if(debug)
{
Serial.print(IRDecoder::errorToString(error));
Serial.print(" - [");
Serial.print(IRDecoder::lastOffset);
Serial.print("] ");
Serial.println((unsigned long) results->rawbuf[IRDecoder::lastOffset]*USECPERTICK, DEC);
}
}
g_irProtocols.Next();
}
return data.isValid;
}
#endif