forked from Attila-FIN/rc-switch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRCSwitch.cpp
1270 lines (1141 loc) · 38.3 KB
/
RCSwitch.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
/*
RCSwitch - Arduino libary for remote control outlet switches
Copyright (c) 2011 Suat Özgür. All right reserved.
Contributors:
- Andre Koehler / info(at)tomate-online(dot)de
- Gordeev Andrey Vladimirovich / gordeev(at)openpyro(dot)com
- Skineffect / http://forum.ardumote.com/viewtopic.php?f=2&t=46
- Dominik Fischer / dom_fischer(at)web(dot)de
- Frank Oltmanns / <first name>.<last name>(at)gmail(dot)com
- Andreas Steinel / A.<lastname>(at)gmail(dot)com
- Max Horn / max(at)quendi(dot)de
- Robert ter Vehn / <first name>.<last name>(at)gmail(dot)com
- Johann Richard / <first name>.<last name>(at)gmail(dot)com
- Vlad Gheorghe / <first name>.<last name>(at)gmail(dot)com https://github.com/vgheo
- Per Ivar Nerseth / <first name>(at)<last name>(dot)com
- Attila Kovacs / a<last name>(at)atinoy(dot)fi
Project home: https://github.com/sui77/rc-switch/
This library 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 2.1 of the License, or (at your option) any later version.
This library 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 library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "RCSwitch.h"
//#define DEBUG //If you comment this line, debug statements are turned off
#ifdef RaspberryPi
// PROGMEM and _P functions are for AVR based microprocessors,
// so we must normalize these for the ARM processor:
#define PROGMEM
#define memcpy_P(dest, src, num) memcpy((dest), (src), (num))
#endif
#if defined(ESP8266) || defined(ESP32)
// interrupt handler and related code must be in RAM on ESP8266,
// according to issue #46.
#define RECEIVE_ATTR ICACHE_RAM_ATTR
#else
#define RECEIVE_ATTR
#endif
/* Format for protocol definitions:
* {
* protocol ID,
* changeCount, the number of timings captured for the specific protocol. Timings are the array of durations of the consecutive low and high levels
* pulselength (microseconds),
* sync bit before the data bits. Normally zero, i.e. {0, 0} {high pulse length, low pulse length},
* "0" bit {high pulse length, low pulse length},
* "1" bit {high pulse length, low pulse length},
* pause bit {high pulse length, low pulse length},
* bool inverted signal (bit starts on falling edge)
* }
*
* pulselength: pulse length in microseconds, e.g. 350
* pause/end bit: {1, 31} means 1 high pulse and 31 low pulses
* (perceived as a 31*pulselength long pulse, total length of sync bit is
* 32*pulselength microseconds), i.e:
* _
* | |_______________________________ (don't count the vertical bars)
* "0" bit: waveform for a data bit of value "0", {1, 3} means 1 high pulse
* and 3 low pulses, total length (1+3)*pulselength, i.e:
* _
* | |___
* "1" bit: waveform for a data bit of value "1", e.g. {3,1}:
* ___
* | |_
*
* These are combined to form Tri-State bits when sending or receiving codes.
*/
#if defined(ESP8266) || defined(ESP32)
static const RCSwitch::Protocol proto[] = {
#else
static const RCSwitch::Protocol PROGMEM proto[] = {
#endif
{1, 50, 290, {0, 0}, {1, 3}, {3, 1}, {1, 31}, false}, // protocol 1 (EV1527)
{2, 255, 650, {0, 0}, {1, 2}, {2, 1}, {1, 10}, false}, // protocol 2
{3, 255, 100, {0, 0}, {4, 11}, {9, 6}, {30, 71}, false}, // protocol 3
{4, 255, 380, {0, 0}, {1, 3}, {3, 1}, {1, 6}, false}, // protocol 4
{5, 255, 500, {0, 0}, {1, 2}, {2, 1}, {6, 14}, false}, // protocol 5
{6, 255, 450, {0, 0}, {1, 2}, {2, 1}, {23, 1}, true}, // protocol 6 (HT6P20B)
{7, 255, 150, {0, 0}, {1, 6}, {6, 1}, {2, 62}, false}, // protocol 7 (HS2303-PT, i. e. used in AUKEY Remote)
{8, 255, 250, {1, 10}, {1, 5}, {1, 1}, {1, 40}, false}, // protocol 8 (Nexa)
{9, 255, 100, {0, 0}, {6, 6}, {6, 12}, {6, 169}, false}, // protocol 9 (Everflourish Single Button)
{10, 255, 100, {0, 0}, {6, 6}, {6, 12}, {6, 120}, false},// protocol 10 (Everflourish All Buttons)
{11, 88, 100, {34, 34}, {5, 4}, {5, 13}, {2, 200}, false},// protocol 11 (Cixi Yidong Electronics , sold as AXXEL, Telco, EVOLOGY, CONECTO, mumbi, Manax etc.)
{12, 26, 333, {0, 1}, {1, 2}, {2, 1}, {45, 0}, true}, // protocol 12 (CAME)
{13, 68, 100, {0, 0}, {3, 8}, {8, 3}, {3, 100}, false}, // protocol 13 (Shi Qiong) - 1+32 bit protocol. The first bit is a leading "1"
};
enum
{
numProto = sizeof(proto) / sizeof(proto[0])
};
#ifndef RCSwitchDisableReceiving
volatile unsigned long RCSwitch::nReceivedValue = 0;
volatile unsigned int RCSwitch::nReceivedBitlength = 0;
volatile unsigned int RCSwitch::nReceivedDelay = 0;
volatile unsigned int RCSwitch::nReceivedProtocol = 0;
int RCSwitch::nReceiveTolerance = 0;
const unsigned int RCSwitch::nSeparationLimit = 4300;
// separationLimit: minimum microseconds between received codes, closer codes are ignored.
// according to discussion on issue #14 it might be more suitable to set the separation
// limit to the same time as the 'low' part of the sync signal for the current protocol.
unsigned int RCSwitch::timings[RCSWITCH_MAX_CHANGES];
unsigned int RCSwitch::timings_copy[RCSWITCH_MAX_CHANGES];
// array to hold the bits received as chars (to support longer frames)
char RCSwitch::receivedBits[RCSWITCH_MAX_CHANGES];
#endif
RCSwitch::RCSwitch()
{
this->nTransmitterPin = -1;
this->setRepeatTransmit(10);
this->setProtocol(1);
#ifndef RCSwitchDisableReceiving
this->nReceiverInterrupt = -1;
this->setReceiveTolerance(35);
RCSwitch::nReceivedValue = 0;
#endif
}
/**
* Sets the protocol to send.
*/
void RCSwitch::setProtocol(Protocol protocol)
{
this->protocol = protocol;
}
/**
* Sets the protocol to send, from a list of predefined protocols
*/
void RCSwitch::setProtocol(int nProtocol)
{
if (nProtocol < 1 || nProtocol > numProto)
{
nProtocol = 1; // TODO: trigger an error, e.g. "bad protocol" ???
}
#if defined(ESP8266) || defined(ESP32)
this->protocol = proto[nProtocol - 1];
#else
memcpy_P(&this->protocol, &proto[nProtocol - 1], sizeof(Protocol));
#endif
}
/**
* Sets the protocol to send with pulse length in microseconds.
*/
void RCSwitch::setProtocol(int nProtocol, int nPulseLength)
{
setProtocol(nProtocol);
this->setPulseLength(nPulseLength);
}
/**
* gets the protocol
*/
RCSwitch::Protocol RCSwitch::getProtocol()
{
return this->protocol;
}
/**
* Sets pulse length in microseconds
*/
void RCSwitch::setPulseLength(int nPulseLength)
{
this->protocol.pulseLength = nPulseLength;
}
/**
* Sets Repeat Transmits
*/
void RCSwitch::setRepeatTransmit(int nRepeatTransmit)
{
this->nRepeatTransmit = nRepeatTransmit;
}
/**
* Set Receiving Tolerance
*/
#ifndef RCSwitchDisableReceiving
void RCSwitch::setReceiveTolerance(int nPercent)
{
RCSwitch::nReceiveTolerance = nPercent;
}
#endif
/**
* Enable transmissions
*
* @param nTransmitterPin Arduino Pin to which the sender is connected to
*/
void RCSwitch::enableTransmit(int nTransmitterPin)
{
this->nTransmitterPin = nTransmitterPin;
pinMode(this->nTransmitterPin, OUTPUT);
}
/**
* Disable transmissions
*/
void RCSwitch::disableTransmit()
{
this->nTransmitterPin = -1;
}
/**
* Switch a remote switch on (Type D REV)
*
* @param sGroup Code of the switch group (A,B,C,D)
* @param nDevice Number of the switch itself (1..3)
*/
void RCSwitch::switchOn(char sGroup, int nDevice)
{
this->sendTriState(this->getCodeWordD(sGroup, nDevice, true));
}
/**
* Switch a remote switch off (Type D REV)
*
* @param sGroup Code of the switch group (A,B,C,D)
* @param nDevice Number of the switch itself (1..3)
*/
void RCSwitch::switchOff(char sGroup, int nDevice)
{
this->sendTriState(this->getCodeWordD(sGroup, nDevice, false));
}
/**
* Switch a remote switch on (Type C Intertechno)
*
* @param sFamily Familycode (a..f)
* @param nGroup Number of group (1..4)
* @param nDevice Number of device (1..4)
*/
void RCSwitch::switchOn(char sFamily, int nGroup, int nDevice)
{
this->sendTriState(this->getCodeWordC(sFamily, nGroup, nDevice, true));
}
/**
* Switch a remote switch off (Type C Intertechno)
*
* @param sFamily Familycode (a..f)
* @param nGroup Number of group (1..4)
* @param nDevice Number of device (1..4)
*/
void RCSwitch::switchOff(char sFamily, int nGroup, int nDevice)
{
this->sendTriState(this->getCodeWordC(sFamily, nGroup, nDevice, false));
}
/**
* Switch a remote switch on (Type B with two rotary/sliding switches)
*
* @param nAddressCode Number of the switch group (1..4)
* @param nChannelCode Number of the switch itself (1..4)
*/
void RCSwitch::switchOn(int nAddressCode, int nChannelCode)
{
this->sendTriState(this->getCodeWordB(nAddressCode, nChannelCode, true));
}
/**
* Switch a remote switch off (Type B with two rotary/sliding switches)
*
* @param nAddressCode Number of the switch group (1..4)
* @param nChannelCode Number of the switch itself (1..4)
*/
void RCSwitch::switchOff(int nAddressCode, int nChannelCode)
{
this->sendTriState(this->getCodeWordB(nAddressCode, nChannelCode, false));
}
/**
* Deprecated, use switchOn(const char* sGroup, const char* sDevice) instead!
* Switch a remote switch on (Type A with 10 pole DIP switches)
*
* @param sGroup Code of the switch group (refers to DIP switches 1..5 where "1" = on and "0" = off, if all DIP switches are on it's "11111")
* @param nChannelCode Number of the switch itself (1..5)
*/
void RCSwitch::switchOn(const char *sGroup, int nChannel)
{
const char *code[6] = {"00000", "10000", "01000", "00100", "00010", "00001"};
this->switchOn(sGroup, code[nChannel]);
}
/**
* Deprecated, use switchOff(const char* sGroup, const char* sDevice) instead!
* Switch a remote switch off (Type A with 10 pole DIP switches)
*
* @param sGroup Code of the switch group (refers to DIP switches 1..5 where "1" = on and "0" = off, if all DIP switches are on it's "11111")
* @param nChannelCode Number of the switch itself (1..5)
*/
void RCSwitch::switchOff(const char *sGroup, int nChannel)
{
const char *code[6] = {"00000", "10000", "01000", "00100", "00010", "00001"};
this->switchOff(sGroup, code[nChannel]);
}
/**
* Switch a remote switch on (Type A with 10 pole DIP switches)
*
* @param sGroup Code of the switch group (refers to DIP switches 1..5 where "1" = on and "0" = off, if all DIP switches are on it's "11111")
* @param sDevice Code of the switch device (refers to DIP switches 6..10 (A..E) where "1" = on and "0" = off, if all DIP switches are on it's "11111")
*/
void RCSwitch::switchOn(const char *sGroup, const char *sDevice)
{
this->sendTriState(this->getCodeWordA(sGroup, sDevice, true));
}
/**
* Switch a remote switch off (Type A with 10 pole DIP switches)
*
* @param sGroup Code of the switch group (refers to DIP switches 1..5 where "1" = on and "0" = off, if all DIP switches are on it's "11111")
* @param sDevice Code of the switch device (refers to DIP switches 6..10 (A..E) where "1" = on and "0" = off, if all DIP switches are on it's "11111")
*/
void RCSwitch::switchOff(const char *sGroup, const char *sDevice)
{
this->sendTriState(this->getCodeWordA(sGroup, sDevice, false));
}
/**
* Returns a char[13], representing the code word to be send.
*
*/
char *RCSwitch::getCodeWordA(const char *sGroup, const char *sDevice, bool bStatus)
{
static char sReturn[13];
int nReturnPos = 0;
for (int i = 0; i < 5; i++)
{
sReturn[nReturnPos++] = (sGroup[i] == '0') ? 'F' : '0';
}
for (int i = 0; i < 5; i++)
{
sReturn[nReturnPos++] = (sDevice[i] == '0') ? 'F' : '0';
}
sReturn[nReturnPos++] = bStatus ? '0' : 'F';
sReturn[nReturnPos++] = bStatus ? 'F' : '0';
sReturn[nReturnPos] = '\0';
return sReturn;
}
/**
* Encoding for type B switches with two rotary/sliding switches.
*
* The code word is a tristate word and with following bit pattern:
*
* +-----------------------------+-----------------------------+----------+------------+
* | 4 bits address | 4 bits address | 3 bits | 1 bit |
* | switch group | switch number | not used | on / off |
* | 1=0FFF 2=F0FF 3=FF0F 4=FFF0 | 1=0FFF 2=F0FF 3=FF0F 4=FFF0 | FFF | on=F off=0 |
* +-----------------------------+-----------------------------+----------+------------+
*
* @param nAddressCode Number of the switch group (1..4)
* @param nChannelCode Number of the switch itself (1..4)
* @param bStatus Whether to switch on (true) or off (false)
*
* @return char[13], representing a tristate code word of length 12
*/
char *RCSwitch::getCodeWordB(int nAddressCode, int nChannelCode, bool bStatus)
{
static char sReturn[13];
int nReturnPos = 0;
if (nAddressCode < 1 || nAddressCode > 4 || nChannelCode < 1 || nChannelCode > 4)
{
return 0;
}
for (int i = 1; i <= 4; i++)
{
sReturn[nReturnPos++] = (nAddressCode == i) ? '0' : 'F';
}
for (int i = 1; i <= 4; i++)
{
sReturn[nReturnPos++] = (nChannelCode == i) ? '0' : 'F';
}
sReturn[nReturnPos++] = 'F';
sReturn[nReturnPos++] = 'F';
sReturn[nReturnPos++] = 'F';
sReturn[nReturnPos++] = bStatus ? 'F' : '0';
sReturn[nReturnPos] = '\0';
return sReturn;
}
/**
* Like getCodeWord (Type C = Intertechno)
*/
char *RCSwitch::getCodeWordC(char sFamily, int nGroup, int nDevice, bool bStatus)
{
static char sReturn[13];
int nReturnPos = 0;
int nFamily = (int)sFamily - 'a';
if (nFamily < 0 || nFamily > 15 || nGroup < 1 || nGroup > 4 || nDevice < 1 || nDevice > 4)
{
return 0;
}
// encode the family into four bits
sReturn[nReturnPos++] = (nFamily & 1) ? 'F' : '0';
sReturn[nReturnPos++] = (nFamily & 2) ? 'F' : '0';
sReturn[nReturnPos++] = (nFamily & 4) ? 'F' : '0';
sReturn[nReturnPos++] = (nFamily & 8) ? 'F' : '0';
// encode the device and group
sReturn[nReturnPos++] = ((nDevice - 1) & 1) ? 'F' : '0';
sReturn[nReturnPos++] = ((nDevice - 1) & 2) ? 'F' : '0';
sReturn[nReturnPos++] = ((nGroup - 1) & 1) ? 'F' : '0';
sReturn[nReturnPos++] = ((nGroup - 1) & 2) ? 'F' : '0';
// encode the status code
sReturn[nReturnPos++] = '0';
sReturn[nReturnPos++] = 'F';
sReturn[nReturnPos++] = 'F';
sReturn[nReturnPos++] = bStatus ? 'F' : '0';
sReturn[nReturnPos] = '\0';
return sReturn;
}
/**
* Encoding for the REV Switch Type
*
* The code word is a tristate word and with following bit pattern:
*
* +-----------------------------+-------------------+----------+--------------+
* | 4 bits address | 3 bits address | 3 bits | 2 bits |
* | switch group | device number | not used | on / off |
* | A=1FFF B=F1FF C=FF1F D=FFF1 | 1=0FF 2=F0F 3=FF0 | 000 | on=10 off=01 |
* +-----------------------------+-------------------+----------+--------------+
*
* Source: http://www.the-intruder.net/funksteckdosen-von-rev-uber-arduino-ansteuern/
*
* @param sGroup Name of the switch group (A..D, resp. a..d)
* @param nDevice Number of the switch itself (1..3)
* @param bStatus Whether to switch on (true) or off (false)
*
* @return char[13], representing a tristate code word of length 12
*/
char *RCSwitch::getCodeWordD(char sGroup, int nDevice, bool bStatus)
{
static char sReturn[13];
int nReturnPos = 0;
// sGroup must be one of the letters in "abcdABCD"
int nGroup = (sGroup >= 'a') ? (int)sGroup - 'a' : (int)sGroup - 'A';
if (nGroup < 0 || nGroup > 3 || nDevice < 1 || nDevice > 3)
{
return 0;
}
for (int i = 0; i < 4; i++)
{
sReturn[nReturnPos++] = (nGroup == i) ? '1' : 'F';
}
for (int i = 1; i <= 3; i++)
{
sReturn[nReturnPos++] = (nDevice == i) ? '1' : 'F';
}
sReturn[nReturnPos++] = '0';
sReturn[nReturnPos++] = '0';
sReturn[nReturnPos++] = '0';
sReturn[nReturnPos++] = bStatus ? '1' : '0';
sReturn[nReturnPos++] = bStatus ? '0' : '1';
sReturn[nReturnPos] = '\0';
return sReturn;
}
/**
* @param sCodeWord a tristate code word consisting of the letter 0, 1, F
*/
void RCSwitch::sendTriState(const char *sCodeWord)
{
// turn the tristate code word into the corresponding bit pattern, then send it
unsigned long code = 0;
unsigned int length = 0;
for (const char *p = sCodeWord; *p; p++)
{
code <<= 2L;
switch (*p)
{
case '0':
// bit pattern 00
break;
case 'F':
// bit pattern 01
code |= 1L;
break;
case '1':
// bit pattern 11
code |= 3L;
break;
}
length += 2;
}
this->send(code, length);
}
/**
* @param sBitString a binary string consisting of the numbers 0, 1
*/
void RCSwitch::send(const char *sBitString)
{
if (this->nTransmitterPin == -1)
return;
#ifndef RCSwitchDisableReceiving
// make sure the receiver is disabled while we transmit
int nReceiverInterrupt_backup = nReceiverInterrupt;
if (nReceiverInterrupt_backup != -1)
{
this->disableReceive();
}
#endif
for (int nRepeat = 0; nRepeat < nRepeatTransmit; nRepeat++)
{
// transmit sync bits at the beginning if they are defined
if (protocol.sync.high > 0 || protocol.sync.low > 0)
{
this->transmit(protocol.sync);
}
// transmit the data bits
for (const char *p = sBitString; *p; p++)
{
if (*p != '0')
{
this->transmit(protocol.one);
}
else
{
this->transmit(protocol.zero);
}
}
// transmit the pause bits at the end
this->transmit(protocol.pause);
}
// Disable transmit after sending (i.e., for inverted protocols)
digitalWrite(this->nTransmitterPin, LOW);
#ifndef RCSwitchDisableReceiving
// enable receiver again if we just disabled it
if (nReceiverInterrupt_backup != -1)
{
this->enableReceive(nReceiverInterrupt_backup);
}
#endif
}
/**
* Transmit the first 'length' bits of the integer 'code'. The
* bits are sent from MSB to LSB, i.e., first the bit at position length-1,
* then the bit at position length-2, and so on, till finally the bit at position 0.
*/
void RCSwitch::send(unsigned long code, unsigned int length)
{
if (this->nTransmitterPin == -1)
return;
#ifndef RCSwitchDisableReceiving
// make sure the receiver is disabled while we transmit
int nReceiverInterrupt_backup = nReceiverInterrupt;
if (nReceiverInterrupt_backup != -1)
{
this->disableReceive();
}
#endif
for (int nRepeat = 0; nRepeat < nRepeatTransmit; nRepeat++)
{
// transmit sync bits at the beginning if they are defined
if (protocol.sync.high > 0 || protocol.sync.low > 0)
{
this->transmit(protocol.sync);
}
if (this->protocol.protocolId == 11)
//If protocol is 11 (Cixi Yidong), transmission is tricky
{
//First the upper 16 bits (remote ID)
for (int i = 24 - 1; i >= 8; i--)
{
if (code & (1L << i))
this->transmit(protocol.one);
else
this->transmit(protocol.zero);
}
//After that the inverted upper 16 bits (remote ID)
for (int i = 24 - 1; i >= 8; i--)
{
if (~code & (1L << i))
this->transmit(protocol.one);
else
this->transmit(protocol.zero);
}
//Last the remaining 8 bits (buttons+command)
for (int i = 8 - 1; i >= 0; i--)
{
if (code & (1L << i))
this->transmit(protocol.one);
else
this->transmit(protocol.zero);
}
}
else if (this->protocol.protocolId == 13) {
// Protocol 13 (Shi Qiong) requires an extra "1" bit transmission at the beginning
this->transmit(protocol.one); // Send the 1st "1" bit
for (int i = length - 2; i >= 0; i--)
{
if (code & (1L << i))
this->transmit(protocol.one);
else
this->transmit(protocol.zero);
}
}
else
{
for (int i = length - 1; i >= 0; i--)
{
if (code & (1L << i))
this->transmit(protocol.one);
else
this->transmit(protocol.zero);
}
}
this->transmit(protocol.pause);
}
// Disable transmit after sending (i.e., for inverted protocols)
digitalWrite(this->nTransmitterPin, LOW);
#ifndef RCSwitchDisableReceiving
// enable receiver again if we just disabled it
if (nReceiverInterrupt_backup != -1)
{
this->enableReceive(nReceiverInterrupt_backup);
}
#endif
}
/**
* Transmit a single high-low pulse.
*/
void RCSwitch::transmit(HighLow pulses)
{
uint8_t firstLogicLevel = (this->protocol.invertedSignal) ? LOW : HIGH;
uint8_t secondLogicLevel = (this->protocol.invertedSignal) ? HIGH : LOW;
// delayMicroseconds doesn't support values above 16383
// so use a while loop instead of delayMicroseconds
unsigned long microsDelayHigh = this->protocol.pulseLength * pulses.high;
unsigned long microsDelayLow = this->protocol.pulseLength * pulses.low;
unsigned long startMicros = 0;
// transmit "high" or the inverted signal
digitalWrite(this->nTransmitterPin, firstLogicLevel);
startMicros = micros();
while (micros() - startMicros < microsDelayHigh)
continue;
// transmit "low" or the inverted signal
digitalWrite(this->nTransmitterPin, secondLogicLevel);
startMicros = micros();
while (micros() - startMicros < microsDelayLow)
continue;
}
#ifndef RCSwitchDisableReceiving
/**
* Enable receiving data
*/
void RCSwitch::enableReceive(int interrupt)
{
this->nReceiverInterrupt = interrupt;
this->enableReceive();
}
void RCSwitch::enableReceive()
{
if (this->nReceiverInterrupt != -1)
{
RCSwitch::nReceivedValue = 0;
RCSwitch::nReceivedBitlength = 0;
#if defined(RaspberryPi) // Raspberry Pi
wiringPiISR(this->nReceiverInterrupt, INT_EDGE_BOTH, &handleInterrupt);
#else // Arduino
attachInterrupt(this->nReceiverInterrupt, handleInterrupt, CHANGE);
#endif
}
}
int RCSwitch::getReceiverInterrupt()
{
return RCSwitch::nReceiverInterrupt;
}
/**
* Disable receiving data
*/
void RCSwitch::disableReceive()
{
#ifndef RaspberryPi // Arduino
detachInterrupt(this->nReceiverInterrupt);
#endif // For Raspberry Pi (wiringPi) you can't unregister the ISR
this->nReceiverInterrupt = -1;
}
bool RCSwitch::available()
{
return RCSwitch::nReceivedValue != 0;
}
void RCSwitch::resetAvailable()
{
RCSwitch::nReceivedValue = 0;
}
unsigned long RCSwitch::getReceivedValue()
{
return RCSwitch::nReceivedValue;
}
unsigned int RCSwitch::getReceivedBitlength()
{
return RCSwitch::nReceivedBitlength;
}
unsigned int RCSwitch::getReceivedDelay()
{
return RCSwitch::nReceivedDelay;
}
unsigned int RCSwitch::getReceivedProtocol()
{
return RCSwitch::nReceivedProtocol;
}
/* use getReceivedRawdata() to output the the raw timings data*/
unsigned int *RCSwitch::getReceivedRawdata()
{
return RCSwitch::timings_copy;
}
/* use getReceivedBitlength() to output the number of raw bits */
char *RCSwitch::getReceivedRawBits()
{
return RCSwitch::receivedBits;
}
/* helper function for the receiveProtocol method */
static inline unsigned int diff(int A, int B)
{
return abs(A - B);
}
/* helper function for debugging decoding binary inputs (used in the receiveProtocol method) */
char *RCSwitch::dec2binWzerofill(unsigned long Dec, unsigned int bitLength)
{
static char bin[64];
unsigned int i = 0;
while (Dec > 0)
{
bin[32 + i++] = ((Dec & 1) > 0) ? '1' : '0';
Dec = Dec >> 1;
}
for (unsigned int j = 0; j < bitLength; j++)
{
if (j >= bitLength - i)
{
bin[j] = bin[31 + i - (j - (bitLength - i))];
}
else
{
bin[j] = '0';
}
}
bin[bitLength] = '\0';
return bin;
}
/**
*
*/
bool RECEIVE_ATTR RCSwitch::receiveProtocol(const int p, unsigned int changeCount)
{
if (changeCount <= 7)
{ // ignore very short transmissions: no device sends them, so this must be noise
return false;
}
#if defined(ESP8266) || defined(ESP32)
const Protocol &pro = proto[p - 1];
#else
Protocol pro;
memcpy_P(&pro, &proto[p - 1], sizeof(Protocol));
#endif
// Checking if protocol's changeCount/number of timings deffinition is matching
#ifdef DEBUG
Serial.print(F("Change count/number of timings is: "));
Serial.println(changeCount);
#endif
if (changeCount != pro.changeCount)
{
return false;
}
unsigned long code = 0;
// Assuming the longer pulse length is the pulse captured in timings[0]
const unsigned int pauseLengthInPulses = ((pro.pause.low) > (pro.pause.high)) ? (pro.pause.low) : (pro.pause.high);
// Caluclate the pulse length in microseconds from the pause/gap stored in the firts place of the RCSwitch::timings[] array
// divided by the longer part of the pause/end part of the protocol parameters. It should be near to the protocol specified pulse length.
const unsigned int calculatedPulseLength = RCSwitch::timings[0] / pauseLengthInPulses;
// First test for the pause length. The pause/end length is protocol specific.
// The first place of the received timings array holds the pause length (low period). It is compared to the protocol specified low period length.
unsigned int pauseLowDuration;
pauseLowDuration = (pro.invertedSignal) ? (pro.pulseLength * pro.pause.high) : (pro.pulseLength * pro.pause.low);
const unsigned int pauseTolerance = (pauseLowDuration * (RCSwitch::nReceiveTolerance / 100.0)) * 0.7; //70% of the general tolerance is enough here
#ifdef DEBUG
Serial.print(F("Protocol pause/end LOW duration: "));
Serial.println(pauseLowDuration);
Serial.print(F("First value in timing array: "));
Serial.println(RCSwitch::timings[0]);
Serial.print(F("Difference: "));
Serial.println(diff(RCSwitch::timings[0], pauseLowDuration));
Serial.print(F("Pause tolerance: "));
Serial.println(pauseTolerance);
#endif
if ((pauseLowDuration > 0) && diff(RCSwitch::timings[0], pauseLowDuration) < pauseTolerance)
{
#ifdef DEBUG
Serial.print(F("Captured pause/end length is matching the protocol: "));
Serial.println(p);
#endif
}
else { // If the protocol deffinition is not matching the pause length
return false;
}
// Calculate the different tolerance values (RCSwitch::nReceiveTolerance % of the low or high durations)
const unsigned int syncLowTolerance = calculatedPulseLength * pro.sync.low * (RCSwitch::nReceiveTolerance / 100.0);
const unsigned int syncHighTolerance = calculatedPulseLength * pro.sync.high * (RCSwitch::nReceiveTolerance / 100.0);
const unsigned int oneLowTolerance = calculatedPulseLength * pro.one.low * (RCSwitch::nReceiveTolerance / 100.0);
const unsigned int oneHighTolerance = calculatedPulseLength * pro.one.high * (RCSwitch::nReceiveTolerance / 100.0);
const unsigned int zeroLowTolerance = calculatedPulseLength * pro.zero.low * (RCSwitch::nReceiveTolerance / 100.0);
const unsigned int zeroHighTolerance = calculatedPulseLength * pro.zero.high * (RCSwitch::nReceiveTolerance / 100.0);
// store bits in the receivedBits char array (to support longer frames)
// if we have sync bits (like Nexa), don't count the initial pause bit and the two sync bits
// otherwise, just ignore the initial pause bit
// const unsigned int receivedBitlength = (pro.sync.high > 0 && pro.sync.low > 0) ? (changeCount - 3) / 2 : (changeCount - 1) / 2;
unsigned int receivedBitlength;
if (pro.sync.high > 0 && pro.sync.low > 0) {
switch (p)
{
case 11: // Cixi Yidong has special long sync sequence
receivedBitlength = (changeCount - 7) / 2;
break;
default:
receivedBitlength = (changeCount - 3) / 2;
break;
}
} else {
receivedBitlength = (changeCount - 1) / 2;
}
unsigned int receivedBitsPos = 0;
/*
* For protocols that start low, the sync/preamble period looks like
* _________
* _____________| |XXXXXXXXXXXX|
*
* |--1st dur--|-2nd dur-|-Start data-|
*
* The 3rd saved duration starts the data.
*
*
* For protocols that start high, the sync/preamble period looks like
*
* ______________
* | |____________|XXXXXXXXXXXXX|
*
* |-filtered out-|--1st dur--|--Start data--|
*
* The 2nd saved duration starts the data
*
*
* For protocol 11 (Cixi Yidong), the sync/preamble is special
*
* __ __ __________
* _________| || || |____________|XXXXXXXXXXXXX|
* 0 1 2 3 4 5 6 7
*
* so the first useful data bit is at position 7, sync starts at position 5 in the timings array
* 0 is 20ms, 1,2,3,4th values represent some pulses. The controlled socket can work without those.
*/
unsigned int firstDataTiming;
switch (p)
{
// For protocol 11 (Cixi Yidong), sync starts at position 5 (firstDataTiming might include the SYNC bit)
case 11:
firstDataTiming = 5;
break;
default:
firstDataTiming = (pro.invertedSignal) ? (2) : (1);
break;
}
#ifdef DEBUG
if (p > 0) // debugging protocols > x
{
Serial.print(F("Testing if this is protocol "));
Serial.print(p);
Serial.print(F(" using "));
Serial.print(changeCount);
Serial.print(F(" timings. PauseLengthInPulses: "));
Serial.print(pauseLengthInPulses);
Serial.print(F(". Calculated pulse length: "));
Serial.print(calculatedPulseLength);
Serial.print(F(" us"));
Serial.print(F(". Tolerance values (us): "));
Serial.print(syncLowTolerance);
Serial.print(F("/"));
Serial.print(syncHighTolerance);
Serial.print(F("/"));
Serial.print(oneLowTolerance);
Serial.print(F("/"));
Serial.print(oneHighTolerance);
Serial.print(F("/"));
Serial.print(zeroLowTolerance);
Serial.print(F("/"));
Serial.print(zeroHighTolerance);
Serial.print(F(". Received Bitlength: "));
Serial.print(receivedBitlength);
Serial.print(F(". First Data Timing Index: "));
Serial.print(firstDataTiming);
Serial.println();
Serial.print(F("Raw timing data: "));
for (unsigned int p = 0; p < changeCount; p++)
{
Serial.print(RCSwitch::timings[p]);
Serial.print(F(","));
}
Serial.println();
Serial.println(F("Starting timings interpretation as bit stream..."));
}
#endif
for (unsigned int i = firstDataTiming; i < changeCount - 1; i += 2)
{
// if the protocol contains a sync bit, check for it (e.g. Nexa)
if ((pro.sync.high > 0 && pro.sync.low > 0) &&
diff(RCSwitch::timings[i], calculatedPulseLength * pro.sync.high) < syncHighTolerance &&
diff(RCSwitch::timings[i + 1], calculatedPulseLength * pro.sync.low) < syncLowTolerance)