Moved all delay functions into a new general file.

Wed, 27 Mar 2024 17:19:46 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Wed, 27 Mar 2024 17:19:46 +0100
changeset 652
16d3d4b58b5b
parent 651
da166cb8470f
child 653
6c4c884be155

Moved all delay functions into a new general file.

thermferm/Makefile file | annotate | diff | comparison | revisions
thermferm/delay.c file | annotate | diff | comparison | revisions
thermferm/delay.h file | annotate | diff | comparison | revisions
thermferm/devices.c file | annotate | diff | comparison | revisions
thermferm/mqtt.c file | annotate | diff | comparison | revisions
thermferm/panel.c file | annotate | diff | comparison | revisions
thermferm/rc-switch.c file | annotate | diff | comparison | revisions
thermferm/server.c file | annotate | diff | comparison | revisions
thermferm/simulator.c file | annotate | diff | comparison | revisions
thermferm/thermferm.c file | annotate | diff | comparison | revisions
--- a/thermferm/Makefile	Wed Mar 27 15:49:31 2024 +0100
+++ b/thermferm/Makefile	Wed Mar 27 17:19:46 2024 +0100
@@ -55,19 +55,20 @@
 
 # DO NOT DELETE THIS LINE - MAKE DEPEND RELIES ON IT
 # Dependencies generated by make depend
-mqtt.o: thermferm.h rdconfig.h devices.h xutil.h mqtt.h
-rc-switch.o: thermferm.h xutil.h rc-switch.h
-slcd.o: thermferm.h slcd.h futil.h xutil.h
-panel.o: thermferm.h lcd-pcf8574.h slcd.h panel.h
-devices.o: thermferm.h devices.h rc-switch.h panel.h xutil.h
+mqtt.o: thermferm.h rdconfig.h devices.h xutil.h delay.h mqtt.h
+thermferm.o: lock.h rdconfig.h devices.h server.h thermferm.h delay.h simulator.h lcd-pcf8574.h lcd-buffer.h slcd.h panel.h futil.h xutil.h pid.h mqtt.h
+panel.o: thermferm.h delay.h lcd-pcf8574.h slcd.h panel.h
+devices.o: thermferm.h delay.h devices.h rc-switch.h panel.h xutil.h
 lcd-buffer.o: thermferm.h lcd-buffer.h lcd-pcf8574.h slcd.h panel.h
-futil.o: thermferm.h futil.h
-thermferm.o: lock.h rdconfig.h devices.h server.h thermferm.h simulator.h lcd-pcf8574.h lcd-buffer.h slcd.h panel.h futil.h xutil.h pid.h mqtt.h
-lock.o: lock.h thermferm.h
 lcd-pcf8574.o: thermferm.h lcd-pcf8574.h slcd.h
 pid.o: thermferm.h pid.h
+rc-switch.o: thermferm.h xutil.h delay.h rc-switch.h
+lock.o: lock.h thermferm.h
+futil.o: thermferm.h futil.h
+delay.o: delay.h thermferm.h
 xutil.o: thermferm.h xutil.h
-server.o: rdconfig.h thermferm.h devices.h server.h lcd-buffer.h xutil.h mqtt.h
-simulator.o: thermferm.h simulator.h
+server.o: rdconfig.h thermferm.h delay.h devices.h server.h lcd-buffer.h xutil.h mqtt.h
+simulator.o: thermferm.h delay.h simulator.h
 rdconfig.o: rdconfig.h thermferm.h pid.h futil.h xutil.h
