diff --git a/IRremote.cpp b/IRremote.cpp index b438bc1..e811cfc 100644 --- a/IRremote.cpp +++ b/IRremote.cpp @@ -18,16 +18,17 @@ // Whynter A/C ARC-110WD added by Francesco Meschia //****************************************************************************** -#ifndef ESP32 -#include -#endif - // Defining IR_GLOBAL here allows us to declare the instantiation of global variables #define IR_GLOBAL # include "IRremote.h" # include "IRremoteInt.h" #undef IR_GLOBAL +#ifndef IR_TIMER_USE_ESP32 +#include +#endif + + //+============================================================================= // The match functions were (apparently) originally MACROs to improve code speed // (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) // As soon as first MARK arrives: // Gap width is recorded; Ready is cleared; New logging starts // -#ifdef ESP32 +#ifdef IR_TIMER_USE_ESP32 void IRTimer() #else ISR (TIMER_INTR_NAME) diff --git a/boarddefs.h b/boarddefs.h index ce8a4f9..c126b54 100644 --- a/boarddefs.h +++ b/boarddefs.h @@ -39,13 +39,14 @@ # define BLINKLED_ON() (PORTD |= B00000001) # define BLINKLED_OFF() (PORTD &= B11111110) +// No system LED on ESP32, disable blinking #elif defined(ESP32) # define BLINKLED 255 # define BLINKLED_ON() 1 # define BLINKLED_OFF() 1 #else # define BLINKLED 13 - #define BLINKLED_ON() (PORTB |= B00100000) +# define BLINKLED_ON() (PORTB |= B00100000) # define BLINKLED_OFF() (PORTB &= B11011111) #endif @@ -129,14 +130,16 @@ // ATtiny84 #elif defined(__AVR_ATtiny84__) - #define IR_USE_TIMER1 // tx = pin 6 + #define IR_USE_TIMER1 // tx = pin 6 //ATtiny85 #elif defined(__AVR_ATtiny85__) - #define IR_USE_TIMER_TINY0 // tx = pin 1 + #define IR_USE_TIMER_TINY0 // tx = pin 1 // Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, Nano, etc // ATmega48, ATmega88, ATmega168, ATmega328 +#elif defined(ESP32) + #define IR_TIMER_USE_ESP32 #else //#define IR_USE_TIMER1 // tx = pin 9 #define IR_USE_TIMER2 // tx = pin 3 @@ -151,21 +154,12 @@ // #if defined(IR_USE_TIMER2) -#ifdef ESP32 // Used in irSend, not implemented yet (FIXME) -#define TIMER_RESET 1 -#define TIMER_ENABLE_PWM 1 -#define TIMER_DISABLE_PWM Serial.println("IRsend not implemented for ESP32 yet"); -#define TIMER_ENABLE_INTR 1 -#define TIMER_DISABLE_INTR 1 -#define TIMER_INTR_NAME 1 -#else #define TIMER_RESET #define TIMER_ENABLE_PWM (TCCR2A |= _BV(COM2B1)) #define TIMER_DISABLE_PWM (TCCR2A &= ~(_BV(COM2B1))) #define TIMER_ENABLE_INTR (TIMSK2 = _BV(OCIE2A)) #define TIMER_DISABLE_INTR (TIMSK2 = 0) #define TIMER_INTR_NAME TIMER2_COMPA_vect -#endif #define TIMER_CONFIG_KHZ(val) ({ \ const uint8_t pwmval = SYSCLOCK / 2000 / (val); \ @@ -551,6 +545,28 @@ #define TIMER_PWM_PIN 1 /* ATtiny85 */ +//--------------------------------------------------------- +// ESP32 (ESP8266 should likely be added here too) +// + +// ESP32 has it own timer API and does not use these macros, but to avoid ifdef'ing +// them out in the common code, they are defined to no-op. This allows the code to compile +// (which it wouldn't otherwise) but irsend will not work until ESP32 specific code is written +// for that -- merlin +// As a warning, sending timing specific code from an ESP32 can be challenging if you need 100% +// reliability because the arduino code may be interrupted and cause your sent waveform to be the +// wrong length. This is specifically an issue for neopixels which require 800Khz resolution. +// IR may just work as is with the common code since it's lower frequency, but if not, the other +// way to do this on ESP32 is using the RMT built in driver like in this incomplete library below +// https://github.com/ExploreEmbedded/ESP32_RMT +#elif defined(IR_TIMER_USE_ESP32) +#define TIMER_RESET 1 +#define TIMER_ENABLE_PWM 1 +#define TIMER_DISABLE_PWM Serial.println("IRsend not implemented for ESP32 yet"); +#define TIMER_ENABLE_INTR 1 +#define TIMER_DISABLE_INTR 1 +#define TIMER_INTR_NAME 1 + //--------------------------------------------------------- // Unknown Timer // diff --git a/examples/IRrecvDemo/IRrecvDemo.ino b/examples/IRrecvDemo/IRrecvDemo.ino index 4635b2c..3e2d280 100644 --- a/examples/IRrecvDemo/IRrecvDemo.ino +++ b/examples/IRrecvDemo/IRrecvDemo.ino @@ -8,11 +8,7 @@ #include -#ifdef ESP32 -int RECV_PIN = 35; -#else int RECV_PIN = 11; -#endif IRrecv irrecv(RECV_PIN); @@ -20,7 +16,9 @@ decode_results results; void setup() { - Serial.begin(115200); + Serial.begin(9600); + // In case the interrupt driver crashes on setup, give a clue + // to the user what's going on. Serial.println("Enabling IRin"); irrecv.enableIRIn(); // Start the receiver Serial.println("Enabled IRin"); diff --git a/examples/IRrecvDump/IRrecvDump.ino b/examples/IRrecvDump/IRrecvDump.ino index 9a21b56..990c3f6 100644 --- a/examples/IRrecvDump/IRrecvDump.ino +++ b/examples/IRrecvDump/IRrecvDump.ino @@ -15,11 +15,7 @@ * You can change this to another available Arduino Pin. * Your IR receiver should be connected to the pin defined here */ -#ifdef ESP32 -int RECV_PIN = 35; -#else int RECV_PIN = 11; -#endif IRrecv irrecv(RECV_PIN); diff --git a/examples/IRrecvDumpV2/IRrecvDumpV2.ino b/examples/IRrecvDumpV2/IRrecvDumpV2.ino index 570baba..7256127 100644 --- a/examples/IRrecvDumpV2/IRrecvDumpV2.ino +++ b/examples/IRrecvDumpV2/IRrecvDumpV2.ino @@ -6,7 +6,7 @@ //------------------------------------------------------------------------------ // Tell IRremote which Arduino pin is connected to the IR Receiver (TSOP4838) // -int recvPin = 35; +int recvPin = 11; IRrecv irrecv(recvPin); //+============================================================================= @@ -14,7 +14,7 @@ IRrecv irrecv(recvPin); // void setup ( ) { - Serial.begin(115200); // Status message will be sent to PC at 9600 baud + Serial.begin(9600); // Status message will be sent to PC at 9600 baud irrecv.enableIRIn(); // Start the receiver } diff --git a/irRecv.cpp b/irRecv.cpp index a15ac0f..6631fb7 100644 --- a/irRecv.cpp +++ b/irRecv.cpp @@ -124,6 +124,8 @@ void IRrecv::enableIRIn ( ) { // Interrupt Service Routine - Fires every 50uS #ifdef ESP32 + // ESP32 has a proper API to setup timers, no weird chip macros needed + // simply call the readable API versions :) // 3 timers, choose #1, 80 divider nanosecond precision, 1 to count up timer = timerBegin(1, 80, 1); timerAttachInterrupt(timer, &IRTimer, 1); diff --git a/irSend.cpp b/irSend.cpp index 6191aa6..c3ef3ff 100644 --- a/irSend.cpp +++ b/irSend.cpp @@ -54,7 +54,7 @@ void IRsend::space (unsigned int time) // void IRsend::enableIROut (int khz) { -// FIXME: implement ESP32 support +// FIXME: implement ESP32 support, see IR_TIMER_USE_ESP32 in boarddefs.h #ifndef ESP32 // Disable the Timer2 Interrupt (which is used for receiving IR) TIMER_DISABLE_INTR; //Timer2 Overflow Interrupt