Better interface for rc-switch library code

Mon, 05 May 2014 20:25:20 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 05 May 2014 20:25:20 +0200
changeset 25
5e0695f6add5
parent 24
873786a20a61
child 26
9322c619c525

Better interface for rc-switch library code

lib/mbselib.h file | annotate | diff | comparison | revisions
lib/rc-switch.c file | annotate | diff | comparison | revisions
rc433/send.c file | annotate | diff | comparison | revisions
--- a/lib/mbselib.h	Mon May 05 13:49:53 2014 +0200
+++ b/lib/mbselib.h	Mon May 05 20:25:20 2014 +0200
@@ -119,14 +119,10 @@
 #define PROTOCOL3_1_LOW_CYCLES   6
 
 
-void switchOn_A(char *, char *);
-void switchOff_A(char *, char *);
-void switchOn_B(int, int);
-void switchOff_B(int, int);
-void switchOn_C(char, int, int);
-void switchOff_C(char, int, int);
-void switchOn_D(char, int);
-void switchOff_D(char, int);
+void toggleTypeA(char *, char *, bool);
+void toggleTypeB(int, int, bool);
+void toggleTypeC(char, int, int, bool);
+void toggleTypeE(char, int, bool);
 
 void enableReceiveIRQ(int interrupt);
 void enableReceive(void);
--- a/lib/rc-switch.c	Mon May 05 13:49:53 2014 +0200
+++ b/lib/rc-switch.c	Mon May 05 20:25:20 2014 +0200
@@ -41,11 +41,19 @@
 int		rcRepeatTransmit = 10;
 int		rcProtocol = 1;
 
+//const char TYPE_A_CODE[ 6][6]  = { "00000", "10000", "01000", "00100", "00010", "00001"};
+const char TYPE_B_CODE[ 5][5]  = { "FFFF", "0FFF", "F0FF", "FF0F", "FFF0" };
+const char TYPE_C_CODE[16][5]  = { "0000", "F000", "0F00", "FF00", "00F0", "F0F0", "0FF0", "FFF0",
+				   "000F", "F00F", "0F0F", "FF0F", "00FF", "F0FF", "0FFF", "FFFF" };
+const char TYPE_D_CODE[5][2][9] = { { "11100001", "11110000" }, { "00000000", "00010001" }, { "10000010", "10010011" },
+				    { "11000011", "11010010" }, { "01000001", "01010000" } };
+
 
 char *getCodeWordA(char*, char*, bool);
 char *getCodeWordB(int, int, bool);
 char *getCodeWordC(char, int, int, bool);
-char *getCodeWordD(char, int, bool);
+
+char *getCodeWordE(char, int, bool);
 void sendTriState(char*);
 void transmit(int, int);
 void send0(void);
@@ -140,25 +148,14 @@
 
 
 /*
- * Switch a remote switch on (Type D REV)
+ * Switch a remote switch on (Type E REV)
  *
- * @param sGroup        Code of the switch group (A,B,C,D)
- * @param nDevice       Number of the switch itself (1..3)
+ * @param sGroup   Code of the switch group (A,B,C,D)
+ * @param nDevice  Number of the switch itself (1..3)
+ * @param bStatus  Status to toggle to
  */
