Mercurial > ~darius > hgwebdir.cgi > avr
annotate tempctrl.c @ 53:98b5a43fc748
Disable WDT in reset just in case we take too long (eg after reset when the
timer is set to a short time)
author | darius@Inchoate |
---|---|
date | Wed, 29 Oct 2008 16:11:15 +1030 |
parents | cb184206344d |
children | 58f1ec46bff6 |
rev | line source |
---|---|
41 | 1 /* |
2 * Temperature control logic | |
3 * | |
4 * Copyright (c) 2008 | |
5 * Daniel O'Connor <darius@dons.net.au>. All rights reserved. | |
6 * | |
7 * Redistribution and use in source and binary forms, with or without | |
8 * modification, are permitted provided that the following conditions | |
9 * are met: | |
10 * 1. Redistributions of source code must retain the above copyright | |
11 * notice, this list of conditions and the following disclaimer. | |
12 * 2. Redistributions in binary form must reproduce the above copyright | |
13 * notice, this list of conditions and the following disclaimer in the | |
14 * documentation and/or other materials provided with the distribution. | |
15 * | |
16 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE | |
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
26 * SUCH DAMAGE. | |
27 */ | |
28 | |
29 #include <stdio.h> | |
30 #include <stdint.h> | |
31 #include <stdlib.h> | |
51
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
32 #include <string.h> |
41 | 33 #include <avr/interrupt.h> |
34 #include <avr/pgmspace.h> | |
35 #include <avr/eeprom.h> | |
46 | 36 #include <avr/wdt.h> |
41 | 37 #include <util/crc16.h> |
38 | |
39 #include "cons.h" | |
40 #include "1wire.h" | |
41 #include "tempctrl.h" | |
42 | |
43 typedef struct { | |
44 int32_t sec; | |
45 int32_t usec; | |
46 } time_t; | |
47 | |
48 /* Holds all the settings needed */ | |
49 typedef struct { | |
50 uint8_t fermenter_ROM[8]; | |
51 uint8_t fridge_ROM[8]; | |
52 uint8_t ambient_ROM[8]; | |
53 int16_t target_temp; | |
54 uint16_t hysteresis; | |
55 | |
56 /* How much to under/overshoot on heating/cooling */ | |
57 int16_t minheatovershoot; | |
58 int16_t mincoolovershoot; | |
59 | |
60 /* Minimum time the cooler can be on/off */ | |
61 int16_t mincoolontime; | |
62 int16_t mincoolofftime; | |
63 | |
64 /* Minimum time the heater can be on/off */ | |
65 int16_t minheatontime; | |
66 int16_t minheatofftime; | |
67 | |
68 #define TC_MODE_AUTO 'a' /* Automatic control */ | |
69 #define TC_MODE_HEAT 'h' /* Force heating */ | |
70 #define TC_MODE_COOL 'c' /* Force cooling */ | |
71 #define TC_MODE_IDLE 'i' /* Force idle */ | |
72 #define TC_MODE_NOTHING 'n' /* Do nothing (like idle but log nothing) */ | |
73 char mode; | |
74 | |
75 /* Bit patterns for various modes */ | |
76 uint8_t coolbits; | |
77 uint8_t heatbits; | |
78 uint8_t idlebits; | |
79 | |
80 /* Check/stale times */ | |
81 int16_t check_interval; | |
82 int16_t stale_factor; | |
83 } __attribute__((packed)) settings_t; | |
84 | |
85 /* Current settings in RAM */ | |
86 static settings_t settings; | |
87 | |
88 /* Our map of EEPROM */ | |
89 struct { | |
90 settings_t settings; | |
91 uint16_t crc; | |
92 } ee_area __attribute__((section(".eeprom"))); | |
93 | |
94 /* Defaults that are shoved into EEPROM if it isn't inited */ | |
95 const PROGMEM settings_t default_settings = { | |
96 .fermenter_ROM = { 0x10, 0xeb, 0x48, 0x21, 0x01, 0x08, 0x00, 0xdf }, | |
97 .fridge_ROM = { 0x10, 0xa6, 0x2a, 0xc4, 0x00, 0x08, 0x00, 0x11 }, | |
98 .ambient_ROM = { 0x10, 0x97, 0x1b, 0xfe, 0x00, 0x08, 0x00, 0xd1 }, | |
99 .target_temp = 1400, | |
100 .hysteresis = 100, | |
101 .minheatovershoot = 50, | |
102 .mincoolovershoot = -50, | |
103 .mincoolontime = 300, | |
104 .mincoolofftime = 600, | |
105 .minheatontime = 60, | |
106 .minheatofftime = 60, | |
49
91e06007fe23
Default to idle mode (saves on compressor stress when testing)
darius@Inchoate
parents:
46
diff
changeset
|
107 .mode = TC_MODE_IDLE, |
46 | 108 .coolbits = _BV(6), |
109 .heatbits = _BV(7), | |
41 | 110 .idlebits = 0x00, |
111 .check_interval = 10, | |
112 .stale_factor = 3 | |
113 }; | |
114 | |
115 /* Local variable declarations */ | |
116 volatile static time_t now; | |
117 | |
118 /* Local function prototypes */ | |
119 static void tempctrl_load_or_init_settings(void); | |
120 static void tempctrl_default_settings(void); | |
121 static void tempctrl_write_settings(void); | |
122 static void setstate(char state); | |
123 static const PROGMEM char*state2long(char s); | |
50
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
124 static void printtemp(char *name, int tmp, char *trailer); |
41 | 125 |
126 /* | |
127 * tempctrl_init | |
128 * | |
129 * Setup timer, should be called with interrupts disabled. | |
130 * | |
131 */ | |
132 void | |
133 tempctrl_init(void) { | |
134 /* Setup timer */ | |
135 /* 16Mhz / 1024 = 15625 Hz / 125 = 125 Hz = IRQ every 8 ms */ | |
136 | |
137 /* CTC mode, no output on pin, Divide clock by 1024 */ | |
138 TCCR0 = _BV(WGM01)| _BV(CS02) | _BV(CS00); | |
139 | |
140 /* Compare with ... */ | |
141 OCR0 = 125; | |
142 | |
143 /* Enable interrupt for match on A */ | |
144 TIMSK = _BV(OCIE0); | |
145 | |
146 now.sec = 0; | |
147 now.usec = 0; | |
148 | |
149 tempctrl_load_or_init_settings(); | |
150 } | |
151 | |
152 /* | |
153 * Timer 0 Compare IRQ | |
154 * | |
155 * Update time counter | |
156 */ | |
157 | |
158 ISR(TIMER0_COMP_vect) { | |
46 | 159 wdt_reset(); |
160 | |
41 | 161 now.usec += 8000; /* 1000000 * 1 / F_CPU / 1024 / 125 */ |
162 while (now.usec > 1000000) { | |
163 now.usec -= 1000000; | |
164 now.sec++; | |
165 } | |
166 } | |
167 | |
168 /* | |
169 * tempctrl_update | |
170 * | |
171 * Should be called in a normal context, could run things that take a long time. | |
172 * (ie 1wire bus stuff) | |
173 * | |
174 */ | |
175 void | |
176 tempctrl_update(void) { | |
177 /* State variables */ | |
178 static int32_t checktime = 0; // Time of next check | |
46 | 179 static int32_t lastdata = INT32_MIN; // Last time we got data |
41 | 180 |
181 static int16_t fermenter_temp = 0; // Fermenter temperature | |
182 static int16_t fridge_temp = 0; // Fridge temperature | |
183 static int16_t ambient_temp = 0; // Ambient temperature | |
46 | 184 static int32_t lastheaton = INT32_MIN; // Last time the heater was on |
185 static int32_t lastheatoff = INT32_MIN;// Last time the heater was off | |
186 static int32_t lastcoolon = INT32_MIN; // Last time the cooler was on | |
187 static int32_t lastcooloff = INT32_MIN;// Last time the cooler was off | |
41 | 188 static char currstate = 'i'; // Current state |
46 | 189 /* We init to times to INT32_MIN so that things function properly when |
190 * now < settings.minheat/cool/on/offtime */ | |
41 | 191 |
192 /* Temporary variables */ | |
193 int32_t t; | |
194 int16_t diff; | |
195 char nextstate; | |
196 int forced; | |
197 int stale; | |
198 | |
199 t = gettod(); | |
200 /* Time to check temperatures? */ | |
201 if (t < checktime) | |
202 return; | |
203 | |
204 checktime = t + settings.check_interval; | |
205 | |
206 /* Don't do any logging, just force idle and leave */ | |
207 if (settings.mode == TC_MODE_NOTHING) { | |
208 nextstate = 'i'; | |
209 goto setstate; | |
210 } | |
211 | |
212 /* Update our temperatures */ | |
50
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
213 fermenter_temp = OWGetTemp(settings.fermenter_ROM); |
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
214 fridge_temp = OWGetTemp(settings.fridge_ROM); |
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
215 ambient_temp = OWGetTemp(settings.ambient_ROM); |
41 | 216 |
50
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
217 if (fermenter_temp > OW_TEMP_BADVAL) |
41 | 218 lastdata = t; |
50
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
219 |
41 | 220 /* Check for stale data */ |
221 if (lastdata + (settings.check_interval * settings.stale_factor) < t) | |
222 stale = 1; | |
223 else | |
224 stale = 0; | |
225 | |
226 /* Default to remaining as we are */ | |
227 nextstate = '-'; | |
228 | |
229 /* Temperature diff, -ve => too cold, +ve => too warm */ | |
230 diff = fermenter_temp - settings.target_temp; | |
231 | |
232 switch (currstate) { | |
233 case 'i': | |
234 /* If we're idle then only heat or cool if the temperate difference is out of the | |
235 * hysteresis band | |
236 */ | |
237 if (abs(diff) > settings.hysteresis) { | |
238 if (diff < 0 && settings.minheatofftime + lastheatoff < t) | |
239 nextstate = 'h'; | |
240 else if (diff > 0 && settings.mincoolofftime + lastcooloff < t) | |
241 nextstate = 'c'; | |
242 } | |
243 break; | |
244 | |
245 case 'c': | |
246 /* Work out if we should go idle (based on min on time & overshoot) */ | |
247 if (diff + settings.mincoolovershoot < 0 && | |
248 settings.mincoolontime + lastcoolon < t) | |
249 nextstate = 'i'; | |
250 break; | |
251 | |
252 case 'h': | |
253 if (diff - settings.minheatovershoot > 0 && | |
254 settings.minheatontime + lastheaton < t) | |
255 nextstate = 'i'; | |
256 break; | |
257 | |
258 default: | |
259 printf_P(PSTR("\r\nUnknown state %c, going to idle\n"), currstate); | |
260 nextstate = 'i'; | |
261 break; | |
262 } | |
263 | |
264 /* Override if we have stale data */ | |
265 if (stale) | |
266 nextstate = 'i'; | |
267 | |
268 /* Handle state forcing */ | |
269 if (settings.mode != TC_MODE_AUTO) | |
270 forced = 1; | |
271 else | |
272 forced = 0; | |
273 | |
274 if (settings.mode == TC_MODE_IDLE) | |
275 nextstate = 'i'; | |
276 else if (settings.mode == TC_MODE_HEAT) | |
277 nextstate = 'h'; | |
278 else if (settings.mode == TC_MODE_COOL) | |
279 nextstate = 'c'; | |
280 | |
42
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
281 // Keep track of when we last turned things on or off |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
282 switch (nextstate) { |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
283 case 'c': |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
284 if (currstate == 'h') |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
285 lastheatoff = t; |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
286 lastcoolon = t; |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
287 break; |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
288 |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
289 case 'h': |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
290 if (currstate == 'c') |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
291 lastcooloff = t; |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
292 lastheaton = t; |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
293 break; |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
294 |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
295 default: |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
296 if (currstate == 'c') |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
297 lastcooloff = t; |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
298 if (currstate == 'h') |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
299 lastheatoff = t; |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
300 } |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
301 |
41 | 302 if (nextstate != '-') |
303 currstate = nextstate; | |
304 | |
50
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
305 printf_P(PSTR("Time: %10ld, "), t); |
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
306 printtemp(PSTR("Target"), settings.target_temp, PSTR(", ")); |
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
307 printtemp(PSTR("Fermenter"), fermenter_temp, PSTR(", ")); |
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
308 printtemp(PSTR("Fridge"), fridge_temp, PSTR(", ")); |
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
309 printtemp(PSTR("Ambient"), ambient_temp, PSTR(", ")); |
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
310 printf_P(PSTR("State: %S, Flags: %S%S\r\n"), state2long(currstate), |
41 | 311 forced ? PSTR("F") : PSTR(""), |
312 stale ? PSTR("S") : PSTR("")); | |
313 | |
314 setstate: | |
315 setstate(currstate); | |
316 } | |
317 | |
318 /* Return 'time of day' (really uptime) */ | |
319 int32_t | |
320 gettod(void) { | |
321 int32_t t; | |
322 | |
323 cli(); | |
324 t = now.sec; | |
325 sei(); | |
326 | |
327 return(t); | |
328 } | |
329 | |
50
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
330 /* |
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
331 * Print out temperature (or NA + error code) with specified trailer |
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
332 */ |
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
333 static void |
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
334 printtemp(char *name, int tmp, char *trailer) { |
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
335 if (tmp > OW_TEMP_BADVAL) |
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
336 printf_P(PSTR("%S: %d.%02d%S"), name, GETWHOLE(tmp), GETFRAC(tmp), trailer); |
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
337 else |
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
338 printf_P(PSTR("%S: NA (%d)%S"), name, tmp, trailer); |
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
339 } |
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
340 |
41 | 341 /* Read the settings from EEPROM |
342 * If the CRC fails then reload from flash | |
343 */ | |
344 static void | |
345 tempctrl_load_or_init_settings(void) { | |
346 uint8_t *dptr; | |
347 uint16_t crc, strcrc; | |
348 int i; | |
349 | |
350 crc = 0; | |
351 eeprom_busy_wait(); | |
352 eeprom_read_block(&settings, &ee_area.settings, sizeof(settings_t)); | |
353 strcrc = eeprom_read_word(&ee_area.crc); | |
354 | |
355 dptr = (uint8_t *)&settings; | |
356 | |
357 for (i = 0; i < sizeof(settings_t); i++) | |
358 crc = _crc16_update(crc, dptr[i]); | |
359 | |
360 /* All OK? */ | |
361 if (crc == strcrc) | |
362 return; | |
363 | |
364 printf_P(PSTR("CRC mismatch got 0x%04x vs 0x%04x, setting defaults\r\n"), crc, strcrc); | |
365 tempctrl_default_settings(); | |
366 tempctrl_write_settings(); | |
367 } | |
368 | |
369 /* Load in the defaults from flash */ | |
370 static void | |
371 tempctrl_default_settings(void) { | |
372 memcpy_P(&settings, &default_settings, sizeof(settings_t)); | |
373 } | |
374 | |
375 /* Write the current settings out to EEPROM */ | |
376 static void | |
377 tempctrl_write_settings(void) { | |
378 uint16_t crc; | |
379 uint8_t *dptr; | |
380 int i; | |
381 | |
382 eeprom_busy_wait(); | |
383 eeprom_write_block(&settings, &ee_area.settings, sizeof(settings_t)); | |
384 | |
385 dptr = (uint8_t *)&settings; | |
386 crc = 0; | |
387 for (i = 0; i < sizeof(settings_t); i++) | |
388 crc = _crc16_update(crc, dptr[i]); | |
389 | |
390 eeprom_write_word(&ee_area.crc, crc); | |
391 } | |
392 | |
393 /* Set the relays to match the desired state */ | |
394 static void | |
395 setstate(char state) { | |
396 switch (state) { | |
397 case 'c': | |
398 PORTC = settings.coolbits; | |
399 break; | |
400 | |
401 case 'h': | |
402 PORTC = settings.heatbits; | |
403 break; | |
404 | |
405 default: | |
406 printf_P(PSTR("Unknown state %c, setting idle\r\n"), state); | |
407 /* fallthrough */ | |
408 | |
409 case 'i': | |
410 PORTC = settings.idlebits; | |
411 break; | |
412 } | |
413 } | |
414 | |
415 /* Handle user command | |
416 * | |
417 */ | |
418 void | |
419 tempctrl_cmd(char *buf) { | |
420 char cmd[6]; | |
421 int16_t data; | |
51
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
422 uint8_t ROM[8]; |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
423 |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
424 if (sscanf_P(buf, PSTR("tc %5s"), cmd, &data) == 0) { |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
425 printf_P(PSTR("Unable to parse tc subcommand\r\n")); |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
426 return; |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
427 } |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
428 |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
429 if (!strcasecmp_P(cmd, PSTR("help"))) { |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
430 printf_P(PSTR( |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
431 "tc help This help\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
432 "tc save Save settings to EEPROM\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
433 "tc load Load or default settings from EEPROM\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
434 "tc dflt Load defaults from flash\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
435 "tc list List current settings\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
436 "tc mode [achin] Change control mode, must be one of\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
437 " a Auto\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
438 " c Always cool\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
439 " h Always heat\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
440 " i Always idle\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
441 " n Like idle but don't log anything\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
442 "tc X Y Set X to Y where X is one of\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
443 " targ Target temperature\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
444 " hys Hysteresis range\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
445 " mhov Minimum heat overshoot\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
446 " mcov Minimum cool overshoot\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
447 " mcon Minimum cool on time\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
448 " mcoff Minimum cool off time\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
449 " mhin Minimum heat on time\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
450 " mhoff Minimum heat off time\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
451 "tc A B Set temperature sensor ID\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
452 " Where A is ferm, frg or amb\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
453 " and B is of the form xx:xx:xx:xx:xx:xx:xx:xx\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
454 "\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
455 " Times are in seconds\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
456 " Temperatures are in hundredths of degrees Celcius\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
457 )); |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
458 return; |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
459 } |
41 | 460 |
51
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
461 if (!strcasecmp_P(cmd, PSTR("save"))) { |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
462 tempctrl_write_settings(); |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
463 return; |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
464 } |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
465 if (!strcasecmp_P(cmd, PSTR("load"))) { |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
466 tempctrl_load_or_init_settings(); |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
467 return; |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
468 } |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
469 if (!strcasecmp_P(cmd, PSTR("dflt"))) { |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
470 tempctrl_default_settings(); |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
471 return; |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
472 } |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
473 if (!strcasecmp_P(cmd, PSTR("list"))) { |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
474 printf_P(PSTR("Fermenter ROM ID %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
475 "Fridge ROM ID %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
476 "Ambient ROM ID %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
477 "Mode - %c, Target - %d, Hystersis - %d\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
478 "Min heat overshoot - %d, Min cool overshoot - %d\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
479 "Min cool on time - %d, Min cool off time - %d\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
480 "Min heat on time - %d, Min heat off time - %d\r\n"), |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
481 settings.fermenter_ROM[0], settings.fermenter_ROM[1], settings.fermenter_ROM[2], settings.fermenter_ROM[3], |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
482 settings.fermenter_ROM[4], settings.fermenter_ROM[5], settings.fermenter_ROM[6], settings.fermenter_ROM[7], |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
483 settings.fridge_ROM[0], settings.fridge_ROM[1], settings.fridge_ROM[2], settings.fridge_ROM[3], |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
484 settings.fridge_ROM[4], settings.fridge_ROM[5], settings.fridge_ROM[6], settings.fridge_ROM[7], |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
485 settings.ambient_ROM[0], settings.ambient_ROM[1], settings.ambient_ROM[2], settings.ambient_ROM[3], |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
486 settings.ambient_ROM[4], settings.ambient_ROM[5], settings.ambient_ROM[6], settings.ambient_ROM[7], |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
487 settings.mode, settings.target_temp, settings.hysteresis, |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
488 settings.minheatovershoot, settings.mincoolovershoot, |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
489 settings.mincoolontime, settings.minheatontime, |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
490 settings.minheatontime, settings.minheatofftime); |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
491 return; |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
492 } |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
493 if (!strcasecmp_P(cmd, PSTR("mode"))) { |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
494 switch (buf[8]) { |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
495 case TC_MODE_AUTO: |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
496 case TC_MODE_HEAT: |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
497 case TC_MODE_COOL: |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
498 case TC_MODE_IDLE: |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
499 case TC_MODE_NOTHING: |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
500 settings.mode = buf[8]; |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
501 break; |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
502 |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
503 default: |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
504 printf_P(PSTR("Unknown mode character '%c'\r\n"), buf[8]); |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
505 break; |
41 | 506 } |
51
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
507 return; |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
508 } |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
509 if (!strcasecmp_P(cmd, PSTR("ferm")) || |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
510 !strcasecmp_P(cmd, PSTR("frg")) || |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
511 !strcasecmp_P(cmd, PSTR("amb"))) { |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
512 |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
513 if (sscanf_P((char *)cmd, PSTR("tc %5s %hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx"), cmd, |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
514 &ROM[0], &ROM[1], &ROM[2], &ROM[3], |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
515 &ROM[4], &ROM[5], &ROM[6], &ROM[7]) != 9) { |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
516 printf_P(PSTR("Unable to parse ROM ID\r\n")); |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
517 } else { |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
518 if (!strcasecmp_P(cmd, PSTR("ferm"))) |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
519 memcpy(settings.fermenter_ROM, ROM, sizeof(ROM)); |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
520 if (!strcasecmp_P(cmd, PSTR("frg"))) |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
521 memcpy(settings.fridge_ROM, ROM, sizeof(ROM)); |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
522 if (!strcasecmp_P(cmd, PSTR("amb"))) |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
523 memcpy(settings.ambient_ROM, ROM, sizeof(ROM)); |
41 | 524 } |
525 } | |
526 | |
51
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
527 if (sscanf_P(buf, PSTR("tc %5s %d"), cmd, &data) != 2) { |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
528 printf_P(PSTR("Unable to parse tc subcommand & value\r\n")); |
41 | 529 return; |
530 } | |
531 | |
532 if (!strcasecmp_P(cmd, PSTR("targ"))) { | |
533 settings.target_temp = data; | |
534 } else if (!strcasecmp_P(cmd, PSTR("hys"))) { | |
535 settings.hysteresis = data; | |
536 } else if (!strcasecmp_P(cmd, PSTR("mhov"))) { | |
537 settings.minheatovershoot = data; | |
538 } else if (!strcasecmp_P(cmd, PSTR("mcov"))) { | |
539 settings.mincoolovershoot = data; | |
540 } else if (!strcasecmp_P(cmd, PSTR("mcon"))) { | |
541 settings.mincoolontime = data; | |
542 } else if (!strcasecmp_P(cmd, PSTR("mcoff"))) { | |
543 settings.mincoolofftime = data; | |
544 } else if (!strcasecmp_P(cmd, PSTR("mhon"))) { | |
545 settings.minheatontime = data; | |
546 } else if (!strcasecmp_P(cmd, PSTR("mhoff"))) { | |
547 settings.minheatofftime = data; | |
548 } else { | |
549 printf_P(PSTR("Unknown setting\r\n")); | |
550 return; | |
551 } | |
552 } | |
553 | |
554 static const PROGMEM char* | |
555 state2long(char s) { | |
556 switch (s) { | |
557 case 'i': | |
558 return PSTR("idle"); | |
559 break; | |
560 | |
561 case 'c': | |
562 return PSTR("cool"); | |
563 break; | |
564 | |
565 case 'h': | |
566 return PSTR("heat"); | |
567 break; | |
568 | |
569 case '-': | |
570 return PSTR("-"); | |
571 break; | |
572 | |
573 default: | |
574 return PSTR("unknown"); | |
575 break; | |
576 } | |
577 } | |
578 |