Skip to content

Commit eae9de4

Browse files
committed
Cleaned up ESP32 integration, reverted ESP32 ifdefs on irreceive examples.
- fixed indenting on existing code in a few places for consistency - introduced IR_TIMER_USE_ESP32 for ifdefs within the code as per request - added comments explaining what's missing for irsend support on ESP32 - IRrecvDemo.ino gets a warning before and after interrupt is enabled in case it causes a crash TESTED=IoTuz ESP32 board and original 328p arduino to make sure current code did not break.
1 parent 1b56da6 commit eae9de4

File tree

7 files changed

+42
-29
lines changed

7 files changed

+42
-29
lines changed

IRremote.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@
1818
// Whynter A/C ARC-110WD added by Francesco Meschia
1919
//******************************************************************************
2020

21-
#ifndef ESP32
22-
#include <avr/interrupt.h>
23-
#endif
24-
2521
// Defining IR_GLOBAL here allows us to declare the instantiation of global variables
2622
#define IR_GLOBAL
2723
# include "IRremote.h"
2824
# include "IRremoteInt.h"
2925
#undef IR_GLOBAL
3026

27+
#ifndef IR_TIMER_USE_ESP32
28+
#include <avr/interrupt.h>
29+
#endif
30+
31+
3132
//+=============================================================================
3233
// The match functions were (apparently) originally MACROs to improve code speed
3334
// (although this would have bloated the code) hence the names being CAPS
@@ -122,7 +123,7 @@ int MATCH_SPACE (int measured_ticks, int desired_us)
122123
// As soon as first MARK arrives:
123124
// Gap width is recorded; Ready is cleared; New logging starts
124125
//
125-
#ifdef ESP32
126+
#ifdef IR_TIMER_USE_ESP32
126127
void IRTimer()
127128
#else
128129
ISR (TIMER_INTR_NAME)

boarddefs.h

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@
3939
# define BLINKLED_ON() (PORTD |= B00000001)
4040
# define BLINKLED_OFF() (PORTD &= B11111110)
4141

42+
// No system LED on ESP32, disable blinking
4243
#elif defined(ESP32)
4344
# define BLINKLED 255
4445
# define BLINKLED_ON() 1
4546
# define BLINKLED_OFF() 1
4647
#else
4748
# define BLINKLED 13
48-
#define BLINKLED_ON() (PORTB |= B00100000)
49+
# define BLINKLED_ON() (PORTB |= B00100000)
4950
# define BLINKLED_OFF() (PORTB &= B11011111)
5051
#endif
5152

@@ -129,14 +130,16 @@
129130

130131
// ATtiny84
131132
#elif defined(__AVR_ATtiny84__)
132-
#define IR_USE_TIMER1 // tx = pin 6
133+
#define IR_USE_TIMER1 // tx = pin 6
133134

134135
//ATtiny85
135136
#elif defined(__AVR_ATtiny85__)
136-
#define IR_USE_TIMER_TINY0 // tx = pin 1
137+
#define IR_USE_TIMER_TINY0 // tx = pin 1
137138

138139
// Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, Nano, etc
139140
// ATmega48, ATmega88, ATmega168, ATmega328
141+
#elif defined(ESP32)
142+
#define IR_TIMER_USE_ESP32
140143
#else
141144
//#define IR_USE_TIMER1 // tx = pin 9
142145
#define IR_USE_TIMER2 // tx = pin 3
@@ -151,21 +154,12 @@
151154
//
152155
#if defined(IR_USE_TIMER2)
153156

154-
#ifdef ESP32 // Used in irSend, not implemented yet (FIXME)
155-
#define TIMER_RESET 1
156-
#define TIMER_ENABLE_PWM 1
157-
#define TIMER_DISABLE_PWM Serial.println("IRsend not implemented for ESP32 yet");
158-
#define TIMER_ENABLE_INTR 1
159-
#define TIMER_DISABLE_INTR 1
160-
#define TIMER_INTR_NAME 1
161-
#else
162157
#define TIMER_RESET
163158
#define TIMER_ENABLE_PWM (TCCR2A |= _BV(COM2B1))
164159
#define TIMER_DISABLE_PWM (TCCR2A &= ~(_BV(COM2B1)))
165160
#define TIMER_ENABLE_INTR (TIMSK2 = _BV(OCIE2A))
166161
#define TIMER_DISABLE_INTR (TIMSK2 = 0)
167162
#define TIMER_INTR_NAME TIMER2_COMPA_vect
168-
#endif
169163

