diff --git a/README.md b/README.md index 8005e2b..1e0292e 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,13 @@ Implementation example: ![HyperSPI](https://user-images.githubusercontent.com/85223482/222923979-f344349a-1f8b-4195-94ca-51721923359e.png) +# External relay power control +You can configure LED power pin in the `platformio.ini` to power off LEDs while not in use. +Review the comments at the top of the file: +* `LED_POWER_PIN` - This is the data pin external power control + +Note: For static color configuration this mechanism will turn off the LEDs. To counter this enable "Continuous Output" in HyperHDR "Smoothing" module. For esp32 and relay control, you may want to disable the "Handshake" option in the Adalight HyperHDR driver to avoid the relay immediately shutting down when resetting the device while initializing the connection. + # Some benchmark results ESP32 MH-ET LIVE mini is capable of 4Mb serial port speed and ESP32-S2 lolin mini is capable of 5Mb. But to give equal chances for a single-segment mode all models were tested using the default speed of 2Mb which should saturate Neopixel data line. Parallel multi-segment mode uses the highest option available because communication performance is critical here. diff --git a/include/main.h b/include/main.h index 0069a12..69e5cee 100644 --- a/include/main.h +++ b/include/main.h @@ -61,6 +61,10 @@ bool serialTaskHandler() } } +#if defined(LED_POWER_PIN) + powerControl.update(incomingSize > 0); +#endif + return (incomingSize > 0); } diff --git a/include/powercontrol.h b/include/powercontrol.h new file mode 100644 index 0000000..8892a30 --- /dev/null +++ b/include/powercontrol.h @@ -0,0 +1,89 @@ +/* powercontrol.h +* +* MIT License +* +* Copyright (c) 2023 awawa-dev +* +* https://github.com/awawa-dev/HyperSerialESP32 +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in all +* copies or substantial portions of the Software. + +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +* SOFTWARE. + */ + +#ifndef POWERCONTROL_H +#define POWERCONTROL_H + + +/** + * @brief Contains logic for turning on and off the power to leds using external relay + * + */ +class +{ + // timeout after which the leds will be turned off if no reset is applied + const unsigned long POWER_OFF_PERIOD = 5 * 1000; + + // last timestamp power off timer got reset + volatile unsigned long lastPowerOffResetTimestamp = 0; + + // caching the PIN state to avoid unnecessary calls to the GPIO register + volatile int currentPowerPinMode = LOW; + + public: + void init() + { + pinMode(LED_POWER_PIN, OUTPUT); + lastPowerOffResetTimestamp = millis(); + powerOn(); + } + + inline void powerOn() + { + if (currentPowerPinMode != HIGH) + { + currentPowerPinMode = HIGH; + digitalWrite(LED_POWER_PIN, currentPowerPinMode); + } + } + + inline void powerOff() + { + if (currentPowerPinMode != LOW) + { + currentPowerPinMode = LOW; + digitalWrite(LED_POWER_PIN, currentPowerPinMode); + } + } + + void update(bool hasData) + { + if (hasData) + { + powerOn(); + lastPowerOffResetTimestamp = millis(); + } + else if (millis() - lastPowerOffResetTimestamp > POWER_OFF_PERIOD) + { + powerOff(); + } + } + +} powerControl; + + +#endif diff --git a/platformio.ini b/platformio.ini index 30b1694..49e80b0 100644 --- a/platformio.ini +++ b/platformio.ini @@ -2,7 +2,8 @@ ; SERIALCOM_SPEED = speed of the serial port (baud), global [env] section ; DATA_PIN = pin/GPIO for the LED strip data channel, specific [board] section ; CLOCK_PIN = pin/GPIO for the LED strip clock channel, specific [board] section -; +; LED_POWER_PIN = pin/GPIO for external relay power control, it will turn off (low state) if no serial data is received after 5 seconds + ; MULTI-SEGMENT SUPPORT ; You can define second segment to handle. Add following parameters (with -D prefix to the build_flags sections). ; SECOND_SEGMENT_START_INDEX = start index of the second segment diff --git a/src/main.cpp b/src/main.cpp index b151d43..35d74b8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -156,8 +156,15 @@ #endif #define SerialPort Serial + +#if defined(LED_POWER_PIN) + #pragma message(VAR_NAME_VALUE(LED_POWER_PIN)) + #include "powercontrol.h" +#endif + #include "main.h" + /** * @brief separete thread for handling incoming data using cyclic buffer * @@ -232,6 +239,12 @@ void setup() delay(50); #endif + #if defined(LED_POWER_PIN) + Serial.write("LED_POWER_PIN = "); + Serial.println(LED_POWER_PIN); + powerControl.init(); + #endif + if (multicore) { // create a semaphore to synchronize threads