IRremote
ir_DistanceWidthProtocol.hpp
Go to the documentation of this file.
1 /*
2  * ir_DistanceWidthProtocol.hpp
3  *
4  * Contains only the decoder functions for universal pulse width or pulse distance protocols!
5  * The send functions are used by almost all protocols and are therefore located in IRSend.hpp.
6  *
7  * If RAM is not more than 2k, the decoder only accepts mark or space durations up to 50 * 50 (MICROS_PER_TICK) = 2500 microseconds
8  * to save RAM space, otherwise it accepts durations up to 10 ms.
9  *
10  * This decoder tries to decode a pulse distance or pulse distance width with constant period (or pulse width - not enabled yet) protocol.
11  * 1. Analyze all space and mark length
12  * 2. Decide which protocol we have
13  * 3. Try to decode with the mark and space data found in step 1
14  * 4. Assume one start bit / header and one stop bit, since pulse distance data must have a stop bit!
15  * No data and address decoding, only raw data as result.
16  *
17  * Pulse distance data can be sent with the generic function as in SendDemo example line 155:
18  * https://github.com/Arduino-IRremote/Arduino-IRremote/blob/d51b540cb2ddf1424888d2d9e6b62fe1ef46859d/examples/SendDemo/SendDemo.ino#L155
19  * void sendPulseDistanceWidthData(unsigned int aOneMarkMicros, unsigned int aOneSpaceMicros, unsigned int aZeroMarkMicros,
20  * unsigned int aZeroSpaceMicros, uint32_t aData, uint8_t aNumberOfBits, bool aMSBfirst, bool aSendStopBit = false)
21  * The header must be sent manually with:
22  * IrSender.mark(MarkMicros)
23  * IrSender.space(SpaceMicros);
24  *
25  * Or send it by filling a DecodedRawDataArray and with the sendPulseDistanceWidthFromArray() function as in SendDemo example line 175:
26  * https://github.com/Arduino-IRremote/Arduino-IRremote/blob/d51b540cb2ddf1424888d2d9e6b62fe1ef46859d/examples/SendDemo/SendDemo.ino#L175
27  * sendPulseDistanceWidthFromArray(uint_fast8_t aFrequencyKHz, unsigned int aHeaderMarkMicros,
28  * unsigned int aHeaderSpaceMicros, unsigned int aOneMarkMicros, unsigned int aOneSpaceMicros, unsigned int aZeroMarkMicros,
29  * unsigned int aZeroSpaceMicros, uint32_t *aDecodedRawDataArray, unsigned int aNumberOfBits, uint8_t aFlags,
30  * unsigned int aRepeatPeriodMillis, int_fast8_t aNumberOfRepeats)
31  *
32  * This file is part of Arduino-IRremote https://github.com/Arduino-IRremote/Arduino-IRremote.
33  *
34  ************************************************************************************
35  * MIT License
36  *
37  * Copyright (c) 2022-2025 Armin Joachimsmeyer
38  *
39  * Permission is hereby granted, free of charge, to any person obtaining a copy
40  * of this software and associated documentation files (the "Software"), to deal
41  * in the Software without restriction, including without limitation the rights
42  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
43  * copies of the Software, and to permit persons to whom the Software is furnished
44  * to do so, subject to the following conditions:
45  *
46  * The above copyright notice and this permission notice shall be included in all
47  * copies or substantial portions of the Software.
48  *
49  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
50  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
51  * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
52  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
53  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
54  * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
55  *
56  ************************************************************************************
57  */
58 #ifndef _IR_DISTANCE_WIDTH_HPP
59 #define _IR_DISTANCE_WIDTH_HPP
60 
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
63 #endif
64 
65 #if defined(DEBUG)
66 #define LOCAL_DEBUG
67 #else
68 //#define LOCAL_DEBUG // This enables debug output only for this file
69 #endif
70 //#define SHOW_DISTANCE_WIDTH_DECODER_ERRORS // Prints errors which prevents data to be decoded as distance width data
71 
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
75 # else
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
77 # endif
78 #endif
79 
80 // Switch the decoding according to your needs
81 //#define USE_MSB_DECODING_FOR_DISTANCE_DECODER // If active, it resembles LG, otherwise LSB first as most other protocols e.g. NEC and Kaseikyo/Panasonic
82 
86 //=====================================================================================
87 // DDD III SSS TTTTTT AA N N CCC EEEE W W III DDD TTTTTT H H
88 // D D I S TT A A NN N C E W W I D D TT H H
89 // D D I SSS TT AAAA N N N C EEE W W W I D D TT HHHH
90 // D D I S TT A A N NN C E W W W I D D TT H H
91 // DDD III SSSS TT A A N N CCC EEEE W W III DDD TT H H
92 //=====================================================================================
93 // see: https://www.mikrocontroller.net/articles/IRMP_-_english#Codings
94 /*
95  Example output of UnitTest.ino for PulseWidth protocol:
96  Protocol=PulseWidth Raw-Data=0x87654321 32 bits LSB first
97  Send on a 8 bit platform with: IrSender.sendPulseDistanceWidth(38, 950, 550, 600, 300, 300, 300, 0x87654321, 32, PROTOCOL_IS_LSB_FIRST, <RepeatPeriodMillis>, <numberOfRepeats>);
98  rawData[66]:
99  -1088600
100  + 950,- 550
101  + 600,- 300 + 300,- 300 + 350,- 250 + 350,- 250
102  + 350,- 300 + 600,- 300 + 300,- 300 + 300,- 300
103  + 650,- 250 + 650,- 250 + 300,- 300 + 350,- 250
104  + 350,- 300 + 300,- 300 + 600,- 300 + 300,- 300
105  + 600,- 300 + 350,- 250 + 600,- 300 + 350,- 250
106  + 350,- 300 + 600,- 300 + 600,- 300 + 300,- 300
107  + 600,- 300 + 600,- 300 + 600,- 300 + 300,- 300
108  + 300,- 300 + 300,- 300 + 350,- 250 + 650
109  Sum: 24500
110 
111  Example output of UnitTest.ino for PulseDistanceWidth protocol:
112  Protocol=PulseDistance Raw-Data=0x76 7 bits LSB first
113  Send on a 8 bit platform with: IrSender.sendPulseDistanceWidth(38, 5950, 500, 550, 1450, 1550, 500, 0x76, 7, PROTOCOL_IS_LSB_FIRST, <RepeatPeriodMillis>, <numberOfRepeats>);
114  rawData[18]:
115  -1092450
116  +5950,- 500
117  +1500,- 500 + 500,-1450 + 550,-1450 +1550,- 450
118  + 550,-1450 + 550,-1450 + 550,-1450 + 550
119  Sum: 20950
120  */
121 
122 #if defined(LOCAL_DEBUG)
123 void printDurations(uint8_t aArray[], uint8_t aMaxIndex) {
124  for (uint_fast8_t i = 0; i <= aMaxIndex; i++) {
125  //Print index at the beginning of a new line
126  if (i % 10 == 0) {
127  if (i == 0) {
128  Serial.print(' '); // indentation for the first index 0
129  } else {
130  Serial.println(); // new line for next indexes 10, 20 etc.
131  }
132  Serial.print(i);
133  Serial.print(F(": "));
134  }
135  // Print number of values in array and duration if != 0
136  Serial.print(aArray[i]);
137  if (aArray[i] != 0) {
138  Serial.print('x');
139  Serial.print(i * (uint16_t) MICROS_PER_TICK);
140  }
141  Serial.print(F(" | "));
142  }
143  Serial.println();
144 }
145 #endif
146 
147 /*
148  * We count all consecutive (allow only one gap between) durations and compute the average.
149  * @return false if more than 2 distinct duration values found
150  */
151 bool aggregateArrayCounts(uint8_t aArray[], uint8_t aMaxIndex, uint8_t *aShortIndex, uint8_t *aLongIndex) {
152  uint8_t tSum = 0;
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) {
158  // Add it to sum and remove array content
159  tSum += tCurrentDurations;
160  tWeightedSum += (tCurrentDurations * i);
161  aArray[i] = 0;
162  tGapCount = 0;
163  } else {
164  tGapCount++;
165  }
166  if (tSum != 0 && (i == aMaxIndex || tGapCount > 1)) {
167  /*
168  * Here we have a sum AND last element OR more than 1 consecutive gap
169  */
170  uint8_t tAggregateIndex = (tWeightedSum + (tSum / 2)) / tSum; // with rounding
171  aArray[tAggregateIndex] = tSum; // disabling this line increases code size by 2 - unbelievable!
172  // store aggregate for later decoding
173  if (*aShortIndex == 0) {
174  *aShortIndex = tAggregateIndex;
175  } else if (*aLongIndex == 0) {
176  *aLongIndex = tAggregateIndex;
177  } else {
178  // we have 3 bins => this is likely no pulse width or distance protocol. e.g. it can be RC5.
179  return false;
180  }
181  // initialize for next aggregation
182  tSum = 0;
183  tWeightedSum = 0;
184  }
185  }
186  return true;
187 }
188 
189 /*
190  * Try to decode a pulse distance or pulse width protocol.
191  * 1. Analyze all space and mark length
192  * 2. Decide if we have an pulse width or distance protocol
193  * 3. Try to decode with the mark and space data found in step 1
194  * No data and address decoding, only raw data as result.
195  *
196  * Restrictions:
197  * Only protocols with at least 7 bits (+ start and trailing stop bit) are accepted.
198  * Pulse or pause duration must be below 2500 us (depends on DISTANCE_WIDTH_DECODER_DURATION_ARRAY_SIZE).
199  *
200  * calloc() version is 700 bytes larger :-(
201  */
203  /*
204  * Array for up to 49 ticks / 2500 us (or 199 ticks / 10 ms us if RAM > 2k)
205  * tick array index 0 covers mark or space durations from 0 to 49 us, and index 49 from 2450 to 2499 us
206  */
207  uint8_t tDurationArray[DISTANCE_WIDTH_DECODER_DURATION_ARRAY_SIZE];
208 
209  /*
210  * Only protocols with at least 7 bits are accepted
211  */
212  if (decodedIRData.rawlen < (2 * 7) + 4) {
213 #if defined(DEBUG) || defined(SHOW_DISTANCE_WIDTH_DECODER_ERRORS)
214  Serial.print(F("PULSE_DISTANCE_WIDTH: "));
215  Serial.print(F("Data length="));
216  Serial.print(decodedIRData.rawlen);
217  Serial.println(F(" is less than 18"));
218 #endif
219  return false;
220  }
221 
222  // Reset duration array
223  memset(tDurationArray, 0, DISTANCE_WIDTH_DECODER_DURATION_ARRAY_SIZE);
224 
225  uint8_t tIndexOfMaxDuration = 0;
226  /*
227  * Count number of mark durations. Skip leading start and trailing stop bit.
228  */
229  for (IRRawlenType i = 3; i < decodedIRData.rawlen - 2; i += 2) {
230  auto tDurationTicks = irparams.rawbuf[i];
231  if (tDurationTicks < DISTANCE_WIDTH_DECODER_DURATION_ARRAY_SIZE) {
232  tDurationArray[tDurationTicks]++; // count duration if less than DISTANCE_WIDTH_DECODER_DURATION_ARRAY_SIZE
233  if (tIndexOfMaxDuration < tDurationTicks) {
234  tIndexOfMaxDuration = tDurationTicks;
235  }
236  } else {
237 #if defined(LOCAL_DEBUG) || defined(SHOW_DISTANCE_WIDTH_DECODER_ERRORS)
238  Serial.print(F("PULSE_DISTANCE_WIDTH: "));
239  Serial.print(F("Mark "));
240  Serial.print(tDurationTicks * MICROS_PER_TICK);
241  Serial.print(F(" is longer than maximum "));
243  Serial.print(F(" us. Index="));
244  Serial.println(i);
245 #endif
246  return false;
247  }
248  }
249 
250  /*
251  * Aggregate mark counts to one duration bin
252  */
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);
259 #endif
260 
261  if (!tSuccess) {
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"));
265 #endif
266  return false;
267  }
268 
269  // Reset duration array
270  memset(tDurationArray, 0, DISTANCE_WIDTH_DECODER_DURATION_ARRAY_SIZE);
271 
272  /*
273  * Count number of space durations. Skip leading start and trailing stop bit.
274  */
275  tIndexOfMaxDuration = 0;
276  for (IRRawlenType i = 4; i < decodedIRData.rawlen - 2; i += 2) {
277  auto tDurationTicks = irparams.rawbuf[i];
278  if (tDurationTicks < DISTANCE_WIDTH_DECODER_DURATION_ARRAY_SIZE) {
279  tDurationArray[tDurationTicks]++;
280  if (tIndexOfMaxDuration < tDurationTicks) {
281  tIndexOfMaxDuration = tDurationTicks;
282  }
283  } else {
284 #if defined(LOCAL_DEBUG) || defined(SHOW_DISTANCE_WIDTH_DECODER_ERRORS)
285  Serial.print(F("PULSE_DISTANCE_WIDTH: "));
286  Serial.print(F("Space "));
287  Serial.print(tDurationTicks * MICROS_PER_TICK);
288  Serial.print(F(" is longer than maximum "));
290  Serial.print(F(" us. Index="));
291  Serial.println(i);
292 #endif
293  return false;
294  }
295  }
296 
297  /*
298  * Aggregate space counts to one duration bin
299  */
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);
306 #endif
307 
308  if (!tSuccess) {
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"));
312 #endif
313  return false;
314  }
315 
316 #if RAW_BUFFER_LENGTH <= (512 -4)
317  uint_fast8_t tNumberOfBits;
318 #else
319  uint16_t tNumberOfBits;
320 #endif
321  tNumberOfBits = (decodedIRData.rawlen / 2) - 1;
322  if (tSpaceTicksLong > 0) {
323  // For PULSE_DISTANCE -including PULSE_DISTANCE_WIDTH- a stop bit is mandatory, for PULSE_WIDTH it is not required!
324  tNumberOfBits--; // Correct for PULSE_DISTANCE stop bit
325  }
326  decodedIRData.numberOfBits = tNumberOfBits;
327 
328  /*
329  * Print characteristics of this protocol. Durations are in (50 us) ticks.
330  * Number of bits, start bit, start pause, long mark, long space, short mark, short space
331  *
332  * NEC: 32, 180, 90, 11, 34, 11, 11
333  * Samsung32: 32, 90, 90, 11, 34, 11, 11
334  * LG: 28, 180, 84, 10, 32, 10, 11
335  * JVC: 16, 168, 84, 10, 32, 10, 10
336  * Kaseikyo: 48. 69, 35, 9, 26, 9, 9
337  * Sony: 12|15|20, 48, 12, 24, 12, 12, 12 // the only known pulse width protocol
338  * Disney monorail
339  * model: 7, 120, 10, 30, 30, 10, 10 // PulseDistanceWidth. Can be seen as direct conversion of a 7 bit serial timing at 250 baud with a 6 ms start bit.
340  */
341 #if defined(LOCAL_DEBUG)
342  Serial.print(F("DistanceWidthTimingInfoStruct: "));
343  Serial.print(tNumberOfBits);
344  Serial.print(F(", "));
345  Serial.print(irparams.rawbuf[1] * MICROS_PER_TICK);
346  Serial.print(F(", "));
347  Serial.print(irparams.rawbuf[2] * MICROS_PER_TICK);
348  Serial.print(F(", "));
349  if(tMarkTicksLong == 0) {
350  Serial.print(tMarkTicksShort * MICROS_PER_TICK);
351  } else {
352  Serial.print(tMarkTicksLong * MICROS_PER_TICK);
353  }
354  Serial.print(F(", "));
355  if(tSpaceTicksLong == 0) {
356  Serial.print(tSpaceTicksShort * MICROS_PER_TICK);
357  } else {
358  Serial.print(tSpaceTicksLong * MICROS_PER_TICK);
359  }
360  Serial.print(F(", "));
361  Serial.print(tMarkTicksShort * MICROS_PER_TICK);
362  Serial.print(F(", "));
363  Serial.println(tSpaceTicksShort * MICROS_PER_TICK);
364 #endif
365 
366  uint8_t tNumberOfAdditionalArrayValues = (tNumberOfBits - 1) / BITS_IN_RAW_DATA_TYPE;
367 
368  /*
369  * We can have the following protocol timings
370  * PULSE_DISTANCE: Pause/spaces have different length and determine the bit value, longer space is 1. Pulses/marks can be constant, like NEC.
371  * PULSE_WIDTH: Pulses/marks have different length and determine the bit value, longer mark is 1. Pause/spaces can be constant, like Sony.
372  * PULSE_DISTANCE_WIDTH: Pulses/marks and pause/spaces have different length, often the bit length is constant, like MagiQuest. Can be decoded by PULSE_DISTANCE approach.
373  */
374 
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"));
379 #endif
380  return false;
381  }
382  unsigned int tSpaceMicrosShort;
383 #if defined(LOCAL_DEBUG)
384  if(tMarkTicksLong > 0 && tSpaceTicksLong > 0) {
385  Serial.println(F("PULSE_DISTANCE_WIDTH signal found"));
386  }
387 #endif
388  tSpaceMicrosShort = tSpaceTicksShort * MICROS_PER_TICK;
389  unsigned int tMarkMicrosShort = tMarkTicksShort * MICROS_PER_TICK;
390  unsigned int tMarkMicrosLong = tMarkTicksLong * MICROS_PER_TICK;
391  unsigned int tSpaceMicrosLong = tSpaceTicksLong * MICROS_PER_TICK;
392  IRRawlenType tStartIndex = 3; // skip leading start bit for decoding.
393 
394  for (uint_fast8_t i = 0; i <= tNumberOfAdditionalArrayValues; ++i) {
395  uint8_t tNumberOfBitsForOneDecode = tNumberOfBits;
396  /*
397  * Decode in 32/64 bit chunks. Only the last chunk can contain less than 32/64 bits
398  */
399  if (tNumberOfBitsForOneDecode > BITS_IN_RAW_DATA_TYPE) {
400  tNumberOfBitsForOneDecode = BITS_IN_RAW_DATA_TYPE;
401  }
402  if (tSpaceTicksLong > 0) {
403  /*
404  * Here short and long space durations found. So we have PULSE_DISTANCE or PULSE_DISTANCE_WIDTH.
405  * PULSE_DISTANCE_WIDTH can successfully be decoded by only using the timings of space :-).
406  */
407  decodedIRData.protocol = PULSE_DISTANCE; // NEC etc. + PULSE_DISTANCE_WIDTH
408 #if defined(USE_THRESHOLD_DECODER)
409  decodeWithThresholdPulseDistanceWidthData(tNumberOfBitsForOneDecode, tStartIndex,
410  ((tSpaceMicrosLong + tSpaceMicrosShort) / 2) + MARK_EXCESS_MICROS, // MARK_EXCESS_MICROS is 0 here if not explicitly specified by user
411 #else
412  decodePulseDistanceWidthData(tNumberOfBitsForOneDecode, tStartIndex, tSpaceMicrosLong,
413 #endif
415 #if defined(USE_MSB_DECODING_FOR_DISTANCE_DECODER)
417 #else
419 #endif
420  );
421 
422  } else {
423  /*
424  * Here no long space duration but short and long mark durations found. So we have PULSE_WIDTH.
425  * Use timing of marks for decoding here.
426  * This else case will most likely never be used, but it only requires 12 bytes additional programming space :-)
427  */
429 #if defined(USE_THRESHOLD_DECODER)
430  decodeWithThresholdPulseDistanceWidthData(tNumberOfBitsForOneDecode, tStartIndex,
431  ((tMarkMicrosLong+ tMarkMicrosShort) / 2) - MARK_EXCESS_MICROS, // MARK_EXCESS_MICROS is 0 here if not explicitly specified by user
432 #else
433  decodePulseDistanceWidthData(tNumberOfBitsForOneDecode, tStartIndex, tMarkMicrosLong,
434 #endif
436 #if defined(USE_MSB_DECODING_FOR_DISTANCE_DECODER)
438 #else
440 #endif
441  );
442 
443  }
444 #if defined(LOCAL_DEBUG)
445  Serial.print(F("PULSE_WIDTH: "));
446  Serial.print(F("decodedRawData=0x"));
447  Serial.println(decodedIRData.decodedRawData, HEX);
448 #endif
449  // fill array with decoded data
450  decodedIRData.decodedRawDataArray[i] = decodedIRData.decodedRawData;
451  tStartIndex += (2 * BITS_IN_RAW_DATA_TYPE);
452  tNumberOfBits -= BITS_IN_RAW_DATA_TYPE;
453  }
454 
455 #if defined(USE_MSB_DECODING_FOR_DISTANCE_DECODER)
457 #endif
458 
459  // Check for repeat. Check also for equality of last DecodedRawData.
461  && decodedIRData.decodedRawDataArray[tNumberOfAdditionalArrayValues] == lastDecodedRawData) {
462  decodedIRData.flags |= IRDATA_FLAGS_IS_REPEAT;
463  }
464  lastDecodedRawData = decodedIRData.decodedRawData;
465 
466  /*
467  * Store timing data to reproduce frame for sending
468  */
469  decodedIRData.DistanceWidthTimingInfo.HeaderMarkMicros = (irparams.rawbuf[1] * MICROS_PER_TICK);
470  decodedIRData.DistanceWidthTimingInfo.HeaderSpaceMicros = (irparams.rawbuf[2] * MICROS_PER_TICK);
471  decodedIRData.DistanceWidthTimingInfo.ZeroMarkMicros = tMarkMicrosShort;
472  decodedIRData.DistanceWidthTimingInfo.ZeroSpaceMicros = tSpaceMicrosShort;
473  if (tMarkMicrosLong != 0) {
474  if (tSpaceMicrosLong == 0) {
475  // PULSE_DISTANCE, Sony
476  decodedIRData.DistanceWidthTimingInfo.OneMarkMicros = tMarkMicrosLong;
477  decodedIRData.DistanceWidthTimingInfo.OneSpaceMicros = tSpaceMicrosShort;
478  } else {
479  // PULSE_DISTANCE_WIDTH, we have 4 distinct values here
480  // Assume long space for a one when we have PulseDistanceWidth like for RS232, where a long inactive period (high) is a 1
481  decodedIRData.DistanceWidthTimingInfo.OneSpaceMicros = tSpaceMicrosLong;
482  decodedIRData.DistanceWidthTimingInfo.OneMarkMicros = tMarkMicrosShort;
483  decodedIRData.DistanceWidthTimingInfo.ZeroMarkMicros = tMarkMicrosLong;
484 // // Assume long mark for a one when we have PulseDistanceWidth
485 // decodedIRData.DistanceWidthTimingInfo.OneSpaceMicros = tSpaceMicrosShort;
486 // decodedIRData.DistanceWidthTimingInfo.ZeroSpaceMicros = tSpaceMicrosLong;
487 // decodedIRData.DistanceWidthTimingInfo.OneMarkMicros = tMarkMicrosLong;
488  }
489  } else {
490  // PULSE_WIDTH, NEC etc.
491  // Here tMarkMicrosLong is 0 => tSpaceMicrosLong != 0
492  decodedIRData.DistanceWidthTimingInfo.OneMarkMicros = tMarkMicrosShort;
493  decodedIRData.DistanceWidthTimingInfo.OneSpaceMicros = tSpaceMicrosLong;
494  }
495 
496 #if defined(LOCAL_DEBUG)
497  Serial.print(F("DistanceWidthTimingInfo="));
498  IrReceiver.printDistanceWidthTimingInfo(&Serial, &decodedIRData.DistanceWidthTimingInfo);
499  Serial.println();
500 #endif
501  return true;
502 }
503 
505 #if defined(LOCAL_DEBUG)
506 #undef LOCAL_DEBUG
507 #endif
508 #endif // _IR_DISTANCE_WIDTH_HPP
MICROS_PER_TICK
#define MICROS_PER_TICK
microseconds per clock interrupt tick
Definition: IRremote.hpp:207
BITS_IN_RAW_DATA_TYPE
#define BITS_IN_RAW_DATA_TYPE
Definition: IRremoteInt.h:106
IRrecv::decodePulseDistanceWidthData
void decodePulseDistanceWidthData(PulseDistanceWidthProtocolConstants *aProtocolConstants, uint_fast8_t aNumberOfBits, IRRawlenType aStartOffset=3)
Decode pulse distance protocols for PulseDistanceWidthProtocolConstants.
Definition: IRReceive.hpp:1081
PROTOCOL_IS_MSB_FIRST
#define PROTOCOL_IS_MSB_FIRST
Definition: IRProtocol.h:178
PROTOCOL_IS_PULSE_WIDTH
#define PROTOCOL_IS_PULSE_WIDTH
Definition: IRProtocol.h:175
IRData::numberOfBits
uint16_t numberOfBits
Number of bits received for data (address + command + parity) - to determine protocol length if diffe...
Definition: IRremoteInt.h:161
IRrecv::printDistanceWidthTimingInfo
void printDistanceWidthTimingInfo(Print *aSerial, DistanceWidthTimingInfoStruct *aDistanceWidthTimingInfo)
Definition: IRReceive.hpp:1903
IRRawlenType
unsigned int IRRawlenType
Definition: IRremoteInt.h:87
aggregateArrayCounts
bool aggregateArrayCounts(uint8_t aArray[], uint8_t aMaxIndex, uint8_t *aShortIndex, uint8_t *aLongIndex)
Definition: ir_DistanceWidthProtocol.hpp:151
MARK_EXCESS_MICROS
#define MARK_EXCESS_MICROS
MARK_EXCESS_MICROS is subtracted from all marks and added to all spaces before decoding,...
Definition: IRremote.hpp:105
PROTOCOL_IS_PULSE_DISTANCE
#define PROTOCOL_IS_PULSE_DISTANCE
Definition: IRProtocol.h:173
IRrecv::decodeDistanceWidth
bool decodeDistanceWidth()
Definition: ir_DistanceWidthProtocol.hpp:202
DISTANCE_WIDTH_DECODER_DURATION_ARRAY_SIZE
#define DISTANCE_WIDTH_DECODER_DURATION_ARRAY_SIZE
Definition: ir_DistanceWidthProtocol.hpp:76
PULSE_WIDTH
@ PULSE_WIDTH
Definition: IRProtocol.h:95
IRData::decodedRawData
IRRawDataType decodedRawData
Up to 32/64 bit decoded raw data, to be used for send<protocol>Raw functions.
Definition: IRremoteInt.h:155
PULSE_DISTANCE
@ PULSE_DISTANCE
Definition: IRProtocol.h:96
IRrecv::decodedIRData
IRData decodedIRData
Definition: IRremoteInt.h:409
IRData::flags
uint8_t flags
IRDATA_FLAGS_IS_REPEAT, IRDATA_FLAGS_WAS_OVERFLOW etc. See IRDATA_FLAGS_* definitions above.
Definition: IRremoteInt.h:162
PROTOCOL_IS_LSB_FIRST
#define PROTOCOL_IS_LSB_FIRST
Definition: IRProtocol.h:179
irparams_struct::rawbuf
IRRawbufType rawbuf[RAW_BUFFER_LENGTH]
raw data / tick counts per mark/space. With 8 bit we can only store up to 12.7 ms....
Definition: IRremoteInt.h:140
IRrecv::irparams
irparams_struct irparams
Definition: IRremoteInt.h:408
IRDATA_FLAGS_IS_MSB_FIRST
#define IRDATA_FLAGS_IS_MSB_FIRST
Value is mainly determined by the (known) protocol.
Definition: IRProtocol.h:155
IRData::rawlen
IRRawlenType rawlen
Counter of entries in rawbuf of last received frame.
Definition: IRremoteInt.h:170
DISTANCE_WIDTH_MAXIMUM_REPEAT_DISTANCE_MICROS
#define DISTANCE_WIDTH_MAXIMUM_REPEAT_DISTANCE_MICROS
Definition: ir_DistanceWidthProtocol.hpp:62
IRData::protocol
decode_type_t protocol
UNKNOWN, NEC, SONY, RC5, PULSE_DISTANCE, ...
Definition: IRremoteInt.h:151
IrReceiver
IRrecv IrReceiver
The receiver instance.
Definition: IRReceive.hpp:64
IRData::initialGapTicks
uint16_t initialGapTicks
Contains the initial gap (pre 4.4: the value in rawbuf[0]) of the last received frame.
Definition: IRremoteInt.h:171
IRrecv::decodeWithThresholdPulseDistanceWidthData
void decodeWithThresholdPulseDistanceWidthData(uint_fast8_t aNumberOfBits, IRRawlenType aStartOffset, uint16_t aOneThresholdMicros, bool aIsPulseWidthProtocol, bool aMSBfirst)
New threshold decoder to be activated by USE_THRESHOLD_DECODER Assumes a 0 for shorter and a 1 for lo...
Definition: IRReceive.hpp:828