-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPT22x2.cpp
More file actions
175 lines (159 loc) · 5.27 KB
/
PT22x2.cpp
File metadata and controls
175 lines (159 loc) · 5.27 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "PT22x2.hpp"
#include <exception>
#include <iostream>
#include <thread>
using std::chrono::microseconds;
using std::array;
using std::runtime_error;
using std::this_thread::sleep_for;
namespace PT22x2 {
Decoder::Decoder(function<void(Codeword)> codewordHandler) :
lasttime(microseconds(0)),
receiving(false),
position(0),
codewordHandler(codewordHandler)
{
}
void Decoder::edgeOccured(microseconds time)
{
auto now = time;
auto delta = now - lasttime;
lasttime = now;
if (delta >= pulsewidth_sync - pulsewidth_tolerance) {
// Sync received, start new codeword
receiving = true;
position = 0;
return;
}
if (receiving) {
if (pulsewidth_short - pulsewidth_tolerance <= delta &&
pulsewidth_short + pulsewidth_tolerance >= delta) {
pulses[position++] = Pulse::Short;
}
else if (pulsewidth_long - pulsewidth_tolerance <= delta &&
pulsewidth_long + pulsewidth_tolerance >= delta) {
pulses[position++] = Pulse::Long;
}
else {
// Noise, stop receiving
receiving = false;
position = 0;
return;
}
if (position == 4*codeword_length) {
// Codeword received, stop receiving and print
receiving = false;
position = 0;
bool got_codeword = false;
Codeword codeword;
try {
codeword = codewordFromPulses(pulses);
got_codeword = true;
}
catch (runtime_error const &e) {
}
if (got_codeword && codewordHandler) {
codewordHandler(codeword);
}
}
}
}
Codeword Decoder::codewordFromPulses(CodewordPulses pulses) {
Codeword codeword;
for (int i=0; i < codeword_length; i++) {
if (pulses[4*i] == Pulse::Short &&
pulses[4*i+1] == Pulse::Long &&
pulses[4*i+2] == Pulse::Short &&
pulses[4*i+3] == Pulse::Long) {
codeword[i] = CodeBit::Zero;
continue;
}
if (pulses[4*i] == Pulse::Long &&
pulses[4*i+1] == Pulse::Short &&
pulses[4*i+2] == Pulse::Long &&
pulses[4*i+3] == Pulse::Short) {
codeword[i] = CodeBit::One;
continue;
}
if (pulses[4*i] == Pulse::Short &&
pulses[4*i+1] == Pulse::Long &&
pulses[4*i+2] == Pulse::Long &&
pulses[4*i+3] == Pulse::Short) {
codeword[i] = CodeBit::Floating;
continue;
}
throw runtime_error("malformed pulses");
}
return codeword;
}
Encoder::Encoder(GPIOPin pin) : pin(pin) {}
CodewordPulses Encoder::pulsesFromCodeword(Codeword codeword) {
CodewordPulses pulses;
for (int i=0; i < codeword_length; i++) {
switch (codeword[i]) {
case CodeBit::Zero:
pulses[4*i] = Pulse::Short;
pulses[4*i+1] = Pulse::Long;
pulses[4*i+2] = Pulse::Short;
pulses[4*i+3] = Pulse::Long;
break;
case CodeBit::One:
pulses[4*i] = Pulse::Long;
pulses[4*i+1] = Pulse::Short;
pulses[4*i+2] = Pulse::Long;
pulses[4*i+3] = Pulse::Short;
break;
case CodeBit::Floating:
pulses[4*i] = Pulse::Short;
pulses[4*i+1] = Pulse::Long;
pulses[4*i+2] = Pulse::Long;
pulses[4*i+3] = Pulse::Short;
break;
}
}
return pulses;
}
void Encoder::send(Codeword codeword) {
auto pulses = pulsesFromCodeword(codeword);
bool nextPulseHigh = true;
for (int i=0; i < codeword_length * 4; i++) {
if (nextPulseHigh) {
pin.setHigh();
nextPulseHigh = false;
}
else {
pin.setLow();
nextPulseHigh = true;
}
if (pulses[i] == Pulse::Short) {
sleep_for(pulsewidth_short);
}
else {
sleep_for(pulsewidth_long);
}
}
// SYNC bit
pin.setHigh();
sleep_for(pulsewidth_short);
pin.setLow();
sleep_for(pulsewidth_sync);
}
}
std::ostream& operator<<(std::ostream &strm, const PT22x2::Codeword &codeword) {
strm << "<Codeword: ";
for (int i=0; i < PT22x2::codeword_length; i++) {
switch (codeword[i]) {
case PT22x2::CodeBit::Zero:
strm << '0';
break;
case PT22x2::CodeBit::One:
strm << '1';
break;
case PT22x2::CodeBit::Floating:
strm << 'f';
break;
}
}
strm << ">";
return strm;
}