-
Notifications
You must be signed in to change notification settings - Fork 0
/
_433D.cpp
469 lines (378 loc) · 9.68 KB
/
_433D.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
_433D.c
2015-11-17
Public Domain
*/
#include <stdio.h>
#include <stdlib.h>
#ifdef RPI
#ifndef WIRINGPI
#include <pigpiod_if2.h>
#include "_433D.h"
/*
RX
Code to read the wireless codes transmitted by 433 MHz wireless fobs.
*/
struct _433D_rx_s
{
int pi;
int gpio;
_433D_rx_CB_t cb;
int min_bits;
int max_bits;
int glitch;
int _cb_id;
int _in_code;
int _edge;
int _bits;
uint64_t _code;
int _gap;
int _t0, _t1;
int _ready;
_433D_rx_data_t _data;
uint32_t _last_edge_tick;
int _e0, _even_edge_len;
int _min0, _max0, _min1, _max1;
};
static void _timings(_433D_rx_t *self, int e0, int e1)
{
/*
Accumulates the short and long pulse length so that an
average short/long pulse length can be calculated. The
figures may be used to tune the transimission settings.
*/
int shorter, longer;
if (e0 < e1)
{
shorter = e0;
longer = e1;
}
else
{
shorter = e1;
longer = e0;
}
if (self->_bits)
{
self->_t0 += shorter;
self->_t1 += longer;
}
else
{
self->_t0 = shorter;
self->_t1 = longer;
}
self->_bits += 1;
}
static void _calibrate(_433D_rx_t *self, int e0, int e1)
{
/*
The first pair of pulses is used as the template for
subsequent pulses. They should be one short, one long, not
necessarily in that order. The ratio between long and short
should really be 2 or more. If less than 1.5 the pulses are
assumed to be noise.
*/
float ratio;
int slack0, slack1;
self->_bits = 0;
_timings(self, e0, e1);
self->_bits = 0;
ratio = (float)self->_t1 / (float)self->_t0;
if (ratio < 1.5) self->_in_code = 0;
slack0 = 0.5 * self->_t0;
slack1 = 0.3 * self->_t1;
self->_min0 = self->_t0 - slack0;
self->_max0 = self->_t0 + slack0;
self->_min1 = self->_t1 - slack1;
self->_max1 = self->_t1 + slack1;
}
static int _test_bit(_433D_rx_t *self, int e0, int e1)
{
/*
Returns the bit value represented by the sequence of pulses.
0: short long
1: long short
2: illegal sequence
*/
_timings(self, e0, e1);
if ( (self->_min0 < e0) && (e0 < self->_max0) &&
(self->_min1 < e1) && (e1 < self->_max1) )
return 0;
else if ( (self->_min0 < e1) && (e1 < self->_max0) &&
(self->_min1 < e0) && (e0 < self->_max1) )
return 1;
else
return 2;
}
static void _cb_RX(
int pi, unsigned gpio, unsigned level, uint32_t tick, void *user)
{
/*
Accumulates the code from pairs of short/long pulses.
The code end is assumed when an edge greater than 5 ms
is detected.
*/
_433D_rx_t *self=(_433D_rx_t *)user;
int edge_len, bit;
// printf("pi=%d gpio=%d level=%d tick=%u\n", pi, gpio, level, tick);
edge_len = tick - self->_last_edge_tick;
self->_last_edge_tick = tick;
if (edge_len > 5000) /* 5000 us, 5 ms. */
{
if (self->_in_code)
{
if ((self->min_bits <= self->_bits) &&
(self->_bits <= self->max_bits))
{
self->_data.bits = self->_bits;
self->_data.code = self->_code;
self->_data.gap = self->_gap;
self->_data.t0 = self->_t0 / self->_bits;
self->_data.t1 = self->_t1 / self->_bits;
self->_ready = 1;
if (self->cb) self->cb(self->_data);
}
}
self->_in_code = 1;
self->_gap = edge_len;
self->_edge = 0;
self->_bits = 0;
self->_code = 0;
}
else if (self->_in_code)
{
if (self->_edge == 0) self->_e0 = edge_len;
else if (self->_edge == 1) _calibrate(self, self->_e0, edge_len);
if (self->_edge % 2) /* Odd edge. */
{
bit = _test_bit(self, self->_even_edge_len, edge_len);
self->_code = self->_code << 1;
if (bit == 1) self->_code += 1;
else if (bit != 0)
{
/* Uncomment the next block if you suspect timing problems. */
/*
if (self->_bits > 7)
{
printf("t=%d b=%d e0=%d e1=%d (b0=%d t0=%d b1=%d t1=%d) c=%u\n",
bit, self->_bits, self->_even_edge_len, edge_len,
self->_min0, self->_max0, self->_min1, self->_max1,
self->_code);
}
*/
self->_in_code = 0;
}
}
else /* Even edge. */
{
self->_even_edge_len = edge_len;
}
self->_edge += 1;
}
}
void _433D_rx_set_bits(_433D_rx_t *self, int min_bits, int max_bits)
{
if (min_bits < 6) min_bits = 6;
if (min_bits > 64) min_bits = 64;
if (max_bits < 6) max_bits = 6;
if (max_bits > 64) max_bits = 64;
if (min_bits <= max_bits)
{
self->min_bits = min_bits;
self->max_bits = max_bits;
}
}
void _433D_rx_set_glitch(_433D_rx_t *self, int glitch)
{
if (glitch < 0) glitch = 0;
if (glitch > 500) glitch = 500;
if (glitch != self->glitch)
{
set_glitch_filter(self->pi, self->gpio, glitch);
}
self->glitch = glitch;
}
int _433D_rx_ready(_433D_rx_t *self)
{
/*
Returns True if a new code is ready.
*/
return self->_ready;
}
uint64_t _433D_rx_code(_433D_rx_t *self)
{
/*
Returns the last receieved code.
*/
self->_ready = 0;
return self->_data.code;
}
void _433D_rx_data(_433D_rx_t *self, _433D_rx_data_t *data)
{
/*
Returns reading of the last receieved code. The reading
consist of the code, the number of bits, the length (in us)
of the gap, short pulse, and long pulse.
*/
self->_ready = 0;
*data = self->_data;
}
_433D_rx_t *_433D_rx(int pi, int gpio, _433D_rx_CB_t cb_func)
{
_433D_rx_t *self;
self = (_433D_rx_t *)malloc(sizeof(_433D_rx_t));
if (!self) return NULL;
self->pi = pi;
self->gpio = gpio;
self->cb = cb_func;
self->min_bits = 8;
self->max_bits = 32;
self->glitch = 150;
self->_in_code = 0;
self->_edge = 0;
self->_code = 0;
self->_gap = 0;
self->_ready = 0;
set_mode(pi, gpio, PI_INPUT);
set_glitch_filter(pi, gpio, self->glitch);
self->_last_edge_tick = get_current_tick(pi);
self->_cb_id = callback_ex(pi, gpio, EITHER_EDGE, _cb_RX, self);
return self;
}
void _433D_rx_cancel(_433D_rx_t *self)
{
if (self)
{
if (self->_cb_id >= 0)
{
set_glitch_filter(self->pi, self->gpio, 0);
callback_cancel(self->_cb_id);
self->_cb_id = -1;
}
free(self);
}
}
/*
TX
Code to transmit the wireless codes sent by 433 MHz wireless fobs.
*/
struct _433D_tx_s
{
int pi;
int gpio;
int repeats;
int bits;
int gap, t0, t1;
int _amble, _wid0, _wid1;
};
static void _make_waves(_433D_tx_t *self)
{
/*
Generates the basic waveforms needed to transmit codes.
*/
gpioPulse_t arr[] = {{(uint32_t)1<<self->gpio, (uint32_t)0, (uint32_t)self->t0},{(uint32_t)0, (uint32_t)1<<self->gpio, (uint32_t)self->gap}};
wave_add_generic(self->pi, 2, arr);
self->_amble = wave_create(self->pi);
gpioPulse_t arr2[] = {{(uint32_t)1<<self->gpio, (uint32_t)0, (uint32_t)self->t0}, {(uint32_t)0, (uint32_t)1<<self->gpio, (uint32_t)self->t1}};
wave_add_generic(self->pi, 2, arr2);
self->_wid0 = wave_create(self->pi);
gpioPulse_t arr3[] = {{(uint32_t)1<<self->gpio, (uint32_t)0, (uint32_t)self->t1}, {(uint32_t)0, (uint32_t)1<<self->gpio, (uint32_t)self->t0}};
wave_add_generic(self->pi, 2, arr3);
self->_wid1 = wave_create(self->pi);
}
void _433D_tx_set_repeats(_433D_tx_t *self, int repeats)
{
/*
Set the number of code repeats.
*/
if ((repeats >=1) && (repeats <= 50)) self->repeats = repeats;
}
void _433D_tx_set_bits(_433D_tx_t *self, int bits)
{
/*
Set the number of code bits.
*/
if ((bits >= 6) && (bits <= 64)) self->bits = bits;
}
void _433D_tx_set_timings(_433D_tx_t *self, int gap, int t0, int t1)
{
/*
Sets the code gap, short pulse, and long pulse length in us.
*/
self->gap = gap;
self->t0 = t0;
self->t1 = t1;
wave_delete(self->pi, self->_amble);
wave_delete(self->pi, self->_wid0);
wave_delete(self->pi, self->_wid1);
_make_waves(self);
}
void _433D_tx_send(_433D_tx_t *self, uint64_t code)
{
/*
Transmits the code (using the current settings of repeats,
bits, gap, short, and long pulse length).
*/
int i, p=0;
uint64_t bit;
char chain[256];
chain[p++] = self->_amble;
chain[p++] = 255;
chain[p++] = 0;
bit = ((uint64_t)1<<(self->bits-1));
for (i=0; i<self->bits; i++)
{
if (code & bit) chain[p] = self->_wid1;
else chain[p]= self->_wid0;
bit >>= 1;
p++;
}
chain[p++] = self->_amble;
chain[p++] = 255;
chain[p++] = 1;
chain[p++] = self->repeats;
chain[p++] = 0;
wave_chain(self->pi, chain, p);
while (wave_tx_busy(self->pi)) time_sleep(0.1);
}
_433D_tx_t *_433D_tx(int pi, int gpio)
{
/*
Instantiate with the Pi and the GPIO connected to the wireless
transmitter.
The number of repeats (default 6) and bits (default 24) may
be set with set_repeats and set_bits.
The pre-/post-amble gap (default 9000 us), short pulse length
(default 300 us), and long pulse length (default 900 us) may
be set with set_timings.
*/
_433D_tx_t *self;
self = (_433D_tx_t *)malloc(sizeof(_433D_tx_t));
if (!self) return NULL;
self->pi = pi;
self->gpio = gpio;
self->repeats = 6;
self->bits = 24;
self->gap = 9000;
self->t0 = 300;
self->t1 = 900;
_make_waves(self);
set_mode(pi, gpio, PI_OUTPUT);
return self;
}
void _433D_tx_cancel(_433D_tx_t *self)
{
/*
Cancels the wireless code transmitter.
*/
if (self)
{
wave_delete(self->pi, self->_amble);
wave_delete(self->pi, self->_wid0);
wave_delete(self->pi, self->_wid1);
free(self);
}
}
#endif
#endif