Skip to content

Commit aa8f7b3

Browse files
committed
Added ESP32 IR receive support (IRsend not implemented yet).
- disable a lot of defines not relevant to ESP32, set them to 1 (no-op) - change default IR pin to 35 from 11 - changed serial speed to 115200 (9600 is too slow to keep up with IR input) - irSend disables code that will not compile on ESP32. It won't work, but it won't break compilation either.
1 parent 048efb2 commit aa8f7b3

File tree

7 files changed

+52
-5
lines changed

7 files changed

+52
-5
lines changed

IRremote.cpp

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

21+
#ifndef ESP32
2122
#include <avr/interrupt.h>
23+
#endif
2224

2325
// Defining IR_GLOBAL here allows us to declare the instantiation of global variables
2426
#define IR_GLOBAL
@@ -120,7 +122,11 @@ int MATCH_SPACE (int measured_ticks, int desired_us)
120122
// As soon as first MARK arrives:
121123
// Gap width is recorded; Ready is cleared; New logging starts
122124
//
125+
#ifdef ESP32
126+
void onTimer()
127+
#else
123128
ISR (TIMER_INTR_NAME)
129+
#endif
124130
{
125131
TIMER_RESET;
126132

boarddefs.h

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

42+
#elif defined(ESP32)
43+
# define BLINKLED 255
44+
# define BLINKLED_ON() 1
45+
# define BLINKLED_OFF() 1
4246
#else
4347
# define BLINKLED 13
4448
#define BLINKLED_ON() (PORTB |= B00100000)
@@ -147,12 +151,21 @@
147151
//
148152
#if defined(IR_USE_TIMER2)
149153

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
150162
#define TIMER_RESET
151163
#define TIMER_ENABLE_PWM (TCCR2A |= _BV(COM2B1))
152164
#define TIMER_DISABLE_PWM (TCCR2A &= ~(_BV(COM2B1)))
153165
#define TIMER_ENABLE_INTR (TIMSK2 = _BV(OCIE2A))
154166
#define TIMER_DISABLE_INTR (TIMSK2 = 0)
155167
#define TIMER_INTR_NAME TIMER2_COMPA_vect
168+
#endif
156169

157170
#define TIMER_CONFIG_KHZ(val) ({ \
158171
const uint8_t pwmval = SYSCLOCK / 2000 / (val); \

examples/IRrecvDemo/IRrecvDemo.ino

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,22 @@
88

99
#include <IRremote.h>
1010

11+
#ifdef ESP32
12+
int RECV_PIN = 35;
13+
#else
1114
int RECV_PIN = 11;
15+
#endif
1216

1317
IRrecv irrecv(RECV_PIN);
1418

1519
decode_results results;
1620

1721
void setup()
1822
{
19-
Serial.begin(9600);
23+
Serial.begin(115200);
24+
Serial.println("Enabling IRin");
2025
irrecv.enableIRIn(); // Start the receiver
26+
Serial.println("Enabled IRin");
2127
}
2228

2329
void loop() {

examples/IRrecvDump/IRrecvDump.ino

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

2024
IRrecv irrecv(RECV_PIN);
2125

2226
decode_results results;
2327

2428
void setup()
2529
{
26-
Serial.begin(9600);
30+
Serial.begin(115200);
2731
irrecv.enableIRIn(); // Start the receiver
2832
}
2933

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 = 11;
9+
int recvPin = 35;
1010
IRrecv irrecv(recvPin);
1111

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

irRecv.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#include "IRremote.h"
22
#include "IRremoteInt.h"
3+
4+
#ifdef ESP32
5+
hw_timer_t *timer;
6+
void onTimer(); // defined in IRremote.cpp
7+
#endif
38

49
//+=============================================================================
510
// Decodes the received IR message
@@ -117,6 +122,15 @@ IRrecv::IRrecv (int recvpin, int blinkpin)
117122
//
118123
void IRrecv::enableIRIn ( )
119124
{
125+
// Interrupt Service Routine - Fires every 50uS
126+
#ifdef ESP32
127+
// 3 timers, choose #1, 80 divider nanosecond precision, 1 to count up
128+
timer = timerBegin(1, 80, 1);
129+
timerAttachInterrupt(timer, &onTimer, 1);
130+
// every 50ns, autoreload = true
131+
timerAlarmWrite(timer, 50, true);
132+
timerAlarmEnable(timer);
133+
#else
120134
cli();
121135
// Setup pulse clock timer interrupt
122136
// Prescale /8 (16M/8 = 0.5 microseconds per tick)
@@ -130,6 +144,7 @@ void IRrecv::enableIRIn ( )
130144
TIMER_RESET;
131145

132146
sei(); // enable interrupts
147+
#endif
133148

134149
// Initialize state machine variables
135150
irparams.rcvstate = STATE_IDLE;

irSend.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ void IRsend::space (unsigned int time)
5454
//
5555
void IRsend::enableIROut (int khz)
5656
{
57+
// FIXME: implement ESP32 support
58+
#ifndef ESP32
5759
// Disable the Timer2 Interrupt (which is used for receiving IR)
5860
TIMER_DISABLE_INTR; //Timer2 Overflow Interrupt
5961

@@ -66,6 +68,7 @@ void IRsend::enableIROut (int khz)
6668
// CS2 = 000: no prescaling
6769
// The top value for the timer. The modulation frequency will be SYSCLOCK / 2 / OCR2A.
6870
TIMER_CONFIG_KHZ(khz);
71+
#endif
6972
}
7073

7174
//+=============================================================================

0 commit comments

Comments
 (0)