-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.cpp
More file actions
63 lines (52 loc) · 1.44 KB
/
Copy pathprotocol.cpp
File metadata and controls
63 lines (52 loc) · 1.44 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
#include "mbed.h"
#include "protocol.h"
#include <stdint.h>
#include <vector>
namespace IR {
void BaseDecoder::decode_bit(uint8_t bit) {
if (bit) {
command |= (bit << cur_bit);
}
cur_bit++;
if (final_bit()) {
uint8_t com = command;
decoding = false;
cur_bit = 0;
clock.stop();
clock.reset();
if (good_startcode(com)) {
for (auto & cmd : commands) {
if (cmp_command(cmd.first)) {
cmd.second();
}
}
}
command = 0;
return;
}
clock.reset();
}
BaseDecoder::BaseDecoder(PinName pin, std::map<uint8_t, Callback<void()>> commands):
signal{pin}, commands{commands}
{
signal.mode(PullNone);
signal.rise(&on_edge);
signal.fall(&on_edge);
}
static std::vector<BaseDecoder *> decoders_list;
void add_decoder_to_list(BaseDecoder *dec) {
decoders_list.push_back(dec);
}
void on_edge() {
for (auto dec : decoders_list) {
if (dec->signal.read() != dec->prev_signal_val) {
dec->prev_signal_val = !dec->prev_signal_val;
}
if (dec->signal.read()) {
dec->decode_rise();
} else {
dec->decode_fall();
}
}
}
}