comparison main.c @ 22:885532437100

A bit of work on the server python
author Matt Johnston <matt@ucc.asn.au>
date Sat, 26 May 2012 10:17:27 +0800
parents 878be5e353a0
children 44c5ab5ea879
comparison
equal deleted inserted replaced
21:2029633912c2 22:885532437100
32 #define MEASURE_WAKE 10 32 #define MEASURE_WAKE 10
33 33
34 #define VALUE_NOSENSOR -9000 34 #define VALUE_NOSENSOR -9000
35 #define VALUE_BROKEN -8000 35 #define VALUE_BROKEN -8000
36 36
37 // limited to uint16_t for now
37 #define COMMS_WAKE 3600 38 #define COMMS_WAKE 3600
38 #define WAKE_SECS 30 39 #define WAKE_SECS 30
39 40
40 #define BAUD 19200 41 #define BAUD 19200
41 #define UBRR ((F_CPU)/8/(BAUD)-1) 42 #define UBRR ((F_CPU)/8/(BAUD)-1)
67 static FILE *crc_stdout = &_crc_stdout; 68 static FILE *crc_stdout = &_crc_stdout;
68 69
69 static uint16_t n_measurements; 70 static uint16_t n_measurements;
70 // stored as decidegrees 71 // stored as decidegrees
71 static int16_t measurements[NUM_MEASUREMENTS][MAX_SENSORS]; 72 static int16_t measurements[NUM_MEASUREMENTS][MAX_SENSORS];
73 static uint32_t first_measurement_clock;
74 // last_measurement_clock is redundant but checks that we're not missing
75 // samples
76 static uint32_t last_measurement_clock;
72 77
73 // boolean flags 78 // boolean flags
74 static uint8_t need_measurement; 79 static uint8_t need_measurement;
75 static uint8_t need_comms; 80 static uint8_t need_comms;
76 81
216 crc_out = 0; 221 crc_out = 0;
217 uint8_t n_sensors; 222 uint8_t n_sensors;
218 eeprom_read(n_sensors, n_sensors); 223 eeprom_read(n_sensors, n_sensors);
219 224
220 fprintf_P(crc_stdout, PSTR("START\n")); 225 fprintf_P(crc_stdout, PSTR("START\n"));
221 fprintf_P(crc_stdout, PSTR("time=%lu\n"), clock_epoch); 226 fprintf_P(crc_stdout, PSTR("now=%lu\n"), clock_epoch);
227 fprintf_P(crc_stdout, PSTR("first_time=%lu\n"), first_measurement_clock);
228 fprintf_P(crc_stdout, PSTR("last_time=%lu\n"), last_measurement_clock);
222 fprintf_P(crc_stdout, PSTR("sensors=%d\n"), n_measurements); 229 fprintf_P(crc_stdout, PSTR("sensors=%d\n"), n_measurements);
223 for (uint8_t s = 0; s < n_sensors; s++) 230 for (uint8_t s = 0; s < n_sensors; s++)
224 { 231 {
225 uint8_t id[ID_LEN]; 232 uint8_t id[ID_LEN];
226 fprintf_P(crc_stdout, PSTR("sensor_id%d="), s); 233 fprintf_P(crc_stdout, PSTR("sensor_id%d="), s);
250 } 257 }
251 258
252 static void 259 static void
253 cmd_btoff() 260 cmd_btoff()
254 { 261 {
255 printf_P(PSTR("Turning off\n")); 262 uint16_t next_wake = COMMS_WAKE - comms_count;
263 printf_P(PSTR("off:%d\n"), next_wake);
256 _delay_ms(50); 264 _delay_ms(50);
257 comms_timeout = 0; 265 comms_timeout = 0;
258 } 266 }
259 267
260 static void 268 static void
460 468
461 ISR(USART_RX_vect) 469 ISR(USART_RX_vect)
462 { 470 {
463 char c = UDR0; 471 char c = UDR0;
464 uart_putchar(c, NULL); 472 uart_putchar(c, NULL);
465 if (c == '\r') 473 // XXX move this out of interrupt handler
466 { 474 if (c == '\r' || c == '\n')
467 readbuf[readpos] = '\0'; 475 {
468 read_handler(); 476 if (readpos > 0)
469 readpos = 0; 477 {
478 readbuf[readpos] = '\0';
479 read_handler();
480 readpos = 0;
481 }
470 } 482 }
471 else 483 else
472 { 484 {
473 readbuf[readpos] = c; 485 readbuf[readpos] = c;
474 readpos++; 486 readpos++;
623 decicelsius = VALUE_BROKEN; 635 decicelsius = VALUE_BROKEN;
624 } 636 }
625 } 637 }
626 measurements[n_measurements][s] = decicelsius; 638 measurements[n_measurements][s] = decicelsius;
627 } 639 }
640
641 if (n_measurements == 0)
642 {
643 first_measurement_clock = clock_epoch;
644 }
645 last_measurement_clock = clock_epoch;
646
628 n_measurements++; 647 n_measurements++;
629 //do_adc_335(); 648 //do_adc_335();
630 } 649 }
631 650
632 static void 651 static void