diff --git a/include/display.h b/include/display.h index 60a53b0..c61e68a 100644 --- a/include/display.h +++ b/include/display.h @@ -7,9 +7,11 @@ #include "ntp_time.h" #define DISPLAY_ADDRESS 0x70 -#define BRIGHTNESS_NIGHT 0 -#define BRIGHTNESS_DAY 11 +#define BRIGHTNESS_MIN 0 +#define BRIGHTNESS_MAX 15 #define BRIGHTNESS_STEP 1 +#define BRIGHTNESS_NIGHT BRIGHTNESS_MIN +#define BRIGHTNESS_DAY 11 #define DISPLAY_TIME 2000 #define DISPLAY_TEMP_TIME 5000 @@ -45,18 +47,21 @@ namespace Display { } } - void changeBrightness(bool increase) { - increase ? brightness = (brightness + BRIGHTNESS_STEP) % 15 : brightness = (brightness - BRIGHTNESS_STEP) % 15; + void setBrightness(unsigned int value) { + brightness = value % (BRIGHTNESS_MAX+1); clockDisplay.setBrightness(brightness); } + void changeBrightness(bool increase) { + increase ? setBrightness(brightness + BRIGHTNESS_STEP) : setBrightness(brightness - BRIGHTNESS_STEP); + } + void adjustBrightness() { if (currentHour > 8 && currentHour < 17) { - brightness = BRIGHTNESS_DAY; + setBrightness(BRIGHTNESS_DAY); } else { - brightness = BRIGHTNESS_NIGHT; + setBrightness(BRIGHTNESS_NIGHT); } - clockDisplay.setBrightness(brightness); } void drawColon(bool colonOn) { diff --git a/include/ha.h b/include/ha.h index 3b72509..0adaf5e 100644 --- a/include/ha.h +++ b/include/ha.h @@ -1,4 +1,5 @@ #include +#include "display.h" #define JSON_SIZE 512 #define TOPIC_SIZE 255 @@ -75,6 +76,22 @@ namespace Ha { }; + struct Brightness : Command { + static constexpr const char* type = "number"; + + Brightness(const char* name, const char* id, onMessage f) : Command(name, id, type, f) { + sprintf(commandTopic, "homeassistant/%s/esp_clock/%s", type, id); + } + + void buildConfig(JsonDocument& jsonDoc) override { + Command::buildConfig(jsonDoc); + jsonDoc["min"] = BRIGHTNESS_MIN; + jsonDoc["max"] = BRIGHTNESS_MAX; + jsonDoc["step"] = BRIGHTNESS_STEP; + } + + }; + struct Sensor : Component { const char* deviceClass; const char* unitMeasure; diff --git a/include/mqtt.h b/include/mqtt.h index b8f8e4b..09c36dc 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -45,24 +45,24 @@ namespace Mqtt { client.disconnect(); } - Ha::Sensor sensors[] = { - Ha::TemperatureSensor{"Living room Temperature", "temperature"}, - Ha::PressureSensor{"Living room Pressure", "pressure"}, - Ha::AltitudeSensor{"Living room Altitude", "altitude"} + Ha::Sensor* sensors[] = { + new Ha::TemperatureSensor{"Living room Temperature", "temperature"}, + new Ha::PressureSensor{"Living room Pressure", "pressure"}, + new Ha::AltitudeSensor{"Living room Altitude", "altitude"} }; - Ha::Command switches[] = { - Ha::Button{"ESP Clock Restart", "restart", + Ha::Command* switches[] = { + new Ha::Button{"ESP Clock Restart", "restart", [](const char* msg) { if (String { "PRESS" }.equals(msg)) ESP.restart(); } }, - Ha::Switch{"ESP Clock Led", "led", + new Ha::Switch{"ESP Clock Led", "led", [](const char* msg) { String{ "ON" }.equals(msg) ? digitalWrite(LED_BUILTIN, LOW) : digitalWrite(LED_BUILTIN, HIGH); } }, - Ha::Switch{"ESP Clock Format 24h", "format_24h", + new Ha::Switch{"ESP Clock Format 24h", "format_24h", [](const char* msg) { if (String{ "ON" }.equals(msg)) { Display::hourFormat24 = true; @@ -72,6 +72,11 @@ namespace Mqtt { Display::drawTime(); } } + }, + new Ha::Brightness{"ESP Clock Brightness", "brightness", + [](const char* msg) { + Display::setBrightness(String{ msg }.toInt()); + } } }; @@ -86,11 +91,11 @@ namespace Mqtt { } void publishConfig() { - for (Ha::Component& cmp : sensors) { - publishComponentConfig(cmp); + for (Ha::Component* cmp : sensors) { + publishComponentConfig(*cmp); } - for (Ha::Component& cmp : switches) { - publishComponentConfig(cmp); + for (Ha::Component* cmp : switches) { + publishComponentConfig(*cmp); } ts.deleteTask(tPublishConfig); } @@ -122,9 +127,9 @@ namespace Mqtt { char msg[len + 1]; memcpy(msg, payload, len); msg[len] = 0; - for (Ha::Command cmd : switches) { - if (String{ cmd.commandTopic }.equals(topic)) { - cmd.f(msg); + for (Ha::Command* cmd : switches) { + if (String{ cmd->commandTopic }.equals(topic)) { + cmd->f(msg); return; } }