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