-
Notifications
You must be signed in to change notification settings - Fork 11
/
DecodeIR.cpp
executable file
·6055 lines (5716 loc) · 182 KB
/
DecodeIR.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
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <set>
#include <iostream>
#include "DecodeIR.h"
#include<assert.h>
#define __cdecl
#define _ASSERT assert
#define DWORD int
#define LPVOID void *
#define strnicmp strncasecmp
static char const version_cstr[]="2.45";
Signal::Signal(
unsigned int* p_Context,
int* p_Bursts,
int n_Freq,
int n_Single,
int n_Repeat,
char* p_Protocol,
int* p_Device,
int* p_SubDevice,
int* p_OBC,
int* p_Hex,
char* p_Misc,
char* p_Error) :
pContext(p_Context),
nFreq_in(n_Freq),
nSingle(n_Single),
nRepeat(n_Repeat),
pProtocol(p_Protocol),
pDevice(p_Device),
pSubDevice(p_SubDevice),
pOBC(p_OBC),
pHex(p_Hex),
pMisc(p_Misc),
pError(p_Error),
pDuration(NULL)
{
// GD 2009 Start
nFrameCount = 0;
nDittos = 0;
nNote_out = -1; // unset
nAuxNote_out = -1; // unset
nNonSpurious = 0; // Only valid when >0
bXMPHalfValid = false;
AminoToggleDetected = false;
ZaptorToggleDetected = false;
bInitLeadIn = false;
// Don't output frame count if repeat or extra frames present. If neither are present
// then *pDevice = -2 can still be used to suppress frame count, as *pDevice is only
// taken as carrying nExtra if nRepeat is nonzero.
bSuppressCount = nRepeat > 0 || *pDevice < -1;
// Older versions of IR.exe add extra count to single count when no repeat frames.
// This is retained for compatibility, so a value of *pDevice , -1 is ignored unless
// nRepeat > 0.
nExtra = nRepeat > 0 && *pDevice < -1 ? - *pDevice : 0;
// Additional info can be provided or returned in Context[] if its length is carried in
// pSubDevice and is > 2. The maximum supported length is 18, due to the form of the
// returned handshake. See decode2() for details of the use of the additional fields.
nContextLength = *pSubDevice < -1 ? - *pSubDevice : 2;
// The only additional incoming data in an enhanced context is a pointer, potentially 64-bit,
// to an array of carrier cycle counts corresponding to the timings in p_Bursts[]. This is
// only used by IRScope, which uses nRepeat=nExtra=0, so it does not need extending in the
// way that pDuration[] is extended from p_Bursts.
pCounts = nContextLength > 7 ? reinterpret_cast<short int*>(*(int64_t *)(&pContext[6])) : NULL;
// GD 2009 End
int nSR = (nRepeat+nSingle)*2;
// pDuration = new float[nRepeat*2 + nSR + 4];
int nSRE = nSR + nExtra*2; // GD 2009
pDuration = new float[nRepeat*2 + nSRE + 4]; // GD 2009
for (int ndx=0; ndx<nSR; ++ndx)
{
pDuration[ndx] = p_Bursts[ndx];
}
memcpy(pDuration+nSR, pDuration+2*nSingle, nRepeat*2*sizeof(float));
// GD 2009 Start
for (int ndx=nSR; ndx<nSRE; ++ndx)
{
pDuration[ndx+2*nRepeat] = p_Bursts[ndx];
}
// GD 2009 End
// memset(pDuration+nSR+2*nRepeat, 0, 4*sizeof(float));
memset(pDuration+nSRE+2*nRepeat, 0, 4*sizeof(float)); // GD 2009
// GD 2009 Start. If nExtra > 0 treat all bursts as single
if ( nExtra > 0 )
{
nNonSpurious = nSingle + 2*nRepeat; // Extra bursts can be spurious
nSingle += 2*nRepeat + nExtra;
nRepeat = 0;
nExtra = 0;
}
// GD 2009 End
if ( nRepeat==0 )
pDuration[nSingle*2-1] = 999999.;
pSuffix = new char[16];
strcpy(pSuffix, "" );
}
Signal::~Signal()
{
delete pDuration;
delete pSuffix;
}
void Signal::setPreempt(int prValue)
// GD 2009. This sets a preempt value when there is a valid decode which
// is not reported at the time, so we could not use NewPreemptValue as
// that only takes effect when protocol is reported.
{
if ( preemptValue < prValue
|| preemptValue == prValue
&& nFrameL > preemptLength )
{
preemptValue = prValue;
preemptLength = nFrameL;
}
}
void Signal::setzContext()
// GD 2009. This sets the zContext value, which records pContext array from the
// start of a repeat sequence, and is used when that start is suppressed by the
// decoder.
{
int nStart = (pFrame - pDuration)/2;
zContext[0] = nStart + (preemptLength<<20);
// nProtocol is incremented before being stored in pContext[1]
zContext[1] = ((nFrameL - MinFrame)<<16) + (preemptValue<<8) + nProtocol + 1;
}
void Signal::decode()
{
typedef void (Signal::* SignalFnc)();
static const SignalFnc Funcs[] = {
&Signal::tryXMP,
&Signal::tryZenith,
&Signal::tryX10,
&Signal::tryXX,
&Signal::tryTDC, // GD 2009. Needs to be above Q2
&Signal::trySejin, // GD 2009
&Signal::tryQ1,
&Signal::tryQ2,
&Signal::tryDirecTV,
&Signal::trySomfy,
&Signal::tryGap, // if this moves, change nGapProtocol value
&Signal::tryRC5,
&Signal::tryRC6,
&Signal::tryCanalSat, // GD 2009
&Signal::tryNokia,
&Signal::tryPCTV,
&Signal::tryF12,
&Signal::trySony,
&Signal::trySingleBurstSize,
&Signal::tryGXB,
&Signal::tryAsync,
&Signal::tryAirboard,
&Signal::tryAirAsync,
&Signal::tryAK,
&Signal::trySunfire,
&Signal::tryGrundig16,
&Signal::tryPid13,
&Signal::tryLutron, // GD 2010
&Signal::tryElan, // DAR 2012
&Signal::tryBryston, // DAR 2012
&Signal::tryHumax, // DAR 2012
&Signal::tryAdNotam // DAR 2012
};
nGapProtocol = 10;
*pProtocol = 0;
// GD 2009 Start - Initialize output variables as IRScope does not appear
// to do so on repeat calls
/* if ( *pDevice >= 0 ) */ *pDevice = -1; // Initialize Device only if not carrying ExtraBurstCount
*pSubDevice = -1;
*pOBC = -1;
for (int n = 0; n < 4; n++) pHex[n] = -1;
*pMisc = 0;
*pError = 0;
// GD 2009 End
int nStart = pContext[0] & 0xFFFFF;
preemptLength = pContext[0] >> 20;
nFrameL = (pContext[1] >> 16) + MinFrame;
nProtocol = (pContext[1] & 0xFF);
preemptValue = (pContext[1] >> 8) & 0xFF;
newPreemptValue = 0;
newPreemptLength = 0;
int nStartLimit = nSingle + nRepeat;
int nTot = nStartLimit + nRepeat;
pMainLimit = pDuration + 2 * nStartLimit;
pFullLimit = pMainLimit + 2 * nRepeat;
if (nRepeat < MinFrame)
nStartLimit = nTot - (MinFrame-1);
// /* Cover bug in CCF2EFC */ if (nStartLimit>200) nStartLimit=200;
for (; nStart<nStartLimit; nStart++)
{
pFrame = pDuration + nStart*2;
frameLeft = 1e9;
if ( nStart )
{
frameLeft = pFrame[-1];
if ( nStart == nSingle )
{
float wrap = pDuration[ 2*(nSingle+nRepeat) -1 ];
if ( wrap >= frameLeft )
frameLeft = wrap;
}
}
if ( nStart == 0 || nStart == nSingle || pFrame[0]+pFrame[1] < pFrame[-2]*.5+pFrame[-1]
|| pFrame[0] > 14000 ) // This last one deals with Lutron, which uses a long lead-in rather than framing
{
pFrameEnd = pFrame + nFrameL*2-3; // Point to Off half of second to last burst (advanced later to be final Off)
nTotDur = 0;
for (float* pDur = pFrame; ; )
{
sortOn.insert(pDur);
sortOff.insert(pDur+1);
sortBurst.insert1(pDur);
nTotDur += pDur[0] + pDur[1];
pDur+=2;
if (pDur >= pFrameEnd)
break;
}
sortBurst.findMid();
int nMaxL = nTot - nStart; // Maximum frame length that could start at nStart
if (nMaxL > MaxFrame)
nMaxL = MaxFrame;
for (; ; nFrameL++) // Loop over posible frame lengths
{
pFrameEnd++; // Point at last burst (lead out)
sortOn.insert(pFrameEnd);
nMaxDur = sortOff.max1; // Excludes final Off
if (nMaxDur < sortOn.max1)
nMaxDur = sortOn.max1;
nTotDur += *pFrameEnd++; // On
frame = *pFrameEnd;
if ( frameLeft <= frame )
frame = frameLeft;
// GD Start
int xxStart = nStart;
int xxFrameL = nFrameL;
// Calc freq for this frame
nFreq = (nContextLength > 4 && pCounts != NULL) ? getFreq(nStart, nStart+nFrameL) : nFreq_in;
// GD End
for ( ;nProtocol < sizeof(Funcs)/sizeof(*Funcs); )
{
(this->*(Funcs[nProtocol]))();
++nProtocol;
if (*pProtocol)
{
if ( newPreemptValue > preemptValue
|| newPreemptValue == preemptValue
&& newPreemptLength > preemptLength )
{
preemptValue = newPreemptValue;
preemptLength = newPreemptLength;
}
pContext[0] = nStart + (preemptLength<<20);
pContext[1] = ((nFrameL - MinFrame)<<16) + (preemptValue<<8) + nProtocol;
return;
}
}
nProtocol = 0;
if ( nFrameL >= nMaxL )
break;
nTotDur += *pFrameEnd; // Off
sortBurst.insert(pFrameEnd-1);
sortOff.insert(pFrameEnd);
} // for (; ; nFrameL++)
sortOn.clear();
sortOff.clear();
sortBurst.clear();
} // if ( nStart == 0 || ...
nFrameL=MinFrame;
if (--preemptLength <= 0)
{
preemptLength = preemptValue = 0;
}
} // for (; nStart<nStartLimit; nStart++)
}
void Signal::decode2()
{
decode();
int xDevice = *pDevice;
int xSubDevice = *pSubDevice;
int xOBC = *pOBC;
int xHex[4];
unsigned int wContext[2]; // the pContext for the next iteration
unsigned int xContext[2]; // the pContext of the first decode of a repeat sequence
unsigned int yContext[2]; // the pContext of the last decode of a repeat sequence
int xNote_out = nNote_out;
int xAuxNote_out = nAuxNote_out;
int contextLengthOut = 2;
int xFrameCount;
int xDittos;
int xInitLeadIn = bInitLeadIn;
int n;
int bSetyContext = true;
// Test if lead-in only with tryGap, note that nProtocol is already incremented
int xLeadIn = ( ((pContext[1] & 0xFF) == nGapProtocol + 1) && (pLead) ) ? 1 : 0;
int yLeadIn;
char xProtocol[256];
char xMisc[256];
int iEndpoint = 0; // -2 & -1 = saved 1 & 0; 0 = no early end; 1 = early end
for (n = 0; n < 4; n++) xHex[n] = pHex[n];
for (n=0; n<2; n++) xContext[n] = nFrameCount == 0 ? pContext[n] : zContext[n];
strcpy(xProtocol, pProtocol);
strcpy(xMisc, pMisc);
if ( *pError == 0 && *pProtocol )
{
do
{
do
{
if (bSetyContext)
{
// yContext is not set if the most recent decode has been suppressed as spurious
for (n=0; n<2; n++) yContext[n] = pContext[n]; // save the current sequence end
}
for (n=0; n<2; n++) wContext[n] = pContext[n]; // save for next iteration
bSetyContext = true;
xFrameCount = ++nFrameCount; // count the frame just validated
xDittos = nDittos; // save the ditto count so not upset by next decode
iEndpoint = ( iEndpoint < 0 ) ? - iEndpoint - 1 : // if <0 restore old value, else test
( nDittos != 0 // don't look for repeats if dittos present
|| *pFrameEnd > 200000 ); // don't look for repeats if more than 200ms between frames
sortOn.clear();
sortOff.clear();
sortBurst.clear();
decode(); // check for spurious next frame even if early end
yLeadIn = ( ((pContext[1] & 0xFF) == nGapProtocol + 1) && (pLead) ) ? 1 : 0;
}
while ( *pError == 0
&& strcmp(pProtocol, xProtocol) == 0
&& *pDevice == xDevice
&& *pSubDevice == xSubDevice
&& *pOBC == xOBC
&& strcmp(pMisc, xMisc) == 0
&& nDittos == 0 // if new frame has dittos, or nDittos=-1, treat as non-matching
&& !bInitLeadIn // if new frame has initial lead-in, treat as non-matching
&& (yContext[0] & 0xFFFFF) + (yContext[1] >> 16) + MinFrame
== (pContext[0] & 0xFFFFF) - yLeadIn // if not consecutive, treat as non-matching
&& !iEndpoint); // exit loop if early end marked
// If a generic Gap decode is followed by a real decode starting one burst later,
// Gap decode was probably spurious, treating a real lead-in as start of data.
// So cancel the Gap decode, remove it from the count and reset the 'x...' variables
if ( *pError == 0 && *pProtocol
&& strncmp(xProtocol, "Gap-", 4) == 0
&& strncmp(pProtocol, "Gap-", 4) != 0
&& ((pContext[0] - yContext[0]) & 0xFFFFF) == 1 ) // check start difference is 1 burst
{
--nFrameCount; // remove from count
iEndpoint = 0; // no early end to count, but let the real decode be tested
xDevice = *pDevice;
xSubDevice = *pSubDevice;
xOBC = *pOBC;
xNote_out = nNote_out;
xAuxNote_out = nAuxNote_out;
xLeadIn = ( ((pContext[1] & 0xFF) == nGapProtocol + 1) && (pLead) ) ? 1 : 0;
for (n = 0; n < 4; n++) xHex[n] = pHex[n];
for (n=0; n<2; n++) xContext[n] = pContext[n];
strcpy(xProtocol, pProtocol);
strcpy(xMisc, pMisc);
}
else
// If a decode is followed by a generic Gap decode with same start frame or one burst,
// later, Gap decode was probably spurious, treating two or more real frames as one.
// So skip the Gap decode and remove it from the count
if ( *pError == 0 && *pProtocol
&& strncmp(pProtocol, "Gap-", 4) == 0
&& ((pContext[0] - yContext[0]) & 0xFFFFE) == 0 ) // check start burst
{
--nFrameCount; // remove spurious Gap decode from count
bSetyContext = false; // do not set yContext from it
iEndpoint = - iEndpoint - 1; // allow to continue but save old value
}
else
// If a decode is followed by a Solidtek decode other than the documented ones of
// Solidtek16 and Solidtek20, or by an S:xx.xx... decode (all these in tryQ2)
// and the whole of this following decode frame lies within the frame of the
// previous decode, then this second decode is probably spurious. So skip
// it and remove it from the count.
if ( *pError == 0 && *pProtocol
&& ( strncmp(pProtocol, "S:", 2) == 0
|| ( strncmp(pProtocol, "Solidtek", 8) == 0
&& strncmp(pProtocol+8, "16", 2) != 0
&& strncmp(pProtocol+8, "20", 2) != 0 ) )
&& ( (pContext[0] & 0xFFFFF) + (pContext[1] >> 16) // compare nStart + nFrameL
<= (yContext[0] & 0xFFFFF) + (yContext[1] >> 16) ) )
{
--nFrameCount; // remove spurious Solidtek decode from count
bSetyContext = false; // do not set yContext from it
iEndpoint = - iEndpoint - 1; // allow to continue but save old value
}
else
// combine two types of X10 frame
if ( *pError == 0
&& strcmp(pProtocol, "X10") == 0
&& strncmp(xProtocol, "X10", 3) == 0
&& xOBC == *pOBC
&& xHex[0] == pHex[0] )
{
if ( xInitLeadIn )
{
xNote_out = 4;
xAuxNote_out = -1;
*xMisc = 0; // cancel the "invalid signal"
xInitLeadIn = false; // only needed once
}
}
else
// If a Solidtek16 or Solidtek20 decode is followed by various decodes
// of fewer frames in which the first 8 bits match the Solidtek data then
// it is probably a spurious decode of a partial Solidtek repeat at the end
// of a signal. Skip the spurious decode and remove it from the count.
if ( *pError == 0
&& ( strncmp(pProtocol, "S:", 2) == 0
|| strcmp(pProtocol, "Blaupunkt{prefix}") == 0
|| strcmp(pProtocol, "XX") == 0
|| strcmp(pProtocol, "DirecTV") == 0)
&& ( strcmp(xProtocol, "Solidtek16") == 0
|| strcmp(xProtocol, "Solidtek20") == 0 ) )
{
int x2;
int xbits;
char xstr[3];
// reconstruct first 8 bits of Solidtek decode as hex string in xstr
if ( strcmp(xProtocol+8, "16") == 0 ) x2 = xOBC;
else x2 = xSubDevice;
xbits = ( msb(xDevice,4)<<3 | msb(x2,3) ) & 0xFF;
sprintf(xstr, "%02X", xbits);
// check those that can be checked with this xbits value
if ( strcmp(pProtocol, "Blaupunkt{prefix}") == 0
&& strcmp(xstr, "00") == 0 // needed to match Blaupunkt
|| strcmp(pProtocol, "DirecTV") == 0
&& *pDevice == 0 && *pOBC == 0
&& strcmp(xstr, "00") == 0 // needed to match DirecTV
|| strncmp(pProtocol, "S:", 2) == 0
&& strncmp(pProtocol+5, xstr, 2 ) == 0 ) // test against yy of S.xx.yy....
{
--nFrameCount; // remove spurious decode from count
bSetyContext = false; // do not set yContext from it
iEndpoint = - iEndpoint - 1; // allow to continue but save old value
}
else
// check the one that needs complemented xbits value
{ sprintf(xstr, "%02X", 255-xbits);
if ( strcmp(pProtocol, "XX") == 0
&& strncmp(pMisc, xstr, 2 ) == 0 )
{
--nFrameCount; // remove spurious decode from count
bSetyContext = false; // do not set yContext from it
iEndpoint = - iEndpoint - 1; // allow to continue but save old value
}
else break;
}
}
else
// The Sony protocol can match the Async protocol if the Sony data comprises
// binary pairs 01 and 10. The Sony lead-in translates to Async 0x80, the
// binary pairs 01 and 10 translate to Async 0xE6 and 0x98. The minimum
// required for an Async decode is 3 such Sony binary pairs. So if a Sony
// decode is followed by such an Async decode then the Async probably comes
// from a truncated Sony frame. Skip the spurious decode and remove it from
// the count.
if ( *pError == 0
&& strncmp(xProtocol, "Sony", 4) == 0
&& strncmp(pProtocol, "Async", 5) == 0
&& strncmp(pMisc, "80", 2) == 0
&& ( strncmp(pMisc+3, "98", 2) == 0
|| strncmp(pMisc+3, "E6", 2) == 0 )
&& ( strncmp(pMisc+6, "98", 2) == 0
|| strncmp(pMisc+6, "E6", 2) == 0 )
&& ( strncmp(pMisc+9, "98", 2) == 0
|| strncmp(pMisc+9, "E6", 2) == 0 ) )
{
--nFrameCount; // remove spurious Async decode from count
bSetyContext = false; // do not set yContext from it
iEndpoint = - iEndpoint - 1; // allow to continue but save old value
}
else
// If an XMP decode with valid first half frame is followed by a Mitsubishi decode with
// same start frame, Mitsubishi decode was probably a spurious decode of an XMP signal
// with one or more missing digits (Mitsubishi frame length is 17, valid XMP is 18).
// So skip the Mitsubishi decode and remove it from the count. If counts suppressed,
// i.e. learning remotes rather than IRScope, do same if following decode is consecutive
// and is another XMP frame.
if ( *pError == 0 && *pProtocol
&& (strcmp(xProtocol, "XMP") == 0 || strncmp(xProtocol, "XMP-", 4) == 0)
&& (strcmp(pProtocol, "Mitsubishi") == 0
&& ((pContext[0] - yContext[0]) & 0xFFFFF) == 0 // check start burst
|| strncmp(pProtocol, "XMP", 3) == 0
&& (yContext[0] & 0xFFFFF) + (yContext[1] >> 16) + MinFrame
== (pContext[0] & 0xFFFFF)
&& bSuppressCount )
&& bXMPHalfValid )
{
// char xstr[100];
// sprintf(xstr, "start1 = %d start2 = %d",(yContext[0] & 0xFFFFF) + (yContext[1] >> 16) + MinFrame,(pContext[0] & 0xFFFFF));
// MessageBox(0,xstr,"Error",0);
if (strcmp(pProtocol, "Mitsubishi") == 0)
{
--nFrameCount; // remove spurious Mitsubishi decode from count
bSetyContext = false; // do not set yContext from it
}
iEndpoint = - iEndpoint - 1; // allow to continue but save old value
}
else break;
}
while ( iEndpoint <= 0 );
// Frame just decoded does not match earlier frame. If it comes from
// extra bursts, treat it as spurious so prevent its decode
if ( nNonSpurious > 0 // value has been set
&& (int)(pContext[0] & 0xFFFFF) >= nNonSpurious )
{
wContext[0] = 0xFFFFF;
}
if ( strcmp(xProtocol, "NEC") == 0
|| strcmp(xProtocol, "NECx") == 0
|| strcmp(xProtocol, "48-NEC") == 0 )
{
if (xFrameCount > 1 || nDittos == 0 && nRepeat > 6 )
{ //nRepeat > 0 changed to nRepeat > 6 DAR Dec 2010 to avoid detecting faulty
strcat(xProtocol, "2"); //ditto(s) as NEC2. Anyway, NEC2 should have nRepeat == 34 if xFrame == 1
}
strcat (xProtocol,pSuffix); //DAR Nov 2010
}
else if ( strcmp(xProtocol, "JVC") == 0
&& !xInitLeadIn )
{
strcat(xProtocol, "{2}");
xNote_out = 1;
}
else if ( strncmp(xProtocol, "RCA", 3) == 0
&& xInitLeadIn )
{
strcat(xProtocol, "(Old)");
xNote_out = 11;
}
else if ( strncmp(xProtocol, "Gap-", 4) == 0
|| strncmp(xProtocol, "??", 2) == 0 )
{
xNote_out = 8;
}
// Restore pContext to value saved before last call to decode()
for (n=0; n<2; n++) pContext[n] = wContext[n];
int iParam = xFrameCount;
*pDevice = xDevice;
*pSubDevice = xSubDevice;
*pOBC = xOBC;
for (n=0; n < 4; n++) pHex[n] = xHex[n];
strcpy(pProtocol, xProtocol);
if (xNote_out == -1) // if note unset, set default
{
if (xDittos > 0) // if dittos present
{
xNote_out = 3; // set ditto-frame note
iParam = xDittos;
}
else
{
xNote_out = 1; // else set identical repeats note
}
}
if (nContextLength > 2) // extended context used by IRScope
{
// set pContext[2] to the pContext[0] value at start of repeat series
// but without its preemptLength value as that is no longer relevant
pContext[2] = xContext[0] & 0xFFFFF;
// if there was a tryGap() lead-in then actual start is one frame earlier
if ( pContext[2] > 0 ) pContext[2] -= xLeadIn;
// carry the preemptLength for the next iteration in pContext[2] and mask
// it out of pContext[0] as that field of pContext[0] is used for the handshake
pContext[2] |= pContext[0] & 0xFFF00000;
pContext[0] &= 0xFFFFF;
contextLengthOut = 3; // record the context length to be returned in the handshake
}
if (nContextLength > 3)
{
// set pContext[3] to the pContext[1] value at start of repeat series
pContext[3] = xContext[1];
contextLengthOut = 4; // record the context length to be returned in the handshake
}
if (nContextLength > 5)
{
// set pContext[4,5] to the pContext[0,1] values at end of repeat series
pContext[4] = yContext[0];
pContext[5] = yContext[1];
contextLengthOut = 6;
}
if (nContextLength > 7)
{
// Incoming, pContext[6,7] pass a 64-bit pointer to a Counts array used to calculate
// the frequency of individual decodes. Outgoing, pContext[6] provides the frequency of
// the repeat series being reported in this return.
pContext[6] = getFreq(pContext[2] & 0xFFFFF, (pContext[0] & 0xFFFFF) + (pContext[1]>>16) + MinFrame);
contextLengthOut = 8;
}
if (nContextLength > 8)
{
// pContext[8] returns the note and aux note indexes (1-based, so add 1) for use by
// IRScope, and the parameter value for the note.
pContext[8] = iParam | (xNote_out + 1)<<16 | (xAuxNote_out + 1)<<24;
contextLengthOut = 9;
}
if (contextLengthOut > 2)
{
// The handshake is that the 12-bit field in pContext[0] that normally carries the
// preemptLength value is now used to carry a negative value, -(contextLengthOut-2).
// The calling program checks to see if the top 8 bits of this value are 0xFF. If
// so then it is interpreted as a context length value, otherwise as a preemptLength
// value. So the maximum supported context length is 18, corresponding to 0xFF0.
pContext[0] |= (2-contextLengthOut)<<20;
}
n = 0;
if ( !bSuppressCount )
{
// nDittos = -1 means no dittos AND treat repeat frames as separate signals
if ( xFrameCount == 1 && xDittos <= 0 )
{
n = sprintf(pMisc, *xMisc == 0 ? "no repeat" : "no repeat: ");
}
else if ( xFrameCount == 2 && xDittos <= 0 ) // nDittos < 0 should not occur in this case
{
n = sprintf(pMisc, *xMisc == 0 ? "+ 1 copy" : "+ 1 copy: ");
}
else if ( xDittos <= 0 ) // nDittos < 0 should not occur in this case
{
n = sprintf(pMisc, *xMisc == 0 ? "+ %d copies" : "+ %d copies: ", xFrameCount-1);
}
else if ( xFrameCount == 1 && xDittos == 1 )
{
n = sprintf(pMisc, *xMisc == 0 ? "+ 1 ditto" : "+ 1 ditto: ");
}
else if ( xFrameCount == 1 )
{
n = sprintf(pMisc, *xMisc == 0 ? "+ %d dittos" : "+ %d dittos: ", xDittos);
}
else // this case should not occur
{
n = sprintf(pMisc, *xMisc == 0 ? "+ %d dittos, %d copies" : "+ %d dittos, %d copies: ",
xDittos, xFrameCount-1);
}
}
// next line reports nStart and nFrameL for diagnostic purposes
// n += sprintf(pMisc+n, "S=%d L=%d ", wContext[0]&0xFFFFF, (wContext[1]>>16)+MinFrame);
strcpy(pMisc+n, xMisc); // append original pMisc text
*pError = 0;
}
}
int Signal::msb(int val, int bits=8)
{
unsigned int t = ((val>>16)&0xFFFF) + ((val&0xFFFF)<<16);
t = ((t>>8)&0xFF00FF) + ((t&0xFF00FF)<<8);
t = ((t>>4)&0xF0F0F0F) + ((t&0xF0F0F0F)<<4);
t = ((t>>2)&0x33333333) + ((t&0x33333333)<<2);
t = ((t>>1)&0x55555555) + ((t&0x55555555)<<1);
return t >> (32-bits);
}
int __cdecl compare( const void *arg1, const void *arg2 )
{
return (int) ( * ( float* ) arg1 - * ( float* ) arg2 );
}
unsigned int Signal::getMsb(int first, int bits)
{
int byte = first >> 3;
int bt = 8-(first & 7);
unsigned int rslt = cBits[byte] & ((1<<bt)-1);
if ( bt >= bits )
{
return rslt >> (bt - bits);
}
for (bits-=bt; bits>=8; bits-=8)
rslt = (rslt<<8) + cBits[++byte];
return (rslt<<bits) + ((unsigned int)(cBits[1+byte]) >> (8-bits));
}
unsigned int Signal::getLsb(int first, int bits)
{
int byte = first >> 3;
int bt = first & 7;
unsigned int rslt = cBits[byte] >> bt;
bt = 8-bt;
if ( bt >= bits )
{
return rslt & ((1<<bits)-1);
}
while ( bt+8 < bits )
{
rslt += cBits[++byte] << bt;
bt += 8;
}
return rslt + ( ( cBits[1+byte] & ((1<<(bits-bt))-1) ) << bt );
}
void Signal::makeMsb()
{
for (int ndx=(nBit+7)>>3; --ndx>=0; )
cBits[ndx] = msb( cBits[ndx] );
}
void Signal::cleanup()
{
memset(cBits, 0, sizeof(cBits));
pBit = pFrame;
nBit = 0;
nState = 0;
}
void Signal::decodeX( int nCount )
{
_ASSERT(nBit+nCount <= sizeof(cBits)*8);
while (--nCount >= 0)
{
float nDelta = nMaxShort - *pBit;
if ( nDelta < 0 )
{
cBits[nBit>>3] |= 1 << (nBit & 7);
}
pBit += 2;
nBit++;
}
}
void Signal::decodeX2( int nCount )
{
_ASSERT(nBit+nCount <= sizeof(cBits)*8);
while (--nCount >= 0)
{
if ( pBit[0]+pBit[1] > nMaxShort )
{
cBits[nBit>>3] |= 1 << (nBit & 7);
}
pBit += 2;
nBit++;
}
}
int Signal::checkDecodeX( int start, int count, float minShort, float maxLong, float maxFront )
{
_ASSERT(nBit+count <= sizeof(cBits)*8);
float *pB = pFrame+start;
if ( pB >= pMainLimit
|| pB+2*count > pFullLimit )
{
return 0;
}
while (--count >= 0)
{
double burst = pB[0]+pB[1];
if ( burst < minShort
|| burst > maxLong
|| pB[0] > maxFront )
{
return 0;
}
if ( burst > nMaxShort )
{
cBits[nBit>>3] |= 1 << (nBit & 7);
}
pB += 2;
nBit++;
}
return 1;
}
int Signal::decodeRaw( int bitsRequested )
{
_ASSERT(nBit+bitsRequested < sizeof(cBits)*8);
while ( bitsRequested > 0 )
{
if (pBit>pFrameEnd)
{
return 0;
}
double x = *pBit * m_rawUnit
+ ( ((pBit-pFrame)&1) ? m_rawGapAdjust : m_rawPulseAdjust );
double y = floor(x);
// GD Added test pBit<pFrameEnd as this return should not occur on a lead-out
if ( pBit < pFrameEnd && x-y > m_rawErrorLimit || y==0. )
{
return 0;
}
int bits = (int)y;
if ( (bitsRequested -= bits) < 0 )
{
bits += bitsRequested;
}
if ( (pBit-pFrame) & 1 )
{
nBit += bits;
}
else
{
do
{
cBits[nBit>>3] |= 1 << (nBit & 7);
nBit++;
} while (--bits);
}
pBit++;
}
return 1;
}
// The inner requirement is:
// dBitMax >= value / ( bits + .3 )
// dBitMin <= value / ( bits - .3 )
// Which implies:
// bits >= value / dBitMax - .3
// bits <= value / dBitMin + .3
//
// dNewMin = value / ( bits + .3 )
// dNewMax = value / ( bits - .3 )
// This implies an overall constraint of
// dBitMin >= max_value / ( max_bits + .3 )
// dBitMax <= min_value / .7
//
int Signal::decodeAsync(
float *pData,
int bits, // previously decoded bits
int sizes, // Bitmask of integerized durations seen
double dBitMin,
double dBitMax,
int bitsPerByte, // 10 or 11 = start + 8 data + 1 or 2 stop
int minTotBits )
{
if (bits >= (int)sizeof(cBits)*bitsPerByte)
return 0;
int phase = bits % bitsPerByte;
int pole = ( pData - pFrame ) & 1; // 0==pulse 1==gap
int minBits = (int)(*pData / dBitMax + .69999); // fewest bits this value might be
if ( minBits == 0 )
minBits = 1;
int maxB = (pole ? bitsPerByte : 9 ) - phase; // most bits that would be valid here
int maxBits = (int)(*pData / dBitMin + .30001); // most bits this value might be
if ( pData == pFrameEnd )
{
if (bits < minTotBits || maxBits <= maxB)
return 0;
sizes &= sizes - 1; // clear one bit
if ( ( sizes & (sizes-1) ) == 0 )
return 0; // Need to see at least three sizes
int bytes = bits/bitsPerByte + 1;
memset(cBits,0xFF,bytes);
nMinShort = floor(dBitMin);
nMaxShort = ceil(dBitMax);
return bytes;
}
if ( maxBits > maxB )
maxBits = maxB;
for (int nGuess = minBits; nGuess <= maxBits; ++nGuess)
{
if (pole && nGuess+phase > 8 && nGuess < maxB)
continue;
double dNewMin = *pData / (nGuess+.3);
if (dNewMin < dBitMin)
dNewMin = dBitMin;
double dNewMax = *pData / (nGuess-.3);
if (dNewMax > dBitMax)
dNewMax = dBitMax;
if ( dNewMin <= dNewMax )
{
int result = decodeAsync(pData+1, bits+nGuess, sizes | (1<<nGuess), dNewMin, dNewMax, bitsPerByte, minTotBits);
if (result)
{
if (!pole)
cBits[bits/bitsPerByte] &= (unsigned char)( ((((0xFF<<nGuess)+1)<<phase) - 1) >>1 );
return result;
}
}
}
return 0;
}
unsigned int Signal::getFreq(int start, int end)
{
int s=0;
double t=0;
double freq;
if (pCounts == NULL) return 0;
for (int n=start; n<end; n++)
{
{
s += pCounts[2*n];
t += pDuration[2*n];
}
}
freq = ((s!=end-start)&&(t > 0)) ? s*1000000./t : 0;
return (unsigned int)freq;
}
void Signal::tryPid13()
// GD 2009 Added comment: Decode assumes frame contains at least one 1 bit
{
// {37.9k,1082}<0,-1|1,-0>(1,-1,F:6,-26)+
if ( nFrameL > 5 )
return;
if ( pFrame[0] < 900.
|| pFrame[0] > 1200. )
{
return;
}
if ( ! framed(nTotDur) )
return;
m_rawUnit = 1. / pFrame[0];
m_rawPulseAdjust = .4f; // Pulse from -.4 to +.2
m_rawGapAdjust = .2f; // Gap from -.2 to +.4
m_rawErrorLimit = .6f;
cleanup();
pBit += 1;
if ( ! decodeRaw(7) || pBit < pFrameEnd )
{
return;
}
// GD 2009 Start: Add any trailing 0's concatenated with lead-out into
// nTotDur and re-test framing. Eliminates spurious decodes of final
// 2 frames of G.I.4DTV and probably others.
float n = 7.f; // number of trailing 0's
unsigned char xBits = cBits[0];
for ( ; xBits != 0; n-- ) { xBits >>= 1; }
if ( ! framed(nTotDur + (n - m_rawGapAdjust) / m_rawUnit ) )
{
return;
}
// GD 2009 End
strcpy(pProtocol,"pid-0013");
*pOBC = getLsb(1,6);
*pHex = ((msb(cBits[0]) >> 1) & 0x3F) | 0x80;
}
// Lutron uses a 40kHz carrier to send an asynchronous signal with 8 start bits,
// 24 data bits and 4 stop bits. The data bits are 6 4-bit values, each being
// the encoding in reversed binary with odd parity of a 3-bit binary value. The
// 18-bit sequence so encoded consists of an 8-bit device code, an 8-bit OBC and
// two even parity bits calculated pairwise, all sent msb.
void Signal::tryLutron()
{
if ( nFrameL < 4 || nFrameL > 10)
return;
if ( pFrame[0] < 14000
|| pFrame[0] > 30000
|| nTotDur < 26*2300 // Nominal min is 29 units
|| nTotDur > 35*2300 // Nominal max is 32 units
|| sortOn.min1 < 2100 // Nominal min is 2100
|| sortOff.min1 < 2100 ) // Nominal min is 2100
{
return;
}
int bParityError = true;
// Number of bits between lead-in and lead-out can be anything from 18 to 24
for (int nBitCnt = 18; bParityError && nBitCnt < 25; nBitCnt++)
{
// Work out m_rawUnit from the total duration of these 18 to 24 bits