Skip to content

Commit

Permalink
🔧 Standard filament runout pins for MKS_UI / MKS TFT (#27640)
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead authored Jan 14, 2025
1 parent 70cdfbe commit 85ebd17
Show file tree
Hide file tree
Showing 18 changed files with 155 additions and 127 deletions.
2 changes: 1 addition & 1 deletion Marlin/src/feature/pause.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ bool load_filament(const_float_t slow_load_length/*=0*/, const_float_t fast_load
impatient_beep(max_beep_count);
#if ALL(HAS_FILAMENT_SENSOR, FILAMENT_CHANGE_RESUME_ON_INSERT)
#if MULTI_FILAMENT_SENSOR
#define _CASE_INSERTED(N) case N-1: if (READ(FIL_RUNOUT##N##_PIN) != FIL_RUNOUT##N##_STATE) wait_for_user = false; break;
#define _CASE_INSERTED(N) case N-1: if (!FILAMENT_IS_OUT(N)) wait_for_user = false; break;
switch (active_extruder) {
REPEAT_1(NUM_RUNOUT_SENSORS, _CASE_INSERTED)
}
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/feature/runout.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
#define HAS_FILAMENT_SWITCH 1
#endif

#define FILAMENT_IS_OUT() (READ(FIL_RUNOUT_PIN) == FIL_RUNOUT_STATE)
#define FILAMENT_IS_OUT(N...) (READ(FIL_RUNOUT##N##_PIN) == FIL_RUNOUT##N##_STATE)

typedef Flags<
#if NUM_MOTION_SENSORS > NUM_RUNOUT_SENSORS
Expand Down
2 changes: 2 additions & 0 deletions Marlin/src/inc/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,8 @@ static_assert(COUNT(arm) == LOGICAL_AXES, "AXIS_RELATIVE_MODES must contain " _L
#error "FILAMENT_RUNOUT_DISTANCE_MM must be greater than or equal to zero."
#elif DISABLED(ADVANCED_PAUSE_FEATURE) && defined(FILAMENT_RUNOUT_SCRIPT)
static_assert(nullptr == strstr(FILAMENT_RUNOUT_SCRIPT, "M600"), "FILAMENT_RUNOUT_SCRIPT cannot make use of M600 unless ADVANCED_PAUSE_FEATURE is enabled");
#elif DGUS_LCD_UI_MKS
#error "MKS UI is not currently compatible with FILAMENT_RUNOUT_SENSOR. Define DGUS_MKS_RUNOUT_SENSOR instead."
#endif
#endif

Expand Down
6 changes: 0 additions & 6 deletions Marlin/src/lcd/extui/dgus/mks/DGUSDisplayDef.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@

#define LOGO_TIME_DELAY TERN(USE_MKS_GREEN_UI, 8000, 1500)

#if ENABLED(DGUS_MKS_RUNOUT_SENSOR)
#define MT_DET_1_PIN 1
#define MT_DET_2_PIN 2
#define MT_DET_PIN_STATE LOW
#endif

#define MKS_FINSH

extern uint16_t manualMoveStep;
Expand Down
14 changes: 9 additions & 5 deletions Marlin/src/lcd/extui/dgus/mks/DGUSScreenHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
#include "../../../../feature/powerloss.h"
#endif

#if ENABLED(DGUS_MKS_RUNOUT_SENSOR)
#define FILAMENT_IS_OUT(N...) (READ(FIL_RUNOUT##N##_PIN) == FIL_RUNOUT##N##_STATE)
#endif

#if HAS_MEDIA
extern ExtUI::FileList filelist;
#endif
Expand Down Expand Up @@ -1374,8 +1378,8 @@ void DGUSScreenHandlerMKS::extrudeLoadInit() {
}

void DGUSScreenHandlerMKS::runoutInit() {
#if PIN_EXISTS(MT_DET_1)
SET_INPUT_PULLUP(MT_DET_1_PIN);
#if ENABLED(DGUS_MKS_RUNOUT_SENSOR) && PIN_EXISTS(FIL_RUNOUT)
SET_INPUT_PULLUP(FIL_RUNOUT_PIN);
#endif
runout_mks.de_count = 0;
runout_mks.de_times = 10;
Expand All @@ -1399,17 +1403,17 @@ void DGUSScreenHandlerMKS::runoutIdle() {
break;

case UNRUNOUT_STATUS:
if (READ(MT_DET_1_PIN) == MT_DET_PIN_STATE)
if (FILAMENT_IS_OUT())
runout_mks.runout_status = RUNOUT_STATUS;
break;

case RUNOUT_BEGIN_STATUS:
if (READ(MT_DET_1_PIN) != MT_DET_PIN_STATE)
if (!FILAMENT_IS_OUT())
runout_mks.runout_status = RUNOUT_WAITING_STATUS;
break;

case RUNOUT_WAITING_STATUS:
if (READ(MT_DET_1_PIN) == MT_DET_PIN_STATE)
if (FILAMENT_IS_OUT())
runout_mks.runout_status = RUNOUT_BEGIN_STATUS;
break;

Expand Down
88 changes: 49 additions & 39 deletions Marlin/src/lcd/extui/mks_ui/mks_hardware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@
#include "mks_hardware.h"
#include "../../../module/endstops.h"

bool pw_det_sta, pw_off_sta, mt_det_sta;
#if PIN_EXISTS(MT_DET_2)
bool pw_det_sta, pw_off_sta;
#if PIN_EXISTS(FIL_RUNOUT)
bool mt_det1_sta;
#endif
#if PIN_EXISTS(FIL_RUNOUT2)
bool mt_det2_sta;
#endif
#if USE_X_MIN
Expand Down Expand Up @@ -96,40 +99,42 @@
constexpr static bool endstopz4_sta = true;
#endif

#define ESTATE(S) (READ(S##_PIN) == S##_ENDSTOP_HIT_STATE)
#define LOWSTATE(S) (READ(S##_PIN) == LOW)

void test_gpio_readlevel_L() {
#if PIN_EXISTS(WIFI_IO0)
WRITE(WIFI_IO0_PIN, HIGH);
#endif
delay(10);
pw_det_sta = (READ(MKS_TEST_POWER_LOSS_PIN) == LOW);
pw_off_sta = (READ(MKS_TEST_PS_ON_PIN) == LOW);
mt_det_sta = (READ(MT_DET_1_PIN) == LOW);
#if PIN_EXISTS(MT_DET_2)
mt_det2_sta = (READ(MT_DET_2_PIN) == LOW);
pw_det_sta = LOWSTATE(MKS_TEST_POWER_LOSS);
pw_off_sta = LOWSTATE(MKS_TEST_PS_ON);
#if PIN_EXISTS(FIL_RUNOUT)
mt_det1_sta = LOWSTATE(FIL_RUNOUT);
#endif
#if PIN_EXISTS(FIL_RUNOUT2)
mt_det2_sta = LOWSTATE(FIL_RUNOUT2);
#endif
TERN_(USE_X_MIN, endstopx1_min = ESTATE(X_MIN));
TERN_(USE_X_MAX, endstopx1_max = ESTATE(X_MAX));
TERN_(USE_X_MIN, endstopx1_min = LOWSTATE(X_MIN));
TERN_(USE_X_MAX, endstopx1_max = LOWSTATE(X_MAX));
#if USE_X2_MIN || USE_X2_MAX
endstopx2_sta = ESTATE(TERN(USE_X2_MIN, X2_MIN, X2_MAX));
endstopx2_sta = LOWSTATE(TERN(USE_X2_MIN, X2_MIN, X2_MAX));
#endif
#if USE_Y_MIN || USE_Y_MAX
endstopy1_sta = ESTATE(TERN(USE_Y_MIN, Y_MIN, Y_MAX));
endstopy1_sta = LOWSTATE(TERN(USE_Y_MIN, Y_MIN, Y_MAX));
#endif
#if USE_Y2_MIN || USE_Y2_MAX
endstopy2_sta = ESTATE(TERN(USE_Y2_MIN, Y2_MIN, Y2_MAX));
endstopy2_sta = LOWSTATE(TERN(USE_Y2_MIN, Y2_MIN, Y2_MAX));
#endif
TERN_(USE_Z_MIN, endstopz1_min = ESTATE(Z_MIN));
TERN_(USE_Z_MAX, endstopz1_max = ESTATE(Z_MAX));
TERN_(USE_Z_MIN, endstopz1_min = LOWSTATE(Z_MIN));
TERN_(USE_Z_MAX, endstopz1_max = LOWSTATE(Z_MAX));
#if USE_Z2_MIN || USE_Z2_MAX
endstopz2_sta = ESTATE(TERN(USE_Z2_MIN, Z2_MIN, Z2_MAX));
endstopz2_sta = LOWSTATE(TERN(USE_Z2_MIN, Z2_MIN, Z2_MAX));
#endif
#if USE_Z3_MIN || USE_Z3_MAX
endstopz3_sta = ESTATE(TERN(USE_Z3_MIN, Z3_MIN, Z3_MAX));
endstopz3_sta = LOWSTATE(TERN(USE_Z3_MIN, Z3_MIN, Z3_MAX));
#endif
#if USE_Z4_MIN || USE_Z4_MAX
endstopz4_sta = ESTATE(TERN(USE_Z4_MIN, Z4_MIN, Z4_MAX));
endstopz4_sta = LOWSTATE(TERN(USE_Z4_MIN, Z4_MIN, Z4_MAX));
#endif
}

Expand All @@ -138,33 +143,35 @@
WRITE(WIFI_IO0_PIN, LOW);
#endif
delay(10);
pw_det_sta = (READ(MKS_TEST_POWER_LOSS_PIN) == HIGH);
pw_off_sta = (READ(MKS_TEST_PS_ON_PIN) == HIGH);
mt_det_sta = (READ(MT_DET_1_PIN) == HIGH);
#if PIN_EXISTS(MT_DET_2)
mt_det2_sta = (READ(MT_DET_2_PIN) == HIGH);
pw_det_sta = !LOWSTATE(MKS_TEST_POWER_LOSS);
pw_off_sta = !LOWSTATE(MKS_TEST_PS_ON);
#if PIN_EXISTS(FIL_RUNOUT)
mt_det1_sta = !LOWSTATE(FIL_RUNOUT);
#endif
TERN_(USE_X_MIN, endstopx1_min = !ESTATE(X_MIN));
TERN_(USE_X_MAX, endstopx1_max = !ESTATE(X_MAX));
#if PIN_EXISTS(FIL_RUNOUT2)
mt_det2_sta = !LOWSTATE(FIL_RUNOUT2);
#endif
TERN_(USE_X_MIN, endstopx1_min = !LOWSTATE(X_MIN));
TERN_(USE_X_MAX, endstopx1_max = !LOWSTATE(X_MAX));
#if USE_X2_MIN || USE_X2_MAX
endstopx2_sta = !ESTATE(TERN(USE_X2_MIN, X2_MIN, X2_MAX));
endstopx2_sta = !LOWSTATE(TERN(USE_X2_MIN, X2_MIN, X2_MAX));
#endif
#if USE_Y_MIN || USE_Y_MAX
endstopy1_sta = !ESTATE(TERN(USE_Y_MIN, Y_MIN, Y_MAX));
endstopy1_sta = !LOWSTATE(TERN(USE_Y_MIN, Y_MIN, Y_MAX));
#endif
#if USE_Y2_MIN || USE_Y2_MAX
endstopy2_sta = !ESTATE(TERN(USE_Y2_MIN, Y2_MIN, Y2_MAX));
endstopy2_sta = !LOWSTATE(TERN(USE_Y2_MIN, Y2_MIN, Y2_MAX));
#endif
TERN_(USE_Z_MIN, endstopz1_min = !ESTATE(Z_MIN));
TERN_(USE_Z_MAX, endstopz1_max = !ESTATE(Z_MAX));
TERN_(USE_Z_MIN, endstopz1_min = !LOWSTATE(Z_MIN));
TERN_(USE_Z_MAX, endstopz1_max = !LOWSTATE(Z_MAX));
#if USE_Z2_MIN || USE_Z2_MAX
endstopz2_sta = !ESTATE(TERN(USE_Z2_MIN, Z2_MIN, Z2_MAX));
endstopz2_sta = !LOWSTATE(TERN(USE_Z2_MIN, Z2_MIN, Z2_MAX));
#endif
#if USE_Z3_MIN || USE_Z3_MAX
endstopz3_sta = !ESTATE(TERN(USE_Z3_MIN, Z3_MIN, Z3_MAX));
endstopz3_sta = !LOWSTATE(TERN(USE_Z3_MIN, Z3_MIN, Z3_MAX));
#endif
#if USE_Z4_MIN || USE_Z4_MAX
endstopz4_sta = !ESTATE(TERN(USE_Z4_MIN, Z4_MIN, Z4_MAX));
endstopz4_sta = !LOWSTATE(TERN(USE_Z4_MIN, Z4_MIN, Z4_MAX));
#endif
}

Expand All @@ -177,11 +184,11 @@
SET_OUTPUT(WIFI_IO0_PIN);
#endif

#if PIN_EXISTS(MT_DET_1)
SET_INPUT_PULLUP(MT_DET_1_PIN);
#if PIN_EXISTS(FIL_RUNOUT)
SET_INPUT_PULLUP(FIL_RUNOUT_PIN);
#endif
#if PIN_EXISTS(MT_DET_2)
SET_INPUT_PULLUP(MT_DET_2_PIN);
#if PIN_EXISTS(FIL_RUNOUT2)
SET_INPUT_PULLUP(FIL_RUNOUT2_PIN);
#endif

SET_INPUT_PULLUP(MKS_TEST_POWER_LOSS_PIN);
Expand Down Expand Up @@ -225,8 +232,11 @@
test_gpio_readlevel_L();
test_gpio_readlevel_H();
test_gpio_readlevel_L();
if (pw_det_sta && pw_off_sta && mt_det_sta
#if PIN_EXISTS(MT_DET_2)
if (pw_det_sta && pw_off_sta
#if PIN_EXISTS(FIL_RUNOUT)
&& mt_det1_sta
#endif
#if PIN_EXISTS(FIL_RUNOUT2)
&& mt_det2_sta
#endif
#if ENABLED(MKS_HARDWARE_TEST_ONLY_E0)
Expand Down
49 changes: 26 additions & 23 deletions Marlin/src/lcd/extui/mks_ui/printer_operation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
#include "../../../feature/powerloss.h"
#endif

#define FILAMENT_IS_OUT(N...) (READ(FIL_RUNOUT##N##_PIN) == FIL_RUNOUT##N##_STATE)
#ifndef FILAMENT_RUNOUT_THRESHOLD
#define FILAMENT_RUNOUT_THRESHOLD 20
#endif

extern uint32_t To_pre_view;
extern bool flash_preview_begin, default_preview_flg, gcode_preview_over;

Expand Down Expand Up @@ -101,6 +106,7 @@ void printer_state_polling() {
update_spi_flash();
}
}

#if ENABLED(POWER_LOSS_RECOVERY)
if (uiCfg.print_state == REPRINTED) {
#if HAS_HOTEND
Expand All @@ -118,6 +124,7 @@ void printer_state_polling() {
#endif

recovery.resume();

#if 0
// Move back to the saved XY
char str_1[16], str_2[16];
Expand All @@ -140,61 +147,57 @@ void printer_state_polling() {
}
#endif

if (uiCfg.print_state == WORKING)
filament_check();
if (uiCfg.print_state == WORKING) filament_check();

TERN_(MKS_WIFI_MODULE, wifi_looping());
}

void filament_pin_setup() {
#if PIN_EXISTS(MT_DET_1)
SET_INPUT_PULLUP(MT_DET_1_PIN);
#if PIN_EXISTS(FIL_RUNOUT1)
SET_INPUT_PULLUP(FIL_RUNOUT1_PIN);
#endif
#if PIN_EXISTS(MT_DET_2)
SET_INPUT_PULLUP(MT_DET_2_PIN);
#if PIN_EXISTS(FIL_RUNOUT2)
SET_INPUT_PULLUP(FIL_RUNOUT2_PIN);
#endif
#if PIN_EXISTS(MT_DET_3)
SET_INPUT_PULLUP(MT_DET_3_PIN);
#if PIN_EXISTS(FIL_RUNOUT3)
SET_INPUT_PULLUP(FIL_RUNOUT3_PIN);
#endif
}

void filament_check() {
#if ANY_PIN(MT_DET_1, MT_DET_2, MT_DET_3)
const int FIL_DELAY = 20;
#endif
#if PIN_EXISTS(MT_DET_1)
#if PIN_EXISTS(FIL_RUNOUT1)
static int fil_det_count_1 = 0;
if (READ(MT_DET_1_PIN) == MT_DET_PIN_STATE)
if (FILAMENT_IS_OUT(1))
fil_det_count_1++;
else if (fil_det_count_1 > 0)
fil_det_count_1--;
#endif

#if PIN_EXISTS(MT_DET_2)
#if PIN_EXISTS(FIL_RUNOUT2)
static int fil_det_count_2 = 0;
if (READ(MT_DET_2_PIN) == MT_DET_PIN_STATE)
if (FILAMENT_IS_OUT(2))
fil_det_count_2++;
else if (fil_det_count_2 > 0)
fil_det_count_2--;
#endif

#if PIN_EXISTS(MT_DET_3)
#if PIN_EXISTS(FIL_RUNOUT3)
static int fil_det_count_3 = 0;
if (READ(MT_DET_3_PIN) == MT_DET_PIN_STATE)
if (FILAMENT_IS_OUT(3))
fil_det_count_3++;
else if (fil_det_count_3 > 0)
fil_det_count_3--;
#endif

if (false
#if PIN_EXISTS(MT_DET_1)
|| fil_det_count_1 >= FIL_DELAY
#if PIN_EXISTS(FIL_RUNOUT1)
|| fil_det_count_1 >= FILAMENT_RUNOUT_THRESHOLD
#endif
#if PIN_EXISTS(MT_DET_2)
|| fil_det_count_2 >= FIL_DELAY
#if PIN_EXISTS(FIL_RUNOUT2)
|| fil_det_count_2 >= FILAMENT_RUNOUT_THRESHOLD
#endif
#if PIN_EXISTS(MT_DET_3)
|| fil_det_count_3 >= FIL_DELAY
#if PIN_EXISTS(FIL_RUNOUT3)
|| fil_det_count_3 >= FILAMENT_RUNOUT_THRESHOLD
#endif
) {
clear_cur_ui();
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/lcd/menu/menu_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void menu_advanced_settings();
#endif
#define STOP_ITEM(A,I,M,L) TERN(HAS_##A##I##_##M##_STATE, _STOP_ITEM, _IF_1_ELSE)(STRINGIFY(A) STRINGIFY(I) S1_SPACE(I) " " L, A##I##_##M)
#define STOP_MINMAX(A,I) STOP_ITEM(A,I,MIN,"Min") STOP_ITEM(A,I,MAX,"Max")
#define FIL_ITEM(N) PSTRING_ITEM_N_P(N-1, MSG_FILAMENT_EN, (READ(FIL_RUNOUT##N##_PIN) != FIL_RUNOUT##N##_STATE) ? PSTR("PRESENT") : PSTR("out"), SS_FULL);
#define FIL_ITEM(N) PSTRING_ITEM_N_P(N-1, MSG_FILAMENT_EN, FILAMENT_IS_OUT(N) ? PSTR("out") : PSTR("PRESENT"), SS_FULL);

static void screen_endstop_test() {
if (ui.use_click()) {
Expand Down
18 changes: 6 additions & 12 deletions Marlin/src/module/endstops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,18 +519,12 @@ void __O2 Endstops::report_states() {
print_es_state(READ(CALIBRATION_PIN) != CALIBRATION_PIN_INVERTING, F(STR_CALIBRATION));
#endif
#if MULTI_FILAMENT_SENSOR
#define _CASE_RUNOUT(N) case N: pin = FIL_RUNOUT##N##_PIN; state = FIL_RUNOUT##N##_STATE; break;
for (uint8_t i = 1; i <= NUM_RUNOUT_SENSORS; ++i) {
pin_t pin;
uint8_t state;
switch (i) {
default: continue;
REPEAT_1(NUM_RUNOUT_SENSORS, _CASE_RUNOUT)
}
SERIAL_ECHOPGM(STR_FILAMENT);
if (i > 1) SERIAL_CHAR(' ', '0' + i);
print_es_state(extDigitalRead(pin) != state);
}
#define _CASE_RUNOUT(N) do{ \
SERIAL_ECHO(F(STR_FILAMENT)); \
if ((N) > 1) SERIAL_CHAR(' ', '0' + char(N)); \
print_es_state(!FILAMENT_IS_OUT(N)); \
}while(0);
REPEAT_1(NUM_RUNOUT_SENSORS, _CASE_RUNOUT)
#undef _CASE_RUNOUT
#elif HAS_FILAMENT_SENSOR
print_es_state(!FILAMENT_IS_OUT(), F(STR_FILAMENT));
Expand Down
Loading

0 comments on commit 85ebd17

Please sign in to comment.