-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIRReciever.cpp
More file actions
executable file
·71 lines (58 loc) · 1.99 KB
/
Copy pathIRReciever.cpp
File metadata and controls
executable file
·71 lines (58 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "IRReciever.h"
#include "prefferences.h"
#if __has_include(<Arduino.h>)
#define RECORD_GAP_MICROS 5000 // ignore NEC repeat bursts
#include <IRremote.hpp>
#include <Arduino.h>
namespace {
constexpr uint8_t kCmdTempUp = 0x46;
constexpr uint8_t kCmdTempDown = 0x15;
constexpr uint8_t kCmdToggle = 0x40;
bool decodeCommand(uint8_t byte, Command& out) {
if (byte == kCmdToggle) { out = Command::ON_OFF; return true; }
if (byte == kCmdTempUp) { out = Command::TEMP_UP; return true; }
if (byte == kCmdTempDown) { out = Command::TEMP_DOWN; return true; }
return false;
}
} // namespace
void IRReceiver::begin() {
IrReceiver.begin(kIrRxPin, DISABLE_LED_FEEDBACK);
Serial.printf("[IR-RX] Ready on GPIO %d\n", kIrRxPin);
}
bool IRReceiver::poll(DecodedFrame& outFrame) {
if (!IrReceiver.decode()) return false;
auto& d = IrReceiver.decodedIRData;
if ((d.flags & IRDATA_FLAGS_IS_REPEAT) || d.protocol == UNKNOWN) {
IrReceiver.resume();
return false;
}
const uint8_t cmdByte = static_cast<uint8_t>(d.command & 0xFF);
const uint16_t addr = static_cast<uint16_t>(d.address);
const int proto = static_cast<int>(d.protocol);
IrReceiver.resume();
Command cmd = Command::NONE;
if (!decodeCommand(cmdByte, cmd)) {
cmd = Command::CUSTOM;
Serial.printf("[IR-RX] custom cmd=0x%02X (addr=0x%04X proto=%d)\n",
cmdByte, addr, proto);
}
const uint32_t now = millis();
if (cmd == lastCmd_ && (now - lastCmdMs_) < kDebouncMs) {
return false;
}
lastCmd_ = cmd;
lastCmdMs_ = now;
if (cmd != Command::CUSTOM) {
Serial.printf("[IR-RX] cmd=0x%02X -> %s\n", cmdByte, commandToString(cmd));
}
outFrame.command = cmd;
outFrame.rawCmd = cmdByte;
outFrame.rawAddress = addr;
outFrame.rawProto = proto;
return true;
}
#else
// Desktop stub
void IRReceiver::begin() {}
bool IRReceiver::poll(DecodedFrame&) { return false; }
#endif