+slcd.o: thermferm.h slcd.h futil.h xutil.h
 # End of generated dependencies
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/thermferm/delay.c	Wed Mar 27 17:19:46 2024 +0100
@@ -0,0 +1,117 @@
+/**
+ * @file delay.c
+ * #brief Thread friendly delay functions.
+ *
+ * Copyright (C) 2024
+ *   
+ * Michiel Broek <mbroek at mbse dot eu>
+ *
+ * This file is part of the mbsePi-apps
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2, or (at your option) any
+ * later version.
+ *
+ * mbsePi-apps is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with ThermFerm; see the file COPYING.  If not, write to the Free
+ * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#include "delay.h"
+#include "thermferm.h"
+
+
+void delay (unsigned int howLong)
+{
+    struct timespec sleeper, dummy ;
+
+    sleeper.tv_sec  = (time_t)(howLong / 1000) ;
+    sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
+
+    nanosleep (&sleeper, &dummy) ;
+}
+
+
+
+
+/**
+ * @brief mDelay Delay number of milliseconds.
+ * @param howLong The number of milliseconds.
+ */
+void mDelay(unsigned int howLong)
+{
+    struct timespec sleeper ;
+    unsigned int mSecs = howLong % 1000 ;
+    unsigned int wSecs = howLong / 1000 ;
+
+    /**/ if (howLong == 0)
+	return ;
+    else {
+	sleeper.tv_sec  = wSecs ;
+	sleeper.tv_nsec = (long)(mSecs * 1000000L) ;
+	nanosleep (&sleeper, NULL) ;
+    }
+}
+
+
+/*
+ * Code from wiringPi, Gordon.
+ *
+ * delayMicroseconds:
+ *      This is somewhat intersting. It seems that on the Pi, a single call
+ *      to nanosleep takes some 80 to 130 microseconds anyway, so while
+ *      obeying the standards (may take longer), it's not always what we
+ *      want!
+ *
+ *      So what I'll do now is if the delay is less than 100uS we'll do it
+ *      in a hard loop, watching a built-in counter on the ARM chip. This is
+ *      somewhat sub-optimal in that it uses 100% CPU, something not an issue
+ *      in a microcontroller, but under a multi-tasking, multi-user OS, it's
+ *      wastefull, however we've no real choice )-:
+ *
+ *      Plan B: It seems all might not be well with that plan, so changing it
+ *      to use gettimeofday () and poll on that instead...
+ *********************************************************************************
+ */
+void delayMicrosecondsHard (unsigned int howLong)
+{
+    struct timeval tNow, tLong, tEnd ;
+
+    gettimeofday (&tNow, NULL) ;
+    tLong.tv_sec  = howLong / 1000000 ;
+    tLong.tv_usec = howLong % 1000000 ;
+    timeradd (&tNow, &tLong, &tEnd) ;
+
+    while (timercmp (&tNow, &tEnd, <))
+	gettimeofday (&tNow, NULL) ;
+}
+
+
+/**
+ * @brief uDelay Delays a number of microseconds. Also used to replace usleep().
+ * @param howLong The number is microseconds.
+ */
+void uDelay(unsigned int howLong)
+{
+    struct timespec sleeper ;
+    unsigned int uSecs = howLong % 1000000 ;
+    unsigned int wSecs = howLong / 1000000 ;
+
+    /**/ if (howLong == 0)
+	return ;
+    else if (howLong < 100)
+	delayMicrosecondsHard(howLong) ;
+    else {
+	sleeper.tv_sec  = wSecs ;
+	sleeper.tv_nsec = (long)(uSecs * 1000L) ;
+	nanosleep (&sleeper, NULL) ;
+    }
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/thermferm/delay.h	Wed Mar 27 17:19:46 2024 +0100
@@ -0,0 +1,12 @@
+/**
+ * @file delay.h
+ * @brief Thread friendly delay functions.
+ */
+
+#ifndef	DELAY_H
+#define	DELAY_H
+
+void mDelay(unsigned int howLong);
+void uDelay(unsigned int howLong);
+
+#endif
--- a/thermferm/devices.c	Wed Mar 27 15:49:31 2024 +0100
+++ b/thermferm/devices.c	Wed Mar 27 17:19:46 2024 +0100
@@ -21,6 +21,7 @@
  *****************************************************************************/
 
 #include "thermferm.h"
+#include "delay.h"
 #include "devices.h"
 #include "rc-switch.h"
 #include "panel.h"
@@ -996,14 +997,14 @@
 	    /*
 	     * Delay a bit after procesing a device.
 	     */
-	    usleep(10000);
+	    mDelay(10);
 	}
 	if (my_shutdown)
 	    break;
 	/*
 	 * Delay a bit after all devices
 	 */
-	usleep(100000);
+	mDelay(100);
     }
 
     syslog(LOG_NOTICE, "Thread my_devices_loop stopped");
--- a/thermferm/mqtt.c	Wed Mar 27 15:49:31 2024 +0100
+++ b/thermferm/mqtt.c	Wed Mar 27 17:19:46 2024 +0100
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * Copyright (C) 2016-2023
+ * Copyright (C) 2016-2024
  *   
  * Michiel Broek <mbroek at mbse dot eu>
  *
@@ -26,6 +26,7 @@
 #include "rdconfig.h"
 #include "devices.h"
 #include "xutil.h"
+#include "delay.h"
 #include "mqtt.h"
 
 extern sys_config       Config;
@@ -1731,7 +1732,7 @@
                     mosquitto_disconnect(mosq);
                     mqtt_disconnect_sent = TRUE;
                 }
-                usleep(100000);
+		mDelay(100);
             }
             rc = MOSQ_ERR_SUCCESS;
         } while (rc == MOSQ_ERR_SUCCESS && mqtt_connected);
--- a/thermferm/panel.c	Wed Mar 27 15:49:31 2024 +0100
+++ b/thermferm/panel.c	Wed Mar 27 17:19:46 2024 +0100
@@ -21,6 +21,7 @@
  *****************************************************************************/
 
 #include "thermferm.h"
+#include "delay.h"
 #include "lcd-pcf8574.h"
 #include "slcd.h"
 #include "panel.h"
@@ -227,7 +228,7 @@
 	/*
 	 * Loop 10 milliseconds
 	 */
-	usleep(10000);
+	mDelay(10);
     }
 
     syslog(LOG_NOTICE, "Thread my_panel_loop stopped");
