encapsulate commands into struct

This commit is contained in:
Nicu Hodos 2021-12-06 16:00:52 +01:00
parent 167606a19e
commit cafe4ec84f
2 changed files with 23 additions and 11 deletions

View File

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

View File

@ -11,24 +11,36 @@ namespace Mqtt {
AsyncMqttClient client;
std::queue<uint8_t> commands;
struct {
const char* topic = "esp_clock/sensor/ir/value";
std::queue<uint8_t> 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();