Finished one-wire ds2413 output

Thu, 11 Apr 2024 13:18:20 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 11 Apr 2024 13:18:20 +0200
changeset 671
b2e2cbb13cb3
parent 670
6c62e99ade00
child 672
0c2c66920d79

Finished one-wire ds2413 output

thermferm/devices.c file | annotate | diff | comparison | revisions
thermferm/one-wire.c file | annotate | diff | comparison | revisions
thermferm/thermferm.h file | annotate | diff | comparison | revisions
--- a/thermferm/devices.c	Thu Apr 11 11:19:04 2024 +0200
+++ b/thermferm/devices.c	Thu Apr 11 13:18:20 2024 +0200
@@ -236,7 +236,7 @@
 {
     devices_list	*device;
     time_t		now, my_timestamp;
-    int			rc, my_value, test_value;
+    int			my_value, test_value;
 #ifdef HAVE_WIRINGPI_H
     int			i;
     char		buf[40];
@@ -296,30 +296,30 @@
 			 * the "other" PIO channel. To make things a bit more complicated
 			 * the bits in the state register differ from the output register.
 			 */
-			uint8_t	state, output;
+//			uint8_t	state, output;
 
-			if ((rc = read_w1(device->address, (char *)"state")) >= 0) {
-			    state = (unsigned int)rc;
-			    output = (state & 0x01) + ((state & 0x04) >> 1);
+//			if ((rc = read_w1(device->address, (char *)"state")) >= 0) {
+//			    state = (unsigned int)rc;
+//			    output = (state & 0x01) + ((state & 0x04) >> 1);
 
-			    if (device->subdevice == 0) {
-				output = (output & 0xfe);
-				output |= (value == 0) ? 0x01 : 0x00;
-			    } else if (device->subdevice == 1) {
-				output = (output & 0xfd);
-				output |= (value == 0) ? 0x02 : 0x00;
-			    } else {
-				output = 0xff;
-			    }
+//			    if (device->subdevice == 0) {
+//				output = (output & 0xfe);
+//				output |= (value == 0) ? 0x01 : 0x00;
+//			    } else if (device->subdevice == 1) {
+//				output = (output & 0xfd);
+//				output |= (value == 0) ? 0x02 : 0x00;
+//			    } else {
+//				output = 0xff;
+//			    }
 
-			    if ((write_w1(device->address, (char *)"output", output)) == 0) {
+//			    if ((write_w1(device->address, (char *)"output", output)) == 0) {
 			    	syslog(LOG_NOTICE, "DS2413 PIO%c value=%d (%s)", (device->subdevice == 0) ? 'A' : 'B', (value == 0) ? 0 : 1, device->comment);
 //				pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 				device->value = (value == 0) ? 0 : 1;
 				device->timestamp = time(NULL);
 //				pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
-			    }
-			}
+//			    }
+//			}
 		    }
 		}
 
--- a/thermferm/one-wire.c	Thu Apr 11 11:19:04 2024 +0200
+++ b/thermferm/one-wire.c	Thu Apr 11 13:18:20 2024 +0200
@@ -160,7 +160,7 @@
 		pthread_mutex_unlock(&mutexes[LOCK_ONE_WIRE]);
 	    }
 	} else if (strcmp(w1type, (char *)"00")) {
-	    syslog(LOG_NOTICE, "One-wire device %d %s unknown", strlen(buffer), buffer);
+	    syslog(LOG_NOTICE, "One-wire device %ld %s unknown", strlen(buffer), buffer);
 	}
     }
     fclose(fp);
@@ -255,6 +255,33 @@
 //			    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;
+			    }
+
+			    if (output != newval) {
+				if ((rc = 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;
+				} else {
+				    syslog(LOG_NOTICE, "One-wire device %s-%d write output rc=%d", dev_w1->address, i, rc);
+				}
+			    }
+			} else {
+			    syslog(LOG_NOTICE, "One-wire device %s-%d read state rc=%d", dev_w1->address, i, rc);
+			}
 		    }
 		} /* for (device = Config.devices; ... */
 	    }
--- a/thermferm/thermferm.h	Thu Apr 11 11:19:04 2024 +0200
+++ b/thermferm/thermferm.h	Thu Apr 11 13:18:20 2024 +0200
@@ -314,7 +314,6 @@
     char			family[3];	///< Device family
     int				present;	///< Present on bus
     int				value;		///< Last value
-//    int				subdevices;	///< Number of subdevices
     time_t			timestamp;	///< Last seen
 } w1_list;
 

mercurial