Compiles with 1-wire bus on PC hardware

Fri, 20 Jun 2014 17:42:36 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Fri, 20 Jun 2014 17:42:36 +0200
changeset 66
805f1d285acd
parent 65
a08a1fce439e
child 67
4a26aaa34fa1

Compiles with 1-wire bus on PC hardware

thermferm/rdconfig.c file | annotate | diff | comparison | revisions
thermferm/sensors.c file | annotate | diff | comparison | revisions
thermferm/server.c file | annotate | diff | comparison | revisions
thermferm/thermferm.c file | annotate | diff | comparison | revisions
thermferm/thermferm.h file | annotate | diff | comparison | revisions
--- a/thermferm/rdconfig.c	Thu Jun 19 15:49:29 2014 +0200
+++ b/thermferm/rdconfig.c	Fri Jun 20 17:42:36 2014 +0200
@@ -22,7 +22,6 @@
 
 #include "thermferm.h"
 
-#ifdef HAVE_WIRINGPI_H
 
 bool		debug = FALSE;
 static char	*mypath;
@@ -32,10 +31,12 @@
 
 
 
-static int getstr(char **);
-static int getint(char **);
+//static int getstr(char **);
+//static int getint(char **);
 static int getw1(char **);
+#ifdef HAVE_WIRINGPI_H
 static int getrcs(char **);
+#endif
 static int getuch(char **);
 static int getfloat(char **);
 //static int getbyt(char **);
@@ -49,10 +50,12 @@
  */
 key_list keytab[] = {
     {(char *)"w1therm",			getw1,		(char **)&Config.w1therms},
+#ifdef HAVE_WIRINGPI_H
     {(char *)"lcd_cols",		getint,		(char **)&Config.lcd_cols},
     {(char *)"lcd_rows",		getint,		(char **)&Config.lcd_rows},
     {(char *)"tx433",			getint,		(char **)&Config.tx433},
     {(char *)"rcswitch",		getrcs,		(char **)&Config.rcswitch},
+#endif
     {(char *)"cs_mode",			getuch,		(char **)&Config.cs_mode},
     {(char *)"cs_beerSet",		getfloat,	(char **)&Config.cs_beerSet},
     {(char *)"cs_fridgeSet",		getfloat,	(char **)&Config.cs_fridgeSet},
@@ -71,7 +74,9 @@
 void killconfig(void)
 {
     w1_therm	*tmp1, *old1;
+#ifdef HAVE_WIRINGPI_H
     rc_switch	*tmp2, *old2;
+#endif
 
     if (Config.name)
 	free(Config.name);
@@ -90,6 +95,7 @@
     Config.w1therms = NULL;
     Config.my_port = 6554;
 
+#ifdef HAVE_WIRINGPI_H
     Config.lcd_cols = 16;
     Config.lcd_rows = 2;
     Config.tx433 = -1;
@@ -105,6 +111,7 @@
 	free(tmp2);
     }
     Config.rcswitch = NULL;
+#endif
 
     defaultControlSettings();
     defaultControlConstants();
@@ -117,7 +124,9 @@
     int		rc = 0;
     FILE	*fp;
     w1_therm    *tmp1;
+#ifdef HAVE_WIRINGPI_H
     rc_switch   *tmp2;
+#endif
 
     if (getenv((char *)"USER") == NULL) {
 	mypath = xstrcpy((char *)"/root");
@@ -138,6 +147,7 @@
     fprintf(fp, "# Configuration file for thermferm %s\n", VERSION);
     fprintf(fp, "\n");
 
+#ifdef HAVE_WIRINGPI_H
     fprintf(fp, "# Radio controllers 433 MHz.\n");
     fprintf(fp, "#\n");
     fprintf(fp, "tx433	%d\n", Config.tx433);
@@ -148,6 +158,7 @@
     fprintf(fp, "lcd_cols	%d\n", Config.lcd_cols);
     fprintf(fp, "lcd_rows	%d\n", Config.lcd_rows);
     fprintf(fp, "\n");
+#endif
 
     fprintf(fp, "# DS18B20 temperature sensors on the 1-wire bus.\n");
     fprintf(fp, "#\n");
@@ -157,6 +168,7 @@
     }
     fprintf(fp, "\n");
 
+#ifdef HAVE_WIRINGPI_H
     fprintf(fp, "# RC switches that we want to control.\n");
     fprintf(fp, "#\n");
     fprintf(fp, "# kwd		address	alias\n");
@@ -164,6 +176,7 @@
 	fprintf(fp, "rcswitch	%s	%s\n", tmp2->address, tmp2->alias);
     }
     fprintf(fp, "\n");
