Compare commits

..

No commits in common. "relay2" and "master" have entirely different histories.

7 changed files with 0 additions and 294 deletions

View File

@ -1,41 +0,0 @@
#ifndef PROPERTY_H
#define PROPERTY_H
#include <LittleFS.h>
template<class T>
class Property {
public:
const String path;
T fallback;
void load() {
String content = "";
if (auto file = LittleFS.open(path, "r")) {
content = file.readString();
file.close();
}
value = toValue(content);
}
protected:
T value;
explicit Property(const String &path, const T fallback)
: path(path),
fallback(fallback),
value(fallback) {
//
}
virtual ~Property() = default;
virtual T toValue(const String &content) =0;
};
#endif

View File

@ -1,34 +0,0 @@
#ifndef PROPERTY_BOOL_H
#define PROPERTY_BOOL_H
#include <Property.h>
class PropertyBool final : public Property<bool> {
public:
PropertyBool(const String &name, const bool fallback)
: Property(name, fallback) {
//
}
// ReSharper disable once CppNonExplicitConversionOperator
operator bool() const {
return value;
}
protected:
bool toValue(const String &content) override {
if (content == "true") {
return true;
}
if (content == "false") {
return false;
}
return fallback;
}
};
#endif

View File

@ -1,28 +0,0 @@
#ifndef PROPERTY_LONG_H
#define PROPERTY_LONG_H
#include <Property.h>
class PropertyLong final : public Property<long> {
public:
PropertyLong(const String &name, const long fallback)
: Property(name, fallback) {
//
}
// ReSharper disable once CppNonExplicitConversionOperator
operator long() const {
return value;
}
protected:
long toValue(const String &content) override {
return content.toInt();
}
};
#endif

View File

@ -1,169 +0,0 @@
#ifndef RELAY2_H
#define RELAY2_H
#include <Arduino.h>
#include "log.h"
#include "PropertyBool.h"
#include "PropertyLong.h"
class Relay2 {
bool state = false;
unsigned long stateSince = 0;
unsigned long manualSince = 0;
public:
const int index;
const int pin;
const bool inverted;
String prefix;
PropertyBool initialState;
PropertyBool maxOnEnabled;
PropertyLong maxOnSeconds;
PropertyBool maxOffEnabled;
PropertyLong maxOffSeconds;
PropertyBool buttonOnEnabled;
PropertyLong buttonOnSeconds;
PropertyBool buttonOffEnabled;
PropertyLong buttonOffSeconds;
PropertyBool powerOnEnabled;
PropertyLong powerOnThreshold;
PropertyLong powerOnSeconds;
PropertyBool powerOffEnabled;
PropertyLong powerOffThreshold;
PropertyLong powerOffSeconds;
Relay2(const int index, const int pin, const bool inverted)
: index(index),
pin(pin),
inverted(inverted),
prefix("/relay" + String(index)),
initialState(prefix + "initialState", false),
maxOnEnabled(prefix + "maxOnEnabled", false),
maxOnSeconds(prefix + "maxOnSeconds", 0),
maxOffEnabled(prefix + "maxOffEnabled", false),
maxOffSeconds(prefix + "maxOffSeconds", 0),
buttonOnEnabled(prefix + "buttonOnEnabled", false),
buttonOnSeconds(prefix + "buttonOnSeconds", 0),
buttonOffEnabled(prefix + "buttonOffEnabled", false),
buttonOffSeconds(prefix + "buttonOffSeconds", 0),
powerOnEnabled(prefix + "powerOnEnabled", false),
powerOnThreshold(prefix + "powerOnThreshold", 0),
powerOnSeconds(prefix + "powerOnSeconds", 0),
powerOffEnabled(prefix + "powerOffEnabled", false),
powerOffThreshold(prefix + "powerOffThreshold", 0),
powerOffSeconds(prefix + "powerOffSeconds", 0) {
//
}
void setup() {
pinMode(pin, OUTPUT);
load();
state = !initialState;
setState(initialState, false);
}
void loop() {
const auto stateAge = millis() - stateSince;
loopMax(stateAge);
if (manualSince > 0) {
loopManual(stateAge);
} else {
loopPower(stateAge);
}
}
bool getState() const {
return state;
}
void setManual(const bool newState) {
setState(newState, false);
}
protected:
void setState(const bool newState, const bool manual) {
if (manual) {
manualSince = max(1UL, millis());
} else {
manualSince = 0;
}
if (state != newState) {
info("[RELAY%d] %s", index, state ? "ON" : "OFF");
state = newState;
stateSince = millis();
digitalWrite(pin, state ^ inverted ? HIGH : LOW);
}
}
private:
void loopMax(const long stateAge) {
if (state && maxOnEnabled && maxOnSeconds && stateAge >= maxOnSeconds) {
setState(false, false);
}
if (!state && maxOffEnabled && maxOffSeconds && stateAge >= maxOffSeconds) {
setState(true, false);
}
}
void loopManual(const long stateAge) {
if (state && buttonOnEnabled && buttonOnSeconds && stateAge >= buttonOnSeconds) {
setState(false, false);
}
if (!state && buttonOffEnabled && buttonOffSeconds && stateAge >= buttonOffSeconds) {
setState(true, false);
}
}
void loopPower(const long stateAge) {
const auto valid = !isnan(gridPowerDeltaValue) && millis() - gridPowerDeltaMillis <= 10000;
// TODO
}
void load() {
initialState.load();
maxOnEnabled.load();
maxOnSeconds.load();
maxOffEnabled.load();
maxOffSeconds.load();
buttonOnEnabled.load();
buttonOnSeconds.load();
buttonOffEnabled.load();
buttonOffSeconds.load();
powerOnEnabled.load();
powerOnThreshold.load();
powerOnSeconds.load();
powerOffEnabled.load();
powerOffThreshold.load();
powerOffSeconds.load();
}
};
#endif

View File

@ -3,7 +3,6 @@
#include "Relay.h"
#include "Button.h"
#include "Relay2.h"
void buttonCallback(Output &output, ButtonEvent event);
@ -11,8 +10,6 @@ extern Output status;
extern Button button0;
extern Relay2 relay99;
extern Relay relay0;
#ifdef SP111

View File

@ -1,13 +0,0 @@
#include "log.h"
#include <HardwareSerial.h>
#include <cstdio>
void info(const char *format, ...) {
char message[512];
va_list args;
va_start(args, format);
vsnprintf(message, sizeof(message), format, args);
Serial.println(message);
va_end(args);
}

View File

@ -1,6 +0,0 @@
#ifndef LOG_H
#define LOG_H
void info(const char *format, ...);
#endif