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
65 #if defined(DEBUG) && !defined(LOCAL_DEBUG)
71 #if !defined(DISTANCE_WIDTH_DECODER_DURATION_ARRAY_SIZE)
72 # if (defined(RAMEND) && RAMEND <= 0x8FF) || (defined(RAMSIZE) && RAMSIZE < 0x8FF)
73 #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
75 #define DISTANCE_WIDTH_DECODER_DURATION_ARRAY_SIZE 200 // The decoder accepts mark or space durations up to 200 * 50 (MICROS_PER_TICK) = 10 milliseconds
121 #if defined(LOCAL_DEBUG)
122 void printDurations(uint8_t aArray[], uint8_t aMaxIndex) {
123 for (uint_fast8_t i = 0; i <= aMaxIndex; i++) {
132 Serial.print(F(
": "));
135 Serial.print(aArray[i]);
136 if (aArray[i] != 0) {
140 Serial.print(F(
" | "));
152 uint16_t tWeightedSum = 0;
153 uint8_t tGapCount = 0;
154 for (uint_fast8_t i = 0; i <= aMaxIndex; i++) {
155 uint8_t tCurrentDurations = aArray[i];
156 if (tCurrentDurations != 0) {
158 tSum += tCurrentDurations;
159 tWeightedSum += (tCurrentDurations * i);
165 if (tSum != 0 && (i == aMaxIndex || tGapCount > 1)) {
169 uint8_t tAggregateIndex = (tWeightedSum + (tSum / 2)) / tSum;
170 aArray[tAggregateIndex] = tSum;
172 if (*aShortIndex == 0) {
173 *aShortIndex = tAggregateIndex;
174 }
else if (*aLongIndex == 0) {
175 *aLongIndex = tAggregateIndex;
218 uint8_t tIndexOfMaxDuration = 0;
225 tDurationArray[tDurationTicks]++;
226 if (tIndexOfMaxDuration < tDurationTicks) {
227 tIndexOfMaxDuration = tDurationTicks;
230 #if defined(LOCAL_DEBUG)
231 Serial.print(F(
"PULSE_DISTANCE_WIDTH: "));
232 Serial.print(F(
"Mark "));
234 Serial.print(F(
" is longer than maximum "));
236 Serial.print(F(
" us. Index="));
246 uint8_t tMarkTicksShort = 0;
247 uint8_t tMarkTicksLong = 0;
248 bool tSuccess =
aggregateArrayCounts(tDurationArray, tIndexOfMaxDuration, &tMarkTicksShort, &tMarkTicksLong);
249 #if defined(LOCAL_DEBUG)
250 Serial.println(F(
"Mark:"));
251 printDurations(tDurationArray, tIndexOfMaxDuration);
255 #if defined(LOCAL_DEBUG)
256 Serial.print(F(
"PULSE_DISTANCE_WIDTH: "));
257 Serial.println(F(
"Mark aggregation failed, more than 2 distinct mark duration values found"));
268 tIndexOfMaxDuration = 0;
272 tDurationArray[tDurationTicks]++;
273 if (tIndexOfMaxDuration < tDurationTicks) {
274 tIndexOfMaxDuration = tDurationTicks;
277 #if defined(LOCAL_DEBUG)
278 Serial.print(F(
"PULSE_DISTANCE_WIDTH: "));
279 Serial.print(F(
"Space "));
281 Serial.print(F(
" is longer than maximum "));
283 Serial.print(F(
" us. Index="));
293 uint8_t tSpaceTicksShort = 0;
294 uint8_t tSpaceTicksLong = 0;
295 tSuccess =
aggregateArrayCounts(tDurationArray, tIndexOfMaxDuration, &tSpaceTicksShort, &tSpaceTicksLong);
296 #if defined(LOCAL_DEBUG)
297 Serial.println(F(
"Space:"));
298 printDurations(tDurationArray, tIndexOfMaxDuration);
302 #if defined(LOCAL_DEBUG)
303 Serial.print(F(
"PULSE_DISTANCE_WIDTH: "));
304 Serial.println(F(
"Space aggregation failed, more than 2 distinct space duration values found"));
322 #if defined(LOCAL_DEBUG)
323 Serial.print(F(
"DistanceWidthTimingInfoStruct: "));
325 Serial.print(F(
", "));
327 Serial.print(F(
", "));
329 Serial.print(F(
", "));
331 Serial.print(F(
", "));
333 Serial.print(F(
", "));
336 #if RAW_BUFFER_LENGTH <= (512 -4)
337 uint_fast8_t tNumberOfBits;
339 uint16_t tNumberOfBits;
342 if (tSpaceTicksLong > 0) {
356 if (tMarkTicksLong == 0 && tSpaceTicksLong == 0) {
357 #if defined(LOCAL_DEBUG)
358 Serial.print(F(
"PULSE_DISTANCE: "));
359 Serial.println(F(
"Only 1 distinct duration value for each space and mark found"));
363 unsigned int tSpaceMicrosShort;
364 #if defined DECODE_STRICT_CHECKS
365 if(tMarkTicksLong > 0 && tSpaceTicksLong > 0) {
367 tSpaceMicrosShort = 0;
376 for (uint_fast8_t i = 0; i <= tNumberOfAdditionalArrayValues; ++i) {
377 uint8_t tNumberOfBitsForOneDecode = tNumberOfBits;
385 if (tSpaceTicksLong > 0) {
393 #
if defined(USE_MSB_DECODING_FOR_DISTANCE_DECODER)
407 #
if defined(USE_MSB_DECODING_FOR_DISTANCE_DECODER)
416 #if defined(LOCAL_DEBUG)
417 Serial.print(F(
"PULSE_WIDTH: "));
418 Serial.println(F(
"Decode failed"));
422 #if defined(LOCAL_DEBUG)
423 Serial.print(F(
"PULSE_WIDTH: "));
424 Serial.print(F(
"decodedRawData=0x"));
433 #if defined(USE_MSB_DECODING_FOR_DISTANCE_DECODER)
445 decodedIRData.DistanceWidthTimingInfo.ZeroMarkMicros = tMarkMicrosShort;
446 decodedIRData.DistanceWidthTimingInfo.ZeroSpaceMicros = tSpaceMicrosShort;
447 if (tMarkMicrosLong != 0) {
448 if (tSpaceMicrosLong == 0) {
450 decodedIRData.DistanceWidthTimingInfo.OneMarkMicros = tMarkMicrosLong;
451 decodedIRData.DistanceWidthTimingInfo.OneSpaceMicros = tSpaceMicrosShort;
455 decodedIRData.DistanceWidthTimingInfo.OneSpaceMicros = tSpaceMicrosLong;
456 decodedIRData.DistanceWidthTimingInfo.OneMarkMicros = tMarkMicrosShort;
457 decodedIRData.DistanceWidthTimingInfo.ZeroMarkMicros = tMarkMicrosLong;
466 decodedIRData.DistanceWidthTimingInfo.OneMarkMicros = tMarkMicrosShort;
467 decodedIRData.DistanceWidthTimingInfo.OneSpaceMicros = tSpaceMicrosLong;
470 #if defined(LOCAL_DEBUG)
471 Serial.print(F(
"DistanceWidthTimingInfo="));
479 #if defined(LOCAL_DEBUG)
482 #endif // _IR_DISTANCE_WIDTH_HPP