Mercurial > templog
annotate main.c @ 338:12123e390169
More minor work
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 14 Jun 2012 00:00:57 +0800 |
parents | 298e502fdcd4 |
children | 024f5571df8c |
rev | line source |
---|---|
306 | 1 /* Name: main.c |
2 * Author: <insert your name here> | |
3 * Copyright: <insert your copyright message here> | |
4 * License: <insert your license reference here> | |
5 */ | |
6 | |
7 #include <stdio.h> | |
8 #include <string.h> | |
319 | 9 #include <stddef.h> |
307 | 10 #include <avr/io.h> |
11 #include <avr/interrupt.h> | |
12 #include <avr/sleep.h> | |
313 | 13 #include <util/delay.h> |
312 | 14 #include <avr/pgmspace.h> |
319 | 15 #include <avr/eeprom.h> |
306 | 16 #include <util/crc16.h> |
17 | |
318 | 18 // for DWORD of get_fattime() |
309 | 19 #include "integer.h" |
318 | 20 |
21 #include "simple_ds18b20.h" | |
319 | 22 #include "onewire.h" |
309 | 23 |
306 | 24 // configuration params |
25 // - measurement interval | |
26 // - transmit interval | |
27 // - bluetooth params | |
28 // - number of sensors (and range?) | |
29 | |
307 | 30 // 1 second. we have 1024 prescaler, 32768 crystal. |
308 | 31 #define SLEEP_COMPARE 32 |
320 | 32 #define MEASURE_WAKE 10 |
310
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
33 |
319 | 34 #define VALUE_NOSENSOR -9000 |
35 #define VALUE_BROKEN -8000 | |
36 | |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
326
diff
changeset
|
37 // limited to uint16_t for now |
310
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
38 #define COMMS_WAKE 3600 |
331
5de3fc71ce48
- Make the python work on openwrt
Matt Johnston <matt@ucc.asn.au>
parents:
330
diff
changeset
|
39 #define WAKE_SECS 250 // XXX testing |
308 | 40 |
316 | 41 #define BAUD 19200 |
313 | 42 #define UBRR ((F_CPU)/8/(BAUD)-1) |
43 | |
44 #define PORT_LED PORTC | |
45 #define DDR_LED DDRC | |
46 #define PIN_LED PC4 | |
307 | 47 |
321
df7384336798
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
320
diff
changeset
|
48 #define PORT_SHDN PORTD |
df7384336798
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
320
diff
changeset
|
49 #define DDR_SHDN DDRD |
df7384336798
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
320
diff
changeset
|
50 #define PIN_SHDN PD7 |
df7384336798
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
320
diff
changeset
|
51 |
319 | 52 #define NUM_MEASUREMENTS 100 |
53 #define MAX_SENSORS 5 | |
312 | 54 |
321
df7384336798
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
320
diff
changeset
|
55 // fixed at 8, have a shorter name |
df7384336798
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
320
diff
changeset
|
56 #define ID_LEN OW_ROMCODE_SIZE |
df7384336798
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
320
diff
changeset
|
57 |
316 | 58 int uart_putchar(char c, FILE *stream); |
320 | 59 static void long_delay(int ms); |
330
7ac6b8846eea
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
328
diff
changeset
|
60 static void blink(); |
320 | 61 |
306 | 62 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, |
63 _FDEV_SETUP_WRITE); | |
64 | |
326
f6b5941b4c34
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
325
diff
changeset
|
65 uint16_t crc_out; |
f6b5941b4c34
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
325
diff
changeset
|
66 static FILE _crc_stdout = FDEV_SETUP_STREAM(uart_putchar, NULL, |
f6b5941b4c34
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
325
diff
changeset
|
67 _FDEV_SETUP_WRITE); |
f6b5941b4c34
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
325
diff
changeset
|
68 // convenience |
f6b5941b4c34
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
325
diff
changeset
|
69 static FILE *crc_stdout = &_crc_stdout; |
f6b5941b4c34
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
325
diff
changeset
|
70 |
323 | 71 static uint16_t n_measurements; |
319 | 72 // stored as decidegrees |
320 | 73 static int16_t measurements[NUM_MEASUREMENTS][MAX_SENSORS]; |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
326
diff
changeset
|
74 static uint32_t first_measurement_clock; |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
326
diff
changeset
|
75 // last_measurement_clock is redundant but checks that we're not missing |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
326
diff
changeset
|
76 // samples |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
326
diff
changeset
|
77 static uint32_t last_measurement_clock; |
306 | 78 |
310
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
79 // boolean flags |
323 | 80 static uint8_t need_measurement; |
81 static uint8_t need_comms; | |
307 | 82 |
322
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
83 // counts down from WAKE_SECS to 0, goes to deep sleep when hits 0 |
323 | 84 static uint8_t comms_timeout; |
307 | 85 |
323 | 86 static uint8_t readpos; |
306 | 87 static char readbuf[30]; |
333
298e502fdcd4
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
332
diff
changeset
|
88 static uint8_t have_cmd; |
306 | 89 |
323 | 90 static uint8_t measure_count; |
91 static uint16_t comms_count; | |
92 | |
93 static uint32_t clock_epoch; | |
310
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
94 |
319 | 95 // thanks to http://projectgus.com/2010/07/eeprom-access-with-arduino/ |
96 #define eeprom_read_to(dst_p, eeprom_field, dst_size) eeprom_read_block((dst_p), (void *)offsetof(struct __eeprom_data, eeprom_field), (dst_size)) | |
97 #define eeprom_read(dst, eeprom_field) eeprom_read_to((&dst), eeprom_field, sizeof(dst)) | |
98 #define eeprom_write_from(src_p, eeprom_field, src_size) eeprom_write_block((src_p), (void *)offsetof(struct __eeprom_data, eeprom_field), (src_size)) | |
99 #define eeprom_write(src, eeprom_field) { eeprom_write_from(&src, eeprom_field, sizeof(src)); } | |
100 | |
101 #define EXPECT_MAGIC 0x67c9 | |
102 | |
103 struct __attribute__ ((__packed__)) __eeprom_data { | |
104 uint16_t magic; | |
105 uint8_t n_sensors; | |
321
df7384336798
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
320
diff
changeset
|
106 uint8_t sensor_id[MAX_SENSORS][ID_LEN]; |
319 | 107 }; |
108 | |
314 | 109 #define DEBUG(str) printf_P(PSTR(str)) |
110 | |
310
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
111 static void deep_sleep(); |
307 | 112 |
322
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
113 // Very first setup |
324 | 114 static void |
322
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
115 setup_chip() |
324 | 116 { |
322
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
117 // Set clock to 2mhz |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
118 cli(); |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
119 CLKPR = _BV(CLKPCE); |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
120 // divide by 4 |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
121 CLKPR = _BV(CLKPS1); |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
122 sei(); |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
123 |
325 | 124 // 3.3v power for bluetooth and SD |
322
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
125 DDR_LED |= _BV(PIN_LED); |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
126 DDR_SHDN |= _BV(PIN_SHDN); |
325 | 127 |
324 | 128 // INT0 setup |
129 EIMSK = _BV(INT0); | |
130 // set pullup | |
131 PORTD |= _BV(PD2); | |
132 } | |
133 | |
322
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
134 static void |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
135 set_aux_power(uint8_t on) |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
136 { |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
137 if (on) |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
138 { |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
139 PORT_SHDN &= ~_BV(PIN_SHDN); |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
140 } |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
141 else |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
142 { |
330
7ac6b8846eea
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
328
diff
changeset
|
143 //PORT_SHDN |= _BV(PIN_SHDN); |
322
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
144 } |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
145 } |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
146 |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
147 static void |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
148 setup_tick_counter() |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
149 { |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
150 // set up counter2. |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
151 // COM21 COM20 Set OC2 on Compare Match (p116) |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
152 // WGM21 Clear counter on compare |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
153 //TCCR2A = _BV(COM2A1) | _BV(COM2A0) | _BV(WGM21); |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
154 // toggle on match |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
155 TCCR2A = _BV(COM2A0); |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
156 // CS22 CS21 CS20 clk/1024 |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
157 TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20); |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
158 // set async mode |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
159 ASSR |= _BV(AS2); |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
160 TCNT2 = 0; |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
161 OCR2A = SLEEP_COMPARE; |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
162 // interrupt |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
163 TIMSK2 = _BV(OCIE2A); |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
164 } |
324 | 165 |
306 | 166 static void |
313 | 167 uart_on() |
306 | 168 { |
314 | 169 // Power reduction register |
170 //PRR &= ~_BV(PRUSART0); | |
171 | |
322
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
172 // All of this needs to be done each time after turning off the PRR |
306 | 173 // baud rate |
313 | 174 UBRR0H = (unsigned char)(UBRR >> 8); |
175 UBRR0L = (unsigned char)UBRR; | |
176 // set 2x clock, improves accuracy of UBRR | |
177 UCSR0A |= _BV(U2X0); | |
312 | 178 UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0); |
306 | 179 //8N1 |
313 | 180 UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); |
312 | 181 } |
182 | |
183 static void | |
184 uart_off() | |
185 { | |
330
7ac6b8846eea
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
328
diff
changeset
|
186 #if 0 |
312 | 187 // Turn of interrupts and disable tx/rx |
188 UCSR0B = 0; | |
189 | |
190 // Power reduction register | |
314 | 191 //PRR |= _BV(PRUSART0); |
330
7ac6b8846eea
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
328
diff
changeset
|
192 #endif |
306 | 193 } |
194 | |
316 | 195 int |
306 | 196 uart_putchar(char c, FILE *stream) |
197 { | |
326
f6b5941b4c34
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
325
diff
changeset
|
198 // XXX could perhaps sleep in the loop for power. |
314 | 199 if (c == '\n') |
200 { | |
316 | 201 loop_until_bit_is_set(UCSR0A, UDRE0); |
326
f6b5941b4c34
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
325
diff
changeset
|
202 UDR0 = '\r'; |
314 | 203 } |
308 | 204 loop_until_bit_is_set(UCSR0A, UDRE0); |
205 UDR0 = c; | |
326
f6b5941b4c34
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
325
diff
changeset
|
206 if (stream == crc_stdout) |
f6b5941b4c34
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
325
diff
changeset
|
207 { |
332
05c1249da994
- Move crc16 to utils and fix it
Matt Johnston <matt@ucc.asn.au>
parents:
331
diff
changeset
|
208 crc_out = _crc_ccitt_update(crc_out, c); |
326
f6b5941b4c34
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
325
diff
changeset
|
209 } |
316 | 210 if (c == '\r') |
211 { | |
212 loop_until_bit_is_set(UCSR0A, UDRE0); | |
326
f6b5941b4c34
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
325
diff
changeset
|
213 UDR0 = '\n'; |
f6b5941b4c34
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
325
diff
changeset
|
214 if (stream == crc_stdout) |
f6b5941b4c34
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
325
diff
changeset
|
215 { |
f6b5941b4c34
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
325
diff
changeset
|
216 crc_out = _crc_ccitt_update(crc_out, '\n'); |
f6b5941b4c34
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
325
diff
changeset
|
217 } |
316 | 218 } |
306 | 219 return 0; |
220 } | |
221 | |
222 static void | |
223 cmd_fetch() | |
224 { | |
326
f6b5941b4c34
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
325
diff
changeset
|
225 crc_out = 0; |
321
df7384336798
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
320
diff
changeset
|
226 uint8_t n_sensors; |
df7384336798
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
320
diff
changeset
|
227 eeprom_read(n_sensors, n_sensors); |
319 | 228 |
326
f6b5941b4c34
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
325
diff
changeset
|
229 fprintf_P(crc_stdout, PSTR("START\n")); |
333
298e502fdcd4
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
332
diff
changeset
|
230 fprintf_P(crc_stdout, PSTR("now=%lu\n" |
298e502fdcd4
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
332
diff
changeset
|
231 "time_step=%lu\n" |
298e502fdcd4
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
332
diff
changeset
|
232 "first_time=%lu\n" |
298e502fdcd4
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
332
diff
changeset
|
233 "last_time=%lu\n"), |
298e502fdcd4
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
332
diff
changeset
|
234 clock_epoch, |
298e502fdcd4
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
332
diff
changeset
|
235 MEASURE_WAKE, |
298e502fdcd4
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
332
diff
changeset
|
236 first_measurement_clock, |
298e502fdcd4
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
332
diff
changeset
|
237 last_measurement_clock); |
298e502fdcd4
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
332
diff
changeset
|
238 fprintf_P(crc_stdout, PSTR("sensors=%u\n"), n_sensors); |
321
df7384336798
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
320
diff
changeset
|
239 for (uint8_t s = 0; s < n_sensors; s++) |
df7384336798
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
320
diff
changeset
|
240 { |
df7384336798
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
320
diff
changeset
|
241 uint8_t id[ID_LEN]; |
333
298e502fdcd4
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
332
diff
changeset
|
242 fprintf_P(crc_stdout, PSTR("sensor_id%u="), s); |
321
df7384336798
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
320
diff
changeset
|
243 eeprom_read_to(id, sensor_id[s], ID_LEN); |
326
f6b5941b4c34
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
325
diff
changeset
|
244 printhex(id, ID_LEN, crc_stdout); |
f6b5941b4c34
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
325
diff
changeset
|
245 fputc('\n', crc_stdout); |
321
df7384336798
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
320
diff
changeset
|
246 } |
333
298e502fdcd4
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
332
diff
changeset
|
247 fprintf_P(crc_stdout, PSTR("measurements=%u\n"), n_measurements); |
320 | 248 for (uint16_t n = 0; n < n_measurements; n++) |
306 | 249 { |
333
298e502fdcd4
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
332
diff
changeset
|
250 fprintf_P(crc_stdout, PSTR("meas%u="), n); |
321
df7384336798
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
320
diff
changeset
|
251 for (uint8_t s = 0; s < n_sensors; s++) |
319 | 252 { |
333
298e502fdcd4
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
332
diff
changeset
|
253 fprintf_P(crc_stdout, PSTR(" %u"), measurements[n][s]); |
319 | 254 } |
326
f6b5941b4c34
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
325
diff
changeset
|
255 fputc('\n', crc_stdout); |
306 | 256 } |
326
f6b5941b4c34
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
325
diff
changeset
|
257 fprintf_P(crc_stdout, PSTR("END\n")); |
331
5de3fc71ce48
- Make the python work on openwrt
Matt Johnston <matt@ucc.asn.au>
parents:
330
diff
changeset
|
258 fprintf_P(stdout, PSTR("CRC=%hu\n"), crc_out); |
306 | 259 } |
260 | |
261 static void | |
262 cmd_clear() | |
263 { | |
313 | 264 n_measurements = 0; |
338 | 265 printf_P(PSTR("cleared\n")); |
306 | 266 } |
267 | |
268 static void | |
269 cmd_btoff() | |
270 { | |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
326
diff
changeset
|
271 uint16_t next_wake = COMMS_WAKE - comms_count; |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
326
diff
changeset
|
272 printf_P(PSTR("off:%d\n"), next_wake); |
313 | 273 _delay_ms(50); |
322
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
274 comms_timeout = 0; |
306 | 275 } |
276 | |
277 static void | |
318 | 278 cmd_measure() |
279 { | |
280 printf_P(PSTR("Measuring\n")); | |
281 need_measurement = 1; | |
282 } | |
283 | |
284 static void | |
285 cmd_sensors() | |
286 { | |
287 uint8_t ret = simple_ds18b20_start_meas(NULL); | |
320 | 288 printf_P(PSTR("All sensors, ret %d, waiting...\n"), ret); |
289 long_delay(DS18B20_TCONV_12BIT); | |
318 | 290 simple_ds18b20_read_all(); |
291 } | |
292 | |
322
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
293 #if 0 |
319 | 294 // 0 on success |
295 static uint8_t | |
296 get_hex_string(const char *hex, uint8_t *out, uint8_t size) | |
297 { | |
298 uint8_t upper; | |
299 uint8_t o; | |
300 for (uint8_t i = 0, z = 0; o < size; i++) | |
301 { | |
302 uint8_t h = hex[i]; | |
303 if (h >= 'A' && h <= 'F') | |
304 { | |
305 // lower case | |
306 h += 0x20; | |
307 } | |
308 uint8_t nibble; | |
309 if (h >= '0' && h <= '9') | |
310 { | |
311 nibble = h - '0'; | |
312 } | |
313 else if (h >= 'a' && h <= 'f') | |
314 { | |
315 nibble = 10 + h - 'a'; | |
316 } | |
317 else if (h == ' ' || h == ':') | |
318 { | |
319 continue; | |
320 } | |
321 else | |
322 { | |
323 printf_P(PSTR("Bad hex 0x%x '%c'\n"), hex[i], hex[i]); | |
324 return 1; | |
325 } | |
326 | |
327 if (z % 2 == 0) | |
328 { | |
329 upper = nibble << 4; | |
330 } | |
331 else | |
332 { | |
333 out[o] = upper | nibble; | |
334 o++; | |
335 } | |
336 | |
337 z++; | |
338 } | |
339 | |
340 if (o != size) | |
341 { | |
342 printf_P(PSTR("Short hex\n")); | |
343 return 1; | |
344 } | |
345 return 0; | |
346 } | |
322
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
347 #endif |
319 | 348 |
349 static void | |
350 add_sensor(uint8_t *id) | |
351 { | |
352 uint8_t n; | |
353 eeprom_read(n, n_sensors); | |
354 if (n < MAX_SENSORS) | |
355 { | |
356 cli(); | |
321
df7384336798
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
320
diff
changeset
|
357 eeprom_write_from(id, sensor_id[n], ID_LEN); |
319 | 358 n++; |
359 eeprom_write(n, n_sensors); | |
360 sei(); | |
361 printf_P(PSTR("Added sensor %d : "), n); | |
326
f6b5941b4c34
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
325
diff
changeset
|
362 printhex(id, ID_LEN, stdout); |
319 | 363 putchar('\n'); |
364 } | |
365 else | |
366 { | |
367 printf_P(PSTR("Too many sensors\n")); | |
368 } | |
369 } | |
370 | |
371 static void | |
372 cmd_add_all() | |
373 { | |
374 uint8_t id[OW_ROMCODE_SIZE]; | |
375 printf_P("Adding all\n"); | |
376 ow_reset(); | |
377 for( uint8_t diff = OW_SEARCH_FIRST; diff != OW_LAST_DEVICE; ) | |
378 { | |
379 diff = ow_rom_search( diff, &id[0] ); | |
380 if( diff == OW_PRESENCE_ERR ) { | |
381 printf_P( PSTR("No Sensor found\r") ); | |
382 return; | |
383 } | |
384 | |
385 if( diff == OW_DATA_ERR ) { | |
386 printf_P( PSTR("Bus Error\r") ); | |
387 return; | |
388 } | |
389 add_sensor(id); | |
390 } | |
391 } | |
392 | |
393 static void | |
394 cmd_init() | |
395 { | |
396 printf_P(PSTR("Resetting sensor list\n")); | |
397 uint8_t zero = 0; | |
398 cli(); | |
399 eeprom_write(zero, n_sensors); | |
400 sei(); | |
401 printf_P(PSTR("Done.\n")); | |
402 } | |
403 | |
404 static void | |
405 check_first_startup() | |
406 { | |
407 uint16_t magic; | |
408 eeprom_read(magic, magic); | |
409 if (magic != EXPECT_MAGIC) | |
410 { | |
411 printf_P(PSTR("First boot, looking for sensors...\n")); | |
322
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
412 // in case of power fumbles don't want to reset during eeprom write, |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
413 long_delay(2); |
319 | 414 cmd_init(); |
415 cmd_add_all(); | |
320 | 416 cli(); |
417 magic = EXPECT_MAGIC; | |
418 eeprom_write(magic, magic); | |
419 sei(); | |
319 | 420 } |
421 } | |
422 | |
318 | 423 static void |
306 | 424 read_handler() |
425 { | |
312 | 426 if (strcmp_P(readbuf, PSTR("fetch")) == 0) |
306 | 427 { |
428 cmd_fetch(); | |
429 } | |
312 | 430 else if (strcmp_P(readbuf, PSTR("clear")) == 0) |
306 | 431 { |
432 cmd_clear(); | |
433 } | |
312 | 434 else if (strcmp_P(readbuf, PSTR("btoff")) == 0) |
306 | 435 { |
436 cmd_btoff(); | |
437 } | |
318 | 438 else if (strcmp_P(readbuf, PSTR("measure")) == 0) |
439 { | |
440 cmd_measure(); | |
441 } | |
442 else if (strcmp_P(readbuf, PSTR("sensors")) == 0) | |
443 { | |
444 cmd_sensors(); | |
445 } | |
319 | 446 else if (strcmp_P(readbuf, PSTR("addall"))== 0) |
447 { | |
448 cmd_add_all(); | |
449 } | |
450 else if (strcmp_P(readbuf, PSTR("init")) == 0) | |
451 { | |
452 cmd_init(); | |
453 } | |
306 | 454 else |
455 { | |
312 | 456 printf_P(PSTR("Bad command\n")); |
306 | 457 } |
458 } | |
459 | |
326
f6b5941b4c34
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
325
diff
changeset
|
460 ISR(INT0_vect) |
324 | 461 { |
462 need_comms = 1; | |
330
7ac6b8846eea
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
328
diff
changeset
|
463 blink(); |
7ac6b8846eea
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
328
diff
changeset
|
464 _delay_ms(100); |
7ac6b8846eea
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
328
diff
changeset
|
465 blink(); |
7ac6b8846eea
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
328
diff
changeset
|
466 _delay_ms(100); |
7ac6b8846eea
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
328
diff
changeset
|
467 blink(); |
7ac6b8846eea
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
328
diff
changeset
|
468 _delay_ms(100); |
7ac6b8846eea
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
328
diff
changeset
|
469 blink(); |
7ac6b8846eea
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
328
diff
changeset
|
470 _delay_ms(100); |
7ac6b8846eea
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
328
diff
changeset
|
471 blink(); |
7ac6b8846eea
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
328
diff
changeset
|
472 _delay_ms(100); |
7ac6b8846eea
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
328
diff
changeset
|
473 blink(); |
7ac6b8846eea
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
328
diff
changeset
|
474 _delay_ms(100); |
7ac6b8846eea
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
328
diff
changeset
|
475 blink(); |
7ac6b8846eea
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
328
diff
changeset
|
476 _delay_ms(100); |
324 | 477 } |
478 | |
479 | |
308 | 480 ISR(USART_RX_vect) |
306 | 481 { |
308 | 482 char c = UDR0; |
318 | 483 uart_putchar(c, NULL); |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
326
diff
changeset
|
484 if (c == '\r' || c == '\n') |
306 | 485 { |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
326
diff
changeset
|
486 if (readpos > 0) |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
326
diff
changeset
|
487 { |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
326
diff
changeset
|
488 readbuf[readpos] = '\0'; |
333
298e502fdcd4
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
332
diff
changeset
|
489 have_cmd = 1; |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
326
diff
changeset
|
490 readpos = 0; |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
326
diff
changeset
|
491 } |
306 | 492 } |
493 else | |
494 { | |
495 readbuf[readpos] = c; | |
496 readpos++; | |
497 if (readpos >= sizeof(readbuf)) | |
498 { | |
499 readpos = 0; | |
500 } | |
501 } | |
502 } | |
503 | |
308 | 504 ISR(TIMER2_COMPA_vect) |
307 | 505 { |
320 | 506 TCNT2 = 0; |
310
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
507 measure_count ++; |
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
508 comms_count ++; |
322
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
509 |
323 | 510 clock_epoch ++; |
511 | |
322
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
512 if (comms_timeout != 0) |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
513 { |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
514 comms_timeout--; |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
515 } |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
516 |
320 | 517 if (measure_count >= MEASURE_WAKE) |
307 | 518 { |
310
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
519 measure_count = 0; |
307 | 520 need_measurement = 1; |
521 } | |
310
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
522 |
322
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
523 if (comms_count >= COMMS_WAKE) |
310
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
524 { |
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
525 comms_count = 0; |
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
526 need_comms = 1; |
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
527 } |
322
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
528 |
307 | 529 } |
530 | |
309 | 531 DWORD get_fattime (void) |
532 { | |
533 return 0; | |
534 } | |
535 | |
307 | 536 static void |
537 deep_sleep() | |
538 { | |
539 // p119 of manual | |
308 | 540 OCR2A = SLEEP_COMPARE; |
541 loop_until_bit_is_clear(ASSR, OCR2AUB); | |
307 | 542 |
543 set_sleep_mode(SLEEP_MODE_PWR_SAVE); | |
544 sleep_mode(); | |
545 } | |
546 | |
547 static void | |
310
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
548 idle_sleep() |
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
549 { |
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
550 set_sleep_mode(SLEEP_MODE_IDLE); |
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
551 sleep_mode(); |
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
552 } |
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
553 |
320 | 554 #if 0 |
555 // untested | |
312 | 556 static void |
557 do_adc_335() | |
558 { | |
314 | 559 //PRR &= ~_BV(PRADC); |
312 | 560 |
561 ADMUX = _BV(ADLAR); | |
562 | |
563 // ADPS2 = /16 prescaler, 62khz at 1mhz clock | |
564 ADCSRA = _BV(ADEN) | _BV(ADPS2); | |
565 | |
566 // measure value | |
567 ADCSRA |= _BV(ADSC); | |
568 loop_until_bit_is_clear(ADCSRA, ADSC); | |
569 uint8_t low = ADCL; | |
570 uint8_t high = ADCH; | |
571 uint16_t f_measure = low + (high << 8); | |
572 | |
573 // set to measure 1.1 reference | |
574 ADMUX = _BV(ADLAR) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); | |
575 ADCSRA |= _BV(ADSC); | |
576 loop_until_bit_is_clear(ADCSRA, ADSC); | |
577 uint8_t low_11 = ADCL; | |
578 uint8_t high_11 = ADCH; | |
579 uint16_t f_11 = low_11 + (high_11 << 8); | |
580 | |
581 float res_volts = 1.1 * f_measure / f_11; | |
582 | |
583 // 10mV/degree | |
584 // scale to 1/5 degree units above 0C | |
585 int temp = (res_volts - 2.73) * 500; | |
319 | 586 // XXX fixme |
587 //measurements[n_measurements] = temp; | |
312 | 588 // XXX something if it hits the limit |
589 | |
590 // measure AVR internal temperature against 1.1 ref. | |
591 ADMUX = _BV(ADLAR) | _BV(MUX3) | _BV(REFS1) | _BV(REFS0); | |
592 ADCSRA |= _BV(ADSC); | |
593 loop_until_bit_is_clear(ADCSRA, ADSC); | |
594 uint16_t res_internal = ADCL; | |
595 res_internal |= ADCH << 8; | |
596 | |
597 float internal_volts = res_internal * (1.1 / 1024.0); | |
598 | |
599 // 1mV/degree | |
600 int internal_temp = (internal_volts - 2.73) * 5000; | |
319 | 601 // XXX fixme |
602 //internal_measurements[n_measurements] = internal_temp; | |
312 | 603 |
604 printf_P("measure %d: external %d, internal %d, 1.1 %d\n", | |
605 n_measurements, temp, internal_temp, f_11); | |
606 | |
607 n_measurements++; | |
314 | 608 //PRR |= _BV(PRADC); |
312 | 609 } |
320 | 610 #endif |
312 | 611 |
310
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
612 static void |
307 | 613 do_measurement() |
614 { | |
319 | 615 uint8_t n_sensors; |
320 | 616 printf("do_measurement\n"); |
319 | 617 eeprom_read(n_sensors, n_sensors); |
320 | 618 printf("do_measurement sensors %d\n", n_sensors); |
319 | 619 |
620 uint8_t ret = simple_ds18b20_start_meas(NULL); | |
320 | 621 printf_P(PSTR("Read all sensors, ret %d, waiting...\n"), ret); |
319 | 622 _delay_ms(DS18B20_TCONV_12BIT); |
623 | |
624 if (n_measurements == NUM_MEASUREMENTS) | |
625 { | |
320 | 626 printf_P(PSTR("Measurements .overflow\n")); |
319 | 627 n_measurements = 0; |
628 } | |
312 | 629 |
320 | 630 for (uint8_t s = 0; s < MAX_SENSORS; s++) |
319 | 631 { |
632 int16_t decicelsius; | |
320 | 633 if (s >= n_sensors) |
319 | 634 { |
635 decicelsius = VALUE_NOSENSOR; | |
636 } | |
637 else | |
638 { | |
321
df7384336798
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
320
diff
changeset
|
639 uint8_t id[ID_LEN]; |
df7384336798
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
320
diff
changeset
|
640 eeprom_read_to(id, sensor_id[s], ID_LEN); |
319 | 641 |
642 uint8_t ret = simple_ds18b20_read_decicelsius(id, &decicelsius); | |
643 if (ret != DS18X20_OK) | |
644 { | |
645 decicelsius = VALUE_BROKEN; | |
646 } | |
647 } | |
320 | 648 measurements[n_measurements][s] = decicelsius; |
319 | 649 } |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
326
diff
changeset
|
650 |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
326
diff
changeset
|
651 if (n_measurements == 0) |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
326
diff
changeset
|
652 { |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
326
diff
changeset
|
653 first_measurement_clock = clock_epoch; |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
326
diff
changeset
|
654 } |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
326
diff
changeset
|
655 last_measurement_clock = clock_epoch; |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
326
diff
changeset
|
656 |
320 | 657 n_measurements++; |
318 | 658 //do_adc_335(); |
307 | 659 } |
660 | |
310
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
661 static void |
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
662 do_comms() |
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
663 { |
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
664 // turn on bluetooth |
330
7ac6b8846eea
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
328
diff
changeset
|
665 set_aux_power(1); |
313 | 666 uart_on(); |
310
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
667 |
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
668 // write sd card here? same 3.3v regulator... |
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
669 |
322
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
670 for (comms_timeout = WAKE_SECS; comms_timeout > 0; ) |
310
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
671 { |
312 | 672 if (need_measurement) |
673 { | |
320 | 674 need_measurement = 0; |
312 | 675 do_measurement(); |
676 } | |
677 | |
333
298e502fdcd4
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
332
diff
changeset
|
678 if (have_cmd) |
298e502fdcd4
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
332
diff
changeset
|
679 { |
298e502fdcd4
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
332
diff
changeset
|
680 have_cmd = 0; |
298e502fdcd4
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
332
diff
changeset
|
681 read_handler(); |
298e502fdcd4
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
332
diff
changeset
|
682 } |
298e502fdcd4
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
332
diff
changeset
|
683 |
322
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
684 // wait for commands from the master |
310
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
685 idle_sleep(); |
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
686 } |
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
687 |
312 | 688 uart_off(); |
322
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
689 set_aux_power(0); |
310
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
690 } |
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
691 |
313 | 692 static void |
693 blink() | |
694 { | |
695 PORT_LED &= ~_BV(PIN_LED); | |
315 | 696 _delay_ms(1); |
313 | 697 PORT_LED |= _BV(PIN_LED); |
698 } | |
699 | |
700 static void | |
701 long_delay(int ms) | |
702 { | |
703 int iter = ms / 100; | |
704 | |
705 for (int i = 0; i < iter; i++) | |
706 { | |
707 _delay_ms(100); | |
708 } | |
709 } | |
710 | |
314 | 711 ISR(BADISR_vect) |
712 { | |
320 | 713 //uart_on(); |
314 | 714 printf_P(PSTR("Bad interrupt\n")); |
715 } | |
716 | |
315 | 717 |
306 | 718 int main(void) |
719 { | |
322
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
720 setup_chip(); |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
721 blink(); |
315 | 722 |
322
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
723 set_aux_power(0); |
313 | 724 |
312 | 725 stdout = &mystdout; |
313 | 726 uart_on(); |
727 | |
322
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
728 printf(PSTR("Started.\n")); |
319 | 729 |
730 check_first_startup(); | |
731 | |
314 | 732 uart_off(); |
306 | 733 |
312 | 734 // turn off everything except timer2 |
314 | 735 //PRR = _BV(PRTWI) | _BV(PRTIM0) | _BV(PRTIM1) | _BV(PRSPI) | _BV(PRUSART0) | _BV(PRADC); |
736 | |
737 // for testing | |
738 uart_on(); | |
739 | |
322
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
740 setup_tick_counter(); |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
741 |
318 | 742 sei(); |
314 | 743 |
331
5de3fc71ce48
- Make the python work on openwrt
Matt Johnston <matt@ucc.asn.au>
parents:
330
diff
changeset
|
744 need_comms = 1; |
5de3fc71ce48
- Make the python work on openwrt
Matt Johnston <matt@ucc.asn.au>
parents:
330
diff
changeset
|
745 |
322
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
746 #if 0 |
312 | 747 for (;;) |
748 { | |
318 | 749 do_comms(); |
312 | 750 } |
322
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
751 #endif |
318 | 752 |
322
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
753 for(;;) |
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
754 { |
307 | 755 if (need_measurement) |
756 { | |
320 | 757 need_measurement = 0; |
307 | 758 do_measurement(); |
310
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
759 continue; |
307 | 760 } |
310
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
761 |
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
762 if (need_comms) |
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
763 { |
322
840f51824254
untested, add comms timeout code
Matt Johnston <matt@ucc.asn.au>
parents:
321
diff
changeset
|
764 need_comms = 0; |
310
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
765 do_comms(); |
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
766 continue; |
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
767 } |
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
768 |
0a64532c1de1
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
309
diff
changeset
|
769 deep_sleep(); |
315 | 770 blink(); |
316 | 771 printf("."); |
306 | 772 } |
318 | 773 |
306 | 774 return 0; /* never reached */ |
775 } |