Mercurial > templog
comparison main.c @ 90:a1c1c2d475b0
print the remainder of timers as well
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Mon, 16 Jul 2012 21:24:16 +0800 |
parents | 51d889ad39a3 |
children | f18fd4257296 |
comparison
equal
deleted
inserted
replaced
89:51d889ad39a3 | 90:a1c1c2d475b0 |
---|---|
54 // fixed at 8, have a shorter name | 54 // fixed at 8, have a shorter name |
55 #define ID_LEN OW_ROMCODE_SIZE | 55 #define ID_LEN OW_ROMCODE_SIZE |
56 | 56 |
57 // #define HAVE_UART_ECHO | 57 // #define HAVE_UART_ECHO |
58 | 58 |
59 // stores a value of clock_epoch combined with the remainder of TCNT2, | |
60 // for 1/32 second accuracy | |
61 struct epoch_ticks | |
62 { | |
63 uint32_t ticks; | |
64 // remainder | |
65 uint8_t rem; | |
66 }; | |
67 | |
59 // eeprom-settable parameters. all timeouts should | 68 // eeprom-settable parameters. all timeouts should |
60 // be a multiple of TICK (6 seconds probably) | 69 // be a multiple of TICK (6 seconds probably) |
61 static uint16_t measure_wake = 120; | 70 static uint16_t measure_wake = 120; |
62 static uint16_t comms_wake = 3600; | 71 static uint16_t comms_wake = 3600; |
63 static uint8_t wake_secs = 30; | 72 static uint8_t wake_secs = 30; |
72 | 81 |
73 // calculated at startup as TOTAL_MEASUREMENTS/n_sensors | 82 // calculated at startup as TOTAL_MEASUREMENTS/n_sensors |
74 static uint16_t max_measurements; | 83 static uint16_t max_measurements; |
75 | 84 |
76 static uint16_t measurements[TOTAL_MEASUREMENTS]; | 85 static uint16_t measurements[TOTAL_MEASUREMENTS]; |
77 static uint32_t first_measurement_clock; | 86 |
87 static struct epoch_ticks first_measurement_clock; | |
78 // last_measurement_clock is redundant but checks that we're not missing | 88 // last_measurement_clock is redundant but checks that we're not missing |
79 // samples | 89 // samples |
80 static uint32_t last_measurement_clock; | 90 static struct epoch_ticks last_measurement_clock; |
81 | 91 static struct epoch_ticks last_comms_clock; |
82 static uint32_t last_comms_clock; | |
83 | 92 |
84 // boolean flags | 93 // boolean flags |
85 static uint8_t need_measurement; | 94 static uint8_t need_measurement; |
86 static uint8_t need_comms; | 95 static uint8_t need_comms; |
87 static uint8_t uart_enabled; | 96 static uint8_t uart_enabled; |
88 static uint8_t stay_awake; | 97 static uint8_t stay_awake; |
98 static uint8_t button_pressed; | |
89 | 99 |
90 // counts down from WAKE_SECS to 0, goes to deep sleep when hits 0 | 100 // counts down from WAKE_SECS to 0, goes to deep sleep when hits 0 |
91 static uint8_t comms_timeout; | 101 static uint8_t comms_timeout; |
92 | 102 |
93 static uint8_t readpos; | 103 static uint8_t readpos; |
181 PORT_SHDN &= ~_BV(PIN_SHDN); | 191 PORT_SHDN &= ~_BV(PIN_SHDN); |
182 } | 192 } |
183 else | 193 else |
184 { | 194 { |
185 PORT_SHDN |= _BV(PIN_SHDN); | 195 PORT_SHDN |= _BV(PIN_SHDN); |
196 } | |
197 } | |
198 | |
199 static void | |
200 get_epoch_ticks(struct epoch_ticks *t) | |
201 { | |
202 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) | |
203 { | |
204 t->ticks = clock_epoch; | |
205 t->rem = TCNT2; | |
186 } | 206 } |
187 } | 207 } |
188 | 208 |
189 static void | 209 static void |
190 set_measurement(uint8_t sensor, uint16_t measurement, uint16_t reading) | 210 set_measurement(uint8_t sensor, uint16_t measurement, uint16_t reading) |
282 { | 302 { |
283 crc_out = 0; | 303 crc_out = 0; |
284 | 304 |
285 fprintf_P(crc_stdout, PSTR("START\n")); | 305 fprintf_P(crc_stdout, PSTR("START\n")); |
286 { | 306 { |
287 uint32_t epoch_copy; | 307 struct epoch_ticks now; |
288 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) | 308 get_epoch_ticks(&now); |
289 { | 309 fprintf_P(crc_stdout, PSTR("now=%lu\n"), now.ticks); |
290 epoch_copy = clock_epoch; | 310 fprintf_P(crc_stdout, PSTR("now_rem=%hhu\n"), now.rem); |
291 } | |
292 fprintf_P(crc_stdout, PSTR("now=%lu\n"), epoch_copy); | |
293 } | 311 } |
294 fprintf_P(crc_stdout, PSTR("time_step=%hu\n"), measure_wake); | 312 fprintf_P(crc_stdout, PSTR("time_step=%hu\n"), measure_wake); |
295 fprintf_P(crc_stdout, PSTR("first_time=%lu\n"), first_measurement_clock); | 313 fprintf_P(crc_stdout, PSTR("first_time=%lu\n"), first_measurement_clock.ticks); |
296 fprintf_P(crc_stdout, PSTR("last_time=%lu\n"), last_measurement_clock); | 314 fprintf_P(crc_stdout, PSTR("first_time_rem=%hhu\n"), first_measurement_clock.rem); |
297 fprintf_P(crc_stdout, PSTR("comms_time=%lu\n"), last_comms_clock); | 315 fprintf_P(crc_stdout, PSTR("last_time=%lu\n"), last_measurement_clock.ticks); |
316 fprintf_P(crc_stdout, PSTR("last_time_rem=%hhu\n"), last_measurement_clock.rem); | |
317 fprintf_P(crc_stdout, PSTR("comms_time=%lu\n"), last_comms_clock.ticks); | |
318 fprintf_P(crc_stdout, PSTR("comms_time_rem=%hhu\n"), last_comms_clock.rem); | |
298 fprintf_P(crc_stdout, PSTR("voltage=%hu\n"), adc_vcc()); | 319 fprintf_P(crc_stdout, PSTR("voltage=%hu\n"), adc_vcc()); |
299 fprintf_P(crc_stdout, PSTR("measure=%hu\n"), measure_wake); | 320 fprintf_P(crc_stdout, PSTR("measure=%hu\n"), measure_wake); |
300 fprintf_P(crc_stdout, PSTR("comms=%hu\n"), comms_wake); | 321 fprintf_P(crc_stdout, PSTR("comms=%hu\n"), comms_wake); |
301 fprintf_P(crc_stdout, PSTR("wake=%hhu\n"), wake_secs); | 322 fprintf_P(crc_stdout, PSTR("wake=%hhu\n"), wake_secs); |
302 fprintf_P(crc_stdout, PSTR("tick=%d\n"), TICK); | 323 fprintf_P(crc_stdout, PSTR("tick_secs=%d\n"), TICK); |
324 fprintf_P(crc_stdout, PSTR("tick_wake=%d\n"), SLEEP_COMPARE); | |
303 fprintf_P(crc_stdout, PSTR("maxsens=%hhu\n"), MAX_SENSORS); | 325 fprintf_P(crc_stdout, PSTR("maxsens=%hhu\n"), MAX_SENSORS); |
304 fprintf_P(crc_stdout, PSTR("totalmeas=%hu\n"), TOTAL_MEASUREMENTS); | 326 fprintf_P(crc_stdout, PSTR("totalmeas=%hu\n"), TOTAL_MEASUREMENTS); |
305 fprintf_P(crc_stdout, PSTR("sensors=%hhu\n"), n_sensors); | 327 fprintf_P(crc_stdout, PSTR("sensors=%hhu\n"), n_sensors); |
306 for (uint8_t s = 0; s < n_sensors; s++) | 328 for (uint8_t s = 0; s < n_sensors; s++) |
307 { | 329 { |
335 { | 357 { |
336 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) | 358 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
337 { | 359 { |
338 comms_count = 0; | 360 comms_count = 0; |
339 } | 361 } |
340 printf_P(PSTR("off:%hu\n"), comms_wake); | 362 uint8_t rem = TCNT2; |
363 printf_P(PSTR("next_wake=%hu,"), comms_wake); | |
364 printf_P(PSTR("rem=%hhu,"), rem); | |
365 printf_P(PSTR("tick_secs=%hhu,"), TICK); | |
366 printf_P(PSTR("tick_wake=%hhu\n"), SLEEP_COMPARE); | |
341 _delay_ms(100); | 367 _delay_ms(100); |
342 comms_timeout = 0; | 368 comms_timeout = 0; |
343 } | 369 } |
344 | 370 |
345 static void | 371 static void |
510 } | 536 } |
511 } | 537 } |
512 | 538 |
513 ISR(INT0_vect) | 539 ISR(INT0_vect) |
514 { | 540 { |
515 need_comms = 1; | 541 button_pressed = 1; |
516 comms_timeout = wake_secs; | |
517 blink(); | 542 blink(); |
518 _delay_ms(100); | 543 _delay_ms(100); |
519 blink(); | 544 blink(); |
520 } | 545 } |
521 | 546 |
634 static void | 659 static void |
635 do_measurement() | 660 do_measurement() |
636 { | 661 { |
637 blink(); | 662 blink(); |
638 | 663 |
664 /* Take the timer here since deep_sleep() below could take 6 seconds */ | |
665 get_epoch_ticks(&last_measurement_clock); | |
666 if (n_measurements == 0) | |
667 { | |
668 first_measurement_clock = last_measurement_clock; | |
669 } | |
670 | |
639 simple_ds18b20_start_meas(NULL); | 671 simple_ds18b20_start_meas(NULL); |
640 // sleep rather than using a long delay | 672 // sleep rather than using a long delay |
641 deep_sleep(); | 673 deep_sleep(); |
642 //_delay_ms(DS18B20_TCONV_12BIT); | 674 //_delay_ms(DS18B20_TCONV_12BIT); |
643 | 675 |
655 reading = VALUE_BROKEN; | 687 reading = VALUE_BROKEN; |
656 } | 688 } |
657 set_measurement(s, n_measurements, reading); | 689 set_measurement(s, n_measurements, reading); |
658 } | 690 } |
659 | 691 |
660 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) | |
661 { | |
662 if (n_measurements == 0) | |
663 { | |
664 first_measurement_clock = clock_epoch; | |
665 } | |
666 last_measurement_clock = clock_epoch; | |
667 } | |
668 | |
669 n_measurements++; | 692 n_measurements++; |
670 //do_adc_335(); | |
671 } | 693 } |
672 | 694 |
673 static void | 695 static void |
674 do_comms() | 696 do_comms() |
675 { | 697 { |
698 get_epoch_ticks(&last_comms_clock); | |
699 | |
676 // turn on bluetooth | 700 // turn on bluetooth |
677 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) | |
678 { | |
679 last_comms_clock = clock_epoch; | |
680 } | |
681 set_aux_power(1); | 701 set_aux_power(1); |
682 // avoid receiving rubbish, perhaps | 702 // avoid receiving rubbish, perhaps |
683 _delay_ms(50); | 703 _delay_ms(50); |
684 uart_on(); | 704 uart_on(); |
685 | 705 |
757 uart_off(); | 777 uart_off(); |
758 | 778 |
759 // turn off everything except timer2 | 779 // turn off everything except timer2 |
760 PRR = _BV(PRTWI) | _BV(PRTIM0) | _BV(PRTIM1) | _BV(PRSPI) | _BV(PRUSART0) | _BV(PRADC); | 780 PRR = _BV(PRTWI) | _BV(PRTIM0) | _BV(PRTIM1) | _BV(PRSPI) | _BV(PRUSART0) | _BV(PRADC); |
761 | 781 |
762 // for testing | |
763 uart_on(); | |
764 | |
765 setup_tick_counter(); | 782 setup_tick_counter(); |
766 | 783 |
767 sei(); | 784 sei(); |
768 | 785 |
769 need_comms = 1; | 786 need_comms = 1; |
770 need_measurement = 1; | 787 need_measurement = 1; |
771 | 788 |
772 for(;;) | 789 for(;;) |
773 { | 790 { |
791 if (button_pressed) | |
792 { | |
793 // debounce | |
794 _delay_ms(200); | |
795 need_comms = 1; | |
796 comms_timeout = wake_secs; | |
797 button_pressed = 0; | |
798 } | |
799 | |
774 if (need_measurement) | 800 if (need_measurement) |
775 { | 801 { |
776 need_measurement = 0; | 802 need_measurement = 0; |
777 do_measurement(); | 803 do_measurement(); |
778 continue; | 804 continue; |