Skip to content

Commit

Permalink
🚸 Minor M43 improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed May 17, 2023
1 parent b5ccd65 commit cbbcc01
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
10 changes: 6 additions & 4 deletions Marlin/src/HAL/AVR/pinsDebug.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 30 additions & 5 deletions Marlin/src/gcode/config/M43.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,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);
Expand All @@ -329,15 +329,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);
/*
Expand All @@ -348,11 +351,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 (;;) {
Expand Down Expand Up @@ -380,6 +403,8 @@ void GcodeSuite::M43() {

safe_delay(200);
}

TERN_(HAS_RESUME_CONTINUE, ui.reset_status());
}
else {
// Report current state of selected pin(s)
Expand Down

0 comments on commit cbbcc01

Please sign in to comment.