annotate tempctrl.c @ 41:5898fba6593c

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