84 lines
2.0 KiB
C++

#if IR
#include <AsyncMqttClient.h>
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include "display.h"
#define IR_INPUT_PIN D6
#define MQTT_HOST IPAddress(192, 168, 5, 138)
#define MQTT_PORT 1883
namespace Ir {
IRrecv irrecv(IR_INPUT_PIN);
decode_results results;
bool avrOn = false;
uint8_t lastCommand = 0x9F;
bool readCommand() {
bool newCommand = false;
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;
}
void displayValue() {
Display::displayValue(lastCommand);
Display::tDisplay.setCallback(Display::displayColon);
Display::adjustTime();
}
void setup() {
irrecv.enableIRIn(); // Start the receiver
}
void loop() {
if (readCommand()) {
Display::tDisplay.setCallback(displayValue);
Display::tDisplay.forceNextIteration();
switch (lastCommand)
{
case 0x9F:
avrOn = false;
break;
case 0xC4:
case 0xD0:
case 0xC0:
avrOn = true;
Wifi::reconnect();
break;
default:
break;
}
}
if (!avrOn && Mqtt::commands.getCurrent() == 0xC7) {
Display::changeBrightness(true);
Mqtt::commands.pop();
}
if (!avrOn && Mqtt::commands.getCurrent() == 0xC8) {
Display::changeBrightness(false);
Mqtt::commands.pop();
}
}
}
#else
namespace Ir {
bool avrOn = false;
void setup() {}
void loop() {}
}
#endif