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.
This commit is contained in:
Phil Hord 2016-04-25 18:01:59 -04:00 committed by Markus Hitter
parent 86c3d97315
commit afb81f21be
1 changed files with 5 additions and 35 deletions

40
temp.c
View File

@ -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--;
}
}