Add define to set data pins to HIGH when relay is off (#2478)

* Add ESP32_DATA_IDLE_HIGH to enable data pin to go HIGH when relay is off

* forgot to remove Serial.print for ESP32_DATA_IDLE_HIGH

* forgot another ifdef preventing compilation on non-esp32 boards

* Extra checks that the bus is actually an RMT bus

Could still blow on new ESP32 variants, but now in a mergable state.

Co-authored-by: Christian Schwinne <cschwinne@gmail.com>
This commit is contained in:
c3n 2022-10-21 03:16:31 +02:00 committed by GitHub
parent c2ac215d43
commit e1365f185c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -300,6 +300,29 @@ void handleButton()
if (analog) lastRead = now; if (analog) lastRead = now;
} }
// If enabled, RMT idle level is set to HIGH when off
// to prevent leakage current when using an N-channel MOSFET to toggle LED power
#ifdef ESP32_DATA_IDLE_HIGH
void esp32RMTInvertIdle()
{
bool idle_out;
for (uint8_t u = 0; u < busses.getNumBusses(); u++)
{
if (u > 7) return; // only 8 RMT channels, TODO: ESP32 variants have less RMT channels
Bus *bus = busses.getBus(u);
if (!bus || bus->getLength()==0 || !IS_DIGITAL(bus->getType()) || IS_2PIN(bus->getType())) continue;
//assumes that bus number to rmt channel mapping stays 1:1
rmt_channel_t ch = static_cast<rmt_channel_t>(u);
rmt_idle_level_t lvl;
rmt_get_idle_level(ch, &idle_out, &lvl);
if (lvl == RMT_IDLE_LEVEL_HIGH) lvl = RMT_IDLE_LEVEL_LOW;
else if (lvl == RMT_IDLE_LEVEL_LOW) lvl = RMT_IDLE_LEVEL_HIGH;
else continue;
rmt_set_idle_level(ch, idle_out, lvl);
}
}
#endif
void handleIO() void handleIO()
{ {
handleButton(); handleButton();
@ -310,6 +333,9 @@ void handleIO()
lastOnTime = millis(); lastOnTime = millis();
if (offMode) if (offMode)
{ {
#ifdef ESP32_DATA_IDLE_HIGH
esp32RMTInvertIdle();
#endif
if (rlyPin>=0) { if (rlyPin>=0) {
pinMode(rlyPin, OUTPUT); pinMode(rlyPin, OUTPUT);
digitalWrite(rlyPin, rlyMde); digitalWrite(rlyPin, rlyMde);
@ -328,6 +354,9 @@ void handleIO()
digitalWrite(LED_BUILTIN, HIGH); digitalWrite(LED_BUILTIN, HIGH);
} }
#endif #endif
#ifdef ESP32_DATA_IDLE_HIGH
esp32RMTInvertIdle();
#endif
if (rlyPin>=0) { if (rlyPin>=0) {
pinMode(rlyPin, OUTPUT); pinMode(rlyPin, OUTPUT);
digitalWrite(rlyPin, !rlyMde); digitalWrite(rlyPin, !rlyMde);