thermferm/one-wire.c

changeset 656
ca47c742a25d
parent 655
780cc08df263
child 657
38162f374842
--- a/thermferm/one-wire.c	Tue Apr 02 15:29:16 2024 +0200
+++ b/thermferm/one-wire.c	Wed Apr 03 12:14:45 2024 +0200
@@ -23,6 +23,8 @@
 #include "thermferm.h"
 #include "statetbl.h"
 #include "one-wire.h"
+#include "devices.h"
+#include "delay.h"
 #include "xutil.h"
 
 
@@ -61,20 +63,22 @@
 SM_DECL(one_wire,(char *)"one-wire")
 SM_STATES
     Scan,
+    Read2413,
     Reading,
     Missing
 SM_NAMES
     (char *)"Scan",
+    (char *)"Read2413",
     (char *)"Reading",
     (char *)"Missing"
 SM_EDECL
 
-    int			found, i;
+    int			found, i, rc;
     FILE		*fp;
-//    devices_list	*device, *ndev;
+    devices_list	*device;
     w1_list		*dev_w1, *n_w1, *cur_w1 = NULL;
     char		buffer[25], w1type[10];
-    uuid_t		uu;
+    uint8_t		state, output;
 
 SM_START(Scan)
 
@@ -94,8 +98,6 @@
     }
     while ((fgets(buffer, 25, fp))) {
 	buffer[strlen(buffer)-1] = '\0';
-//	syslog(LOG_NOTICE, "device %d %s", strlen(buffer), buffer);
-
 	strncpy(w1type, buffer, 2);
 	w1type[2] = '\0';
 
@@ -149,6 +151,58 @@
 	}
     }
     fclose(fp);
+    SM_PROCEED(Read2413);
+
+SM_STATE(Read2413)
+
+    if (my_shutdown) {
+	SM_SUCCESS;
+    }
+
+    for (dev_w1 = w1_devices; dev_w1; dev_w1 = dev_w1->next) {
+	if (strcmp(dev_w1->family, "3a") == 0) {
+	    syslog(LOG_NOTICE, "ds2413 %s", dev_w1->address);
+	    for (i = 0; i < 2; i++) {
+		for (device = Config.devices; device; device = device->next) {
+		    if ((strcmp(dev_w1->address, device->address) == 0) && (device->subdevice == i) && (device->direction == DEVDIR_IN_BIN)) {
+			/*
+			 * First make sure that if this device is configured as input
+			 * to drive the output high.
+			 * TODO: This must be made different. Check state and fix only if wrong.
+			 */
+			if ((rc = read_w1(device->address, (char *)"state")) >= 0) {
+			    state = (unsigned int)rc;
+			    output = ((state & 0x02) >> 1) + ((state & 0x08) >> 2);         /* Both latch states    */
+			    if (i == 0) {
+				output = (output & 0xfe);
+				output |= 0x01;
+			    } else if (i == 1) {
+				output = (output & 0xfd);
+				output |= 0x02;
+			    } else {
+				output = 0xff;
+			    }
+			    write_w1(device->address, (char *)"output", output);
+			}
+			if ((rc = read_w1(device->address, (char *)"state")) >= 0) {
+			    pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
+			    /*
+			     * Read PIOA or PIOB pin state bits
+			     */
+			    if (device->subdevice == 0)
+				device->value = (rc & 0x01) ? 0 : 1;
+			    else if (device->subdevice == 1)
+				device->value = (rc & 0x04) ? 0 : 1;
+			    device->timestamp = time(NULL);
+			    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
+			}
+			mDelay(20);
+		    }
+		}
+	    }
+	}
+    }
+
     SM_PROCEED(Reading);
 
 SM_STATE(Reading)

mercurial