thermferm/thermferm.c

changeset 645
49eb753a958b
parent 644
07cc86900473
child 646
e3edc783006b
--- a/thermferm/thermferm.c	Sat Mar 23 09:31:01 2024 +0100
+++ b/thermferm/thermferm.c	Sat Mar 23 12:18:14 2024 +0100
@@ -50,9 +50,8 @@
 units_list		*current_unit = NULL;		/* In panel editor this points to the current unit. */
 float			temp_temp = 20.0;
 
-#ifndef HAVE_WIRINGPI_H
 pthread_t		threads[5];
-#endif
+pthread_mutex_t		mutexes[5];
 extern const char       UNITMODE[5][8];
 extern const char	PROFSTATE[4][6];
 
@@ -141,9 +140,9 @@
 {
     char		buf[21];
 
+    pthread_mutex_lock(&mutexes[LOCK_LCD]);
+    pthread_mutex_lock(&mutexes[LOCK_MENU]);
 #ifdef HAVE_WIRINGPI_H
-    piLock(LOCK_LCD);
-    piLock(LOCK_MENU);
     lcdClear(lcdHandle);
     lcdPosition(lcdHandle, 0, 0);
 #endif
@@ -374,25 +373,21 @@
 				break;
     }
 
-#ifdef HAVE_WIRINGPI_H
-    piUnlock(LOCK_MENU);
-    piUnlock(LOCK_LCD);
-#endif
+    pthread_mutex_unlock(&mutexes[LOCK_MENU]);
+    pthread_mutex_unlock(&mutexes[LOCK_LCD]);
 }
 
 
 
 void stopLCD(void)
 {
+    pthread_mutex_lock(&mutexes[LOCK_LCD]);
 #ifdef HAVE_WIRINGPI_H
-    piLock(LOCK_LCD);
     lcdClear(lcdHandle);
 #endif
     slcdClear(slcdHandle);
     setBacklight(0);
-#ifdef HAVE_WIRINGPI_H
-    piUnlock(LOCK_LCD);
-#endif
+    pthread_mutex_unlock(&mutexes[LOCK_LCD]);
 }
 
 
@@ -1014,9 +1009,7 @@
     int			row, rc, run = 1, seconds = 0, minutes = 0, temp;
     int			run_seconds, run_minutes, run_hours, tot_minutes, key;
     struct tm		*tm;
-#ifndef HAVE_WIRINGPI_H
     long		t = 0;
-#endif
     int			current_step, valid_step, time_until_now, previous_fridge_mode;
     float		previous_target_lo, previous_target_hi;
     float		LCDair, LCDbeer, LCDspL, LCDspH;
@@ -1036,61 +1029,37 @@
 	wrconfig();
     }
 
-#ifdef HAVE_WIRINGPI_H
-    rc = piThreadCreate(my_devices_loop);
-#else
     rc = pthread_create(&threads[t], NULL, my_devices_loop, (void *)t );
-#endif
     if (rc) {
 	fprintf(stderr, "my_devices_loop thread didn't start rc=%d\n", rc);
 	syslog(LOG_NOTICE, "my_devices_loop thread didn't start rc=%d", rc);
-#ifndef HAVE_WIRINGPI_H
     } else {
 	t++;
-#endif
     }
 
-#ifdef HAVE_WIRINGPI_H
-    rc = piThreadCreate(my_server_loop);
-#else
     rc = pthread_create(&threads[t], NULL, my_server_loop, (void *)t );
-#endif
     if (rc) {
 	fprintf(stderr, "my_server_loop thread didn't start rc=%d\n", rc);
 	syslog(LOG_NOTICE, "my_server_loop thread didn't start rc=%d", rc);
-#ifndef HAVE_WIRINGPI_H
     } else {
 	t++;
-#endif
     }
 
-#ifdef HAVE_WIRINGPI_H
-    rc = piThreadCreate(my_panel_loop);
-#else
     rc = pthread_create(&threads[t], NULL, my_panel_loop, (void *)t );
