-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrc5.cpp
More file actions
76 lines (63 loc) · 1.96 KB
/
Copy pathrc5.cpp
File metadata and controls
76 lines (63 loc) · 1.96 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
72
73
74
75
76
#include "mbed.h"
#include "rc5.h"
#include "protocol.h"
#include <chrono>
#include <map>
#include <list>
namespace IR {
namespace RC5 {
static constexpr size_t SIGNAL_LEN = 14;
static constexpr size_t COMMAND_LEN = 6;
static const std::chrono::microseconds med_pulse = 1500us;
static const std::chrono::microseconds max_long_pulse = 3000us;
Decoder::Decoder(PinName pin, std::map<uint8_t, Callback<void()>> commands)
: BaseDecoder(pin, commands)
{
add_decoder_to_list(this);
};
bool Decoder::good_startcode(uint8_t com) {
return ((com & (1 << 0)) && (com & (1 << 1)));
}
void Decoder::decode_reset() {
command = 0;
decoding = true;
clock.reset();
clock.start();
cur_bit = 0;
}
void Decoder::decode_fall() {
if (!decoding || clock.elapsed_time() > max_long_pulse) {
decode_reset();
BaseDecoder::decode_bit(1);
return;
}
if (!GET_BIT(command, cur_bit) && clock.elapsed_time() < med_pulse) {
clock.reset();
return;
}
BaseDecoder::decode_bit(1);
}
bool Decoder::final_bit() {
return cur_bit >= SIGNAL_LEN;
}
static unsigned int swap_bits(unsigned int val, int bits_num) {
for (int i = 0; i < bits_num / 2; i++) {
val ^= ((val & (1 << i)) >> i) << (bits_num - i - 1);
val ^= ((val & (1 << (bits_num - i - 1))) >> (bits_num - i - 1)) << i;
val ^= ((val & (1 << i)) >> i) << (bits_num - i - 1);
}
return val;
}
bool Decoder::cmp_command(uint16_t cmd) {
return cmd == swap_bits((command >> 8) & 0x3F, COMMAND_LEN);
}
void Decoder::decode_rise() {
if (!decoding) return;
if (cur_bit && GET_BIT(command, cur_bit) && clock.elapsed_time() < med_pulse) {
clock.reset();
return;
}
BaseDecoder::decode_bit(0);
}
}
}