change brightness over MQTT

This commit is contained in:
Nicu Hodos 2023-07-09 19:48:00 +02:00
parent 9767db6cce
commit c2cc10c2cd
3 changed files with 49 additions and 22 deletions

View File

@ -7,9 +7,11 @@
#include "ntp_time.h" #include "ntp_time.h"
#define DISPLAY_ADDRESS 0x70 #define DISPLAY_ADDRESS 0x70
#define BRIGHTNESS_NIGHT 0 #define BRIGHTNESS_MIN 0
#define BRIGHTNESS_DAY 11 #define BRIGHTNESS_MAX 15
#define BRIGHTNESS_STEP 1 #define BRIGHTNESS_STEP 1
#define BRIGHTNESS_NIGHT BRIGHTNESS_MIN
#define BRIGHTNESS_DAY 11
#define DISPLAY_TIME 2000 #define DISPLAY_TIME 2000
#define DISPLAY_TEMP_TIME 5000 #define DISPLAY_TEMP_TIME 5000
@ -45,18 +47,21 @@ namespace Display {
} }
} }
void changeBrightness(bool increase) { void setBrightness(unsigned int value) {
increase ? brightness = (brightness + BRIGHTNESS_STEP) % 15 : brightness = (brightness - BRIGHTNESS_STEP) % 15; brightness = value % (BRIGHTNESS_MAX+1);
clockDisplay.setBrightness(brightness); clockDisplay.setBrightness(brightness);
} }
void changeBrightness(bool increase) {
increase ? setBrightness(brightness + BRIGHTNESS_STEP) : setBrightness(brightness - BRIGHTNESS_STEP);
}
void adjustBrightness() { void adjustBrightness() {
if (currentHour > 8 && currentHour < 17) { if (currentHour > 8 && currentHour < 17) {
brightness = BRIGHTNESS_DAY; setBrightness(BRIGHTNESS_DAY);
} else { } else {
brightness = BRIGHTNESS_NIGHT; setBrightness(BRIGHTNESS_NIGHT);
} }
clockDisplay.setBrightness(brightness);
} }
void drawColon(bool colonOn) { void drawColon(bool colonOn) {

View File

@ -1,4 +1,5 @@
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include "display.h"
#define JSON_SIZE 512 #define JSON_SIZE 512
#define TOPIC_SIZE 255 #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 { struct Sensor : Component {
const char* deviceClass; const char* deviceClass;
const char* unitMeasure; const char* unitMeasure;

View File

@ -45,24 +45,24 @@ namespace Mqtt {
client.disconnect(); client.disconnect();
} }
Ha::Sensor sensors[] = { Ha::Sensor* sensors[] = {
Ha::TemperatureSensor{"Living room Temperature", "temperature"}, new Ha::TemperatureSensor{"Living room Temperature", "temperature"},
Ha::PressureSensor{"Living room Pressure", "pressure"}, new Ha::PressureSensor{"Living room Pressure", "pressure"},
Ha::AltitudeSensor{"Living room Altitude", "altitude"} new Ha::AltitudeSensor{"Living room Altitude", "altitude"}
}; };
Ha::Command switches[] = { Ha::Command* switches[] = {
Ha::Button{"ESP Clock Restart", "restart", new Ha::Button{"ESP Clock Restart", "restart",
[](const char* msg) { [](const char* msg) {
if (String { "PRESS" }.equals(msg)) ESP.restart(); if (String { "PRESS" }.equals(msg)) ESP.restart();
} }
}, },
Ha::Switch{"ESP Clock Led", "led", new Ha::Switch{"ESP Clock Led", "led",
[](const char* msg) { [](const char* msg) {
String{ "ON" }.equals(msg) ? digitalWrite(LED_BUILTIN, LOW) : digitalWrite(LED_BUILTIN, HIGH); 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) { [](const char* msg) {
if (String{ "ON" }.equals(msg)) { if (String{ "ON" }.equals(msg)) {
Display::hourFormat24 = true; Display::hourFormat24 = true;
@ -72,6 +72,11 @@ namespace Mqtt {
Display::drawTime(); 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() { void publishConfig() {
for (Ha::Component& cmp : sensors) { for (Ha::Component* cmp : sensors) {
publishComponentConfig(cmp); publishComponentConfig(*cmp);
} }
for (Ha::Component& cmp : switches) { for (Ha::Component* cmp : switches) {
publishComponentConfig(cmp); publishComponentConfig(*cmp);
} }
ts.deleteTask(tPublishConfig); ts.deleteTask(tPublishConfig);
} }
@ -122,9 +127,9 @@ namespace Mqtt {
char msg[len + 1]; char msg[len + 1];
memcpy(msg, payload, len); memcpy(msg, payload, len);
msg[len] = 0; msg[len] = 0;
for (Ha::Command cmd : switches) { for (Ha::Command* cmd : switches) {
if (String{ cmd.commandTopic }.equals(topic)) { if (String{ cmd->commandTopic }.equals(topic)) {
cmd.f(msg); cmd->f(msg);
return; return;
} }
} }