-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
290 lines (262 loc) · 11.7 KB
/
test.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <string.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#define MANCHESTER_GAP_DURATION 4 // us
#define MANCHESTER_GAP_WIDTH (MANCHESTER_GAP_DURATION)
#define MANCHESTER_GAP_MIN (MANCHESTER_GAP_WIDTH - MANCHESTER_GAP_WIDTH/2)
#define MANCHESTER_GAP_MAX (MANCHESTER_GAP_WIDTH + MANCHESTER_GAP_WIDTH/2 + 4)
#define MANCHESTER_GAP_LONG_DURATION 8 // us
#define MANCHESTER_GAP_SHORT_DURATION 4 // us
#define MANCHESTER_GAP_LONG_WIDTH (MANCHESTER_GAP_LONG_DURATION)
#define MANCHESTER_GAP_SHORT_WIDTH (MANCHESTER_GAP_SHORT_DURATION)
#define MANCHESTER_GAP_START_WIDTH_THRESHOLD_MIN (MANCHESTER_GAP_LONG_WIDTH)
#define MANCHESTER_GAP_START_WIDTH_THRESHOLD_MAX (MANCHESTER_GAP_LONG_WIDTH) * 1.5
#define MANCHESTER_GAP_LONG_WIDTH_THRESHOLD (MANCHESTER_GAP_LONG_WIDTH - MANCHESTER_GAP_LONG_WIDTH/8)
#define MANCHESTER_GAP_SHORT_WIDTH_THRESHOLD (MANCHESTER_GAP_SHORT_WIDTH - MANCHESTER_GAP_SHORT_WIDTH/8)
#define MANCHESTER_END MANCHESTER_GAP_LONG_WIDTH * 1.5
enum miller_state {
WAIT_FOR_START,
LAST_BIT_ZERO,
LAST_BIT_ONE,
END_OF_FRAME,
};
static enum miller_state current_state = WAIT_FOR_START;
static unsigned int count_one = 0;
static unsigned int count_zero = 0;
static unsigned int decoded_bit_num = 0;
static unsigned char current_frame[1000] = { 0 };
static unsigned char
compute_even_parity (unsigned char c)
{
unsigned int i;
unsigned char parity = 0;
for (i = 0; i < 8; i++) {
parity ^= (c & (0x1 << i)) >> i;
}
return parity;
}
static void
set_next_bit (unsigned char bit)
{
if (bit) {
/* 1 */
current_frame[decoded_bit_num] = 1;
} else {
/* 0 */
current_frame[decoded_bit_num] = 0;
}
decoded_bit_num++;
}
static unsigned char
get_bit (unsigned int n)
{
return current_frame[n];
}
static void
clear_frame (void)
{
memset(current_frame, 0, sizeof(current_frame));
decoded_bit_num = 0;
}
int in[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int main(){
#ifdef DEBUG
std::cout << " MANCHESTER_GAP_WIDTH " << MANCHESTER_GAP_WIDTH << std::endl;
std::cout << " MANCHESTER_GAP_MIN " << MANCHESTER_GAP_MIN << std::endl;
std::cout << " MANCHESTER_GAP_MAX " << MANCHESTER_GAP_MAX << std::endl;
std::cout << " MANCHESTER_GAP_LONG_WIDTH " << MANCHESTER_GAP_LONG_WIDTH << std::endl;
std::cout << " MANCHESTER_GAP_SHORT_WIDTH " << MANCHESTER_GAP_SHORT_WIDTH << std::endl;
std::cout << " MANCHESTER_GAP_START_WIDTH_THRESHOLD_MIN " << MANCHESTER_GAP_START_WIDTH_THRESHOLD_MIN << std::endl;
std::cout << " MANCHESTER_GAP_START_WIDTH_THRESHOLD_MAX " << MANCHESTER_GAP_START_WIDTH_THRESHOLD_MAX << std::endl;
std::cout << " MANCHESTER_GAP_LONG_WIDTH_THRESHOLD " << MANCHESTER_GAP_LONG_WIDTH_THRESHOLD << std::endl;
std::cout << " MANCHESTER_GAP_SHORT_WIDTH_THRESHOLD " << MANCHESTER_GAP_SHORT_WIDTH_THRESHOLD << std::endl;
std::cout << " MANCHESTER_END " << MANCHESTER_END << std::endl;
#endif
int decoded_bytes_num = 0;
unsigned char no_parity_mode = 0;
for (int i = 0; i < sizeof(in); i++) {
if (current_state == WAIT_FOR_START){
if (in[i] > 0) {
if(count_zero > 0){
if(count_zero >= MANCHESTER_GAP_START_WIDTH_THRESHOLD_MIN && count_zero <= MANCHESTER_GAP_START_WIDTH_THRESHOLD_MAX) {
if (count_one > MANCHESTER_GAP_START_WIDTH_THRESHOLD_MIN) {
// This is the first pulse of a frame (START)
#ifdef DEBUG
std::cout << " THIS IS WHERE OUR DREAM START" << std::endl;
#endif
current_state = LAST_BIT_ONE;
}
count_one = 0;
} else if (count_zero < MANCHESTER_GAP_MIN) {
// noise
count_one += count_zero;
// count_zero should be initialized.
count_zero = 0;
} else {
count_zero = 0;
}
}
count_one++;
} else {
count_zero++;
}
} else if (current_state == LAST_BIT_ZERO) {
if (in[i] > 0) {
if (count_zero > 0) {
if (count_zero > MANCHESTER_END && decoded_bit_num > 0) {
#ifdef DEBUG
/* END OF FRAME
* in case of of noise
* (1, 0, END) (0, 0, END)
*/
std::cout << " END (0, 0, END) " << std::endl;
#endif
current_state = END_OF_FRAME;
} else if (count_zero >= MANCHESTER_GAP_MIN && count_zero <= MANCHESTER_GAP_MAX) {
if (count_one > MANCHESTER_GAP_LONG_WIDTH_THRESHOLD) {
#ifdef DEBUG
std::cout << " 1 (LAST_BIT_ZERO)" << std::endl;
#endif
// before_last_bit = 0, last_bit = 1
current_state = LAST_BIT_ONE;
set_next_bit(1);
} else if (count_one > MANCHESTER_GAP_SHORT_WIDTH_THRESHOLD) {
#ifdef DEBUG
std::cout << " 0 (LAST_BIT_ZERO)" << std::endl;
#endif
current_state = LAST_BIT_ZERO;
set_next_bit(0);
// before_last_bit = 0, last bit = 0
count_zero = 0;
}
count_one = 0;
} else if (count_zero < MANCHESTER_GAP_MIN) {
// Consider the zeros as ones (noise)
count_one += count_zero;
count_zero = 0;
} }
count_one++;
} else {
if (count_zero > MANCHESTER_END && decoded_bit_num > 0) {
if (count_one > MANCHESTER_GAP_LONG_WIDTH_THRESHOLD) {
set_next_bit(1);
#ifdef DEBUG
std::cout << " END (0, 1, END) (last_bit_zero) " << std::endl;
#endif
current_state = END_OF_FRAME;
} else {
#ifdef DEBUG
// in case of 1, 0, end
std::cout << " END (1, 0, END) " << std::endl;
#endif
current_state = END_OF_FRAME;
}
} else {
count_zero++;
}
}
} else if (current_state == LAST_BIT_ONE) {
if (count_zero > MANCHESTER_END && decoded_bit_num > 0) {
/* in case of 1, 1, end
* in case of 0, 1, end
*/
#ifdef DEBUG
std::cout << " END (1, 1, END) (0, 1, END) " << std::endl;
#endif
current_state = END_OF_FRAME;
} else { if (in[i] == 0){
if (count_one > 0) {
if (count_one >= MANCHESTER_GAP_MIN && count_one <= MANCHESTER_GAP_MAX) {
if (count_zero > MANCHESTER_GAP_LONG_WIDTH_THRESHOLD) {
#ifdef DEBUG
std::cout << " 0 (LAST_BIT_ONE)" << std::endl;
#endif
current_state = LAST_BIT_ZERO;
set_next_bit(0);
// before_last_bit = 1, last_bit = 0
} else if (count_zero > MANCHESTER_GAP_SHORT_WIDTH_THRESHOLD) {
#ifdef DEBUG
std::cout << " 1 (LAST_BIT_ONE)" << std::endl;
#endif
current_state = LAST_BIT_ONE;
set_next_bit(1);
// before_last_bit = 1, last_bit = 1
count_one = 0;
}
count_zero = 0;
} else if (count_one < MANCHESTER_GAP_MIN) {
// Consider the ones as zeros (noise)
count_zero += count_one;
count_one = 0;
}
}
count_zero++;
} else {
count_one++;
} }
}
if (current_state == END_OF_FRAME) {
unsigned char tmp_byte = 0;
unsigned int in_bit = 0, out_bit = 0;
unsigned char parity_ok;
if (decoded_bit_num > 0) {
/* Assume that the frame is in no parity mode if its length
* is valid in no parity mode, and not in Standard mode.
* NOTE: For a length of 9x8xN, the last known mode is used.
*/
if ((decoded_bit_num % 72) != 0) {
no_parity_mode = (((decoded_bit_num % 9) != 0) && ((decoded_bit_num % 8) == 0));
}
/* Decode and print the frame */
printf("Tag ->");
#ifdef DEBUG
printf(" ");
#endif
while (in_bit < decoded_bit_num) {
int out[] = {};
out[decoded_bytes_num] |= get_bit(in_bit) << out_bit;
#ifdef DEBUG
printf("%u", get_bit(in_bit));
#endif
in_bit++;
out_bit++;
if (!no_parity_mode && (out_bit == 8 && in_bit < decoded_bit_num)) {
/* Check parity if needed */
parity_ok = (compute_even_parity(out[decoded_bytes_num]) == !get_bit(in_bit));
#ifdef DEBUG
printf("-%u", get_bit(in_bit));
#endif
in_bit++;
}
if (out_bit == 8 || in_bit == decoded_bit_num) {
/* Print the byte */
if (decoded_bit_num == 7) {
/* Short command */
printf(" [%02X]", out[decoded_bytes_num]);
} else if (out_bit < 8 || (decoded_bit_num == 8 && !no_parity_mode)) {
/* Broken */
printf(" /%02X\\", out[decoded_bytes_num]);
} else if (parity_ok || no_parity_mode) {
printf(" %02X ", out[decoded_bytes_num]);
} else {
printf(" (%02X)", out[decoded_bytes_num]);
}
#ifdef DEBUG
printf(" ");
#endif
decoded_bytes_num++;
out_bit = 0;
}
}
if (no_parity_mode) {
printf(" (No parity)");
}
printf("\n");
clear_frame();
}
current_state = WAIT_FOR_START;
}
}
return 0;
}