forked from tedhale/hottub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.c
executable file
·131 lines (117 loc) · 2.8 KB
/
common.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*---------------------------------------------------------------------------
common.c misc shared functions
Ted Hale
---------------------------------------------------------------------------*/
#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/timeb.h>
#include <pthread.h>
#include <wiringPi.h>
#include "hottub.h"
//************************************************************************
/*
void Log(char *format, ... )
{
va_list arglist;
va_start ( arglist, format );
vprintf (format, arglist );
}
*/
//***************************************************************************
int Sleep(int millisecs)
{
return usleep(1000*millisecs);
}
//************************************************************************
long long int getTicks()
{
struct timeval now;
gettimeofday(&now, NULL);
return (1000000*now.tv_sec)+now.tv_usec;
}
//************************************************************************
// read chars into buffer until EOF or newline
int read_line(FILE *fp, char *bp, int mx)
{ char c = '\0';
int i = 0;
memset(bp,0,mx);
/* Read one line from the source file */
while ( ( (c = getc(fp)) != '\n' ) && (i<mx) )
{
if ((c == EOF)||(c == 255)) /* return -1 on EOF */
{
LogDbg("read_line> got EOF");
sleep(1);
if (i>0) break;
else return(-1);
}
bp[i++] = c;
}
bp[i] = '\0';
return(i);
}
//************************************************************************
// get var=value from config file
int ReadConfigString(char *var, char *defaultVal, char *out, int sz, char *file)
{
FILE *f;
char line[100];
char *p;
LogDbg("ReadConfigString> get %s from %s ",var,file);
piLock(1);
f = fopen(file,"r");
if (!f)
{
Log("ReadConfigString> error %d opening %s",errno,file);
piUnlock(1);
return 1;
}
while (read_line(f,line,sizeof(line))>=0)
{
LogDbg("ReadConfigString> read: %s",line);
if ((line[0]==';')||(line[0]=='#'))
continue;
p = strchr(line,'=');
if (!p)
continue;
*p=0;
p++;
if (!strcmp(line,var))
{
strncpy(out,p,sz);
fclose(f);
Log("ReadConfigString> return %s=%s",var,out);
piUnlock(1);
return 1;
}
}
fclose(f);
strncpy(out,defaultVal,sz);
Log("ReadConfigString> return %s=%s",var,out);
piUnlock(1);
return 0;
}
//************************************************************************
// save the desired temp to file
void saveDesiredTemp()
{
FILE *f;
char temp[20];
piLock(1);
f = fopen(DESIREDTEMPFILE,"w");
if (f==NULL)
{
piUnlock(1);
Log("saveDesiredTemp> error %d opening %s",errno,DESIREDTEMPFILE);
return;
}
Log("saveDesiredTemp> set to %6.1f degrees",desiredTemp);
sprintf(temp,"%6.1f",desiredTemp);
fwrite(temp,strlen(temp),1,f);
fclose(f);
piUnlock(1);
}