Skip to content

Commit 9c46f81

Browse files
NiKiZezehnm
authored andcommitted
Minimize changes against master
WIP for crankyoldgit#2144
1 parent f4db0d9 commit 9c46f81

File tree

1 file changed

+36
-51
lines changed

1 file changed

+36
-51
lines changed

src/IRrecv.cpp

Lines changed: 36 additions & 51 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 // ESP_ARDUINO_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 // defined(ESP_ARDUINO_VERSION)
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
@@ -142,8 +138,7 @@ typedef struct hw_timer_s {
142138
#endif // _ESP32_IRRECV_TIMER_HACK / End of Horrible Hack.
143139

144140
namespace _IRrecv {
145-
static hw_timer_t *timer = NULL; // Declare ESP32 timer variable
146-
141+
static hw_timer_t *timer = NULL;
147142
} // namespace _IRrecv
148143
#endif // ESP32
149144
using _IRrecv::timer;
@@ -250,15 +245,14 @@ static void USE_IRAM_ATTR gpio_intr() {
250245
// @see https://github.com/crankyoldgit/IRremoteESP8266/issues/1350
251246
// @see https://github.com/espressif/arduino-esp32/blob/6b0114366baf986c155e8173ab7c22bc0c5fcedc/cores/esp32/esp32-hal-timer.c#L176-L178
252247
timer->dev->config.alarm_en = 1;
253-
#elif ARDUINO_COREV3
248+
#elif defined(_ESP32_ARDUINOV3)
254249
// For ESP32 core version 3.x, replace `timerAlarmEnable`
255250
timerWrite(timer, 0);
256251
uint64_t alarm_value = 50000; // Example value (50ms)
257252
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);
253+
#else // !_ESP32_ARDUINOV3
254+
timerWrite(timer, 0);
255+
timerAlarmEnable(timer);
262256
#endif // _ESP32_IRRECV_TIMER_HACK
263257
#endif // ESP32
264258
}
@@ -379,16 +373,15 @@ void IRrecv::enableIRIn(const bool pullup) {
379373
pinMode(params.recvpin, INPUT);
380374
#endif // UNIT_TEST
381375
}
382-
383376
#if defined(ESP32)
384377
// Initialise the ESP32 timer.
385-
#if ARDUINO_COREV3
378+
#if defined(_ESP32_ARDUINOV3)
386379
// Use newer timerBegin signature for ESP32 core version 3.x
387380
timer = timerBegin(1000000); // Initialize with 1MHz (1us per tick)
388-
#else
389-
// Fallback for ESP32 core version 2.x or earlier
381+
#else // _ESP32_ARDUINOV3
382+
// 80MHz / 80 = 1 uSec granularity.
390383
timer = timerBegin(_timer_num, 80, true);
391-
#endif // ARDUINO_COREV3
384+
#endif // _ESP32_ARDUINOV3
392385

393386
// Ensure the timer is successfully initialized
394387
#ifdef IR_DEBUG
@@ -399,15 +392,16 @@ void IRrecv::enableIRIn(const bool pullup) {
399392
#endif // IR_DEBUG
400393
assert(timer != NULL); // Check we actually got the timer.
401394
// Set the timer so it only fires once, and set its trigger in microseconds.
402-
#if ARDUINO_COREV3
403-
timerWrite(timer, 0); // Reset the timer for ESP32 core version 3.x
404-
timerAttachInterrupt(timer, &read_timeout);
405-
#else
406-
// Attach timer interrupt for core version 2.x
407-
timerAlarmWrite(timer, MS_TO_USEC(params.timeout), true);
408-
timerAttachInterrupt(timer, &read_timeout, false);
409-
#endif // ARDUINO_COREV3
410-
395+
#if defined(_ESP32_ARDUINOV3)
396+
timerWrite(timer, 0); // Reset the timer for ESP32 core version 3.x
397+
timerAttachInterrupt(timer, &read_timeout);
398+
#else // _ESP32_ARDUINOV3
399+
timerAlarmWrite(timer, MS_TO_USEC(params.timeout), ONCE);
400+
// Note: Interrupt needs to be attached before it can be enabled or disabled.
401+
// Note: EDGE (true) is not supported, use LEVEL (false). Ref: #1713
402+
// See: https://github.com/espressif/arduino-esp32/blob/caef4006af491130136b219c1205bdcf8f08bf2b/cores/esp32/esp32-hal-timer.c#L224-L227
403+
timerAttachInterrupt(timer, &read_timeout, false);
404+
#endif // _ESP32_ARDUINOV3
411405
#endif // ESP32
412406

413407
// Initialise state machine variables
@@ -431,20 +425,14 @@ void IRrecv::disableIRIn(void) {
431425
#ifndef UNIT_TEST
432426
#if defined(ESP8266)
433427
os_timer_disarm(&timer);
434-
#endif // ESP8266
435-
#if defined(ESP32)
436-
// Check for ESP32 core version and handle timer functions differently
437-
#if ARDUINO_COREV3
438-
// For ESP32 core version 3.x
439-
timerWrite(timer, 0); // Reset the timer
440-
timerDetachInterrupt(timer); // Detach the interrupt
441-
timerEnd(timer); // End the timer
442-
#else
443-
// For ESP32 core version 2.x
444-
timerAlarmDisable(timer); // Disable the alarm
445-
timerDetachInterrupt(timer); // Detach the interrupt
446-
timerEnd(timer); // End the timer
447-
#endif
428+
#elif defined(_ESP32_ARDUINOV3)
429+
timerWrite(timer, 0); // Reset the timer
430+
timerDetachInterrupt(timer);
431+
timerEnd(timer);
432+
#elif defined(ESP32)
433+
timerAlarmDisable(timer);
434+
timerDetachInterrupt(timer);
435+
timerEnd(timer);
448436
#endif // ESP32
449437
detachInterrupt(params.recvpin);
450438
#endif // UNIT_TEST
@@ -471,14 +459,11 @@ void IRrecv::resume(void) {
471459
params.overflow = false;
472460
#if defined(ESP32)
473461
// Check for ESP32 core version and handle timer functions differently
474-
#if ARDUINO_COREV3
475-
// For ESP32 core version 3.x
476-
timerWrite(timer, 0); // Reset the timer (no need for timerAlarmDisable)
477-
#else
478-
// For ESP32 core version 2.x
479-
timerAlarmDisable(timer); // Disable the alarm
480-
#endif // ARDUINO_COREV3
481-
462+
#if defined(_ESP32_ARDUINOV3)
463+
timerWrite(timer, 0); // Reset the timer (no need for timerAlarmDisable)
464+
#else // _ESP32_ARDUINOV3
465+
timerAlarmDisable(timer);
466+
#endif // _ESP32_ARDUINOV3
482467
// Re-enable GPIO interrupt in both versions
483468
gpio_intr_enable((gpio_num_t)params.recvpin);
484469
#endif // ESP32

0 commit comments

Comments
 (0)