comparison main.c @ 15:54b0fda9cba7

Add shutdown handling, print sensors in "fetch" output
author Matt Johnston <matt@ucc.asn.au>
date Sun, 20 May 2012 22:16:56 +0800
parents 426fb44ece3f
children 5075d8c428bd bf733e8e8cf0
comparison
equal deleted inserted replaced
14:426fb44ece3f 15:54b0fda9cba7
41 41
42 #define PORT_LED PORTC 42 #define PORT_LED PORTC
43 #define DDR_LED DDRC 43 #define DDR_LED DDRC
44 #define PIN_LED PC4 44 #define PIN_LED PC4
45 45
46 #define PORT_SHDN PORTD
47 #define DDR_SHDN DDRD
48 #define PIN_SHDN PD7
49
46 #define NUM_MEASUREMENTS 100 50 #define NUM_MEASUREMENTS 100
47 #define MAX_SENSORS 5 51 #define MAX_SENSORS 5
52
53 // fixed at 8, have a shorter name
54 #define ID_LEN OW_ROMCODE_SIZE
48 55
49 int uart_putchar(char c, FILE *stream); 56 int uart_putchar(char c, FILE *stream);
50 static void long_delay(int ms); 57 static void long_delay(int ms);
51 58
52 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, 59 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
76 #define EXPECT_MAGIC 0x67c9 83 #define EXPECT_MAGIC 0x67c9
77 84
78 struct __attribute__ ((__packed__)) __eeprom_data { 85 struct __attribute__ ((__packed__)) __eeprom_data {
79 uint16_t magic; 86 uint16_t magic;
80 uint8_t n_sensors; 87 uint8_t n_sensors;
81 uint8_t sensor_id[MAX_SENSORS][8]; 88 uint8_t sensor_id[MAX_SENSORS][ID_LEN];
82 }; 89 };
83 90
84 #define DEBUG(str) printf_P(PSTR(str)) 91 #define DEBUG(str) printf_P(PSTR(str))
85 92
86 static void deep_sleep(); 93 static void deep_sleep();
132 139
133 static void 140 static void
134 cmd_fetch() 141 cmd_fetch()
135 { 142 {
136 uint16_t crc = 0; 143 uint16_t crc = 0;
137 uint8_t sens; 144 uint8_t n_sensors;
138 eeprom_read(sens, n_sensors); 145 eeprom_read(n_sensors, n_sensors);
139 146
147 printf_P(PSTR("%d sensors\n"), n_measurements);
148 for (uint8_t s = 0; s < n_sensors; s++)
149 {
150 uint8_t id[ID_LEN];
151 printf_P(PSTR("%d : "), s);
152 eeprom_read_to(id, sensor_id[s], ID_LEN);
153 printhex(id, ID_LEN);
154 putchar('\n');
155 for (uint8_t i = 0; i < ID_LEN; i++)
156 {
157 crc = _crc_ccitt_update(crc, id[i]);
158 }
159 }
140 printf_P(PSTR("%d measurements\n"), n_measurements); 160 printf_P(PSTR("%d measurements\n"), n_measurements);
141 for (uint16_t n = 0; n < n_measurements; n++) 161 for (uint16_t n = 0; n < n_measurements; n++)
142 { 162 {
143 printf_P(PSTR("%3d :"), n); 163 printf_P(PSTR("%3d :"), n);
144 for (uint8_t s = 0; s < sens; s++) 164 for (uint8_t s = 0; s < n_sensors; s++)
145 { 165 {
146 printf_P(PSTR(" %6d"), measurements[n][s]); 166 printf_P(PSTR(" %6d"), measurements[n][s]);
147 crc = _crc_ccitt_update(crc, measurements[n][s]); 167 crc = _crc_ccitt_update(crc, measurements[n][s]);
148 } 168 }
149 putchar('\n'); 169 putchar('\n');
161 static void 181 static void
162 cmd_btoff() 182 cmd_btoff()
163 { 183 {
164 printf_P(PSTR("Turning off\n")); 184 printf_P(PSTR("Turning off\n"));
165 _delay_ms(50); 185 _delay_ms(50);
186 PORTD |= _BV(PIN_SHDN);
166 comms_done = 1; 187 comms_done = 1;
167 } 188 }
168 189
169 static void 190 static void
170 cmd_measure() 191 cmd_measure()
242 uint8_t n; 263 uint8_t n;
243 eeprom_read(n, n_sensors); 264 eeprom_read(n, n_sensors);
244 if (n < MAX_SENSORS) 265 if (n < MAX_SENSORS)
245 { 266 {
246 cli(); 267 cli();
247 eeprom_write_from(id, sensor_id[n], 8); 268 eeprom_write_from(id, sensor_id[n], ID_LEN);
248 n++; 269 n++;
249 eeprom_write(n, n_sensors); 270 eeprom_write(n, n_sensors);
250 sei(); 271 sei();
251 printf_P(PSTR("Added sensor %d : "), n); 272 printf_P(PSTR("Added sensor %d : "), n);
252 printhex(id, 8); 273 printhex(id, ID_LEN);
253 putchar('\n'); 274 putchar('\n');
254 } 275 }
255 else 276 else
256 { 277 {
257 printf_P(PSTR("Too many sensors\n")); 278 printf_P(PSTR("Too many sensors\n"));
281 } 302 }
282 303
283 static void 304 static void
284 cmd_add_sensor(const char* hex_addr) 305 cmd_add_sensor(const char* hex_addr)
285 { 306 {
286 uint8_t id[8]; 307 uint8_t id[ID_LEN];
287 uint8_t ret = get_hex_string(hex_addr, id, 8); 308 uint8_t ret = get_hex_string(hex_addr, id, ID_LEN);
288 if (ret) 309 if (ret)
289 { 310 {
290 return; 311 return;
291 } 312 }
292 add_sensor(id); 313 add_sensor(id);
320 sei(); 341 sei();
321 } 342 }
322 } 343 }
323 344
324 static void 345 static void
346 cmd_toggle()
347 {
348 PORT_SHDN ^= _BV(PIN_SHDN);
349 printf_P(PSTR("toggling power 3.3v %d\n"), PORT_SHDN & _BV(PIN_SHDN));
350 }
351
352 static void
325 read_handler() 353 read_handler()
326 { 354 {
327 if (strcmp_P(readbuf, PSTR("fetch")) == 0) 355 if (strcmp_P(readbuf, PSTR("fetch")) == 0)
328 { 356 {
329 cmd_fetch(); 357 cmd_fetch();
341 cmd_measure(); 369 cmd_measure();
342 } 370 }
343 else if (strcmp_P(readbuf, PSTR("sensors")) == 0) 371 else if (strcmp_P(readbuf, PSTR("sensors")) == 0)
344 { 372 {
345 cmd_sensors(); 373 cmd_sensors();
374 }
375 else if (strcmp_P(readbuf, PSTR("toggle")) == 0)
376 {
377 cmd_toggle();
346 } 378 }
347 else if (strncmp_P(readbuf, PSTR("adds "), strlen("adds ")) == 0) 379 else if (strncmp_P(readbuf, PSTR("adds "), strlen("adds ")) == 0)
348 { 380 {
349 cmd_add_sensor(readbuf + strlen("adds ")); 381 cmd_add_sensor(readbuf + strlen("adds "));
350 } 382 }
509 { 541 {
510 decicelsius = VALUE_NOSENSOR; 542 decicelsius = VALUE_NOSENSOR;
511 } 543 }
512 else 544 else
513 { 545 {
514 uint8_t id[8]; 546 uint8_t id[ID_LEN];
515 eeprom_read_to(id, sensor_id[s], 8); 547 eeprom_read_to(id, sensor_id[s], ID_LEN);
516 548
517 uint8_t ret = simple_ds18b20_read_decicelsius(id, &decicelsius); 549 uint8_t ret = simple_ds18b20_read_decicelsius(id, &decicelsius);
518 if (ret != DS18X20_OK) 550 if (ret != DS18X20_OK)
519 { 551 {
520 decicelsius = VALUE_BROKEN; 552 decicelsius = VALUE_BROKEN;
599 int main(void) 631 int main(void)
600 { 632 {
601 set_2mhz(); 633 set_2mhz();
602 634
603 DDR_LED |= _BV(PIN_LED); 635 DDR_LED |= _BV(PIN_LED);
636 DDR_SHDN |= _BV(PIN_SHDN);
604 blink(); 637 blink();
605 638
606 stdout = &mystdout; 639 stdout = &mystdout;
607 uart_on(); 640 uart_on();
608 641
633 TCNT2 = 0; 666 TCNT2 = 0;
634 OCR2A = SLEEP_COMPARE; 667 OCR2A = SLEEP_COMPARE;
635 // interrupt 668 // interrupt
636 TIMSK2 = _BV(OCIE2A); 669 TIMSK2 = _BV(OCIE2A);
637 670
638 #if 0
639 for (;;) 671 for (;;)
640 { 672 {
641 do_comms(); 673 do_comms();
642 } 674 }
643 #endif
644 675
645 for(;;){ 676 for(;;){
646 /* insert your main loop code here */ 677 /* insert your main loop code here */
647 if (need_measurement) 678 if (need_measurement)
648 { 679 {