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