-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathIRSenderESP32.cpp
70 lines (63 loc) · 1.96 KB
/
IRSenderESP32.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <Arduino.h>
// IRSender implementation for ESP32
// Tested on R51M/E control with SENSEI air conditioner
// Maksym Krasovskyi
#if defined ESP32
#include <IRSender.h>
#if ( defined(ESP_ARDUINO_VERSION_MAJOR) && (ESP_ARDUINO_VERSION_MAJOR >= 3) )
#include <driver/gpio.h>
#endif // ESP_ARDUINO_VERSION_MAJOR >= 3
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)
{
_frequency = frequency * 1000;
}
// Send an IR 'mark' symbol, i.e. transmitter ON
void IRSenderESP32::mark(int markLength)
{
#if ( defined(ESP_ARDUINO_VERSION_MAJOR) && (ESP_ARDUINO_VERSION_MAJOR >= 3) )
ledcAttach(_pin, _frequency, 8);
#else // ESP_ARDUINO_VERSION_MAJOR >= 3
ledcSetup(_pwmChannel, _frequency, 8);
ledcAttachPin(_pin, _pwmChannel);
#endif // ESP_ARDUINO_VERSION_MAJOR >= 3
long beginning = micros();
#if ( defined(ESP_ARDUINO_VERSION_MAJOR) && (ESP_ARDUINO_VERSION_MAJOR >= 3) )
ledcWrite(_pin, 127);
#else // ESP_ARDUINO_VERSION_MAJOR >= 3
ledcWrite(_pwmChannel, 127);
#endif // ESP_ARDUINO_VERSION_MAJOR >= 3
while((int)(micros() - beginning) < markLength);
#if ( defined(ESP_ARDUINO_VERSION_MAJOR) && (ESP_ARDUINO_VERSION_MAJOR >= 3) )
ledcDetach(_pin);
pinMode(_pin, OUTPUT);
#else // ESP_ARDUINO_VERSION_MAJOR >= 3
gpio_reset_pin(static_cast<gpio_num_t>(_pin));
#endif // ESP_ARDUINO_VERSION_MAJOR >= 3
if (_inverted)
digitalWrite(_pin, HIGH);
else
digitalWrite(_pin, LOW);
}
// Send an IR 'space' symbol, i.e. transmitter OFF
void IRSenderESP32::space(int spaceLength)
{
if (_inverted)
digitalWrite(_pin, HIGH);
else
digitalWrite(_pin, LOW);
if (spaceLength < 16383) {
delayMicroseconds(spaceLength);
} else {
delay(spaceLength/1000);
}
}
#endif