Initial (OFF, ON, BLINK)

This commit is contained in:
Patrick Haßel 2025-08-28 12:15:05 +02:00
parent 7154e16b42
commit c12ae7a50f
6 changed files with 72 additions and 13 deletions

31
src/Initial.h Normal file
View File

@ -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

View File

@ -3,6 +3,8 @@
#include <Arduino.h> #include <Arduino.h>
#include "Initial.h"
class Output { class Output {
protected: protected:
@ -15,7 +17,7 @@ protected:
const bool logState; const bool logState;
bool initial = false; Initial initial = INITIAL_OFF;
long onCount = -1; long onCount = -1;
@ -35,6 +37,15 @@ protected:
digitalWrite(pin, state ^ inverted ? HIGH : LOW); digitalWrite(pin, state ^ inverted ? HIGH : LOW);
} }
void _applyInitial() {
_write(initial != INITIAL_OFF);
if (initial != INITIAL_BLINK) {
onCount = 0;
} else {
onCount = -1;
}
}
public: public:
Output(const String &name, const uint8_t pin, const bool inverted, const bool logState) : name(name), pin(pin), inverted(inverted), logState(logState) { 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() { virtual void setup() {
pinMode(pin, OUTPUT); pinMode(pin, OUTPUT);
_write(initial); _applyInitial();
} }
bool get() const { bool get() const {
@ -87,7 +98,7 @@ public:
name = value; name = value;
} }
virtual void setInitial(const bool value) { virtual void setInitial(const Initial value) {
initial = value; initial = value;
} }
@ -103,7 +114,7 @@ public:
offMillis = value; offMillis = value;
} }
bool getInitial() const { Initial getInitial() const {
return initial; return initial;
} }

View File

@ -19,10 +19,10 @@ public:
void setup() override { void setup() override {
Output::setup(); Output::setup();
Output::setName(configRead(String("relayName") + index, nameFallback)); 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::setOnMillis(configRead(String("relayOnMillis") + index, 0L));
Output::setOffMillis(configRead(String("relayOffMillis") + index, 0L)); Output::setOffMillis(configRead(String("relayOffMillis") + index, 0L));
_write(initial); _applyInitial();
} }
void setName(const String &value) override { void setName(const String &value) override {
@ -30,9 +30,9 @@ public:
configWrite(String("relayName") + index, nameFallback, value); configWrite(String("relayName") + index, nameFallback, value);
} }
void setInitial(const bool value) override { void setInitial(const Initial value) override {
Output::setInitial(value); Output::setInitial(value);
configWrite(String("relayInitial") + index, false, value); configWrite(String("relayInitial") + index, INITIAL_OFF, value);
} }
void setOnMillis(const unsigned long value) override { void setOnMillis(const unsigned long value) override {

View File

@ -73,3 +73,11 @@ bool configWrite(const String &name, const String &fallback, const String &value
} }
return false; 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));
}

View File

@ -3,6 +3,8 @@
#include <WString.h> #include <WString.h>
#include "Initial.h"
void configSetup(); void configSetup();
long configRead(const String &name, long fallback); 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); 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 #endif

View File

@ -33,12 +33,15 @@ void httpRelay(const int index, Output &relay) {
const auto initialKey = String("initial") + index; const auto initialKey = String("initial") + index;
if (server.hasArg(initialKey)) { if (server.hasArg(initialKey)) {
const auto initial = server.arg(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()); Serial.printf("[HTTP] %s = %s\n", initialKey.c_str(), initial.c_str());
relay.setInitial(true); relay.setInitial(INITIAL_OFF);
} else if (initial == "false") { } else if (initial == "ON") {
Serial.printf("[HTTP] %s = %s\n", initialKey.c_str(), initial.c_str()); 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) { void httpRelayJson(const Output &relay, const JsonObject json) {
json["state"] = relay.get(); json["state"] = relay.get();
json["stateMillis"] = relay.getStateMillis(); json["stateMillis"] = relay.getStateMillis();
json["initial"] = relay.getInitial(); json["initial"] = initialToString(relay.getInitial());
json["onCount"] = relay.getOnCount(); json["onCount"] = relay.getOnCount();
json["onMillis"] = relay.getOnMillis(); json["onMillis"] = relay.getOnMillis();
json["offMillis"] = relay.getOffMillis(); json["offMillis"] = relay.getOffMillis();