annotate tempctrl.c @ 61:0910ab6f0095

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