Mercurial > templog
comparison main.c @ 27:dbbd503119ba
Add some web server handling
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Tue, 12 Jun 2012 00:09:09 +0800 |
parents | d3e5934fe55c |
children | e18d7e89c17d |
comparison
equal
deleted
inserted
replaced
26:d3e5934fe55c | 27:dbbd503119ba |
---|---|
83 // counts down from WAKE_SECS to 0, goes to deep sleep when hits 0 | 83 // counts down from WAKE_SECS to 0, goes to deep sleep when hits 0 |
84 static uint8_t comms_timeout; | 84 static uint8_t comms_timeout; |
85 | 85 |
86 static uint8_t readpos; | 86 static uint8_t readpos; |
87 static char readbuf[30]; | 87 static char readbuf[30]; |
88 static uint8_t have_cmd; | |
88 | 89 |
89 static uint8_t measure_count; | 90 static uint8_t measure_count; |
90 static uint16_t comms_count; | 91 static uint16_t comms_count; |
91 | 92 |
92 static uint32_t clock_epoch; | 93 static uint32_t clock_epoch; |
224 crc_out = 0; | 225 crc_out = 0; |
225 uint8_t n_sensors; | 226 uint8_t n_sensors; |
226 eeprom_read(n_sensors, n_sensors); | 227 eeprom_read(n_sensors, n_sensors); |
227 | 228 |
228 fprintf_P(crc_stdout, PSTR("START\n")); | 229 fprintf_P(crc_stdout, PSTR("START\n")); |
229 fprintf_P(crc_stdout, PSTR("now=%lu\n"), clock_epoch); | 230 fprintf_P(crc_stdout, PSTR("now=%lu\n" |
230 fprintf_P(crc_stdout, PSTR("first_time=%lu\n"), first_measurement_clock); | 231 "time_step=%lu\n" |
231 fprintf_P(crc_stdout, PSTR("last_time=%lu\n"), last_measurement_clock); | 232 "first_time=%lu\n" |
232 fprintf_P(crc_stdout, PSTR("sensors=%d\n"), n_measurements); | 233 "last_time=%lu\n"), |
234 clock_epoch, | |
235 MEASURE_WAKE, | |
236 first_measurement_clock, | |
237 last_measurement_clock); | |
238 fprintf_P(crc_stdout, PSTR("sensors=%u\n"), n_sensors); | |
233 for (uint8_t s = 0; s < n_sensors; s++) | 239 for (uint8_t s = 0; s < n_sensors; s++) |
234 { | 240 { |
235 uint8_t id[ID_LEN]; | 241 uint8_t id[ID_LEN]; |
236 fprintf_P(crc_stdout, PSTR("sensor_id%d="), s); | 242 fprintf_P(crc_stdout, PSTR("sensor_id%u="), s); |
237 eeprom_read_to(id, sensor_id[s], ID_LEN); | 243 eeprom_read_to(id, sensor_id[s], ID_LEN); |
238 printhex(id, ID_LEN, crc_stdout); | 244 printhex(id, ID_LEN, crc_stdout); |
239 fputc('\n', crc_stdout); | 245 fputc('\n', crc_stdout); |
240 } | 246 } |
241 fprintf_P(crc_stdout, PSTR("measurements=%d\n"), n_measurements); | 247 fprintf_P(crc_stdout, PSTR("measurements=%u\n"), n_measurements); |
242 for (uint16_t n = 0; n < n_measurements; n++) | 248 for (uint16_t n = 0; n < n_measurements; n++) |
243 { | 249 { |
244 fprintf_P(crc_stdout, PSTR("meas%3d="), n); | 250 fprintf_P(crc_stdout, PSTR("meas%u="), n); |
245 for (uint8_t s = 0; s < n_sensors; s++) | 251 for (uint8_t s = 0; s < n_sensors; s++) |
246 { | 252 { |
247 fprintf_P(crc_stdout, PSTR(" %6d"), measurements[n][s]); | 253 fprintf_P(crc_stdout, PSTR(" %u"), measurements[n][s]); |
248 } | 254 } |
249 fputc('\n', crc_stdout); | 255 fputc('\n', crc_stdout); |
250 } | 256 } |
251 fprintf_P(crc_stdout, PSTR("END\n")); | 257 fprintf_P(crc_stdout, PSTR("END\n")); |
252 fprintf_P(stdout, PSTR("CRC=%hu\n"), crc_out); | 258 fprintf_P(stdout, PSTR("CRC=%hu\n"), crc_out); |
394 sei(); | 400 sei(); |
395 printf_P(PSTR("Done.\n")); | 401 printf_P(PSTR("Done.\n")); |
396 } | 402 } |
397 | 403 |
398 static void | 404 static void |
399 cmd_settime(const char *str) | |
400 { | |
401 clock_epoch = strtoul(str, NULL, 10); | |
402 printf_P(PSTR("Time set to %lu\n"), clock_epoch); | |
403 } | |
404 | |
405 static void | |
406 check_first_startup() | 405 check_first_startup() |
407 { | 406 { |
408 uint16_t magic; | 407 uint16_t magic; |
409 eeprom_read(magic, magic); | 408 eeprom_read(magic, magic); |
410 if (magic != EXPECT_MAGIC) | 409 if (magic != EXPECT_MAGIC) |
445 cmd_sensors(); | 444 cmd_sensors(); |
446 } | 445 } |
447 else if (strcmp_P(readbuf, PSTR("addall"))== 0) | 446 else if (strcmp_P(readbuf, PSTR("addall"))== 0) |
448 { | 447 { |
449 cmd_add_all(); | 448 cmd_add_all(); |
450 } | |
451 else if (strncmp_P(readbuf, PSTR("settime "), | |
452 strlen("settime ") == 0)) | |
453 { | |
454 cmd_settime(&readbuf[strlen("settime ")]); | |
455 } | 449 } |
456 else if (strcmp_P(readbuf, PSTR("init")) == 0) | 450 else if (strcmp_P(readbuf, PSTR("init")) == 0) |
457 { | 451 { |
458 cmd_init(); | 452 cmd_init(); |
459 } | 453 } |
485 | 479 |
486 ISR(USART_RX_vect) | 480 ISR(USART_RX_vect) |
487 { | 481 { |
488 char c = UDR0; | 482 char c = UDR0; |
489 uart_putchar(c, NULL); | 483 uart_putchar(c, NULL); |
490 // XXX move this out of interrupt handler | |
491 if (c == '\r' || c == '\n') | 484 if (c == '\r' || c == '\n') |
492 { | 485 { |
493 if (readpos > 0) | 486 if (readpos > 0) |
494 { | 487 { |
495 readbuf[readpos] = '\0'; | 488 readbuf[readpos] = '\0'; |
496 read_handler(); | 489 have_cmd = 1; |
497 readpos = 0; | 490 readpos = 0; |
498 } | 491 } |
499 } | 492 } |
500 else | 493 else |
501 { | 494 { |
672 set_aux_power(1); | 665 set_aux_power(1); |
673 uart_on(); | 666 uart_on(); |
674 | 667 |
675 // write sd card here? same 3.3v regulator... | 668 // write sd card here? same 3.3v regulator... |
676 | 669 |
677 printf("ready> \n"); | |
678 | |
679 for (comms_timeout = WAKE_SECS; comms_timeout > 0; ) | 670 for (comms_timeout = WAKE_SECS; comms_timeout > 0; ) |
680 { | 671 { |
681 if (need_measurement) | 672 if (need_measurement) |
682 { | 673 { |
683 need_measurement = 0; | 674 need_measurement = 0; |
684 printf("measure from do_comms\n"); | |
685 do_measurement(); | 675 do_measurement(); |
676 } | |
677 | |
678 if (have_cmd) | |
679 { | |
680 have_cmd = 0; | |
681 read_handler(); | |
686 } | 682 } |
687 | 683 |
688 // wait for commands from the master | 684 // wait for commands from the master |
689 idle_sleep(); | 685 idle_sleep(); |
690 } | 686 } |