55 #ifndef _IR_DISTANCE_WIDTH_HPP
56 #define _IR_DISTANCE_WIDTH_HPP
58 #if defined(DEBUG) && !defined(LOCAL_DEBUG)
65 #define DURATION_ARRAY_SIZE 50
74 #if defined(LOCAL_DEBUG)
75 void printDurations(uint8_t aArray[], uint8_t aMaxIndex) {
76 for (uint_fast8_t i = 0; i <= aMaxIndex; i++) {
84 Serial.print(F(
": "));
86 Serial.print(aArray[i]);
91 Serial.print(F(
" | "));
102 uint16_t tWeightedSum = 0;
103 for (uint_fast8_t i = 0; i <= aMaxIndex; i++) {
104 uint8_t tCurrentDurations = aArray[i];
105 if (tCurrentDurations != 0) {
107 tSum += tCurrentDurations;
108 tWeightedSum += (tCurrentDurations * i);
111 if ((tCurrentDurations == 0 || i == aMaxIndex) && tSum != 0) {
113 uint8_t tAggregateIndex = (tWeightedSum + (tSum / 2)) / tSum;
114 aArray[tAggregateIndex] = tSum;
116 if (*aShortIndex == 0) {
117 *aShortIndex = tAggregateIndex;
118 }
else if (*aLongIndex == 0) {
119 *aLongIndex = tAggregateIndex;
158 uint8_t tIndexOfMaxDuration = 0;
165 tDurationArray[tDurationTicks]++;
166 if (tIndexOfMaxDuration < tDurationTicks) {
167 tIndexOfMaxDuration = tDurationTicks;
170 #if defined(LOCAL_DEBUG)
171 Serial.print(F(
"PULSE_DISTANCE_WIDTH: "));
172 Serial.print(F(
"Mark "));
174 Serial.print(F(
" is longer than "));
176 Serial.print(F(
" us. Index="));
186 uint8_t tMarkTicksShort = 0;
187 uint8_t tMarkTicksLong = 0;
188 bool tSuccess =
aggregateArrayCounts(tDurationArray, tIndexOfMaxDuration, &tMarkTicksShort, &tMarkTicksLong);
189 #if defined(LOCAL_DEBUG)
190 Serial.println(F(
"Mark:"));
191 printDurations(tDurationArray, tIndexOfMaxDuration);
195 #if defined(LOCAL_DEBUG)
196 Serial.print(F(
"PULSE_DISTANCE_WIDTH: "));
197 Serial.println(F(
"Mark aggregation failed, more than 2 distinct mark duration values found"));
208 tIndexOfMaxDuration = 0;
212 tDurationArray[tDurationTicks]++;
213 if (tIndexOfMaxDuration < tDurationTicks) {
214 tIndexOfMaxDuration = tDurationTicks;
217 #if defined(LOCAL_DEBUG)
218 Serial.print(F(
"PULSE_DISTANCE_WIDTH: "));
219 Serial.print(F(
"Space "));
221 Serial.print(F(
" is longer than "));
223 Serial.print(F(
" us. Index="));
233 uint8_t tSpaceTicksShort = 0;
234 uint8_t tSpaceTicksLong = 0;
235 tSuccess =
aggregateArrayCounts(tDurationArray, tIndexOfMaxDuration, &tSpaceTicksShort, &tSpaceTicksLong);
236 #if defined(LOCAL_DEBUG)
237 Serial.println(F(
"Space:"));
238 printDurations(tDurationArray, tIndexOfMaxDuration);
242 #if defined(LOCAL_DEBUG)
243 Serial.print(F(
"PULSE_DISTANCE_WIDTH: "));
244 Serial.println(F(
"Space aggregation failed, more than 2 distinct space duration values found"));
260 #if defined(LOCAL_DEBUG)
261 Serial.print(F(
"ProtocolConstants: "));
263 Serial.print(F(
", "));
265 Serial.print(F(
", "));
267 Serial.print(F(
", "));
269 Serial.print(F(
", "));
270 if (tMarkTicksLong == 0) {
275 Serial.print(F(
", "));
279 uint8_t tStartIndex = 3;
282 if (tSpaceTicksLong > 0 && tMarkTicksLong == 0) {
287 #if __INT_WIDTH__ < 32
288 uint8_t tNumberOfAdditionalArrayValues = (tNumberOfBits - 1) / 32;
290 uint8_t tNumberOfAdditionalArrayValues = (tNumberOfBits - 1) / 64;
301 if (tMarkTicksLong == 0 && tSpaceTicksLong == 0) {
302 #if defined(LOCAL_DEBUG)
303 Serial.print(F(
"PULSE_DISTANCE: "));
304 Serial.println(F(
"Only 1 distinct duration value for each space and mark found"));
308 #if defined DECODE_STRICT_CHECKS
309 if(tMarkTicksLong > 0 && tSpaceTicksLong > 0) {
311 tSpaceTicksShort = 0;
315 for (uint_fast8_t i = 0; i <= tNumberOfAdditionalArrayValues; ++i) {
316 uint8_t tNumberOfBitsForOneDecode = tNumberOfBits;
320 #if __INT_WIDTH__ < 32
321 if (tNumberOfBitsForOneDecode > 32) {
322 tNumberOfBitsForOneDecode = 32;
325 if (tNumberOfBitsForOneDecode > 64) {
326 tNumberOfBitsForOneDecode = 64;
330 if (tMarkTicksLong > 0) {
337 #
if defined(DISTANCE_DO_MSB_DECODING)
350 #
if defined(DISTANCE_DO_MSB_DECODING)
358 #if defined(LOCAL_DEBUG)
359 Serial.print(F(
"PULSE_WIDTH: "));
360 Serial.println(F(
"Decode failed"));
364 #if defined(LOCAL_DEBUG)
365 Serial.print(F(
"PULSE_WIDTH: "));
366 Serial.print(F(
"decodedRawData=0x"));
371 #if __INT_WIDTH__ < 32
380 #if defined(DISTANCE_DO_MSB_DECODING)
387 if (tMarkTicksLong == 0) {
398 #if defined(LOCAL_DEBUG)
401 #endif // _IR_DISTANCE_WIDTH_HPP