-void switchOn_D(char sGroup, int nDevice) {
-    sendTriState( 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 switchOff_D(char sGroup, int nDevice) {
-    sendTriState( getCodeWordD(sGroup, nDevice, false) );
+void toggleTypeE(char sGroup, int nDevice, bool bStatus) {
+    sendTriState( getCodeWordE(sGroup, nDevice, bStatus) );
 }
 
 
@@ -169,94 +166,42 @@
  * @param sFamily  Familycode (a..f)
  * @param nGroup   Number of group (1..4)
  * @param nDevice  Number of device (1..4)
- */
-void switchOn_C(char sFamily, int nGroup, int nDevice) {
-    sendTriState( 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)
+ * @param bStatus  Status to toggle to
  */
-void switchOff_C(char sFamily, int nGroup, int nDevice) {
-    sendTriState( 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 switchOn_B(int nAddressCode, int nChannelCode) {
-    sendTriState( getCodeWordB(nAddressCode, nChannelCode, true) );
+void toggleTypeC(char sFamily, int nGroup, int nDevice, bool bStatus) {
+    sendTriState( getCodeWordC(sFamily, nGroup, nDevice, bStatus) );
 }
 
 
 
 /*
- * Switch a remote switch off (Type B with two rotary/sliding switches)
+ * Switch a remote switch on/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)
+ * @param iGroup    Number of the switch group (1..4)
+ * @param iDevice   Number of the switch itself (1..4)
+ * @param bStatus   Status to toggle to
  */
-void switchOff_B(int nAddressCode, int nChannelCode) {
-    sendTriState( getCodeWordB(nAddressCode, nChannelCode, false) );
+void toggleTypeB(int iGroup, int iDevice, bool bStatus)
+{
+    char  *str = xstrcpy(getCodeWordB(iGroup, iDevice, bStatus));
+
+//    save(TYPE_B);
+    sendTriState( str );
+//    load();
+    free(str);
 }
 
 
 
-/**
- * Deprecated, use switchOn(char* sGroup, 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(char* sGroup, int nChannel) {
-  char* code[6] = { (char *)"00000", (char *)"10000", (char *)"01000", (char *)"00100", (char *)"00010", (char *)"00001" };
-  this->switchOn(sGroup, code[nChannel]);
-}*/
-
-/**
- * Deprecated, use switchOff(char* sGroup, 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)
+ * @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")
+ * @param bStatus   Status to toggle to
  */
-/*void RCSwitch::switchOff(char* sGroup, int nChannel) {
-  char* code[6] = { (char *)"00000", (char *)"10000", (char *)"01000", (char *)"00100", (char *)"00010", (char *)"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 switchOn_A(char* sGroup, char* sDevice) {
-    sendTriState( 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 switchOff_A(char* sGroup, char* sDevice) {
-    sendTriState( getCodeWordA(sGroup, sDevice, false) );
+void toggleTypeA(char* sGroup, char* sDevice, bool bStatus) {
+    sendTriState( getCodeWordA(sGroup, sDevice, bStatus) );
 }
 
 
@@ -282,29 +227,23 @@
     int		i, nReturnPos = 0;
     static char sReturn[13];
    
-    char* code[5] = { (char *)"FFFF", (char *)"0FFF", (char *)"F0FF", (char *)"FF0F", (char *)"FFF0" };
     if (nAddressCode < 1 || nAddressCode > 4 || nChannelCode < 1 || nChannelCode > 4) {
     	return '\0';
     }
     for (i = 0; i<4; i++) {
-	sReturn[nReturnPos++] = code[nAddressCode][i];
+	sReturn[nReturnPos++] = TYPE_B_CODE[nAddressCode][i];
     }
 
     for (i = 0; i<4; i++) {
-	sReturn[nReturnPos++] = code[nChannelCode][i];
+	sReturn[nReturnPos++] = TYPE_B_CODE[nChannelCode][i];
     }
    
     sReturn[nReturnPos++] = 'F';
     sReturn[nReturnPos++] = 'F';
     sReturn[nReturnPos++] = 'F';
-   
-    if (bStatus) {
-	sReturn[nReturnPos++] = 'F';
-    } else {
-	sReturn[nReturnPos++] = '0';
-    }
-   
+    sReturn[nReturnPos++] = bStatus ? 'F' : '0';
     sReturn[nReturnPos] = '\0';
+
     return sReturn;
 }
 
@@ -319,25 +258,16 @@
 char *getCodeWordA(char* sGroup, char* sDevice, bool bOn)
 {
     static char sDipSwitches[13];
-    int i = 0;
-    int j = 0;
+    int		i, j = 0;
 
     for (i=0; i < 5; i++) {
-	if (sGroup[i] == '0') {
-            sDipSwitches[j++] = 'F';
-    	} else {
-            sDipSwitches[j++] = '0';
-    	}
+	sDipSwitches[j++] = (sGroup[i] == '0') ? 'F' : '0';
     }
   
     for (i=0; i < 5; i++) {
-        if (sDevice[i] == '0') {
-            sDipSwitches[j++] = 'F';
-        } else {
-            sDipSwitches[j++] = '0';
-        }
+	sDipSwitches[j++] = (sDevice[i] == '0') ? 'F' : '0';
     }
-  
+
     if ( bOn ) {
         sDipSwitches[j++] = '0';
         sDipSwitches[j++] = 'F';
@@ -359,16 +289,21 @@
 {
     static char sReturn[13];
     int		i, nReturnPos = 0;
-  
-    if ( (uint8_t)sFamily < 97 || (uint8_t)sFamily > 112 || nGroup < 1 || nGroup > 4 || nDevice < 1 || nDevice > 4) {
+ 
+    if (sFamily < 'a') {
+	// To also enable capital 'A' to 'F'
+	sFamily += 32;
+    }
+
+    if ( sFamily < 'a' || sFamily > 'f' || nGroup < 1 || nGroup > 4 || nDevice < 1 || nDevice > 4) {
     	return '\0';
     }
   
-    char* sDeviceGroupCode =  dec2binWzerofill(  (nDevice-1) + (nGroup-1)*4, 4  );
-    char familycode[16][5] = { "0000", "F000", "0F00", "FF00", "00F0", "F0F0", "0FF0", "FFF0", "000F", "F00F", "0F0F", "FF0F", "00FF", "F0FF", "0FFF", "FFFF" };
     for (i = 0; i<4; i++) {
-    	sReturn[nReturnPos++] = familycode[ (int)sFamily - 97 ][i];
+    	sReturn[nReturnPos++] = TYPE_C_CODE[ sFamily - 'a' ][i];
     }
+
+    char *sDeviceGroupCode = dec2binWzerofill(  (nDevice-1) + (nGroup-1)*4, 4  );
     for (i = 0; i<4; i++) {
     	sReturn[nReturnPos++] = (sDeviceGroupCode[3-i] == '1' ? 'F' : '0');
     }
@@ -376,14 +311,53 @@
     sReturn[nReturnPos++] = '0';
     sReturn[nReturnPos++] = 'F';
     sReturn[nReturnPos++] = 'F';
-  
-    if (bStatus) {
-    	sReturn[nReturnPos++] = 'F';
-    } else {
-    	sReturn[nReturnPos++] = '0';
+    sReturn[nReturnPos++] = bStatus ? 'F' : '0';
+    sReturn[nReturnPos] = '\0';
+
+    return sReturn;
+}
+
+
+
+/*
+ * Decoding for the Quigg Switch Type
+ *
+ * Returns a char[22], representing the States to be send.
+ * A Code Word consists of 1 start bit, 12 address bits and 8 command data bits.
+ * A Code Bit can have 2 different states: "0" (low), "1" (high)
+ *
+ * +--------------+--------------------------------+------------------------------+
+ * | 1 bits start | 12 bits address (device group) | 8 bits (command/switch data) |
+ * |        1     |           110011001100         |             00010001         |
+ * +--------------+--------------------------------+------------------------------+
+ *
+ * Source: https://github.com/d-a-n/433-codes/blob/master/database.md#quigg
+ *
+ * @param sGroup        12-bit Binary ID of the Device Group
+ * @param nDevice       Number of the switch itself (1..4, or 0 to switch the entire Group)
+ * @param bStatus       Wether to switch on (true) or off (false)
+ *
+ * @return char[22]
+ */
+char *getCodeWordD(char *sGroup, int nDevice, bool bStatus)
+{
+    static char sReturn[22];
+    int		i, nReturnPos = 0;
+
+    /* Startbit */
+    sReturn[nReturnPos++] = '1';
+
+    /* 12 bit Group */
+    for (i = 0; i < 12; ++i) {
+	sReturn[nReturnPos++] = sGroup[i];
     }
-  
-    sReturn[nReturnPos] = '\0';
+
+    /* 8 Bit Device Identifier + Status (undividable!) */
+    for (i = 0; i < 8; ++i) {
+	sReturn[nReturnPos++] = TYPE_D_CODE[nDevice][bStatus][i];
+    }
+    sReturn[nReturnPos] = 0;
+
     return sReturn;
 }
 
@@ -409,7 +383,7 @@
  *
  * @return char[13]
  */
-char *getCodeWordD(char sGroup, int nDevice, bool bStatus){
+char *getCodeWordE(char sGroup, int nDevice, bool bStatus){
     static char sReturn[13];
     int		i, nReturnPos = 0;
 
--- a/rc433/send.c	Mon May 05 13:49:53 2014 +0200
+++ b/rc433/send.c	Mon May 05 20:25:20 2014 +0200
@@ -32,7 +32,8 @@
     fprintf(stdout, "       send -t A -g 00001 -d 00001 -s 1\n");
     fprintf(stdout, "       send -t B -g 2 -d 2 -s 0\n");
     fprintf(stdout, "       send -t C -f c -g 3 -d 3 -s 1\n");
-    fprintf(stdout, "       send -t D -g A -d 2 -s 0\n");
+    fprintf(stdout, "\n");
+    fprintf(stdout, "       send -t E -g A -d 2 -s 0\n");
 }
 
 
@@ -118,8 +119,8 @@
     /*
      * Check all option and combinations
      */
-    if ((cType < 'A') || (cType > 'D')) {
-	fprintf(stderr, "Type must be A, B, C or D\n");
+    if ((cType < 'A') || (cType > 'E')) {
+	fprintf(stderr, "Type must be A, B, C or E\n");
 	return 1;
     }
     if ((iState < 0) || (iState > 1)) {
@@ -163,7 +164,7 @@
 	    return 1;
 	}
     }
-    if (cType == 'D') {
+    if (cType == 'E') {
 	if ((iDevice < 1) || (iDevice > 3)) {
 	    fprintf(stderr, "Device must be 1..3\n");
     	    return 1;
@@ -194,31 +195,19 @@
     switch (cType) {
 	case 'A':
 		    printf("Type A sending group[%s] device[%s] command[%d]\n", sGroup, sDevice, iState);
-		    if (iState)
-			switchOn_A(sGroup, sDevice);
-		    else
-			switchOff_A(sGroup, sDevice);
+		    toggleTypeA(sGroup, sDevice, iState);
 		    break;
 	case 'B':
 		    printf("Type B sending group[%d] device[%i] command[%i]\n", iGroup, iDevice, iState);
-		    if (iState)
-			switchOn_B(iGroup, iDevice);
-		    else
-			switchOff_B(iGroup, iDevice);
+		    toggleTypeB(iGroup, iDevice, iState);
 		    break;
 	case 'C':
 		    printf("Type C sending family[%c] group[%d] device[%i] command[%i]\n", cFamily, iGroup, iDevice, iState);
-		    if (iState)
-			switchOn_C(cFamily, iGroup, iDevice);
-		    else
-			switchOff_C(cFamily, iGroup, iDevice);
+		    toggleTypeC(cFamily, iGroup, iDevice, iState);
 		    break;
-	case 'D':
-		    printf("Type D sending group[%c] device[%i] command[%i]\n", cGroup, iDevice, iState);
-		    if (iState)
-			switchOn_D(cGroup, iDevice);
-		    else
-			switchOff_D(cGroup, iDevice);
+	case 'E':
+		    printf("Type E sending group[%c] device[%i] command[%i]\n", cGroup, iDevice, iState);
+		    toggleTypeE(cGroup, iDevice, iState);
 		    break;
     }
     disableTransmit();

mercurial