All four types RC switches are now implemented

Sun, 04 May 2014 22:29:18 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sun, 04 May 2014 22:29:18 +0200
changeset 22
a3b058c67289
parent 21
ff107bf4d216
child 23
d820a6f3ec16

All four types RC switches are now implemented

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	Sun May 04 17:30:18 2014 +0200
+++ b/lib/mbselib.h	Sun May 04 22:29:18 2014 +0200
@@ -110,21 +110,14 @@
 #define PROTOCOL3_1_LOW_CYCLES   6
 
 
-void switchOn(int, int);
-void switchOff(int, int);
-/*
-void switchOn(char* sGroup, int nSwitchNumber);
-void switchOff(char* sGroup, int nSwitchNumber);
-void switchOn(char sFamily, int nGroup, int nDevice);
-void switchOff(char sFamily, int nGroup, int nDevice);
-void switchOn(char* sGroup, char* sDevice);
-void switchOff(char* sGroup, char* sDevice);
-void switchOn(char sGroup, int nDevice);
-void switchOff(char sGroup, int nDevice);
-*/
-//void sendTriState(char* Code);
-//void send(unsigned long Code, unsigned int length);
-//void send(char* Code);
+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 enableReceiveIRQ(int interrupt);
 void enableReceive(void);
@@ -144,8 +137,6 @@
 void setRepeatTransmit(int);
 void setReceiveTolerance(int);
 void setProtocol(int);
-//void setProtocol(int nProtocol, int nPulseLength);
-
 
 #endif
 
--- a/lib/rc-switch.c	Sun May 04 17:30:18 2014 +0200
+++ b/lib/rc-switch.c	Sun May 04 22:29:18 2014 +0200
@@ -145,30 +145,36 @@
  * @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) );
-}*/
+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 RCSwitch::switchOff(char sGroup, int nDevice) {
-  this->sendTriState( this->getCodeWordD(sGroup, nDevice, false) );
-}*/
+void switchOff_D(char sGroup, int nDevice) {
+    sendTriState( 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) );
-}*/
+void switchOn_C(char sFamily, int nGroup, int nDevice) {
+    sendTriState( getCodeWordC(sFamily, nGroup, nDevice, true) );
+}
+
+
 
 /**
  * Switch a remote switch off (Type C Intertechno)
@@ -177,9 +183,9 @@
  * @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) );
-}*/
+void switchOff_C(char sFamily, int nGroup, int nDevice) {
+    sendTriState( getCodeWordC(sFamily, nGroup, nDevice, false) );
+}
 
 
 
@@ -189,7 +195,7 @@
  * @param nAddressCode  Number of the switch group (1..4)
  * @param nChannelCode  Number of the switch itself (1..4)
  */
