From 420d1ac7435b777444fe366d789b5db1f7c3d70d Mon Sep 17 00:00:00 2001 From: N-Storm Date: Tue, 2 Apr 2019 13:38:32 +0300 Subject: [PATCH] New bitbang method for ESP8266 using built-in startWaveform() function (more accurate carrier generation -> better range) --- IRSender.h | 13 +++++++++++++ IRSenderESP8266.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 IRSenderESP8266.cpp diff --git a/IRSender.h b/IRSender.h index a6bb13c..03800c8 100644 --- a/IRSender.h +++ b/IRSender.h @@ -8,6 +8,7 @@ #ifdef ESP8266 #include // From IRremoteESP8266 library +#include #endif class IRSender @@ -70,6 +71,18 @@ class IRSenderIRremoteESP8266 : public IRSender private: IRsend _ir; }; + +class IRSenderESP8266 : public IRSender +{ + public: + IRSenderESP8266(uint8_t pin); + void setFrequency(int frequency); + void space(int spaceLength); + void mark(int markLength); + + protected: + uint32_t _halfPeriodicTime; +}; #endif #endif diff --git a/IRSenderESP8266.cpp b/IRSenderESP8266.cpp new file mode 100644 index 0000000..f199bc7 --- /dev/null +++ b/IRSenderESP8266.cpp @@ -0,0 +1,45 @@ +#include +#ifdef ESP8266 +#include +#include + +// Send IR using the 'bit banging' with startWaveform function on ESP8266 etc. + +IRSenderESP8266::IRSenderESP8266(uint8_t pin) : IRSender(pin) +{ + pinMode(_pin, OUTPUT); +} + + +void IRSenderESP8266::setFrequency(int frequency) +{ + // Enables IR output. The khz value controls the modulation frequency in kilohertz. + _halfPeriodicTime = 500/frequency; // T = 1/f but we need T/2 in microsecond and f is in kHz +} + + +// Send an IR 'mark' symbol, i.e. transmitter ON +void IRSenderESP8266::mark(int markLength) +{ + long beginning = micros(); + + startWaveform(_pin, _halfPeriodicTime, _halfPeriodicTime, markLength); + while((int)(micros() - beginning) < markLength); + + stopWaveform(_pin); + digitalWrite(_pin, LOW); +} + + +// Send an IR 'space' symbol, i.e. transmitter OFF +void IRSenderESP8266::space(int spaceLength) +{ + digitalWrite(_pin, LOW); + + if (spaceLength < 16383) { + delayMicroseconds(spaceLength); + } else { + delay(spaceLength/1000); + } +} +#endif