From efb781bed5999b25164fbf6e65050d169881b8eb Mon Sep 17 00:00:00 2001 From: Wurstnase Date: Fri, 20 May 2016 08:22:33 +0200 Subject: [PATCH] Temp: faster cycles in continuous mode. Thermistors and AD595 can be faster in that mode. The new stategy is: 1. read the value 2. start the adc 3. return the result - next cycle instead of: 1. start the adc - wait 10ms 2. read the value 3. return the result - next cycle Review changes by Traumflug: fixed the warnings appearing in some configurations (case NEEDS_START_ADC undefined and case NEEDS_START_ADC defined, but TEMP_READ_CONTINUOUS == 0) --- temp.c | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/temp.c b/temp.c index 4f7a9bf..f957a70 100644 --- a/temp.c +++ b/temp.c @@ -268,17 +268,31 @@ static uint16_t temp_read_max6675(temp_sensor_t i) { #ifdef TEMP_THERMISTOR static uint16_t temp_read_thermistor(temp_sensor_t i) { + // Needs to be 'static', else GCC diagnostics emits a warning "'result' may + // be used uninitialized". No surprise about this warning, it's part of the + // logic that in some configurations temp_sensors_runtime[i].active never + // reaches more than 1. + static uint16_t result; + switch (temp_sensors_runtime[i].active++) { case 1: // Start ADC conversion. #ifdef NEEDS_START_ADC + #if TEMP_READ_CONTINUOUS + result = analog_read(i); + #endif start_adc(); - return TEMP_NOT_READY; + #if ! TEMP_READ_CONTINUOUS + return TEMP_NOT_READY; + #endif #endif - // else fall through to conversion + // If not in continuous mode or no need for start_adc() fall through. case 2: // Convert temperature values. + #if ! defined NEEDS_START_ADC || ! TEMP_READ_CONTINUOUS + result = analog_read(i); + #endif temp_sensors_runtime[i].active = 0; - return temp_table_lookup(analog_read(i), i); + return temp_table_lookup(result, i); } return TEMP_NOT_READY; } @@ -326,19 +340,30 @@ static uint16_t temp_read_mcp3008(temp_sensor_t i) { #ifdef TEMP_AD595 static uint16_t temp_read_ad595(temp_sensor_t i) { + // Needs to be 'static', see comment in temp_read_thermistor(). + static uint16_t result; + switch (temp_sensors_runtime[i].active++) { case 1: // Start ADC conversion. #ifdef NEEDS_START_ADC + #if TEMP_READ_CONTINUOUS + result = analog_read(i); + #endif start_adc(); - return TEMP_NOT_READY; + #if ! TEMP_READ_CONTINUOUS + return TEMP_NOT_READY; + #endif #endif - // else fall through to conversion + // If not in continuous mode or no need for start_adc() fall through. case 2: // Convert temperature values. + #if ! TEMP_READ_CONTINUOUS + result = analog_read(i); + #endif temp_sensors_runtime[i].active = 0; // Convert >> 8 instead of >> 10 because internal temp is stored as // 14.2 fixed point. - return (analog_read(i) * 500L) >> 8; + return (result * 500L) >> 8; } return TEMP_NOT_READY; }