-void switchOn(int nAddressCode, int nChannelCode) {
+void switchOn_B(int nAddressCode, int nChannelCode) {
     sendTriState( getCodeWordB(nAddressCode, nChannelCode, true) );
 }
 
@@ -201,7 +207,7 @@
  * @param nAddressCode  Number of the switch group (1..4)
  * @param nChannelCode  Number of the switch itself (1..4)
  */
-void switchOff(int nAddressCode, int nChannelCode) {
+void switchOff_B(int nAddressCode, int nChannelCode) {
     sendTriState( getCodeWordB(nAddressCode, nChannelCode, false) );
 }
 
@@ -237,9 +243,11 @@
  * @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(char* sGroup, char* sDevice) {
-    this->sendTriState( this->getCodeWordA(sGroup, sDevice, true) );
-}*/
+void switchOn_A(char* sGroup, char* sDevice) {
+    sendTriState( getCodeWordA(sGroup, sDevice, true) );
+}
+
+
 
 /**
  * Switch a remote switch off (Type A with 10 pole DIP switches)
@@ -247,9 +255,9 @@
  * @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(char* sGroup, char* sDevice) {
-    this->sendTriState( this->getCodeWordA(sGroup, sDevice, false) );
-}*/
+void switchOff_A(char* sGroup, char* sDevice) {
+    sendTriState( getCodeWordA(sGroup, sDevice, false) );
+}
 
 
 
--- a/rc433/send.c	Sun May 04 17:30:18 2014 +0200
+++ b/rc433/send.c	Sun May 04 22:29:18 2014 +0200
@@ -24,35 +24,204 @@
 #include "send.h"
 
 
+
+void help(void)
+{
+    fprintf(stdout, "mbsePi-apps send v%s\n\n", VERSION);
+    fprintf(stdout, "Usage: send [-t type ] [-f family ] [-g group ] [-d device] [-s state ] [-h]\n");
+    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");
+}
+
+
+
+int isAcode(char *s)
+{
+    int	i;
+
+    if (strlen(s) != 5)
+	return FALSE;
+    for (i = 0; i < 5; i++) {
+	if ((s[i] < '0') || (s[i] > '1'))
+	    return FALSE;
+    }
+
+    return TRUE;
+}
+
+
+
+/*
+ * A - char group, char device (2 x 5 binary)
+ * B - int system, int unit
+ * C - char family, int group, int device
+ * D - char group, int device
+ */
+
 int main(int argc, char *argv[]) {
-    
+    char	buf[127], *endptr;
+    int		c;
+    char	cType = '0', *sGroup = NULL, *sDevice = NULL, cFamily = '0', cGroup = '0';
+    int		iState = -1, iGroup = -1, iDevice = -1;
+
+    while (1) {
+	int	option_index = 0;
+	static struct option long_options[] = {
+	    {"device", 1, 0, 'd'},
+	    {"family", 1, 0, 'f'},
+	    {"group", 1, 0, 'g'},
+	    {"help", 0, 0, 'h'},
+	    {"state", 1, 0, 's'},
+	    {"type", 1, 0, 't'},
+	    {0, 0, 0, 0}
+	};
+
+	c = getopt_long(argc, argv, "d:f:g:hs:t:", long_options, &option_index);
+	if (c == -1)
+	    break;
+
+	switch (c) {
+	    case 'd':	snprintf(buf, 127, "%s", optarg);
+			if (cType == 'A') {
+			    sDevice = xstrcpy(buf);
+			} else {
+			    iDevice = strtol(buf, &endptr, 10);
+			}
+			break;
+	    case 'f':	snprintf(buf, 127, "%s", optarg);
+			if (cType == 'C') {
+			    cFamily = tolower(buf[0]);
+			}
+			break;
+	    case 'g':	snprintf(buf, 127, "%s", optarg);
+			if (cType == 'A') {
+			    sGroup = xstrcpy(buf);
+			} else if (cType == 'D') {
+			    cGroup = toupper(buf[0]);
+			} else {
+			    iGroup = strtol(buf, &endptr, 10);
+			}
+			break;
+	    case 'h':   help();
+			return 1;
+	    case 's':	snprintf(buf, 127, "%s", optarg);
+			iState = strtol(buf, &endptr, 10);
+			break;
+	    case 't':	snprintf(buf, 127, "%s", optarg);
+			cType = toupper(buf[0]);
+			break;
+	}
+    }
+
+    /*
+     * Check all option and combinations
+     */
+    if ((cType < 'A') || (cType > 'D')) {
+	fprintf(stderr, "Type must be A, B, C or D\n");
+	return 1;
+    }
+    if ((iState < 0) || (iState > 1)) {
+	fprintf(stderr, "State must be 0 or 1\n");
+	return 1;
+    }
+    if (cType == 'A') {
+	if (sGroup == NULL) {
+	    fprintf(stderr, "Group not set\n");
+	    return 1;
+	}
+	if (sDevice == NULL) {
+	    fprintf(stderr, "Device not set\n");
+	    return 1;
+	}
+	if (isAcode(sGroup) == FALSE) {
+	    fprintf(stderr, "Group must be 5 binary digits like 10000\n");
+	    return 1;
+	}
+	if (isAcode(sDevice) == FALSE) {
+	    fprintf(stderr, "Device must be 5 binary digits like 10000\n");
+	    return 1;
+	}
+    } else {
+	if (iDevice == -1) {
+	    fprintf(stderr, "Device not set\n");
+	    return 1;
+	}
+    }
+    if ((cType == 'B') || (cType == 'C')) {
+	if (iGroup == -1) {
+	    fprintf(stderr, "Group not set\n");
+	    return 1;
+	}
+	if ((iGroup < 1) || (iGroup > 4)) {
+	    fprintf(stderr, "Group must be 1..4\n");
+	    return 1;
+	}
+	if ((iDevice < 1) || (iDevice > 4)) {
+	    fprintf(stderr, "Device must be 1..4\n");
+	    return 1;
+	}
+    }
+    if (cType == 'D') {
+	if ((iDevice < 1) || (iDevice > 3)) {
+	    fprintf(stderr, "Device must be 1..3\n");
+    	    return 1;
+	}
+	if ((cGroup < 'A') || (cGroup > 'D')) {
+	    fprintf(stderr, "Group must be A..D\n");
+	    return 1;
+	}
+    }
+    if (cType == 'C') {
+	if ((cFamily < 'a') || (cFamily > 'f')) {
+	    fprintf(stderr, "Family must be a..f\n");
+	    return 1;
+	}
+    }
+
     /*
      output PIN is hardcoded for testing purposes
      see https://projects.drogon.net/raspberry-pi/wiringpi/pins/
      for pin mapping of the raspberry pi GPIO connector
      */
     int PIN = 0;
-    int systemCode = atoi(argv[1]);
-    int unitCode = atoi(argv[2]);
-    int command  = atoi(argv[3]);
     
     if (wiringPiSetup () )
 	return 1;
 
-    printf("sending systemCode[%d] unitCode[%i] command[%i]\n", systemCode, unitCode, command);
     enableTransmit(PIN);
-    
-    switch(command) {
-        case 1:
-            switchOn(systemCode, unitCode);
-            break;
-        case 0:
-            switchOff(systemCode, unitCode);
-            break;
-        default:
-            printf("command[%i] is unsupported\n", command);
-            return -1;
+    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);
+		    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);
+		    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);
+		    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);
+		    break;
     }
-	
+    disableTransmit();
+
     return 0;
 }

mercurial