brightness:

- use callback for when brightness is updated - avoid dependency on mqtt
- encapsulate brigthness in its own namespace - improve readability
This commit is contained in:
Nicu Hodos 2023-07-10 13:30:59 +02:00
parent b2952cb6a2
commit 58d60c02b8
2 changed files with 42 additions and 45 deletions

View File

@ -5,7 +5,6 @@
#include <Adafruit_I2CDevice.h> #include <Adafruit_I2CDevice.h>
#include <SPI.h> #include <SPI.h>
#include "ntp_time.h" #include "ntp_time.h"
#include "mqtt.h"
#define DISPLAY_ADDRESS 0x70 #define DISPLAY_ADDRESS 0x70
#define BRIGHTNESS_MIN 0 #define BRIGHTNESS_MIN 0
@ -16,13 +15,6 @@
#define DISPLAY_TIME 2000 #define DISPLAY_TIME 2000
#define DISPLAY_TEMP_TIME 5000 #define DISPLAY_TEMP_TIME 5000
namespace Mqtt {
void publishBrightness();
bool connected();
}
Task tPublishBrightness(TASK_IMMEDIATE, TASK_ONCE, Mqtt::publishBrightness, &ts, false, Mqtt::connected);
namespace Display { namespace Display {
void display(); void display();
@ -35,6 +27,29 @@ namespace Display {
// Create display object // Create display object
Adafruit_7segment clockDisplay = Adafruit_7segment(); Adafruit_7segment clockDisplay = Adafruit_7segment();
namespace Brightness {
uint8_t current = BRIGHTNESS_NIGHT;
void (*brightnessChangedCallback)();
void set(uint8_t value) {
current = value % (BRIGHTNESS_MAX + 1);
clockDisplay.setBrightness(current);
brightnessChangedCallback();
}
void onChanged(void (*f)()) {
brightnessChangedCallback = f;
}
void change(bool increase) {
increase ? set(current + BRIGHTNESS_STEP) : set(current - BRIGHTNESS_STEP);
}
void update() {
currentHour > 8 && currentHour < 17 ? set(BRIGHTNESS_DAY) : set(BRIGHTNESS_NIGHT);
}
}
void drawTime() { void drawTime() {
int displayHour = hourFormat24 ? hour() : hourFormat12(); int displayHour = hourFormat24 ? hour() : hourFormat12();
int displayMinute = minute(); int displayMinute = minute();
@ -54,29 +69,11 @@ namespace Display {
} }
} }
void setBrightness(uint8_t value) {
brightness = value % (BRIGHTNESS_MAX+1);
clockDisplay.setBrightness(brightness);
tPublishBrightness.restart();
}
void changeBrightness(bool increase) {
increase ? setBrightness(brightness + BRIGHTNESS_STEP) : setBrightness(brightness - BRIGHTNESS_STEP);
}
void updateBrightness() {
if (currentHour > 8 && currentHour < 17) {
setBrightness(BRIGHTNESS_DAY);
} else {
setBrightness(BRIGHTNESS_NIGHT);
}
}
void drawColon(bool colonOn) { void drawColon(bool colonOn) {
if (colonOn) { if (colonOn) {
if (currentHour != hour()) { if (currentHour != hour()) {
currentHour = hour(); currentHour = hour();
updateBrightness(); Brightness::update();
if (currentHour == 8) Wifi::reconnect(); if (currentHour == 8) Wifi::reconnect();
} }
if (currentMin != minute()) { if (currentMin != minute()) {
@ -125,7 +122,7 @@ namespace Display {
void setup() { void setup() {
clockDisplay.begin(DISPLAY_ADDRESS); clockDisplay.begin(DISPLAY_ADDRESS);
clockDisplay.setBrightness(brightness); clockDisplay.setBrightness(Brightness::current);
drawTime(); drawTime();
display(); display();
} }

View File

@ -40,10 +40,6 @@ namespace Mqtt {
const char* espClockTopic = "homeassistant/+/esp_clock/#"; const char* espClockTopic = "homeassistant/+/esp_clock/#";
bool connected() {
return client.connected();
}
void connect() { void connect() {
client.connect(); client.connect();
} }
@ -61,7 +57,7 @@ namespace Mqtt {
Ha::Command* brightnessMqtt = (new Ha::Brightness{ "ESP Clock Brightness", "brightness", Ha::Command* brightnessMqtt = (new Ha::Brightness{ "ESP Clock Brightness", "brightness",
[](const char* msg) { [](const char* msg) {
Display::setBrightness(String{ msg }.toInt()); Display::Brightness::set(String{ msg }.toInt());
} }
})->withStateTopic(); })->withStateTopic();
@ -90,6 +86,10 @@ namespace Mqtt {
brightnessMqtt brightnessMqtt
}; };
uint16_t publish(const char* topic, const char* message) {
return client.publish(topic, 0, true, message);
}
void publishComponentConfig(Ha::Component& component) { void publishComponentConfig(Ha::Component& component) {
StaticJsonDocument<JSON_SIZE> jsonDoc; StaticJsonDocument<JSON_SIZE> jsonDoc;
component.buildConfig(jsonDoc); component.buildConfig(jsonDoc);
@ -97,7 +97,14 @@ namespace Mqtt {
char message[JSON_SIZE]; char message[JSON_SIZE];
serializeJson(jsonDoc, message); serializeJson(jsonDoc, message);
client.publish(component.configTopic, 0, true, message); publish(component.configTopic, message);
}
void publishBrightness() {
if (!client.connected()) return ;
char message[32];
sprintf(message, "%u", Display::Brightness::current);
publish(brightnessMqtt->stateTopic, message);
} }
void publishConfig() { void publishConfig() {
@ -107,7 +114,7 @@ namespace Mqtt {
for (Ha::Component* cmp : switches) { for (Ha::Component* cmp : switches) {
publishComponentConfig(*cmp); publishComponentConfig(*cmp);
} }
// publishBrightness(Display::brightness); publishBrightness();
ts.deleteTask(tPublishConfig); ts.deleteTask(tPublishConfig);
} }
@ -119,20 +126,14 @@ namespace Mqtt {
jsonDoc["altitude"] = Bmp::data.altitude; jsonDoc["altitude"] = Bmp::data.altitude;
char message[255]; char message[255];
serializeJson(jsonDoc, message); serializeJson(jsonDoc, message);
client.publish(Ha::Sensor::stateTopic, 0, true, message); publish(Ha::Sensor::stateTopic, message);
}
void publishBrightness() {
char message[32];
sprintf(message, "%X", Display::brightness);
client.publish(brightnessMqtt->stateTopic, 0, true, message);
} }
void publishCommand() { void publishCommand() {
if (uint8_t cmd = commands.getCurrent()) { if (uint8_t cmd = commands.getCurrent()) {
char message[32]; char message[32];
sprintf(message, "%X", cmd); sprintf(message, "%X", cmd);
if (client.publish(commands.topic, 0, true, message) != 0) { if (publish(commands.topic, message) != 0) {
Serial.print(cmd, HEX); Serial.print(cmd, HEX);
Serial.println(); Serial.println();
commands.queue.pop(); commands.queue.pop();
@ -153,11 +154,11 @@ namespace Mqtt {
} }
void setup() { void setup() {
Display::Brightness::onChanged(publishBrightness);
client.onConnect([](bool sessionPresent) { client.onConnect([](bool sessionPresent) {
tPublishConfig.enable(); tPublishConfig.enable();
tPublishBmp.enableIfNot(); tPublishBmp.enableIfNot();
tPublishCommand.enableDelayed(); tPublishCommand.enableDelayed();
tPublishBrightness.enable();
client.subscribe(espClockTopic, 0); client.subscribe(espClockTopic, 0);
tReConnect.disable(); tReConnect.disable();
Serial.println("Connected to MQTT"); Serial.println("Connected to MQTT");
@ -166,7 +167,6 @@ namespace Mqtt {
tReConnect.enableDelayed(); tReConnect.enableDelayed();
tPublishCommand.disable(); tPublishCommand.disable();
tPublishBmp.disable(); tPublishBmp.disable();
tPublishBrightness.disable();
Serial.println("Disconnected from MQTT"); Serial.println("Disconnected from MQTT");
}); });
client.onMessage(onMessage); client.onMessage(onMessage);