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; break;
} }
} }
if (!avrOn && Mqtt::getCurrentCommand() == 0xC7) { if (!avrOn && Mqtt::commands.getCurrent() == 0xC7) {
Display::changeBrightness(true); Display::changeBrightness(true);
Mqtt::commands.pop(); Mqtt::commands.pop();
} }
if (!avrOn && Mqtt::getCurrentCommand() == 0xC8) { if (!avrOn && Mqtt::commands.getCurrent() == 0xC8) {
Display::changeBrightness(false); Display::changeBrightness(false);
Mqtt::commands.pop(); Mqtt::commands.pop();
} }

View File

@ -11,24 +11,36 @@ namespace Mqtt {
AsyncMqttClient client; 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() { void publishCommand() {
if (!commands.empty() && client.connected()) { if (!commands.list.empty() && client.connected()) {
char message[32]; char message[32];
sprintf(message, "%X", commands.front()); uint8_t cmd = commands.front();
if (client.publish("esp_clock/sensor/ir/value", 0, true, message) != 0) { sprintf(message, "%X", cmd);
Serial.print(commands.front(), HEX); if (client.publish(commands.topic, 0, true, message) != 0) {
Serial.print(cmd, HEX);
Serial.println(); Serial.println();
commands.pop(); commands.pop();
} }
} }
} }
uint8_t getCurrentCommand() {
return commands.empty() ? 0 : commands.front();
}
void setup() { void setup() {
client.onConnect([](bool sessionPresent) { client.onConnect([](bool sessionPresent) {
tPublish.enableDelayed(); tPublish.enableDelayed();