Skip to content

Commit dc6aabd

Browse files
committed
Minimize changes against master
WIP for crankyoldgit#2144
1 parent 1b0f35f commit dc6aabd

File tree

1 file changed

+35
-50
lines changed

1 file changed

+35
-50
lines changed

src/IRrecv.cpp

Lines changed: 35 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ static ETSTimer timer;
5656
} // namespace _IRrecv
5757
#endif // ESP8266
5858
#if defined(ESP32)
59+
#if ( defined(ESP_ARDUINO_VERSION) && \
60+
(ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) )
61+
#define _ESP32_ARDUINOV3
62+
#endif // Version > 3
5963
// We need a horrible timer hack for ESP32 Arduino framework < v2.0.0
6064
#if !defined(_ESP32_IRRECV_TIMER_HACK)
6165
// Version check
@@ -69,14 +73,6 @@ static ETSTimer timer;
6973
#endif // Version check
7074
#endif // !defined(_ESP32_IRRECV_TIMER_HACK)
7175

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-
8076
#if _ESP32_IRRECV_TIMER_HACK
8177
// Required structs/types from:
8278
// https://github.com/espressif/arduino-esp32/blob/6b0114366baf986c155e8173ab7c22bc0c5fcedc/cores/esp32/esp32-hal-timer.c#L28-L58
@@ -250,15 +246,14 @@ static void USE_IRAM_ATTR gpio_intr() {
250246
// @see https://github.com/crankyoldgit/IRremoteESP8266/issues/1350
251247
// @see https://github.com/espressif/arduino-esp32/blob/6b0114366baf986c155e8173ab7c22bc0c5fcedc/cores/esp32/esp32-hal-timer.c#L176-L178
252248
timer->dev->config.alarm_en = 1;
253-
#elif ARDUINO_COREV3
249+
#elif defined(_ESP32_ARDUINOV3)
254250
// For ESP32 core version 3.x, replace `timerAlarmEnable`
255251
timerWrite(timer, 0);
256252
uint64_t alarm_value = 50000; // Example value (50ms)
257253
timerAlarm(timer, alarm_value, false, 0);
258-
#else
259-
// For ESP32 core version 2.x, keep using `timerAlarmEnable`
260-
timerWrite(timer, 0);
261-
timerAlarmEnable(timer);
254+
#else // !_ESP32_ARDUINOV3
255+
timerWrite(timer, 0);
256+
timerAlarmEnable(timer);
262257
#endif // _ESP32_IRRECV_TIMER_HACK
263258
#endif // ESP32
264259
}
@@ -374,13 +369,12 @@ void IRrecv::enableIRIn(const bool pullup) {
374369

375370
#if defined(ESP32)
376371
// Initialise the ESP32 timer.
377-
#if ARDUINO_COREV3
372+
#if defined(_ESP32_ARDUINOV3)
378373
// Use newer timerBegin signature for ESP32 core version 3.x
379374
timer = timerBegin(1000000); // Initialize with 1MHz (1us per tick)
380-
#else
381-
// Fallback for ESP32 core version 2.x or earlier
382-
timer = timerBegin(0, 1000000, true); // Old signature with divider
383-
#endif
375+
#else // _ESP32_ARDUINOV3
376+
timer = timerBegin(0, 1000000, true);
377+
#endif // _ESP32_ARDUINOV3
384378

385379
// Ensure the timer is successfully initialized
386380
#ifdef DEBUG
@@ -391,15 +385,16 @@ void IRrecv::enableIRIn(const bool pullup) {
391385
#endif // DEBUG
392386
assert(timer != NULL); // Check we actually got the timer.
393387
// Set the timer so it only fires once, and set its trigger in microseconds.
394-
#if ARDUINO_COREV3
395-
timerWrite(timer, 0); // Reset the timer for ESP32 core version 3.x
396-
timerAttachInterrupt(timer, &read_timeout);
397-
#else
398-
// Attach timer interrupt for core version 2.x
399-
timerAlarmWrite(timer, MS_TO_USEC(params.timeout), true);
400-
timerAttachInterrupt(timer, &read_timeout, false);
401-
#endif
402-
388+
#if defined(_ESP32_ARDUINOV3)
389+
timerWrite(timer, 0); // Reset the timer for ESP32 core version 3.x
390+
timerAttachInterrupt(timer, &read_timeout);
391+
#else // _ESP32_ARDUINOV3
392+
timerAlarmWrite(timer, MS_TO_USEC(params.timeout), ONCE);
393+
// Note: Interrupt needs to be attached before it can be enabled or disabled.
394+
// Note: EDGE (true) is not supported, use LEVEL (false). Ref: #1713
395+
// See: https://github.com/espressif/arduino-esp32/blob/caef4006af491130136b219c1205bdcf8f08bf2b/cores/esp32/esp32-hal-timer.c#L224-L227
396+
timerAttachInterrupt(timer, &read_timeout, false);
397+
#endif // _ESP32_ARDUINOV3
403398
#endif // ESP32
404399

405400
// Initialise state machine variables
@@ -423,20 +418,14 @@ void IRrecv::disableIRIn(void) {
423418
#ifndef UNIT_TEST
424419
#if defined(ESP8266)
425420
os_timer_disarm(&timer);
426-
#endif // ESP8266
427-
#if defined(ESP32)
428-
// Check for ESP32 core version and handle timer functions differently
429-
#if ARDUINO_COREV3
430-
// For ESP32 core version 3.x
431-
timerWrite(timer, 0); // Reset the timer
432-
timerDetachInterrupt(timer); // Detach the interrupt
433-
timerEnd(timer); // End the timer
434-
#else
435-
// For ESP32 core version 2.x
436-
timerAlarmDisable(timer); // Disable the alarm
437-
timerDetachInterrupt(timer); // Detach the interrupt
438-
timerEnd(timer); // End the timer
439-
#endif
421+
#elif defined(_ESP32_ARDUINOV3)
422+
timerWrite(timer, 0); // Reset the timer
423+
timerDetachInterrupt(timer);
424+
timerEnd(timer);
425+
#elif defined(ESP32)
426+
timerAlarmDisable(timer);
427+
timerDetachInterrupt(timer);
428+
timerEnd(timer);
440429
#endif // ESP32
441430
detachInterrupt(params.recvpin);
442431
#endif // UNIT_TEST
@@ -463,15 +452,11 @@ void IRrecv::resume(void) {
463452
params.overflow = false;
464453
#if defined(ESP32)
465454
// Check for ESP32 core version and handle timer functions differently
466-
#if defined(ESP_ARDUINO_VERSION) && \
467-
(ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0))
468-
// For ESP32 core version 3.x
469-
timerWrite(timer, 0); // Reset the timer (no need for timerAlarmDisable)
470-
#else
471-
// For ESP32 core version 2.x
472-
timerAlarmDisable(timer); // Disable the alarm
473-
#endif
474-
455+
#if defined(_ESP32_ARDUINOV3)
456+
timerWrite(timer, 0); // Reset the timer (no need for timerAlarmDisable)
457+
#else // _ESP32_ARDUINOV3
458+
timerAlarmDisable(timer);
459+
#endif
475460
// Re-enable GPIO interrupt in both versions
476461
gpio_intr_enable((gpio_num_t)params.recvpin);
477462
#endif // ESP32

0 commit comments

Comments
 (0)