brewco/util.c

changeset 487
d5bc44183aa4
parent 486
5a237a99a793
child 488
bee1f70fb42b
equal deleted inserted replaced
486:5a237a99a793 487:d5bc44183aa4
1 /*****************************************************************************
2 * Copyright (C) 2015
3 *
4 * Michiel Broek <mbroek at mbse dot eu>
5 *
6 * This file is part of the mbsePi-apps
7 *
8 * This is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * mbsePi-apps is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with ThermFerm; see the file COPYING. If not, write to the Free
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 *****************************************************************************/
22
23 #include "brewco.h"
24 #include "util.h"
25 #include "slcd.h"
26
27 #ifdef HAVE_WIRINGPI_H
28 extern int lcdHandle;
29 #endif
30 extern int slcdHandle;
31
32
33 /*
34 * Make directory tree, the name must end with a /
35 */
36 int mkdirs(char *name, mode_t mode)
37 {
38 char buf[PATH_MAX], *p, *q;
39 int last = 0, oldmask;
40
41 memset(&buf, 0, sizeof(buf));
42 strncpy(buf, name, sizeof(buf)-1);
43 buf[sizeof(buf)-1] = '\0';
44
45 p = buf+1;
46
47 oldmask = umask(000);
48 while ((q = strchr(p, '/'))) {
49 *q = '\0';
50 mkdir(buf, mode);
51 last = errno;
52 *q = '/';
53 p = q+1;
54 }
55
56 umask(oldmask);
57
58 if ((last == 0) || (last == EEXIST)) {
59 return TRUE;
60 } else {
61 syslog(LOG_NOTICE, "mkdirs(%s)", name);
62 return FALSE;
63 }
64 }
65
66
67
68 /*
69 * Test if the given file exists. The second option is:
70 * R_OK - test for Read rights
71 * W_OK - test for Write rights
72 * X_OK - test for eXecute rights
73 * F_OK - test file presence only
74 */
75 int file_exist(char *path, int mode)
76 {
77 if (access(path, mode) != 0)
78 return errno;
79
80 return 0;
81 }
82
83
84
85 /*
86 * Buffered filecopy, filetime is preserved.
87 */
88 int file_cp(char *from, char *to)
89 {
90 char *line;
91 FILE *stfrom, *stto;
92 int dummy, bread;
93 static int error;
94 struct stat sb;
95 struct utimbuf ut;
96
97 stfrom = fopen(from, "r");
98 if (stfrom == NULL)
99 return errno;
100
101 stto = fopen(to, "w");
102 if (stto == NULL) {
103 error = errno;
104 fclose(stfrom);
105 return error;
106 }
107
108 line = malloc(16384);
109 do {
110 bread = fread(line, 1, 16384, stfrom);
111 dummy = fwrite(line, 1, bread, stto);
112 if (bread != dummy) {
113 error = errno;
114 fclose(stfrom);
115 fclose(stto);
116 unlink(to);
117 free(line);
118 return error;
119 }
120 } while (bread != 0);
121 free(line);
122 fclose(stfrom);
123 if (fclose(stto) != 0) {
124 error = errno;
125 unlink(to);
126 return error;
127 }
128
129 /*
130 * copy successfull, now copy file- and modification-time
131 */
132 if (stat(from, &sb) == 0) {
133 ut.actime = mktime(localtime(&sb.st_atime));
134 ut.modtime = mktime(localtime(&sb.st_mtime));
135 if (utime(to, &ut) != 0) {
136 error = errno;
137 unlink(to);
138 return error;
139 }
140 chmod(to, sb.st_mode);
141 }
142
143 return 0;
144 }
145
146
147 #ifndef HAVE_WIRINGPI_H
148 long millis(void)
149 {
150 struct timespec now;
151
152 clock_gettime(CLOCK_REALTIME , &now);
153 return ((now.tv_sec * 1000000000) + now.tv_nsec) / 1000000;
154 }
155 #endif
156
157
158 void hlt_status(int value)
159 {
160 #ifdef HAVE_WIRINGPI_H
161 piLock(LOCK_LCD);
162 lcdPosition(lcdHandle, 0, 1);
163 #endif
164 slcdPosition(slcdHandle, 0, 1);
165 if (value) {
166 #ifdef HAVE_WIRINGPI_H
167 lcdPutchar(lcdHandle, 5);
168 #endif
169 slcdPutchar(slcdHandle, 5);
170 } else {
171 #ifdef HAVE_WIRINGPI_H
172 lcdPutchar(lcdHandle, 6);
173 #endif
174 slcdPutchar(slcdHandle, 6);
175 }
176 #ifdef HAVE_WIRINGPI_H
177 piUnlock(LOCK_LCD);
178 #endif
179 }
180
181
182
183 void mlt_status(int value)
184 {
185 #ifdef HAVE_WIRINGPI_H
186 piLock(LOCK_LCD);
187 lcdPosition(lcdHandle, 18, 1);
188 #endif
189 slcdPosition(slcdHandle, 18, 1);
190 if (value) {
191 #ifdef HAVE_WIRINGPI_H
192 lcdPutchar(lcdHandle, 5);
193 #endif
194 slcdPutchar(slcdHandle, 5);
195 } else {
196 #ifdef HAVE_WIRINGPI_H
197 lcdPutchar(lcdHandle, 6);
198 #endif
199 slcdPutchar(slcdHandle, 6);
200 }
201 #ifdef HAVE_WIRINGPI_H
202 piUnlock(LOCK_LCD);
203 #endif
204 }
205
206
207
208 void pump_status(int value)
209 {
210 #ifdef HAVE_WIRINGPI_H
211 piLock(LOCK_LCD);
212 lcdPosition(lcdHandle, 19, 1);
213 #endif
214 slcdPosition(slcdHandle, 19, 1);
215 if (value) {
216 #ifdef HAVE_WIRINGPI_H
217 lcdPutchar(lcdHandle, 3);
218 #endif
219 slcdPutchar(slcdHandle, 3);
220 } else {
221 #ifdef HAVE_WIRINGPI_H
222 lcdPutchar(lcdHandle, 4);
223 #endif
224 slcdPutchar(slcdHandle, 4);
225 }
226 #ifdef HAVE_WIRINGPI_H
227 piUnlock(LOCK_LCD);
228 #endif
229 }
230
231
232
233 /* From ArdBir */
234 float Arrotonda025(float Num){
235 // Appoggio la parte intera
236 int Appoggio= (int)Num;
237
238 // Arrotondo il valore con peso 0.25
239 return Appoggio+(int)((Num-Appoggio)*1000/225)*0.25;
240 }
241
242 float ConvertiCtoF(float Num){
243 Num = Num/16; // Recupero il valore
244 Num = (Num*1.8)+32; // Converto in °F
245 Num = Arrotonda025(Num);
246 return Num*16; // Preparo il valore per la registrazione
247 }
248 float ConvertiFtoC(float Num){
249 Num = Num/16; // Recupero il valore
250 Num = (Num-32)/1.8; // Converto in °C
251 Num = Arrotonda025(Num);
252 return Num*16; // Preparo il valore per la registrazione
253 }
254

mercurial