From afb81f21be6279880f66f1bbcd68ac0d5973894a Mon Sep 17 00:00:00 2001 From: Phil Hord Date: Mon, 25 Apr 2016 18:01:59 -0400 Subject: [PATCH] temp.c: cleanup temp_sensor_tick() and next_read_time sanity. Integrate the next_read_time countdown into the loop as is common. Check for start_adc() in the same loop -- before decrementing the timer -- and call it when needed on the tick before we need the results. One concern I have still is that start_adc() may be called twice within a few microseconds if two probes need to be read. I expect it should only be called once, but I am not readily familiar with the AVR ADC conversion protocol. --- temp.c | 40 +++++----------------------------------- 1 file changed, 5 insertions(+), 35 deletions(-) diff --git a/temp.c b/temp.c index c239c04..6dffd5e 100644 --- a/temp.c +++ b/temp.c @@ -288,6 +288,7 @@ void temp_sensor_tick() { temp_sensor_t i = 0; for (; i < NUM_TEMP_SENSORS; i++) { + temp_sensors_runtime[i].next_read_time--; if (temp_sensors_runtime[i].next_read_time == 0) { uint16_t temp = 0; //time to deal with this temp sensor @@ -403,44 +404,13 @@ void temp_sensor_tick() { } #ifdef NEEDS_START_ADC - /** - Sensors that use analog_read() need an start_adc() a 10ms cycle before - they actually do the reading so that ADC conversion can start and - finish in time. - */ + // Start the ADC one tick (10ms) before it's needed, if it'll be needed. if (temp_sensors_runtime[i].next_read_time == 1) { - uint8_t needs_start_adc = 0; - - // Determine if the sensor uses analog_read(). - switch(temp_sensors[i].temp_type) { - #ifdef TEMP_THERMISTOR - case TT_THERMISTOR: - needs_start_adc = 1; - break; - #endif - - #ifdef TEMP_AD595 - case TT_AD595: - needs_start_adc = 1; - break; - #endif - - default: /* Prevent compiler warning. */ - break; - } - - if (needs_start_adc) { - start_adc(); - } + if (temp_sensors[i].temp_type == TT_THERMISTOR || + temp_sensors[i].temp_type == TT_AD595) + start_adc(); } #endif - - /** - Decrement the counter here so that we avoid a off-by-one error. It's - assumed that sensor update code in the switch statement above has set a - non-zero next_read_time (!). - */ - temp_sensors_runtime[i].next_read_time--; } }