Skip to content

Commit 5d9da88

Browse files
BorisKofmanBorisKofman
authored andcommitted
Update IRrecv.cpp
1 parent a67832c commit 5d9da88

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

src/IRrecv.cpp

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ static ETSTimer timer;
6969
#endif // Version check
7070
#endif // !defined(_ESP32_IRRECV_TIMER_HACK)
7171

72+
// Define ARDUINO_COREV3 macro
73+
#if defined(ESP_ARDUINO_VERSION) && \
74+
(ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0))
75+
#define ARDUINO_COREV3 1
76+
#else
77+
#define ARDUINO_COREV3 0
78+
#endif
79+
7280
#if _ESP32_IRRECV_TIMER_HACK
7381
// Required structs/types from:
7482
// https://github.com/espressif/arduino-esp32/blob/6b0114366baf986c155e8173ab7c22bc0c5fcedc/cores/esp32/esp32-hal-timer.c#L28-L58
@@ -242,21 +250,15 @@ static void USE_IRAM_ATTR gpio_intr() {
242250
// @see https://github.com/crankyoldgit/IRremoteESP8266/issues/1350
243251
// @see https://github.com/espressif/arduino-esp32/blob/6b0114366baf986c155e8173ab7c22bc0c5fcedc/cores/esp32/esp32-hal-timer.c#L176-L178
244252
timer->dev->config.alarm_en = 1;
245-
#else // _ESP32_IRRECV_TIMER_HACK
246-
// Check the ESP32 core version
247-
#if defined(ESP_ARDUINO_VERSION) && \
248-
(ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0))
249-
// For ESP32 core version 3.x, replace `timerAlarmEnable`
250-
timerWrite(timer, 0);
251-
uint64_t alarm_value = 50000; // Example value (50ms)
252-
timerAlarm(timer, alarm_value, false, 0); // Disable auto-reload
253-
254-
#else
253+
#elif ARDUINO_COREV3
254+
// For ESP32 core version 3.x, replace `timerAlarmEnable`
255+
timerWrite(timer, 0);
256+
uint64_t alarm_value = 50000; // Example value (50ms)
257+
timerAlarm(timer, alarm_value, false, 0); // Disable auto-reloadmer, alarm_value, false, 0); // Disable auto-reload
258+
#else
255259
// For ESP32 core version 2.x, keep using `timerAlarmEnable`
256260
timerWrite(timer, 0);
257261
timerAlarmEnable(timer);
258-
#endif
259-
260262
#endif // _ESP32_IRRECV_TIMER_HACK
261263
#endif // ESP32
262264
}
@@ -372,15 +374,12 @@ void IRrecv::enableIRIn(const bool pullup) {
372374

373375
#if defined(ESP32)
374376
// Initialise the ESP32 timer.
375-
#if defined(ESP_ARDUINO_VERSION) && \
376-
(ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0))
377-
// Use newer timerBegin signature for ESP32 core version 3.x
378-
timer = timerBegin(1000000); // Initialize with 1MHz (1us per tick)
377+
#if ARDUINO_COREV3
378+
// Use newer timerBegin signature for ESP32 core version 3.x
379+
timer = timerBegin(1000000); // Initialize with 1MHz (1us per tick)
379380
#else
380-
// Initialise the ESP32 timer.
381-
// 80MHz / 80 = 1 uSec granularity.
382-
// Check for ESP32 core version and handle timerBegin differently
383-
timer = timerBegin(_timer_num, 80, true);
381+
// Fallback for ESP32 core version 2.x or earlier
382+
timer = timerBegin(0, 1000000, true); // Old signature with divider
384383
#endif
385384

386385
// Ensure the timer is successfully initialized
@@ -392,8 +391,7 @@ void IRrecv::enableIRIn(const bool pullup) {
392391
#endif // DEBUG
393392
assert(timer != NULL); // Check we actually got the timer.
394393
// Set the timer so it only fires once, and set its trigger in microseconds.
395-
#if defined(ESP_ARDUINO_VERSION) && \
396-
(ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0))
394+
#if ARDUINO_COREV3
397395
timerWrite(timer, 0); // Reset the timer for ESP32 core version 3.x
398396
timerAttachInterrupt(timer, &read_timeout);
399397
#else
@@ -428,8 +426,7 @@ void IRrecv::disableIRIn(void) {
428426
#endif // ESP8266
429427
#if defined(ESP32)
430428
// Check for ESP32 core version and handle timer functions differently
431-
#if defined(ESP_ARDUINO_VERSION) && \
432-
(ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0))
429+
#if ARDUINO_COREV3
433430
// For ESP32 core version 3.x
434431
timerWrite(timer, 0); // Reset the timer
435432
timerDetachInterrupt(timer); // Detach the interrupt

0 commit comments

Comments
 (0)