Skip to content

Commit

Permalink
🎨 Minor temp / UI refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Jan 10, 2024
1 parent 320b7a9 commit 12d7995
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 86 deletions.
2 changes: 1 addition & 1 deletion Marlin/src/MarlinCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ void idle(const bool no_stepper_sleep/*=false*/) {
TERN_(HAS_BEEPER, buzzer.tick());

// Handle UI input / draw events
TERN(DWIN_CREALITY_LCD, dwinUpdate(), ui.update());
ui.update();

// Run i2c Position Encoders
#if ENABLED(I2C_POSITION_ENCODERS)
Expand Down
34 changes: 19 additions & 15 deletions Marlin/src/feature/controllerfan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,19 @@ void ControllerFan::set_fan_speed(const uint8_t s) {
}

void ControllerFan::update() {
static millis_t lastMotorOn = 0, // Last time a motor was turned on
nextMotorCheck = 0; // Last time the state was checked
static millis_t lastComponentOn = 0, // Last time a stepper, heater, etc. was turned on
nextFanCheck = 0; // Last time the state was checked
const millis_t ms = millis();
if (ELAPSED(ms, nextMotorCheck)) {
nextMotorCheck = ms + 2500UL; // Not a time critical function, so only check every 2.5s

// If any triggers for the controller fan are true...
// - At least one stepper driver is enabled
// - The heated bed is enabled
// - TEMP_SENSOR_BOARD is reporting >= CONTROLLER_FAN_MIN_BOARD_TEMP
// - TEMP_SENSOR_SOC is reporting >= CONTROLLER_FAN_MIN_SOC_TEMP
if (ELAPSED(ms, nextFanCheck)) {
nextFanCheck = ms + 2500UL; // Not a time critical function, so only check every 2.5s

/**
* If any triggers for the controller fan are true...
* - At least one stepper driver is enabled
* - The heated bed (MOSFET) is enabled
* - TEMP_SENSOR_BOARD is reporting >= CONTROLLER_FAN_MIN_BOARD_TEMP
* - TEMP_SENSOR_SOC is reporting >= CONTROLLER_FAN_MIN_SOC_TEMP
*/
const ena_mask_t axis_mask = TERN(CONTROLLER_FAN_USE_Z_ONLY, _BV(Z_AXIS), (ena_mask_t)~TERN0(CONTROLLER_FAN_IGNORE_Z, _BV(Z_AXIS)));
if ( (stepper.axis_enabled.bits & axis_mask)
|| TERN0(HAS_HEATED_BED, thermalManager.temp_bed.soft_pwm_amount > 0)
Expand All @@ -75,13 +77,15 @@ void ControllerFan::update() {
#ifdef CONTROLLER_FAN_MIN_SOC_TEMP
|| thermalManager.wholeDegSoc() >= CONTROLLER_FAN_MIN_SOC_TEMP
#endif
) lastMotorOn = ms; //... set time to NOW so the fan will turn on
) lastComponentOn = ms; //... set time to NOW so the fan will turn on

// Fan Settings. Set fan > 0:
// - If AutoMode is on and steppers have been enabled for CONTROLLERFAN_IDLE_TIME seconds.
// - If System is on idle and idle fan speed settings is activated.
/**
* Fan Settings. Set fan > 0:
* - If AutoMode is on and hot components have been powered for CONTROLLERFAN_IDLE_TIME seconds.
* - If System is on idle and idle fan speed settings is activated.
*/
set_fan_speed(
settings.auto_mode && lastMotorOn && PENDING(ms, lastMotorOn + SEC_TO_MS(settings.duration))
settings.auto_mode && lastComponentOn && PENDING(ms, lastComponentOn + SEC_TO_MS(settings.duration))
? settings.active_speed : settings.idle_speed
);

Expand Down
4 changes: 4 additions & 0 deletions Marlin/src/inc/Conditionals_LCD.h
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,10 @@
#define HAS_DISPLAY 1
#endif

#if ANY(HAS_DISPLAY, DWIN_CREALITY_LCD)
#define HAS_UI_UPDATE 1
#endif

#if HAS_WIRED_LCD && !HAS_GRAPHICAL_TFT && !IS_DWIN_MARLINUI
#define HAS_LCDPRINT 1
#endif
Expand Down
8 changes: 5 additions & 3 deletions Marlin/src/lcd/e3v2/creality/dwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4077,11 +4077,13 @@ void dwinInitScreen() {
}

void dwinUpdate() {
eachMomentUpdate(); // Status update
hmiSDCardUpdate(); // SD card update
dwinHandleScreen(); // Rotary encoder update
eachMomentUpdate(); // Status update
hmiSDCardUpdate(); // SD card update
dwinHandleScreen(); // Rotary encoder update
}

void MarlinUI::update() { dwinUpdate(); }

void eachMomentUpdate() {
static millis_t next_var_update_ms = 0, next_rts_update_ms = 0;

Expand Down
6 changes: 3 additions & 3 deletions Marlin/src/lcd/marlinui.h
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,10 @@ class MarlinUI {
template<typename... Args>
static void status_printf(int8_t level, FSTR_P const ffmt, Args... more) { status_printf_P(level, FTOP(ffmt), more...); }

#if HAS_DISPLAY
// Periodic or as-needed display update
static void update() IF_DISABLED(HAS_UI_UPDATE, {});

static void update();
#if HAS_DISPLAY

static void abort_print();
static void pause_print();
Expand Down Expand Up @@ -628,7 +629,6 @@ class MarlinUI {

#else // No LCD

static void update() {}
static void kill_screen(FSTR_P const, FSTR_P const) {}

#endif
Expand Down
110 changes: 51 additions & 59 deletions Marlin/src/module/temperature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,7 @@ PGMSTR(str_t_heating_failed, STR_T_HEATING_FAILED);
#if HAS_HEATED_BED
bed_info_t Temperature::temp_bed; // = { 0 }
// Init min and max temp with extreme values to prevent false errors during startup
raw_adc_t Temperature::mintemp_raw_BED = TEMP_SENSOR_BED_RAW_LO_TEMP,
Temperature::maxtemp_raw_BED = TEMP_SENSOR_BED_RAW_HI_TEMP;
temp_raw_range_t Temperature::temp_sensor_range_bed = { TEMP_SENSOR_BED_RAW_LO_TEMP, TEMP_SENSOR_BED_RAW_HI_TEMP };
#if WATCH_BED
bed_watch_t Temperature::watch_bed; // = { 0 }
#endif
Expand All @@ -505,8 +504,7 @@ PGMSTR(str_t_heating_failed, STR_T_HEATING_FAILED);
#if HAS_HEATED_CHAMBER
millis_t next_cool_check_ms = 0;
celsius_float_t old_temp = 9999;
raw_adc_t Temperature::mintemp_raw_CHAMBER = TEMP_SENSOR_CHAMBER_RAW_LO_TEMP,
Temperature::maxtemp_raw_CHAMBER = TEMP_SENSOR_CHAMBER_RAW_HI_TEMP;
temp_raw_range_t Temperature::temp_sensor_range_chamber = { TEMP_SENSOR_CHAMBER_RAW_LO_TEMP, TEMP_SENSOR_CHAMBER_RAW_HI_TEMP };
#if WATCH_CHAMBER
chamber_watch_t Temperature::watch_chamber; // = { 0 }
#endif
Expand All @@ -522,8 +520,7 @@ PGMSTR(str_t_heating_failed, STR_T_HEATING_FAILED);
bool flag_cooler_state;
//bool flag_cooler_excess = false;
celsius_float_t previous_temp = 9999;
raw_adc_t Temperature::mintemp_raw_COOLER = TEMP_SENSOR_COOLER_RAW_LO_TEMP,
Temperature::maxtemp_raw_COOLER = TEMP_SENSOR_COOLER_RAW_HI_TEMP;
temp_raw_range_t Temperature::temp_sensor_range_cooler = { TEMP_SENSOR_COOLER_RAW_LO_TEMP, TEMP_SENSOR_COOLER_RAW_HI_TEMP };
#if WATCH_COOLER
cooler_watch_t Temperature::watch_cooler; // = { 0 }
#endif
Expand All @@ -538,8 +535,7 @@ PGMSTR(str_t_heating_failed, STR_T_HEATING_FAILED);
#if HAS_TEMP_BOARD
board_info_t Temperature::temp_board; // = { 0 }
#if ENABLED(THERMAL_PROTECTION_BOARD)
raw_adc_t Temperature::mintemp_raw_BOARD = TEMP_SENSOR_BOARD_RAW_LO_TEMP,
Temperature::maxtemp_raw_BOARD = TEMP_SENSOR_BOARD_RAW_HI_TEMP;
temp_raw_range_t Temperature::temp_sensor_range_board = { TEMP_SENSOR_BOARD_RAW_LO_TEMP, TEMP_SENSOR_BOARD_RAW_HI_TEMP };
#endif
#endif

Expand Down Expand Up @@ -700,7 +696,7 @@ volatile bool Temperature::raw_temps_ready = false;
TERN_(EXTENSIBLE_UI, ExtUI::onPidTuning(ExtUI::result_t::PID_STARTED));
TERN_(PROUI_PID_TUNE, dwinPidTuning(isbed ? PIDTEMPBED_START : PIDTEMP_START));

if (target > GHV(CHAMBER_MAX_TARGET, BED_MAX_TARGET, temp_range[heater_id].maxtemp - (HOTEND_OVERSHOOT))) {
if (target > GHV(CHAMBER_MAX_TARGET, BED_MAX_TARGET, hotend_max_target(heater_id))) {
SERIAL_ECHOPGM(STR_PID_AUTOTUNE); SERIAL_ECHOLNPGM(STR_PID_TEMP_TOO_HIGH);
TERN_(EXTENSIBLE_UI, ExtUI::onPidTuning(ExtUI::result_t::PID_TEMP_TOO_HIGH));
TERN_(PROUI_PID_TUNE, dwinPidTuning(PID_TEMP_TOO_HIGH));
Expand All @@ -718,7 +714,7 @@ volatile bool Temperature::raw_temps_ready = false;

#if ENABLED(PRINTER_EVENT_LEDS)
const celsius_float_t start_temp = GHV(degChamber(), degBed(), degHotend(heater_id));
LEDColor color = ONHEATINGSTART();
const LEDColor oldcolor = ONHEATINGSTART();
#endif

TERN_(TEMP_TUNING_MAINTAIN_FAN, adaptive_fan_slowing = false);
Expand Down Expand Up @@ -882,7 +878,7 @@ volatile bool Temperature::raw_temps_ready = false;
if (set_result)
GHV(_set_chamber_pid(tune_pid), _set_bed_pid(tune_pid), _set_hotend_pid(heater_id, tune_pid));

TERN_(PRINTER_EVENT_LEDS, printerEventLEDs.onPidTuningDone(color));
TERN_(PRINTER_EVENT_LEDS, printerEventLEDs.onPidTuningDone(oldcolor));

TERN_(EXTENSIBLE_UI, ExtUI::onPidTuning(ExtUI::result_t::PID_DONE));
TERN_(PROUI_PID_TUNE, dwinPidTuning(AUTOTUNE_DONE));
Expand All @@ -894,13 +890,13 @@ volatile bool Temperature::raw_temps_ready = false;
hal.idletask();

// Run UI update
TERN(DWIN_CREALITY_LCD, dwinUpdate(), ui.update());
ui.update();
}
wait_for_heatup = false;

disable_all_heaters();

TERN_(PRINTER_EVENT_LEDS, printerEventLEDs.onPidTuningDone(color));
TERN_(PRINTER_EVENT_LEDS, printerEventLEDs.onPidTuningDone(oldcolor));

TERN_(EXTENSIBLE_UI, ExtUI::onPidTuning(ExtUI::result_t::PID_DONE));
TERN_(PROUI_PID_TUNE, dwinPidTuning(AUTOTUNE_DONE));
Expand Down Expand Up @@ -1143,7 +1139,7 @@ volatile bool Temperature::raw_temps_ready = false;
}

Temperature::MPC_autotuner::MeasurementState Temperature::MPC_autotuner::housekeeping() {
const millis_t report_interval_ms = 1000UL;
constexpr millis_t report_interval_ms = 1000UL;
curr_time_ms = millis();

if (updateTemperaturesIfReady()) { // temp sample ready
Expand All @@ -1158,7 +1154,7 @@ volatile bool Temperature::raw_temps_ready = false;
}

hal.idletask();
TERN(DWIN_CREALITY_LCD, dwinUpdate(), ui.update());
ui.update();

if (!wait_for_heatup) {
SERIAL_ECHOLNPGM(STR_MPC_AUTOTUNE_INTERRUPTED);
Expand Down Expand Up @@ -1834,12 +1830,18 @@ void Temperature::mintemp_error(const heater_id_t heater_id OPTARG(ERR_INCLUDE_T
// Check if temperature is within the correct band
if (WITHIN(temp_bed.celsius, BED_MINTEMP, BED_MAXTEMP)) {
#if ENABLED(BED_LIMIT_SWITCHING)

// Range-limited "bang-bang" bed heating
if (temp_bed.is_above_target(BED_HYSTERESIS))
temp_bed.soft_pwm_amount = 0;
else if (temp_bed.is_below_target(BED_HYSTERESIS))
temp_bed.soft_pwm_amount = MAX_BED_POWER >> 1;

#else // !PIDTEMPBED && !BED_LIMIT_SWITCHING

// Simple (noisy) "bang-bang" bed heating
temp_bed.soft_pwm_amount = temp_bed.is_below_target() ? MAX_BED_POWER >> 1 : 0;

#endif
}
else {
Expand Down Expand Up @@ -2632,31 +2634,8 @@ void Temperature::updateTemperaturesFromRawValues() {
TERN_(HAS_POWER_MONITOR, power_monitor.capture_values());

#if HAS_HOTEND
static constexpr int8_t temp_dir[HOTENDS] = {
#if TEMP_SENSOR_IS_ANY_MAX_TC(0)
0
#else
TEMPDIR(0)
#endif
#if HAS_MULTI_HOTEND
#if TEMP_SENSOR_IS_ANY_MAX_TC(1)
, 0
#else
, TEMPDIR(1)
#endif
#endif
#if HOTENDS > 2
#if TEMP_SENSOR_IS_ANY_MAX_TC(2)
, 0
#else
, TEMPDIR(2)
#endif
#endif
#if HOTENDS > 3
#define _TEMPDIR(N) , TEMPDIR(N)
REPEAT_S(3, HOTENDS, _TEMPDIR)
#endif
};
#define _TEMPDIR(N) TEMP_SENSOR_IS_ANY_MAX_TC(N) ? 0 : TEMPDIR(N),
static constexpr int8_t temp_dir[HOTENDS] = { REPEAT(HOTENDS, _TEMPDIR) };

HOTEND_LOOP() {
const raw_adc_t r = temp_hotend[e].getraw();
Expand All @@ -2683,31 +2662,37 @@ void Temperature::updateTemperaturesFromRawValues() {

#endif // HAS_HOTEND

#define TP_CMP(S,A,B) (TEMPDIR(S) < 0 ? ((A)<(B)) : ((A)>(B)))
#if ENABLED(THERMAL_PROTECTION_BED)
if (TP_CMP(BED, temp_bed.getraw(), maxtemp_raw_BED)) MAXTEMP_ERROR(H_BED, temp_bed.celsius);
if (temp_bed.target > 0 && !is_bed_preheating() && TP_CMP(BED, mintemp_raw_BED, temp_bed.getraw())) MINTEMP_ERROR(H_BED, temp_bed.celsius);
if (TP_CMP(BED, temp_bed.getraw(), temp_sensor_range_bed.raw_max))
MAXTEMP_ERROR(H_BED, temp_bed.celsius);
if (temp_bed.target > 0 && !is_bed_preheating() && TP_CMP(BED, temp_sensor_range_bed.raw_min, temp_bed.getraw()))
MINTEMP_ERROR(H_BED, temp_bed.celsius);
#endif

#if ALL(HAS_HEATED_CHAMBER, THERMAL_PROTECTION_CHAMBER)
if (TP_CMP(CHAMBER, temp_chamber.getraw(), maxtemp_raw_CHAMBER)) MAXTEMP_ERROR(H_CHAMBER, temp_chamber.celsius);
if (temp_chamber.target > 0 && TP_CMP(CHAMBER, mintemp_raw_CHAMBER, temp_chamber.getraw())) MINTEMP_ERROR(H_CHAMBER, temp_chamber.celsius);
if (TP_CMP(CHAMBER, temp_chamber.getraw(), temp_sensor_range_chamber.raw_max))
MAXTEMP_ERROR(H_CHAMBER, temp_chamber.celsius);
if (temp_chamber.target > 0 && TP_CMP(CHAMBER, temp_sensor_range_chamber.raw_min, temp_chamber.getraw()))
MINTEMP_ERROR(H_CHAMBER, temp_chamber.celsius);
#endif

#if ALL(HAS_COOLER, THERMAL_PROTECTION_COOLER)
if (cutter.unitPower > 0 && TP_CMP(COOLER, temp_cooler.getraw(), maxtemp_raw_COOLER)) MAXTEMP_ERROR(H_COOLER, temp_cooler.celsius);
if (TP_CMP(COOLER, mintemp_raw_COOLER, temp_cooler.getraw())) MINTEMP_ERROR(H_COOLER, temp_cooler.celsius);
if (cutter.unitPower > 0 && TP_CMP(COOLER, temp_cooler.getraw(), temp_sensor_range_cooler.raw_max))
MAXTEMP_ERROR(H_COOLER, temp_cooler.celsius);
if (TP_CMP(COOLER, temp_sensor_range_cooler.raw_min, temp_cooler.getraw()))
MINTEMP_ERROR(H_COOLER, temp_cooler.celsius);
#endif

#if ALL(HAS_TEMP_BOARD, THERMAL_PROTECTION_BOARD)
if (TP_CMP(BOARD, temp_board.getraw(), maxtemp_raw_BOARD)) MAXTEMP_ERROR(H_BOARD, temp_board.celsius);
if (TP_CMP(BOARD, mintemp_raw_BOARD, temp_board.getraw())) MINTEMP_ERROR(H_BOARD, temp_board.celsius);
if (TP_CMP(BOARD, temp_board.getraw(), temp_sensor_range_board.raw_max))
MAXTEMP_ERROR(H_BOARD, temp_board.celsius);
if (TP_CMP(BOARD, temp_sensor_range_board.raw_min, temp_board.getraw()))
MINTEMP_ERROR(H_BOARD, temp_board.celsius);
#endif

#if ALL(HAS_TEMP_SOC, THERMAL_PROTECTION_SOC)
if (TP_CMP(SOC, temp_soc.getraw(), maxtemp_raw_SOC)) MAXTEMP_ERROR(H_SOC, temp_soc.celsius);
#endif
#undef TP_CMP

} // Temperature::updateTemperaturesFromRawValues

Expand All @@ -2733,7 +2718,6 @@ void Temperature::init() {

TERN_(PROBING_HEATERS_OFF, paused_for_probing = false);


// Init (and disable) SPI thermocouples
#if TEMP_SENSOR_IS_ANY_MAX_TC(0) && PIN_EXISTS(TEMP_0_CS)
OUT_WRITE(TEMP_0_CS_PIN, HIGH);
Expand Down Expand Up @@ -3051,23 +3035,31 @@ void Temperature::init() {

// TODO: combine these into the macros above
#if HAS_HEATED_BED
while (analog_to_celsius_bed(mintemp_raw_BED) < BED_MINTEMP) mintemp_raw_BED += TEMPDIR(BED) * (OVERSAMPLENR);
while (analog_to_celsius_bed(maxtemp_raw_BED) > BED_MAXTEMP) maxtemp_raw_BED -= TEMPDIR(BED) * (OVERSAMPLENR);
while (analog_to_celsius_bed(temp_sensor_range_bed.raw_min) < BED_MINTEMP)
temp_sensor_range_bed.raw_min += TEMPDIR(BED) * (OVERSAMPLENR);
while (analog_to_celsius_bed(temp_sensor_range_bed.raw_max) > BED_MAXTEMP)
temp_sensor_range_bed.raw_max -= TEMPDIR(BED) * (OVERSAMPLENR);
#endif

#if HAS_HEATED_CHAMBER
while (analog_to_celsius_chamber(mintemp_raw_CHAMBER) < CHAMBER_MINTEMP) mintemp_raw_CHAMBER += TEMPDIR(CHAMBER) * (OVERSAMPLENR);
while (analog_to_celsius_chamber(maxtemp_raw_CHAMBER) > CHAMBER_MAXTEMP) maxtemp_raw_CHAMBER -= TEMPDIR(CHAMBER) * (OVERSAMPLENR);
while (analog_to_celsius_chamber(temp_sensor_range_chamber.raw_min) < CHAMBER_MINTEMP)
temp_sensor_range_chamber.raw_min += TEMPDIR(CHAMBER) * (OVERSAMPLENR);
while (analog_to_celsius_chamber(temp_sensor_range_chamber.raw_max) > CHAMBER_MAXTEMP)
temp_sensor_range_chamber.raw_max -= TEMPDIR(CHAMBER) * (OVERSAMPLENR);
#endif

#if HAS_COOLER
while (analog_to_celsius_cooler(mintemp_raw_COOLER) > COOLER_MINTEMP) mintemp_raw_COOLER += TEMPDIR(COOLER) * (OVERSAMPLENR);
while (analog_to_celsius_cooler(maxtemp_raw_COOLER) < COOLER_MAXTEMP) maxtemp_raw_COOLER -= TEMPDIR(COOLER) * (OVERSAMPLENR);
while (analog_to_celsius_cooler(temp_sensor_range_cooler.raw_min) > COOLER_MINTEMP)
temp_sensor_range_cooler.raw_min += TEMPDIR(COOLER) * (OVERSAMPLENR);
while (analog_to_celsius_cooler(temp_sensor_range_cooler.raw_max) < COOLER_MAXTEMP)
temp_sensor_range_cooler.raw_max -= TEMPDIR(COOLER) * (OVERSAMPLENR);
#endif

#if ALL(HAS_TEMP_BOARD, THERMAL_PROTECTION_BOARD)
while (analog_to_celsius_board(mintemp_raw_BOARD) < BOARD_MINTEMP) mintemp_raw_BOARD += TEMPDIR(BOARD) * (OVERSAMPLENR);
while (analog_to_celsius_board(maxtemp_raw_BOARD) > BOARD_MAXTEMP) maxtemp_raw_BOARD -= TEMPDIR(BOARD) * (OVERSAMPLENR);
while (analog_to_celsius_board(temp_sensor_range_board.raw_min) < BOARD_MINTEMP)
temp_sensor_range_board.raw_min += TEMPDIR(BOARD) * (OVERSAMPLENR);
while (analog_to_celsius_board(temp_sensor_range_board.raw_max) > BOARD_MAXTEMP)
temp_sensor_range_board.raw_max -= TEMPDIR(BOARD) * (OVERSAMPLENR);
#endif

#if ALL(HAS_TEMP_SOC, THERMAL_PROTECTION_SOC)
Expand Down
Loading

0 comments on commit 12d7995

Please sign in to comment.