-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRF433any.cpp
1925 lines (1632 loc) · 56.1 KB
/
RF433any.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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// RF433any.cpp
// See README.md about the purpose of this library
/*
Copyright 2021 Sébastien Millet
`RF433any' is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
`RF433any' is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this program. If not, see
<https://www.gnu.org/licenses>.
*/
/*
Schematic
1. Arduino board. Tested with NANO and UNO.
2. Radio Frequence 433Mhz RECEIVER like MX-RM-5V.
RF433 RECEIVER data pin must be plugged on a board' digital PIN that can
trigger interrupts, that is, D2 or D3.
This RECEIVER PIN is defined at the time a 'Track' object is created. This
library does not set it at compile time.
See file schema.fzz (Fritzing format) or schema.png, for a circuit example
with receiver plugged on D2.
*/
/*
**About the classes Band, Rail and Track**
1. About none of these - the signal as we see it.
The Radio-Frequence signal is supposed to be OOK (On-Off Keying), and
auto-synchronized.
The signal is a succession of low signal and high signal, low when no RF signal
received, high when a RF signal is received.
The coding relies on durations being either 'short' or 'long', and sometimes
much longer (to initialize, and to separate signal pieces).
The durations can be one of:
- short
- long, typically, twice as long as short
- separator, much longer than the long one (at least 3 or 4 times longer)
- initialization, at least as long as the separator, often much longer. It
serves to make receiver ready to receive coded signal to come.
A signal structure is as follows:
1. Initialization (very long high signal)
2. Succession of low and high signals being 'short' or 'long'
3. Separator (high signal)
4. Possibly, repetition of steps 2 and 3
The succession of 'short' and 'long' is then decoded into original data, either
based on tri-bit scheme (inverted or not), or, Manchester.
Note that there can be complexities:
- After the long initialization high signal, addition of 'intermediate' prefix
to the signal (longer than 'long', but shorter than 'separator'). Seen on a
NICE FLO/R telecommand (/R means Rolling Code), while not seen on NICE FLO
(fix code). The author guesses this prefix serves to let the receiver know the
signal to come is FLO/R instead of FLO.
- After the long initialization high signal, succession of {low=short,
high=short} followed by a separator. This serves as a synchronization
sequence.
- While most protocols use same lengths for low and high signals, on NICE FLO/R
this rule is not met, that is: the 'short' and 'long' durations of the low
signal are different from 'short' and 'long' durations of the high signal.
2. About Rail
The Rail manages the succession of durations for one, and only one, of signal
realms (low or high).
That is, if you note dow the signal as usually (by line, one low followed by one
high):
LOW, HIGH
150, 200
145, 400
290, 195
...
Then the values below LOW (150, 145, 290, ...) are one Rail, and the values
below HIGH (200, 400, 195, ...) are another Rail.
3. About Bands
A band aims to categorize a duration, short or long. Therefore, a Rail is made
of 2 bands, one for the short duration, one for the long duration.
4. About Tracks
Rails live their own live but at some point, they must work in conjunction
(start and stop together, and provide final decoded values). This is the purpose
of a Track, that is made of 2 Rails.
In the end, a Track provides a convenient interface to the caller.
5. Overall schema
track -> r_low -> b_short = manage short duration on LOW signal
| `-> b_long = manage long duration on LOW signal
|
`-> r_high -> b_short = manage short duration on HIGH signal
`-> b_long = manage long duration on HIGH signal
*/
#include "RF433any.h"
#include <Arduino.h>
#define ASSERT_OUTPUT_TO_SERIAL
#define assert(cond) { \
if (!(cond)) { \
rf433any_assert_failed(__LINE__); \
} \
}
static void rf433any_assert_failed(int line) {
#ifdef ASSERT_OUTPUT_TO_SERIAL
Serial.print("\nRF433any.cpp:");
Serial.print(line);
Serial.println(": assertion failed, aborted.");
#endif
while (1)
;
}
// * ****************** *******************************************************
// * compact, uncompact *******************************************************
// * ****************** *******************************************************
// compact() aims to represent 16-bit integers in 8-bit, to the cost of
// precision.
// The three sets (first one looses 4 bits, middle looses 7, last looses 12)
// have been chosen so that smaller durations don't loose too much precision.
// The higher the number, the more precision gets lost. This could be seen as
// 'the floating point number representation of the (very) poor man' (or,
// floating point numbers without... a floating point!)
//
// Any way, keep in mind Arduino timer produces values always multiple of 4,
// that shifts bit-loss by 2.
// For example, the first set (that looses 4 bits) actually really looses 2 bits
// of precision.
duration_t compact(uint16_t u) {
#ifdef RF433ANY_DBG_NO_COMPACT_DURATIONS
// compact not activated -> compact() is a no-op
return u;
#else
if (u < 2048) {
return u >> 4;
}
if (u < 17408) {
return 128 + ((u - 2048) >> 7);
}
if (u < 46080)
return 248 + ((u - 17408) >> 12);
return 255;
#endif
}
// uncompact() is the opposite of compact(), yes!
// Left here in case tests are needed (it is not used in release code).
uint16_t uncompact(duration_t b) {
#ifdef RF433ANY_DBG_NO_COMPACT_DURATIONS
// compact not activated -> uncompact() is a no-op
return b;
#else
uint16_t u = b;
if (u < 128) {
return u << 4;
}
u &= 0x7f;
if (u < 120) {
return (u << 7) + 2048;
}
return ((u - 120) << 12) + 17408;
#endif
}
// * **** *********************************************************************
// * Band *********************************************************************
// * **** *********************************************************************
inline void Band::breset() {
inf = 0;
sup = 0;
mid = 0;
}
inline bool Band::init(uint16_t d) {
#ifdef RF433ANY_DBG_TRACE
dbgf("B> init: %u", d);
#endif
if (d >= BAND_MIN_D && d <= BAND_MAX_D) {
mid = d;
uint16_t d_divided_by_4 = d >> 2;
inf = d - d_divided_by_4;
sup = d + d_divided_by_4;
got_it = true;
} else {
got_it = false;
}
return got_it;
}
inline bool Band::init_sep(uint16_t d) {
#ifdef RF433ANY_DBG_TRACE
dbgf("BSEP> init: %u", d);
#endif
sup = RF433ANY_MAX_SEP_DURATION;
inf = d >> 1;
inf += (inf >> 2);
mid = d;
got_it = true;
return got_it;
}
inline bool Band::test_value_init_if_needed(uint16_t d) {
if (!mid) {
init(d);
} else {
got_it = (d >= inf && d <= sup);
#ifdef RF433ANY_DBG_TRACE
dbgf("B> cmp %u to [%u, %u]", d, inf, sup);
#endif
}
#ifdef RF433ANY_DBG_TRACE
dbgf("B> res: %d", got_it);
#endif
return got_it;
}
inline bool Band::test_value(uint16_t d) {
if (!mid) {
got_it = false;
#ifdef RF433ANY_DBG_TRACE
dbgf("BSEP> cmp %u to uninitialized d", d);
#endif
} else {
got_it = (d >= inf && d <= sup);
#ifdef RF433ANY_DBG_TRACE
dbgf("BSEP> cmp %u to [%u, %u]", d, inf, sup);
#endif
}
#ifdef RF433ANY_DBG_TRACE
dbgf("BSEP> res: %d", got_it);
#endif
return got_it;
}
// * **** *********************************************************************
// * Rail *********************************************************************
// * **** *********************************************************************
Rail::Rail(byte arg_mood):mood(arg_mood) {
rreset();
}
inline void Rail::rreset() {
rreset_soft();
b_short.breset();
b_long.breset();
b_sep.breset();
}
inline void Rail::rreset_soft() {
status = RAIL_OPEN;
index = 0;
rec = 0;
}
inline bool Rail::rail_eat(uint16_t d) {
#ifdef RF433ANY_DBG_TRACE
dbgf("R> index = %d, d = %u", index, d);
#endif
if (status != RAIL_OPEN)
return false;
byte count_got_it = 0;
if (b_short.test_value_init_if_needed(d))
++count_got_it;
if (b_long.test_value_init_if_needed(d))
++count_got_it;
byte band_count = get_band_count();
#ifdef RF433ANY_DBG_TRACE
dbgf("R> b_short.got_it = %d, b_long.got_it = %d, "
"band_count = %d", b_short.got_it, b_long.got_it,
band_count);
for (int i = 0; i < 2; ++i) {
dbgf("R> [%i]: inf = %u, mid = %u, sup = %u", i,
(i == 0 ? b_short.inf : b_long.inf),
(i == 0 ? b_short.mid : b_long.mid),
(i == 0 ? b_short.sup : b_long.sup));
}
#endif
if (band_count == 1 && !count_got_it) {
Band *pband;
// IMPORTANT
// We are using below 'unsigned long' although they are
// initialized using uint16_t values.
// We need 'unsigned long' because later in the code, we check
// whether 'big' is not more than 4 times 'short' (if it is, then
// the coding shape is too distorted and we give up).
// We do this check by calculating 'small << 2', therefore it
// could be that this operation ends up above 16-bit max unsigned
// integer value.
unsigned long small;
unsigned long big;
if (d < b_short.inf) {
pband = &b_short;
small = d;
big = b_short.mid;
} else if (d > b_short.sup) {
pband = &b_long;
small = b_short.mid;
big = d;
} else {
// Should not happen.
// If value is within band range, then why the hell didn't the
// range grab it?
assert(false);
}
#ifdef RF433ANY_DBG_TRACE
dbg("R> P0");
dbgf("R> small = %lu, small * 4 = %lu, big = %lu",
small, small << 2, big);
#endif
if ((small << 2) >= big) {
if (pband->init(d)) {
#ifdef RF433ANY_DBG_TRACE
dbg("R> P1");
#endif
// As we now know who's who (b_short is b_short and b_long
// is b_long, yes), we can adjust boundaries accordingly.
b_short.inf = (b_short.mid >> 1) - (b_short.mid >> 3);
if (mood == RAIL_MOOD_LAXIST) {
b_short.sup = (b_short.mid + b_long.mid) >> 1;
b_long.inf = b_short.sup + 1;
}
b_long.sup = b_long.mid + (b_long.mid >> 1) + (b_long.mid >> 3);
count_got_it = 1;
band_count = 2;
// Test if intervals overlap?
// That is, test if b_short.sup >= b_long.inf?
// Not done for now...
;
if (pband == &b_short) {
// The first N signals received ('N' equals 'index')
// happened to be LONG ones => to be recorded as as many
// ONEs.
rec = ((recorded_t)1 << index) - 1;
}
}
}
}
if (!band_count) {
status = RAIL_ERROR;
return false;
}
if (!count_got_it || (band_count == 2 && count_got_it == 2)) {
if (!b_sep.mid) {
// BAND_MAX_D is 30000, and multiplying .mid by 2 will produce a
// maximum value of 60000, that's OK for an unsigned 16-bit int.
if (d >= (b_short.mid << 1) && d >= (b_long.mid << 1)) {
#ifdef RF433ANY_DBG_TRACE
dbg("R> init b_sep");
#endif
// We can end up with an overlap between b_sep and b_long.
// Not an issue.
b_sep.init_sep(d);
} else {
#ifdef RF433ANY_DBG_TRACE
dbg("R> no init of b_sep (d too small)");
#endif
}
}
status = (b_sep.test_value(d) ? RAIL_STP_RCVD : RAIL_ERROR);
#ifdef RF433ANY_DBG_TRACE
dbgf("R> rail terminated, status = %d", status);
#endif
} else {
if (band_count == 2) {
if (b_short.got_it == b_long.got_it) {
assert(false);
}
last_bit_recorded = (b_short.got_it ? 0 : 1);
rec = (rec << 1) | last_bit_recorded;
} else {
last_bit_recorded = 0;
}
if (++index == (sizeof(rec) << 3)) {
status = RAIL_FULL;
}
}
return (status == RAIL_OPEN);
}
#ifdef RF433ANY_DBG_TRACK
const char* status_names[] = {
"open",
"full",
"stop received",
"closed",
"error"
};
void Rail::rail_debug() const {
dbgf(" \"bits\":%i,\"v\":0x" FMTRECORDEDT
",\"railstatus\":\"%s\",\"n\":%d,", index, rec, status_names[status],
(b_short.mid == b_long.mid ? 1 : 2));
for (byte i = 0; i < 3; ++i) {
dbgf(" \"%s\":{\"inf\":%u,\"mid\":%u,\"sup\":%u}%s",
(i == 0 ? "b_short" : (i == 1 ? "b_long" : "b_sep")),
(i == 0 ? b_short.inf : (i == 1 ? b_long.inf : b_sep.inf)),
(i == 0 ? b_short.mid : (i == 1 ? b_long.mid : b_sep.mid)),
(i == 0 ? b_short.sup : (i == 1 ? b_long.sup : b_sep.sup)),
(i == 2 ? "" : ",")
);
}
}
#endif
byte Rail::get_band_count() const {
return b_short.mid == b_long.mid ? (b_short.mid ? 1 : 0) : 2;
}
// * **** *********************************************************************
// * Misc *********************************************************************
// * **** *********************************************************************
#ifdef RF433ANY_DBG_RAWCODE
const char *sts_names[] = {
"CONT",
"SSEP",
"LSEP",
"2SEP",
"ERR"
};
void RawCode::debug_rawcode() const {
dbgf("> nb_sections = %d, initseq = %u",
nb_sections, initseq);
for (byte i = 0; i < nb_sections; ++i) {
const Section *psec = §ions[i];
dbgf(" %02d %s", i, sts_names[psec->sts]);
dbgf(" sep = %u", psec->ts.sep);
dbgf(" low: [%d] n = %2d, v = 0x" FMTRECORDEDT "",
psec->low_bands, psec->low_bits, psec->low_rec);
dbgf(" high: [%d] n = %2d, v = 0x" FMTRECORDEDT "",
psec->high_bands, psec->high_bits, psec->high_rec);
}
}
#endif
// * ********* ****************************************************************
// * BitVector ****************************************************************
// * ********* ****************************************************************
BitVector::BitVector():
array(nullptr),
allocated(0),
nb_bits(0) {
}
void BitVector::prepare_BitVector_construction(short arg_nb_bits,
short arg_nb_bytes, short n) {
assert(arg_nb_bits > 0);
assert((arg_nb_bits + 7) >> 3 == arg_nb_bytes);
assert(arg_nb_bytes == n);
array = (uint8_t*)malloc(arg_nb_bytes);
allocated = arg_nb_bytes;
nb_bits = arg_nb_bits;
}
BitVector::BitVector(short arg_nb_bits, short arg_nb_bytes, byte b0,
byte b1) {
prepare_BitVector_construction(arg_nb_bits, arg_nb_bytes, 2);
array[1] = b0;
array[0] = b1;
}
BitVector::BitVector(short arg_nb_bits, short arg_nb_bytes, byte b0, byte b1,
byte b2) {
prepare_BitVector_construction(arg_nb_bits, arg_nb_bytes, 3);
array[2] = b0;
array[1] = b1;
array[0] = b2;
}
BitVector::BitVector(short arg_nb_bits, short arg_nb_bytes, byte b0, byte b1,
byte b2, byte b3) {
prepare_BitVector_construction(arg_nb_bits, arg_nb_bytes, 4);
array[3] = b0;
array[2] = b1;
array[1] = b2;
array[0] = b3;
}
BitVector::BitVector(short arg_nb_bits, short arg_nb_bytes, byte b0, byte b1,
byte b2, byte b3, byte b4) {
prepare_BitVector_construction(arg_nb_bits, arg_nb_bytes, 5);
array[4] = b0;
array[3] = b1;
array[2] = b2;
array[1] = b3;
array[0] = b4;
}
BitVector::BitVector(short arg_nb_bits, short arg_nb_bytes, byte b0, byte b1,
byte b2, byte b3, byte b4, byte b5) {
prepare_BitVector_construction(arg_nb_bits, arg_nb_bytes, 6);
array[5] = b0;
array[4] = b1;
array[3] = b2;
array[2] = b3;
array[1] = b4;
array[0] = b5;
}
BitVector::~BitVector() {
if (array)
free(array);
}
void BitVector::add_bit(byte v) {
if (!allocated)
array = (uint8_t*)malloc(1);
if (nb_bits >= (allocated << 3)) {
byte old_allocated = allocated;
// FIXME
++allocated; // Could be another formula ('<<= 1', ...)
array = (uint8_t*)realloc(array, allocated);
for (byte i = old_allocated; i < allocated; ++i)
array[i] = 0;
}
++nb_bits;
for (short i = allocated - 1; i >= 0; --i) {
byte b;
if (i > 0) {
b = !!(array[i - 1] & 0x80);
} else {
// Defensive programming:
// Normally v is 0 or 1, but I normalize it, just in case.
b = !!v;
}
array[i]= (array[i] << 1) | b;
}
}
int BitVector::get_nb_bits() const {
return nb_bits;
}
byte BitVector::get_nb_bytes() const {
return (nb_bits + 7) >> 3;
}
// Bit numbering starts at 0
byte BitVector::get_nth_bit(byte n) const {
assert(n >= 0 && n < nb_bits);
byte index = (n >> 3);
byte bitread = (1 << (n & 0x07));
return !!(array[index] & bitread);
}
// Bit numbering starts at 0
byte BitVector::get_nth_byte(byte n) const {
assert(n >= 0 && n < get_nb_bytes());
return array[n];
}
// *IMPORTANT*
// If no data got received, returns nullptr. So, you must test the
// returned value.
//
// *IMPORTANT (2)*
// The return value is malloc'd so caller must think of freeing it.
// For example:
// char *s = data_to_str_with_malloc(data);
// ...
// if (s) // DON'T FORGET (s can be null)
// free(s); // DON'T FORGET! (if non-null, s must be freed)
char* BitVector::to_str() const {
if (!get_nb_bits())
return nullptr;
byte nb_bytes = get_nb_bytes();
char *ret = (char*)malloc(nb_bytes * 3);
char tmp[3];
int j = 0;
for (int i = nb_bytes - 1; i >= 0 ; --i) {
snprintf(tmp, sizeof(tmp), "%02x", get_nth_byte(i));
ret[j] = tmp[0];
ret[j + 1] = tmp[1];
ret[j + 2] = (i > 0 ? ' ' : '\0');
j += 3;
}
assert(j <= nb_bytes * 3);
return ret;
}
short BitVector::cmp(const BitVector *p) const {
assert(p);
short cmp_nb_bits = (get_nb_bits() > p->get_nb_bits());
if (!cmp_nb_bits)
cmp_nb_bits = -(get_nb_bits() < p->get_nb_bits());
if (cmp_nb_bits)
return cmp_nb_bits;
for (int i = get_nb_bits() - 1; i >= 0; --i) {
byte v1 = get_nth_bit(i);
byte v2 = p->get_nth_bit(i);
if (v1 > v2)
return 1;
if (v1 < v2)
return -1;
}
return 0;
}
// * ******* ******************************************************************
// * Decoder ******************************************************************
// * ******* ******************************************************************
#ifdef RF433ANY_DBG_DECODER
const char *dec_id_names[] = {
"INC",
"SYN",
"TRI",
"TRN",
"MAN",
"UNK"
};
#endif
Decoder::Decoder(byte arg_convention):
next(nullptr),
pdata(new BitVector()),
convention(arg_convention),
nb_errors(0) {
tsext.initseq = 0;
tsext.first_low = 0;
tsext.first_high = 0;
tsext.first_low_ignored = 0;
tsext.last_low = 0;
}
Decoder::~Decoder() {
if (pdata)
delete pdata;
if (next)
delete next;
}
Decoder* Decoder::build_decoder(byte id, byte convention) {
switch (id) {
case RF433ANY_ID_RAW_SYNC:
return new DecoderRawSync(0);
case RF433ANY_ID_TRIBIT:
return new DecoderTriBit(convention);
case RF433ANY_ID_TRIBIT_INV:
return new DecoderTriBitInv(convention);
case RF433ANY_ID_MANCHESTER:
return new DecoderManchester(convention);
case RF433ANY_ID_RAW_UNKNOWN_CODING:
return new DecoderRawUnknownCoding();
default:
assert(false);
}
return nullptr; // Never executed
}
void Decoder::attach(Decoder *pdec) {
assert(!next);
next = pdec;
}
void Decoder::detach() {
next = nullptr;
}
void Decoder::add_data_bit(byte valbit) {
pdata->add_bit(valbit);
}
byte Decoder::get_nb_errors() const { return nb_errors; }
int Decoder::get_nb_bits() const { return pdata ? pdata->get_nb_bits() : 0; }
void Decoder::set_ts(const uint16_t& arg_initseq, const Timings& ts) {
tsext.initseq = arg_initseq;
tsext.low_short = ts.low_short;
tsext.low_long = ts.low_long;
tsext.high_short = ts.high_short;
tsext.high_long = ts.high_long;
tsext.sep = ts.sep;
if (arg_initseq && tsext.sep > arg_initseq)
tsext.sep = arg_initseq;
}
void Decoder::get_tsext(TimingsExt *p_tsext) const {
*p_tsext = tsext;
p_tsext->first_low_ignored = first_lo_ignored();
}
void Decoder::take_into_account_first_low_high(const Section *psec,
bool is_cont_of_prev_sec) {
tsext.last_low = psec->last_low;
if (is_cont_of_prev_sec)
return;
tsext.first_low = psec->first_low;
tsext.first_high = psec->first_high;
Signal e[2];
for (short i = 0; i < 2; ++i) {
uint16_t d = (i == 0 ? tsext.first_low : tsext.first_high);
uint16_t short_d = (i == 0 ? psec->ts.low_short : psec->ts.high_short);
uint16_t long_d = (i == 0 ? psec->ts.low_long : psec->ts.high_long);
Band b_short;
Band b_long;
b_short.init(short_d);
b_long.init(long_d);
// b_short.sup = (b_short.mid + b_long.mid) >> 1;
// b_long.inf = b_short.sup + 1;
bool is_short = b_short.test_value(d);
bool is_long = b_long.test_value(d);
if (is_short && !is_long) {
e[i] = Signal::SHORT;
} else if (!is_short && is_long) {
e[i] = Signal::LONG;
} else if (is_short && is_long && short_d == long_d) {
e[i] = Signal::SHORT;
} else {
e[i] = Signal::OTHER;
}
}
if (e[0] != Signal::OTHER && e[1] != Signal::OTHER) {
add_signal_step(e[0], e[1]);
tsext.first_low = 0;
tsext.first_high = 0;
}
}
void Decoder::decode_section(const Section *psec, bool is_cont_of_prev_sec) {
take_into_account_first_low_high(psec, is_cont_of_prev_sec);
byte pos_low = psec->low_bits;
byte pos_high = psec->high_bits;
while (pos_low >= 1 || pos_high >= 1) {
Signal sd_low = Signal::OTHER;
Signal sd_high = Signal::OTHER;
if (pos_low >= 1) {
--pos_low;
sd_low = ((((recorded_t)1 << pos_low) & psec->low_rec) ?
Signal::LONG : Signal::SHORT);
}
if (pos_high >= 1) {
--pos_high;
sd_high =
((((recorded_t)1 << pos_high) & psec->high_rec) ?
Signal::LONG : Signal::SHORT);
}
add_signal_step(sd_low, sd_high);
}
}
uint16_t Decoder::first_lo_ignored() const {
return 0;
}
const BitVector* Decoder::get_pdata() const {
return pdata;
}
BitVector* Decoder::take_away_data() {
if (pdata) {
BitVector *ret = pdata;
pdata = nullptr;
return ret;
} else
return nullptr;
}
#ifdef RF433ANY_DBG_DECODER
void Decoder::dbg_data(byte seq) const {
char *buf = pdata->to_str();
if (buf) {
dbgf("[%d] Received %d bits%s: %s", seq, get_nb_bits(),
(get_nb_errors() ? "(!)" : ""), buf);
free(buf);
} else {
dbgf("[%d] No data received, type = %s", seq, dec_id_names[get_id()]);
}
}
void Decoder::dbg_meta(byte disp_level) const {
if (disp_level <= 1)
return;
if (!tsext.first_low && !tsext.first_high) {
if (!tsext.high_short && !tsext.high_long) {
dbgf(" T=%s, E=%u, I=%u, S=%u, L=%u, P=%u, Y=%u, Z=%u",
dec_id_names[get_id()], nb_errors, tsext.initseq,
tsext.low_short, tsext.low_long, tsext.sep,
first_lo_ignored(), tsext.last_low);
} else {
dbgf(" T=%s, E=%u, I=%u, S(lo)=%u, L(lo)=%u, "
"S(hi)=%u, L(hi)=%u, P=%u, Y=%u, Z=%u",
dec_id_names[get_id()], nb_errors, tsext.initseq,
tsext.low_short, tsext.low_long, tsext.high_short,
tsext.high_long, tsext.sep, first_lo_ignored(),
tsext.last_low);
}
} else {
if (!tsext.high_short && !tsext.high_long) {
dbgf(" T=%s, E=%u, I=%u, S=%u, L=%u, P=%u, U=%u, "
"V=%u, Y=%u, Z=%u",
dec_id_names[get_id()], nb_errors, tsext.initseq,
tsext.low_short, tsext.low_long, tsext.sep, tsext.first_low,
tsext.first_high, first_lo_ignored(), tsext.last_low);
} else {
dbgf(" T=%s, E=%u, I=%u, S(lo)=%u, L(lo)=%u, "
"S(hi)=%u, L(hi)=%u, P=%u, U=%u, V=%u, Y=%u, Z=%u",
dec_id_names[get_id()], nb_errors, tsext.initseq,
tsext.low_short, tsext.low_long, tsext.high_short,
tsext.high_long, tsext.sep, tsext.first_low,
tsext.first_high, first_lo_ignored(), tsext.last_low);
}
}
}
void Decoder::dbg_next(byte disp_level, byte seq) const {
if (next)
next->dbg_decoder(disp_level, seq + 1);
}
#endif
// * ********************** ***************************************************
// * DecoderRawInconsistent ***************************************************
// * ********************** ***************************************************
#ifdef RF433ANY_DBG_DECODER
void DecoderRawInconsistent::dbg_decoder(byte disp_level, byte seq) const {
dbgf("[%d] Inconsistent signal", seq);
dbg_meta(disp_level);
dbg_next(disp_level, seq);
}
#endif
// * ************** ***********************************************************
// * DecoderRawSync ***********************************************************
// * ************** ***********************************************************
void DecoderRawSync::add_signal_step(Signal lo, Signal hi) {
if (!sync_shape_set) {
sync_shape = lo;
sync_shape_set = true;
}
if (lo != sync_shape) {
++nb_errors;
} else if (hi == Signal::OTHER) {
} else if (lo != hi) {
++nb_errors;
} else {
++nb_low_high;
}
}
void DecoderRawSync::add_sync(byte n) {
nb_low_high += n;
}
int DecoderRawSync::get_nb_bits() const { return nb_low_high; }
#ifdef RF433ANY_DBG_DECODER
void DecoderRawSync::dbg_decoder(byte disp_level, byte seq) const {
dbgf("[%d] Sync %d", seq, nb_low_high);
dbg_meta(disp_level);
dbg_next(disp_level, seq);
}
#endif
// * *********************** **************************************************
// * DecoderRawUnknownCoding **************************************************
// * *********************** **************************************************
void DecoderRawUnknownCoding::add_signal_step(Signal lo, Signal hi) {
if (hi == Signal::OTHER) {
unused_final_low = lo;
terminates_with_sep = true;
return;
}
for (short i = 0; i < 2; ++i) {
Signal x = (i ? hi : lo);
add_data_bit(x == Signal::SHORT ? 0 : 1);
}
}
#ifdef RF433ANY_DBG_DECODER
void DecoderRawUnknownCoding::dbg_decoder(byte disp_level, byte seq) const {
dbgf("[%d] Unknown encoding: %d signal bits", seq, pdata->get_nb_bits());
if (disp_level <= 1)
return;
int n = pdata->get_nb_bits();
assert(!(n & 1));
int sz = ((int)n * 3) / 2 + 4;
char *buf = new char[sz];
int p = 0;
for (int i = n - 1; i >= 1; i -= 2) {
byte vlo = pdata->get_nth_bit(i);
byte vhi = pdata->get_nth_bit(i - 1);
buf[p] = (vlo ? 'L' : 'S');
buf[p + 1] = (vhi ? 'L' : 'S');
buf[p + 2] = ':';
p += 3;
}
assert(p + 2 < sz);
if (terminates_with_sep) {
if (unused_final_low == Signal::SHORT)
buf[p] = 'S';
else
buf[p] = 'L';
buf[p + 1] = 'P';
buf[p + 2] = '\0';
} else {
if (!p)
buf[p] = '\0';
else
buf[p - 1] = '\0';
}
Serial.print(" Signal: ");
Serial.print(buf);