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