+#endif
 
     fprintf(fp, "# Control Settings.\n");
     fprintf(fp, "#\n");
@@ -286,6 +299,7 @@
 
 
 
+/*
 static int getstr(char **dest)
 {
     if (debug)
@@ -294,9 +308,11 @@
     *dest = xstrcpy(v);
     return 0;
 }
+*/
 
 
 
+/*
 static int getint(char **dest)
 {
     if (debug)
@@ -308,6 +324,7 @@
 	*((int*)dest)=atoi(v);
     return 0;
 }
+*/
 
 
 
@@ -362,6 +379,7 @@
 
 
 
+#ifdef HAVE_WIRINGPI_H
 static int getrcs(char **dest)
 {
     char        *p, *q = NULL, *r = NULL;
@@ -399,6 +417,7 @@
 
     return 0;
 }
+#endif
 
 
 
@@ -467,5 +486,4 @@
 }
 */
 
-#endif
 
--- a/thermferm/sensors.c	Thu Jun 19 15:49:29 2014 +0200
+++ b/thermferm/sensors.c	Fri Jun 20 17:42:36 2014 +0200
@@ -22,16 +22,17 @@
 
 #include "thermferm.h"
 
-#ifdef HAVE_WIRINGPI_H
-
 
 extern bool		debug;
 extern sys_config	Config;
 extern int		my_shutdown;
 
 
-
+#ifdef HAVE_WIRINGPI_H
 PI_THREAD (my_sensors_loop)
+#else
+int my_sensors_loop(void)
+#endif
 {
     w1_therm		*tmp1, *old1;
     char		*device, line[60], *p = NULL;
@@ -46,6 +47,7 @@
      * Loop forever until the external shutdown variable is set.
      */
     for (;;) {
+
     	/*
     	 * Here send our 1-wire sensors values
     	 */
@@ -131,4 +133,3 @@
 
 
 
-#endif
--- a/thermferm/server.c	Thu Jun 19 15:49:29 2014 +0200
+++ b/thermferm/server.c	Fri Jun 20 17:42:36 2014 +0200
@@ -22,12 +22,13 @@
 
 #include "thermferm.h"
 
-#ifdef HAVE_WIRINGPI_H
 
 extern bool		my_shutdown;
 extern bool		debug;
+#ifdef HAVE_WIRINGPI_H
 extern int		lcdHandle;
 extern unsigned char	lcdbuf[MAX_LCDS][20][4];
+#endif
 extern sys_config       Config;
 
 int			s;		/* connected socket			*/
@@ -150,12 +151,14 @@
 		srv_send((char *)"ack");
 	    } else if (strncmp(buf, "lcd", 3) == 0) {
 		sprintf(obuf, "[\"                    \", \"                    \", \"                    \", \"                    \"]");
+#ifdef HAVE_WIRINGPI_H
 		for (i = 0; i < 20; i++) {
 		    obuf[i+2]  = lcdbuf[lcdHandle][i][0];
 		    obuf[i+26] = lcdbuf[lcdHandle][i][1];
 		    obuf[i+50] = lcdbuf[lcdHandle][i][2];
 		    obuf[i+74] = lcdbuf[lcdHandle][i][3];
 		}
+#endif
 		srv_send(obuf);
 	    } else if (strncmp(buf, "getMode", 7) == 0) {
 		srv_send("%c", Config.cs_mode);
@@ -285,8 +288,11 @@
 }
 
 
-
+#ifdef HAVE_WIRINGPI_H
 PI_THREAD (my_server_loop)
+#else
+int my_server_loop(void)
+#endif
 {
     socklen_t   addrlen;
     int         optval = 1;
@@ -317,19 +323,19 @@
     if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1) {
 	syslog(LOG_NOTICE, "Can't setsockopt SO_REUSEADDR socket: %s", strerror(errno));
 	close(ls);
-	return NULL;
+	return 0;
     }
 
     if (bind(ls, (struct sockaddr *)&myaddr_in, sizeof(struct sockaddr_in)) == -1) {
 	syslog(LOG_NOTICE, "Can't bind to listen socket: %s", strerror(errno));
 	close(ls);
-	return NULL;
+	return 0;
     }
 
     if (listen(ls, 5) == -1) {
 	syslog(LOG_NOTICE, "Can't listen on listen socket: %s", strerror(errno));
 	close(ls);
-	return NULL;
+	return 0;
     }
 
     syslog(LOG_NOTICE, "listen socket created %d", ls);
@@ -370,5 +376,3 @@
 }
 
 
