From c12ae7a50f55d63ac786188e8f8bb55ec0f2f9d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Ha=C3=9Fel?= Date: Thu, 28 Aug 2025 12:15:05 +0200 Subject: [PATCH] Initial (OFF, ON, BLINK) --- src/Initial.h | 31 +++++++++++++++++++++++++++++++ src/Output.h | 19 +++++++++++++++---- src/Relay.h | 8 ++++---- src/config.cpp | 8 ++++++++ src/config.h | 6 ++++++ src/http.cpp | 13 ++++++++----- 6 files changed, 72 insertions(+), 13 deletions(-) create mode 100644 src/Initial.h diff --git a/src/Initial.h b/src/Initial.h new file mode 100644 index 0000000..28427fa --- /dev/null +++ b/src/Initial.h @@ -0,0 +1,31 @@ +#ifndef INITIAL_H +#define INITIAL_H + +enum Initial { + INITIAL_OFF, + INITIAL_ON, + INITIAL_BLINK, +}; + +inline Initial stringToInitial(const String &str) { + if (str == "ON") { + return INITIAL_ON; + } + if (str == "BLINK") { + return INITIAL_BLINK; + } + return INITIAL_OFF; +} + +inline const char *initialToString(const Initial value) { + switch (value) { + case INITIAL_ON: + return "ON"; + case INITIAL_BLINK: + return "BLINK"; + default: + return "OFF"; + } +} + +#endif \ No newline at end of file diff --git a/src/Output.h b/src/Output.h index 47dcf5f..3d8c320 100644 --- a/src/Output.h +++ b/src/Output.h @@ -3,6 +3,8 @@ #include +#include "Initial.h" + class Output { protected: @@ -15,7 +17,7 @@ protected: const bool logState; - bool initial = false; + Initial initial = INITIAL_OFF; long onCount = -1; @@ -35,6 +37,15 @@ protected: digitalWrite(pin, state ^ inverted ? HIGH : LOW); } + void _applyInitial() { + _write(initial != INITIAL_OFF); + if (initial != INITIAL_BLINK) { + onCount = 0; + } else { + onCount = -1; + } + } + public: Output(const String &name, const uint8_t pin, const bool inverted, const bool logState) : name(name), pin(pin), inverted(inverted), logState(logState) { @@ -45,7 +56,7 @@ public: virtual void setup() { pinMode(pin, OUTPUT); - _write(initial); + _applyInitial(); } bool get() const { @@ -87,7 +98,7 @@ public: name = value; } - virtual void setInitial(const bool value) { + virtual void setInitial(const Initial value) { initial = value; } @@ -103,7 +114,7 @@ public: offMillis = value; } - bool getInitial() const { + Initial getInitial() const { return initial; } diff --git a/src/Relay.h b/src/Relay.h index 81ba12f..097aefc 100644 --- a/src/Relay.h +++ b/src/Relay.h @@ -19,10 +19,10 @@ public: void setup() override { Output::setup(); Output::setName(configRead(String("relayName") + index, nameFallback)); - Output::setInitial(configRead(String("relayInitial") + index, false)); + Output::setInitial(configRead(String("relayInitial") + index, INITIAL_OFF)); Output::setOnMillis(configRead(String("relayOnMillis") + index, 0L)); Output::setOffMillis(configRead(String("relayOffMillis") + index, 0L)); - _write(initial); + _applyInitial(); } void setName(const String &value) override { @@ -30,9 +30,9 @@ public: configWrite(String("relayName") + index, nameFallback, value); } - void setInitial(const bool value) override { + void setInitial(const Initial value) override { Output::setInitial(value); - configWrite(String("relayInitial") + index, false, value); + configWrite(String("relayInitial") + index, INITIAL_OFF, value); } void setOnMillis(const unsigned long value) override { diff --git a/src/config.cpp b/src/config.cpp index e18dcc5..6bd8ec7 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -73,3 +73,11 @@ bool configWrite(const String &name, const String &fallback, const String &value } return false; } + +Initial configRead(const String &name, const Initial &fallback) { + return stringToInitial(configRead(name, initialToString(fallback))); +} + +bool configWrite(const String &name, const Initial &fallback, const Initial &value) { + return configWrite(name, initialToString(fallback), initialToString(value)); +} diff --git a/src/config.h b/src/config.h index 217b769..268f50b 100644 --- a/src/config.h +++ b/src/config.h @@ -3,6 +3,8 @@ #include +#include "Initial.h" + void configSetup(); long configRead(const String &name, long fallback); @@ -21,4 +23,8 @@ String configRead(const String &name, const String &fallback); bool configWrite(const String &name, const String &fallback, const String &value); +Initial configRead(const String &name, const Initial &fallback); + +bool configWrite(const String &name, const Initial &fallback, const Initial &value); + #endif diff --git a/src/http.cpp b/src/http.cpp index e18344f..5f53a88 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -33,12 +33,15 @@ void httpRelay(const int index, Output &relay) { const auto initialKey = String("initial") + index; if (server.hasArg(initialKey)) { const auto initial = server.arg(initialKey); - if (initial == "true") { + if (initial == "OFF") { Serial.printf("[HTTP] %s = %s\n", initialKey.c_str(), initial.c_str()); - relay.setInitial(true); - } else if (initial == "false") { + relay.setInitial(INITIAL_OFF); + } else if (initial == "ON") { Serial.printf("[HTTP] %s = %s\n", initialKey.c_str(), initial.c_str()); - relay.setInitial(false); + relay.setInitial(INITIAL_ON); + } else if (initial == "BLINK") { + Serial.printf("[HTTP] %s = %s\n", initialKey.c_str(), initial.c_str()); + relay.setInitial(INITIAL_BLINK); } } @@ -67,7 +70,7 @@ void httpRelay(const int index, Output &relay) { void httpRelayJson(const Output &relay, const JsonObject json) { json["state"] = relay.get(); json["stateMillis"] = relay.getStateMillis(); - json["initial"] = relay.getInitial(); + json["initial"] = initialToString(relay.getInitial()); json["onCount"] = relay.getOnCount(); json["onMillis"] = relay.getOnMillis(); json["offMillis"] = relay.getOffMillis();