Alternative ESP8266 bitbang carrirer generation method

This commit is contained in:
N-Storm 2019-04-02 13:50:51 +03:00
parent 420d1ac743
commit 011204472a
2 changed files with 63 additions and 0 deletions

View File

@ -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

51
IRSenderESP8266Alt.cpp Normal file
View File

@ -0,0 +1,51 @@
#include <Arduino.h>
#ifdef ESP8266
#include <IRSender.h>
// 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