--- a/thermferm/rc-switch.c	Wed Mar 27 15:49:31 2024 +0100
+++ b/thermferm/rc-switch.c	Wed Mar 27 17:19:46 2024 +0100
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * Copyright (C) 2014
+ * Copyright (C) 2014-2024
  *   
  * Michiel Broek <mbroek at mbse dot eu>
  *
@@ -25,6 +25,7 @@
 
 #include "thermferm.h"
 #include "xutil.h"
+#include "delay.h"
 #include "rc-switch.h"
 
 #ifdef HAVE_WIRINGPI_H
@@ -594,9 +595,11 @@
             disabled_Receive = TRUE;
         }
         digitalWrite(rcTransmitterPin, bHighFirst ? HIGH : LOW);
-	delayMicroseconds( rcPulseLength * nFirstPulses);
+	uDelay(rcPulseLength * nFirstPulses);
+//	delayMicroseconds( rcPulseLength * nFirstPulses);
         digitalWrite(rcTransmitterPin, bHighFirst ? LOW : HIGH);
-	delayMicroseconds( rcPulseLength * nSecondPulses);
+	uDelay(rcPulseLength * nSecondPulses);
+//	delayMicroseconds( rcPulseLength * nSecondPulses);
         
         if (disabled_Receive) {
             enableReceiveIRQ(nReceiverInterrupt_backup);
@@ -663,7 +666,8 @@
         transmit(4,71,TRUE);
     } else if (rcProtocol == TYPE_D) {
 	transmit(0,1,FALSE);
-	delayMicroseconds(80000);
+	mDelay(80);
+//	delayMicroseconds(80000);
     }
 }
 
--- a/thermferm/server.c	Wed Mar 27 15:49:31 2024 +0100
+++ b/thermferm/server.c	Wed Mar 27 17:19:46 2024 +0100
@@ -22,6 +22,7 @@
 
 #include "rdconfig.h"
 #include "thermferm.h"
+#include "delay.h"
 #include "devices.h"
 #include "server.h"
 #include "lcd-buffer.h"
@@ -1376,7 +1377,7 @@
 	 */
 	run_pause = TRUE;
 	for (;;) {
-	    usleep(100000);
+	    mDelay(100);
 	    if (run_hold)
 		break;
 	}
@@ -1405,7 +1406,7 @@
 	 */
 	run_pause = TRUE;
 	for (;;) {
-	    usleep(100000);
+	    mDelay(100);
 	    if (run_hold)
 		break;
 	}
@@ -1532,7 +1533,7 @@
 	 */
 	run_pause = TRUE;
 	for (;;) {
-	    usleep(100000);
+	    mDelay(100);
 	    if (run_hold)
 		break;
 	}
@@ -2229,7 +2230,7 @@
 	    my_server_state = 0;
 	    return 0;
 	}
-	usleep(100000);
+	mDelay(100);
     }
 }
 
--- a/thermferm/simulator.c	Wed Mar 27 15:49:31 2024 +0100
+++ b/thermferm/simulator.c	Wed Mar 27 17:19:46 2024 +0100
@@ -21,6 +21,7 @@
  *****************************************************************************/
 
 #include "thermferm.h"
+#include "delay.h"
 #include "simulator.h"
 
 int			my_simulator_state = 0;
@@ -124,9 +125,9 @@
 		simulator->air_temperature += ((simulator->beer_temperature - simulator->air_temperature) / 2500.0);
 		simulator->chiller_temperature = simulator->cooler_temp;	// Libk these
 	    }
-	    usleep(100000);
+	    mDelay(100L);
 	}
-	usleep(50000);
+	mDelay(50L);
     }
 
     syslog(LOG_NOTICE, "Thread my_simulator_loop stopped");
--- a/thermferm/thermferm.c	Wed Mar 27 15:49:31 2024 +0100
+++ b/thermferm/thermferm.c	Wed Mar 27 17:19:46 2024 +0100
@@ -25,6 +25,7 @@
 #include "devices.h"
 #include "server.h"
 #include "thermferm.h"
+#include "delay.h"
 #include "simulator.h"
 #include "lcd-pcf8574.h"
 #include "lcd-buffer.h"
@@ -1128,7 +1129,7 @@
 	    run_hold = TRUE;
 	    syslog(LOG_NOTICE, "run_pause: entering hold state");
 	    for (;;) {
-		usleep(100000);
+		mDelay(100);
 		if (! run_pause)
 		    break;
 	    }
@@ -1177,7 +1178,7 @@
 		} else {
 		    Config.temp_state = 2;
 		}
-		usleep(10000);
+		mDelay(10);
 	    }
 	    row++;
 
@@ -1200,7 +1201,7 @@
 		} else {
 		    Config.hum_state = 2;
 		}
-		usleep(10000);
+		mDelay(10);
 	    }
 	    row++;
 	    /*
@@ -1876,7 +1877,7 @@
 	if (key != KEY_NONE)
 	    panel_key_events(key);
 
-	usleep(100000);
+	mDelay(100);
 
     } while (run);
 

mercurial