22 * ir_DistanceWidthProtocol.hpp
33 *
44 * Contains only the decoder functions for universal pulse width or pulse distance protocols!
5- * The send functions are used by almost all protocols and therefore in IRSend.hh.
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.
69 *
710 * This decoder tries to decode a pulse distance or pulse distance width with constant period (or pulse width - not enabled yet) protocol.
811 * 1. Analyze all space and mark length
3134 ************************************************************************************
3235 * MIT License
3336 *
34- * Copyright (c) 2022-2023 Armin Joachimsmeyer
37+ * Copyright (c) 2022-2024 Armin Joachimsmeyer
3538 *
3639 * Permission is hereby granted, free of charge, to any person obtaining a copy
3740 * of this software and associated documentation files (the "Software"), to deal
6568// #define LOCAL_DEBUG // This enables debug output only for this file
6669#endif
6770
68- // accept durations up to 50 * 50 (MICROS_PER_TICK) 2500 microseconds
69- #define DURATION_ARRAY_SIZE 50
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
74+ # else
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
76+ # endif
77+ #endif
7078
7179// Switch the decoding according to your needs
7280// #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
@@ -148,9 +156,15 @@ bool aggregateArrayCounts(uint8_t aArray[], uint8_t aMaxIndex, uint8_t *aShortIn
148156 * 2. Decide if we have an pulse width or distance protocol
149157 * 3. Try to decode with the mark and space data found in step 1
150158 * No data and address decoding, only raw data as result.
159+ *
160+ * calloc() version is 700 bytes larger :-(
151161 */
152162bool IRrecv::decodeDistanceWidth () {
153- uint8_t tDurationArray[DURATION_ARRAY_SIZE]; // For up to 49 ticks / 2450 us
163+ /*
164+ * Array for up to 49 ticks / 2500 us (or 199 ticks / 10 ms us if RAM > 2k)
165+ * 0 tick covers mark or space durations from 0 to 49 us, and 49 ticks from 2450 to 2499 us
166+ */
167+ uint8_t tDurationArray[DISTANCE_WIDTH_DECODER_DURATION_ARRAY_SIZE];
154168
155169 /*
156170 * Accept only protocols with at least 8 bits
@@ -164,16 +178,16 @@ bool IRrecv::decodeDistanceWidth() {
164178 }
165179
166180 // Reset duration array
167- memset (tDurationArray, 0 , DURATION_ARRAY_SIZE );
181+ memset (tDurationArray, 0 , DISTANCE_WIDTH_DECODER_DURATION_ARRAY_SIZE );
168182
169183 uint8_t tIndexOfMaxDuration = 0 ;
170184 /*
171- * Count number of mark durations up to 49 ticks . Skip leading start and trailing stop bit.
185+ * Count number of mark durations. Skip leading start and trailing stop bit.
172186 */
173187 for (IRRawlenType i = 3 ; i < decodedIRData.rawlen - 2 ; i += 2 ) {
174188 auto tDurationTicks = decodedIRData.rawDataPtr ->rawbuf [i];
175- if (tDurationTicks < DURATION_ARRAY_SIZE ) {
176- tDurationArray[tDurationTicks]++; // count duration if less than DURATION_ARRAY_SIZE (50)
189+ if (tDurationTicks < DISTANCE_WIDTH_DECODER_DURATION_ARRAY_SIZE ) {
190+ tDurationArray[tDurationTicks]++; // count duration if less than DISTANCE_WIDTH_DECODER_DURATION_ARRAY_SIZE
177191 if (tIndexOfMaxDuration < tDurationTicks) {
178192 tIndexOfMaxDuration = tDurationTicks;
179193 }
@@ -183,7 +197,7 @@ bool IRrecv::decodeDistanceWidth() {
183197 Serial.print (F (" Mark " ));
184198 Serial.print (tDurationTicks * MICROS_PER_TICK);
185199 Serial.print (F (" is longer than maximum " ));
186- Serial.print (DURATION_ARRAY_SIZE * MICROS_PER_TICK);
200+ Serial.print (DISTANCE_WIDTH_DECODER_DURATION_ARRAY_SIZE * MICROS_PER_TICK);
187201 Serial.print (F (" us. Index=" ));
188202 Serial.println (i);
189203#endif
@@ -211,15 +225,15 @@ bool IRrecv::decodeDistanceWidth() {
211225 }
212226
213227 // Reset duration array
214- memset (tDurationArray, 0 , DURATION_ARRAY_SIZE );
228+ memset (tDurationArray, 0 , DISTANCE_WIDTH_DECODER_DURATION_ARRAY_SIZE );
215229
216230 /*
217231 * Count number of space durations. Skip leading start and trailing stop bit.
218232 */
219233 tIndexOfMaxDuration = 0 ;
220234 for (IRRawlenType i = 4 ; i < decodedIRData.rawlen - 2 ; i += 2 ) {
221235 auto tDurationTicks = decodedIRData.rawDataPtr ->rawbuf [i];
222- if (tDurationTicks < DURATION_ARRAY_SIZE ) {
236+ if (tDurationTicks < DISTANCE_WIDTH_DECODER_DURATION_ARRAY_SIZE ) {
223237 tDurationArray[tDurationTicks]++;
224238 if (tIndexOfMaxDuration < tDurationTicks) {
225239 tIndexOfMaxDuration = tDurationTicks;
@@ -230,7 +244,7 @@ bool IRrecv::decodeDistanceWidth() {
230244 Serial.print (F (" Space " ));
231245 Serial.print (tDurationTicks * MICROS_PER_TICK);
232246 Serial.print (F (" is longer than maximum " ));
233- Serial.print (DURATION_ARRAY_SIZE * MICROS_PER_TICK);
247+ Serial.print (DISTANCE_WIDTH_DECODER_DURATION_ARRAY_SIZE * MICROS_PER_TICK);
234248 Serial.print (F (" us. Index=" ));
235249 Serial.println (i);
236250#endif
0 commit comments