comparison main.c @ 323:5181ea55ea77

Add clock_epoch
author Matt Johnston <matt@ucc.asn.au>
date Tue, 22 May 2012 21:24:04 +0800
parents 840f51824254
children f1016b151689
comparison
equal deleted inserted replaced
322:840f51824254 323:5181ea55ea77
58 static void long_delay(int ms); 58 static void long_delay(int ms);
59 59
60 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, 60 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
61 _FDEV_SETUP_WRITE); 61 _FDEV_SETUP_WRITE);
62 62
63 static uint16_t n_measurements = 0; 63 static uint16_t n_measurements;
64 // stored as decidegrees 64 // stored as decidegrees
65 static int16_t measurements[NUM_MEASUREMENTS][MAX_SENSORS]; 65 static int16_t measurements[NUM_MEASUREMENTS][MAX_SENSORS];
66 66
67 // boolean flags 67 // boolean flags
68 static uint8_t need_measurement = 0; 68 static uint8_t need_measurement;
69 static uint8_t need_comms = 0; 69 static uint8_t need_comms;
70 70
71 // counts down from WAKE_SECS to 0, goes to deep sleep when hits 0 71 // counts down from WAKE_SECS to 0, goes to deep sleep when hits 0
72 static uint8_t comms_timeout = 0; 72 static uint8_t comms_timeout;
73 73
74 static uint8_t readpos = 0; 74 static uint8_t readpos;
75 static char readbuf[30]; 75 static char readbuf[30];
76 76
77 static uint8_t measure_count = 0; 77 static uint8_t measure_count;
78 static uint16_t comms_count = 0; 78 static uint16_t comms_count;
79
80 static uint32_t clock_epoch;
79 81
80 // thanks to http://projectgus.com/2010/07/eeprom-access-with-arduino/ 82 // thanks to http://projectgus.com/2010/07/eeprom-access-with-arduino/
81 #define eeprom_read_to(dst_p, eeprom_field, dst_size) eeprom_read_block((dst_p), (void *)offsetof(struct __eeprom_data, eeprom_field), (dst_size)) 83 #define eeprom_read_to(dst_p, eeprom_field, dst_size) eeprom_read_block((dst_p), (void *)offsetof(struct __eeprom_data, eeprom_field), (dst_size))
82 #define eeprom_read(dst, eeprom_field) eeprom_read_to((&dst), eeprom_field, sizeof(dst)) 84 #define eeprom_read(dst, eeprom_field) eeprom_read_to((&dst), eeprom_field, sizeof(dst))
83 #define eeprom_write_from(src_p, eeprom_field, src_size) eeprom_write_block((src_p), (void *)offsetof(struct __eeprom_data, eeprom_field), (src_size)) 85 #define eeprom_write_from(src_p, eeprom_field, src_size) eeprom_write_block((src_p), (void *)offsetof(struct __eeprom_data, eeprom_field), (src_size))
187 } 189 }
188 return 0; 190 return 0;
189 } 191 }
190 192
191 static void 193 static void
194 update_crc(uint16_t *crc, const void *data, uint8_t len)
195 {
196 for (uint8_t i = 0; i < len; i++)
197 {
198 *crc = _crc_ccitt_update(*crc, ((const uint8_t*)data)[i]);
199 }
200 }
201
202 static void
192 cmd_fetch() 203 cmd_fetch()
193 { 204 {
194 uint16_t crc = 0; 205 uint16_t crc = 0;
195 uint8_t n_sensors; 206 uint8_t n_sensors;
196 eeprom_read(n_sensors, n_sensors); 207 eeprom_read(n_sensors, n_sensors);
197 208
209 printf_P(PSTR("Time %lu\n"), clock_epoch);
210 update_crc(&crc, &clock_epoch, sizeof(clock_epoch));
198 printf_P(PSTR("%d sensors\n"), n_measurements); 211 printf_P(PSTR("%d sensors\n"), n_measurements);
199 for (uint8_t s = 0; s < n_sensors; s++) 212 for (uint8_t s = 0; s < n_sensors; s++)
200 { 213 {
201 uint8_t id[ID_LEN]; 214 uint8_t id[ID_LEN];
202 printf_P(PSTR("%d : "), s); 215 printf_P(PSTR("%d : "), s);
203 eeprom_read_to(id, sensor_id[s], ID_LEN); 216 eeprom_read_to(id, sensor_id[s], ID_LEN);
204 printhex(id, ID_LEN); 217 printhex(id, ID_LEN);
205 putchar('\n'); 218 putchar('\n');
206 for (uint8_t i = 0; i < ID_LEN; i++) 219 update_crc(&crc, id, ID_LEN);
207 {
208 crc = _crc_ccitt_update(crc, id[i]);
209 }
210 } 220 }
211 printf_P(PSTR("%d measurements\n"), n_measurements); 221 printf_P(PSTR("%d measurements\n"), n_measurements);
212 for (uint16_t n = 0; n < n_measurements; n++) 222 for (uint16_t n = 0; n < n_measurements; n++)
213 { 223 {
214 printf_P(PSTR("%3d :"), n); 224 printf_P(PSTR("%3d :"), n);
215 for (uint8_t s = 0; s < n_sensors; s++) 225 for (uint8_t s = 0; s < n_sensors; s++)
216 { 226 {
217 printf_P(PSTR(" %6d"), measurements[n][s]); 227 printf_P(PSTR(" %6d"), measurements[n][s]);
218 crc = _crc_ccitt_update(crc, measurements[n][s]); 228 update_crc(&crc, &measurements[n][s], sizeof(measurements[n][s]));
219 } 229 }
220 putchar('\n'); 230 putchar('\n');
221 } 231 }
222 printf_P(PSTR("CRC : %d\n"), crc); 232 printf_P(PSTR("CRC : %d\n"), crc);
223 } 233 }
363 sei(); 373 sei();
364 printf_P(PSTR("Done.\n")); 374 printf_P(PSTR("Done.\n"));
365 } 375 }
366 376
367 static void 377 static void
378 cmd_settime(const char *str)
379 {
380 clock_epoch = strtoul(str, NULL, 10);
381 printf_P(PSTR("Time set to %lu\n"), clock_epoch);
382 }
383
384 static void
368 check_first_startup() 385 check_first_startup()
369 { 386 {
370 uint16_t magic; 387 uint16_t magic;
371 eeprom_read(magic, magic); 388 eeprom_read(magic, magic);
372 if (magic != EXPECT_MAGIC) 389 if (magic != EXPECT_MAGIC)
408 } 425 }
409 else if (strcmp_P(readbuf, PSTR("addall"))== 0) 426 else if (strcmp_P(readbuf, PSTR("addall"))== 0)
410 { 427 {
411 cmd_add_all(); 428 cmd_add_all();
412 } 429 }
430 else if (strncmp_P(readbuf, PSTR("settime "),
431 strlen("settime ") == 0))
432 {
433 cmd_settime(&readbuf[strlen("settime ")]);
434 }
413 else if (strcmp_P(readbuf, PSTR("init")) == 0) 435 else if (strcmp_P(readbuf, PSTR("init")) == 0)
414 { 436 {
415 cmd_init(); 437 cmd_init();
416 } 438 }
417 else 439 else
444 ISR(TIMER2_COMPA_vect) 466 ISR(TIMER2_COMPA_vect)
445 { 467 {
446 TCNT2 = 0; 468 TCNT2 = 0;
447 measure_count ++; 469 measure_count ++;
448 comms_count ++; 470 comms_count ++;
471
472 clock_epoch ++;
449 473
450 if (comms_timeout != 0) 474 if (comms_timeout != 0)
451 { 475 {
452 comms_timeout--; 476 comms_timeout--;
453 } 477 }