Update the code for the status led

This commit is contained in:
Yurii
2023-11-16 03:29:06 +03:00
parent b5760eb314
commit 6204b46c17
3 changed files with 64 additions and 54 deletions

View File

@@ -18,6 +18,7 @@ lib_deps =
jandrassy/TelnetStream@^1.2.4
gyverlibs/EEManager@^2.0
gyverlibs/GyverPID@^3.3
gyverlibs/GyverBlinker@^1.0
milesburton/DallasTemperature@^3.11.0
https://github.com/Laxilef/WiFiManager/archive/refs/heads/patch-1.zip
;https://github.com/tzapu/WiFiManager.git#v2.0.16-rc.2
@@ -152,5 +153,5 @@ build_flags =
-D OT_OUT_PIN_DEFAULT=22
-D SENSOR_OUTDOOR_PIN_DEFAULT=12
-D SENSOR_INDOOR_PIN_DEFAULT=13
-D LED_STATUS_PIN=18
-D LED_STATUS_PIN=2 ; 18
-D LED_OT_RX_PIN=19

View File

@@ -1,3 +1,5 @@
#include <Blinker.h>
extern MqttTask* tMqtt;
extern SensorsTask* tSensors;
extern OpenThermTask* tOt;
@@ -7,6 +9,7 @@ public:
MainTask(bool _enabled = false, unsigned long _interval = 0) : Task(_enabled, _interval) {}
protected:
Blinker* blinker = nullptr;
unsigned long lastHeapInfo = 0;
unsigned long firstFailConnect = 0;
unsigned int heapSize = 0;
@@ -21,15 +24,18 @@ protected:
}
void setup() {
#ifdef LED_STATUS_PIN
pinMode(LED_STATUS_PIN, OUTPUT);
digitalWrite(LED_STATUS_PIN, false);
#endif
#if defined(ESP32)
#if defined(ESP32)
heapSize = ESP.getHeapSize();
#elif defined(ESP8266)
#elif defined(ESP8266)
heapSize = 81920;
#elif
#elif
heapSize = 99999;
#endif
#endif
minFreeHeapSize = heapSize;
}
@@ -81,7 +87,9 @@ protected:
tOt->enable();
}
ledStatus();
#ifdef LED_STATUS_PIN
ledStatus(LED_STATUS_PIN);
#endif
#ifdef USE_TELNET
yield();
@@ -109,56 +117,65 @@ protected:
}
}
void ledStatus() {
static byte blinkLeft = 0;
void ledStatus(uint8_t ledPin) {
uint8_t errors[4];
uint8_t errCount = 0;
static uint8_t errPos = 0;
static unsigned long endBlinkTime = 0;
static bool ledOn = false;
static unsigned long changeTime = 0;
byte errNo = 0;
if (!vars.states.otStatus) {
errNo = 1;
} else if (vars.states.fault) {
errNo = 2;
} else if (vars.states.emergency) {
errNo = 3;
if (this->blinker == nullptr) {
this->blinker = new Blinker(ledPin);
}
if (errNo == 0) {
if (!ledOn) {
digitalWrite(LED_STATUS_PIN, true);
ledOn = true;
}
if (WiFi.status() != WL_CONNECTED) {
errors[errCount++] = 2;
}
if (blinkLeft > 0) {
blinkLeft = 0;
}
if (0 && !vars.states.otStatus) {
errors[errCount++] = 3;
}
} else {
if (blinkLeft == 0) {
if (ledOn) {
digitalWrite(LED_STATUS_PIN, false);
ledOn = false;
changeTime = millis();
}
if (vars.states.fault) {
errors[errCount++] = 4;
}
if (millis() - changeTime >= 3000) {
blinkLeft = errNo;
}
}
if (vars.states.emergency) {
errors[errCount++] = 5;
}
if (blinkLeft > 0 && millis() - changeTime >= 500) {
if (ledOn) {
digitalWrite(LED_STATUS_PIN, false);
ledOn = false;
blinkLeft--;
} else {
digitalWrite(LED_STATUS_PIN, true);
if (this->blinker->ready()) {
endBlinkTime = millis();
}
if (!this->blinker->running() && millis() - endBlinkTime >= 5000) {
if (errCount == 0) {
if (!ledOn) {
digitalWrite(ledPin, true);
ledOn = true;
}
changeTime = millis();
return;
} else if (ledOn) {
digitalWrite(ledPin, false);
ledOn = false;
endBlinkTime = millis();
return;
}
if (errPos >= errCount) {
errPos = 0;
// end of error list
this->blinker->blink(10, 50, 50);
} else {
this->blinker->blink(errors[errPos++], 300, 300);
}
}
this->blinker->tick();
}
};

View File

@@ -33,14 +33,6 @@
#define SENSOR_INDOOR_PIN_DEFAULT 0
#endif
#ifndef LED_STATUS_PIN
#define LED_STATUS_PIN 0
#endif
#ifndef LED_OT_RX_PIN
#define LED_OT_RX_PIN 0
#endif
#ifdef USE_TELNET
#define INFO_STREAM TelnetStream
#define WARN_STREAM TelnetStream