Skip to content

Commit

Permalink
FIX for Brightness OFF after sending ON on first run
Browse files Browse the repository at this point in the history
  • Loading branch information
dbambus committed Oct 23, 2024
1 parent 23f620c commit b09e4e9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
8 changes: 8 additions & 0 deletions include/led.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,16 @@ void Led::shiftColumnToRight() {
//------------------------------------------------------------------------------

void Led::setState(bool newState) {
static bool firstRun = true;
static uint8_t oldBrightness[3];

if (firstRun) {
for (uint8_t i = 0; i < 3; i++) {
oldBrightness[i] = G.color[i].B * 100;
}
firstRun = false;
}

if (newState) {
for (uint8_t i = 0; i < 3; i++) {
G.color[i].B = oldBrightness[i] / 100.f;
Expand Down
10 changes: 6 additions & 4 deletions include/mqtt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,15 @@ None
void Mqtt::callback(char *topic, byte *payload, unsigned int length) {
StaticJsonDocument<512> doc;

Serial.print("Received message [");
Serial.print(topic);
Serial.print("] ");

char msg[length + 1];
// Convert payload to a null-terminated string
memcpy(msg, payload, length);
msg[length] = '\0';

Serial.print("Received message [");
Serial.print(msg);
Serial.print("] ");

// Deserialize JSON
DeserializationError error = deserializeJson(doc, msg);

Expand All @@ -204,8 +204,10 @@ void Mqtt::callback(char *topic, byte *payload, unsigned int length) {
if (doc.containsKey("state")) {
const char *state = doc["state"];
if (!strcmp(state, "ON")) {
Serial.println("ON");
led.setState(true);
} else if (!strcmp(state, "OFF")) {
Serial.println("OFF");
led.setState(false);
}
}
Expand Down

0 comments on commit b09e4e9

Please sign in to comment.