annotate tempctrl.c @ 45:efd44dc40934

Add macros for number system used for temperatures. Add error strings and improve comments for ROM programming.
author darius@Inchoate
date Mon, 20 Oct 2008 22:11:18 +1030
parents f219538d6ea7
children fb272cb82bcb
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 typedef struct {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
42 int32_t sec;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
43 int32_t usec;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
44 } time_t;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
45
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
46 /* Holds all the settings needed */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
47 typedef struct {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
48 uint8_t fermenter_ROM[8];
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
49 uint8_t fridge_ROM[8];
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
50 uint8_t ambient_ROM[8];
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
51 int16_t target_temp;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
52 uint16_t hysteresis;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
53
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
54 /* How much to under/overshoot on heating/cooling */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
55 int16_t minheatovershoot;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
56 int16_t mincoolovershoot;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
57
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
58 /* Minimum time the cooler can be on/off */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
59 int16_t mincoolontime;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
60 int16_t mincoolofftime;
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 heater can be on/off */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
63 int16_t minheatontime;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
64 int16_t minheatofftime;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
65
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
66 #define TC_MODE_AUTO 'a' /* Automatic control */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
67 #define TC_MODE_HEAT 'h' /* Force heating */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
68 #define TC_MODE_COOL 'c' /* Force cooling */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
69 #define TC_MODE_IDLE 'i' /* Force idle */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
70 #define TC_MODE_NOTHING 'n' /* Do nothing (like idle but log nothing) */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
71 char mode;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
72
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
73 /* Bit patterns for various modes */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
74 uint8_t coolbits;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
75 uint8_t heatbits;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
76 uint8_t idlebits;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
77
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
78 /* Check/stale times */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
79 int16_t check_interval;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
80 int16_t stale_factor;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
81 } __attribute__((packed)) settings_t;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
82
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
83 /* Current settings in RAM */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
84 static settings_t settings;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
85
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
86 /* Our map of EEPROM */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
87 struct {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
88 settings_t settings;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
89 uint16_t crc;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
90 } ee_area __attribute__((section(".eeprom")));
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
91
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
92 /* Defaults that are shoved into EEPROM if it isn't inited */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
93 const PROGMEM settings_t default_settings = {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
94 .fermenter_ROM = { 0x10, 0xeb, 0x48, 0x21, 0x01, 0x08, 0x00, 0xdf },
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
95 .fridge_ROM = { 0x10, 0xa6, 0x2a, 0xc4, 0x00, 0x08, 0x00, 0x11 },
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
96 .ambient_ROM = { 0x10, 0x97, 0x1b, 0xfe, 0x00, 0x08, 0x00, 0xd1 },
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
97 .target_temp = 1400,
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
98 .hysteresis = 100,
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
99 .minheatovershoot = 50,
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
100 .mincoolovershoot = -50,
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
101 .mincoolontime = 300,
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
102 .mincoolofftime = 600,
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
103 .minheatontime = 60,
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
104 .minheatofftime = 60,
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
105 .mode = TC_MODE_AUTO,
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
106 .coolbits = _BV(7),
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
107 .heatbits = _BV(6),
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
108 .idlebits = 0x00,
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
109 .check_interval = 10,
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
110 .stale_factor = 3
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
111 };
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
112
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
113 /* Local variable declarations */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
114 volatile static time_t now;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
115
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
116 /* Local function prototypes */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
117 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
118 static void tempctrl_load_or_init_settings(void);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
119 static void tempctrl_default_settings(void);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
120 static void tempctrl_write_settings(void);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
121 static void setstate(char state);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
122 static const PROGMEM char*state2long(char s);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
123
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
124 /*
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
125 * tempctrl_init
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
126 *
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
127 * Setup timer, should be called with interrupts disabled.
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
128 *
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
129 */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
130 void
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
131 tempctrl_init(void) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
132 /* Setup timer */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
133 /* 16Mhz / 1024 = 15625 Hz / 125 = 125 Hz = IRQ every 8 ms */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
134
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
135 /* CTC mode, no output on pin, Divide clock by 1024 */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
136 TCCR0 = _BV(WGM01)| _BV(CS02) | _BV(CS00);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
137
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
138 /* Compare with ... */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
139 OCR0 = 125;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
140
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
141 /* Enable interrupt for match on A */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
142 TIMSK = _BV(OCIE0);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
143
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
144 now.sec = 0;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
145 now.usec = 0;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
146
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
147 tempctrl_load_or_init_settings();
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
148 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
149
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
150 /*
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
151 * Timer 0 Compare IRQ
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
152 *
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
153 * Update time counter
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
154 */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
155
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
156 ISR(TIMER0_COMP_vect) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
157 now.usec += 8000; /* 1000000 * 1 / F_CPU / 1024 / 125 */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
158 while (now.usec > 1000000) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
159 now.usec -= 1000000;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
160 now.sec++;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
161 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
162 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
163
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
164 /*
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
165 * tempctrl_update
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
166 *
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
167 * 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
168 * (ie 1wire bus stuff)
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
169 *
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
170 */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
171 void
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
172 tempctrl_update(void) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
173 /* State variables */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
174 static int32_t checktime = 0; // Time of next check
43
51e7458d2e76 Init lastdata same as lastheat/coolon/off otherwise we won't handle thing
darius@inchoate.localdomain
parents: 42
diff changeset
175 static int32_t lastdata = -100000; // Last time we got data
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
176
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
177 static int16_t fermenter_temp = 0; // Fermenter temperature
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
178 static int16_t fridge_temp = 0; // Fridge temperature
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
179 static int16_t ambient_temp = 0; // Ambient temperature
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
180 // These are inited like this so we will still heat/cool when
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
181 // now < settings.minheatofftime
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
182 static int32_t lastheaton = -100000; // Last time the heater was on
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
183 static int32_t lastheatoff = -100000; // Last time the heater was off
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
184 static int32_t lastcoolon = -100000; // Last time the cooler was on
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
185 static int32_t lastcooloff = -100000; // Last time the cooler was off
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
186 static char currstate = 'i'; // Current state
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
187
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
188 /* Temporary variables */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
189 int32_t t;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
190 int16_t diff;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
191 char nextstate;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
192 int forced;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
193 int stale;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
194
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
195 t = gettod();
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
196 /* Time to check temperatures? */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
197 if (t < checktime)
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
198 return;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
199
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
200 checktime = t + settings.check_interval;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
201
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
202 /* Don't do any logging, just force idle and leave */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
203 if (settings.mode == TC_MODE_NOTHING) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
204 nextstate = 'i';
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
205 goto setstate;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
206 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
207
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
208 /* Update our temperatures */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
209 printf_P(PSTR("Time: %ld, Target: %d.%02d, "), now.sec, GETWHOLE(settings.target_temp),
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
210 GETFRAC(settings.target_temp));
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
211
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
212 if (gettemp(PSTR("Fermenter"), settings.fermenter_ROM, &fermenter_temp, 0))
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
213 lastdata = t;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
214
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
215 /* Check for stale data */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
216 if (lastdata + (settings.check_interval * settings.stale_factor) < t)
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
217 stale = 1;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
218 else
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
219 stale = 0;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
220
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
221 gettemp(PSTR("Fridge"), settings.fridge_ROM, &fridge_temp, 0);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
222 gettemp(PSTR("Ambient"), settings.ambient_ROM, &ambient_temp, 1);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
223
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
224 /* Default to remaining as we are */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
225 nextstate = '-';
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
226
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
227 /* Temperature diff, -ve => too cold, +ve => too warm */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
228 diff = fermenter_temp - settings.target_temp;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
229
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
230 switch (currstate) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
231 case 'i':
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
232 /* 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
233 * hysteresis band
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
234 */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
235 if (abs(diff) > settings.hysteresis) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
236 if (diff < 0 && settings.minheatofftime + lastheatoff < t)
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
237 nextstate = 'h';
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
238 else if (diff > 0 && settings.mincoolofftime + lastcooloff < t)
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
239 nextstate = 'c';
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
240 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
241 break;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
242
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
243 case 'c':
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
244 /* Work out if we should go idle (based on min on time & overshoot) */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
245 if (diff + settings.mincoolovershoot < 0 &&
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
246 settings.mincoolontime + lastcoolon < t)
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
247 nextstate = 'i';
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
248 break;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
249
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
250 case 'h':
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
251 if (diff - settings.minheatovershoot > 0 &&
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
252 settings.minheatontime + lastheaton < t)
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
253 nextstate = 'i';
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
254 break;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
255
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
256 default:
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
257 printf_P(PSTR("\r\nUnknown state %c, going to idle\n"), currstate);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
258 nextstate = 'i';
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
259 break;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
260 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
261
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
262 /* Override if we have stale data */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
263 if (stale)
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
264 nextstate = 'i';
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
265
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
266 /* Handle state forcing */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
267 if (settings.mode != TC_MODE_AUTO)
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
268 forced = 1;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
269 else
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
270 forced = 0;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
271
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
272 if (settings.mode == TC_MODE_IDLE)
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
273 nextstate = 'i';
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
274 else if (settings.mode == TC_MODE_HEAT)
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
275 nextstate = 'h';
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
276 else if (settings.mode == TC_MODE_COOL)
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
277 nextstate = 'c';
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
278
42
97ae82023d5b Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents: 41
diff changeset
279 // Keep track of when we last turned things on or off
97ae82023d5b Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents: 41
diff changeset
280 switch (nextstate) {
97ae82023d5b Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents: 41
diff changeset
281 case 'c':
97ae82023d5b Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents: 41
diff changeset
282 if (currstate == 'h')
97ae82023d5b Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents: 41
diff changeset
283 lastheatoff = t;
97ae82023d5b Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents: 41
diff changeset
284 lastcoolon = t;
97ae82023d5b Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents: 41
diff changeset
285 break;
97ae82023d5b Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents: 41
diff changeset
286
97ae82023d5b Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents: 41
diff changeset
287 case 'h':
97ae82023d5b Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents: 41
diff changeset
288 if (currstate == 'c')
97ae82023d5b Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents: 41
diff changeset
289 lastcooloff = t;
97ae82023d5b Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents: 41
diff changeset
290 lastheaton = t;
97ae82023d5b Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents: 41
diff changeset
291 break;
97ae82023d5b Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents: 41
diff changeset
292
97ae82023d5b Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents: 41
diff changeset
293 default:
97ae82023d5b Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents: 41
diff changeset
294 if (currstate == 'c')
97ae82023d5b Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents: 41
diff changeset
295 lastcooloff = t;
97ae82023d5b Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents: 41
diff changeset
296 if (currstate == 'h')
97ae82023d5b Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents: 41
diff changeset
297 lastheatoff = t;
97ae82023d5b Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents: 41
diff changeset
298 }
97ae82023d5b Actually keep track of when the heater & cooler go on/off..
darius@inchoate.localdomain
parents: 41
diff changeset
299
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
300 if (nextstate != '-')
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
301 currstate = nextstate;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
302
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
303 printf_P(PSTR(", State: %S, Flags: %S%S\r\n"), state2long(currstate),
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
304 forced ? PSTR("F") : PSTR(""),
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
305 stale ? PSTR("S") : PSTR(""));
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
306
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
307 setstate:
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
308 setstate(currstate);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
309 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
310
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
311 /*
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
312 * Log a temperature & store it if valid
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
313 *
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
314 * Returns 1 if it was valid, 0 otherwise
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
315 */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
316 static int
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
317 gettemp(const PROGMEM char *name, uint8_t *ROM, int16_t *temp, uint8_t last) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
318 int16_t tmp;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
319
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
320 tmp = OWGetTemp(ROM);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
321 printf_P(PSTR("%S: "), name);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
322 if (tmp > OW_TEMP_BADVAL) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
323 printf_P(PSTR("%d.%02d%S"), GETWHOLE(tmp), GETFRAC(tmp), last ? PSTR("") : PSTR(", "));
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
324 *temp = tmp;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
325 return(1);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
326 } else {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
327 printf_P(PSTR("NA (%d)%S"), tmp, last ? PSTR("") : PSTR(", "));
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
328 return(0);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
329 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
330 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
331
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
332 /* Return 'time of day' (really uptime) */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
333 int32_t
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
334 gettod(void) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
335 int32_t t;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
336
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
337 cli();
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
338 t = now.sec;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
339 sei();
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
340
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
341 return(t);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
342 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
343
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
344 /* Read the settings from EEPROM
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
345 * If the CRC fails then reload from flash
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
346 */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
347 static void
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
348 tempctrl_load_or_init_settings(void) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
349 uint8_t *dptr;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
350 uint16_t crc, strcrc;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
351 int i;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
352
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
353 crc = 0;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
354 eeprom_busy_wait();
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
355 eeprom_read_block(&settings, &ee_area.settings, sizeof(settings_t));
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
356 strcrc = eeprom_read_word(&ee_area.crc);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
357
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
358 dptr = (uint8_t *)&settings;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
359
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
360 for (i = 0; i < sizeof(settings_t); i++)
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
361 crc = _crc16_update(crc, dptr[i]);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
362
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
363 /* All OK? */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
364 if (crc == strcrc)
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
365 return;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
366
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
367 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
368 tempctrl_default_settings();
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
369 tempctrl_write_settings();
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
370 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
371
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
372 /* Load in the defaults from flash */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
373 static void
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
374 tempctrl_default_settings(void) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
375 memcpy_P(&settings, &default_settings, sizeof(settings_t));
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
376 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
377
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
378 /* Write the current settings out to EEPROM */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
379 static void
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
380 tempctrl_write_settings(void) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
381 uint16_t crc;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
382 uint8_t *dptr;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
383 int i;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
384
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
385 eeprom_busy_wait();
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
386 eeprom_write_block(&settings, &ee_area.settings, sizeof(settings_t));
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
387
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
388 dptr = (uint8_t *)&settings;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
389 crc = 0;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
390 for (i = 0; i < sizeof(settings_t); i++)
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
391 crc = _crc16_update(crc, dptr[i]);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
392
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
393 eeprom_write_word(&ee_area.crc, crc);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
394 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
395
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
396 /* Set the relays to match the desired state */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
397 static void
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
398 setstate(char state) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
399 switch (state) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
400 case 'c':
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
401 PORTC = settings.coolbits;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
402 break;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
403
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
404 case 'h':
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
405 PORTC = settings.heatbits;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
406 break;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
407
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
408 default:
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
409 printf_P(PSTR("Unknown state %c, setting idle\r\n"), state);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
410 /* fallthrough */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
411
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
412 case 'i':
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
413 PORTC = settings.idlebits;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
414 break;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
415 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
416 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
417
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
418 /* Handle user command
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
419 *
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
420 */
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
421 void
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
422 tempctrl_cmd(char *buf) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
423 char cmd[6];
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
424 int16_t data;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
425 int i;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
426
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
427 i = sscanf_P(buf, PSTR("tc %5s %d"), cmd, &data);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
428
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
429 if (i == 1) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
430 if (!strcasecmp_P(cmd, PSTR("help"))) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
431 printf_P(PSTR(
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
432 "tc help This help\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
433 "tc save Save settings to EEPROM\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
434 "tc load Load or default settings from EEPROM\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
435 "tc dflt Load defaults from flash\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
436 "tc list List current settings\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
437 "tc mode [achin] Change control mode, must be one of\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
438 " a Auto\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
439 " c Always cool\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
440 " h Always heat\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
441 " i Always idle\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
442 " n Like idle but don't log anything\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
443 "\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
444 "tc X Y Set X to Y where X is one of\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
445 " targ Target temperature\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
446 " hys Hysteresis range\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
447 " mhov Minimum heat overshoot\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
448 " mcov Minimum cool overshoot\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
449 " mcon Minimum cool on time\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
450 " mcoff Minimum cool off time\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
451 " mhin Minimum heat on time\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
452 " mhoff Minimum heat off time\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
453 " Times are in seconds\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
454 " Temperatures are in hundredths of degrees Celcius\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
455 ));
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
456 return;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
457 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
458
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
459 if (!strcasecmp_P(cmd, PSTR("save"))) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
460 tempctrl_write_settings();
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
461 return;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
462 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
463 if (!strcasecmp_P(cmd, PSTR("load"))) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
464 tempctrl_load_or_init_settings();
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
465 return;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
466 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
467 if (!strcasecmp_P(cmd, PSTR("dflt"))) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
468 tempctrl_default_settings();
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
469 return;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
470 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
471 if (!strcasecmp_P(cmd, PSTR("list"))) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
472 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
473 "Fridge ROM ID %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
474 "Ambient ROM ID %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
475 "Target - %d, Hystersis - %d\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
476 "Min heat overshoot - %d, Min cool overshoot - %d\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
477 "Min cool on time - %d, Min cool off time - %d\r\n"
44
f219538d6ea7 Fix typo.
darius@inchoate.localdomain
parents: 43
diff changeset
478 "Min heat on time - %d, Min heat off time - %d\r\n"),
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
479 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
480 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
481 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
482 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
483 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
484 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
485 settings.target_temp, settings.hysteresis,
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
486 settings.minheatovershoot, settings.mincoolovershoot,
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
487 settings.mincoolontime, settings.minheatontime,
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
488 settings.minheatontime, settings.minheatofftime);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
489 return;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
490 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
491 if (!strcasecmp_P(cmd, PSTR("mode"))) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
492 switch (buf[8]) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
493 case TC_MODE_AUTO:
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
494 case TC_MODE_HEAT:
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
495 case TC_MODE_COOL:
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
496 case TC_MODE_IDLE:
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
497 case TC_MODE_NOTHING:
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
498 settings.mode = buf[8];
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
499 break;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
500
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
501 default:
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
502 printf_P(PSTR("Unknown mode character '%c'\r\n"), buf[8]);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
503 break;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
504 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
505 return;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
506 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
507
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
508 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
509
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
510 if (i != 2) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
511 printf_P(PSTR("Unable to parse command\r\n"));
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
512 return;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
513 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
514
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
515 if (!strcasecmp_P(cmd, PSTR("targ"))) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
516 settings.target_temp = data;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
517 } else if (!strcasecmp_P(cmd, PSTR("hys"))) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
518 settings.hysteresis = data;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
519 } else if (!strcasecmp_P(cmd, PSTR("mhov"))) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
520 settings.minheatovershoot = data;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
521 } else if (!strcasecmp_P(cmd, PSTR("mcov"))) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
522 settings.mincoolovershoot = data;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
523 } else if (!strcasecmp_P(cmd, PSTR("mcon"))) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
524 settings.mincoolontime = data;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
525 } else if (!strcasecmp_P(cmd, PSTR("mcoff"))) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
526 settings.mincoolofftime = data;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
527 } else if (!strcasecmp_P(cmd, PSTR("mhon"))) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
528 settings.minheatontime = data;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
529 } else if (!strcasecmp_P(cmd, PSTR("mhoff"))) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
530 settings.minheatofftime = data;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
531 } else {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
532 printf_P(PSTR("Unknown setting\r\n"));
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
533 return;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
534 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
535 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
536
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
537 static const PROGMEM char*
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
538 state2long(char s) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
539 switch (s) {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
540 case 'i':
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
541 return PSTR("idle");
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
542 break;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
543
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
544 case 'c':
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
545 return PSTR("cool");
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
546 break;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
547
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
548 case 'h':
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
549 return PSTR("heat");
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
550 break;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
551
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
552 case '-':
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
553 return PSTR("-");
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
554 break;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
555
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
556 default:
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
557 return PSTR("unknown");
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
558 break;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
559 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
560 }
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents:
diff changeset
561