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"
#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) {

View File

@ -1,4 +1,5 @@
#include <ArduinoJson.h>
#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;

View File

@ -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;
}
}