Skip to content

Commit

Permalink
Power relay control (#41)
Browse files Browse the repository at this point in the history
* implemented external LED power control

* Update powercontrol.h

removed tab indentation

* Add caching for digitalWrites

---------

Co-authored-by: Awawa <[email protected]>
  • Loading branch information
phil535 and awawa-dev authored Aug 26, 2023
1 parent de46939 commit 88ed3c9
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions include/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ bool serialTaskHandler()
}
}

#if defined(LED_POWER_PIN)
powerControl.update(incomingSize > 0);
#endif

return (incomingSize > 0);
}

Expand Down
89 changes: 89 additions & 0 deletions include/powercontrol.h
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 88ed3c9

Please sign in to comment.