mirror of
https://github.com/MarlinFirmware/Marlin.git
synced 2026-11-26 23:29:02 +00:00
♻️ Simplify M43 pin loops and add pins debugging unit tests (#28775)
* ♻️ Add PinsDebug::get_pin helpers to simplify M43 * ✅ Add unit tests for pins debugging
This commit is contained in:
parent
ebf7cb88c5
commit
74248a4eb8
4 changed files with 148 additions and 10 deletions
|
|
@ -69,8 +69,8 @@ inline void toggle_pins() {
|
|||
if (M43_INVALID_PIN(start) || M43_INVALID_PIN(end)) return invalid_pin_err();
|
||||
|
||||
for (uint8_t i = start; i <= end; ++i) {
|
||||
pin_t pin = PinsDebug::pin_from_index(i);
|
||||
if (!PinsDebug::is_valid(pin)) continue;
|
||||
pin_t pin;
|
||||
if (!PinsDebug::get_pin(i, pin)) continue;
|
||||
if (PinsDebug::is_untouchable(i, pin, ignore_protection)) {
|
||||
PinsDebug::print_state(pin, ignore_protection, true, F("Untouched "));
|
||||
SERIAL_EOL();
|
||||
|
|
@ -335,9 +335,8 @@ void GcodeSuite::M43() {
|
|||
uint8_t pin_state[pin_count];
|
||||
bool can_watch = false;
|
||||
for (uint8_t i = first_pin; i <= last_pin; ++i) {
|
||||
pin_t pin = PinsDebug::pin_from_index(i);
|
||||
if (!PinsDebug::is_valid(pin)) continue;
|
||||
if (PinsDebug::is_untouchable(i, pin, ignore_protection)) continue;
|
||||
pin_t pin;
|
||||
if (!PinsDebug::get_touchable_pin(i, ignore_protection, pin)) continue;
|
||||
can_watch = true;
|
||||
pinMode(pin, INPUT_PULLUP);
|
||||
delay(1);
|
||||
|
|
@ -378,9 +377,8 @@ void GcodeSuite::M43() {
|
|||
|
||||
for (;;) {
|
||||
for (uint8_t i = first_pin; i <= last_pin; ++i) {
|
||||
const pin_t pin = PinsDebug::pin_from_index(i);
|
||||
if (!PinsDebug::is_valid(pin)) continue;
|
||||
if (PinsDebug::is_untouchable(i, pin, ignore_protection)) continue;
|
||||
pin_t pin;
|
||||
if (!PinsDebug::get_touchable_pin(i, ignore_protection, pin)) continue;
|
||||
const byte val =
|
||||
/*
|
||||
isAnalogPin(pin)
|
||||
|
|
@ -407,8 +405,8 @@ void GcodeSuite::M43() {
|
|||
else {
|
||||
// Report current state of selected pin(s)
|
||||
for (uint8_t i = first_pin; i <= last_pin; ++i) {
|
||||
const pin_t pin = PinsDebug::pin_from_index(i);
|
||||
if (PinsDebug::is_valid(pin)) PinsDebug::print_state(pin, ignore_protection, true);
|
||||
pin_t pin;
|
||||
if (PinsDebug::get_pin(i, pin)) PinsDebug::print_state(pin, ignore_protection, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -253,6 +253,17 @@ struct PinsDebugBase {
|
|||
return Impl::never_touch(index) || (!ignore_protection && marlin.pin_is_protected(pin));
|
||||
}
|
||||
|
||||
// Get the pin for an M43 index. False if the index maps to no valid pin.
|
||||
static FORCE_INLINE bool get_pin(const uint16_t index, pin_t &pin) {
|
||||
pin = Impl::pin_from_index(index);
|
||||
return Impl::is_valid(pin);
|
||||
}
|
||||
|
||||
// Same, but also false for a pin M43 mustn't touch
|
||||
static FORCE_INLINE bool get_touchable_pin(const uint16_t index, const bool ignore_protection, pin_t &pin) {
|
||||
return get_pin(index, pin) && !is_untouchable(index, pin, ignore_protection);
|
||||
}
|
||||
|
||||
// Pretty report with PWM info
|
||||
static void print_state(const pin_t pin, const bool ignore, const bool extended=false, FSTR_P const start_string=nullptr) {
|
||||
char buffer[MAX_NAME_LENGTH + 1]; // for the sprintf statements
|
||||
|
|
|
|||
118
Marlin/tests/pins/test_pinsdebug.cpp
Normal file
118
Marlin/tests/pins/test_pinsdebug.cpp
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
/**
|
||||
* Marlin 3D Printer Firmware
|
||||
* Copyright (c) 2026 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
||||
*
|
||||
* Based on Sprinter and grbl.
|
||||
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "../test/unit_tests.h"
|
||||
|
||||
#if ENABLED(PINS_DEBUGGING)
|
||||
|
||||
#include "src/pins/pinsDebug.h"
|
||||
|
||||
using Pins = PinsDebug;
|
||||
|
||||
// The pin table holds every pin M43 reports, so each entry must be usable
|
||||
MARLIN_TEST(pinsdebug, pin_table_entries) {
|
||||
TEST_ASSERT_GREATER_THAN_UINT32(0, COUNT(pin_array));
|
||||
for (uint16_t i = 0; i < COUNT(pin_array); ++i) {
|
||||
PGM_P const name = (PGM_P)pgm_read_ptr(&pin_array[i].name);
|
||||
TEST_ASSERT_NOT_NULL(name);
|
||||
TEST_ASSERT_GREATER_THAN_UINT32(0, strlen_P(name));
|
||||
TEST_ASSERT_LESS_OR_EQUAL_UINT32(MAX_NAME_LENGTH, strlen_P(name));
|
||||
TEST_ASSERT_EQUAL(pin_array[i].pin, getPinByIndex(i));
|
||||
TEST_ASSERT_EQUAL(pin_array[i].is_digital, pinTableIsDigital(i));
|
||||
}
|
||||
}
|
||||
|
||||
// The HAL must supply the required hooks with usable values
|
||||
MARLIN_TEST(pinsdebug, hal_constants) {
|
||||
TEST_ASSERT_GREATER_THAN_UINT32(0, Pins::count);
|
||||
TEST_ASSERT_GREATER_THAN_UINT32(0, Pins::name_pad);
|
||||
}
|
||||
|
||||
// Valid pins are within range, and the first and last of them are valid
|
||||
MARLIN_TEST(pinsdebug, is_valid_range) {
|
||||
TEST_ASSERT_TRUE(Pins::is_valid(0));
|
||||
TEST_ASSERT_TRUE(Pins::is_valid(pin_t(Pins::count - 1)));
|
||||
TEST_ASSERT_FALSE(Pins::is_valid(pin_t(-1)));
|
||||
}
|
||||
|
||||
// get_pin maps an index to its pin, and reports an index with no valid pin
|
||||
MARLIN_TEST(pinsdebug, get_pin) {
|
||||
pin_t pin = pin_t(-2);
|
||||
TEST_ASSERT_TRUE(Pins::get_pin(0, pin));
|
||||
TEST_ASSERT_EQUAL(Pins::pin_from_index(0), pin);
|
||||
|
||||
const uint16_t last = Pins::count - 1;
|
||||
TEST_ASSERT_TRUE(Pins::get_pin(last, pin));
|
||||
TEST_ASSERT_EQUAL(Pins::pin_from_index(last), pin);
|
||||
|
||||
// Past the end of the pin range
|
||||
TEST_ASSERT_FALSE(Pins::get_pin(Pins::count, pin));
|
||||
}
|
||||
|
||||
// Pins in the sensitive list are untouchable unless protection is ignored,
|
||||
// and other pins are touchable either way
|
||||
MARLIN_TEST(pinsdebug, touchable_pins) {
|
||||
bool found_protected = false, found_free = false;
|
||||
for (uint16_t i = 0; i < Pins::count; ++i) {
|
||||
pin_t pin;
|
||||
if (!Pins::get_pin(i, pin)) continue;
|
||||
if (Pins::never_touch(i)) continue;
|
||||
|
||||
pin_t got = pin_t(-2);
|
||||
if (marlin.pin_is_protected(pin)) {
|
||||
found_protected = true;
|
||||
TEST_ASSERT_TRUE(Pins::is_untouchable(i, pin, false));
|
||||
TEST_ASSERT_FALSE(Pins::is_untouchable(i, pin, true));
|
||||
TEST_ASSERT_FALSE(Pins::get_touchable_pin(i, false, got));
|
||||
TEST_ASSERT_TRUE(Pins::get_touchable_pin(i, true, got));
|
||||
TEST_ASSERT_EQUAL(pin, got);
|
||||
}
|
||||
else {
|
||||
found_free = true;
|
||||
TEST_ASSERT_FALSE(Pins::is_untouchable(i, pin, false));
|
||||
TEST_ASSERT_TRUE(Pins::get_touchable_pin(i, false, got));
|
||||
TEST_ASSERT_EQUAL(pin, got);
|
||||
}
|
||||
}
|
||||
TEST_ASSERT_TRUE(found_protected);
|
||||
TEST_ASSERT_TRUE(found_free);
|
||||
}
|
||||
|
||||
// A pin that is not valid is never touchable
|
||||
MARLIN_TEST(pinsdebug, invalid_pin_not_touchable) {
|
||||
pin_t pin = pin_t(-2);
|
||||
TEST_ASSERT_FALSE(Pins::get_touchable_pin(Pins::count, true, pin));
|
||||
}
|
||||
|
||||
// Analog inputs have an analog index and digital pins don't
|
||||
MARLIN_TEST(pinsdebug, analog_index) {
|
||||
for (uint16_t i = 0; i < COUNT(pin_array); ++i) {
|
||||
const pin_t pin = getPinByIndex(i);
|
||||
if (!Pins::is_valid(pin)) continue;
|
||||
if (Pins::is_analog(pin))
|
||||
TEST_ASSERT_GREATER_OR_EQUAL_INT(0, Pins::analog_index(pin));
|
||||
else
|
||||
TEST_ASSERT_LESS_THAN_INT(0, Pins::analog_index(pin));
|
||||
}
|
||||
}
|
||||
|
||||
#endif // PINS_DEBUGGING
|
||||
11
test/004-pins_debugging.ini
Normal file
11
test/004-pins_debugging.ini
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#
|
||||
# Test configuration with Pins Debugging enabled
|
||||
#
|
||||
[config:base]
|
||||
ini_use_config = base
|
||||
|
||||
# Unit tests must use BOARD_SIMULATED to run natively in Linux
|
||||
motherboard = BOARD_SIMULATED
|
||||
|
||||
# Option to support the M43 pins debugging tests
|
||||
pins_debugging = on
|
||||
Loading…
Add table
Reference in a new issue