-#endif
     if (rc) {
 	fprintf(stderr, "my_panel_loop thread didn't start rc=%d\n", rc);
 	syslog(LOG_NOTICE, "my_panel_loop thread didn't start rc=%d", rc);
-#ifndef HAVE_WIRINGPI_H
     } else {
 	t++;
-#endif
     }
 
 #ifdef USE_SIMULATOR
-#ifdef HAVE_WIRINGPI_H
-    rc = piThreadCreate(my_simulator_loop);
-#else
     rc = pthread_create(&threads[t], NULL, my_simulator_loop, (void *)t );
-#endif
     if (rc) {
 	fprintf(stderr, "my_simulator_loop thread didn't start rc=%d\n", rc);
 	syslog(LOG_NOTICE, "my_simulator_loop thread didn't start rc=%d", rc);
-#ifndef HAVE_WIRINGPI_H
     } else {
 	t++;
-#endif
     }
 #endif
 
@@ -1132,15 +1101,10 @@
 	}
     }
 
-#ifdef HAVE_WIRINGPI_H
-    piLock(LOCK_LCD);
-#endif
+    pthread_mutex_lock(&mutexes[LOCK_LCD]);
     lcd_buf_write(1, (char *)"   ThermFerm    ");
     lcd_buf_write(2, (char *)" Version %s     ", VERSION);
-#ifdef HAVE_WIRINGPI_H
-    piUnlock(LOCK_LCD);
-#endif
-
+    pthread_mutex_unlock(&mutexes[LOCK_LCD]);
 
     do {
 	if (my_shutdown)
@@ -1160,17 +1124,13 @@
 	    }
 	    syslog(LOG_NOTICE, "run_pause: leaving hold state");
 	    run_hold = FALSE;
-#ifdef HAVE_WIRINGPI_H
 	    /*
 	     * In case the LCD buffers were cleared, setup the first page.
 	     */
-	    piLock(LOCK_LCD);
-#endif
+	    pthread_mutex_lock(&mutexes[LOCK_LCD]);
 	    lcd_buf_write(1, (char *)"   ThermFerm    ");
 	    lcd_buf_write(2, (char *)" Version %s     ", VERSION);
-#ifdef HAVE_WIRINGPI_H
-	    piUnlock(LOCK_LCD);
-#endif
+	    pthread_mutex_unlock(&mutexes[LOCK_LCD]);
 	}
 
 	now = time(NULL);
@@ -1183,22 +1143,14 @@
 
 	    row = 3;
 	    tm = localtime(&now);
-#ifdef HAVE_WIRINGPI_H
-	    piLock(LOCK_LCD);
-#endif
+	    pthread_mutex_lock(&mutexes[LOCK_LCD]);
 	    lcd_buf_write(row++, "   %02d-%02d-%04d   ", tm->tm_mday, tm->tm_mon + 1, tm->tm_year + 1900);
 	    lcd_buf_write(row++, "    %02d:%02d:%02d    ", tm->tm_hour, tm->tm_min, tm->tm_sec);
-#ifdef HAVE_WIRINGPI_H
-	    piUnlock(LOCK_LCD);
-#endif
+	    pthread_mutex_unlock(&mutexes[LOCK_LCD]);
 
-#ifdef HAVE_WIRINGPI_H
-	    piLock(LOCK_LCD);
-#endif
+	    pthread_mutex_lock(&mutexes[LOCK_LCD]);
 	    lcd_buf_write(row, "Room temp N/A       ");
