Added MIGRATION document. Trying to drop usleep() call to replace by nanosleep(). Some code cleanup.

Sat, 23 Mar 2024 09:31:01 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 23 Mar 2024 09:31:01 +0100
changeset 644
07cc86900473
parent 643
586d376ab629
child 645
49eb753a958b

Added MIGRATION document. Trying to drop usleep() call to replace by nanosleep(). Some code cleanup.

MIGRATION file | annotate | diff | comparison | revisions
thermferm/devices.c file | annotate | diff | comparison | revisions
thermferm/thermferm.c file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MIGRATION	Sat Mar 23 09:31:01 2024 +0100
@@ -0,0 +1,43 @@
+		Migration from wiringPi to PIGPIO.
+		==================================
+
+WiringPi is unmaintained and replaced by pigpio. This will need large parts
+of thermferm to be rewritten. It is also a good moment to replace the current
+server protocol with json data and start sending data chaanges over
+websockets.
+
+Some parts of the code can make use of direct kernel support, since kernel 4
+there are lots of new modules. Fact is that thermferm is started in 2014,
+that is 10 years ago.
+
+The current devices model is not very practical. We need to separate the parts
+that don't need gpio out of it. All one-wire devices need their own driver
+thread.
+
+
+	Current threads.
+	----------------
+
+These are the current running threads.
+1. my_devices_loop.
+2. my_server_loop.
+3. my_panel_loop.
+4. my_simulator_loop (if enabled).
+
+
+
+	Steps to do.
+	------------
+
+Write a new thread for the one-wire devices. Make it a dynamic table. The
+old device table is still needed to tie devices to fermenters.
+Try to read all temperature sensors at once. The kernel supports this now.
+The devices thread needs to use the collected temperatures.
+Read all ds2413 devices from sys/bus/w1. If output bits are set different
+then send output values.
+Create tables for ds28b20 and ds2413. Simulated and real sensors should
+share these tables.
+
+
+Add pigpio library and make it testable by the configure script.
+
--- a/thermferm/devices.c	Fri Mar 22 11:48:35 2024 +0100
+++ b/thermferm/devices.c	Sat Mar 23 09:31:01 2024 +0100
@@ -77,6 +77,7 @@
 void dht11Read(void) {
     int         	tries = 5;
     unsigned short	got_correct_data = 0;
+    struct timespec	ts;
 
     if (dht11_pin == -1)
 	return;
@@ -92,13 +93,19 @@
 	 */
 	pinMode(dht11_pin, OUTPUT);                 
 	digitalWrite(dht11_pin, HIGH);
-	usleep(1000);
+	/* 1 mSec */
+	ts.tv_sec = 0;
+	ts.tv_nsec = 1000000;
+	nanosleep(&ts, &ts);
 
 	/*
 	 * Low for at least 18 milliseconds
 	 */
 	digitalWrite(dht11_pin, LOW);
-	usleep(20000);
+	/* 20 mSec */
+	ts.tv_sec = 0;
+	ts.tv_nsec = 20 * 1000000;
+	nanosleep(&ts, &ts);
 	digitalWrite(dht11_pin, HIGH);
 	pinMode(dht11_pin, INPUT);
 
@@ -107,9 +114,17 @@
 	 */
 	for (i=0; i<MAXTIMINGS; i++) {
 	    counter = 0;
+	    /* 10 uS */
+	    ts.tv_sec = 0;
+	    ts.tv_nsec = 10 * 1000;
+//	    nanosleep(&ts, NULL);
 	    delayMicroseconds(10);
 	    while (sizecvt(digitalRead(dht11_pin)) == laststate) {
 		counter++;
+		/* 1 uS */
+		ts.tv_sec = 0;
+		ts.tv_nsec = 1000;
+//		nanosleep(&ts, NULL);
 		delayMicroseconds(1);
 		if (counter == 255) {
 		    break;
@@ -164,6 +179,7 @@
 		dht11_temperature = t;
 		dht11_humidity = h;
 		dht11_valid = TRUE;
+		syslog(LOG_NOTICE, "dht11 t:%d h:%d tries:%d", t, h, 6-tries);
 	} else {
 		tries--;
 		if (tries == 0)
--- a/thermferm/thermferm.c	Fri Mar 22 11:48:35 2024 +0100
+++ b/thermferm/thermferm.c	Sat Mar 23 09:31:01 2024 +0100
@@ -1273,7 +1273,6 @@
 		    if (rc == DEVPRESENT_YES) {
 			if (unit->air_temperature != temp) {
 			    unit->mqtt_flag |= MQTT_FLAG_DATA;
-//			    pub_domoticz_temp(unit->air_idx, temp);
 			}
 			unit->air_temperature = temp;
 			unit->air_state = 0;
@@ -1293,7 +1292,6 @@
 		    if (rc == DEVPRESENT_YES) {
 			if (unit->beer_temperature != temp) {
 			    unit->mqtt_flag |= MQTT_FLAG_DATA;
-//			    pub_domoticz_temp(unit->beer_idx, temp);
 			}
     			unit->beer_temperature = temp;
 			unit->beer_state = 0;
@@ -1309,7 +1307,6 @@
 		    if (rc == DEVPRESENT_YES) {
 			if (unit->chiller_temperature != temp) {
 			    unit->mqtt_flag |= MQTT_FLAG_DATA;
-//			    pub_domoticz_temp(unit->chiller_idx, temp);
 			}
 			unit->chiller_temperature = temp;
 			unit->chiller_state = 0;
@@ -1330,14 +1327,12 @@
 			    if (unit->door_state == 0) {
 			    	syslog(LOG_NOTICE, "Unit `%s' door closed", unit->alias);
 			    	unit->door_state = 1;
-//				pub_domoticz_output(unit->door_idx, unit->door_state);
 				unit->mqtt_flag |= MQTT_FLAG_DATA;
 			    }
 			} else {
 			    if (unit->door_state) {
 			    	syslog(LOG_NOTICE, "Unit `%s' door opened", unit->alias);
 			    	unit->door_state = 0;
-//				pub_domoticz_output(unit->door_idx, unit->door_state);
 				unit->mqtt_flag |= MQTT_FLAG_DATA;
 			    }
 			    /*
@@ -1364,14 +1359,12 @@
 			    if (unit->psu_state == 0) {
 				syslog(LOG_NOTICE, "Unit `%s' PSU (12 volt) is on", unit->alias);
 				unit->psu_state = 1;
-//				pub_domoticz_output(unit->psu_idx, unit->psu_state);
 				unit->mqtt_flag |= MQTT_FLAG_DATA;
 			    }
 			} else {
 			    if (unit->psu_state) {
 				syslog(LOG_NOTICE, "Unit `%s' PSU (12 volt) is off", unit->alias);
 				unit->psu_state = 0;
-//				pub_domoticz_output(unit->psu_idx, unit->psu_state);
 				unit->mqtt_flag |= MQTT_FLAG_DATA;
 			    }
 			    unit->alarm_flag |= ALARM_FLAG_PSU;
@@ -1716,7 +1709,6 @@
 				if (unit->heater_state != power) {
 				    syslog(LOG_NOTICE, "Unit `%s' heater %d%% => %d%%", unit->alias, unit->heater_state, power);
 				    unit->heater_state = power;
-//				    pub_domoticz_output(unit->heater_idx, unit->heater_state);
 				    if (unit->heater_address) {
 					unit->mqtt_flag |= MQTT_FLAG_DATA;
 				    }
@@ -1729,7 +1721,6 @@
 				if (unit->heater_state) {
 				    syslog(LOG_NOTICE, "Unit `%s' heater On => Off", unit->alias);
 				    unit->heater_state = 0;
-//				    pub_domoticz_output(unit->heater_idx, unit->heater_state);
 				    if (unit->heater_address) {
 					unit->mqtt_flag |= MQTT_FLAG_DATA;
 				    }
@@ -1752,7 +1743,6 @@
 				if (unit->cooler_state != power) {
 				    syslog(LOG_NOTICE, "Unit `%s' cooler %d%% => %d%%", unit->alias, unit->cooler_state, power);
 				    unit->cooler_state = power;
-//				    pub_domoticz_output(unit->cooler_idx, unit->cooler_state);
 				    if (unit->cooler_address) {
 					unit->mqtt_flag |= MQTT_FLAG_DATA;
 				    }
@@ -1765,7 +1755,6 @@
 				if (unit->cooler_state) {
 				    syslog(LOG_NOTICE, "Unit `%s' cooler On => Off", unit->alias);
 				    unit->cooler_state = 0;
-//				    pub_domoticz_output(unit->cooler_idx, unit->cooler_state);
 				    if (unit->cooler_address) {
 					unit->mqtt_flag |= MQTT_FLAG_DATA;
 				    }
@@ -1792,7 +1781,6 @@
 			    	if (! unit->fan_state) {
 				    syslog(LOG_NOTICE, "Unit `%s' Fan Off => On", unit->alias);
 				    unit->fan_state = 100;
-//				    pub_domoticz_output(unit->fan_idx, unit->fan_state);
 				    if (unit->fan_address) {
 					unit->mqtt_flag |= MQTT_FLAG_DATA;
 				    }
@@ -1805,7 +1793,6 @@
 			    	if (unit->fan_state) {
 				    syslog(LOG_NOTICE, "Unit `%s' Fan On => Off", unit->alias);
 			    	    unit->fan_state = 0;
-//				    pub_domoticz_output(unit->fan_idx, unit->fan_state);
 				    if (unit->fan_address) {
 					unit->mqtt_flag |= MQTT_FLAG_DATA;
 				    }

mercurial