Skip to content

Commit 0e72c90

Browse files
committed
πŸ§‘β€πŸ’» Add Temperature::is_above_target
1 parent 984a758 commit 0e72c90

File tree

3 files changed

+29
-28
lines changed

3 files changed

+29
-28
lines changed

β€ŽMarlin/src/lcd/e3v2/jyersui/dwin.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,7 @@ void CrealityDWINClass::Menu_Item_Handler(uint8_t menu, uint8_t item, bool draw/
12131213
if (thermalManager.temp_hotend[0].target < thermalManager.extrude_min_temp)
12141214
Popup_Handler(ETemp);
12151215
else {
1216-
if (thermalManager.temp_hotend[0].is_below_target(-2)) {
1216+
if (thermalManager.temp_hotend[0].is_below_target(2)) {
12171217
Popup_Handler(Heating);
12181218
thermalManager.wait_for_hotend(0);
12191219
}
@@ -1356,7 +1356,7 @@ void CrealityDWINClass::Menu_Item_Handler(uint8_t menu, uint8_t item, bool draw/
13561356
Popup_Handler(ETemp);
13571357
}
13581358
else {
1359-
if (thermalManager.temp_hotend[0].is_below_target(-2)) {
1359+
if (thermalManager.temp_hotend[0].is_below_target(2)) {
13601360
Popup_Handler(Heating);
13611361
thermalManager.wait_for_hotend(0);
13621362
Redraw_Menu();
@@ -1743,7 +1743,7 @@ void CrealityDWINClass::Menu_Item_Handler(uint8_t menu, uint8_t item, bool draw/
17431743
if (thermalManager.temp_hotend[0].target < thermalManager.extrude_min_temp)
17441744
Popup_Handler(ETemp);
17451745
else {
1746-
if (thermalManager.temp_hotend[0].is_below_target(-2)) {
1746+
if (thermalManager.temp_hotend[0].is_below_target(2)) {
17471747
Popup_Handler(Heating);
17481748
thermalManager.wait_for_hotend(0);
17491749
}
@@ -1762,7 +1762,7 @@ void CrealityDWINClass::Menu_Item_Handler(uint8_t menu, uint8_t item, bool draw/
17621762
Popup_Handler(ETemp);
17631763
}
17641764
else {
1765-
if (thermalManager.temp_hotend[0].is_below_target(-2)) {
1765+
if (thermalManager.temp_hotend[0].is_below_target(2)) {
17661766
Popup_Handler(Heating);
17671767
thermalManager.wait_for_hotend(0);
17681768
}
@@ -1780,7 +1780,7 @@ void CrealityDWINClass::Menu_Item_Handler(uint8_t menu, uint8_t item, bool draw/
17801780
if (thermalManager.temp_hotend[0].target < thermalManager.extrude_min_temp)
17811781
Popup_Handler(ETemp);
17821782
else {
1783-
if (thermalManager.temp_hotend[0].is_below_target(-2)) {
1783+
if (thermalManager.temp_hotend[0].is_below_target(2)) {
17841784
Popup_Handler(Heating);
17851785
thermalManager.wait_for_hotend(0);
17861786
}
@@ -4505,7 +4505,7 @@ void CrealityDWINClass::Popup_Control() {
45054505
if (thermalManager.temp_hotend[0].target < thermalManager.extrude_min_temp)
45064506
Popup_Handler(ETemp);
45074507
else {
4508-
if (thermalManager.temp_hotend[0].is_below_target(-2)) {
4508+
if (thermalManager.temp_hotend[0].is_below_target(2)) {
45094509
Popup_Handler(Heating);
45104510
thermalManager.wait_for_hotend(0);
45114511
}

β€ŽMarlin/src/module/temperature.cpp

+21-21
Original file line numberDiff line numberDiff line change
@@ -1599,22 +1599,25 @@ void Temperature::mintemp_error(const heater_id_t heater_id) {
15991599
#endif
16001600

16011601
#if HEATER_IDLE_HANDLER
1602-
if (heater_idle[IDLE_INDEX_BED].timed_out) {
1602+
const bool bed_timed_out = heater_idle[IDLE_INDEX_BED].timed_out;
1603+
if (bed_timed_out) {
16031604
temp_bed.soft_pwm_amount = 0;
16041605
if (DISABLED(PIDTEMPBED)) WRITE_HEATER_BED(LOW);
16051606
}
1606-
else
1607+
#else
1608+
constexpr bool bed_timed_out = false;
16071609
#endif
1608-
{
1610+
1611+
if (!bed_timed_out) {
16091612
#if ENABLED(PIDTEMPBED)
16101613
temp_bed.soft_pwm_amount = WITHIN(temp_bed.celsius, BED_MINTEMP, BED_MAXTEMP) ? (int)get_pid_output_bed() >> 1 : 0;
16111614
#else
16121615
// Check if temperature is within the correct band
16131616
if (WITHIN(temp_bed.celsius, BED_MINTEMP, BED_MAXTEMP)) {
16141617
#if ENABLED(BED_LIMIT_SWITCHING)
1615-
if (temp_bed.celsius >= temp_bed.target + BED_HYSTERESIS)
1618+
if (temp_bed.is_above_target((BED_HYSTERESIS) - 1))
16161619
temp_bed.soft_pwm_amount = 0;
1617-
else if (temp_bed.is_below_target(-(BED_HYSTERESIS) + 1))
1620+
else if (temp_bed.is_below_target((BED_HYSTERESIS) - 1))
16181621
temp_bed.soft_pwm_amount = MAX_BED_POWER >> 1;
16191622
#else // !PIDTEMPBED && !BED_LIMIT_SWITCHING
16201623
temp_bed.soft_pwm_amount = temp_bed.is_below_target() ? MAX_BED_POWER >> 1 : 0;
@@ -1641,7 +1644,7 @@ void Temperature::mintemp_error(const heater_id_t heater_id) {
16411644
#endif
16421645

16431646
#if ENABLED(THERMAL_PROTECTION_CHAMBER)
1644-
if (degChamber() > CHAMBER_MAXTEMP) maxtemp_error(H_CHAMBER);
1647+
if (degChamber() > (CHAMBER_MAXTEMP)) maxtemp_error(H_CHAMBER);
16451648
#endif
16461649

16471650
#if WATCH_CHAMBER
@@ -1669,13 +1672,12 @@ void Temperature::mintemp_error(const heater_id_t heater_id) {
16691672
#if CHAMBER_FAN_MODE == 0
16701673
fan_chamber_pwm = CHAMBER_FAN_BASE;
16711674
#elif CHAMBER_FAN_MODE == 1
1672-
fan_chamber_pwm = (temp_chamber.celsius > temp_chamber.target) ? (CHAMBER_FAN_BASE) + (CHAMBER_FAN_FACTOR) * (temp_chamber.celsius - temp_chamber.target) : 0;
1675+
fan_chamber_pwm = temp_chamber.is_above_target() ? (CHAMBER_FAN_BASE) + (CHAMBER_FAN_FACTOR) * (temp_chamber.celsius - temp_chamber.target) : 0;
16731676
#elif CHAMBER_FAN_MODE == 2
16741677
fan_chamber_pwm = (CHAMBER_FAN_BASE) + (CHAMBER_FAN_FACTOR) * ABS(temp_chamber.celsius - temp_chamber.target);
1675-
if (temp_chamber.soft_pwm_amount)
1676-
fan_chamber_pwm += (CHAMBER_FAN_FACTOR) * 2;
1678+
if (temp_chamber.soft_pwm_amount) fan_chamber_pwm += (CHAMBER_FAN_FACTOR) * 2;
16771679
#elif CHAMBER_FAN_MODE == 3
1678-
fan_chamber_pwm = CHAMBER_FAN_BASE + _MAX((CHAMBER_FAN_FACTOR) * (temp_chamber.celsius - temp_chamber.target), 0);
1680+
fan_chamber_pwm = (CHAMBER_FAN_BASE) + _MAX((CHAMBER_FAN_FACTOR) * (temp_chamber.celsius - temp_chamber.target), 0);
16791681
#endif
16801682
NOMORE(fan_chamber_pwm, 255);
16811683
set_fan_speed(CHAMBER_FAN_INDEX, fan_chamber_pwm);
@@ -1688,7 +1690,7 @@ void Temperature::mintemp_error(const heater_id_t heater_id) {
16881690
#ifndef MIN_COOLING_SLOPE_DEG_CHAMBER_VENT
16891691
#define MIN_COOLING_SLOPE_DEG_CHAMBER_VENT 1.5
16901692
#endif
1691-
if (!flag_chamber_excess_heat && temp_chamber.celsius - temp_chamber.target >= HIGH_EXCESS_HEAT_LIMIT) {
1693+
if (!flag_chamber_excess_heat && temp_chamber.is_above_target((HIGH_EXCESS_HEAT_LIMIT) - 1)) {
16921694
// Open vent after MIN_COOLING_SLOPE_TIME_CHAMBER_VENT seconds if the
16931695
// temperature didn't drop at least MIN_COOLING_SLOPE_DEG_CHAMBER_VENT
16941696
if (next_cool_check_ms_2 == 0 || ELAPSED(ms, next_cool_check_ms_2)) {
@@ -1702,7 +1704,7 @@ void Temperature::mintemp_error(const heater_id_t heater_id) {
17021704
next_cool_check_ms_2 = 0;
17031705
old_temp = 9999;
17041706
}
1705-
if (flag_chamber_excess_heat && (temp_chamber.target - temp_chamber.celsius >= LOW_EXCESS_HEAT_LIMIT))
1707+
if (flag_chamber_excess_heat && temp_chamber.is_above_target((LOW_EXCESS_HEAT_LIMIT) - 1))
17061708
flag_chamber_excess_heat = false;
17071709
#endif
17081710
}
@@ -1734,9 +1736,9 @@ void Temperature::mintemp_error(const heater_id_t heater_id) {
17341736
}
17351737
else {
17361738
#if ENABLED(CHAMBER_LIMIT_SWITCHING)
1737-
if (temp_chamber.celsius >= temp_chamber.target + TEMP_CHAMBER_HYSTERESIS)
1739+
if (temp_chamber.is_above_target((TEMP_CHAMBER_HYSTERESIS) - 1))
17381740
temp_chamber.soft_pwm_amount = 0;
1739-
else if (temp_chamber.is_below_target(-(TEMP_CHAMBER_HYSTERESIS) + 1))
1741+
else if (temp_chamber.is_below_target((TEMP_CHAMBER_HYSTERESIS) - 1))
17401742
temp_chamber.soft_pwm_amount = (MAX_CHAMBER_POWER) >> 1;
17411743
#else
17421744
temp_chamber.soft_pwm_amount = temp_chamber.is_below_target() ? (MAX_CHAMBER_POWER) >> 1 : 0;
@@ -1788,20 +1790,18 @@ void Temperature::mintemp_error(const heater_id_t heater_id) {
17881790
if (temp_cooler.target == 0) temp_cooler.target = COOLER_MIN_TARGET;
17891791
if (ELAPSED(ms, next_cooler_check_ms)) {
17901792
next_cooler_check_ms = ms + COOLER_CHECK_INTERVAL;
1791-
if (temp_cooler.celsius > temp_cooler.target) {
1792-
temp_cooler.soft_pwm_amount = temp_cooler.celsius > temp_cooler.target ? MAX_COOLER_POWER : 0;
1793-
flag_cooler_state = temp_cooler.soft_pwm_amount > 0 ? true : false; // used to allow M106 fan control when cooler is disabled
1793+
if (temp_cooler.is_above_target()) { // too warm?
1794+
temp_cooler.soft_pwm_amount = MAX_COOLER_POWER;
17941795
#if ENABLED(COOLER_FAN)
1795-
int16_t fan_cooler_pwm = (COOLER_FAN_BASE) + (COOLER_FAN_FACTOR) * ABS(temp_cooler.celsius - temp_cooler.target);
1796-
NOMORE(fan_cooler_pwm, 255);
1797-
set_fan_speed(COOLER_FAN_INDEX, fan_cooler_pwm); // Set cooler fan pwm
1796+
const int16_t fan_cooler_pwm = (COOLER_FAN_BASE) + (COOLER_FAN_FACTOR) * ABS(temp_cooler.celsius - temp_cooler.target);
1797+
set_fan_speed(COOLER_FAN_INDEX, _MIN(fan_cooler_pwm, 255)); // Set cooler fan pwm
17981798
cooler_fan_flush_ms = ms + 5000;
17991799
#endif
18001800
}
18011801
else {
18021802
temp_cooler.soft_pwm_amount = 0;
18031803
#if ENABLED(COOLER_FAN)
1804-
set_fan_speed(COOLER_FAN_INDEX, temp_cooler.celsius > temp_cooler.target - 2 ? COOLER_FAN_BASE : 0);
1804+
set_fan_speed(COOLER_FAN_INDEX, temp_cooler.is_above_target(-2) ? COOLER_FAN_BASE : 0);
18051805
#endif
18061806
WRITE_HEATER_COOLER(LOW);
18071807
}

β€ŽMarlin/src/module/temperature.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,8 @@ typedef struct TempInfo {
421421
typedef struct HeaterInfo : public TempInfo {
422422
celsius_t target;
423423
uint8_t soft_pwm_amount;
424-
bool is_below_target(const celsius_t offs=0) const { return (celsius < (target + offs)); }
424+
bool is_below_target(const celsius_t offs=0) const { return (target - celsius > offs); } // celsius < target - offs
425+
bool is_above_target(const celsius_t offs=0) const { return (celsius - target > offs); } // celsius > target + offs
425426
} heater_info_t;
426427

427428
// A heater with PID stabilization

0 commit comments

Comments
Β (0)