26 #include "../config.h" |
26 #include "../config.h" |
27 #include "mbselib.h" |
27 #include "mbselib.h" |
28 |
28 |
29 #ifdef HAVE_WIRINGPI_H |
29 #ifdef HAVE_WIRINGPI_H |
30 |
30 |
|
31 |
|
32 #define TYPE_UNDEF 0 |
|
33 #define TYPE_MINIMUM 0 |
|
34 #define TYPE_A 1 |
|
35 #define TYPE_B 2 |
|
36 #define TYPE_C 3 |
|
37 #define TYPE_D 4 |
|
38 #define TYPE_E 3 // TODO: Which Protocol does REV use? |
|
39 #define TYPE_MAXIMUM 4 |
|
40 |
|
41 // Number of maximum High/Low changes per packet. |
|
42 // We can handle up to (unsigned long) => 32 bit * 2 H/L changes per bit + 2 for sync |
|
43 #define RCSWITCH_MAX_CHANGES 67 |
|
44 |
|
45 // i.e. ProtocolCount + 1 (for TYPE_UNDEF) |
|
46 #define MAX_PROTOCOLS 5 |
|
47 |
|
48 #define PROTOCOL_A_SYNC_FACTOR 31 |
|
49 #define PROTOCOL_A_ZERO_FIRST_CYCLES 1 |
|
50 #define PROTOCOL_A_ZERO_SECOND_CYCLES 3 |
|
51 #define PROTOCOL_A_ONE_FIRST_CYCLES 3 |
|
52 #define PROTOCOL_A_ONE_SECOND_CYCLES 1 |
|
53 #define PROTOCOL_A_HIGH_FIRST true |
|
54 |
|
55 #define PROTOCOL_B_SYNC_FACTOR 10 |
|
56 #define PROTOCOL_B_ZERO_FIRST_CYCLES 1 |
|
57 #define PROTOCOL_B_ZERO_SECOND_CYCLES 2 |
|
58 #define PROTOCOL_B_ONE_FIRST_CYCLES 2 |
|
59 #define PROTOCOL_B_ONE_SECOND_CYCLES 1 |
|
60 #define PROTOCOL_B_HIGH_FIRST true |
|
61 |
|
62 #define PROTOCOL_C_SYNC_FACTOR 71 |
|
63 #define PROTOCOL_C_ZERO_FIRST_CYCLES 4 |
|
64 #define PROTOCOL_C_ZERO_SECOND_CYCLES 11 |
|
65 #define PROTOCOL_C_ONE_FIRST_CYCLES 9 |
|
66 #define PROTOCOL_C_ONE_SECOND_CYCLES 6 |
|
67 #define PROTOCOL_C_HIGH_FIRST true |
|
68 |
|
69 // I think, this will work for receive, however, I haven't tested, as I don't own a receiver... |
|
70 // As Type D doesn't sync acc. to https://github.com/d-a-n/433-codes/blob/master/database.md#quigg |
|
71 // the sync factor is totally undetermined. |
|
72 // Malte Diers, 22.11.2013 |
|
73 #define PROTOCOL_D_SYNC_FACTOR 1 |
|
74 #define PROTOCOL_D_ZERO_FIRST_CYCLES 1 |
|
75 #define PROTOCOL_D_ZERO_SECOND_CYCLES 2 |
|
76 #define PROTOCOL_D_ONE_FIRST_CYCLES 2 |
|
77 #define PROTOCOL_D_ONE_SECOND_CYCLES 1 |
|
78 #define PROTOCOL_D_HIGH_FIRST false |
|
79 |
|
80 |
|
81 #define PROTOCOL3_SYNC_FACTOR 71 |
|
82 #define PROTOCOL3_0_HIGH_CYCLES 4 |
|
83 #define PROTOCOL3_0_LOW_CYCLES 11 |
|
84 #define PROTOCOL3_1_HIGH_CYCLES 9 |
|
85 #define PROTOCOL3_1_LOW_CYCLES 6 |
|
86 |
|
87 |
|
88 |
31 unsigned long rcReceivedValue = 0; |
89 unsigned long rcReceivedValue = 0; |
32 unsigned int rcReceivedBitlength = 0; |
90 unsigned int rcReceivedBitlength = 0; |
33 unsigned int rcReceivedDelay = 0; |
91 unsigned int rcReceivedDelay = 0; |
34 unsigned int rcReceivedProtocol = 0; |
92 unsigned int rcReceivedProtocol = 0; |
35 int rcReceiveTolerance = 60; |
93 int rcReceiveTolerance = 60; |
36 int rcReceiverInterruptPin = -1; |
94 int rcReceiverInterruptPin = -1; |
37 |
95 |
38 unsigned int timings[RCSWITCH_MAX_CHANGES]; |
96 unsigned int timings[RCSWITCH_MAX_CHANGES]; |
39 int rcTransmitterPin = -1; |
97 int rcTransmitterPin = -1; |
40 int rcPulseLength = 350; |
98 int rcPulseLength = 350; // thermometers 2.4 msec = 2400 |
41 int rcRepeatTransmit = 10; |
99 int rcRepeatTransmit = 10; |
42 int rcProtocol = 1; |
100 int rcProtocol = 1; |
|
101 |
|
102 int backupProtocol; |
|
103 int backupPulseLength; |
|
104 int backupRepeatTransmit; |
|
105 |
43 |
106 |
44 //const char TYPE_A_CODE[ 6][6] = { "00000", "10000", "01000", "00100", "00010", "00001"}; |
107 //const char TYPE_A_CODE[ 6][6] = { "00000", "10000", "01000", "00100", "00010", "00001"}; |
45 const char TYPE_B_CODE[ 5][5] = { "FFFF", "0FFF", "F0FF", "FF0F", "FFF0" }; |
108 const char TYPE_B_CODE[ 5][5] = { "FFFF", "0FFF", "F0FF", "FF0F", "FFF0" }; |
46 const char TYPE_C_CODE[16][5] = { "0000", "F000", "0F00", "FF00", "00F0", "F0F0", "0FF0", "FFF0", |
109 const char TYPE_C_CODE[16][5] = { "0000", "F000", "0F00", "FF00", "00F0", "F0F0", "0FF0", "FFF0", |
47 "000F", "F00F", "0F0F", "FF0F", "00FF", "F0FF", "0FFF", "FFFF" }; |
110 "000F", "F00F", "0F0F", "FF0F", "00FF", "F0FF", "0FFF", "FFFF" }; |
48 const char TYPE_D_CODE[5][2][9] = { { "11100001", "11110000" }, { "00000000", "00010001" }, { "10000010", "10010011" }, |
111 const char TYPE_D_CODE[5][2][9] = { { "11100001", "11110000" }, { "00000000", "00010001" }, { "10000010", "10010011" }, |
49 { "11000011", "11010010" }, { "01000001", "01010000" } }; |
112 { "11000011", "11010010" }, { "01000001", "01010000" } }; |
|
113 /* Type A Type D */ |
|
114 const int PULSE_LENGTH[MAX_PROTOCOLS] = { 0, 350, 650, 100, 666, }; |
|
115 const int REPEAT_TRANSMIT[MAX_PROTOCOLS] = { 0, 10, 10, 10, 4, }; |
|
116 const int SYNC_FACTOR[MAX_PROTOCOLS] = { 0, PROTOCOL_A_SYNC_FACTOR, PROTOCOL_B_SYNC_FACTOR, PROTOCOL_C_SYNC_FACTOR, PROTOCOL_D_SYNC_FACTOR, }; |
|
117 const int ZERO_FIRST_CYCLES[MAX_PROTOCOLS] = { 0, PROTOCOL_A_ZERO_FIRST_CYCLES, PROTOCOL_B_ZERO_FIRST_CYCLES, PROTOCOL_C_ZERO_FIRST_CYCLES, PROTOCOL_D_ZERO_FIRST_CYCLES, }; |
|
118 const int ZERO_SECOND_CYCLES[MAX_PROTOCOLS] = { 0, PROTOCOL_A_ZERO_SECOND_CYCLES, PROTOCOL_B_ZERO_SECOND_CYCLES, PROTOCOL_C_ZERO_SECOND_CYCLES, PROTOCOL_D_ZERO_SECOND_CYCLES, }; |
|
119 const int ONE_FIRST_CYCLES[MAX_PROTOCOLS] = { 0, PROTOCOL_A_ONE_FIRST_CYCLES, PROTOCOL_B_ONE_FIRST_CYCLES, PROTOCOL_C_ONE_FIRST_CYCLES, PROTOCOL_D_ONE_FIRST_CYCLES, }; |
|
120 const int ONE_SECOND_CYCLES[MAX_PROTOCOLS] = { 0, PROTOCOL_A_ONE_SECOND_CYCLES, PROTOCOL_B_ONE_SECOND_CYCLES, PROTOCOL_C_ONE_SECOND_CYCLES, PROTOCOL_D_ONE_SECOND_CYCLES, }; |
|
121 const bool HIGH_FIRST[MAX_PROTOCOLS] = { 0, PROTOCOL_A_HIGH_FIRST, PROTOCOL_B_HIGH_FIRST, PROTOCOL_C_HIGH_FIRST, PROTOCOL_D_HIGH_FIRST, }; |
50 |
122 |
51 |
123 |
52 char *getCodeWordA(char*, char*, bool); |
124 char *getCodeWordA(char*, char*, bool); |
53 char *getCodeWordB(int, int, bool); |
125 char *getCodeWordB(int, int, bool); |
54 char *getCodeWordC(char, int, int, bool); |
126 char *getCodeWordC(char, int, int, bool); |
55 |
127 |
56 char *getCodeWordE(char, int, bool); |
128 char *getCodeWordE(char, int, bool); |
57 void sendTriState(char*); |
129 void sendTriState(char*); |
58 void transmit(int, int); |
130 void transmit(int, int, bool); |
59 void send0(void); |
131 void send0(void); |
60 void send1(void); |
132 void send1(void); |
61 void sendT0(void); |
133 void sendT0(void); |
62 void sendT1(void); |
134 void sendT1(void); |
63 void sendTF(void); |
135 void sendTF(void); |
64 void sendSync(void); |
136 void sendSync(void); |
65 bool receiveProtocol1(unsigned int); |
137 bool receiveProtocol1(unsigned int); |
66 bool receiveProtocol2(unsigned int); |
138 bool receiveProtocol2(unsigned int); |
67 bool receiveProtocol3(unsigned int); |
139 bool receiveProtocol3(unsigned int); |
68 void handleInterrupt(void); |
140 void handleInterrupt(void); |
69 char *dec2binWzerofill(unsigned long, unsigned int); |
|
70 char *dec2binWcharfill(unsigned long, unsigned int, char); |
141 char *dec2binWcharfill(unsigned long, unsigned int, char); |
71 |
142 |
|
143 void setReceiveTolerance(int); |
|
144 void setProtocol(int); |
|
145 |
|
146 void saveProtocol(int); |
|
147 void loadProtocol(void); |
72 |
148 |
73 |
149 |
74 |
150 |
75 /* |
151 /* |
76 * Sets the protocol to send. |
152 * Sets the protocol to send. |
77 */ |
153 */ |
78 void setProtocol(int nProtocol) { |
154 void setProtocol(int nProtocol) { |
79 rcProtocol = nProtocol; |
155 |
80 if (nProtocol == 1){ |
156 if ((nProtocol < TYPE_MINIMUM) || (nProtocol > TYPE_MAXIMUM)) { |
81 setPulseLength(350); |
157 return; |
82 } |
158 } |
83 else if (nProtocol == 2) { |
159 |
84 setPulseLength(650); |
160 rcProtocol = nProtocol; |
85 } |
161 rcPulseLength = PULSE_LENGTH[nProtocol]; |
86 else if (nProtocol == 3) { |
162 rcRepeatTransmit = REPEAT_TRANSMIT[nProtocol]; |
87 setPulseLength(100); |
|
88 } |
|
89 } |
|
90 |
|
91 |
|
92 |
|
93 /** |
|
94 * Sets the protocol to send with pulse length in microseconds. |
|
95 */ |
|
96 /*void RCSwitch::setProtocol(int nProtocol, int nPulseLength) { |
|
97 this->nProtocol = nProtocol; |
|
98 this->setPulseLength(nPulseLength); |
|
99 }*/ |
|
100 |
|
101 |
|
102 |
|
103 /* |
|
104 * Sets pulse length in microseconds |
|
105 */ |
|
106 void setPulseLength(int nPulseLength) { |
|
107 rcPulseLength = nPulseLength; |
|
108 } |
|
109 |
|
110 |
|
111 |
|
112 /* |
|
113 * Sets Repeat Transmits |
|
114 */ |
|
115 void setRepeatTransmit(int nRepeatTransmit) { |
|
116 rcRepeatTransmit = nRepeatTransmit; |
|
117 } |
163 } |
118 |
164 |
119 |
165 |
120 |
166 |
121 /* |
167 /* |
122 * Set Receiving Tolerance |
168 * Set Receiving Tolerance |
123 */ |
169 */ |
124 void setReceiveTolerance(int nPercent) { |
170 void setReceiveTolerance(int nPercent) { |
125 rcReceiveTolerance = nPercent; |
171 rcReceiveTolerance = nPercent; |
126 } |
172 } |
|
173 |
127 |
174 |
128 |
175 |
129 /* |
176 /* |
130 * Enable transmissions |
177 * Enable transmissions |
131 * |
178 * |
599 * __ |
662 * __ |
600 * Waveform Protocol 2: | |_ |
663 * Waveform Protocol 2: | |_ |
601 * _________ |
664 * _________ |
602 * Waveform Protocol 3: | |______ |
665 * Waveform Protocol 3: | |______ |
603 */ |
666 */ |
604 void send1(void) { |
667 //void send1(void) { |
605 if (rcProtocol == 1){ |
668 // if (rcProtocol == 1){ |
606 transmit(3,1); |
669 // transmit(3,1); |
607 } |
670 // } |
608 else if (rcProtocol == 2) { |
671 // else if (rcProtocol == 2) { |
609 transmit(2,1); |
672 // transmit(2,1); |
610 } |
673 // } |
611 else if (rcProtocol == 3) { |
674 // else if (rcProtocol == 3) { |
612 transmit(9,6); |
675 // transmit(9,6); |
613 } |
676 // } |
614 } |
677 //} |
615 |
678 |
616 |
679 |
617 |
680 |
618 /* |
681 /* |
619 * Sends a Tri-State "0" Bit |
682 * Sends a Tri-State "0" Bit |
620 * _ _ |
683 * _ _ |
621 * Waveform: | |___| |___ |
684 * Waveform: | |___| |___ |
622 */ |
685 */ |
623 void sendT0(void) { |
686 void sendT0(void) { |
624 transmit(1,3); |
687 transmit(ZERO_FIRST_CYCLES[rcProtocol], ZERO_SECOND_CYCLES[rcProtocol], HIGH_FIRST[rcProtocol]); |
625 transmit(1,3); |
688 transmit(ZERO_FIRST_CYCLES[rcProtocol], ZERO_SECOND_CYCLES[rcProtocol], HIGH_FIRST[rcProtocol]); |
|
689 // transmit(1,3,true); |
|
690 // transmit(1,3,true); |
626 } |
691 } |
627 |
692 |
628 |
693 |
629 |
694 |
630 /* |
695 /* |
631 * Sends a Tri-State "1" Bit |
696 * Sends a Tri-State "1" Bit |
632 * ___ ___ |
697 * ___ ___ |
633 * Waveform: | |_| |_ |
698 * Waveform: | |_| |_ |
634 */ |
699 */ |
635 void sendT1(void) { |
700 void sendT1(void) { |
636 transmit(3,1); |
701 transmit(ONE_FIRST_CYCLES[rcProtocol], ONE_SECOND_CYCLES[rcProtocol], HIGH_FIRST[rcProtocol]); |
637 transmit(3,1); |
702 transmit(ONE_FIRST_CYCLES[rcProtocol], ONE_SECOND_CYCLES[rcProtocol], HIGH_FIRST[rcProtocol]); |
|
703 // transmit(3,1,true); |
|
704 // transmit(3,1,true); |
638 } |
705 } |
639 |
706 |
640 |
707 |
641 |
708 |
642 /* |
709 /* |
643 * Sends a Tri-State "F" Bit |
710 * Sends a Tri-State "F" Bit |
644 * _ ___ |
711 * _ ___ |
645 * Waveform: | |___| |_ |
712 * Waveform: | |___| |_ |
646 */ |
713 */ |
647 void sendTF(void) { |
714 void sendTF(void) { |
648 transmit(1,3); |
715 transmit(ZERO_FIRST_CYCLES[rcProtocol], ZERO_SECOND_CYCLES[rcProtocol], HIGH_FIRST[rcProtocol]); |
649 transmit(3,1); |
716 transmit(ONE_FIRST_CYCLES[rcProtocol], ONE_SECOND_CYCLES[rcProtocol], HIGH_FIRST[rcProtocol]); |
|
717 // transmit(1,3,true); |
|
718 // transmit(3,1,true); |
650 } |
719 } |
651 |
720 |
652 |
721 |
653 |
722 |
654 /* |
723 /* |
655 * Sends a "Sync" Bit |
724 * Sends a "Sync" Bit |
656 * _ |
725 * _ |
657 * Waveform Protocol 1: | |_______________________________ |
726 * Waveform Protocol 1: | |_______________________________ |
658 * _ |
727 * _ |
659 * Waveform Protocol 2: | |__________ |
728 * Waveform Protocol 2: | |__________ |
660 * _ |
729 * ____ |
661 * Waveform Protocol 3: | |_______________________________________________________________________ |
730 * Waveform Protocol 3: | |_______________________________________________________________________ |
|
731 * |
|
732 * Waveform Protocol D: (none, just pause 80 msecs) |
662 */ |
733 */ |
663 void sendSync(void) { |
734 void sendSync(void) { |
664 |
735 |
665 if (rcProtocol == 1){ |
736 if (rcProtocol == TYPE_A) { |
666 transmit(1,31); |
737 transmit(1,31,true); |
667 } |
738 } else if (rcProtocol == TYPE_B) { |
668 else if (rcProtocol == 2) { |
739 transmit(1,10,true); |
669 transmit(1,10); |
740 } else if (rcProtocol == TYPE_C) { |
670 } |
741 transmit(4,71,true); |
671 else if (rcProtocol == 3) { |
742 } else if (rcProtocol == TYPE_D) { |
672 transmit(1,71); |
743 transmit(0,1,false); |
|
744 delayMicroseconds(80000); |
673 } |
745 } |
674 } |
746 } |
675 |
747 |
676 |
748 |
677 |
749 |