diff --git a/Marlin/src/module/temperature.cpp b/Marlin/src/module/temperature.cpp index 11c3c58b7a..2481ab76bf 100644 --- a/Marlin/src/module/temperature.cpp +++ b/Marlin/src/module/temperature.cpp @@ -83,7 +83,9 @@ #endif #endif -#define MAX6675_SEPARATE_SPI (EITHER(HEATER_0_USES_MAX6675, HEATER_1_USES_MAX6675) && PINS_EXIST(MAX6675_SCK, MAX6675_DO)) +#if EITHER(HEATER_0_USES_MAX6675, HEATER_1_USES_MAX6675) && PINS_EXIST(MAX6675_SCK, MAX6675_DO) + #define MAX6675_SEPARATE_SPI 1 +#endif #if MAX6675_SEPARATE_SPI #include "../libs/private_spi.h" @@ -2226,16 +2228,12 @@ void Temperature::disable_all_heaters() { #define THERMOCOUPLE_MAX_ERRORS 15 #endif - int Temperature::read_max6675( - #if COUNT_6675 > 1 - const uint8_t hindex - #endif - ) { - #if COUNT_6675 == 1 - constexpr uint8_t hindex = 0; - #else + int Temperature::read_max6675(TERN_(HAS_MULTI_6675, const uint8_t hindex/*=0*/)) { + #if HAS_MULTI_6675 // Needed to return the correct temp when this is called too soon static uint16_t max6675_temp_previous[COUNT_6675] = { 0 }; + #else + constexpr uint8_t hindex = 0; #endif static uint8_t max6675_errors[COUNT_6675] = { 0 }; @@ -2263,26 +2261,19 @@ void Temperature::disable_all_heaters() { static millis_t next_max6675_ms[COUNT_6675] = { 0 }; millis_t ms = millis(); if (PENDING(ms, next_max6675_ms[hindex])) - return int( - #if COUNT_6675 == 1 - max6675_temp - #else - max6675_temp_previous[hindex] // Need to return the correct previous value - #endif - ); + return int(TERN(HAS_MULTI_6675, max6675_temp_previous[hindex], max6675_temp)); next_max6675_ms[hindex] = ms + MAX6675_HEAT_INTERVAL; - #if COUNT_6675 == 1 && MAX6675_IS_MAX31865 - max6675_temp = int(max31865_0.temperature(MAX31865_SENSOR_OHMS_0, MAX31865_CALIBRATION_OHMS_0)); - #endif - - #if COUNT_6675 > 1 && MAX6675_IS_MAX31865 - #if hindex == 0 - max6675_temp = int(max31865_0.temperature(MAX31865_SENSOR_OHMS_0, MAX31865_CALIBRATION_OHMS_0)); - #elif hindex == 1 - max6675_temp = int(max31865_1.temperature(MAX31865_SENSOR_OHMS_1, MAX31865_CALIBRATION_OHMS_1)); - #endif + #if MAX6675_IS_MAX31865 + max6675_temp = int( + #if HAS_MULTI_6675 + hindex ? max31865_1.temperature(MAX31865_SENSOR_OHMS_1, MAX31865_CALIBRATION_OHMS_1) + : max31865_0.temperature(MAX31865_SENSOR_OHMS_0, MAX31865_CALIBRATION_OHMS_0) + #else + max31865_0.temperature(MAX31865_SENSOR_OHMS_0, MAX31865_CALIBRATION_OHMS_0) + #endif + ); #endif // @@ -2293,7 +2284,7 @@ void Temperature::disable_all_heaters() { spiInit(MAX6675_SPEED_BITS); #endif - #if COUNT_6675 > 1 + #if HAS_MULTI_6675 #define WRITE_MAX6675(V) do{ switch (hindex) { case 1: WRITE(MAX6675_SS2_PIN, V); break; default: WRITE(MAX6675_SS_PIN, V); } }while(0) #define SET_OUTPUT_MAX6675() do{ switch (hindex) { case 1: SET_OUTPUT(MAX6675_SS2_PIN); break; default: SET_OUTPUT(MAX6675_SS_PIN); } }while(0) #elif HEATER_1_USES_MAX6675 @@ -2312,28 +2303,13 @@ void Temperature::disable_all_heaters() { // Read a big-endian temperature value max6675_temp = 0; for (uint8_t i = sizeof(max6675_temp); i--;) { - max6675_temp |= ( - #if MAX6675_SEPARATE_SPI - max6675_spi.receive() - #else - spiRec() - #endif - ); + max6675_temp |= TERN(MAX6675_SEPARATE_SPI, max6675_spi.receive(), spiRec()); if (i > 0) max6675_temp <<= 8; // shift left if not the last byte } WRITE_MAX6675(HIGH); // disable TT_MAX6675 - uint8_t fault = 1; - #if COUNT_6675 == 1 && MAX6675_IS_MAX31865 - fault = max31865_0.readFault(); - #elif MAX6675_IS_MAX31865 - #if hindex == 0 - fault = max31865_0.readFault(); - #elif hindex == 1 - fault = max31865_1.readFault(); - #endif - #endif + const uint8_t fault = TERN0(MAX6675_IS_MAX31865, TERN_(HAS_MULTI_6675, hindex ? max31865_1.readFault() :) max31865_0.readFault()); if (DISABLED(IGNORE_THERMOCOUPLE_ERRORS) && (max6675_temp & MAX6675_ERROR_MASK) && fault) { max6675_errors[hindex] += 1; @@ -2364,15 +2340,7 @@ void Temperature::disable_all_heaters() { else if (fault & MAX31865_FAULT_OVUV) SERIAL_ECHOLNPGM("Under/Over voltage"); - #if COUNT_6675 == 1 - max31865_0.clearFault(); - #else - #if hindex == 0 - max31865_0.clearFault(); - #elif hindex == 1 - max31865_1.clearFault(); - #endif - #endif + TERN_(HAS_MULTI_6675, hindex ? max31865_1.clearFault() :) max31865_0.clearFault(); } #else SERIAL_ECHOLNPGM("MAX6675"); @@ -2380,7 +2348,7 @@ void Temperature::disable_all_heaters() { // Thermocouple open max6675_temp = 4 * ( - #if COUNT_6675 > 1 + #if HAS_MULTI_6675 hindex ? HEATER_1_MAX6675_TMAX : HEATER_0_MAX6675_TMAX #else TERN(HEATER_1_USES_MAX6675, HEATER_1_MAX6675_TMAX, HEATER_0_MAX6675_TMAX) @@ -2399,9 +2367,7 @@ void Temperature::disable_all_heaters() { if (max6675_temp & 0x00002000) max6675_temp |= 0xFFFFC000; // Support negative temperature #endif - #if COUNT_6675 > 1 - max6675_temp_previous[hindex] = max6675_temp; - #endif + TERN_(HAS_MULTI_6675, max6675_temp_previous[hindex] = max6675_temp); return int(max6675_temp); }