Code added on the wrong machine

Sat, 09 Aug 2014 22:48:21 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 09 Aug 2014 22:48:21 +0200
changeset 202
5d09ca728809
parent 201
e589247c779c
child 203
47e5109c7f53

Code added on the wrong machine

thermferm/devices.c file | annotate | diff | comparison | revisions
--- a/thermferm/devices.c	Sat Aug 09 21:43:09 2014 +0200
+++ b/thermferm/devices.c	Sat Aug 09 22:48:21 2014 +0200
@@ -23,6 +23,7 @@
 #include "thermferm.h"
 #include "devices.h"
 #include "rc-switch.h"
+#include "panel.h"
 #include "xutil.h"
 
 
@@ -31,6 +32,138 @@
 extern int		my_shutdown;
 
 
+#ifdef HAVE_WIRINGPI_H
+
+#define MAXTIMINGS 100
+
+int             dht11_pin = -1;
+int             dht11_temperature = -1;
+int             dht11_humidity = -1;
+int             dht11_valid = FALSE;
+int             dht11_t_offset = 0;
+int             dht11_h_offset = 0;
+
+
+static uint8_t sizecvt(const int read_value) {
+    /* 
+     * digitalRead() and friends from wiringpi are defined as returning a value
+     *            < 256. However, they are returned as int() types. This is a safety function 
+     */
+    if (read_value > 255 || read_value < 0) {
+	syslog(LOG_NOTICE, "invalid data from wiringPi library");
+    }
+	                      
+    return (uint8_t)read_value;
+}
+
+
+
+/*
+ * DHT11 sensor read. This should be used in a thread loop.
+ */
+void dht11Read(void) {
+    int         	tries = 5;
+    unsigned short	got_correct_data = 0;
+
+    if (dht11_pin == -1)
+	return;
+
+    while (tries && !got_correct_data) {
+	uint8_t	laststate = HIGH;
+	uint8_t	counter = 0;
+	uint8_t	j = 0, i = 0;
+	int	dht11_dat[5] = {0,0,0,0,0};
+
+	/*
+	 * Select output mode to send the start signal.
+	 */
+	pinMode(dht11_pin, OUTPUT);                 
+	digitalWrite(dht11_pin, HIGH);
+	usleep(1000);
+
+	/*
+	 * Low for at least 18 milliseconds
+	 */
+	digitalWrite(dht11_pin, LOW);
+	usleep(20000);
+	digitalWrite(dht11_pin, HIGH);
+	pinMode(dht11_pin, INPUT);
+
+	/*
+	 * Detect change and read data
+	 */
+	for (i=0; i<MAXTIMINGS; i++) {
+	    counter = 0;
+	    delayMicroseconds(10);
+	    while (sizecvt(digitalRead(dht11_pin)) == laststate) {
+		counter++;
+		delayMicroseconds(1);
+		if (counter == 255) {
+		    break;
+		}
+	    }
+	    laststate = sizecvt(digitalRead(dht11_pin));
+
+	    if (counter == 255) 
+		break;
+
+	    /*
+	     * ignore first 3 transitions
+	     */
+	    if ((i >= 4) && (i%2 == 0)) {
+
+		// shove each bit into the storage bytes
+		dht11_dat[(int)((double)j/8)] <<= 1;
+		if (counter > 16)
+		    dht11_dat[(int)((double)j/8)] |= 1;
+		j++;
+	    }
+	}
+
+	/*
+	 * If there is no sensor, j = 0
+	 */
+	if ((counter == 255) && (j == 0)) {
+	    if (dht11_temperature != -1) {
+		syslog(LOG_NOTICE, "dht11 sensor disappeared");
+	    } else {
+		syslog(LOG_NOTICE, "dht11 sensor not present");
+	    }
+	    dht11_temperature = -1;
+	    dht11_humidity = -1;
+	    dht11_valid = FALSE;
+	    return;
+	} 
+
+	/*
+	 * check we read 40 bits (8bit x 5 ) + verify checksum in the last byte
+	 * print it out if data is good
+	 */
+	if ((j >= 40) && (dht11_dat[4] == ((dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF))) {
+		got_correct_data = 1;
+
+		int h = dht11_dat[0] + dht11_dat[1];
+		int t = (dht11_dat[2] & 0x7F) + dht11_dat[3];
+		t += dht11_t_offset;
+		h += dht11_h_offset;
+
+		if ((dht11_dat[2] & 0x80) != 0) 
+		    t *= -1;
+
+		dht11_temperature = t;
+		dht11_humidity = h;
+		dht11_valid = TRUE;
+	} else {
+		tries--;
+		if (tries == 0)
+		    syslog(LOG_INFO, "dht11 data checksum was wrong 5 times");
+		usleep(100000);
+	}
+    }
+}
+
+#endif
+
 
 
 int device_out(char *uuid, int value)
@@ -220,7 +353,23 @@
 	    	ndev->subdevice = i;
 	    	ndev->gpiopin = pin;
 	    	ndev->timestamp = time(NULL);
-	    	if (i == 7) {
+		if (i == PANEL_LED) {
+		    ndev->direction = DEVDIR_OUT_BIN;
+		    ndev->inuse = 1;
+		    ndev->comment = xstrcpy((char *)"Frontpanel LED");
+		} else if (i == PANEL_ENTER) {
+		    ndev->direction = DEVDIR_IN_BIN;
+		    ndev->inuse = 1;
+		    ndev->comment = xstrcpy((char *)"Frontpanel Enter key");
+		} else if (i == PANEL_DOWN) {
+		    ndev->direction = DEVDIR_IN_BIN;
+		    ndev->inuse = 1;
+		    ndev->comment = xstrcpy((char *)"Frontpanel Down key");
+		} else if (i == PANEL_UP) {
+		    ndev->direction = DEVDIR_IN_BIN;
+		    ndev->inuse = 1;
+		    ndev->comment = xstrcpy((char *)"Frontpanel Up key");
+		} else if (i == 7) {
 		    ndev->direction = DEVDIR_INTERN;
 		    ndev->inuse = 1;
 		    ndev->comment = xstrcpy((char *)"1-Wire bus");
@@ -325,6 +474,12 @@
 			    addr = NULL;
 			}
 			break;
+		case DEVTYPE_DHT:
+			if (device->subdevice == 0) {
+			} else if (device->subdevice == 1) {
+
+			}
+			break;
 		default:
 			break;
 	    }

mercurial