thermferm/devices.c

changeset 162
6fc9e3f7962f
parent 161
493e39bb0a08
child 163
3d813570a5e3
--- a/thermferm/devices.c	Thu Jul 31 19:29:54 2014 +0200
+++ b/thermferm/devices.c	Thu Jul 31 21:04:06 2014 +0200
@@ -37,12 +37,14 @@
 int devices_detect(void)
 {
     struct dirent	*de;
-//    FILE		*fp;
     DIR			*fd;
     devices_list	*device, *ndev;
     int			found, subdevices, i, rc = 0;
-    char		family[3];
+    char		buf[40];
     uuid_t		uu;
+#ifdef HAVE_WIRINGPI_H
+    int			pin;
+#endif
 
     /*
      * Scan for 1-wire devices
@@ -59,12 +61,12 @@
 		}
 
 		if (found == FALSE) {
-		    strncpy(family, de->d_name, 2);
-		    family[2] = '\0';
+		    strncpy(buf, de->d_name, 2);
+		    buf[2] = '\0';
 		    subdevices = 1;
-		    if (strcmp(family, (char *)"29") == 0)
+		    if (strcmp(buf, (char *)"29") == 0)
 			subdevices = 8;
-		    if (strcmp(family, (char *)"3a") == 0)
+		    if (strcmp(buf, (char *)"3a") == 0)
 			subdevices = 2;
 		    for (i = 0; i < subdevices; i++) {
 			ndev = (devices_list *)malloc(sizeof(devices_list));
@@ -75,21 +77,21 @@
 			uuid_unparse(uu, ndev->uuid);
 			ndev->type = DEVTYPE_W1;
 			ndev->direction = DEVDIR_UNDEF;
-			if (strcmp(family, (char *)"10") == 0) {
+			if (strcmp(buf, (char *)"10") == 0) {
 			    ndev->direction = DEVDIR_IN_ANALOG;
-			    ndev->description = xstrcpy((char *)"18S20,Digital thermometer");
-			} else if (strcmp(family, (char *)"28") == 0) {
+			    ndev->description = xstrcpy((char *)"DS18S20 Digital thermometer");
+			} else if (strcmp(buf, (char *)"28") == 0) {
 			    ndev->direction = DEVDIR_IN_ANALOG;
-			    ndev->description = xstrcpy((char *)"18B20,Digital thermometer");
-			} else if (strcmp(family, (char *)"29") == 0) {
-			    ndev->description = xstrcpy((char *)"2408,8 Channel addressable switch/LCD");
-			} else if (strcmp(family, (char *)"3a") == 0) {
-			    ndev->description = xstrcpy((char *)"2413,Dual channel addressable switch");
-			} else if (strcmp(family, (char *)"w1") == 0) {
+			    ndev->description = xstrcpy((char *)"DS18B20 Digital thermometer");
+			} else if (strcmp(buf, (char *)"29") == 0) {
+			    ndev->description = xstrcpy((char *)"DS2408 8 Channel addressable switch/LCD");
+			} else if (strcmp(buf, (char *)"3a") == 0) {
+			    ndev->description = xstrcpy((char *)"DS2413 Dual channel addressable switch");
+			} else if (strcmp(buf, (char *)"w1") == 0) {
 			    ndev->description = xstrcpy((char *)"Master System device");
 			} else {
 			    ndev->description = xstrcpy((char *)"Unknown device family ");
-			    ndev->description = xstrcat(ndev->description, family);
+			    ndev->description = xstrcat(ndev->description, buf);
 			}
 			ndev->value = ndev->inuse = 0;
 			ndev->present = DEVPRESENT_YES;
@@ -116,6 +118,72 @@
 	}
     }
 
+#ifdef HAVE_WIRINGPI_H
+    if (piBoardRev() == 2) {
+	/*
+	 * Support rev B boards only
+	 */
+	found = FALSE;
+	for (device = Config.devices; device; device = device->next) {
+	    if (device->type == DEVTYPE_GPIO) {
+		found = TRUE;
+		break;
+	    }
+	}
+
+    	if (found == FALSE) {
+	    /*
+	     * There were no GPIO devices found.
+	     */
+	    subdevices = 12;
+	    pin = 0;
+	    for (i = 0; i < subdevices; i++) {
+	    	if (i == 8)
+		    pin = 17;
+
+	    	ndev = (devices_list *)malloc(sizeof(devices_list));
+	    	ndev->next = NULL;
+	    	ndev->version = 1;
+	    	ndev->uuid = malloc(37);
+	    	uuid_generate(uu);
+	    	uuid_unparse(uu, ndev->uuid);
+	    	ndev->type = DEVTYPE_GPIO;
+	    	ndev->value = digitalRead(pin);
+	    	ndev->present = DEVPRESENT_YES;
+	    	snprintf(buf, 39, "GPIO%d", i);
+	    	ndev->address = xstrcpy(buf);
+	    	snprintf(buf, 39, "Raspberry GPIO %d", i);
+	    	ndev->description = xstrcpy(buf);
+	    	ndev->subdevice = i;
+	    	ndev->gpiopin = pin;
+	    	ndev->timestamp = time(NULL);
+	    	if (i == 7) {
+		    ndev->direction = DEVDIR_INTERN;
+		    ndev->inuse = 1;
+		    ndev->comment = xstrcpy((char *)"1-Wire bus");
+	    	} else {
+		    ndev->direction = DEVDIR_IN_BIN;
+		    ndev->inuse = 0;
+		    ndev->comment = xstrcpy((char *)"Raspberry GPIO");
+	    	}
+	    	pin++;
+
+	    	if (Config.devices == NULL) {
+		    Config.devices = ndev;
+	    	} else {
+		    for (device = Config.devices; device; device = device->next) {
+		        if (device->next == NULL) {
+			    device->next = ndev;
+			    break;
+		    	}
+		    }
+	    	}
+	    	rc++;
+	    }
+	}
+    }
+#endif
+
     return rc;
 }
 

mercurial