60 lines
1.5 KiB
C++
60 lines
1.5 KiB
C++
#include <Arduino.h>
|
|
#include "config.h"
|
|
#include "app_config.h"
|
|
#include "relays.h"
|
|
|
|
RelayState relays;
|
|
|
|
static uint8_t relayShiftRegisterState = 0;
|
|
|
|
static void writeRelayShiftRegister()
|
|
{
|
|
digitalWrite(LILYGO_T_RELAY_S3_LATCH_PIN, LOW);
|
|
shiftOut(
|
|
LILYGO_T_RELAY_S3_DATA_PIN,
|
|
LILYGO_T_RELAY_S3_CLOCK_PIN,
|
|
MSBFIRST,
|
|
relayShiftRegisterState
|
|
);
|
|
digitalWrite(LILYGO_T_RELAY_S3_LATCH_PIN, HIGH);
|
|
}
|
|
|
|
void initRelays() {
|
|
pinMode(LILYGO_T_RELAY_S3_DATA_PIN, OUTPUT);
|
|
pinMode(LILYGO_T_RELAY_S3_CLOCK_PIN, OUTPUT);
|
|
pinMode(LILYGO_T_RELAY_S3_LATCH_PIN, OUTPUT);
|
|
pinMode(LILYGO_T_RELAY_S3_OE_PIN, OUTPUT);
|
|
|
|
// Keep outputs disabled while clearing the shift register.
|
|
digitalWrite(LILYGO_T_RELAY_S3_OE_PIN, HIGH);
|
|
relayShiftRegisterState = 0;
|
|
writeRelayShiftRegister();
|
|
|
|
// Enable relay outputs after known-off state is written.
|
|
digitalWrite(LILYGO_T_RELAY_S3_OE_PIN, LOW);
|
|
|
|
updateRelayOutputs();
|
|
}
|
|
|
|
void updateRelayOutputs() {
|
|
uint8_t nextState = 0;
|
|
|
|
for (int i = 0; i < MAX_RELAYS; i++) {
|
|
if (!appConfig.relays[i].available) {
|
|
continue;
|
|
}
|
|
|
|
const uint8_t channel = appConfig.relays[i].hardwareChannel;
|
|
if (channel < 1 || channel > LILYGO_T_RELAY_S3_RELAY_COUNT) {
|
|
continue;
|
|
}
|
|
|
|
if (relays.state[i]) {
|
|
nextState |= (1U << (channel - 1));
|
|
}
|
|
}
|
|
|
|
relayShiftRegisterState = nextState;
|
|
writeRelayShiftRegister();
|
|
}
|