-
Notifications
You must be signed in to change notification settings - Fork 0
/
signalDecoder4.cpp
2239 lines (1971 loc) · 60 KB
/
signalDecoder4.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
/*
* Pattern Decoder Library V4
* Library to decode radio signals based on patternd detection
* 2014-2015 N.Butzek, S.Butzek
* 2015-2017 S.Butzek
* 2020 Ralf9
* This library contains classes to perform decoding of digital signals
* typical for home automation. The focus for the moment is on different sensors
* like weather sensors (temperature, humidity Logilink, TCM, Oregon Scientific, ...),
* remote controlled power switches (Intertechno, TCM, ARCtech, ...) which use
* encoder chips like PT2262 and EV1527-type and manchester encoder to send
* information in the 433MHz or 868 Mhz Band.
*
* The classes in this library follow the approach to detect a recurring pattern in the
* recived signal. For Manchester there is a class which decodes the signal.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "signalDecoder4.h"
#ifdef DEBUGGLEICH
//Helper function to check buffer for bad data
bool SignalDetectorClass::checkMBuffer()
{
for (uint16_t i = 0; i < messageLen-1; i++)
{
if ( (pattern[message[i]] ^ pattern[message[i+1]]) > 0)
//if (message[i] == message[i+1])
{
return false;
}
}
return true;
}
#endif
void SignalDetectorClass::bufferMove(const uint16_t start)
{
m_truncated = false;
if (start == 0 || messageLen == 0) {
//MSG_PRINTLN(F("buffMove start||msgLen=0"));
//printOut();
return;
}
if (start > messageLen - 1) {
if (start > messageLen) {
DBG_PRINT(F(" buffMove overflow!"));
DBG_PRINT(F(" start=")); MSG_PRINTLN(start);
//printOut();
}
reset();
}
else if (message.moveLeft(start))
{
m_truncated = true;
//messageLen = messageLen - start;
messageLen = message.valcount;
if (messageLen > 0) {
//last = &pattern[message[messageLen - 1]]; //Eventuell wird last auf einen nicht mehr vorhandenen Wert gesetzt, da der Puffer komplett gelöscht wurde
} else {
last = NULL;
}
} else {
DBG_PRINT(" moveErr "); DBG_PRINTLN(start)
//printOut();
}
#if DEBUGGLEICH > 1
//if (bMoveFlag == 1 || !checkMBuffer())
if (!checkMBuffer())
{
bMoveFlag = 0;
MSG_PRINT(F("after buffermove ->"));
MSG_PRINT(F(" start=")); MSG_PRINT(start);
MSG_PRINT(F(" bytecnt=")); MSG_PRINT(message.bytecount);
MSG_PRINT(F(" valcnt=")); MSG_PRINT(message.valcount);
MSG_PRINT(F(" bcnt=")); MSG_PRINT(message.bcnt);
//MSG_PRINT(F(" mTrunc=")); MSG_PRINT(m_truncated);
MSG_PRINTLN(F(" wrong Data in Buffer"));
printOut();
}
#endif
}
inline void SignalDetectorClass::addData(const uint8_t value)
{
//message += value;
/*if (message.valcount >= 254)
{
DBG_PRINTLN(""); DBG_PRINT(__FUNCTION__); DBG_PRINT(" msglen: "); DBG_PRINT(messageLen);
}*/
//uint8_t nb;
#if DEBUGGLEICH > 1
bMoveFlag = 0;
//if (messageLen > 0 && value == message[messageLen-1]) { // && state > 0) {
if (messageLen > 0 && (pattern[message[messageLen-1]] ^ pattern[value]) > 0) {
bMoveFlag = 1;
MSG_PRINT(F("addDataGleich ->"));
MSG_PRINT(F(" val=")); MSG_PRINT(value);
printOut();
}
#endif
if (message.addValue(value))
{
messageLen++;
#if DEBUGGLEICH > 1
if (!checkMBuffer())
{
bMoveFlag = 1;
MSG_PRINT(F("addData->"));
MSG_PRINT(F("val=")); MSG_PRINTLN(value);
printOut();
}
#endif
}
else {
MSG_PRINT(F("val=")); MSG_PRINT(value);
MSG_PRINT(F(" bytecnt=")); MSG_PRINT(message.bytecount);
MSG_PRINTLN(F(" addData overflow!!"));
//printOut();
}
firstLast = *first; // zum debuggen
}
inline void SignalDetectorClass::addPattern()
{
pattern[pattern_pos] = *first; //Store pulse in pattern array
pattern_pos++;
}
inline void SignalDetectorClass::updPattern( const uint8_t ppos)
{
pattern[ppos] = (long(pattern[ppos]) + *first) / 2; // Moving average
}
inline void SignalDetectorClass::doDetect()
{
//printOut();
bool valid;
valid = (messageLen == 0 || last == NULL || (*first ^ *last) < 0); // true if a and b have opposite signs
valid &= (*first > cMaxPulse); // if low maxPulse detected, start processMessage()
//if (abs(*first) < 90) {
// DBG_PRINT("first<90 ");DBG_PRINTLN(*first);
//}
int8_t fidx = 0;
if (!valid) { // Nachrichtenende erkannt -> alles bis zum Nachrichtenende wird ausgegeben
for (uint8_t n = 70; n > 0; --n) { // maxMsgSize 1500 / minMessageSize 26 = 57
// Try output
processMessage(0);
if (messageLen == 0) {
last = NULL;
break;
}
}
if (messageLen > 0) reset(); // nur zur Sicherheit, duerfte eigentlich nie vorkommen
MsMoveCount = 0;
MuMoveCount = 0;
addPattern();
}
else { // valid
if (messageLen >= maxMsgSize) {
processMessage(1); // message Puffer voll aber kein message Ende
if (mcDetected == false) {
calcHisto();
}
}
else if (messageLen == minMessageLen) {
state = detecting; // Set state to detecting, because we have more than minMessageLen data gathered, so this is no noise
if (_rssiCallback != NULL)
rssiValue = _rssiCallback();
}
fidx = findpatt(*first);
if (fidx >= 0) {
// Upd pattern
updPattern(fidx);
}
else if (mcDetected) {
processMessage(2);
}
else {
// Add pattern
if (patternLen == cMaxNumPattern)
{
//calcHisto();
//bool gr2Flag = false;
if (histo[pattern_pos] > 2 && state > 0)
{
//gr2Flag = true;
//DBG_PRINTLN(F("addP_histop>2")); // pattern buffer full after proccessMessage
//printOut();
bool saveMsgSuccess = false;
for (uint8_t n = 70; n > 0; --n) {
if (printMsgSuccess) {
saveMsgSuccess = true;
printMsgSuccess = false;
}
processMessage(2); // Patternpuffer overflow
if (messageLen < minMessageLen || printMsgSuccess == false) {
break;
}
}
MsMoveCount = 0;
MuMoveCount = 0;
printMsgSuccess = saveMsgSuccess;
//printOut();
calcHisto();
}
if (messageLen > 0 && histo[pattern_pos] > 0) {
for (uint16_t i = messageLen; i > 0; --i)
{
if (message[i-1] == pattern_pos) // Finde den letzten Verweis im Array auf den Index der gleich ueberschrieben wird
{
/* // Test ob es was bringen wuerde, wenn der letzte Wert in message[] nicht ueberschrieben wuerde -> keine Vorteile erkennbar.
if (i == messageLen - 1) {
printOut();
} */
bufferMove(i); // i um eins erhoehen, damit zukuenftigen Berechnungen darauf aufbauen koennen
calcHisto();
break;
}
if (i == 1 && messageLen > 253)
{
//DBG_PRINT(millis());
DBG_PRINTLN(F(" mb nm b proc ")); // message buffer not moved before proccessMessage
}
}
/*if (gr2Flag) {
DBG_PRINTLN(F("addP_histop>2 nach buffmove")); // pattern buffer full after proccessMessage
printOut();
}*/
}
}
fidx = pattern_pos;
addPattern();
if (pattern_pos == cMaxNumPattern)
{
pattern_pos = 0; // Wenn der Positions Index am Ende angelegt ist, gehts wieder bei 0 los und wir ueberschreiben alte pattern
patternLen = cMaxNumPattern;
mcDetected = false; // When changing a pattern, we need to redetect a manchester signal and we are not in a buffer full mode scenario
}
if (pattern_pos > patternLen) patternLen = pattern_pos;
}
}
// Add data to buffer
addData(fidx);
histo[fidx]++;
#if DEBUGDETECT > 3
DBG_PRINT("Pulse: "); DBG_PRINT(*first);
DBG_PRINT(", "); DBG_PRINT(*last);
DBG_PRINT(", TOL: "); DBG_PRINT(tol); DBG_PRINT(", fidx: "); DBG_PRINT(fidx);
DBG_PRINT(", Vld: "); DBG_PRINT(valid);
DBG_PRINT(", pattPos: "); DBG_PRINT(pattern_pos);
DBG_PRINT(", mLen: "); DBG_PRINT(messageLen);
DBG_PRINT(", BC:"); DBG_PRINT(message.bytecount);
DBG_PRINT(", vcnt:"); DBG_PRINT(message.valcount);
DBG_PRINTLN(" ");
#endif
}
bool SignalDetectorClass::decode(const int16_t * pulse)
{
success = false;
if (messageLen > 0) {
last = &pattern[message[messageLen - 1]];
lastPulse = *first; // zum debuggen
} else {
last = NULL;
}
*first = *pulse;
doDetect();
return success;
}
bool SignalDetectorClass::compress_pattern()
{
bool ret = false;
calcHisto();
/*if (!checkMBuffer())
{
MSG_PRINTLN(F("befCompP->wrongDat inBuf"));
printOut();
}*/
for (uint8_t idx = 0; idx<patternLen-1; idx++)
{
if (histo[idx] == 0)
continue;
for (uint8_t idx2 = idx + 1; idx2<patternLen; idx2++)
{
if (histo[idx2] == 0 || (pattern[idx] ^ pattern[idx2]) >> 15)
continue;
const int16_t tol = int16_t(((abs(pattern[idx2])*tolFact) + (abs(pattern[idx])*tolFact)) / 2);
if (inTol(pattern[idx2], pattern[idx], tol)) // Pattern are very equal, so we can combine them
{
#if DEBUGDoDETECT >3
DBG_PRINT(" compr:idx= "); DBG_PRINT(idx); DBG_PRINT("/"); DBG_PRINT(pattern[idx]);
DBG_PRINT(" idx2= "); DBG_PRINT(idx2); DBG_PRINT("/"); DBG_PRINT(pattern[idx2]);
DBG_PRINT(" tol="); DBG_PRINT(tol);
#endif
uint16_t change_count = 0;
// Change val -> ref_val in message array
for (uint16_t i = 0; i<messageLen && change_count < histo[idx2]; i++)
{
if (message[i] == idx2)
{
message.changeValue(i, idx);
change_count++;
}
}
#if DEBUGDETECT>3
DBG_PRINT("compr: "); DBG_PRINT(idx2); DBG_PRINT("->"); DBG_PRINT(idx); DBG_PRINT(";");
DBG_PRINT(histo[idx2]); DBG_PRINT("*"); DBG_PRINT(pattern[idx2]);
DBG_PRINT("->");
DBG_PRINT(histo[idx]); DBG_PRINT("*"); DBG_PRINT(pattern[idx]);
#endif // DEBUGDETECT
int16_t sum = histo[idx] + histo[idx2];
//int lPatternIdx = pattern[idx];
pattern[idx] = ((long(pattern[idx]) * histo[idx]) + (long(pattern[idx2]) * histo[idx2])) / sum;
//if (abs(pattern[idx]) < 90) {
// DBG_PRINT("upfirst<90 ");DBG_PRINT(idx);DBG_PRINT(" ");DBG_PRINT(idx2);DBG_PRINT(" ");DBG_PRINTLN(lPatternIdx);
// printOut();
//}
histo[idx] += histo[idx2];
pattern[idx2] = histo[idx2]= 0;
ret = true;
#if DEBUGDETECT >3
DBG_PRINT(" idx:"); DBG_PRINT(pattern[idx]);
DBG_PRINT(" idx2:"); DBG_PRINT(pattern[idx2]);
DBG_PRINTLN(";");
#endif // DEBUGDETECT
}
}
}
#if DEBUGDoDETECT >3
if (ret==true)
DBG_PRINTLN(" ");
#endif
#ifdef DEBUGGLEICH
if (!checkMBuffer())
{
MSG_PRINTLN(F("aftCompP->wrongD InBuf"));
printOut();
}
#endif
return ret;
}
void SignalDetectorClass::processMessage(const uint8_t p_valid)
{
//p_valid: 0 - Nachrichtenende erkannt
// 1 - message Puffer voll aber kein message Ende
// 2 - Patternpuffer overflow
//yield();
if (p_valid > 0) {
m_truncated = true;
} else { // nicht valid (message Ende) -> reset wenn kleiner minMessageLen
m_truncated = false;
}
/*DBG_PRINT("msgRec:");
DBG_PRINT(*first);
DBG_PRINT(" ");
DBG_PRINTLN(*last);*/
if (mcDetected == true || messageLen >= minMessageLen) {
success = false;
uint8_t n = 0;
m_overflow = (messageLen == maxMsgSize) ? true : false;
#if DEBUGDETECT >= 1
DBG_PRINTLN("msgRec:");
#endif
state = searching;
if (mcDetected == false) {
//if (MsMoveCount == 0 && MuMoveCount == 0) { // bei Wiederholungen wird kein compress_pattern benoetigt
//if (MuOverflCount == 0) { // bei Overflow Nachrichten wird kein compress_pattern benoetigt
compress_pattern();
//}
if (MsMoveCount > 0) {
mstart = 0;
while (mstart < 10) {
if (message[mstart + 1] == sync && message[mstart] == clock) { // nach sync und clock von der 1.Nachricht suchen
state = syncfound; // sync und clock gefunden -> es muss nicht erneut nach clock und sync gesucht werden
break;
}
mstart++;
}
if (state == searching) { // die sync und clock Werte stimmen nicht mit der 1.Nachricht ueberein
MsMoveCount = 0;
calcHisto();
}
} else if (MuMoveCount > 0) {
calcHisto();
}
if (state == searching) {
getClock();
}
if (state == clockfound && MSenabled && mcRepeat == false) getSync(); // wenn MC Wiederholungen ausgegeben werden, wird die MS Decodierung uebersprungen
}
else {
calcHisto();
}
#if DEBUGDECODE >1
DBG_PRINT("msgRec state="); DBG_PRINTLN(state)
#endif
#if DEBUGDETECT >= 1
printOut();
#endif
if (state == syncfound && messageLen >= minMessageLen) // Messages mit clock / Sync Verhaeltnis pruefen
{
#if DEBUGDECODE >1
MSG_PRINTLN(" MScheck: ");
//printOut();
#endif
// Setup of some protocol identifiers, should be retrieved via fhem in future
mend = mstart + 12;
uint8_t mstartNeu = mstart + 10;
uint8_t mstartAlt = mstart;
bool m_endfound = false;
while (mstartNeu > mstart) // testen ob innerhalb 10 Zeichen nach dem sync ein weiterer sync folgt, falls ja diesen als neuen mstart verwenden
{
if (message[mstartNeu + 1] == sync) {
if (message[mstartNeu] == clock) {
while (mstartNeu < mstartNeu+40 && mstartNeu < (messageLen - minMessageLen)) // alle folgenden sync ueberspringen
{
if (message[mstartNeu + 2] != clock || message[mstartNeu + 3] != sync) { // folgt kein weiterer sync?
#ifdef DEBUGDECODE
DBG_PRINT(F("MStart:")); DBG_PRINT(mstart);
DBG_PRINT(F(" new MStart:")); DBG_PRINTLN(mstartNeu);
#endif
mstart = mstartNeu; // weiterer sync -> neuer mstart
mend = mstart + 2;
break;
}
mstartNeu += 2;
}
break;
}
}
mstartNeu -= 2;
}
while (mend < messageLen - 1)
{
if (message[mend + 1] == sync && message[mend] == clock) { // Ende der MS-Nachricht gefunden
mend -= 1; // Previus signal is last from message
m_endfound = true;
break;
}
mend += 2;
if ((mend - mstart) > defMsMaxMsgSize) {
m_overflow = true;
break;
}
}
//if (mend > messageLen-1 || (!m_endfound && (mend - mstart) <= defMsMaxMsgSize)) mend = messageLen - 1; // Reduce mend if we are behind messageLen-1
if (mend > messageLen-1) mend = messageLen - 1; // Reduce mend if we are behind messageLen-1
calcHisto(mstart, mend); // Recalc histogram due to shortened message
//if (message[messageLen-1] == 7 || mstart > 10) {
//if (mstart > 30) {
// DBG_PRINT("MStart>30 valid="); DBG_PRINTLN(p_valid,DEC);
// printOut();
//}
#ifdef DEBUGDECODE
DBG_PRINT("Index: ");
DBG_PRINT(" MStart: "); DBG_PRINT(mstart);
DBG_PRINT(" SYNC: "); DBG_PRINT(sync);
DBG_PRINT(", CP: "); DBG_PRINT(clock);
DBG_PRINT(" - MEnd: "); DBG_PRINT(mend);
DBG_PRINT(F(" msglen=")); DBG_PRINTLN(messageLen);
#endif // DEBUGDECODE
if ((m_endfound && (mend - mstart) >= minMessageLen) || (!m_endfound && (messageLen - mstart) >= minMessageLen))
{
if (m_endfound || (!m_endfound && (messageLen - mstart) < defMsMaxMsgSize && p_valid != 2)) // nicht ausgeben, wenn kein Ende gefunden und patternpuffer overflow
{
#if DEBUGDECODE >1
MSG_PRINTLN(F("Filter Match: "));;
#endif
//preamble = "";
//postamble = "";
if (MsMoveCount < 2) { // die ersten beiden MS Nachrichten werden auf jeden Fall ausgegeben
MsEqSkip = 0;
}
if (MsMoveCount < MsMoveCountmax && MsEqSkip == 0) {
/* Output raw message Data */
if (MredEnabled) {
int16_t patternInt;
uint8_t patternLow;
uint8_t patternIdx;
MSG_PRINT(MSG_START); MSG_PRINT("Ms"); MSG_PRINT(SERIAL_DELIMITER);
for (uint8_t idx = 0; idx < patternLen; idx++)
{
if (pattern[idx] == 0 || histo[idx] == 0) continue;
patternIdx = idx;
patternInt = pattern[idx];
if (patternInt < 0) {
patternIdx = idx | B10100000; // Bit5 = 1 (Vorzeichen negativ)
patternInt = -patternInt;
}
else {
patternIdx = idx | B10000000; // Bit5 = 0 (Vorzeichen positiv)
}
patternLow = lowByte(patternInt);
if (bitRead(patternLow,7) == 0) {
bitSet(patternLow,7);
}
else {
bitSet(patternIdx, 4); // wenn bei patternLow Bit7 gesetzt ist, dann bei patternIdx Bit4 = 1
}
MSG_WRITE(patternIdx);
MSG_WRITE(patternLow);
MSG_WRITE(highByte(patternInt) | B10000000);
MSG_PRINT(SERIAL_DELIMITER);
}
if ((mend & 1) == 1) { // ungerade
MSG_PRINT("D");
}
else {
MSG_PRINT("d");
}
if ((mstart & 1) == 1) { // ungerade
mstart--;
message.getByte(mstart/2,&n);
n = (n & 15) | 128; // high nibble = 8 als Kennzeichen fuer ungeraden mstart
MSG_WRITE(n);
mstart += 2;
}
for (uint16_t i = mstart; i <= mend; i=i+2) {
message.getByte(i/2,&n);
MSG_WRITE(n);
}
MSG_PRINT(SERIAL_DELIMITER);
MSG_PRINT("C"); MSG_PRINT(clock, HEX); MSG_PRINT(SERIAL_DELIMITER);
MSG_PRINT("S"); MSG_PRINT(sync, HEX); MSG_PRINT(SERIAL_DELIMITER);
#ifdef CMP_CC1101
MSG_PRINT("R"); MSG_PRINT(rssiValue, HEX); MSG_PRINT(SERIAL_DELIMITER);
#endif
}
else {
char msbuf[15];
uint8_t idx;
if (MsMoveCount == 0) {
msEqCnt = 0;
for (idx = 0; idx < patternLen; idx++)
{
ms0pattern[idx] = 0;
}
}
else {
if (msEqCnt == 0) {
msEqCnt = 1;
}
for (idx = 0; idx < patternLen; idx++)
{
if (pattern[idx] == 0 || histo[idx] == 0) continue;
if (pattern[idx] != ms0pattern[idx]) { // pattern vergleichen
msEqCnt = 0;
break;
}
}
//MSG_PRINT(msEqCnt);
}
if (msEqCnt == 0) { // wenn pattern gleich wie vorherige MS Nachricht, dann ueberspringen
buf[0] = MSG_START;
buf[1] = 0;
strcat(buf,"MS;");
for (idx = 0; idx < patternLen; idx++)
{
if (pattern[idx] == 0 || histo[idx] == 0) continue;
//if (MsMoveCount == 0) {
ms0pattern[idx] = pattern[idx]; // pattern merken
//}
n = sprintf(msbuf, "P%u=%i;", idx,pattern[idx]);
strcat(buf,msbuf);
}
strcat(buf,"D=");
n = strlen(buf);
idxDat = n; // Beginn der Daten merken;
//MSG_PRINT(buf);
}
char msg;
n = idxDat;
for (uint16_t i = mstart; i <= mend; ++i)
{
msg = message[i] + '0';
if (msEqCnt > 0 && msg != buf[n]) { // Daten vergleichen
msEqCnt = 0; // Daten sind nicht gleich
}
if (msEqCnt == 0) {
buf[n] = message[i] + '0';
}
n++;
}
if (msEqCnt != 0) { // MS Nachrichten sind gleich
if (msEqCnt == 2 && MSeqEnabled) {
MsEqSkip =1;
}
msEqCnt++;
}
if (MsEqSkip == 0) {
MSG_WRITE(buf,n);
n = sprintf(msbuf, ";CP=%x;SP=%x;", clock, sync); // ClockPulse & SyncPulse
MSG_PRINT(msbuf);
#ifdef CMP_CC1101
n = sprintf(msbuf, "R=%u;", rssiValue); // Signal Level (RSSI)
MSG_PRINT(msbuf);
#endif
if (msEqCnt > 0) {
MSG_PRINT(F("Q;"));
//MSG_PRINT(msEqCnt);
//MSG_PRINT(F(";"));
}
}
}
if (m_overflow && MsEqSkip == 0) {
MSG_PRINT("O"); MSG_PRINT(SERIAL_DELIMITER);
}
if (MdebEnabled && MsEqSkip == 0) {
if (p_valid == 0) { // Nachrichtenende erkannt
MSG_PRINT("e"); MSG_PRINT(SERIAL_DELIMITER);
} else if (p_valid == 2) { // Patternpuffer overflow
MSG_PRINT("p"); MSG_PRINT(SERIAL_DELIMITER);
}
if (mstartAlt > 1) {
MSG_PRINT("b"); MSG_PRINT(mstartAlt); MSG_PRINT(SERIAL_DELIMITER); // Position von ersten gefundenen Sync
}
if (mstart > mstartAlt) {
MSG_PRINT(F("s")); MSG_PRINT(mstart - mstartAlt); MSG_PRINT(SERIAL_DELIMITER); // Anzahl der uebersprungenen Sync
}
}
}
m_truncated = false;
if ((m_endfound || MsMoveCount > 0)) {
if (MdebEnabled && MsMoveCount < MsMoveCountmax && MsEqSkip == 0) {
MSG_PRINT("m"); MSG_PRINT(MsMoveCount); MSG_PRINT(SERIAL_DELIMITER);
}
MsMoveCount++;
}
if (m_endfound) {
if ((p_valid > 0 || (p_valid == 0 && (messageLen - mend) >= minMessageLen)) && MsMoveCount > 0) { // wenn Nachrichtenende erkannt wurde, dann muss der Rest laenger als minMessageLen sein
//MSG_PRINT(F("MS move. messageLen ")); MSG_PRINT(messageLen); MSG_PRINT(" "); MSG_PRINTLN(MsMoveCount)
bufferMove(mend+1);
//MSG_PRINT(F("MS move. messageLen ")); MSG_PRINTLN(messageLen);
mstart = 0;
}
}
if (MsMoveCount <= MsMoveCountmax && MsEqSkip == 0) {
MSG_PRINT(MSG_END);
MSG_PRINT("\n");
}
printMsgSuccess = true;
//success = true;
}
else if (m_endfound == false && mstart > 0) // && mend + 1 >= maxMsgSize) // Start found, but no end. We remove everything bevore start and hope to find the end later
{
if (p_valid == 0) { // wenn ein Nachrichtenende erkannt wurde, kann alles geloescht werden
m_truncated = false;
#if DEBUGDECODE >1
DBG_PRINT(F(" MSmoveMsg,noEnd->reset "));
#endif
}
else {
#if DEBUGDECODE >1
DBG_PRINT(F(" MSmoveMsg,noEnd "));
#endif
bufferMove(mstart);
mstart = 0;
//m_truncated = true; // Flag that we truncated the message array and want to receiver some more data
}
}
else if (m_endfound && mend < maxMsgSize) { // Start and end found, but end is not at end of buffer, so we remove only what was checked
// mir ist nicht klar in welchen Faellen dies aufgerufen wird
#if DEBUGDECODE >1
DBG_PRINTLN(F(" MSmoveMsg "));
#endif
bufferMove(mend+1);
mstart = 0;
//m_truncated = true; // Flag that we truncated the message array and want to receiver some more data
//success = true; // don't process other message types
}
else {
#ifdef DEBUGDECODE
MSG_PRINTLN(F(" Buffer overflow, flushing message array"));
#endif
//MSG_PRINT(MSG_START);
//MSG_PRINT("Buffer overflow while processing signal");
//MSG_PRINT(MSG_END);
if (p_valid != 2) {
reset(); // Our Messagebuffer is not big enough, no chance to get complete Message
}
//success = true; // don't process other message types
}
success = true;
}
else { // m_endfound && (mend - mstart) < minMessageLen) -> weiter mit MU message verarbeitung
success = false;
}
}
if (success == false && (MUenabled || MCenabled)) {
#if DEBUGDECODE >1
DBG_PRINT(" check:");
//printOut();
#endif
// Message has a clock puls, but no sync. Try to decode this
//preamble = "";
//postamble = "";
if (MCenabled && state != syncfound)
{
//DBG_PRINT(" mc: ");
static ManchesterpatternDecoder mcdecoder(this); // Init Manchester Decoder class
if (mcDetected == false)
{
mcdecoder.reset();
mcdecoder.setMinBitLen(mcMinBitLen);
}
#if DEBUGDETECT>3
MSG_PRINT("vcnt: "); MSG_PRINTLN(mcdecoder.ManchesterBits.valcount);
#endif
#if DEBUGDECODE > 2
DBG_PRINTLN("");
#endif
if (mcDetected || mcdecoder.isManchester()) // Check if valid manchester pattern and try to decode
{
#if MCDEBUGDECODE > 1
MSG_PRINT(MSG_START);
MSG_PRINT("DMC");
MSG_PRINT(SERIAL_DELIMITER);
for (uint8_t idx = 0; idx < patternLen; idx++)
{
if (idx > 4 && histo[idx] == 0) continue;
MSG_PRINT('P'); MSG_PRINT(idx); MSG_PRINT('='); MSG_PRINT(pattern[idx]); MSG_PRINT(SERIAL_DELIMITER);
}
MSG_PRINT("D=");
//mend = min(mend, messageLen); // Workaround if mend=255
for (uint16_t i = 0; i < messageLen; ++i)
{
if (i == mstart || i == mend) {
MSG_PRINT("_");
}
MSG_PRINT(message[i]);
}
MSG_PRINT(SERIAL_DELIMITER);
if (m_overflow) {
MSG_PRINT("O"); MSG_PRINT(SERIAL_DELIMITER);
}
if (MdebEnabled) {
if (p_valid == 0) { // Nachrichtenende erkannt
MSG_PRINT("e"); MSG_PRINT(SERIAL_DELIMITER);
} else if (p_valid == 2) { // Patternpuffer overflow
MSG_PRINT("p"); MSG_PRINT(SERIAL_DELIMITER);
}
if (mcValid == false) {
MSG_PRINT("i"); MSG_PRINT(SERIAL_DELIMITER);
}
}
MSG_PRINTLN(MSG_END);
#endif
if (mcdecoder.doDecode())
{
MSG_PRINT(MSG_START);
n = sprintf(buf, "MC;LL=%i;LH=%i;SL=%i;SH=%i;D=", pattern[mcdecoder.longlow], pattern[mcdecoder.longhigh], pattern[mcdecoder.shortlow], pattern[mcdecoder.shorthigh]);
MSG_PRINT(buf);
/*MSG_PRINT("MC");
MSG_PRINT(SERIAL_DELIMITER);
MSG_PRINT("LL="); MSG_PRINT(pattern[mcdecoder.longlow]); MSG_PRINT(SERIAL_DELIMITER);
MSG_PRINT("LH="); MSG_PRINT(pattern[mcdecoder.longhigh]); MSG_PRINT(SERIAL_DELIMITER);
MSG_PRINT("SL="); MSG_PRINT(pattern[mcdecoder.shortlow]); MSG_PRINT(SERIAL_DELIMITER);
MSG_PRINT("SH="); MSG_PRINT(pattern[mcdecoder.shorthigh]); MSG_PRINT(SERIAL_DELIMITER);
MSG_PRINT("D=");*/
mcdecoder.printBufMessageHexStr();
n = sprintf(buf, ";C=%u;L=%u;", mcdecoder.clock, mcdecoder.ManchesterBits.valcount);
MSG_PRINT(buf);
/*MSG_PRINT(SERIAL_DELIMITER);
MSG_PRINT("C="); MSG_PRINT(mcdecoder.clock); MSG_PRINT(SERIAL_DELIMITER);
MSG_PRINT("L="); MSG_PRINT(mcdecoder.ManchesterBits.valcount); MSG_PRINT(SERIAL_DELIMITER);*/
#ifdef CMP_CC1101
MSG_PRINT("R="); MSG_PRINT(rssiValue); MSG_PRINT(SERIAL_DELIMITER); // Signal Level (RSSI)
#endif
if (MdebEnabled) {
if (mcValid == false) {
MSG_PRINT("i"); MSG_PRINT(SERIAL_DELIMITER);
}
if (mcdecoder.mc_sync_pos > 0) {
MSG_PRINT("s"); MSG_PRINT(mcdecoder.mc_sync_pos); MSG_PRINT(SERIAL_DELIMITER);
MSG_PRINT("b"); MSG_PRINT(mstart); MSG_PRINT(SERIAL_DELIMITER);
#ifdef MCDEBUGDECODE
uint8_t ims = mstart;
if (mstart > 3) {
ims = 3;
} else {
ims = mstart;
}
MSG_PRINT("s");
uint8_t ic;
uint8_t ip;
for (ic = mstart-ims; ic < mstart+30; ic++) {
if (ic == mstart) {
MSG_PRINT("_");
}
ip = message[ic];
if (ip == mcdecoder.longlow) {
MSG_PRINT("l");
} else if (ip == mcdecoder.longhigh) {
MSG_PRINT("L");
} else if (ip == mcdecoder.shortlow) {
MSG_PRINT("s");
} else if (ip == mcdecoder.shorthigh) {
MSG_PRINT("S");
} else {
MSG_PRINT(ip);
}
}
MSG_PRINT(SERIAL_DELIMITER);
#endif
}
}
if (m_overflow) {
MSG_PRINT("O"); MSG_PRINT(SERIAL_DELIMITER);
}
mcDetected = false;
success = true;
printMsgSuccess = true;
m_truncated = false;
if (p_valid == 1 || (messageLen - mend) >= minMessageLen) { // wenn Nachrichtenende erkannt wurde, dann muss der Rest laenger als minMessageLen sein
//printOut();
//MSG_PRINT("p_valid=");
//MSG_PRINTLN(p_valid);
bufferMove(mend-1);
mstart = 0;
mcRepeat = true;
if (MdebEnabled) {
MSG_PRINT("w"); MSG_PRINT(SERIAL_DELIMITER);
}
}
MSG_PRINT(MSG_END);
MSG_PRINT("\n");
}
else if (mcDetected == true) { // && m_truncated == true) {
/* DBG_PRINT("p_valid=");
DBG_PRINT(p_valid);
DBG_PRINT(" m_trunc=");
DBG_PRINT(m_truncated);
DBG_PRINT("mlen=");
DBG_PRINTLN(messageLen);
*/
success = true; // Prevents MU Processing
}
}
}
if (MUenabled && (state == clockfound || state == syncfound) && success == false && messageLen >= minMessageLen) {
#if DEBUGDECODE > 1
DBG_PRINT(" MU found: ");
#endif // DEBUGDECODE
bool m_endfound = false;
bool isMuRepeat = false;
mstart = 0;
calcHisto();
if (MuSplitThresh) {
isMuRepeat = isMuMessageRepeat();
}
#ifdef DEBUGMUREPEAT
DBG_PRINTLN(isMuRepeat, DEC);
#endif
if (isMuRepeat) {
mend = minMessageLen;
if (mend <= messageLen) {
for (uint16_t i = mend; i < messageLen-1; ++i) {
if (abs(pattern[message[i]]) >= MuSplitThresh) {
mend = i;
m_endfound = true;
//MSG_PRINT(F("MUend found="));
//MSG_PRINTLN(mend);
//printOut();
break;
}
}
}
//printOut();
}
if (!m_endfound) {
mend = messageLen;
}
else {
calcHisto(0, mend); // Recalc histogram due to shortened message
}
uint8_t Omend = 0;
if (MredEnabled) {
int16_t patternInt;
uint8_t patternLow;
uint8_t patternIdx;
//if (MuOverflCount == 0) {
MSG_PRINT(MSG_START); MSG_PRINT("Mu"); MSG_PRINT(SERIAL_DELIMITER);
for (uint8_t idx = 0; idx < patternLen; idx++)
{
if (pattern[idx] == 0 || histo[idx] == 0) continue;
patternIdx = idx;
patternInt = pattern[idx];