-#endif
-
--- a/thermferm/thermferm.c	Thu Jun 19 15:49:29 2014 +0200
+++ b/thermferm/thermferm.c	Fri Jun 20 17:42:36 2014 +0200
@@ -22,8 +22,6 @@
 
 #include "thermferm.h"
 
-#ifdef HAVE_WIRINGPI_H
-
 
 int			tempA = 80;
 int			tempB = 80;
@@ -38,17 +36,21 @@
 
 extern bool		debug;
 extern sys_config	Config;
+#ifdef HAVE_WIRINGPI_H
 extern int		lcdHandle;
 extern unsigned char	lcdbuf[MAX_LCDS][20][4];
+#endif
 int			lcdupdate;
 
 
 int  server(void);
 void help(void);
 void die(int);
+#ifdef HAVE_WIRINGPI_H
 void sendRCswitch(char *, int);
 void stopLCD(void);
 void stopRCswitch(void);
+#endif
 
 
 
@@ -79,6 +81,7 @@
 
 
 
+#ifdef HAVE_WIRINGPI_H
 void sendRCswitch(char *address, int state)
 {
     char    *cmd = NULL;
@@ -95,9 +98,10 @@
     syslog(LOG_NOTICE, "Switch %s rc=%d", cmd, rc);
     free(cmd);
 }
+#endif
 
 
-
+#ifdef HAVE_WIRINGPI_H
 void stopLCD(void)
 {
     mb_lcdClear(lcdHandle);
@@ -124,6 +128,7 @@
 	cmd = NULL;
     }
 }
+#endif
 
 
 
@@ -131,7 +136,9 @@
 {
     int		rc, c, i;
     pid_t	frk;
+#ifdef HAVE_WIRINGPI_H
     char	buf[80];
+#endif
 
     while (1) {
 	int option_index = 0;
@@ -174,6 +181,7 @@
 	    signal(i, (void (*))die);
     }
 
+#ifdef HAVE_WIRINGPI_H
     if (wiringPiSetup () )
 	return 1;
 
@@ -195,6 +203,7 @@
 	syslog(LOG_NOTICE, "Using 433 MHz transmitter on pin %d", Config.tx433);
     	enableTransmit(Config.tx433);
     }
+#endif
 
     if (debug) {
 	/*
@@ -220,7 +229,9 @@
 	    case -1:	
 		    	syslog(LOG_NOTICE, "Daemon fork failed: %s", strerror(errno));
 			syslog(LOG_NOTICE, "Finished, rc=1");
+#ifdef HAVE_WIRINGPI_H
 			stopLCD();
+#endif
 			exit(1);
 	    case 0:	/*
 			 * Run the daemon
@@ -265,14 +276,18 @@
     char                buf[1024];
     time_t		now, last = (time_t)0;
     w1_therm		*tmp1;
+#ifdef HAVE_WIRINGPI_H
     rc_switch		*tmp2;
-    int			rc, run = 1, temp;
+    int			rc;
+#endif
+    int			run = 1, temp;
 
     if (lockprog((char *)"thermferm")) {
 	syslog(LOG_NOTICE, "Can't lock");
 	return 1;
     }
 
+#ifdef HAVE_WIRINGPI_H
     rc = piThreadCreate(my_sensors_loop);
     if (rc) {
 	fprintf(stderr, "my_sensors_loop thread didn't start rc=%d\n", rc);
@@ -284,6 +299,7 @@
 	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);
     }
+#endif
 
     snprintf(buf, 1023, "tempA,coolerA,tempB,coolerB");
     logger((char *)"thermferm.log", (char *)"thermferm", buf);
@@ -295,17 +311,23 @@
 	    run = 0;
 
 	tmp1 = Config.w1therms;
+#ifdef HAVE_WIRINGPI_H
 	tmp2 = Config.rcswitch;
+#endif
 	if (((tmp1->lastval / 100) < (tempA - 5)) && (coolerA == 1)) {
 	    coolerA = 0;
 	    syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler off", (tmp1->lastval / 1000.0));
+#ifdef HAVE_WIRINGPI_H
 	    sendRCswitch(tmp2->address, 0);
+#endif
 	    lcdupdate = TRUE;
 	}
 	if (((tmp1->lastval / 100) > (tempA + 5)) && (coolerA == 0)) {
 	    coolerA = 1;
 	    syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler on", (tmp1->lastval / 1000.0));
+#ifdef HAVE_WIRINGPI_H
 	    sendRCswitch(tmp2->address, 1);
+#endif
 	    lcdupdate = TRUE;
 	}
 	if (tmp1->update) {
@@ -313,17 +335,23 @@
 	    lcdupdate = TRUE;
 	}
 	tmp1 = tmp1->next;
+#ifdef HAVE_WIRINGPI_H
 	tmp2 = tmp2->next;
+#endif
 	if (((tmp1->lastval / 100) < (tempB - 5)) && (coolerB == 1)) {
 	    coolerB = 0;
 	    syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler off", (tmp1->lastval / 1000.0));
+#ifdef HAVE_WIRINGPI_H
 	    sendRCswitch(tmp2->address, 0);
+#endif
 	    lcdupdate = TRUE;
 	}
 	if (((tmp1->lastval / 100) > (tempB + 5)) && (coolerB == 0)) {
 	    coolerB = 1;
 	    syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler on", (tmp1->lastval / 1000.0));
+#ifdef HAVE_WIRINGPI_H
 	    sendRCswitch(tmp2->address, 1);
+#endif
 	    lcdupdate = TRUE;
 	}
 	if (tmp1->update) {
@@ -331,6 +359,7 @@
 	    lcdupdate = TRUE;
 	}
 
+#ifdef HAVE_WIRINGPI_H
 	if (run && lcdupdate) {
 	    lcdPosition(lcdHandle, 0, 0);
 	    tmp1 = Config.w1therms;
@@ -342,6 +371,7 @@
 	    snprintf(buf, 16, "%5.2f %cC %c %s            ", tmp1->lastval / 1000.0, 0xdf, coolerB ? '-' : ' ', tmp1->alias);
 	    mb_lcdPuts(lcdHandle, buf);
 	}
+#endif
 
 	now = time(NULL);
 	if (((int)now - (int)last) > 60) {
@@ -367,12 +397,14 @@
      */
     usleep(1500000);
 
+#ifdef HAVE_WIRINGPI_H
     if (Config.tx433 != -1) {
 	stopRCswitch();
     }
 
     stopLCD();
     disableTransmit();
+#endif
 
     wrconfig((char *)"thermferm.conf");
 
@@ -384,15 +416,3 @@
     return 0;
 }
 
