55 #ifndef _IR_DISTANCE_WIDTH_HPP
56 #define _IR_DISTANCE_WIDTH_HPP
58 #if !defined(DISTANCE_WIDTH_MAXIMUM_REPEAT_DISTANCE_MICROS)
59 #define DISTANCE_WIDTH_MAXIMUM_REPEAT_DISTANCE_MICROS 100000 // 100 ms, bit it is just a guess
62 #if defined(DEBUG) && !defined(LOCAL_DEBUG)
69 #define DURATION_ARRAY_SIZE 50
85 #if defined(LOCAL_DEBUG)
86 void printDurations(uint8_t aArray[], uint8_t aMaxIndex) {
87 for (uint_fast8_t i = 0; i <= aMaxIndex; i++) {
96 Serial.print(F(
": "));
99 Serial.print(aArray[i]);
100 if (aArray[i] != 0) {
104 Serial.print(F(
" | "));
115 uint16_t tWeightedSum = 0;
116 for (uint_fast8_t i = 0; i <= aMaxIndex; i++) {
117 uint8_t tCurrentDurations = aArray[i];
118 if (tCurrentDurations != 0) {
120 tSum += tCurrentDurations;
121 tWeightedSum += (tCurrentDurations * i);
124 if ((tCurrentDurations == 0 || i == aMaxIndex) && tSum != 0) {
126 uint8_t tAggregateIndex = (tWeightedSum + (tSum / 2)) / tSum;
127 aArray[tAggregateIndex] = tSum;
129 if (*aShortIndex == 0) {
130 *aShortIndex = tAggregateIndex;
131 }
else if (*aLongIndex == 0) {
132 *aLongIndex = tAggregateIndex;
171 uint8_t tIndexOfMaxDuration = 0;
178 tDurationArray[tDurationTicks]++;
179 if (tIndexOfMaxDuration < tDurationTicks) {
180 tIndexOfMaxDuration = tDurationTicks;
183 #if defined(LOCAL_DEBUG)
184 Serial.print(F(
"PULSE_DISTANCE_WIDTH: "));
185 Serial.print(F(
"Mark "));
187 Serial.print(F(
" is longer than maximum "));
189 Serial.print(F(
" us. Index="));
199 uint8_t tMarkTicksShort = 0;
200 uint8_t tMarkTicksLong = 0;
201 bool tSuccess =
aggregateArrayCounts(tDurationArray, tIndexOfMaxDuration, &tMarkTicksShort, &tMarkTicksLong);
202 #if defined(LOCAL_DEBUG)
203 Serial.println(F(
"Mark:"));
204 printDurations(tDurationArray, tIndexOfMaxDuration);
208 #if defined(LOCAL_DEBUG)
209 Serial.print(F(
"PULSE_DISTANCE_WIDTH: "));
210 Serial.println(F(
"Mark aggregation failed, more than 2 distinct mark duration values found"));
221 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(
"Space "));
234 Serial.print(F(
" is longer than maximum "));
236 Serial.print(F(
" us. Index="));
246 uint8_t tSpaceTicksShort = 0;
247 uint8_t tSpaceTicksLong = 0;
248 tSuccess =
aggregateArrayCounts(tDurationArray, tIndexOfMaxDuration, &tSpaceTicksShort, &tSpaceTicksLong);
249 #if defined(LOCAL_DEBUG)
250 Serial.println(F(
"Space:"));
251 printDurations(tDurationArray, tIndexOfMaxDuration);
255 #if defined(LOCAL_DEBUG)
256 Serial.print(F(
"PULSE_DISTANCE_WIDTH: "));
257 Serial.println(F(
"Space aggregation failed, more than 2 distinct space duration values found"));
273 #if defined(LOCAL_DEBUG)
274 Serial.print(F(
"DistanceWidthTimingInfoStruct: "));
276 Serial.print(F(
", "));
278 Serial.print(F(
", "));
280 Serial.print(F(
", "));
282 Serial.print(F(
", "));
284 Serial.print(F(
", "));
287 uint8_t tStartIndex = 3;
290 if (tSpaceTicksLong > 0 && tMarkTicksLong == 0) {
305 if (tMarkTicksLong == 0 && tSpaceTicksLong == 0) {
306 #if defined(LOCAL_DEBUG)
307 Serial.print(F(
"PULSE_DISTANCE: "));
308 Serial.println(F(
"Only 1 distinct duration value for each space and mark found"));
312 unsigned int tSpaceMicrosShort;
313 #if defined DECODE_STRICT_CHECKS
314 if(tMarkTicksLong > 0 && tSpaceTicksLong > 0) {
316 tSpaceMicrosShort = 0;
324 for (uint_fast8_t i = 0; i <= tNumberOfAdditionalArrayValues; ++i) {
325 uint8_t tNumberOfBitsForOneDecode = tNumberOfBits;
333 if (tMarkTicksLong > 0) {
339 tSpaceMicrosShort, 0,
340 #
if defined(USE_MSB_DECODING_FOR_DISTANCE_DECODER)
352 tSpaceMicrosLong, tSpaceMicrosShort,
353 #
if defined(USE_MSB_DECODING_FOR_DISTANCE_DECODER)
361 #if defined(LOCAL_DEBUG)
362 Serial.print(F(
"PULSE_WIDTH: "));
363 Serial.println(F(
"Decode failed"));
367 #if defined(LOCAL_DEBUG)
368 Serial.print(F(
"PULSE_WIDTH: "));
369 Serial.print(F(
"decodedRawData=0x"));
378 #if defined(USE_MSB_DECODING_FOR_DISTANCE_DECODER)
390 decodedIRData.DistanceWidthTimingInfo.ZeroMarkMicros = tMarkMicrosShort;
391 decodedIRData.DistanceWidthTimingInfo.ZeroSpaceMicros = tSpaceMicrosShort;
392 if (tMarkMicrosLong != 0) {
393 decodedIRData.DistanceWidthTimingInfo.OneMarkMicros = tMarkMicrosLong;
395 decodedIRData.DistanceWidthTimingInfo.OneSpaceMicros = tSpaceMicrosShort;
396 if (tSpaceMicrosLong != 0) {
398 decodedIRData.DistanceWidthTimingInfo.ZeroSpaceMicros = tSpaceMicrosLong;
401 decodedIRData.DistanceWidthTimingInfo.OneMarkMicros = tMarkMicrosShort;
404 decodedIRData.DistanceWidthTimingInfo.OneSpaceMicros = tSpaceMicrosLong;
407 #if defined(LOCAL_DEBUG)
408 Serial.print(F(
"DistanceWidthTimingInfo="));
416 #if defined(LOCAL_DEBUG)
419 #endif // _IR_DISTANCE_WIDTH_HPP