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 <SPI.h>
#include "ntp_time.h"
#include "mqtt.h"
#define DISPLAY_ADDRESS 0x70
#define BRIGHTNESS_MIN 0
@ -16,13 +15,6 @@
#define DISPLAY_TIME 2000
#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 {
void display();
@ -35,6 +27,29 @@ namespace Display {
// Create display object
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() {
int displayHour = hourFormat24 ? hour() : hourFormat12();
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) {
if (colonOn) {
if (currentHour != hour()) {
currentHour = hour();
updateBrightness();
Brightness::update();
if (currentHour == 8) Wifi::reconnect();
}
if (currentMin != minute()) {
@ -125,7 +122,7 @@ namespace Display {
void setup() {
clockDisplay.begin(DISPLAY_ADDRESS);
clockDisplay.setBrightness(brightness);
clockDisplay.setBrightness(Brightness::current);
drawTime();
display();
}

View File

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