Mercurial > ~darius > hgwebdir.cgi > tempctrl
annotate tempctrl.c @ 54:58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
a pin)
Move the time of day timer to OCR2 so we can use PB3/OC0 for PWM.
author | darius@Inchoate |
---|---|
date | Wed, 29 Oct 2008 17:41:04 +1030 |
parents | cb184206344d |
children | 6b1057409d9a |
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 /* How much to under/overshoot on heating/cooling */ | |
56 int16_t minheatovershoot; | |
57 int16_t mincoolovershoot; | |
58 | |
59 /* Minimum time the cooler can be on/off */ | |
60 int16_t mincoolontime; | |
61 int16_t mincoolofftime; | |
62 | |
63 /* Minimum time the heater can be on/off */ | |
64 int16_t minheatontime; | |
65 int16_t minheatofftime; | |
66 | |
67 #define TC_MODE_AUTO 'a' /* Automatic control */ | |
68 #define TC_MODE_HEAT 'h' /* Force heating */ | |
69 #define TC_MODE_COOL 'c' /* Force cooling */ | |
70 #define TC_MODE_IDLE 'i' /* Force idle */ | |
71 #define TC_MODE_NOTHING 'n' /* Do nothing (like idle but log nothing) */ | |
72 char mode; | |
73 | |
74 /* Bit patterns for various modes */ | |
75 uint8_t coolbits; | |
76 uint8_t heatbits; | |
77 uint8_t idlebits; | |
78 | |
79 /* Check/stale times */ | |
80 int16_t check_interval; | |
81 int16_t stale_factor; | |
54
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
82 |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
83 /* Beep if stale */ |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
84 int8_t dobeep; |
41 | 85 } __attribute__((packed)) settings_t; |
86 | |
87 /* Current settings in RAM */ | |
88 static settings_t settings; | |
89 | |
90 /* Our map of EEPROM */ | |
91 struct { | |
92 settings_t settings; | |
93 uint16_t crc; | |
94 } ee_area __attribute__((section(".eeprom"))); | |
95 | |
96 /* Defaults that are shoved into EEPROM if it isn't inited */ | |
97 const PROGMEM settings_t default_settings = { | |
98 .fermenter_ROM = { 0x10, 0xeb, 0x48, 0x21, 0x01, 0x08, 0x00, 0xdf }, | |
99 .fridge_ROM = { 0x10, 0xa6, 0x2a, 0xc4, 0x00, 0x08, 0x00, 0x11 }, | |
100 .ambient_ROM = { 0x10, 0x97, 0x1b, 0xfe, 0x00, 0x08, 0x00, 0xd1 }, | |
101 .target_temp = 1400, | |
102 .hysteresis = 100, | |
103 .minheatovershoot = 50, | |
104 .mincoolovershoot = -50, | |
105 .mincoolontime = 300, | |
106 .mincoolofftime = 600, | |
107 .minheatontime = 60, | |
108 .minheatofftime = 60, | |
49
91e06007fe23
Default to idle mode (saves on compressor stress when testing)
darius@Inchoate
parents:
46
diff
changeset
|
109 .mode = TC_MODE_IDLE, |
46 | 110 .coolbits = _BV(6), |
111 .heatbits = _BV(7), | |
41 | 112 .idlebits = 0x00, |
113 .check_interval = 10, | |
54
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
114 .stale_factor = 3, |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
115 .dobeep = 1 |
41 | 116 }; |
117 | |
118 /* Local variable declarations */ | |
119 volatile static time_t now; | |
54
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
120 #ifndef WITHUSB |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
121 volatile static uint8_t beeping = 0; |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
122 volatile static uint8_t lasttoggle = 0; |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
123 #endif |
41 | 124 |
125 /* Local function prototypes */ | |
126 static void tempctrl_load_or_init_settings(void); | |
127 static void tempctrl_default_settings(void); | |
128 static void tempctrl_write_settings(void); | |
129 static void setstate(char state); | |
130 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
|
131 static void printtemp(char *name, int tmp, char *trailer); |
41 | 132 |
133 /* | |
134 * tempctrl_init | |
135 * | |
136 * Setup timer, should be called with interrupts disabled. | |
137 * | |
138 */ | |
139 void | |
140 tempctrl_init(void) { | |
141 /* Setup timer */ | |
142 /* 16Mhz / 1024 = 15625 Hz / 125 = 125 Hz = IRQ every 8 ms */ | |
143 | |
144 /* CTC mode, no output on pin, Divide clock by 1024 */ | |
54
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
145 TCCR2 = _BV(WGM21)| _BV(CS22) | _BV(CS21) | _BV(CS20); |
41 | 146 |
147 /* Compare with ... */ | |
54
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
148 OCR2 = 125; |
41 | 149 |
150 /* Enable interrupt for match on A */ | |
54
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
151 TIMSK = _BV(OCIE2); |
41 | 152 |
153 now.sec = 0; | |
154 now.usec = 0; | |
155 | |
156 tempctrl_load_or_init_settings(); | |
157 } | |
158 | |
159 /* | |
54
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
160 * Timer 2 Compare IRQ |
41 | 161 * |
162 * Update time counter | |
163 */ | |
164 | |
54
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
165 ISR(TIMER2_COMP_vect) { |
46 | 166 wdt_reset(); |
167 | |
41 | 168 now.usec += 8000; /* 1000000 * 1 / F_CPU / 1024 / 125 */ |
169 while (now.usec > 1000000) { | |
170 now.usec -= 1000000; | |
171 now.sec++; | |
172 } | |
54
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
173 |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
174 #ifndef WITHUSB |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
175 if (beeping) { |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
176 lasttoggle++; |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
177 // 63 * 8ms = ~0.5s |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
178 if (lasttoggle > 63) { |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
179 DDRB ^= _BV(PB3); |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
180 lasttoggle = 0; |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
181 } |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
182 } else { |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
183 DDRB &= ~_BV(PB3); |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
184 lasttoggle = 0; |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
185 } |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
186 #endif |
41 | 187 } |
188 | |
189 /* | |
190 * tempctrl_update | |
191 * | |
192 * Should be called in a normal context, could run things that take a long time. | |
193 * (ie 1wire bus stuff) | |
194 * | |
195 */ | |
196 void | |
197 tempctrl_update(void) { | |
198 /* State variables */ | |
199 static int32_t checktime = 0; // Time of next check | |
46 | 200 static int32_t lastdata = INT32_MIN; // Last time we got data |
41 | 201 |
202 static int16_t fermenter_temp = 0; // Fermenter temperature | |
203 static int16_t fridge_temp = 0; // Fridge temperature | |
204 static int16_t ambient_temp = 0; // Ambient temperature | |
46 | 205 static int32_t lastheaton = INT32_MIN; // Last time the heater was on |
206 static int32_t lastheatoff = INT32_MIN;// Last time the heater was off | |
207 static int32_t lastcoolon = INT32_MIN; // Last time the cooler was on | |
208 static int32_t lastcooloff = INT32_MIN;// Last time the cooler was off | |
41 | 209 static char currstate = 'i'; // Current state |
46 | 210 /* We init to times to INT32_MIN so that things function properly when |
211 * now < settings.minheat/cool/on/offtime */ | |
41 | 212 |
213 /* Temporary variables */ | |
214 int32_t t; | |
215 int16_t diff; | |
216 char nextstate; | |
217 int forced; | |
218 int stale; | |
219 | |
220 t = gettod(); | |
221 /* Time to check temperatures? */ | |
222 if (t < checktime) | |
223 return; | |
224 | |
225 checktime = t + settings.check_interval; | |
226 | |
227 /* Don't do any logging, just force idle and leave */ | |
228 if (settings.mode == TC_MODE_NOTHING) { | |
229 nextstate = 'i'; | |
230 goto setstate; | |
231 } | |
232 | |
233 /* 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
|
234 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
|
235 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
|
236 ambient_temp = OWGetTemp(settings.ambient_ROM); |
41 | 237 |
50
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
238 if (fermenter_temp > OW_TEMP_BADVAL) |
41 | 239 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
|
240 |
41 | 241 /* Check for stale data */ |
242 if (lastdata + (settings.check_interval * settings.stale_factor) < t) | |
243 stale = 1; | |
244 else | |
245 stale = 0; | |
246 | |
247 /* Default to remaining as we are */ | |
248 nextstate = '-'; | |
249 | |
250 /* Temperature diff, -ve => too cold, +ve => too warm */ | |
251 diff = fermenter_temp - settings.target_temp; | |
252 | |
253 switch (currstate) { | |
254 case 'i': | |
255 /* If we're idle then only heat or cool if the temperate difference is out of the | |
256 * hysteresis band | |
257 */ | |
258 if (abs(diff) > settings.hysteresis) { | |
259 if (diff < 0 && settings.minheatofftime + lastheatoff < t) | |
260 nextstate = 'h'; | |
261 else if (diff > 0 && settings.mincoolofftime + lastcooloff < t) | |
262 nextstate = 'c'; | |
263 } | |
264 break; | |
265 | |
266 case 'c': | |
267 /* Work out if we should go idle (based on min on time & overshoot) */ | |
268 if (diff + settings.mincoolovershoot < 0 && | |
269 settings.mincoolontime + lastcoolon < t) | |
270 nextstate = 'i'; | |
271 break; | |
272 | |
273 case 'h': | |
274 if (diff - settings.minheatovershoot > 0 && | |
275 settings.minheatontime + lastheaton < t) | |
276 nextstate = 'i'; | |
277 break; | |
278 | |
279 default: | |
280 printf_P(PSTR("\r\nUnknown state %c, going to idle\n"), currstate); | |
281 nextstate = 'i'; | |
282 break; | |
283 } | |
284 | |
285 /* Override if we have stale data */ | |
286 if (stale) | |
287 nextstate = 'i'; | |
288 | |
54
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
289 /* Handle beeping */ |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
290 if (settings.dobeep && stale) |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
291 beeping = 1; |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
292 else |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
293 beeping = 0; |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
294 |
41 | 295 /* Handle state forcing */ |
296 if (settings.mode != TC_MODE_AUTO) | |
297 forced = 1; | |
298 else | |
299 forced = 0; | |
300 | |
301 if (settings.mode == TC_MODE_IDLE) | |
302 nextstate = 'i'; | |
303 else if (settings.mode == TC_MODE_HEAT) | |
304 nextstate = 'h'; | |
305 else if (settings.mode == TC_MODE_COOL) | |
306 nextstate = 'c'; | |
307 | |
42
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
308 // 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
|
309 switch (nextstate) { |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
310 case 'c': |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
311 if (currstate == 'h') |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
312 lastheatoff = t; |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
313 lastcoolon = t; |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
314 break; |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
315 |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
316 case 'h': |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
317 if (currstate == 'c') |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
318 lastcooloff = t; |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
319 lastheaton = t; |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
320 break; |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
321 |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
322 default: |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
323 if (currstate == 'c') |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
324 lastcooloff = t; |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
325 if (currstate == 'h') |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
326 lastheatoff = t; |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
327 } |
97ae82023d5b
Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents:
41
diff
changeset
|
328 |
41 | 329 if (nextstate != '-') |
330 currstate = nextstate; | |
331 | |
50
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
332 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
|
333 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
|
334 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
|
335 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
|
336 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
|
337 printf_P(PSTR("State: %S, Flags: %S%S\r\n"), state2long(currstate), |
41 | 338 forced ? PSTR("F") : PSTR(""), |
339 stale ? PSTR("S") : PSTR("")); | |
340 | |
341 setstate: | |
342 setstate(currstate); | |
343 } | |
344 | |
345 /* Return 'time of day' (really uptime) */ | |
346 int32_t | |
347 gettod(void) { | |
348 int32_t t; | |
349 | |
350 cli(); | |
351 t = now.sec; | |
352 sei(); | |
353 | |
354 return(t); | |
355 } | |
356 | |
50
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
357 /* |
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
358 * 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
|
359 */ |
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
360 static void |
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
361 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
|
362 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
|
363 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
|
364 else |
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
365 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
|
366 } |
a13e0ccc1d2d
Rejig how temperatures are logged. Print out the final line at the end so it
darius@Inchoate
parents:
49
diff
changeset
|
367 |
41 | 368 /* Read the settings from EEPROM |
369 * If the CRC fails then reload from flash | |
370 */ | |
371 static void | |
372 tempctrl_load_or_init_settings(void) { | |
373 uint8_t *dptr; | |
374 uint16_t crc, strcrc; | |
375 int i; | |
376 | |
377 crc = 0; | |
378 eeprom_busy_wait(); | |
379 eeprom_read_block(&settings, &ee_area.settings, sizeof(settings_t)); | |
380 strcrc = eeprom_read_word(&ee_area.crc); | |
381 | |
382 dptr = (uint8_t *)&settings; | |
383 | |
384 for (i = 0; i < sizeof(settings_t); i++) | |
385 crc = _crc16_update(crc, dptr[i]); | |
386 | |
387 /* All OK? */ | |
388 if (crc == strcrc) | |
389 return; | |
390 | |
391 printf_P(PSTR("CRC mismatch got 0x%04x vs 0x%04x, setting defaults\r\n"), crc, strcrc); | |
392 tempctrl_default_settings(); | |
393 tempctrl_write_settings(); | |
394 } | |
395 | |
396 /* Load in the defaults from flash */ | |
397 static void | |
398 tempctrl_default_settings(void) { | |
399 memcpy_P(&settings, &default_settings, sizeof(settings_t)); | |
400 } | |
401 | |
402 /* Write the current settings out to EEPROM */ | |
403 static void | |
404 tempctrl_write_settings(void) { | |
405 uint16_t crc; | |
406 uint8_t *dptr; | |
407 int i; | |
408 | |
409 eeprom_busy_wait(); | |
410 eeprom_write_block(&settings, &ee_area.settings, sizeof(settings_t)); | |
411 | |
412 dptr = (uint8_t *)&settings; | |
413 crc = 0; | |
414 for (i = 0; i < sizeof(settings_t); i++) | |
415 crc = _crc16_update(crc, dptr[i]); | |
416 | |
417 eeprom_write_word(&ee_area.crc, crc); | |
418 } | |
419 | |
420 /* Set the relays to match the desired state */ | |
421 static void | |
422 setstate(char state) { | |
423 switch (state) { | |
424 case 'c': | |
425 PORTC = settings.coolbits; | |
426 break; | |
427 | |
428 case 'h': | |
429 PORTC = settings.heatbits; | |
430 break; | |
431 | |
432 default: | |
433 printf_P(PSTR("Unknown state %c, setting idle\r\n"), state); | |
434 /* fallthrough */ | |
435 | |
436 case 'i': | |
437 PORTC = settings.idlebits; | |
438 break; | |
439 } | |
440 } | |
441 | |
442 /* Handle user command | |
443 * | |
444 */ | |
445 void | |
446 tempctrl_cmd(char *buf) { | |
447 char cmd[6]; | |
448 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
|
449 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
|
450 |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
451 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
|
452 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
|
453 return; |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
454 } |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
455 |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
456 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
|
457 printf_P(PSTR( |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
458 "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
|
459 "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
|
460 "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
|
461 "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
|
462 "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
|
463 "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
|
464 " 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
|
465 " 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
|
466 " 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
|
467 " 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
|
468 " n Like idle but don't log anything\r\n" |
54
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
469 #ifndef WITHUSB |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
470 "tc beep [01] Enable/disable beeping when data is stale\r\n" |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
471 #endif |
51
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
472 "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
|
473 " 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
|
474 " 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
|
475 " 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
|
476 " 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
|
477 " 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
|
478 " 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
|
479 " 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
|
480 " 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
|
481 "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
|
482 " 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
|
483 " 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
|
484 "\r\n" |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
485 " 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
|
486 " 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
|
487 )); |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
488 return; |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
489 } |
41 | 490 |
51
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
491 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
|
492 tempctrl_write_settings(); |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
493 return; |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
494 } |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
495 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
|
496 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
|
497 return; |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
498 } |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
499 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
|
500 tempctrl_default_settings(); |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
501 return; |
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 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
|
504 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
|
505 "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
|
506 "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
|
507 "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
|
508 "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
|
509 "Min cool on time - %d, Min cool off time - %d\r\n" |
54
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
510 "Min heat on time - %d, Min heat off time - %d\r\n" |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
511 "Beep on stale - %S\r\n"), |
51
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
512 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
|
513 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
|
514 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
|
515 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
|
516 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
|
517 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
|
518 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
|
519 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
|
520 settings.mincoolontime, settings.minheatontime, |
54
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
521 settings.minheatontime, settings.minheatofftime, |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
522 settings.dobeep ? PSTR("yes") : PSTR("no")); |
51
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
523 return; |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
524 } |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
525 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
|
526 switch (buf[8]) { |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
527 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
|
528 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
|
529 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
|
530 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
|
531 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
|
532 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
|
533 break; |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
534 |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
535 default: |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
536 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
|
537 break; |
41 | 538 } |
51
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
539 return; |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
540 } |
54
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
541 if (!strcasecmp_P(cmd, PSTR("beep"))) { |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
542 if (buf[8] == '1') |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
543 settings.dobeep = 1; |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
544 else if (buf[8] == '0') |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
545 settings.dobeep = 0; |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
546 else |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
547 printf_P(PSTR("Expected a 0 or 1\r\n")); |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
548 return; |
58f1ec46bff6
Add a beeper when the data is stale (only when USB is disabled as they share
darius@Inchoate
parents:
51
diff
changeset
|
549 } |
51
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
550 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
|
551 !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
|
552 !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
|
553 |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
554 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
|
555 &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
|
556 &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
|
557 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
|
558 } else { |
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
559 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
|
560 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
|
561 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
|
562 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
|
563 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
|
564 memcpy(settings.ambient_ROM, ROM, sizeof(ROM)); |
41 | 565 } |
566 } | |
567 | |
51
cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
darius@Inchoate
parents:
50
diff
changeset
|
568 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
|
569 printf_P(PSTR("Unable to parse tc subcommand & value\r\n")); |
41 | 570 return; |
571 } | |
572 | |
573 if (!strcasecmp_P(cmd, PSTR("targ"))) { | |
574 settings.target_temp = data; | |
575 } else if (!strcasecmp_P(cmd, PSTR("hys"))) { | |
576 settings.hysteresis = data; | |
577 } else if (!strcasecmp_P(cmd, PSTR("mhov"))) { | |
578 settings.minheatovershoot = data; | |
579 } else if (!strcasecmp_P(cmd, PSTR("mcov"))) { | |
580 settings.mincoolovershoot = data; | |
581 } else if (!strcasecmp_P(cmd, PSTR("mcon"))) { | |
582 settings.mincoolontime = data; | |
583 } else if (!strcasecmp_P(cmd, PSTR("mcoff"))) { | |
584 settings.mincoolofftime = data; | |
585 } else if (!strcasecmp_P(cmd, PSTR("mhon"))) { | |
586 settings.minheatontime = data; | |
587 } else if (!strcasecmp_P(cmd, PSTR("mhoff"))) { | |
588 settings.minheatofftime = data; | |
589 } else { | |
590 printf_P(PSTR("Unknown setting\r\n")); | |
591 return; | |
592 } | |
593 } | |
594 | |
595 static const PROGMEM char* | |
596 state2long(char s) { | |
597 switch (s) { | |
598 case 'i': | |
599 return PSTR("idle"); | |
600 break; | |
601 | |
602 case 'c': | |
603 return PSTR("cool"); | |
604 break; | |
605 | |
606 case 'h': | |
607 return PSTR("heat"); | |
608 break; | |
609 | |
610 case '-': | |
611 return PSTR("-"); | |
612 break; | |
613 | |
614 default: | |
615 return PSTR("unknown"); | |
616 break; | |
617 } | |
618 } | |
619 |