Mercurial > templog
annotate main.c @ 117:eecd2612a68e
stay on
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 05 Oct 2012 22:48:49 +0800 |
parents | 9afff8f29844 |
children | bb0c946460f3 |
rev | line source |
---|---|
0 | 1 #include <stdio.h> |
2 #include <string.h> | |
13 | 3 #include <stddef.h> |
116
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
4 #include <stdbool.h> |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
5 #include <stdlib.h> |
1 | 6 #include <avr/io.h> |
7 #include <avr/interrupt.h> | |
8 #include <avr/sleep.h> | |
7 | 9 #include <util/delay.h> |
6 | 10 #include <avr/pgmspace.h> |
13 | 11 #include <avr/eeprom.h> |
54
0d3d14af55c2
add "awake" and "reset" functions
Matt Johnston <matt@ucc.asn.au>
parents:
52
diff
changeset
|
12 #include <avr/wdt.h> |
59 | 13 #include <util/atomic.h> |
0 | 14 #include <util/crc16.h> |
15 | |
12 | 16 #include "simple_ds18b20.h" |
13 | 17 #include "onewire.h" |
3 | 18 |
0 | 19 // configuration params |
20 // - measurement interval | |
21 // - transmit interval | |
22 // - bluetooth params | |
23 // - number of sensors (and range?) | |
24 | |
80 | 25 // TICK should be 8 or less (8 untested). all timers need |
26 // to be a multiple. | |
27 | |
28 #define TICK 6 | |
29 // we have 1024 prescaler, 32768 crystal. | |
30 #define SLEEP_COMPARE (32*TICK-1) | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
31 |
75
ca08442635ca
report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
32 #define VALUE_NOSENSOR 0x07D0 // 125 degrees |
ca08442635ca
report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
33 #define VALUE_BROKEN 0x07D1 // 125.0625 |
13 | 34 |
10 | 35 #define BAUD 19200 |
7 | 36 #define UBRR ((F_CPU)/8/(BAUD)-1) |
37 | |
38 #define PORT_LED PORTC | |
39 #define DDR_LED DDRC | |
40 #define PIN_LED PC4 | |
1 | 41 |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
42 #define PORT_SHDN PORTD |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
43 #define DDR_SHDN DDRD |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
44 #define PIN_SHDN PD7 |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
45 |
115 | 46 #define PORT_FRIDGE PORTD |
47 #define DDR_FRIDGE DDRD | |
48 #define PIN_FRIDGE PD6 | |
49 | |
80 | 50 // total amount of 16bit values available for measurements. |
51 // adjust emperically, be sure to allow enough stack space too | |
52 #define TOTAL_MEASUREMENTS 840 | |
53 | |
54 // each sensor slot uses 8 bytes | |
55 #define MAX_SENSORS 6 | |
6 | 56 |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
57 // fixed at 8, have a shorter name |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
58 #define ID_LEN OW_ROMCODE_SIZE |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
59 |
33 | 60 // #define HAVE_UART_ECHO |
61 | |
90
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
62 // stores a value of clock_epoch combined with the remainder of TCNT2, |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
63 // for 1/32 second accuracy |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
64 struct epoch_ticks |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
65 { |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
66 uint32_t ticks; |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
67 // remainder |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
68 uint8_t rem; |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
69 }; |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
70 |
80 | 71 // eeprom-settable parameters. all timeouts should |
72 // be a multiple of TICK (6 seconds probably) | |
115 | 73 static uint16_t measure_wake = 61; // not a divisor of comms_wake |
74 static uint16_t comms_wake = 600; | |
80 | 75 static uint8_t wake_secs = 30; |
115 | 76 // decidegrees |
77 static int16_t fridge_setpoint = 180; // 18.0ºC | |
78 static int16_t fridge_difference = 5; // 0.5ºC | |
79 static uint16_t fridge_delay = 600; // seconds | |
114 | 80 |
59 | 81 // ---- Atomic guards required accessing these variables |
82 static uint32_t clock_epoch; | |
83 static uint16_t comms_count; | |
84 static uint16_t measure_count; | |
85 // ---- End atomic guards required | |
86 | |
17 | 87 static uint16_t n_measurements; |
59 | 88 |
80 | 89 // calculated at startup as TOTAL_MEASUREMENTS/n_sensors |
90 static uint16_t max_measurements; | |
91 | |
92 static uint16_t measurements[TOTAL_MEASUREMENTS]; | |
90
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
93 |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
94 static struct epoch_ticks first_measurement_clock; |
22
885532437100
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
95 // last_measurement_clock is redundant but checks that we're not missing |
885532437100
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
96 // samples |
90
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
97 static struct epoch_ticks last_measurement_clock; |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
98 static struct epoch_ticks last_comms_clock; |
59 | 99 |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
100 // boolean flags |
17 | 101 static uint8_t need_measurement; |
102 static uint8_t need_comms; | |
40 | 103 static uint8_t uart_enabled; |
80 | 104 static uint8_t stay_awake; |
90
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
105 static uint8_t button_pressed; |
1 | 106 |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
107 // counts down from WAKE_SECS to 0, goes to deep sleep when hits 0 |
17 | 108 static uint8_t comms_timeout; |
1 | 109 |
17 | 110 static uint8_t readpos; |
0 | 111 static char readbuf[30]; |
27
dbbd503119ba
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
26
diff
changeset
|
112 static uint8_t have_cmd; |
0 | 113 |
80 | 114 static uint8_t n_sensors; |
115 static uint8_t sensor_id[MAX_SENSORS][ID_LEN]; | |
116 | |
115 | 117 static int16_t last_fridge = DS18X20_INVALID_DECICELSIUS; |
118 static int16_t last_wort = DS18X20_INVALID_DECICELSIUS; | |
119 static struct epoch_ticks fridge_off_clock = {0}; | |
80 | 120 |
121 int uart_putchar(char c, FILE *stream); | |
122 static void long_delay(int ms); | |
123 static void blink(); | |
124 static uint16_t adc_vcc(); | |
125 | |
126 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, | |
127 _FDEV_SETUP_WRITE); | |
128 | |
129 static uint16_t crc_out; | |
130 static FILE _crc_stdout = FDEV_SETUP_STREAM(uart_putchar, NULL, | |
131 _FDEV_SETUP_WRITE); | |
132 // convenience | |
133 static FILE *crc_stdout = &_crc_stdout; | |
134 | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
135 |
13 | 136 // thanks to http://projectgus.com/2010/07/eeprom-access-with-arduino/ |
137 #define eeprom_read_to(dst_p, eeprom_field, dst_size) eeprom_read_block((dst_p), (void *)offsetof(struct __eeprom_data, eeprom_field), (dst_size)) | |
138 #define eeprom_read(dst, eeprom_field) eeprom_read_to((&dst), eeprom_field, sizeof(dst)) | |
139 #define eeprom_write_from(src_p, eeprom_field, src_size) eeprom_write_block((src_p), (void *)offsetof(struct __eeprom_data, eeprom_field), (src_size)) | |
140 #define eeprom_write(src, eeprom_field) { eeprom_write_from(&src, eeprom_field, sizeof(src)); } | |
141 | |
142 #define EXPECT_MAGIC 0x67c9 | |
143 | |
144 struct __attribute__ ((__packed__)) __eeprom_data { | |
145 uint16_t magic; | |
80 | 146 uint16_t measure_wake; |
147 uint16_t comms_wake; | |
148 uint8_t wake_secs; | |
114 | 149 |
115 | 150 int16_t fridge_setpoint; // decidegrees |
151 uint8_t fridge_difference; // decidegrees | |
152 uint16_t fridge_delay; | |
114 | 153 |
115 | 154 #if 0 |
155 static uint8_t wort_id[ID_LEN]; | |
156 static uint8_t fridge_id[ID_LEN]; | |
157 #endif | |
158 }; | |
114 | 159 |
115 | 160 static const uint8_t fridge_id[ID_LEN] = |
161 {0x28,0xCE,0xB2,0x1A,0x03,0x00,0x00,0x99}; | |
162 static const uint8_t wort_id[ID_LEN] = | |
163 {0x28,0x49,0xBC,0x1A,0x03,0x00,0x00,0x54}; | |
13 | 164 |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
165 static void deep_sleep(); |
1 | 166 |
117 | 167 // 0 or 1 |
168 static uint8_t | |
169 is_fridge_on() | |
170 { | |
171 if (PORT_FRIDGE & _BV(PIN_FRIDGE)) | |
172 { | |
173 return 1; | |
174 } | |
175 else | |
176 { | |
177 return 0; | |
178 } | |
179 } | |
180 | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
181 // Very first setup |
18 | 182 static void |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
183 setup_chip() |
18 | 184 { |
54
0d3d14af55c2
add "awake" and "reset" functions
Matt Johnston <matt@ucc.asn.au>
parents:
52
diff
changeset
|
185 cli(); |
0d3d14af55c2
add "awake" and "reset" functions
Matt Johnston <matt@ucc.asn.au>
parents:
52
diff
changeset
|
186 |
0d3d14af55c2
add "awake" and "reset" functions
Matt Johnston <matt@ucc.asn.au>
parents:
52
diff
changeset
|
187 // stop watchdog timer (might have been used to cause a reset) |
0d3d14af55c2
add "awake" and "reset" functions
Matt Johnston <matt@ucc.asn.au>
parents:
52
diff
changeset
|
188 wdt_reset(); |
0d3d14af55c2
add "awake" and "reset" functions
Matt Johnston <matt@ucc.asn.au>
parents:
52
diff
changeset
|
189 MCUSR &= ~_BV(WDRF); |
0d3d14af55c2
add "awake" and "reset" functions
Matt Johnston <matt@ucc.asn.au>
parents:
52
diff
changeset
|
190 WDTCSR |= _BV(WDCE) | _BV(WDE); |
0d3d14af55c2
add "awake" and "reset" functions
Matt Johnston <matt@ucc.asn.au>
parents:
52
diff
changeset
|
191 WDTCSR = 0; |
0d3d14af55c2
add "awake" and "reset" functions
Matt Johnston <matt@ucc.asn.au>
parents:
52
diff
changeset
|
192 |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
193 // Set clock to 2mhz |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
194 CLKPR = _BV(CLKPCE); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
195 // divide by 4 |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
196 CLKPR = _BV(CLKPS1); |
52
c3f5e02c1c42
try a few more power saving measures, untested
Matt Johnston <matt@ucc.asn.au>
parents:
46
diff
changeset
|
197 |
c3f5e02c1c42
try a few more power saving measures, untested
Matt Johnston <matt@ucc.asn.au>
parents:
46
diff
changeset
|
198 // enable pullups |
c3f5e02c1c42
try a few more power saving measures, untested
Matt Johnston <matt@ucc.asn.au>
parents:
46
diff
changeset
|
199 PORTB = 0xff; // XXX change when using SPI |
c3f5e02c1c42
try a few more power saving measures, untested
Matt Johnston <matt@ucc.asn.au>
parents:
46
diff
changeset
|
200 PORTD = 0xff; |
c3f5e02c1c42
try a few more power saving measures, untested
Matt Johnston <matt@ucc.asn.au>
parents:
46
diff
changeset
|
201 PORTC = 0xff; |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
202 |
19 | 203 // 3.3v power for bluetooth and SD |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
204 DDR_LED |= _BV(PIN_LED); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
205 DDR_SHDN |= _BV(PIN_SHDN); |
19 | 206 |
115 | 207 DDR_FRIDGE |= _BV(PIN_FRIDGE); |
208 | |
46
9ccd965d938a
Use the PRR etc, set value to proper ones
Matt Johnston <matt@ucc.asn.au>
parents:
44
diff
changeset
|
209 // set pullup |
9ccd965d938a
Use the PRR etc, set value to proper ones
Matt Johnston <matt@ucc.asn.au>
parents:
44
diff
changeset
|
210 PORTD |= _BV(PD2); |
41 | 211 // INT0 setup |
52
c3f5e02c1c42
try a few more power saving measures, untested
Matt Johnston <matt@ucc.asn.au>
parents:
46
diff
changeset
|
212 EICRA = (1<<ISC01); // falling edge - data sheet says it won't work? |
41 | 213 EIMSK = _BV(INT0); |
52
c3f5e02c1c42
try a few more power saving measures, untested
Matt Johnston <matt@ucc.asn.au>
parents:
46
diff
changeset
|
214 |
c3f5e02c1c42
try a few more power saving measures, untested
Matt Johnston <matt@ucc.asn.au>
parents:
46
diff
changeset
|
215 // comparator disable |
c3f5e02c1c42
try a few more power saving measures, untested
Matt Johnston <matt@ucc.asn.au>
parents:
46
diff
changeset
|
216 ACSR = _BV(ACD); |
c3f5e02c1c42
try a few more power saving measures, untested
Matt Johnston <matt@ucc.asn.au>
parents:
46
diff
changeset
|
217 |
c3f5e02c1c42
try a few more power saving measures, untested
Matt Johnston <matt@ucc.asn.au>
parents:
46
diff
changeset
|
218 // disable adc pin input buffers |
c3f5e02c1c42
try a few more power saving measures, untested
Matt Johnston <matt@ucc.asn.au>
parents:
46
diff
changeset
|
219 DIDR0 = 0x3F; // acd0-adc5 |
c3f5e02c1c42
try a few more power saving measures, untested
Matt Johnston <matt@ucc.asn.au>
parents:
46
diff
changeset
|
220 DIDR1 = (1<<AIN1D)|(1<<AIN0D); // ain0/ain1 |
c3f5e02c1c42
try a few more power saving measures, untested
Matt Johnston <matt@ucc.asn.au>
parents:
46
diff
changeset
|
221 |
c3f5e02c1c42
try a few more power saving measures, untested
Matt Johnston <matt@ucc.asn.au>
parents:
46
diff
changeset
|
222 sei(); |
18 | 223 } |
224 | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
225 static void |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
226 set_aux_power(uint8_t on) |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
227 { |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
228 if (on) |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
229 { |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
230 PORT_SHDN &= ~_BV(PIN_SHDN); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
231 } |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
232 else |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
233 { |
44 | 234 PORT_SHDN |= _BV(PIN_SHDN); |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
235 } |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
236 } |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
237 |
90
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
238 static void |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
239 get_epoch_ticks(struct epoch_ticks *t) |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
240 { |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
241 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
242 { |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
243 t->ticks = clock_epoch; |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
244 t->rem = TCNT2; |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
245 } |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
246 } |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
247 |
80 | 248 static void |
249 set_measurement(uint8_t sensor, uint16_t measurement, uint16_t reading) | |
250 { | |
251 measurements[sensor*max_measurements + measurement] = reading; | |
252 } | |
253 | |
254 static uint16_t | |
255 get_measurement(uint8_t sensor, uint16_t measurement) | |
256 { | |
257 return measurements[sensor*max_measurements + measurement]; | |
258 } | |
259 | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
260 static void |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
261 setup_tick_counter() |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
262 { |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
263 // set up counter2. |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
264 // COM21 COM20 Set OC2 on Compare Match (p116) |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
265 // WGM21 Clear counter on compare |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
266 //TCCR2A = _BV(COM2A1) | _BV(COM2A0) | _BV(WGM21); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
267 // toggle on match |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
268 TCCR2A = _BV(COM2A0); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
269 // CS22 CS21 CS20 clk/1024 |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
270 TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
271 // set async mode |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
272 ASSR |= _BV(AS2); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
273 TCNT2 = 0; |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
274 OCR2A = SLEEP_COMPARE; |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
275 // interrupt |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
276 TIMSK2 = _BV(OCIE2A); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
277 } |
18 | 278 |
0 | 279 static void |
7 | 280 uart_on() |
0 | 281 { |
8 | 282 // Power reduction register |
46
9ccd965d938a
Use the PRR etc, set value to proper ones
Matt Johnston <matt@ucc.asn.au>
parents:
44
diff
changeset
|
283 PRR &= ~_BV(PRUSART0); |
8 | 284 |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
285 // All of this needs to be done each time after turning off the PRR |
0 | 286 // baud rate |
7 | 287 UBRR0H = (unsigned char)(UBRR >> 8); |
288 UBRR0L = (unsigned char)UBRR; | |
289 // set 2x clock, improves accuracy of UBRR | |
290 UCSR0A |= _BV(U2X0); | |
6 | 291 UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0); |
0 | 292 //8N1 |
7 | 293 UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); |
41 | 294 uart_enabled = 1; |
6 | 295 } |
296 | |
297 static void | |
298 uart_off() | |
299 { | |
89
51d889ad39a3
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
87
diff
changeset
|
300 // Turn off interrupts and disable tx/rx |
6 | 301 UCSR0B = 0; |
41 | 302 uart_enabled = 0; |
6 | 303 |
304 // Power reduction register | |
46
9ccd965d938a
Use the PRR etc, set value to proper ones
Matt Johnston <matt@ucc.asn.au>
parents:
44
diff
changeset
|
305 PRR |= _BV(PRUSART0); |
0 | 306 } |
307 | |
10 | 308 int |
0 | 309 uart_putchar(char c, FILE *stream) |
310 { | |
41 | 311 if (!uart_enabled) |
312 { | |
313 return EOF; | |
314 } | |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
315 // XXX could perhaps sleep in the loop for power. |
8 | 316 if (c == '\n') |
317 { | |
10 | 318 loop_until_bit_is_set(UCSR0A, UDRE0); |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
319 UDR0 = '\r'; |
8 | 320 } |
2 | 321 loop_until_bit_is_set(UCSR0A, UDRE0); |
322 UDR0 = c; | |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
323 if (stream == crc_stdout) |
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
324 { |
26
d3e5934fe55c
- Move crc16 to utils and fix it
Matt Johnston <matt@ucc.asn.au>
parents:
25
diff
changeset
|
325 crc_out = _crc_ccitt_update(crc_out, c); |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
326 } |
10 | 327 if (c == '\r') |
328 { | |
329 loop_until_bit_is_set(UCSR0A, UDRE0); | |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
330 UDR0 = '\n'; |
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
331 if (stream == crc_stdout) |
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
332 { |
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
333 crc_out = _crc_ccitt_update(crc_out, '\n'); |
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
334 } |
10 | 335 } |
40 | 336 return (unsigned char)c; |
0 | 337 } |
338 | |
339 static void | |
340 cmd_fetch() | |
341 { | |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
342 crc_out = 0; |
55
8e897a682208
untested code to log voltage and internal temperature
Matt Johnston <matt@ucc.asn.au>
parents:
54
diff
changeset
|
343 |
81
4a2a82d6302c
try and be a bit more frugal with stack
Matt Johnston <matt@ucc.asn.au>
parents:
80
diff
changeset
|
344 fprintf_P(crc_stdout, PSTR("START\n")); |
59 | 345 { |
90
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
346 struct epoch_ticks now; |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
347 get_epoch_ticks(&now); |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
348 fprintf_P(crc_stdout, PSTR("now=%lu\n"), now.ticks); |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
349 fprintf_P(crc_stdout, PSTR("now_rem=%hhu\n"), now.rem); |
59 | 350 } |
81
4a2a82d6302c
try and be a bit more frugal with stack
Matt Johnston <matt@ucc.asn.au>
parents:
80
diff
changeset
|
351 fprintf_P(crc_stdout, PSTR("time_step=%hu\n"), measure_wake); |
90
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
352 fprintf_P(crc_stdout, PSTR("first_time=%lu\n"), first_measurement_clock.ticks); |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
353 fprintf_P(crc_stdout, PSTR("first_time_rem=%hhu\n"), first_measurement_clock.rem); |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
354 fprintf_P(crc_stdout, PSTR("last_time=%lu\n"), last_measurement_clock.ticks); |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
355 fprintf_P(crc_stdout, PSTR("last_time_rem=%hhu\n"), last_measurement_clock.rem); |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
356 fprintf_P(crc_stdout, PSTR("comms_time=%lu\n"), last_comms_clock.ticks); |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
357 fprintf_P(crc_stdout, PSTR("comms_time_rem=%hhu\n"), last_comms_clock.rem); |
81
4a2a82d6302c
try and be a bit more frugal with stack
Matt Johnston <matt@ucc.asn.au>
parents:
80
diff
changeset
|
358 fprintf_P(crc_stdout, PSTR("voltage=%hu\n"), adc_vcc()); |
87 | 359 fprintf_P(crc_stdout, PSTR("measure=%hu\n"), measure_wake); |
360 fprintf_P(crc_stdout, PSTR("comms=%hu\n"), comms_wake); | |
361 fprintf_P(crc_stdout, PSTR("wake=%hhu\n"), wake_secs); | |
115 | 362 fprintf_P(crc_stdout, PSTR("fridge=%.1f\n"), fridge_setpoint/10.0); |
363 fprintf_P(crc_stdout, PSTR("fridge_diff=%.1f\n"), fridge_difference/10.0); | |
364 fprintf_P(crc_stdout, PSTR("fridge_delay=%hu\n"), fridge_delay); | |
117 | 365 fprintf_P(crc_stdout, PSTR("fridge_status=%hhu\n"), is_fridge_on()); |
90
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
366 fprintf_P(crc_stdout, PSTR("tick_secs=%d\n"), TICK); |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
367 fprintf_P(crc_stdout, PSTR("tick_wake=%d\n"), SLEEP_COMPARE); |
87 | 368 fprintf_P(crc_stdout, PSTR("maxsens=%hhu\n"), MAX_SENSORS); |
369 fprintf_P(crc_stdout, PSTR("totalmeas=%hu\n"), TOTAL_MEASUREMENTS); | |
81
4a2a82d6302c
try and be a bit more frugal with stack
Matt Johnston <matt@ucc.asn.au>
parents:
80
diff
changeset
|
370 fprintf_P(crc_stdout, PSTR("sensors=%hhu\n"), n_sensors); |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
371 for (uint8_t s = 0; s < n_sensors; s++) |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
372 { |
81
4a2a82d6302c
try and be a bit more frugal with stack
Matt Johnston <matt@ucc.asn.au>
parents:
80
diff
changeset
|
373 fprintf_P(crc_stdout, PSTR("sensor_id%hhu="), s); |
76
6e47a61edc47
don't store sensors in eeprom, scan at startup instead
Matt Johnston <matt@ucc.asn.au>
parents:
75
diff
changeset
|
374 printhex(sensor_id[s], ID_LEN, crc_stdout); |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
375 fputc('\n', crc_stdout); |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
376 } |
36
a670a67ba489
- decrease measurement interval, measure at start
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
377 fprintf_P(crc_stdout, PSTR("measurements=%hu\n"), n_measurements); |
14 | 378 for (uint16_t n = 0; n < n_measurements; n++) |
0 | 379 { |
46
9ccd965d938a
Use the PRR etc, set value to proper ones
Matt Johnston <matt@ucc.asn.au>
parents:
44
diff
changeset
|
380 fprintf_P(crc_stdout, PSTR("meas%hu="), n); |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
381 for (uint8_t s = 0; s < n_sensors; s++) |
13 | 382 { |
80 | 383 fprintf_P(crc_stdout, PSTR(" %04hx"), get_measurement(s, n)); |
13 | 384 } |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
385 fputc('\n', crc_stdout); |
0 | 386 } |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
387 fprintf_P(crc_stdout, PSTR("END\n")); |
25
2943f62c8e62
- Make the python work on openwrt
Matt Johnston <matt@ucc.asn.au>
parents:
24
diff
changeset
|
388 fprintf_P(stdout, PSTR("CRC=%hu\n"), crc_out); |
0 | 389 } |
390 | |
391 static void | |
392 cmd_clear() | |
393 { | |
7 | 394 n_measurements = 0; |
32 | 395 printf_P(PSTR("cleared\n")); |
0 | 396 } |
397 | |
398 static void | |
399 cmd_btoff() | |
400 { | |
93
f18fd4257296
don't reset the wake time when btoff happens
Matt Johnston <matt@ucc.asn.au>
parents:
90
diff
changeset
|
401 uint8_t rem; |
f18fd4257296
don't reset the wake time when btoff happens
Matt Johnston <matt@ucc.asn.au>
parents:
90
diff
changeset
|
402 uint16_t count_copy; |
59 | 403 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
404 { | |
93
f18fd4257296
don't reset the wake time when btoff happens
Matt Johnston <matt@ucc.asn.au>
parents:
90
diff
changeset
|
405 count_copy = comms_count; |
f18fd4257296
don't reset the wake time when btoff happens
Matt Johnston <matt@ucc.asn.au>
parents:
90
diff
changeset
|
406 rem = TCNT2; |
59 | 407 } |
93
f18fd4257296
don't reset the wake time when btoff happens
Matt Johnston <matt@ucc.asn.au>
parents:
90
diff
changeset
|
408 printf_P(PSTR("next_wake=%hu,"), comms_wake-count_copy); |
90
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
409 printf_P(PSTR("rem=%hhu,"), rem); |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
410 printf_P(PSTR("tick_secs=%hhu,"), TICK); |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
411 printf_P(PSTR("tick_wake=%hhu\n"), SLEEP_COMPARE); |
46
9ccd965d938a
Use the PRR etc, set value to proper ones
Matt Johnston <matt@ucc.asn.au>
parents:
44
diff
changeset
|
412 _delay_ms(100); |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
413 comms_timeout = 0; |
117 | 414 stay_awake = 0; |
0 | 415 } |
416 | |
417 static void | |
54
0d3d14af55c2
add "awake" and "reset" functions
Matt Johnston <matt@ucc.asn.au>
parents:
52
diff
changeset
|
418 cmd_reset() |
0d3d14af55c2
add "awake" and "reset" functions
Matt Johnston <matt@ucc.asn.au>
parents:
52
diff
changeset
|
419 { |
0d3d14af55c2
add "awake" and "reset" functions
Matt Johnston <matt@ucc.asn.au>
parents:
52
diff
changeset
|
420 printf_P(PSTR("reset\n")); |
0d3d14af55c2
add "awake" and "reset" functions
Matt Johnston <matt@ucc.asn.au>
parents:
52
diff
changeset
|
421 _delay_ms(100); |
0d3d14af55c2
add "awake" and "reset" functions
Matt Johnston <matt@ucc.asn.au>
parents:
52
diff
changeset
|
422 cli(); // disable interrupts |
0d3d14af55c2
add "awake" and "reset" functions
Matt Johnston <matt@ucc.asn.au>
parents:
52
diff
changeset
|
423 wdt_enable(WDTO_15MS); // enable watchdog |
0d3d14af55c2
add "awake" and "reset" functions
Matt Johnston <matt@ucc.asn.au>
parents:
52
diff
changeset
|
424 while(1); // wait for watchdog to reset processor |
0d3d14af55c2
add "awake" and "reset" functions
Matt Johnston <matt@ucc.asn.au>
parents:
52
diff
changeset
|
425 } |
0d3d14af55c2
add "awake" and "reset" functions
Matt Johnston <matt@ucc.asn.au>
parents:
52
diff
changeset
|
426 |
0d3d14af55c2
add "awake" and "reset" functions
Matt Johnston <matt@ucc.asn.au>
parents:
52
diff
changeset
|
427 static void |
12 | 428 cmd_measure() |
429 { | |
33 | 430 printf_P(PSTR("measuring\n")); |
12 | 431 need_measurement = 1; |
432 } | |
433 | |
434 static void | |
435 cmd_sensors() | |
436 { | |
437 uint8_t ret = simple_ds18b20_start_meas(NULL); | |
81
4a2a82d6302c
try and be a bit more frugal with stack
Matt Johnston <matt@ucc.asn.au>
parents:
80
diff
changeset
|
438 printf_P(PSTR("All sensors, ret %hhu, waiting...\n"), ret); |
14 | 439 long_delay(DS18B20_TCONV_12BIT); |
12 | 440 simple_ds18b20_read_all(); |
441 } | |
442 | |
13 | 443 static void |
76
6e47a61edc47
don't store sensors in eeprom, scan at startup instead
Matt Johnston <matt@ucc.asn.au>
parents:
75
diff
changeset
|
444 init_sensors() |
13 | 445 { |
41 | 446 uint8_t id[OW_ROMCODE_SIZE]; |
76
6e47a61edc47
don't store sensors in eeprom, scan at startup instead
Matt Johnston <matt@ucc.asn.au>
parents:
75
diff
changeset
|
447 printf_P(PSTR("init sensors\n")); |
13 | 448 ow_reset(); |
41 | 449 for( uint8_t diff = OW_SEARCH_FIRST; diff != OW_LAST_DEVICE; ) |
450 { | |
451 diff = ow_rom_search( diff, &id[0] ); | |
452 if( diff == OW_PRESENCE_ERR ) { | |
453 printf_P( PSTR("No Sensor found\r") ); | |
454 return; | |
455 } | |
456 | |
457 if( diff == OW_DATA_ERR ) { | |
458 printf_P( PSTR("Bus Error\r") ); | |
459 return; | |
460 } | |
76
6e47a61edc47
don't store sensors in eeprom, scan at startup instead
Matt Johnston <matt@ucc.asn.au>
parents:
75
diff
changeset
|
461 |
6e47a61edc47
don't store sensors in eeprom, scan at startup instead
Matt Johnston <matt@ucc.asn.au>
parents:
75
diff
changeset
|
462 if (n_sensors < MAX_SENSORS) |
6e47a61edc47
don't store sensors in eeprom, scan at startup instead
Matt Johnston <matt@ucc.asn.au>
parents:
75
diff
changeset
|
463 { |
6e47a61edc47
don't store sensors in eeprom, scan at startup instead
Matt Johnston <matt@ucc.asn.au>
parents:
75
diff
changeset
|
464 memcpy(sensor_id[n_sensors], id, ID_LEN); |
81
4a2a82d6302c
try and be a bit more frugal with stack
Matt Johnston <matt@ucc.asn.au>
parents:
80
diff
changeset
|
465 printf_P(PSTR("Added sensor %hhu : "), n_sensors); |
76
6e47a61edc47
don't store sensors in eeprom, scan at startup instead
Matt Johnston <matt@ucc.asn.au>
parents:
75
diff
changeset
|
466 printhex(id, ID_LEN, stdout); |
6e47a61edc47
don't store sensors in eeprom, scan at startup instead
Matt Johnston <matt@ucc.asn.au>
parents:
75
diff
changeset
|
467 putchar('\n'); |
6e47a61edc47
don't store sensors in eeprom, scan at startup instead
Matt Johnston <matt@ucc.asn.au>
parents:
75
diff
changeset
|
468 n_sensors++; |
6e47a61edc47
don't store sensors in eeprom, scan at startup instead
Matt Johnston <matt@ucc.asn.au>
parents:
75
diff
changeset
|
469 } |
6e47a61edc47
don't store sensors in eeprom, scan at startup instead
Matt Johnston <matt@ucc.asn.au>
parents:
75
diff
changeset
|
470 else |
6e47a61edc47
don't store sensors in eeprom, scan at startup instead
Matt Johnston <matt@ucc.asn.au>
parents:
75
diff
changeset
|
471 { |
6e47a61edc47
don't store sensors in eeprom, scan at startup instead
Matt Johnston <matt@ucc.asn.au>
parents:
75
diff
changeset
|
472 printf_P(PSTR("Too many sensors\n")); |
6e47a61edc47
don't store sensors in eeprom, scan at startup instead
Matt Johnston <matt@ucc.asn.au>
parents:
75
diff
changeset
|
473 } |
13 | 474 } |
80 | 475 |
476 max_measurements = TOTAL_MEASUREMENTS / n_sensors; | |
477 } | |
478 | |
479 static void | |
480 load_params() | |
481 { | |
482 uint16_t magic; | |
483 eeprom_read(magic, magic); | |
484 if (magic == EXPECT_MAGIC) | |
485 { | |
486 eeprom_read(measure_wake, measure_wake); | |
487 eeprom_read(comms_wake, comms_wake); | |
488 eeprom_read(wake_secs, wake_secs); | |
115 | 489 eeprom_read(fridge_setpoint, fridge_setpoint); |
490 eeprom_read(fridge_difference, fridge_difference); | |
491 eeprom_read(fridge_delay, fridge_delay); | |
80 | 492 } |
493 } | |
494 | |
495 static void | |
496 cmd_get_params() | |
497 { | |
81
4a2a82d6302c
try and be a bit more frugal with stack
Matt Johnston <matt@ucc.asn.au>
parents:
80
diff
changeset
|
498 printf_P(PSTR("measure %hu\n"), measure_wake); |
4a2a82d6302c
try and be a bit more frugal with stack
Matt Johnston <matt@ucc.asn.au>
parents:
80
diff
changeset
|
499 printf_P(PSTR("comms %hu\n"), comms_wake); |
4a2a82d6302c
try and be a bit more frugal with stack
Matt Johnston <matt@ucc.asn.au>
parents:
80
diff
changeset
|
500 printf_P(PSTR("wake %hhu\n"), wake_secs); |
4a2a82d6302c
try and be a bit more frugal with stack
Matt Johnston <matt@ucc.asn.au>
parents:
80
diff
changeset
|
501 printf_P(PSTR("tick %d\n"), TICK); |
116
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
502 printf_P(PSTR("fridge %.1fº\n"), fridge_setpoint / 10.0f); |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
503 printf_P(PSTR("fridge difference %.1fº\n"), fridge_difference / 10.0f); |
115 | 504 printf_P(PSTR("fridge_delay %hu\n"), fridge_delay); |
81
4a2a82d6302c
try and be a bit more frugal with stack
Matt Johnston <matt@ucc.asn.au>
parents:
80
diff
changeset
|
505 printf_P(PSTR("sensors %hhu (%hhu)\n"), |
4a2a82d6302c
try and be a bit more frugal with stack
Matt Johnston <matt@ucc.asn.au>
parents:
80
diff
changeset
|
506 n_sensors, MAX_SENSORS); |
4a2a82d6302c
try and be a bit more frugal with stack
Matt Johnston <matt@ucc.asn.au>
parents:
80
diff
changeset
|
507 printf_P(PSTR("meas %hu (%hu)\n"), |
80 | 508 max_measurements, TOTAL_MEASUREMENTS); |
13 | 509 } |
510 | |
511 static void | |
80 | 512 cmd_set_params(const char *params) |
13 | 513 { |
80 | 514 uint16_t new_measure_wake; |
515 uint16_t new_comms_wake; | |
516 uint8_t new_wake_secs; | |
116
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
517 int ret = sscanf_P(params, PSTR("%hu %hu %hhu"), |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
518 &new_measure_wake, &new_comms_wake, &new_wake_secs); |
80 | 519 |
116
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
520 if (ret != 3) |
13 | 521 { |
80 | 522 printf_P(PSTR("Bad values\n")); |
523 } | |
524 else | |
525 { | |
115 | 526 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
527 { | |
528 eeprom_write(new_measure_wake, measure_wake); | |
529 eeprom_write(new_comms_wake, comms_wake); | |
530 eeprom_write(new_wake_secs, wake_secs); | |
531 uint16_t magic = EXPECT_MAGIC; | |
532 eeprom_write(magic, magic); | |
533 } | |
80 | 534 printf_P(PSTR("set_params for next boot\n")); |
116
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
535 printf_P(PSTR("measure %hu comms %hu wake %hhu\n"), |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
536 new_measure_wake, new_comms_wake, new_wake_secs); |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
537 } |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
538 } |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
539 |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
540 // returns true if eeprom was written |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
541 static bool |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
542 set_initial_eeprom() |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
543 { |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
544 uint16_t magic; |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
545 eeprom_read(magic, magic); |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
546 if (magic == EXPECT_MAGIC) |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
547 { |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
548 return false; |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
549 } |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
550 |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
551 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
552 { |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
553 eeprom_write(measure_wake, measure_wake); |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
554 eeprom_write(comms_wake, comms_wake); |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
555 eeprom_write(wake_secs, wake_secs); |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
556 eeprom_write(fridge_setpoint, fridge_setpoint); |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
557 eeprom_write(fridge_difference, fridge_difference); |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
558 eeprom_write(fridge_delay, fridge_delay); |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
559 magic = EXPECT_MAGIC; |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
560 eeprom_write(magic, magic); |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
561 } |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
562 |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
563 return true; |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
564 } |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
565 |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
566 static void |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
567 cmd_set_fridge_setpoint(char *params) |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
568 { |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
569 float new_f = atof(params); |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
570 if (new_f < 2 || new_f > 30) |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
571 { |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
572 printf_P(PSTR("Bad fridge value %f\n"), new_f); |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
573 return; |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
574 } |
114 | 575 |
116
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
576 fridge_setpoint = new_f * 10; |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
577 bool written = set_initial_eeprom(); |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
578 if (!written) |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
579 { |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
580 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
581 { |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
582 eeprom_write(fridge_setpoint, fridge_setpoint); |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
583 } |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
584 } |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
585 } |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
586 |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
587 static void |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
588 cmd_set_fridge_difference(char *params) |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
589 { |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
590 float new_f = atof(params); |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
591 if (new_f < 0 || new_f > 30) |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
592 { |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
593 printf_P(PSTR("Bad fridge value %f\n"), new_f); |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
594 return; |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
595 } |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
596 |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
597 fridge_difference = new_f * 10; |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
598 bool written = set_initial_eeprom(); |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
599 if (!written) |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
600 { |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
601 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
602 { |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
603 eeprom_write(fridge_difference, fridge_difference); |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
604 } |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
605 } |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
606 } |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
607 |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
608 static void |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
609 cmd_set_fridge_delay(char *params) |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
610 { |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
611 uint16_t new_delay = atoi(params); |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
612 if (new_delay < 5) |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
613 { |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
614 printf_P(PSTR("Bad fridge delay %d\n"), new_delay); |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
615 return; |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
616 } |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
617 |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
618 fridge_delay = new_delay; |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
619 bool written = set_initial_eeprom(); |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
620 if (!written) |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
621 { |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
622 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
623 { |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
624 eeprom_write(fridge_delay, fridge_delay); |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
625 } |
114 | 626 } |
80 | 627 } |
115 | 628 |
80 | 629 static void |
630 cmd_awake() | |
631 { | |
632 stay_awake = 1; | |
633 printf_P(PSTR("awake\n")); | |
13 | 634 } |
635 | |
12 | 636 static void |
0 | 637 read_handler() |
638 { | |
6 | 639 if (strcmp_P(readbuf, PSTR("fetch")) == 0) |
0 | 640 { |
641 cmd_fetch(); | |
642 } | |
6 | 643 else if (strcmp_P(readbuf, PSTR("clear")) == 0) |
0 | 644 { |
645 cmd_clear(); | |
646 } | |
6 | 647 else if (strcmp_P(readbuf, PSTR("btoff")) == 0) |
0 | 648 { |
649 cmd_btoff(); | |
650 } | |
12 | 651 else if (strcmp_P(readbuf, PSTR("measure")) == 0) |
652 { | |
653 cmd_measure(); | |
654 } | |
655 else if (strcmp_P(readbuf, PSTR("sensors")) == 0) | |
656 { | |
657 cmd_sensors(); | |
658 } | |
80 | 659 else if (strcmp_P(readbuf, PSTR("get_params")) == 0) |
660 { | |
661 cmd_get_params(); | |
662 } | |
83 | 663 else if (strncmp_P(readbuf, PSTR("set_params "), 11) == 0) |
80 | 664 { |
83 | 665 cmd_set_params(&readbuf[11]); |
80 | 666 } |
667 else if (strcmp_P(readbuf, PSTR("awake")) == 0) | |
668 { | |
669 cmd_awake(); | |
670 } | |
116
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
671 else if (strncmp_P(readbuf, PSTR("fridge_setpoint "), 16) == 0) |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
672 { |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
673 cmd_set_fridge_setpoint(&readbuf[16]); |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
674 } |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
675 else if (strncmp_P(readbuf, PSTR("fridge_diff "), 12) == 0) |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
676 { |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
677 cmd_set_fridge_difference(&readbuf[12]); |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
678 } |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
679 else if (strncmp_P(readbuf, PSTR("fridge_delay "), 13) == 0) |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
680 { |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
681 cmd_set_fridge_delay(&readbuf[13]); |
9afff8f29844
set fridge params separatelly
Matt Johnston <matt@ucc.asn.au>
parents:
115
diff
changeset
|
682 } |
54
0d3d14af55c2
add "awake" and "reset" functions
Matt Johnston <matt@ucc.asn.au>
parents:
52
diff
changeset
|
683 else if (strcmp_P(readbuf, PSTR("reset")) == 0) |
0d3d14af55c2
add "awake" and "reset" functions
Matt Johnston <matt@ucc.asn.au>
parents:
52
diff
changeset
|
684 { |
0d3d14af55c2
add "awake" and "reset" functions
Matt Johnston <matt@ucc.asn.au>
parents:
52
diff
changeset
|
685 cmd_reset(); |
0d3d14af55c2
add "awake" and "reset" functions
Matt Johnston <matt@ucc.asn.au>
parents:
52
diff
changeset
|
686 } |
0 | 687 else |
688 { | |
83 | 689 printf_P(PSTR("Bad command '%s'\n"), readbuf); |
0 | 690 } |
691 } | |
692 | |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
19
diff
changeset
|
693 ISR(INT0_vect) |
18 | 694 { |
90
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
695 button_pressed = 1; |
24
44c5ab5ea879
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
22
diff
changeset
|
696 blink(); |
44c5ab5ea879
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
22
diff
changeset
|
697 _delay_ms(100); |
44c5ab5ea879
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
22
diff
changeset
|
698 blink(); |
18 | 699 } |
700 | |
701 | |
2 | 702 ISR(USART_RX_vect) |
0 | 703 { |
2 | 704 char c = UDR0; |
33 | 705 #ifdef HAVE_UART_ECHO |
12 | 706 uart_putchar(c, NULL); |
33 | 707 #endif |
22
885532437100
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
708 if (c == '\r' || c == '\n') |
0 | 709 { |
22
885532437100
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
710 if (readpos > 0) |
885532437100
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
711 { |
885532437100
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
712 readbuf[readpos] = '\0'; |
27
dbbd503119ba
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
26
diff
changeset
|
713 have_cmd = 1; |
22
885532437100
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
714 readpos = 0; |
885532437100
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
715 } |
0 | 716 } |
717 else | |
718 { | |
719 readbuf[readpos] = c; | |
720 readpos++; | |
721 if (readpos >= sizeof(readbuf)) | |
722 { | |
723 readpos = 0; | |
724 } | |
725 } | |
726 } | |
727 | |
2 | 728 ISR(TIMER2_COMPA_vect) |
1 | 729 { |
14 | 730 TCNT2 = 0; |
80 | 731 measure_count += TICK; |
732 comms_count += TICK; | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
733 |
80 | 734 clock_epoch += TICK; |
17 | 735 |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
736 if (comms_timeout != 0) |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
737 { |
80 | 738 comms_timeout -= TICK; |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
739 } |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
740 |
80 | 741 if (measure_count >= measure_wake) |
1 | 742 { |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
743 measure_count = 0; |
1 | 744 need_measurement = 1; |
745 } | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
746 |
80 | 747 if (comms_count >= comms_wake) |
41 | 748 { |
749 comms_count = 0; | |
750 need_comms = 1; | |
751 } | |
1 | 752 } |
753 | |
754 static void | |
755 deep_sleep() | |
756 { | |
757 // p119 of manual | |
2 | 758 OCR2A = SLEEP_COMPARE; |
759 loop_until_bit_is_clear(ASSR, OCR2AUB); | |
1 | 760 |
761 set_sleep_mode(SLEEP_MODE_PWR_SAVE); | |
762 sleep_mode(); | |
763 } | |
764 | |
765 static void | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
766 idle_sleep() |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
767 { |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
768 set_sleep_mode(SLEEP_MODE_IDLE); |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
769 sleep_mode(); |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
770 } |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
771 |
59 | 772 static uint16_t |
773 adc_vcc() | |
55
8e897a682208
untested code to log voltage and internal temperature
Matt Johnston <matt@ucc.asn.au>
parents:
54
diff
changeset
|
774 { |
8e897a682208
untested code to log voltage and internal temperature
Matt Johnston <matt@ucc.asn.au>
parents:
54
diff
changeset
|
775 PRR &= ~_BV(PRADC); |
8e897a682208
untested code to log voltage and internal temperature
Matt Johnston <matt@ucc.asn.au>
parents:
54
diff
changeset
|
776 |
62 | 777 // /16 prescaler |
55
8e897a682208
untested code to log voltage and internal temperature
Matt Johnston <matt@ucc.asn.au>
parents:
54
diff
changeset
|
778 ADCSRA = _BV(ADEN) | _BV(ADPS2); |
8e897a682208
untested code to log voltage and internal temperature
Matt Johnston <matt@ucc.asn.au>
parents:
54
diff
changeset
|
779 |
8e897a682208
untested code to log voltage and internal temperature
Matt Johnston <matt@ucc.asn.au>
parents:
54
diff
changeset
|
780 // set to measure 1.1 reference |
58 | 781 ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); |
60 | 782 // average a number of samples |
783 uint16_t sum = 0; | |
784 uint8_t num = 0; | |
785 for (uint8_t n = 0; n < 20; n++) | |
59 | 786 { |
787 ADCSRA |= _BV(ADSC); | |
788 loop_until_bit_is_clear(ADCSRA, ADSC); | |
55
8e897a682208
untested code to log voltage and internal temperature
Matt Johnston <matt@ucc.asn.au>
parents:
54
diff
changeset
|
789 |
60 | 790 uint8_t low_11 = ADCL; |
791 uint8_t high_11 = ADCH; | |
792 uint16_t val = low_11 + (high_11 << 8); | |
6 | 793 |
60 | 794 if (n >= 4) |
795 { | |
796 sum += val; | |
797 num++; | |
798 } | |
799 } | |
800 ADCSRA = 0; | |
801 PRR |= _BV(PRADC); | |
6 | 802 |
80 | 803 //float res_volts = 1.1 * 1024 * num / sum; |
804 //return 1000 * res_volts; | |
805 return ((uint32_t)1100*1024*num) / sum; | |
6 | 806 } |
807 | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
808 static void |
115 | 809 do_fridge() |
810 { | |
811 struct epoch_ticks now; | |
812 get_epoch_ticks(&now); | |
813 if (now.ticks - fridge_off_clock.ticks < fridge_delay) | |
814 { | |
815 return; | |
816 } | |
817 | |
818 if (last_wort == DS18X20_INVALID_DECICELSIUS) | |
819 { | |
820 // can't really do much sensible.... alert perhaps? | |
821 need_comms = 1; | |
822 return; | |
823 } | |
824 | |
825 int16_t wort_delta = last_wort - fridge_setpoint; | |
826 uint8_t fridge_on = PORT_FRIDGE & _BV(PIN_FRIDGE); | |
827 if (fridge_on) | |
828 { | |
829 if (last_wort <= fridge_setpoint) | |
830 { | |
831 // too cold, turn off | |
832 PORT_FRIDGE &= ~_BV(PIN_FRIDGE); | |
833 fridge_off_clock = now; | |
834 } | |
835 } | |
836 else | |
837 { | |
838 if (wort_delta > fridge_difference) | |
839 { | |
840 // too hot, turn on | |
841 PORT_FRIDGE |= _BV(PIN_FRIDGE); | |
842 } | |
843 } | |
844 } | |
845 | |
846 static void | |
1 | 847 do_measurement() |
848 { | |
75
ca08442635ca
report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
849 blink(); |
ca08442635ca
report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
850 |
90
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
851 /* Take the timer here since deep_sleep() below could take 6 seconds */ |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
852 get_epoch_ticks(&last_measurement_clock); |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
853 if (n_measurements == 0) |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
854 { |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
855 first_measurement_clock = last_measurement_clock; |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
856 } |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
857 |
55
8e897a682208
untested code to log voltage and internal temperature
Matt Johnston <matt@ucc.asn.au>
parents:
54
diff
changeset
|
858 simple_ds18b20_start_meas(NULL); |
59 | 859 // sleep rather than using a long delay |
117 | 860 idle_sleep(); |
59 | 861 //_delay_ms(DS18B20_TCONV_12BIT); |
13 | 862 |
80 | 863 if (n_measurements == max_measurements) |
13 | 864 { |
865 n_measurements = 0; | |
866 } | |
6 | 867 |
80 | 868 for (uint8_t s = 0; s < n_sensors; s++) |
13 | 869 { |
75
ca08442635ca
report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
870 uint16_t reading; |
80 | 871 uint8_t ret = simple_ds18b20_read_raw(sensor_id[s], &reading); |
872 if (ret != DS18X20_OK) | |
13 | 873 { |
80 | 874 reading = VALUE_BROKEN; |
13 | 875 } |
80 | 876 set_measurement(s, n_measurements, reading); |
115 | 877 |
878 if (memcmp(sensor_id[s], fridge_id, sizeof(fridge_id)) == 0) | |
879 { | |
880 last_fridge = ds18b20_raw16_to_decicelsius(reading);; | |
881 } | |
882 if (memcmp(sensor_id[s], wort_id, sizeof(wort_id)) == 0) | |
883 { | |
884 last_wort = ds18b20_raw16_to_decicelsius(reading);; | |
885 } | |
13 | 886 } |
22
885532437100
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
887 |
14 | 888 n_measurements++; |
1 | 889 } |
890 | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
891 static void |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
892 do_comms() |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
893 { |
90
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
894 get_epoch_ticks(&last_comms_clock); |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
895 |
41 | 896 // turn on bluetooth |
24
44c5ab5ea879
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
22
diff
changeset
|
897 set_aux_power(1); |
89
51d889ad39a3
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
87
diff
changeset
|
898 // avoid receiving rubbish, perhaps |
51d889ad39a3
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
87
diff
changeset
|
899 _delay_ms(50); |
7 | 900 uart_on(); |
41 | 901 |
902 // write sd card here? same 3.3v regulator... | |
903 | |
80 | 904 for (comms_timeout = wake_secs; |
905 comms_timeout > 0 || stay_awake; | |
906 ) | |
41 | 907 { |
6 | 908 if (need_measurement) |
909 { | |
14 | 910 need_measurement = 0; |
6 | 911 do_measurement(); |
115 | 912 do_fridge(); |
44 | 913 continue; |
6 | 914 } |
915 | |
27
dbbd503119ba
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
26
diff
changeset
|
916 if (have_cmd) |
dbbd503119ba
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
26
diff
changeset
|
917 { |
dbbd503119ba
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
26
diff
changeset
|
918 have_cmd = 0; |
dbbd503119ba
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
26
diff
changeset
|
919 read_handler(); |
44 | 920 continue; |
27
dbbd503119ba
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
26
diff
changeset
|
921 } |
dbbd503119ba
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
26
diff
changeset
|
922 |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
923 // wait for commands from the master |
41 | 924 idle_sleep(); |
925 } | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
926 |
6 | 927 uart_off(); |
46
9ccd965d938a
Use the PRR etc, set value to proper ones
Matt Johnston <matt@ucc.asn.au>
parents:
44
diff
changeset
|
928 // in case bluetooth takes time to flush |
9ccd965d938a
Use the PRR etc, set value to proper ones
Matt Johnston <matt@ucc.asn.au>
parents:
44
diff
changeset
|
929 _delay_ms(100); |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
930 set_aux_power(0); |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
931 } |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
932 |
7 | 933 static void |
934 blink() | |
935 { | |
936 PORT_LED &= ~_BV(PIN_LED); | |
9 | 937 _delay_ms(1); |
7 | 938 PORT_LED |= _BV(PIN_LED); |
939 } | |
940 | |
941 static void | |
942 long_delay(int ms) | |
943 { | |
944 int iter = ms / 100; | |
945 | |
946 for (int i = 0; i < iter; i++) | |
947 { | |
948 _delay_ms(100); | |
949 } | |
950 } | |
951 | |
8 | 952 ISR(BADISR_vect) |
953 { | |
14 | 954 //uart_on(); |
8 | 955 printf_P(PSTR("Bad interrupt\n")); |
956 } | |
957 | |
0 | 958 int main(void) |
959 { | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
960 setup_chip(); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
961 blink(); |
9 | 962 |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
963 set_aux_power(0); |
7 | 964 |
6 | 965 stdout = &mystdout; |
7 | 966 uart_on(); |
967 | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
968 printf(PSTR("Started.\n")); |
13 | 969 |
80 | 970 load_params(); |
13 | 971 |
76
6e47a61edc47
don't store sensors in eeprom, scan at startup instead
Matt Johnston <matt@ucc.asn.au>
parents:
75
diff
changeset
|
972 init_sensors(); |
6e47a61edc47
don't store sensors in eeprom, scan at startup instead
Matt Johnston <matt@ucc.asn.au>
parents:
75
diff
changeset
|
973 |
8 | 974 uart_off(); |
0 | 975 |
6 | 976 // turn off everything except timer2 |
46
9ccd965d938a
Use the PRR etc, set value to proper ones
Matt Johnston <matt@ucc.asn.au>
parents:
44
diff
changeset
|
977 PRR = _BV(PRTWI) | _BV(PRTIM0) | _BV(PRTIM1) | _BV(PRSPI) | _BV(PRUSART0) | _BV(PRADC); |
8 | 978 |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
979 setup_tick_counter(); |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
980 |
12 | 981 sei(); |
8 | 982 |
25
2943f62c8e62
- Make the python work on openwrt
Matt Johnston <matt@ucc.asn.au>
parents:
24
diff
changeset
|
983 need_comms = 1; |
36
a670a67ba489
- decrease measurement interval, measure at start
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
984 need_measurement = 1; |
25
2943f62c8e62
- Make the python work on openwrt
Matt Johnston <matt@ucc.asn.au>
parents:
24
diff
changeset
|
985 |
117 | 986 stay_awake = 1; |
987 | |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
988 for(;;) |
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
989 { |
90
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
990 if (button_pressed) |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
991 { |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
992 // debounce |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
993 _delay_ms(200); |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
994 need_comms = 1; |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
995 comms_timeout = wake_secs; |
a1c1c2d475b0
print the remainder of timers as well
Matt Johnston <matt@ucc.asn.au>
parents:
89
diff
changeset
|
996 button_pressed = 0; |
41 | 997 continue; |
1 | 998 } |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
999 |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
1000 if (need_comms) |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
1001 { |
16
5075d8c428bd
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
15
diff
changeset
|
1002 need_comms = 0; |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
1003 do_comms(); |
41 | 1004 continue; |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
1005 } |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
1006 |
97 | 1007 if (need_measurement) |
1008 { | |
1009 need_measurement = 0; | |
1010 do_measurement(); | |
115 | 1011 do_fridge(); |
97 | 1012 continue; |
1013 } | |
1014 | |
41 | 1015 deep_sleep(); |
0 | 1016 } |
12 | 1017 |
0 | 1018 return 0; /* never reached */ |
1019 } |