Fixed initialisation of new GPIO devices.

Tue, 15 Sep 2015 14:44:21 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 15 Sep 2015 14:44:21 +0200
changeset 392
034746506c3d
parent 391
a89b3ac7e97b
child 393
220f2f30bf68

Fixed initialisation of new GPIO devices.

thermferm/devices.c file | annotate | diff | comparison | revisions
thermferm/devices.h file | annotate | diff | comparison | revisions
--- a/thermferm/devices.c	Thu Aug 20 21:14:44 2015 +0200
+++ b/thermferm/devices.c	Tue Sep 15 14:44:21 2015 +0200
@@ -168,6 +168,9 @@
 #endif
 
 
+/*
+ * Read one byte from a 1-wire device like a DS2413
+ */
 int read_w1(char *address, char *file)
 {
     char	*addr = NULL;
@@ -187,8 +190,6 @@
     	    	if ((read(fn, &val, 1)) == 1) {
 		    rc = (int)val;
 		    goto leave;
-//    	    	} else {
-//	    	    syslog(LOG_NOTICE, "read_w1() read %s try=%d: %s", addr, retries, strerror(errno));
     	    	}
 	    }
 	    syslog(LOG_NOTICE, "read_w1() read %s fatal: %s", addr, strerror(errno));
@@ -214,6 +215,9 @@
 
 
 
+/*
+ * Write a byte to a 1-wire device like a DS2413
+ */
 int write_w1(char *address, char *file, uint8_t val)
 {
     char	*addr = NULL;
@@ -388,6 +392,36 @@
 }	   
 
 
+/*
+ * Returns 0 if failed.
+ * Returns 1 if success, value contains new value.
+ */
+int device_in(char *uuid, int *value)
+{
+    devices_list	*device;
+
+    if (uuid == NULL)
+	return 0;
+
+#ifdef HAVE_WIRINGPI_H
+    piLock(LOCK_DEVICES);
+#endif
+
+    for (device = Config.devices; device; device = device->next) {
+	if (! strcmp(uuid, device->uuid)) {
+
+	}
+    }
+
+#ifdef HAVE_WIRINGPI_H
+    piUnlock(LOCK_DEVICES);
+#endif
+
+    return 0;
+}
+
+
+
 
 /*
  * Auto detect hotplugged or known to be present devices
@@ -521,6 +555,7 @@
 	    	uuid_unparse(uu, ndev->uuid);
 	    	ndev->type = DEVTYPE_GPIO;
 	    	ndev->value = digitalRead(pin);
+		ndev->offset = 0;
 	    	ndev->present = DEVPRESENT_YES;
 	    	ndev->address = xstrcpy((char *)"GPIO");
 	    	snprintf(buf, 39, "Raspberry GPIO %d", i);
@@ -848,7 +883,7 @@
 #ifdef HAVE_WIRINGPI_H
 		case DEVTYPE_DHT:
 			/*
-			 * Make sure we don't read the sensor withing 2 seconds.
+			 * Make sure we don't read the sensor within 2 seconds.
 			 */
 			now = time(NULL);
 			if ((int)(now - dht11_last) > 2) {
@@ -878,6 +913,17 @@
 			    piUnlock(LOCK_DEVICES);
 			}
 			break;
+
+		case DEVTYPE_GPIO:
+			if (device->direction == DEVDIR_IN_BIN) {
+			    piLock(LOCK_DEVICES);
+			    device->value = digitalRead(device->gpiopin);
+			    device->offset = 0;
+			    device->timestamp = time(NULL);
+			    piUnlock(LOCK_DEVICES);
+			}
+			break;
+
 #endif
 #ifdef USE_SIMULATOR
 		case DEVTYPE_SIM:
--- a/thermferm/devices.h	Thu Aug 20 21:14:44 2015 +0200
+++ b/thermferm/devices.h	Tue Sep 15 14:44:21 2015 +0200
@@ -3,6 +3,7 @@
 
 
 int device_out(char *, int);
+int device_in(char *, int *);
 int devices_detect(void);
 
 #ifdef HAVE_WIRINGPI_H

mercurial