thermferm/one-wire.c

changeset 732
b0e99e30a008
parent 731
8b7c63bddf75
--- a/thermferm/one-wire.c	Mon May 06 15:34:32 2024 +0200
+++ b/thermferm/one-wire.c	Tue May 07 14:11:31 2024 +0200
@@ -142,7 +142,7 @@
     (char *)"Websocket"
 SM_EDECL
 
-    int			found, i, rc, value, conv_time;
+    int			found, w, rc, value, conv_time;
     FILE		*fp;
     devices_list	*device;
     w1_list		*dev_w1, *n_w1, *cur_w1 = NULL;
@@ -270,80 +270,78 @@
 
     for (dev_w1 = w1_devices; dev_w1; dev_w1 = dev_w1->next) {
 	if (strcmp(dev_w1->family, "3a") == 0) {
-	    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)) {
+	    for (device = Config.devices; device; device = device->next) {
+		if ((strcmp(dev_w1->address, device->address) == 0) && (device->direction == DEVDIR_IN_BIN)) {
+		    /*
+		     * First make sure that this device is configured as input
+		     * to drive the output high. Fix if programmed as output.
+		     */
+		    if ((rc = read_w1(device->address, (char *)"state")) >= 0) {
+			state = (unsigned int)rc;
+			output = ((state & 0x02) >> 1) + ((state & 0x08) >> 2);		/* Both latch states	*/
+			if ((device->subdevice == 0) && ((state & 0x02) == 0)) {	/* Fix A side		*/
+			    syslog(LOG_NOTICE, "One-wire device %s-%d out %02x -> %02x", dev_w1->address, device->subdevice, output, output | 0x01);
+			    output |= 0x01;
+			    write_w1(device->address, (char *)"output", output);
+			    mDelay(10);
+			    if ((rc = read_w1(device->address, (char *)"state")) >= 0)	/* Read PIO again	*/
+				state = (unsigned int)rc;
+			}
+			if ((device->subdevice == 1) && ((state & 0x08) == 0)) {	/* Fix B side		*/
+			    syslog(LOG_NOTICE, "One-wire device %s-%d out %02x -> %02x", dev_w1->address, device->subdevice, output, output | 0x02);
+			    output |= 0x02;
+			    write_w1(device->address, (char *)"output", output);
+			    mDelay(10);
+			    if ((rc = read_w1(device->address, (char *)"state")) >= 0)	/* Read PIO again       */
+				state = (unsigned int)rc;
+			}
+			dev_w1->rd_cache = state;
+
+			newval = ((state & 0x04) >> 1) + (state & 0x01);
+			dev_w1->wr_cache = newval;
+			if (newval != dev_w1->value) {
+			    syslog(LOG_NOTICE, "One-wire device %s-%d in %02x value %d => %d", dev_w1->address, device->subdevice, state, dev_w1->value, newval);
+			    dev_w1->value = newval;
+			    dev_w1->timestamp = time(NULL);
+			    changed = true;
+			}
+
+//			pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 			/*
-			 * First make sure that this device is configured as input
-			 * to drive the output high. Fix if programmed as output.
+			 * Read PIOA or PIOB pin state bits
 			 */
-			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) && ((state & 0x02) == 0)) {				/* Fix A side		*/
-				syslog(LOG_NOTICE, "One-wire device %s-%d out %02x -> %02x", dev_w1->address, i, output, output | 0x01);
-				output |= 0x01;
-				write_w1(device->address, (char *)"output", output);
-				mDelay(10);
-				if ((rc = read_w1(device->address, (char *)"state")) >= 0)	/* Read PIO again	*/
-				    state = (unsigned int)rc;
-			    }
-			    if ((i == 1) && ((state & 0x08) == 0)) {				/* Fix B side		*/
-				syslog(LOG_NOTICE, "One-wire device %s-%d out %02x -> %02x", dev_w1->address, i, output, output | 0x02);
-				output |= 0x02;
-				write_w1(device->address, (char *)"output", output);
-				mDelay(10);
-				if ((rc = read_w1(device->address, (char *)"state")) >= 0)      /* Read PIO again       */
-				    state = (unsigned int)rc;
-			    }
-			    dev_w1->rd_cache = state;
+		    	if (device->subdevice == 0)
+			    device->value = (state & 0x01) ? 0 : 1;
+		    	else if (device->subdevice == 1)
+			    device->value = (state & 0x04) ? 0 : 1;
+		    	device->timestamp = time(NULL);
+//		    	pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
+		    }
+		    mDelay(20);
+		} else if ((strcmp(dev_w1->address, device->address) == 0) && (device->direction == DEVDIR_OUT_BIN)) {
+		    /*
+		     * Sync output state
+		     */
+		    if ((rc = read_w1(device->address, (char *)"state")) >= 0) {
+                	state = (unsigned int)rc;
+			newval = output = (state & 0x01) + ((state & 0x04) >> 1);
 
-			    newval = ((state & 0x04) >> 1) + (state & 0x01);
-			    dev_w1->wr_cache = newval;
-			    if (newval != dev_w1->value) {
-			    	syslog(LOG_NOTICE, "One-wire device %s-%d in %02x value %d => %d", dev_w1->address, i, state, dev_w1->value, newval);
-			    	dev_w1->value = newval;
+			if (device->subdevice == 0) {
+			    newval = (newval & 0xfe);
+			    newval |= (device->value) ? 0x00 : 0x01;
+			} else if (device->subdevice == 1) {
+			    newval = (newval & 0xfd);
+			    newval |= (device->value) ? 0x00 : 0x02;
+			}
+
+			dev_w1->wr_cache = newval;
+			if (output != newval) {
+			    if ((write_w1(dev_w1->address, (char *)"output", newval)) == 0) {
+				syslog(LOG_NOTICE, "One-wire device %s-%d out %02x -> %02x", dev_w1->address, device->subdevice, output, newval);
+				dev_w1->value = newval;
 				dev_w1->timestamp = time(NULL);
 				changed = true;
 			    }
-
-//			    pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
-			    /*
-			     * Read PIOA or PIOB pin state bits
-			     */
-			    if (device->subdevice == 0)
-				device->value = (state & 0x01) ? 0 : 1;
-			    else if (device->subdevice == 1)
-				device->value = (state & 0x04) ? 0 : 1;
-			    device->timestamp = time(NULL);
-//			    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
-			}
-			mDelay(20);
-		    } else if ((strcmp(dev_w1->address, device->address) == 0) && (device->subdevice == i) && (device->direction == DEVDIR_OUT_BIN)) {
-			/*
-			 * Sync output state
-			 */
-			if ((rc = read_w1(device->address, (char *)"state")) >= 0) {
-                            state = (unsigned int)rc;
-			    newval = output = (state & 0x01) + ((state & 0x04) >> 1);
-
-			    if (device->subdevice == 0) {
-				newval = (newval & 0xfe);
-				newval |= (device->value) ? 0x00 : 0x01;
-			    } else if (device->subdevice == 1) {
-				newval = (newval & 0xfd);
-				newval |= (device->value) ? 0x00 : 0x02;
-			    }
-
-			    dev_w1->wr_cache = newval;
-			    if (output != newval) {
-				if ((write_w1(dev_w1->address, (char *)"output", newval)) == 0) {
-				    syslog(LOG_NOTICE, "One-wire device %s-%d out %02x -> %02x", dev_w1->address, i, output, newval);
-				    dev_w1->value = newval;
-				    dev_w1->timestamp = time(NULL);
-				    changed = true;
-				}
-			    }
 			}
 		    }
 		} /* for (device = Config.devices; ... */
@@ -467,13 +465,13 @@
 
 SM_STATE(Websocket)
 
-    for (i = 0; i < 10; i++) {
+    for (w = 0; w < 10; w++) {
 	if (my_one_wire_shutdown) {
 	    SM_SUCCESS;
 	}
 
 	mDelay(100);
-	if (changed && i == 5)
+	if (changed && w == 5)
 	    one_wire_ws();
     }
     SM_PROCEED(ScanNew);

mercurial