/
main.cpp
59 lines (49 loc) · 1.25 KB
/
main.cpp
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
// Bricks
#include <Bricks.Brick.h>
#include <Bricks.Inbox.h>
using namespace Bricks;
// Local
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <IRsend.h>
#include <IRutils.h>
#include <IRcode.h>
#include <codes.h>
const uint16_t RECEIVER_PIN = D4;
const uint16_t SEND_PIN = D3;
const uint16_t BUFFER = 1024;
const uint8_t TIMEOUT = 50;
const uint16_t FREQUENCY = 38000; // in Hz. e.g. 38kHz.
IRrecv irrecv(RECEIVER_PIN, BUFFER, TIMEOUT);
IRsend irsend(SEND_PIN);
decode_results results;
IRCode *code;
// Bricks callbacks
void sendCode(const uint8_t *macAddr, const Message message) {
code = codes[atoi(message.value)];
}
void setup() {
// Logging
Serial.begin(115200);
Log.begin(LOG_LEVEL_NOTICE, &Serial);
// Configure ESPNow
gBrick.init();
gInbox.init("IR Controller");
gInbox.actions[0] = new Action("sendCode", &sendCode);
// Start the IR receiver / sender
irrecv.enableIRIn();
irsend.begin();
}
void loop() {
if(code) {
Log.notice("IRED: Sending [%s]" CR, code->key);
irsend.sendRaw(code->timings, code->length, FREQUENCY);
code = nullptr;
}
else if(irrecv.decode(&results)) {
Log.notice("IRED: Received signal" CR);
Log.notice(resultToSourceCode(&results).c_str());
irrecv.resume();
}
gInbox.loop();
}