lib/rc-switch.c

changeset 25
5e0695f6add5
parent 24
873786a20a61
child 28
32ed1ea4d0b6
equal deleted inserted replaced
24:873786a20a61 25:5e0695f6add5
39 int rcTransmitterPin = -1; 39 int rcTransmitterPin = -1;
40 int rcPulseLength = 350; 40 int rcPulseLength = 350;
41 int rcRepeatTransmit = 10; 41 int rcRepeatTransmit = 10;
42 int rcProtocol = 1; 42 int rcProtocol = 1;
43 43
44 //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" };
46 const char TYPE_C_CODE[16][5] = { "0000", "F000", "0F00", "FF00", "00F0", "F0F0", "0FF0", "FFF0",
47 "000F", "F00F", "0F0F", "FF0F", "00FF", "F0FF", "0FFF", "FFFF" };
48 const char TYPE_D_CODE[5][2][9] = { { "11100001", "11110000" }, { "00000000", "00010001" }, { "10000010", "10010011" },
49 { "11000011", "11010010" }, { "01000001", "01010000" } };
50
44 51
45 char *getCodeWordA(char*, char*, bool); 52 char *getCodeWordA(char*, char*, bool);
46 char *getCodeWordB(int, int, bool); 53 char *getCodeWordB(int, int, bool);
47 char *getCodeWordC(char, int, int, bool); 54 char *getCodeWordC(char, int, int, bool);
48 char *getCodeWordD(char, int, bool); 55
56 char *getCodeWordE(char, int, bool);
49 void sendTriState(char*); 57 void sendTriState(char*);
50 void transmit(int, int); 58 void transmit(int, int);
51 void send0(void); 59 void send0(void);
52 void send1(void); 60 void send1(void);
53 void sendT0(void); 61 void sendT0(void);
138 } 146 }
139 147
140 148
141 149
142 /* 150 /*
143 * Switch a remote switch on (Type D REV) 151 * Switch a remote switch on (Type E REV)
144 * 152 *
145 * @param sGroup Code of the switch group (A,B,C,D) 153 * @param sGroup Code of the switch group (A,B,C,D)
146 * @param nDevice Number of the switch itself (1..3) 154 * @param nDevice Number of the switch itself (1..3)
147 */ 155 * @param bStatus Status to toggle to
148 void switchOn_D(char sGroup, int nDevice) { 156 */
149 sendTriState( getCodeWordD(sGroup, nDevice, true) ); 157 void toggleTypeE(char sGroup, int nDevice, bool bStatus) {
150 } 158 sendTriState( getCodeWordE(sGroup, nDevice, bStatus) );
151
152
153
154 /*
155 * Switch a remote switch off (Type D REV)
156 *
157 * @param sGroup Code of the switch group (A,B,C,D)
158 * @param nDevice Number of the switch itself (1..3)
159 */
160 void switchOff_D(char sGroup, int nDevice) {
161 sendTriState( getCodeWordD(sGroup, nDevice, false) );
162 } 159 }
163 160
164 161
165 162
166 /* 163 /*
167 * Switch a remote switch on (Type C Intertechno) 164 * Switch a remote switch on (Type C Intertechno)
168 * 165 *
169 * @param sFamily Familycode (a..f) 166 * @param sFamily Familycode (a..f)
170 * @param nGroup Number of group (1..4) 167 * @param nGroup Number of group (1..4)
171 * @param nDevice Number of device (1..4) 168 * @param nDevice Number of device (1..4)
172 */ 169 * @param bStatus Status to toggle to
173 void switchOn_C(char sFamily, int nGroup, int nDevice) { 170 */
174 sendTriState( getCodeWordC(sFamily, nGroup, nDevice, true) ); 171 void toggleTypeC(char sFamily, int nGroup, int nDevice, bool bStatus) {
175 } 172 sendTriState( getCodeWordC(sFamily, nGroup, nDevice, bStatus) );
176 173 }
177 174
178 175
179 /** 176
180 * Switch a remote switch off (Type C Intertechno) 177 /*
181 * 178 * Switch a remote switch on/off (Type B with two rotary/sliding switches)
182 * @param sFamily Familycode (a..f) 179 *
183 * @param nGroup Number of group (1..4) 180 * @param iGroup Number of the switch group (1..4)
184 * @param nDevice Number of device (1..4) 181 * @param iDevice Number of the switch itself (1..4)
185 */ 182 * @param bStatus Status to toggle to
186 void switchOff_C(char sFamily, int nGroup, int nDevice) { 183 */
187 sendTriState( getCodeWordC(sFamily, nGroup, nDevice, false) ); 184 void toggleTypeB(int iGroup, int iDevice, bool bStatus)
188 } 185 {
189 186 char *str = xstrcpy(getCodeWordB(iGroup, iDevice, bStatus));
190 187
191 188 // save(TYPE_B);
192 /* 189 sendTriState( str );
193 * Switch a remote switch on (Type B with two rotary/sliding switches) 190 // load();
194 * 191 free(str);
195 * @param nAddressCode Number of the switch group (1..4) 192 }
196 * @param nChannelCode Number of the switch itself (1..4) 193
197 */ 194
198 void switchOn_B(int nAddressCode, int nChannelCode) { 195
199 sendTriState( getCodeWordB(nAddressCode, nChannelCode, true) ); 196 /*
200 }
201
202
203
204 /*
205 * Switch a remote switch off (Type B with two rotary/sliding switches)
206 *
207 * @param nAddressCode Number of the switch group (1..4)
208 * @param nChannelCode Number of the switch itself (1..4)
209 */
210 void switchOff_B(int nAddressCode, int nChannelCode) {
211 sendTriState( getCodeWordB(nAddressCode, nChannelCode, false) );
212 }
213
214
215
216 /**
217 * Deprecated, use switchOn(char* sGroup, char* sDevice) instead!
218 * Switch a remote switch on (Type A with 10 pole DIP switches) 197 * Switch a remote switch on (Type A with 10 pole DIP switches)
219 * 198 *
220 * @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") 199 * @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")
221 * @param nChannelCode Number of the switch itself (1..5) 200 * @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")
222 */ 201 * @param bStatus Status to toggle to
223 /*void RCSwitch::switchOn(char* sGroup, int nChannel) { 202 */
224 char* code[6] = { (char *)"00000", (char *)"10000", (char *)"01000", (char *)"00100", (char *)"00010", (char *)"00001" }; 203 void toggleTypeA(char* sGroup, char* sDevice, bool bStatus) {
225 this->switchOn(sGroup, code[nChannel]); 204 sendTriState( getCodeWordA(sGroup, sDevice, bStatus) );
226 }*/
227
228 /**
229 * Deprecated, use switchOff(char* sGroup, char* sDevice) instead!
230 * Switch a remote switch off (Type A with 10 pole DIP switches)
231 *
232 * @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")
233 * @param nChannelCode Number of the switch itself (1..5)
234 */
235 /*void RCSwitch::switchOff(char* sGroup, int nChannel) {
236 char* code[6] = { (char *)"00000", (char *)"10000", (char *)"01000", (char *)"00100", (char *)"00010", (char *)"00001" };
237 this->switchOff(sGroup, code[nChannel]);
238 }*/
239
240 /**
241 * Switch a remote switch on (Type A with 10 pole DIP switches)
242 *
243 * @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")
244 * @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")
245 */
246 void switchOn_A(char* sGroup, char* sDevice) {
247 sendTriState( getCodeWordA(sGroup, sDevice, true) );
248 }
249
250
251
252 /**
253 * Switch a remote switch off (Type A with 10 pole DIP switches)
254 *
255 * @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")
256 * @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")
257 */
258 void switchOff_A(char* sGroup, char* sDevice) {
259 sendTriState( getCodeWordA(sGroup, sDevice, false) );
260 } 205 }
261 206
262 207
263 208
264 /* 209 /*
280 char *getCodeWordB(int nAddressCode, int nChannelCode, bool bStatus) 225 char *getCodeWordB(int nAddressCode, int nChannelCode, bool bStatus)
281 { 226 {
282 int i, nReturnPos = 0; 227 int i, nReturnPos = 0;
283 static char sReturn[13]; 228 static char sReturn[13];
284 229
285 char* code[5] = { (char *)"FFFF", (char *)"0FFF", (char *)"F0FF", (char *)"FF0F", (char *)"FFF0" };
286 if (nAddressCode < 1 || nAddressCode > 4 || nChannelCode < 1 || nChannelCode > 4) { 230 if (nAddressCode < 1 || nAddressCode > 4 || nChannelCode < 1 || nChannelCode > 4) {
287 return '\0'; 231 return '\0';
288 } 232 }
289 for (i = 0; i<4; i++) { 233 for (i = 0; i<4; i++) {
290 sReturn[nReturnPos++] = code[nAddressCode][i]; 234 sReturn[nReturnPos++] = TYPE_B_CODE[nAddressCode][i];
291 } 235 }
292 236
293 for (i = 0; i<4; i++) { 237 for (i = 0; i<4; i++) {
294 sReturn[nReturnPos++] = code[nChannelCode][i]; 238 sReturn[nReturnPos++] = TYPE_B_CODE[nChannelCode][i];
295 } 239 }
296 240
297 sReturn[nReturnPos++] = 'F'; 241 sReturn[nReturnPos++] = 'F';
298 sReturn[nReturnPos++] = 'F'; 242 sReturn[nReturnPos++] = 'F';
299 sReturn[nReturnPos++] = 'F'; 243 sReturn[nReturnPos++] = 'F';
300 244 sReturn[nReturnPos++] = bStatus ? 'F' : '0';
301 if (bStatus) {
302 sReturn[nReturnPos++] = 'F';
303 } else {
304 sReturn[nReturnPos++] = '0';
305 }
306
307 sReturn[nReturnPos] = '\0'; 245 sReturn[nReturnPos] = '\0';
246
308 return sReturn; 247 return sReturn;
309 } 248 }
310 249
311 250
312 251
317 * 256 *
318 */ 257 */
319 char *getCodeWordA(char* sGroup, char* sDevice, bool bOn) 258 char *getCodeWordA(char* sGroup, char* sDevice, bool bOn)
320 { 259 {
321 static char sDipSwitches[13]; 260 static char sDipSwitches[13];
322 int i = 0; 261 int i, j = 0;
323 int j = 0;
324 262
325 for (i=0; i < 5; i++) { 263 for (i=0; i < 5; i++) {
326 if (sGroup[i] == '0') { 264 sDipSwitches[j++] = (sGroup[i] == '0') ? 'F' : '0';
327 sDipSwitches[j++] = 'F';
328 } else {
329 sDipSwitches[j++] = '0';
330 }
331 } 265 }
332 266
333 for (i=0; i < 5; i++) { 267 for (i=0; i < 5; i++) {
334 if (sDevice[i] == '0') { 268 sDipSwitches[j++] = (sDevice[i] == '0') ? 'F' : '0';
335 sDipSwitches[j++] = 'F'; 269 }
336 } else { 270
337 sDipSwitches[j++] = '0';
338 }
339 }
340
341 if ( bOn ) { 271 if ( bOn ) {
342 sDipSwitches[j++] = '0'; 272 sDipSwitches[j++] = '0';
343 sDipSwitches[j++] = 'F'; 273 sDipSwitches[j++] = 'F';
344 } else { 274 } else {
345 sDipSwitches[j++] = 'F'; 275 sDipSwitches[j++] = 'F';
357 */ 287 */
358 char *getCodeWordC(char sFamily, int nGroup, int nDevice, bool bStatus) 288 char *getCodeWordC(char sFamily, int nGroup, int nDevice, bool bStatus)
359 { 289 {
360 static char sReturn[13]; 290 static char sReturn[13];
361 int i, nReturnPos = 0; 291 int i, nReturnPos = 0;
292
293 if (sFamily < 'a') {
294 // To also enable capital 'A' to 'F'
295 sFamily += 32;
296 }
297
298 if ( sFamily < 'a' || sFamily > 'f' || nGroup < 1 || nGroup > 4 || nDevice < 1 || nDevice > 4) {
299 return '\0';
300 }
362 301
363 if ( (uint8_t)sFamily < 97 || (uint8_t)sFamily > 112 || nGroup < 1 || nGroup > 4 || nDevice < 1 || nDevice > 4) {
364 return '\0';
365 }
366
367 char* sDeviceGroupCode = dec2binWzerofill( (nDevice-1) + (nGroup-1)*4, 4 );
368 char familycode[16][5] = { "0000", "F000", "0F00", "FF00", "00F0", "F0F0", "0FF0", "FFF0", "000F", "F00F", "0F0F", "FF0F", "00FF", "F0FF", "0FFF", "FFFF" };
369 for (i = 0; i<4; i++) { 302 for (i = 0; i<4; i++) {
370 sReturn[nReturnPos++] = familycode[ (int)sFamily - 97 ][i]; 303 sReturn[nReturnPos++] = TYPE_C_CODE[ sFamily - 'a' ][i];
371 } 304 }
305
306 char *sDeviceGroupCode = dec2binWzerofill( (nDevice-1) + (nGroup-1)*4, 4 );
372 for (i = 0; i<4; i++) { 307 for (i = 0; i<4; i++) {
373 sReturn[nReturnPos++] = (sDeviceGroupCode[3-i] == '1' ? 'F' : '0'); 308 sReturn[nReturnPos++] = (sDeviceGroupCode[3-i] == '1' ? 'F' : '0');
374 } 309 }
375 310
376 sReturn[nReturnPos++] = '0'; 311 sReturn[nReturnPos++] = '0';
377 sReturn[nReturnPos++] = 'F'; 312 sReturn[nReturnPos++] = 'F';
378 sReturn[nReturnPos++] = 'F'; 313 sReturn[nReturnPos++] = 'F';
379 314 sReturn[nReturnPos++] = bStatus ? 'F' : '0';
380 if (bStatus) {
381 sReturn[nReturnPos++] = 'F';
382 } else {
383 sReturn[nReturnPos++] = '0';
384 }
385
386 sReturn[nReturnPos] = '\0'; 315 sReturn[nReturnPos] = '\0';
316
317 return sReturn;
318 }
319
320
321
322 /*
323 * Decoding for the Quigg Switch Type
324 *
325 * Returns a char[22], representing the States to be send.
326 * A Code Word consists of 1 start bit, 12 address bits and 8 command data bits.
327 * A Code Bit can have 2 different states: "0" (low), "1" (high)
328 *
329 * +--------------+--------------------------------+------------------------------+
330 * | 1 bits start | 12 bits address (device group) | 8 bits (command/switch data) |
331 * | 1 | 110011001100 | 00010001 |
332 * +--------------+--------------------------------+------------------------------+
333 *
334 * Source: https://github.com/d-a-n/433-codes/blob/master/database.md#quigg
335 *
336 * @param sGroup 12-bit Binary ID of the Device Group
337 * @param nDevice Number of the switch itself (1..4, or 0 to switch the entire Group)
338 * @param bStatus Wether to switch on (true) or off (false)
339 *
340 * @return char[22]
341 */
342 char *getCodeWordD(char *sGroup, int nDevice, bool bStatus)
343 {
344 static char sReturn[22];
345 int i, nReturnPos = 0;
346
347 /* Startbit */
348 sReturn[nReturnPos++] = '1';
349
350 /* 12 bit Group */
351 for (i = 0; i < 12; ++i) {
352 sReturn[nReturnPos++] = sGroup[i];
353 }
354
355 /* 8 Bit Device Identifier + Status (undividable!) */
356 for (i = 0; i < 8; ++i) {
357 sReturn[nReturnPos++] = TYPE_D_CODE[nDevice][bStatus][i];
358 }
359 sReturn[nReturnPos] = 0;
360
387 return sReturn; 361 return sReturn;
388 } 362 }
389 363
390 364
391 365
407 * @param nDevice Number of the switch itself (1..3) 381 * @param nDevice Number of the switch itself (1..3)
408 * @param bStatus Wether to switch on (true) or off (false) 382 * @param bStatus Wether to switch on (true) or off (false)
409 * 383 *
410 * @return char[13] 384 * @return char[13]
411 */ 385 */
412 char *getCodeWordD(char sGroup, int nDevice, bool bStatus){ 386 char *getCodeWordE(char sGroup, int nDevice, bool bStatus){
413 static char sReturn[13]; 387 static char sReturn[13];
414 int i, nReturnPos = 0; 388 int i, nReturnPos = 0;
415 389
416 // Building 4 bits address 390 // Building 4 bits address
417 // (Potential problem if dec2binWcharfill not returning correct string) 391 // (Potential problem if dec2binWcharfill not returning correct string)

mercurial