Mercurial > templog
comparison main.c @ 4:54d369e3d689
Fill out more main.c structure
Add onewire files
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 09 May 2012 00:22:28 +0800 |
parents | 888be1b234b6 |
children | 011160e5b1ce |
comparison
equal
deleted
inserted
replaced
3:888be1b234b6 | 4:54d369e3d689 |
---|---|
19 // - bluetooth params | 19 // - bluetooth params |
20 // - number of sensors (and range?) | 20 // - number of sensors (and range?) |
21 | 21 |
22 // 1 second. we have 1024 prescaler, 32768 crystal. | 22 // 1 second. we have 1024 prescaler, 32768 crystal. |
23 #define SLEEP_COMPARE 32 | 23 #define SLEEP_COMPARE 32 |
24 #define SECONDS_WAKE 60 | 24 #define MEASURE_WAKE 60 |
25 | |
26 #define COMMS_WAKE 3600 | |
25 | 27 |
26 #define BAUD 9600 | 28 #define BAUD 9600 |
27 #define UBRR ((F_CPU)/16/(BAUD)-1) | 29 #define UBRR ((F_CPU)/16/(BAUD)-1) |
28 | 30 |
29 static int uart_putchar(char c, FILE *stream); | 31 static int uart_putchar(char c, FILE *stream); |
31 _FDEV_SETUP_WRITE); | 33 _FDEV_SETUP_WRITE); |
32 | 34 |
33 static uint8_t n_measurements; | 35 static uint8_t n_measurements; |
34 static uint8_t measurements[500]; | 36 static uint8_t measurements[500]; |
35 | 37 |
38 // boolean flags | |
36 static uint8_t need_measurement; | 39 static uint8_t need_measurement; |
40 static uint8_t need_comms; | |
41 static uint8_t comms_done; | |
37 | 42 |
38 static uint8_t readpos; | 43 static uint8_t readpos; |
39 static char readbuf[30]; | 44 static char readbuf[30]; |
40 | 45 |
41 static uint8_t sec_count; | 46 static uint8_t sec_count; |
47 static uint16_t bt_count; | |
48 | |
49 static void deep_sleep(); | |
42 | 50 |
43 static void | 51 static void |
44 uart_init(unsigned int ubrr) | 52 uart_init(unsigned int ubrr) |
45 { | 53 { |
46 // baud rate | 54 // baud rate |
80 } | 88 } |
81 | 89 |
82 static void | 90 static void |
83 cmd_btoff() | 91 cmd_btoff() |
84 { | 92 { |
93 comms_done = 1; | |
85 } | 94 } |
86 | 95 |
87 static void | 96 static void |
88 read_handler() | 97 read_handler() |
89 { | 98 { |
125 } | 134 } |
126 } | 135 } |
127 | 136 |
128 ISR(TIMER2_COMPA_vect) | 137 ISR(TIMER2_COMPA_vect) |
129 { | 138 { |
130 sec_count ++; | 139 measure_count ++; |
131 if (sec_count == SECONDS_WAKE) | 140 comms_count ++; |
132 { | 141 if (measure_count == MEASURE_WAKE) |
133 sec_count = 0; | 142 { |
143 measure_count = 0; | |
134 need_measurement = 1; | 144 need_measurement = 1; |
135 } | 145 } |
146 | |
147 if (comms_count == COMMS_WAKE) | |
148 { | |
149 comms_count = 0; | |
150 need_comms = 1; | |
151 } | |
136 } | 152 } |
137 | 153 |
138 DWORD get_fattime (void) | 154 DWORD get_fattime (void) |
139 { | 155 { |
140 return 0; | 156 return 0; |
150 set_sleep_mode(SLEEP_MODE_PWR_SAVE); | 166 set_sleep_mode(SLEEP_MODE_PWR_SAVE); |
151 sleep_mode(); | 167 sleep_mode(); |
152 } | 168 } |
153 | 169 |
154 static void | 170 static void |
171 idle_sleep() | |
172 { | |
173 set_sleep_mode(SLEEP_MODE_IDLE); | |
174 sleep_mode(); | |
175 } | |
176 | |
177 static void | |
155 do_measurement() | 178 do_measurement() |
156 { | 179 { |
157 need_measurement = 0; | 180 need_measurement = 0; |
181 } | |
182 | |
183 static void | |
184 do_comms() | |
185 { | |
186 need_comms = 0; | |
187 | |
188 // turn on bluetooth | |
189 // turn on serial (interrupts) | |
190 | |
191 // write sd card here? same 3.3v regulator... | |
192 | |
193 comms_done = 0; | |
194 for (;;) | |
195 { | |
196 if (comms_done) | |
197 { | |
198 break; | |
199 } | |
200 idle_sleep(); | |
201 } | |
202 | |
203 // turn off bluetooth | |
204 // turn off serial (interrupts) | |
158 } | 205 } |
159 | 206 |
160 int main(void) | 207 int main(void) |
161 { | 208 { |
162 uart_init(UBRR); | 209 uart_init(UBRR); |
175 for(;;){ | 222 for(;;){ |
176 /* insert your main loop code here */ | 223 /* insert your main loop code here */ |
177 if (need_measurement) | 224 if (need_measurement) |
178 { | 225 { |
179 do_measurement(); | 226 do_measurement(); |
227 continue; | |
180 } | 228 } |
229 | |
230 if (need_comms) | |
231 { | |
232 do_comms(); | |
233 continue; | |
234 } | |
235 | |
236 deep_sleep(); | |
181 } | 237 } |
182 return 0; /* never reached */ | 238 return 0; /* never reached */ |
183 } | 239 } |