diff --git a/IRSender.cpp b/IRSender.cpp index 1749546..e45dcab 100644 --- a/IRSender.cpp +++ b/IRSender.cpp @@ -44,6 +44,14 @@ uint8_t IRSender::bitReverse(uint8_t x) } +// Some boards have inverted signals, so we need to set the pin to HIGH to turn the IR transmitter off +// For example M5Stick-C where LED positive on VCC and negative on pin 9 +void IRSender::invert(bool inverted) +{ + _inverted = inverted; +} + + // Definitions of virtual functions void IRSender::setFrequency(int) {}; void IRSender::space(int) {}; diff --git a/IRSender.h b/IRSender.h index 3a8e1b4..a69cbfe 100644 --- a/IRSender.h +++ b/IRSender.h @@ -29,11 +29,13 @@ class IRSender virtual void setFrequency(int frequency); void sendIRbyte(uint8_t sendByte, int bitMarkLength, int zeroSpaceLength, int oneSpaceLength); uint8_t bitReverse(uint8_t x); + virtual void invert(bool inverted); virtual void space(int spaceLength); virtual void mark(int markLength); protected: uint8_t _pin; + bool _inverted = false; }; diff --git a/IRSenderESP32.cpp b/IRSenderESP32.cpp index 135b15e..07f7028 100644 --- a/IRSenderESP32.cpp +++ b/IRSenderESP32.cpp @@ -13,6 +13,10 @@ IRSenderESP32::IRSenderESP32(uint8_t pin, uint8_t pwmChannel) : IRSender(pin) { _pwmChannel = pwmChannel; pinMode(_pin, OUTPUT); + // If we have an inverted signal, we need to set the pin from default LOW + // to HIGH to make it off + if (_inverted) + digitalWrite(_pin, HIGH); } void IRSenderESP32::setFrequency(int frequency) @@ -37,13 +41,19 @@ void IRSenderESP32::mark(int markLength) #endif // ESP_ARDUINO_VERSION_MAJOR >= 3 while((int)(micros() - beginning) < markLength); gpio_reset_pin(static_cast(_pin)); - digitalWrite(_pin, LOW); + if (_inverted) + digitalWrite(_pin, HIGH); + else + digitalWrite(_pin, LOW); } // Send an IR 'space' symbol, i.e. transmitter OFF void IRSenderESP32::space(int spaceLength) { - digitalWrite(_pin, LOW); + if (_inverted) + digitalWrite(_pin, HIGH); + else + digitalWrite(_pin, LOW); if (spaceLength < 16383) { delayMicroseconds(spaceLength);