From 011204472a661c82c4ddbfd3786a78031fa0f4fd Mon Sep 17 00:00:00 2001 From: N-Storm Date: Tue, 2 Apr 2019 13:50:51 +0300 Subject: [PATCH] Alternative ESP8266 bitbang carrirer generation method --- IRSender.h | 12 ++++++++++ IRSenderESP8266Alt.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 IRSenderESP8266Alt.cpp diff --git a/IRSender.h b/IRSender.h index 03800c8..4586c89 100644 --- a/IRSender.h +++ b/IRSender.h @@ -83,6 +83,18 @@ class IRSenderESP8266 : public IRSender protected: uint32_t _halfPeriodicTime; }; + +class IRSenderESP8266Alt : public IRSender +{ + public: + IRSenderESP8266Alt(uint8_t pin); + void setFrequency(int frequency); + void space(int spaceLength); + void mark(int markLength); + + protected: + uint32_t _halfPeriodicTime; +}; #endif #endif diff --git a/IRSenderESP8266Alt.cpp b/IRSenderESP8266Alt.cpp new file mode 100644 index 0000000..7d797e4 --- /dev/null +++ b/IRSenderESP8266Alt.cpp @@ -0,0 +1,51 @@ +#include +#ifdef ESP8266 +#include + +// Send IR using the 'bit banging' on ESP8266 using alternative ESP.getCycleCount() method + +// Cycles compensation due to while cycles and polling ESP.getCycleCount() +#define ESP8266_CYCLES_COMPENSATION 33 + +IRSenderESP8266Alt::IRSenderESP8266Alt(uint8_t pin) : IRSender(pin) +{ + pinMode(_pin, OUTPUT); +} + + +void IRSenderESP8266Alt::setFrequency(int frequency) +{ + // Enables IR output. The khz value controls the modulation frequency in kilohertz. + _halfPeriodicTime = F_CPU / (frequency * 1000 * 2); // This one stores half period time in CPU ticks +} + + +// Send an IR 'mark' symbol, i.e. transmitter ON +void IRSenderESP8266Alt::mark(int markLength) +{ + long beginning = micros(); + + while((int)(micros() - beginning) < markLength){ + uint32_t periodStart = ESP.getCycleCount(); + digitalWrite(_pin, HIGH); + while (ESP.getCycleCount() - periodStart < _halfPeriodicTime - ESP8266_CYCLES_COMPENSATION); + + periodStart = ESP.getCycleCount(); + digitalWrite(_pin, LOW); + while (ESP.getCycleCount() - periodStart < _halfPeriodicTime - ESP8266_CYCLES_COMPENSATION); + } +} + + +// Send an IR 'space' symbol, i.e. transmitter OFF +void IRSenderESP8266Alt::space(int spaceLength) +{ + digitalWrite(_pin, LOW); + + if (spaceLength < 16383) { + delayMicroseconds(spaceLength); + } else { + delay(spaceLength/1000); + } +} +#endif