comparison tempctrl.c @ 50:a13e0ccc1d2d

Rejig how temperatures are logged. Print out the final line at the end so it is printed in one hit.
author darius@Inchoate
date Wed, 29 Oct 2008 16:06:42 +1030
parents 91e06007fe23
children cb184206344d
comparison
equal deleted inserted replaced
49:91e06007fe23 50:a13e0ccc1d2d
113 113
114 /* Local variable declarations */ 114 /* Local variable declarations */
115 volatile static time_t now; 115 volatile static time_t now;
116 116
117 /* Local function prototypes */ 117 /* Local function prototypes */
118 static int gettemp(const char *name, uint8_t *ROM, int16_t *temp, uint8_t last);
119 static void tempctrl_load_or_init_settings(void); 118 static void tempctrl_load_or_init_settings(void);
120 static void tempctrl_default_settings(void); 119 static void tempctrl_default_settings(void);
121 static void tempctrl_write_settings(void); 120 static void tempctrl_write_settings(void);
122 static void setstate(char state); 121 static void setstate(char state);
123 static const PROGMEM char*state2long(char s); 122 static const PROGMEM char*state2long(char s);
123 static void printtemp(char *name, int tmp, char *trailer);
124 124
125 /* 125 /*
126 * tempctrl_init 126 * tempctrl_init
127 * 127 *
128 * Setup timer, should be called with interrupts disabled. 128 * Setup timer, should be called with interrupts disabled.
207 nextstate = 'i'; 207 nextstate = 'i';
208 goto setstate; 208 goto setstate;
209 } 209 }
210 210
211 /* Update our temperatures */ 211 /* Update our temperatures */
212 printf_P(PSTR("Time: %ld, Target: %d.%02d, "), now.sec, GETWHOLE(settings.target_temp), 212 fermenter_temp = OWGetTemp(settings.fermenter_ROM);
213 GETFRAC(settings.target_temp)); 213 fridge_temp = OWGetTemp(settings.fridge_ROM);
214 214 ambient_temp = OWGetTemp(settings.ambient_ROM);
215 if (gettemp(PSTR("Fermenter"), settings.fermenter_ROM, &fermenter_temp, 0)) 215
216 if (fermenter_temp > OW_TEMP_BADVAL)
216 lastdata = t; 217 lastdata = t;
217 218
218 /* Check for stale data */ 219 /* Check for stale data */
219 if (lastdata + (settings.check_interval * settings.stale_factor) < t) 220 if (lastdata + (settings.check_interval * settings.stale_factor) < t)
220 stale = 1; 221 stale = 1;
221 else 222 else
222 stale = 0; 223 stale = 0;
223
224 gettemp(PSTR("Fridge"), settings.fridge_ROM, &fridge_temp, 0);
225 gettemp(PSTR("Ambient"), settings.ambient_ROM, &ambient_temp, 1);
226 224
227 /* Default to remaining as we are */ 225 /* Default to remaining as we are */
228 nextstate = '-'; 226 nextstate = '-';
229 227
230 /* Temperature diff, -ve => too cold, +ve => too warm */ 228 /* Temperature diff, -ve => too cold, +ve => too warm */
301 } 299 }
302 300
303 if (nextstate != '-') 301 if (nextstate != '-')
304 currstate = nextstate; 302 currstate = nextstate;
305 303
306 printf_P(PSTR(", State: %S, Flags: %S%S\r\n"), state2long(currstate), 304 printf_P(PSTR("Time: %10ld, "), t);
305 printtemp(PSTR("Target"), settings.target_temp, PSTR(", "));
306 printtemp(PSTR("Fermenter"), fermenter_temp, PSTR(", "));
307 printtemp(PSTR("Fridge"), fridge_temp, PSTR(", "));
308 printtemp(PSTR("Ambient"), ambient_temp, PSTR(", "));
309 printf_P(PSTR("State: %S, Flags: %S%S\r\n"), state2long(currstate),
307 forced ? PSTR("F") : PSTR(""), 310 forced ? PSTR("F") : PSTR(""),
308 stale ? PSTR("S") : PSTR("")); 311 stale ? PSTR("S") : PSTR(""));
309 312
310 setstate: 313 setstate:
311 setstate(currstate); 314 setstate(currstate);
312 }
313
314 /*
315 * Log a temperature & store it if valid
316 *
317 * Returns 1 if it was valid, 0 otherwise
318 */
319 static int
320 gettemp(const char *name, uint8_t *ROM, int16_t *temp, uint8_t last) {
321 int16_t tmp;
322
323 tmp = OWGetTemp(ROM);
324 printf_P(PSTR("%S: "), name);
325 if (tmp > OW_TEMP_BADVAL) {
326 printf_P(PSTR("%d.%02d%S"), GETWHOLE(tmp), GETFRAC(tmp), last ? PSTR("") : PSTR(", "));
327 *temp = tmp;
328 return(1);
329 } else {
330 printf_P(PSTR("NA (%d)%S"), tmp, last ? PSTR("") : PSTR(", "));
331 return(0);
332 }
333 } 315 }
334 316
335 /* Return 'time of day' (really uptime) */ 317 /* Return 'time of day' (really uptime) */
336 int32_t 318 int32_t
337 gettod(void) { 319 gettod(void) {
340 cli(); 322 cli();
341 t = now.sec; 323 t = now.sec;
342 sei(); 324 sei();
343 325
344 return(t); 326 return(t);
327 }
328
329 /*
330 * Print out temperature (or NA + error code) with specified trailer
331 */
332 static void
333 printtemp(char *name, int tmp, char *trailer) {
334 if (tmp > OW_TEMP_BADVAL)
335 printf_P(PSTR("%S: %d.%02d%S"), name, GETWHOLE(tmp), GETFRAC(tmp), trailer);
336 else
337 printf_P(PSTR("%S: NA (%d)%S"), name, tmp, trailer);
345 } 338 }
346 339
347 /* Read the settings from EEPROM 340 /* Read the settings from EEPROM
348 * If the CRC fails then reload from flash 341 * If the CRC fails then reload from flash
349 */ 342 */