Skip to content

Commit d4d8065

Browse files
committed
Fix idle level issue. Code cleanup
1 parent 64c452b commit d4d8065

File tree

3 files changed

+20
-30
lines changed

3 files changed

+20
-30
lines changed

src/IRremoteESP8266.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,11 +1035,11 @@ typedef volatile const uint16_t atomic_const_uint16_t;
10351035
// Turning this on may make for more reliable transmission on the ESP32
10361036
// platform. [Disabled by default]
10371037
#ifndef ENABLE_ESP32_RMT_USAGE
1038-
#if ESP32
1038+
#if defined(ESP32)
10391039
#define ENABLE_ESP32_RMT_USAGE true // Currently enabled for testing.
1040-
#else // ESP32
1040+
#else // defined(ESP32)
10411041
#define ENABLE_ESP32_RMT_USAGE false
1042-
#endif // ESP32
1042+
#endif // defined(ESP32)
10431043
#endif // ENABLE_ESP32_RMT_USAGE
10441044

10451045
/// Enumerator for defining and numbering of supported IR protocol.

src/IRsend.cpp

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ IRsend::IRsend(uint16_t IRsendPin, bool inverted,
5959
// TX specific configurations
6060
_rmt_config.tx_config.loop_en = false;
6161
_rmt_config.tx_config.carrier_en = true;
62+
_rmt_config.tx_config.carrier_level = RMT_CARRIER_LEVEL_HIGH;
6263
_rmt_config.tx_config.carrier_duty_percent = _dutycycle;
6364
_rmt_config.tx_config.carrier_freq_hz = 38000; // Default IR TX frequency.
64-
65-
_rmt_config.tx_config.idle_output_en = false;
65+
_rmt_config.tx_config.idle_output_en = true;
6666
_rmt_config.tx_config.idle_level = inverted ? RMT_IDLE_LEVEL_HIGH :
6767
RMT_IDLE_LEVEL_LOW;
6868
#endif // ENABLE_ESP32_RMT_USAGE
@@ -82,8 +82,9 @@ IRsend::~IRsend(void) {
8282
void IRsend::begin() {
8383
#ifndef UNIT_TEST
8484
#if ENABLE_ESP32_RMT_USAGE
85-
_rmt_config.tx_config.idle_output_en = true;
86-
rmt_config(&_rmt_config) && rmt_driver_install(_rmt_config.channel, 0, 0);
85+
assert(!rmt_config(&_rmt_config));
86+
assert(!rmt_driver_install(_rmt_config.channel, 0, 0));
87+
_is_rmt_driver_installed = true;
8788
#else
8889
pinMode(IRpin, OUTPUT);
8990
#endif // ENABLE_ESP32_RMT_USAGE
@@ -94,35 +95,27 @@ void IRsend::begin() {
9495
#if ENABLE_ESP32_RMT_USAGE
9596
/// Disable the pin for output.
9697
void IRsend::end() {
97-
rmt_driver_uninstall(_rmt_config.channel);
98+
if (_is_rmt_driver_installed)
99+
_is_rmt_driver_installed = rmt_driver_uninstall(_rmt_config.channel);
98100
}
99101
#endif // ENABLE_ESP32_RMT_USAGE
100102

101103
/// Turn off the IR LED.
102104
void IRsend::ledOff() {
103105
#ifndef UNIT_TEST
104-
#if ENABLE_ESP32_RMT_USAGE
105-
rmt_set_idle_level(_rmt_config.channel,
106-
_rmt_config.tx_config.idle_output_en,
107-
(outputOn == LOW) ? RMT_IDLE_LEVEL_HIGH
108-
: RMT_IDLE_LEVEL_LOW);
109-
#else
106+
#if !(ENABLE_ESP32_RMT_USAGE)
110107
digitalWrite(IRpin, outputOff);
111-
#endif // ENABLE_ESP32_RMT_USAGE
108+
#endif // !(ENABLE_ESP32_RMT_USAGE)
109+
112110
#endif // UNIT_TEST
113111
}
114112

115113
/// Turn on the IR LED.
116114
void IRsend::ledOn() {
117115
#ifndef UNIT_TEST
118-
#if ENABLE_ESP32_RMT_USAGE
119-
rmt_set_idle_level(_rmt_config.channel,
120-
_rmt_config.tx_config.idle_output_en,
121-
(outputOn == HIGH) ? RMT_IDLE_LEVEL_HIGH
122-
: RMT_IDLE_LEVEL_LOW);
123-
#else
116+
#if !(ENABLE_ESP32_RMT_USAGE)
124117
digitalWrite(IRpin, outputOn);
125-
#endif // ENABLE_ESP32_RMT_USAGE
118+
#endif // !(ENABLE_ESP32_RMT_USAGE)
126119
#endif // UNIT_TEST
127120
}
128121

@@ -172,8 +165,8 @@ void IRsend::enableIROut(uint32_t freq, uint8_t duty) {
172165
#if ENABLE_ESP32_RMT_USAGE
173166
_rmt_config.tx_config.carrier_duty_percent = _dutycycle;
174167
_rmt_config.tx_config.carrier_freq_hz = freq;
175-
// (Re)Load up the rmt config and if the config is okay, install the driver.
176-
rmt_config(&_rmt_config);
168+
// Update rmt config.
169+
assert(!rmt_config(&_rmt_config));
177170
#endif // ENABLE_ESP32_RMT_USAGE
178171
}
179172

@@ -227,15 +220,11 @@ uint16_t IRsend::mark(uint16_t usec) {
227220
#if ENABLE_ESP32_RMT_USAGE
228221
// ESP32 does this very differently if using the RMT drvier
229222
_rmt_config.tx_config.carrier_duty_percent = _dutycycle;
230-
static const rmt_item32_t payload[] = {
231-
// mark signal
223+
static const rmt_item32_t mark_signal[] = {
232224
{{{ usec, 1, 0, 0 }}}, // mark
233-
// RMT end marker
234-
{{{ 0, 1, 0, 0 }}}
235225
};
236226
// Try sending the mark signal via rmt.
237-
if (!rmt_write_items(RMT_CHANNEL_0, payload,
238-
sizeof(payload) / sizeof(payload[0]), true))
227+
if (!rmt_write_items(_rmt_config.channel, mark_signal, 1, true))
239228
return 0; // We failed. So report no pulses.
240229
// Calculate & return how many pulses we should have sent. We can't actually
241230
// counthow many were really sent.

src/IRsend.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,7 @@ class IRsend {
957957
#endif // SEND_SONY
958958
#if ENABLE_ESP32_RMT_USAGE
959959
rmt_config_t _rmt_config;
960+
bool _is_rmt_driver_installed = false;
960961
#endif // ENABLE_ESP32_RMT_USAGE
961962
};
962963

0 commit comments

Comments
 (0)