diff --git a/include/display.h b/include/display.h index 1369549..21218cd 100644 --- a/include/display.h +++ b/include/display.h @@ -12,16 +12,20 @@ #define BRIGHTNESS_STEP 1 #define BRIGHTNESS_NIGHT BRIGHTNESS_MIN #define BRIGHTNESS_DAY 11 -#define DISPLAY_TIME 2000 -#define DISPLAY_TEMP_TIME 5000 +#define SECONDS(value) value*1000 namespace Display { - void display(); - Task tDisplay(500, TASK_FOREVER, display, &ts, true); + void displayTime(); + void drawTime(); + void drawColon(bool); + Task tDisplayTime(500, TASK_FOREVER, displayTime, &ts, false, []() { + drawTime(); + return true; + }, []() { + drawColon(false); + }); - int currentHour = -1; - int currentMin = -1; bool hourFormat24 = false; void (*hourFormatChangedCallback)(); void onHourFormatChanged(void (*f)()) { @@ -71,6 +75,7 @@ namespace Display { void drawColon(bool colonOn) { if (colonOn) { + static int currentHour = -1; if (currentHour != hour()) { currentHour = hour(); if (currentHour == 8) { @@ -81,6 +86,7 @@ namespace Display { Brightness::set(BRIGHTNESS_NIGHT); } } + static int currentMin = -1; if (currentMin != minute()) { currentMin = minute(); drawTime(); @@ -89,8 +95,8 @@ namespace Display { clockDisplay.drawColon(colonOn); } - void display() { - static bool colonOn = false; + void displayTime() { + static bool colonOn = true; drawColon(colonOn); clockDisplay.writeDisplay(); @@ -99,30 +105,24 @@ namespace Display { } void displayTemp(float value) { - tDisplay.disable(); - drawColon(false); + tDisplayTime.disable(); clockDisplay.printFloat(value, 1); clockDisplay.writeDisplay(); - drawTime(); - tDisplay.enableDelayed(DISPLAY_TEMP_TIME); + tDisplayTime.enableDelayed(SECONDS(10)); } void displayValue(uint8_t value) { - tDisplay.disable(); - drawColon(false); + tDisplayTime.disable(); clockDisplay.print(value, HEX); clockDisplay.writeDisplay(); - drawTime(); - tDisplay.enableDelayed(DISPLAY_TIME); + tDisplayTime.enableDelayed(SECONDS(2)); } void displayText(const char c[]) { - tDisplay.disable(); - drawColon(false); + tDisplayTime.disable(); clockDisplay.println(c); clockDisplay.writeDisplay(); - drawTime(); - tDisplay.enableDelayed(DISPLAY_TIME); + tDisplayTime.enableDelayed(SECONDS(2)); } void changeHourFormat24(bool format24) { @@ -134,7 +134,5 @@ namespace Display { void setup() { clockDisplay.begin(DISPLAY_ADDRESS); clockDisplay.setBrightness(Brightness::current); - drawTime(); - display(); } } diff --git a/include/mqtt.h b/include/mqtt.h index 250cae9..b752d52 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -19,7 +19,6 @@ namespace Mqtt { void disconnect(); Task tReConnect(5 * TASK_MINUTE, TASK_FOREVER, connect, &ts); Task tPublishInit(TASK_IMMEDIATE, TASK_ONCE, publishInit, &ts); - Task tPublishBmp(5 * TASK_MINUTE, TASK_FOREVER, publishBmp280, &ts); Task tPublishCommand(TASK_SECOND, TASK_FOREVER, publishCommand, &ts); AsyncMqttClient client; @@ -120,7 +119,6 @@ namespace Mqtt { } void publishBmp280() { - Bmp::data.readAll(); StaticJsonDocument<255> jsonDoc; jsonDoc["temperature"] = Bmp::data.temp; jsonDoc["pressure"] = Bmp::data.pressure; @@ -159,7 +157,6 @@ namespace Mqtt { Display::onHourFormatChanged(hourFormatMqtt->publishState); client.onConnect([](bool sessionPresent) { tPublishInit.enable(); - tPublishBmp.enableIfNot(); tPublishCommand.enableDelayed(); client.subscribe(espClockTopic, 0); tReConnect.disable(); @@ -168,7 +165,6 @@ namespace Mqtt { client.onDisconnect([](AsyncMqttClientDisconnectReason reason) { tReConnect.enableDelayed(); tPublishCommand.disable(); - tPublishBmp.disable(); Serial.println("Disconnected from MQTT"); }); client.onMessage(onMessage); diff --git a/src/esp_clock.cpp b/src/esp_clock.cpp index fb31b65..f95dd8e 100644 --- a/src/esp_clock.cpp +++ b/src/esp_clock.cpp @@ -27,6 +27,17 @@ Task tButton(TASK_IMMEDIATE, TASK_ONCE, []() { Task tLed(TASK_IMMEDIATE, TASK_ONCE, []() { Mqtt::ledMqtt->publishState(); }, &ts); +Task tReadBmp(TASK_MINUTE, TASK_FOREVER, []() { + static float lastTemp = 0; + float temp = Bmp::data.temp; + Bmp::data.readAll(); + if (temp == Bmp::data.temp) return; + Mqtt::publishBmp280(); + if (abs(lastTemp - Bmp::data.temp) > 0.2) { + lastTemp = Bmp::data.temp; + Display::displayTemp(Bmp::data.temp); + } +}, &ts); void setup() { @@ -47,6 +58,8 @@ void setup() { pinMode(BUTTON, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(BUTTON), onButtonPressed, FALLING); attachInterrupt(digitalPinToInterrupt(LED_BUILTIN), onLed, CHANGE); + + tReadBmp.enable(); } void loop() {