-#else
-
-
-int main(int argc, char *argv[])
-{
-    fprintf(stderr, "Compiled on a system without a wiringPi library.\n");
-    fprintf(stderr, "This program is useless and will do nothing.\n");
-    return 0;
-}
-
-
-#endif
--- a/thermferm/thermferm.h	Thu Jun 19 15:49:29 2014 +0200
+++ b/thermferm/thermferm.h	Fri Jun 20 17:42:36 2014 +0200
@@ -64,20 +64,24 @@
     int			update;			/* Value updated		*/
 } w1_therm;
 
+#ifdef HAVE_WIRINGPI_H
 typedef struct _rc_switch {
     struct _rc_switch	*next;
     char		*address;		/* Address code			*/
     char		*alias;			/* Friendly name		*/
 } rc_switch;
+#endif
 
 typedef struct _sys_config {
     char		*name;			/* Configuration name		*/
     int			my_port;		/* my client/server port	*/
     w1_therm		*w1therms;		/* 1-wire temp sensors		*/
+#ifdef HAVE_WIRINGPI_H
     int			lcd_cols;		/* LCD display columns		*/
     int			lcd_rows;		/* LCD display rows		*/
     int			tx433;			/* 433 MHz transmitter pin	*/
     rc_switch		*rcswitch;		/* 433 MHz RC Power switches	*/
+#endif
     						/* ControlSettings:		*/
     unsigned char	cs_mode;		/* mode				*/
     float		cs_beerSet;		/* beer temperature		*/
@@ -130,12 +134,14 @@
 void mb_lcdPuts(const int, const char *);
 void mb_lcdClear(const int);
 
+#endif
 
 /* logger.c */
 void logger(char *, char *, char *);
 
 
 /* rc-switch.c */
+#ifdef HAVE_WIRINGPI_H
 int  toggleSwitch(char *);
 int  toggleTypeA(char *, char *, bool);
 int  toggleTypeB(int, int, bool);
@@ -158,20 +164,25 @@
 void disableTransmit(void);
 
 char *dec2binWzerofill(unsigned long, unsigned int);
-
+#endif
 
 /* dht11.c */
 void dht11Read(void);
 void dht11Init(int, int, int);
 
-
+#ifdef HAVE_WIRINGPI_H
 PI_THREAD (my_sensors_loop);
+#else
+int my_sensors_loop(void);
+#endif
 
 /* server.c */
 void defaultControlSettings(void);
 void defaultControlConstants(void);
+#ifdef HAVE_WIRINGPI_H
 PI_THREAD (my_server_loop);
-
+#else
+int my_server_loop(void);
 #endif
 
 

mercurial