-#ifdef HAVE_WIRINGPI_H
-	    piUnlock(LOCK_LCD);
-#endif
+	    pthread_mutex_unlock(&mutexes[LOCK_LCD]);
 	    int updateHT = 0;
             if (Config.temp_address) {
 		rc = device_in(Config.temp_address, &temp);
@@ -1207,13 +1159,9 @@
 			updateHT = 1;
 		    Config.temp_value = temp;
 		    Config.temp_state = 0;
-#ifdef HAVE_WIRINGPI_H
-		    piLock(LOCK_LCD);
-#endif
+		    pthread_mutex_lock(&mutexes[LOCK_LCD]);
 		    lcd_buf_write(row, "Room temp %.1f%c    ", Config.temp_value / 1000.0, 0x01);
-#ifdef HAVE_WIRINGPI_H
-		    piUnlock(LOCK_LCD);
-#endif
+		    pthread_mutex_unlock(&mutexes[LOCK_LCD]);
 		} else if (rc == DEVPRESENT_ERROR) {
 		    Config.temp_state = 1;
 		} else {
@@ -1223,13 +1171,9 @@
 	    }
 	    row++;
 
-#ifdef HAVE_WIRINGPI_H
-	    piLock(LOCK_LCD);
-#endif
+	    pthread_mutex_lock(&mutexes[LOCK_LCD]);
 	    lcd_buf_write(row, " Humidity N/A       ");
-#ifdef HAVE_WIRINGPI_H
-	    piUnlock(LOCK_LCD);
-#endif
+	    pthread_mutex_unlock(&mutexes[LOCK_LCD]);
 
 	    if (Config.hum_address) {
 		rc = device_in(Config.hum_address, &temp);
@@ -1238,13 +1182,9 @@
 			updateHT = 1;
 		    Config.hum_value = temp;
 		    Config.hum_state = 0;
-#ifdef HAVE_WIRINGPI_H
-		    piLock(LOCK_LCD);
-#endif
+		    pthread_mutex_lock(&mutexes[LOCK_LCD]);
 		    lcd_buf_write(row, " Humidity %.1f%%     ", Config.hum_value / 1000.0);
-#ifdef HAVE_WIRINGPI_H
-		    piUnlock(LOCK_LCD);
-#endif
+		    pthread_mutex_unlock(&mutexes[LOCK_LCD]);
 		} else if (rc == DEVPRESENT_ERROR) {
 		    Config.hum_state = 1;
 		} else {
@@ -1843,9 +1783,7 @@
 		if (seconds == 60) {
 		    unit->mqtt_flag |= MQTT_FLAG_DATA;
 		}
-#ifdef HAVE_WIRINGPI_H
-		piLock(LOCK_LCD);
-#endif
+		pthread_mutex_lock(&mutexes[LOCK_LCD]);
 		/*
 		 * Write 4 rows to the LCD to display the unit state
 		 */
@@ -1853,9 +1791,7 @@
 		lcd_buf_write(row++, "%s                   ", unit->product_name);
 		lcd_buf_write(row++, "%c%5.1f\2 A%6.2f\1    ", LCDstatC, LCDspH, LCDair);
 		lcd_buf_write(row++, "%c%5.1f\2 B%6.2f\1    ", LCDstatH, LCDspL, LCDbeer);
-#ifdef HAVE_WIRINGPI_H
-		piUnlock(LOCK_LCD);
-#endif
+		pthread_mutex_unlock(&mutexes[LOCK_LCD]);
 
 		/*
 		 * Publish MQTT messages set in flag
@@ -1883,21 +1819,13 @@
 		}
 	    } /* for units */
 
-#ifdef HAVE_WIRINGPI_H
-	    piLock(LOCK_MENU);
-#endif
+	    pthread_mutex_lock(&mutexes[LOCK_MENU]);
 	    if (setupmenu == MENU_NONE) {
-#ifdef HAVE_WIRINGPI_H
-    		piLock(LOCK_LCD);
-#endif
+		pthread_mutex_lock(&mutexes[LOCK_LCD]);
 	    	lcd_buf_show();
-#ifdef HAVE_WIRINGPI_H
-		piUnlock(LOCK_LCD);
-#endif
+		pthread_mutex_unlock(&mutexes[LOCK_LCD]);
 	    }
-#ifdef HAVE_WIRINGPI_H
-	    piUnlock(LOCK_MENU);
-#endif
+	    pthread_mutex_unlock(&mutexes[LOCK_MENU]);
 
 	    if (seconds == 60) {
 		seconds = 0;

mercurial