Skip to content

Commit

Permalink
Use a struct
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Jun 8, 2023
1 parent 6c159b2 commit af72def
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 30 deletions.
19 changes: 8 additions & 11 deletions Marlin/src/feature/hotend_idle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,26 @@
extern HotendIdleProtection hotend_idle;

millis_t HotendIdleProtection::next_protect_ms = 0;
uint16_t HotendIdleProtection::timeout = HOTEND_IDLE_TIMEOUT_SEC,
HotendIdleProtection::trigger = HOTEND_IDLE_MIN_TRIGGER,
HotendIdleProtection::nozzle_target = HOTEND_IDLE_NOZZLE_TARGET,
HotendIdleProtection::bed_target = HOTEND_IDLE_BED_TARGET;
hotend_idle_settings_t HotendIdleProtection::cfg; // Initialized by settings.load()

void HotendIdleProtection::check_hotends(const millis_t &ms) {
bool do_prot = false;
HOTEND_LOOP() {
const bool busy = (TERN0(HAS_RESUME_CONTINUE, wait_for_user) || planner.has_blocks_queued());
if (thermalManager.degHotend(e) >= (trigger) && !busy) {
if (thermalManager.degHotend(e) >= cfg.trigger && !busy) {
do_prot = true; break;
}
}
if (bool(next_protect_ms) != do_prot)
next_protect_ms = do_prot ? ms + (timeout * 1000) : 0;
next_protect_ms = do_prot ? ms + cfg.timeout * 1000 : 0;
}

void HotendIdleProtection::check_e_motion(const millis_t &ms) {
static float old_e_position = 0;
if (old_e_position != current_position.e) {
old_e_position = current_position.e; // Track filament motion
if (next_protect_ms) // If some heater is on then...
next_protect_ms = ms + (timeout * 1000); // ...delay the timeout till later
next_protect_ms = ms + cfg.timeout * 1000; // ...delay the timeout till later
}
}

Expand All @@ -83,12 +80,12 @@ void HotendIdleProtection::timed_out() {
SERIAL_ECHOLNPGM("Hotend Idle Timeout");
LCD_MESSAGE(MSG_HOTEND_IDLE_TIMEOUT);
HOTEND_LOOP() {
if (nozzle_target < thermalManager.degTargetHotend(e))
thermalManager.setTargetHotend(nozzle_target, e);
if (cfg.nozzle_target < thermalManager.degTargetHotend(e))
thermalManager.setTargetHotend(cfg.nozzle_target, e);
}
#if HAS_HEATED_BED
if (bed_target < thermalManager.degTargetBed())
thermalManager.setTargetBed(bed_target);
if (cfg.bed_target < thermalManager.degTargetBed())
thermalManager.setTargetBed(cfg.bed_target);
#endif
}

Expand Down
14 changes: 12 additions & 2 deletions Marlin/src/feature/hotend_idle.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,22 @@
*/
#pragma once

#include "../core/millis_t.h"
#include "../inc/MarlinConfig.h"

typedef struct {
uint16_t timeout, trigger, nozzle_target, bed_target;
void set_defaults() {
timeout = HOTEND_IDLE_TIMEOUT_SEC;
trigger = HOTEND_IDLE_MIN_TRIGGER;
nozzle_target = HOTEND_IDLE_NOZZLE_TARGET;
bed_target = HOTEND_IDLE_BED_TARGET;
}
} hotend_idle_settings_t;

class HotendIdleProtection {
public:
static void check();
static uint16_t timeout, trigger, nozzle_target, bed_target;
static hotend_idle_settings_t cfg;
private:
static millis_t next_protect_ms;
static void check_hotends(const millis_t &ms);
Expand Down
8 changes: 4 additions & 4 deletions Marlin/src/lcd/menu/menu_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,10 @@ void menu_advanced_settings();
START_MENU();
BACK_ITEM(MSG_BACK);

EDIT_ITEM(uint16_3, MSG_TIMEOUT, &hotend_idle.timeout, 0, 999);
EDIT_ITEM(uint16_3, MSG_TEMPERATURE, &hotend_idle.trigger, 0, 999);
EDIT_ITEM(uint16_3, MSG_HOTEND_IDLE_NOZZLE_TARGET, &hotend_idle.nozzle_target, 0, 999);
EDIT_ITEM(uint16_3, MSG_HOTEND_IDLE_BED_TARGET, &hotend_idle.bed_target, 0, 999);
EDIT_ITEM(uint16_3, MSG_TIMEOUT, &hotend_idle.cfg.timeout, 0, 999);
EDIT_ITEM(uint16_3, MSG_TEMPERATURE, &hotend_idle.cfg.trigger, 0, 999);
EDIT_ITEM(uint16_3, MSG_HOTEND_IDLE_NOZZLE_TARGET, &hotend_idle.cfg.nozzle_target, 0, 999);
EDIT_ITEM(uint16_3, MSG_HOTEND_IDLE_BED_TARGET, &hotend_idle.cfg.bed_target, 0, 999);

END_MENU();
}
Expand Down
22 changes: 9 additions & 13 deletions Marlin/src/module/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -628,13 +628,10 @@ typedef struct SettingsDataStruct {
#endif

//
// HOTEND_IDLE_TIMEOUT Settings
// HOTEND_IDLE_TIMEOUT
//
#if ENABLED(HOTEND_IDLE_TIMEOUT)
uint16_t hotend_idle_timeout,
hotend_idle_trigger,
hotend_idle_nozzle_target,
hotend_idle_bed_target;
hotend_idle_settings_t hotend_idle_config;
#endif

} SettingsData;
Expand Down Expand Up @@ -1732,10 +1729,7 @@ void MarlinSettings::postprocess() {
// HOTEND_IDLE_TIMEOUT
//
#if ENABLED(HOTEND_IDLE_TIMEOUT)
EEPROM_WRITE(hotend_idle.timeout);
EEPROM_WRITE(hotend_idle.trigger);
EEPROM_WRITE(hotend_idle.nozzle_target);
EEPROM_WRITE(hotend_idle.bed_target);
EEPROM_WRITE(hotend_idle.cfg);
#endif

//
Expand Down Expand Up @@ -2809,10 +2803,7 @@ void MarlinSettings::postprocess() {
// HOTEND_IDLE_TIMEOUT
//
#if ENABLED(HOTEND_IDLE_TIMEOUT)
EEPROM_READ(hotend_idle.timeout);
EEPROM_READ(hotend_idle.trigger);
EEPROM_READ(hotend_idle.nozzle_target);
EEPROM_READ(hotend_idle.bed_target);
EEPROM_READ(hotend_idle.cfg);
#endif

//
Expand Down Expand Up @@ -3624,6 +3615,11 @@ void MarlinSettings::reset() {
#endif
#endif

//
// Hotend Idle Timeout
//
TERN_(HOTEND_IDLE_TIMEOUT, hotend_idle.cfg.set_defaults());

postprocess();

#if ANY(EEPROM_CHITCHAT, DEBUG_LEVELING_FEATURE)
Expand Down

0 comments on commit af72def

Please sign in to comment.