improve commands queueing

This commit is contained in:
Nicu Hodos 2022-09-23 20:54:08 +02:00
parent dc7fa37793
commit cbdc5612fc
2 changed files with 18 additions and 20 deletions

View File

@ -16,25 +16,21 @@ namespace Ir {
Task tLoop(TASK_IMMEDIATE, TASK_FOREVER, loop, &ts, true);
IRrecv irrecv(IR_INPUT_PIN);
decode_results results;
bool avrOn = false;
uint8_t lastCommand = 0x9F;
bool readCommand() {
bool newCommand = false;
uint8_t readCommand() {
uint8_t lastCommand = 0;
decode_results results;
if (irrecv.decode(&results)) {
if (results.decode_type == NEC && results.command != 0) {
Serial.print(F(" C=0x"));
Serial.print(results.command, HEX);
Serial.println();
lastCommand = results.command;
Mqtt::commands.push(results.command);
newCommand = true;
}
irrecv.resume(); // Receive the next value
}
return newCommand;
return lastCommand;
}
void command(const char c[]) {
@ -45,42 +41,47 @@ namespace Ir {
}
void loop() {
if (readCommand()) {
if (uint8_t lastCommand = readCommand()) {
switch (lastCommand)
{
case 0x9F:
avrOn = false;
tCheckWifi.restartDelayed();
Display::displayText("Off");
Mqtt::commands.push(lastCommand);
break;
case 0x12:
Display::displayText("Slp");
Mqtt::commands.push(lastCommand);
break;
case 0xC1:
Display::displayText("Mute");
Mqtt::commands.push(lastCommand);
break;
case 0xC4:
command("Play");
Mqtt::commands.push(lastCommand);
break;
case 0xC7:
avrOn ? Display::displayText(" Up") : Display::changeBrightness(true);
Mqtt::commands.pop();
break;
case 0xC8:
avrOn ? Display::displayText(" Dn") : Display::changeBrightness(false);
Mqtt::commands.pop();
break;
case 0xD0:
command("Stop");
Mqtt::commands.push(lastCommand);
break;
case 0xC0:
command("On");
Mqtt::commands.push(lastCommand);
break;
case 0x84:
Display::displayTemp(Bmp::data.readTemp());
break;
default:
Display::displayValue(lastCommand);
Mqtt::commands.push(lastCommand);
break;
}
}

View File

@ -20,7 +20,7 @@ namespace Mqtt {
return queue.empty() ? 0 : queue.front();
}
void push(uint8_t el) {
queue.push(el);
if (client.connected()) queue.push(el);
}
void pop() {
queue.pop();
@ -30,9 +30,8 @@ namespace Mqtt {
const char* bmpTopic = "esp_clock/sensor/bmp280/data";
void publishCommand() {
if (!commands.queue.empty() && client.connected()) {
if (uint8_t cmd = commands.getCurrent()) {
char message[32];
uint8_t cmd = commands.queue.front();
sprintf(message, "%X", cmd);
if (client.publish(commands.topic, 0, true, message) != 0) {
Serial.print(cmd, HEX);
@ -43,12 +42,10 @@ namespace Mqtt {
}
void publishBmp280() {
if (client.connected()) {
Bmp::data.readAll();
char message[255];
sprintf(message, "{\"temperature\":%.2f, \"pressure\":%.2f, \"altitude\": %.2f}", Bmp::data.temp, Bmp::data.pressure, Bmp::data.altitude);
client.publish(bmpTopic, 0, true, message);
}
Bmp::data.readAll();
char message[255];
sprintf(message, "{\"temperature\":%.2f, \"pressure\":%.2f, \"altitude\": %.2f}", Bmp::data.temp, Bmp::data.pressure, Bmp::data.altitude);
client.publish(bmpTopic, 0, true, message);
}
void setup() {