-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathir_DistanceProtocol.hpp
More file actions
355 lines (326 loc) · 14.6 KB
/
Copy pathir_DistanceProtocol.hpp
File metadata and controls
355 lines (326 loc) · 14.6 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* ir_DistanceProtocol.hpp
*
* This decoder tries to decode a pulse width or pulse distance protocol.
* 1. Analyze all space and mark length
* 2. Decide if we have an pulse width or distance protocol
* 3. Try to decode with the mark and space data found in step 1
* No data and address decoding, only raw data as result.
*
* Pulse distance data can be sent with the generic function:
* void sendPulseDistanceWidthData(unsigned int aOneMarkMicros, unsigned int aOneSpaceMicros, unsigned int aZeroMarkMicros,
* unsigned int aZeroSpaceMicros, uint32_t aData, uint8_t aNumberOfBits, bool aMSBfirst, bool aSendStopBit = false)
* The header must be sent manually with:
* IrSender.mark(MarkMicros)
* IrSender.space(SpaceMicros);
* see also: SendDemo example line 150
*
* This file is part of Arduino-IRremote https://github.com/Arduino-IRremote/Arduino-IRremote.
*
************************************************************************************
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
************************************************************************************
*/
#ifndef _IR_DISTANCE_HPP
#define _IR_DISTANCE_HPP
#include <Arduino.h>
// accept durations up to 50 * 50 (MICROS_PER_TICK) 2500 microseconds
#define DURATION_ARRAY_SIZE 50
// Switch the decoding according to your needs
#define DISTANCE_DO_MSB_DECODING PROTOCOL_IS_LSB_FIRST // this results in the same decodedRawData as e.g. the NEC and Kaseikyo/Panasonic decoder
//#define DISTANCE_DO_MSB_DECODING PROTOCOL_IS_MSB_FIRST // this resembles the JVC, Denon
//#define INFO // Activate this to save program memory and suppress info output.
//#define DEBUG // Activate this for lots of lovely debug output from this decoder.
#include "IRremoteInt.h" // evaluates the DEBUG for IR_DEBUG_PRINT
//#include "LongUnion.h"
/** \addtogroup Decoder Decoders and encoders for different protocols
* @{
*/
// see: https://www.mikrocontroller.net/articles/IRMP_-_english#Codings
#if defined(DEBUG)
void printDurations(uint8_t aArray[], uint8_t aMaxIndex) {
for (uint_fast8_t i = 0; i <= aMaxIndex; i++) {
if (i % 10 == 0) {
if (i == 0) {
Serial.print(' '); // indentation for the 0
} else {
Serial.println();
}
Serial.print(i);
Serial.print(F(":"));
}
Serial.print(F(" | "));
Serial.print(aArray[i]);
}
Serial.println();
}
#endif
bool aggregateArrayCounts(uint8_t aArray[], uint8_t aMaxIndex, uint8_t *aShortIndex, uint8_t *aLongIndex) {
uint8_t tSum = 0;
uint16_t tWeightedSum = 0;
for (uint_fast8_t i = 0; i <= aMaxIndex; i++) {
uint8_t tCurrentDurations = aArray[i];
if (tCurrentDurations != 0) {
// Add it to sum and remove array content
tSum += tCurrentDurations;
tWeightedSum += (tCurrentDurations * i);
aArray[i] = 0;
}
if ((tCurrentDurations == 0 || i == aMaxIndex) && tSum != 0) {
// here we have a sum and a gap after the values
uint8_t tAggregateIndex = (tWeightedSum + (tSum / 2)) / tSum; // with rounding
aArray[tAggregateIndex] = tSum; // disabling this line increases code size by 2 - unbelievable!
// store aggregate for later decoding
if (*aShortIndex == 0) {
*aShortIndex = tAggregateIndex;
} else if (*aLongIndex == 0) {
*aLongIndex = tAggregateIndex;
} else {
// we have 3 bins => this is likely no pulse width or distance protocol. e.g. it can be RC5.
return false;
}
// initialize for next aggregation
tSum = 0;
tWeightedSum = 0;
}
}
return true;
}
/*
* Try to decode a pulse width or pulse distance protocol.
* 1. Analyze all space and mark length
* 2. Decide if we have an pulse width or distance protocol
* 3. Try to decode with the mark and space data found in step 1
* No data and address decoding, only raw data as result.
*/
bool IRrecv::decodeDistance() {
uint8_t tDurationArray[DURATION_ARRAY_SIZE];
/*
* Accept only protocols with at least 8 bits
*/
if (decodedIRData.rawDataPtr->rawlen < (2 * 8) + 4) {
IR_DEBUG_PRINT(F("PULSE_DISTANCE: "));
IR_DEBUG_PRINT(F("Data length="));
IR_DEBUG_PRINT(decodedIRData.rawDataPtr->rawlen);
IR_DEBUG_PRINTLN(F(" is less than 20"));
return false;
}
uint_fast8_t i;
// Reset duration array
memset(tDurationArray, 0, sizeof(tDurationArray));
uint8_t tMaxDurationIndex = 0;
/*
* Count number of mark durations. Skip leading start and trailing stop bit.
*/
for (i = 3; i < (uint_fast8_t) decodedIRData.rawDataPtr->rawlen - 2; i += 2) {
uint8_t tDurationTicks = decodedIRData.rawDataPtr->rawbuf[i];
if (tDurationTicks < sizeof(tDurationArray)) {
tDurationArray[tDurationTicks]++;
if (tMaxDurationIndex < tDurationTicks) {
tMaxDurationIndex = tDurationTicks;
}
}
}
/*
* Aggregate mark counts to one duration bin
*/
uint8_t tMarkTicksShort = 0;
uint8_t tMarkTicksLong = 0;
if (!aggregateArrayCounts(tDurationArray, tMaxDurationIndex, &tMarkTicksShort, &tMarkTicksLong)) {
IR_DEBUG_PRINT(F("PULSE_DISTANCE: "));
IR_DEBUG_PRINTLN(F("Mark aggregation failed, more than 2 distinct mark duration values found"));
}
#if defined(DEBUG)
Serial.println(F("Mark:"));
printDurations(tDurationArray, tMaxDurationIndex);
#endif
// Reset duration array
memset(tDurationArray, 0, sizeof(tDurationArray));
/*
* Count number of space durations. Skip leading start and trailing stop bit.
*/
tMaxDurationIndex = 0;
for (i = 4; i < (uint_fast8_t) decodedIRData.rawDataPtr->rawlen - 2; i += 2) {
uint8_t tDurationTicks = decodedIRData.rawDataPtr->rawbuf[i];
if (tDurationTicks < sizeof(tDurationArray)) {
tDurationArray[tDurationTicks]++;
if (tMaxDurationIndex < tDurationTicks) {
tMaxDurationIndex = tDurationTicks;
}
}
}
/*
* Aggregate space counts to one duration bin
*/
uint8_t tSpaceTicksShort = 0;
uint8_t tSpaceTicksLong = 0;
if (!aggregateArrayCounts(tDurationArray, tMaxDurationIndex, &tSpaceTicksShort, &tSpaceTicksLong)) {
IR_DEBUG_PRINT(F("PULSE_DISTANCE: "));
IR_DEBUG_PRINTLN(F("Space aggregation failed, more than 2 distinct space duration values found"));
return false;
}
#if defined(DEBUG)
Serial.println(F("Space:"));
printDurations(tDurationArray, tMaxDurationIndex);
#endif
// skip leading start and trailing stop bit.
uint16_t tNumberOfBits = (decodedIRData.rawDataPtr->rawlen / 2) - 2;
decodedIRData.numberOfBits = tNumberOfBits;
/*
* Print characteristics of this protocol. Durations are in ticks.
* Number of bits, start bit, start pause, short mark, long mark, short space, long space
*
* NEC: 32, 180, 90, 11, 0, 11, 34
* Samsung32: 32, 90, 90, 11, 0, 11, 34
* LG: 28, 180, 84, 10, 0, 11, 32
* JVC: 16, 168, 84, 10, 0, 10, 32
* Kaseikyo: 48. 69, 35, 9, 0, 9, 26
* Sony: 12|15|20, 48, 12, 12, 24, 12, 0 // the only known pulse width protocol
*/
IR_DEBUG_PRINT(F("Protocol characteristic for a " STR(MICROS_PER_TICK) " us tick: "));
IR_DEBUG_PRINT(decodedIRData.numberOfBits);
IR_DEBUG_PRINT(F(", "));
IR_DEBUG_PRINT(decodedIRData.rawDataPtr->rawbuf[1]);
IR_DEBUG_PRINT(F(", "));
IR_DEBUG_PRINT(decodedIRData.rawDataPtr->rawbuf[2]);
IR_DEBUG_PRINT(F(", "));
IR_DEBUG_PRINT(tMarkTicksShort);
IR_DEBUG_PRINT(F(", "));
IR_DEBUG_PRINT(tMarkTicksLong);
IR_DEBUG_PRINT(F(", "));
IR_DEBUG_PRINT(tSpaceTicksShort);
IR_DEBUG_PRINT(F(", "));
IR_DEBUG_PRINTLN(tSpaceTicksLong);
uint8_t tStartIndex = 3;
uint8_t tNumberOfAdditionalLong = (tNumberOfBits - 1) / 32;
/*
* decide, if we have an pulse width or distance protocol
*/
if (tSpaceTicksLong > 0) {
// // check if last bit can be decoded as data or not, in this case take it as a stop bit
// if (decodePulseDistanceData(1, decodedIRData.rawDataPtr->rawlen - 3, tMarkTicksShort * MICROS_PER_TICK,
// tSpaceTicksLong * MICROS_PER_TICK, tSpaceTicksShort * MICROS_PER_TICK, DISTANCE_DO_MSB_DECODING)) {
// Serial.print(F("tNumberOfBits++ "));
// tNumberOfBits++;
// }
/*
* Here short and long space duration found. Decode in 32 bit chunks.
*/
for (uint_fast8_t i = 0; i <= tNumberOfAdditionalLong; ++i) {
uint8_t tNumberOfBitsForOneDecode = tNumberOfBits;
if (tNumberOfBitsForOneDecode > 32) {
tNumberOfBitsForOneDecode = 32;
}
if (!decodePulseDistanceData(tNumberOfBitsForOneDecode, tStartIndex, tMarkTicksShort * MICROS_PER_TICK,
tSpaceTicksLong * MICROS_PER_TICK, tSpaceTicksShort * MICROS_PER_TICK, DISTANCE_DO_MSB_DECODING)) {
IR_DEBUG_PRINT(F("PULSE_DISTANCE: "));
IR_DEBUG_PRINTLN(F("Decode failed"));
return false;
} else {
if (i == 0) {
// Print protocol timing only once
IR_INFO_PRINTLN();
IR_INFO_PRINT(F("PULSE_DISTANCE:"));
IR_INFO_PRINT(F(" HeaderMarkMicros="));
IR_INFO_PRINT(decodedIRData.rawDataPtr->rawbuf[1] * MICROS_PER_TICK);
IR_INFO_PRINT(F(" HeaderSpaceMicros="));
IR_INFO_PRINT(decodedIRData.rawDataPtr->rawbuf[2] * MICROS_PER_TICK);
IR_INFO_PRINT(F(" MarkMicros="));
IR_INFO_PRINT(tMarkTicksShort * MICROS_PER_TICK);
IR_INFO_PRINT(F(" OneSpaceMicros="));
IR_INFO_PRINT(tSpaceTicksLong * MICROS_PER_TICK);
IR_INFO_PRINT(F(" ZeroSpaceMicros="));
IR_INFO_PRINT(tSpaceTicksShort * MICROS_PER_TICK);
IR_INFO_PRINT(F(" NumberOfBits="));
IR_INFO_PRINT(decodedIRData.numberOfBits);
IR_INFO_PRINT(F(" DecodedRawData:"));
}
IR_INFO_PRINT(F(" 0x"));
IR_INFO_PRINT(decodedIRData.decodedRawData, HEX);
tStartIndex += 64;
tNumberOfBits -= 32;
}
IR_INFO_PRINTLN();
}
// Store ticks used for decoding in extra
decodedIRData.extra = (tSpaceTicksShort << 8) | tSpaceTicksLong;
decodedIRData.protocol = PULSE_DISTANCE;
} else {
if (tMarkTicksLong == 0) {
IR_DEBUG_PRINT(F("PULSE_DISTANCE: "));
IR_DEBUG_PRINTLN(F("Only 1 distinct duration value for each space and mark found"));
return false;
}
//#define SUPPORT_PULSE_WIDTH_DECODING
#if defined(SUPPORT_PULSE_WIDTH_DECODING) // The only known pulse width protocol is Sony
// // check if last bit can be decoded as data or not, in this case take it as a stop bit
// if (decodePulseWidthData(1, decodedIRData.rawDataPtr->rawlen - 3, tMarkTicksLong * MICROS_PER_TICK,
// tMarkTicksShort * MICROS_PER_TICK, tSpaceTicksShort * MICROS_PER_TICK, DISTANCE_DO_MSB_DECODING)) {
// tNumberOfBits++;
// }
// decode without leading start bit. Currently only seen for sony protocol
for (uint_fast8_t i = 0; i <= tNumberOfAdditionalLong; ++i) {
uint8_t tNumberOfBitsForOneDecode = tNumberOfBits;
if (tNumberOfBitsForOneDecode > 32) {
tNumberOfBitsForOneDecode = 32;
}
if (!decodePulseWidthData(tNumberOfBitsForOneDecode, tStartIndex, tMarkTicksLong * MICROS_PER_TICK,
tMarkTicksShort * MICROS_PER_TICK, tSpaceTicksShort * MICROS_PER_TICK, DISTANCE_DO_MSB_DECODING)) {
IR_DEBUG_PRINT(F("PULSE_WIDTH: "));
IR_DEBUG_PRINTLN(F("Decode failed"));
return false;
}
if (i == 0) {
// Print protocol timing and length only once
IR_INFO_PRINTLN();
IR_INFO_PRINT(F("PULSE_WIDTH:"));
IR_INFO_PRINT(F(" HeaderMarkMicros="));
IR_INFO_PRINT(decodedIRData.rawDataPtr->rawbuf[1] * MICROS_PER_TICK);
IR_INFO_PRINT(F(" HeaderSpaceMicros="));
IR_INFO_PRINT(decodedIRData.rawDataPtr->rawbuf[2] * MICROS_PER_TICK);
IR_INFO_PRINT(F(" OneMarkMicros="));
IR_INFO_PRINT(tMarkTicksLong * MICROS_PER_TICK);
IR_INFO_PRINT(F(" ZeroMarkMicros="));
IR_INFO_PRINT(tMarkTicksShort * MICROS_PER_TICK);
IR_INFO_PRINT(F(" SpaceMicros="));
IR_INFO_PRINT(tSpaceTicksShort * MICROS_PER_TICK);
IR_INFO_PRINT(F(" NumberOfBits="));
IR_INFO_PRINT(decodedIRData.numberOfBits);
IR_INFO_PRINT(F(" DecodedRawData:"));
}
IR_INFO_PRINT(F(" 0x"));
IR_INFO_PRINT(decodedIRData.decodedRawData, HEX);
tStartIndex += 64;
tNumberOfBits -= 32;
}
IR_INFO_PRINTLN();
// Store ticks used for decoding in extra
decodedIRData.extra = (tMarkTicksShort << 8) | tMarkTicksLong;
decodedIRData.protocol = PULSE_WIDTH;
#endif
}
if (DISTANCE_DO_MSB_DECODING) {
decodedIRData.flags = IRDATA_FLAGS_IS_MSB_FIRST;
}
return true;
}
/** @}*/
#endif // _IR_DISTANCE_HPP