58 #ifndef _IR_DISTANCE_WIDTH_HPP
59 #define _IR_DISTANCE_WIDTH_HPP
61 #if !defined(DISTANCE_WIDTH_MAXIMUM_REPEAT_DISTANCE_MICROS)
62 #define DISTANCE_WIDTH_MAXIMUM_REPEAT_DISTANCE_MICROS 100000 // 100 ms, bit it is just a guess
72 #if !defined(DISTANCE_WIDTH_DECODER_DURATION_ARRAY_SIZE)
73 # if (defined(RAMEND) && RAMEND <= 0x8FF) || (defined(RAMSIZE) && RAMSIZE < 0x8FF)
74 #define DISTANCE_WIDTH_DECODER_DURATION_ARRAY_SIZE 50 // To save program space, the decoder only accepts mark or space durations up to 50 * 50 (MICROS_PER_TICK) = 2500 microseconds
76 #define DISTANCE_WIDTH_DECODER_DURATION_ARRAY_SIZE 200 // The decoder accepts mark or space durations up to 200 * 50 (MICROS_PER_TICK) = 10 milliseconds
122 #if defined(LOCAL_DEBUG)
123 void printDurations(uint8_t aArray[], uint8_t aMaxIndex) {
124 for (uint_fast8_t i = 0; i <= aMaxIndex; i++) {
133 Serial.print(F(
": "));
136 Serial.print(aArray[i]);
137 if (aArray[i] != 0) {
141 Serial.print(F(
" | "));
153 uint16_t tWeightedSum = 0;
154 uint8_t tGapCount = 0;
155 for (uint_fast8_t i = 0; i <= aMaxIndex; i++) {
156 uint8_t tCurrentDurations = aArray[i];
157 if (tCurrentDurations != 0) {
159 tSum += tCurrentDurations;
160 tWeightedSum += (tCurrentDurations * i);
166 if (tSum != 0 && (i == aMaxIndex || tGapCount > 1)) {
170 uint8_t tAggregateIndex = (tWeightedSum + (tSum / 2)) / tSum;
171 aArray[tAggregateIndex] = tSum;
173 if (*aShortIndex == 0) {
174 *aShortIndex = tAggregateIndex;
175 }
else if (*aLongIndex == 0) {
176 *aLongIndex = tAggregateIndex;
213 #if defined(DEBUG) || defined(SHOW_DISTANCE_WIDTH_DECODER_ERRORS)
214 Serial.print(F(
"PULSE_DISTANCE_WIDTH: "));
215 Serial.print(F(
"Data length="));
217 Serial.println(F(
" is less than 18"));
225 uint8_t tIndexOfMaxDuration = 0;
232 tDurationArray[tDurationTicks]++;
233 if (tIndexOfMaxDuration < tDurationTicks) {
234 tIndexOfMaxDuration = tDurationTicks;
237 #if defined(LOCAL_DEBUG) || defined(SHOW_DISTANCE_WIDTH_DECODER_ERRORS)
238 Serial.print(F(
"PULSE_DISTANCE_WIDTH: "));
239 Serial.print(F(
"Mark "));
241 Serial.print(F(
" is longer than maximum "));
243 Serial.print(F(
" us. Index="));
253 uint8_t tMarkTicksShort = 0;
254 uint8_t tMarkTicksLong = 0;
255 bool tSuccess =
aggregateArrayCounts(tDurationArray, tIndexOfMaxDuration, &tMarkTicksShort, &tMarkTicksLong);
256 #if defined(LOCAL_DEBUG)
257 Serial.println(F(
"Mark:"));
258 printDurations(tDurationArray, tIndexOfMaxDuration);
262 #if defined(LOCAL_DEBUG) || defined(SHOW_DISTANCE_WIDTH_DECODER_ERRORS)
263 Serial.print(F(
"PULSE_DISTANCE_WIDTH: "));
264 Serial.println(F(
"Mark aggregation failed, more than 2 distinct mark duration values found"));
275 tIndexOfMaxDuration = 0;
279 tDurationArray[tDurationTicks]++;
280 if (tIndexOfMaxDuration < tDurationTicks) {
281 tIndexOfMaxDuration = tDurationTicks;
284 #if defined(LOCAL_DEBUG) || defined(SHOW_DISTANCE_WIDTH_DECODER_ERRORS)
285 Serial.print(F(
"PULSE_DISTANCE_WIDTH: "));
286 Serial.print(F(
"Space "));
288 Serial.print(F(
" is longer than maximum "));
290 Serial.print(F(
" us. Index="));
300 uint8_t tSpaceTicksShort = 0;
301 uint8_t tSpaceTicksLong = 0;
302 tSuccess =
aggregateArrayCounts(tDurationArray, tIndexOfMaxDuration, &tSpaceTicksShort, &tSpaceTicksLong);
303 #if defined(LOCAL_DEBUG)
304 Serial.println(F(
"Space:"));
305 printDurations(tDurationArray, tIndexOfMaxDuration);
309 #if defined(LOCAL_DEBUG) || defined(SHOW_DISTANCE_WIDTH_DECODER_ERRORS)
310 Serial.print(F(
"PULSE_DISTANCE_WIDTH: "));
311 Serial.println(F(
"Space aggregation failed, more than 2 distinct space duration values found"));
316 #if RAW_BUFFER_LENGTH <= (512 -4)
317 uint_fast8_t tNumberOfBits;
319 uint16_t tNumberOfBits;
322 if (tSpaceTicksLong > 0) {
341 #if defined(LOCAL_DEBUG)
342 Serial.print(F(
"DistanceWidthTimingInfoStruct: "));
343 Serial.print(tNumberOfBits);
344 Serial.print(F(
", "));
346 Serial.print(F(
", "));
348 Serial.print(F(
", "));
349 if(tMarkTicksLong == 0) {
354 Serial.print(F(
", "));
355 if(tSpaceTicksLong == 0) {
360 Serial.print(F(
", "));
362 Serial.print(F(
", "));
375 if (tMarkTicksLong == 0 && tSpaceTicksLong == 0) {
376 #if defined(LOCAL_DEBUG)
377 Serial.print(F(
"PULSE_DISTANCE: "));
378 Serial.println(F(
"Cannot decode, because there is only one distinct duration value for each space and mark"));
382 unsigned int tSpaceMicrosShort;
383 #if defined(LOCAL_DEBUG)
384 if(tMarkTicksLong > 0 && tSpaceTicksLong > 0) {
385 Serial.println(F(
"PULSE_DISTANCE_WIDTH signal found"));
394 for (uint_fast8_t i = 0; i <= tNumberOfAdditionalArrayValues; ++i) {
395 uint8_t tNumberOfBitsForOneDecode = tNumberOfBits;
402 if (tSpaceTicksLong > 0) {
408 #if defined(USE_THRESHOLD_DECODER)
415 #
if defined(USE_MSB_DECODING_FOR_DISTANCE_DECODER)
429 #if defined(USE_THRESHOLD_DECODER)
436 #
if defined(USE_MSB_DECODING_FOR_DISTANCE_DECODER)
444 #if defined(LOCAL_DEBUG)
445 Serial.print(F(
"PULSE_WIDTH: "));
446 Serial.print(F(
"decodedRawData=0x"));
455 #if defined(USE_MSB_DECODING_FOR_DISTANCE_DECODER)
461 &&
decodedIRData.decodedRawDataArray[tNumberOfAdditionalArrayValues] == lastDecodedRawData) {
462 decodedIRData.flags |= IRDATA_FLAGS_IS_REPEAT;
471 decodedIRData.DistanceWidthTimingInfo.ZeroMarkMicros = tMarkMicrosShort;
472 decodedIRData.DistanceWidthTimingInfo.ZeroSpaceMicros = tSpaceMicrosShort;
473 if (tMarkMicrosLong != 0) {
474 if (tSpaceMicrosLong == 0) {
476 decodedIRData.DistanceWidthTimingInfo.OneMarkMicros = tMarkMicrosLong;
477 decodedIRData.DistanceWidthTimingInfo.OneSpaceMicros = tSpaceMicrosShort;
481 decodedIRData.DistanceWidthTimingInfo.OneSpaceMicros = tSpaceMicrosLong;
482 decodedIRData.DistanceWidthTimingInfo.OneMarkMicros = tMarkMicrosShort;
483 decodedIRData.DistanceWidthTimingInfo.ZeroMarkMicros = tMarkMicrosLong;
492 decodedIRData.DistanceWidthTimingInfo.OneMarkMicros = tMarkMicrosShort;
493 decodedIRData.DistanceWidthTimingInfo.OneSpaceMicros = tSpaceMicrosLong;
496 #if defined(LOCAL_DEBUG)
497 Serial.print(F(
"DistanceWidthTimingInfo="));
505 #if defined(LOCAL_DEBUG)
508 #endif // _IR_DISTANCE_WIDTH_HPP