diff --git a/Marlin/src/HAL/AVR/pinsDebug.h b/Marlin/src/HAL/AVR/pinsDebug.h index dab4e44715..b91bde182d 100644 --- a/Marlin/src/HAL/AVR/pinsDebug.h +++ b/Marlin/src/HAL/AVR/pinsDebug.h @@ -64,11 +64,13 @@ #define VALID_PIN(pin) (pin >= 0 && pin < NUM_DIGITAL_PINS ? 1 : 0) #if AVR_ATmega1284_FAMILY - #define DIGITAL_PIN_TO_ANALOG_PIN(P) int(analogInputToDigitalPin(0) - (P)) - #define IS_ANALOG(P) ((P) >= analogInputToDigitalPin(7) && (P) <= analogInputToDigitalPin(0)) + #define IS_ANALOG(P) WITHIN(P, analogInputToDigitalPin(7), analogInputToDigitalPin(0)) + #define DIGITAL_PIN_TO_ANALOG_PIN(P) int(IS_ANALOG(P) ? (P) - analogInputToDigitalPin(7) : -1) #else - #define DIGITAL_PIN_TO_ANALOG_PIN(P) int((P) - analogInputToDigitalPin(0)) - #define IS_ANALOG(P) ((P) >= analogInputToDigitalPin(0) && ((P) <= analogInputToDigitalPin(15) || (P) <= analogInputToDigitalPin(7))) + #define _ANALOG1(P) WITHIN(P, analogInputToDigitalPin(0), analogInputToDigitalPin(7)) + #define _ANALOG2(P) WITHIN(P, analogInputToDigitalPin(8), analogInputToDigitalPin(15)) + #define IS_ANALOG(P) (_ANALOG1(P) || _ANALOG2(P)) + #define DIGITAL_PIN_TO_ANALOG_PIN(P) int(_ANALOG1(P) ? (P) - analogInputToDigitalPin(0) : _ANALOG2(P) ? (P) - analogInputToDigitalPin(8) + 8 : -1) #endif #define GET_ARRAY_PIN(p) pgm_read_byte(&pin_array[p].pin) #define MULTI_NAME_PAD 26 // space needed to be pretty if not first name assigned to a pin diff --git a/Marlin/src/gcode/config/M43.cpp b/Marlin/src/gcode/config/M43.cpp index d408ded3ac..a3e837eb53 100644 --- a/Marlin/src/gcode/config/M43.cpp +++ b/Marlin/src/gcode/config/M43.cpp @@ -305,7 +305,7 @@ void GcodeSuite::M43() { // 'P' Get the range of pins to test or watch uint8_t first_pin = PARSED_PIN_INDEX('P', 0), - last_pin = parser.seenval('L') ? PARSED_PIN_INDEX('L', 0) : parser.seenval('P') ? first_pin : (NUMBER_PINS_TOTAL) - 1; + last_pin = parser.seenval('L') ? PARSED_PIN_INDEX('L', 0) : (parser.seenval('P') ? first_pin : (NUMBER_PINS_TOTAL) - 1); NOMORE(first_pin, (NUMBER_PINS_TOTAL) - 1); NOMORE(last_pin, (NUMBER_PINS_TOTAL) - 1); @@ -321,15 +321,18 @@ void GcodeSuite::M43() { // 'W' Watch until click, M108, or reset if (parser.boolval('W')) { - SERIAL_ECHOLNPGM("Watching pins"); #ifdef ARDUINO_ARCH_SAM NOLESS(first_pin, 2); // Don't hijack the UART pins #endif - uint8_t pin_state[last_pin - first_pin + 1]; + + const uint8_t pin_count = last_pin - first_pin + 1; + uint8_t pin_state[pin_count]; + bool can_watch = false; LOOP_S_LE_N(i, first_pin, last_pin) { pin_t pin = GET_PIN_MAP_PIN_M43(i); if (!VALID_PIN(pin)) continue; if (M43_NEVER_TOUCH(i) || (!ignore_protection && pin_is_protected(pin))) continue; + can_watch = true; pinMode(pin, INPUT_PULLUP); delay(1); /* @@ -340,11 +343,31 @@ void GcodeSuite::M43() { pin_state[i - first_pin] = extDigitalRead(pin); } + const bool multipin = (pin_count > 1); + + if (!can_watch) { + SERIAL_ECHOPGM("Specified pin"); + SERIAL_ECHOPGM_P(multipin ? PSTR("s are") : PSTR(" is")); + SERIAL_ECHOLNPGM(" protected. Use 'I' to override."); + return; + } + + // "Watching pin(s) # - #" + SERIAL_ECHOPGM("Watching pin"); + if (multipin) SERIAL_CHAR('s'); + SERIAL_CHAR(' '); SERIAL_ECHO(first_pin); + if (multipin) SERIAL_ECHOPGM(" - ", last_pin); + SERIAL_EOL(); + #if HAS_RESUME_CONTINUE KEEPALIVE_STATE(PAUSED_FOR_USER); wait_for_user = true; - TERN_(HOST_PROMPT_SUPPORT, hostui.prompt_do(PROMPT_USER_CONTINUE, F("M43 Wait Called"), FPSTR(CONTINUE_STR))); - TERN_(EXTENSIBLE_UI, ExtUI::onUserConfirmRequired(F("M43 Wait Called"))); + TERN_(HOST_PROMPT_SUPPORT, hostui.prompt_do(PROMPT_USER_CONTINUE, F("M43 Waiting..."), FPSTR(CONTINUE_STR))); + #if ENABLED(EXTENSIBLE_UI) + ExtUI::onUserConfirmRequired(F("M43 Waiting...")); + #else + LCD_MESSAGE(MSG_USERWAIT); + #endif #endif for (;;) { @@ -372,6 +395,8 @@ void GcodeSuite::M43() { safe_delay(200); } + + TERN_(HAS_RESUME_CONTINUE, ui.reset_status()); } else { // Report current state of selected pin(s)