170164
#define TIMER_CONFIG_KHZ(val) ({ \
171165
const uint8_t pwmval = SYSCLOCK / 2000 / (val); \
@@ -551,6 +545,28 @@
551545

552546
#define TIMER_PWM_PIN 1 /* ATtiny85 */
553547

548+
//---------------------------------------------------------
549+
// ESP32 (ESP8266 should likely be added here too)
550+
//
551+
552+
// ESP32 has it own timer API and does not use these macros, but to avoid ifdef'ing
553+
// them out in the common code, they are defined to no-op. This allows the code to compile
554+
// (which it wouldn't otherwise) but irsend will not work until ESP32 specific code is written
555+
// for that -- merlin
556+
// As a warning, sending timing specific code from an ESP32 can be challenging if you need 100%
557+
// reliability because the arduino code may be interrupted and cause your sent waveform to be the
558+
// wrong length. This is specifically an issue for neopixels which require 800Khz resolution.
559+
// IR may just work as is with the common code since it's lower frequency, but if not, the other
560+
// way to do this on ESP32 is using the RMT built in driver like in this incomplete library below
561+
// https://github.com/ExploreEmbedded/ESP32_RMT
562+
#elif defined(IR_TIMER_USE_ESP32)
563+
#define TIMER_RESET 1
564+
#define TIMER_ENABLE_PWM 1
565+
#define TIMER_DISABLE_PWM Serial.println("IRsend not implemented for ESP32 yet");
566+
#define TIMER_ENABLE_INTR 1
567+
#define TIMER_DISABLE_INTR 1
568+
#define TIMER_INTR_NAME 1
569+
554570
//---------------------------------------------------------
555571
// Unknown Timer
556572
//

examples/IRrecvDemo/IRrecvDemo.ino

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,17 @@
88

99
#include <IRremote.h>
1010

11-
#ifdef ESP32
12-
int RECV_PIN = 35;
13-
#else
1411
int RECV_PIN = 11;
15-
#endif
1612

1713
IRrecv irrecv(RECV_PIN);
1814

1915
decode_results results;
2016

2117
void setup()
2218
{
23-
Serial.begin(115200);
19+
Serial.begin(9600);
20+
// In case the interrupt driver crashes on setup, give a clue
21+
// to the user what's going on.
2422
Serial.println("Enabling IRin");
2523
irrecv.enableIRIn(); // Start the receiver
2624
Serial.println("Enabled IRin");

examples/IRrecvDump/IRrecvDump.ino

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@
1515
* You can change this to another available Arduino Pin.
1616
* Your IR receiver should be connected to the pin defined here
1717
*/
18-
#ifdef ESP32
19-
int RECV_PIN = 35;
20-
#else
2118
int RECV_PIN = 11;
22-
#endif
2319

2420
IRrecv irrecv(RECV_PIN);
2521

examples/IRrecvDumpV2/IRrecvDumpV2.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
//------------------------------------------------------------------------------
77
// Tell IRremote which Arduino pin is connected to the IR Receiver (TSOP4838)
88
//
9-
int recvPin = 35;
9+
int recvPin = 11;
1010
IRrecv irrecv(recvPin);
1111

1212
//+=============================================================================
1313
// Configure the Arduino
1414
//
1515
void setup ( )
1616
{
17-
Serial.begin(115200); // Status message will be sent to PC at 9600 baud
17+
Serial.begin(9600); // Status message will be sent to PC at 9600 baud
1818
irrecv.enableIRIn(); // Start the receiver
1919
}
2020

irRecv.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ void IRrecv::enableIRIn ( )
124124
{
125125
// Interrupt Service Routine - Fires every 50uS
126126
#ifdef ESP32
127+
// ESP32 has a proper API to setup timers, no weird chip macros needed
128+
// simply call the readable API versions :)
127129
// 3 timers, choose #1, 80 divider nanosecond precision, 1 to count up
128130
timer = timerBegin(1, 80, 1);
129131
timerAttachInterrupt(timer, &IRTimer, 1);

irSend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void IRsend::space (unsigned int time)
5454
//
5555
void IRsend::enableIROut (int khz)
5656
{
57-
// FIXME: implement ESP32 support
57+
// FIXME: implement ESP32 support, see IR_TIMER_USE_ESP32 in boarddefs.h
5858
#ifndef ESP32
5959
// Disable the Timer2 Interrupt (which is used for receiving IR)
6060
TIMER_DISABLE_INTR; //Timer2 Overflow Interrupt

0 commit comments

Comments
 (0)