From cafe4ec84fe509cb58c275acf995ba7b5b5e5bb9 Mon Sep 17 00:00:00 2001 From: Nicolae Hodos Date: Mon, 6 Dec 2021 16:00:52 +0100 Subject: [PATCH] encapsulate commands into struct --- include/ir.h | 4 ++-- include/mqtt.h | 30 +++++++++++++++++++++--------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/include/ir.h b/include/ir.h index 7161f78..d1938bb 100644 --- a/include/ir.h +++ b/include/ir.h @@ -56,11 +56,11 @@ namespace Ir { break; } } - if (!avrOn && Mqtt::getCurrentCommand() == 0xC7) { + if (!avrOn && Mqtt::commands.getCurrent() == 0xC7) { Display::changeBrightness(true); Mqtt::commands.pop(); } - if (!avrOn && Mqtt::getCurrentCommand() == 0xC8) { + if (!avrOn && Mqtt::commands.getCurrent() == 0xC8) { Display::changeBrightness(false); Mqtt::commands.pop(); } diff --git a/include/mqtt.h b/include/mqtt.h index 06e9590..603e4ab 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -11,24 +11,36 @@ namespace Mqtt { AsyncMqttClient client; - std::queue commands; + struct { + const char* topic = "esp_clock/sensor/ir/value"; + std::queue list; + uint8_t getCurrent() { + return list.empty() ? 0 : list.front(); + } + void push(uint8_t el) { + list.push(el); + } + void pop() { + list.pop(); + } + uint8_t front() { + return list.front(); + } + } commands; void publishCommand() { - if (!commands.empty() && client.connected()) { + if (!commands.list.empty() && client.connected()) { char message[32]; - sprintf(message, "%X", commands.front()); - if (client.publish("esp_clock/sensor/ir/value", 0, true, message) != 0) { - Serial.print(commands.front(), HEX); + uint8_t cmd = commands.front(); + sprintf(message, "%X", cmd); + if (client.publish(commands.topic, 0, true, message) != 0) { + Serial.print(cmd, HEX); Serial.println(); commands.pop(); } } } - uint8_t getCurrentCommand() { - return commands.empty() ? 0 : commands.front(); - } - void setup() { client.onConnect([](bool sessionPresent) { tPublish.enableDelayed();