♻️ Minimize endstop bits

This commit is contained in:
Scott Lahteine 2021-05-18 22:53:52 -05:00 committed by Scott Lahteine
parent 046bac6769
commit 894c954e8f
3 changed files with 68 additions and 44 deletions

View File

@ -56,12 +56,12 @@ Endstops endstops;
// private: // private:
bool Endstops::enabled, Endstops::enabled_globally; // Initialized by settings.load() bool Endstops::enabled, Endstops::enabled_globally; // Initialized by settings.load()
volatile uint8_t Endstops::hit_state;
Endstops::esbits_t Endstops::live_state = 0; volatile Endstops::endstop_mask_t Endstops::hit_state;
Endstops::endstop_mask_t Endstops::live_state = 0;
#if ENDSTOP_NOISE_THRESHOLD #if ENDSTOP_NOISE_THRESHOLD
Endstops::esbits_t Endstops::validated_live_state; Endstops::endstop_mask_t Endstops::validated_live_state;
uint8_t Endstops::endstop_poll_count; uint8_t Endstops::endstop_poll_count;
#endif #endif
@ -356,7 +356,7 @@ void Endstops::resync() {
#endif #endif
void Endstops::event_handler() { void Endstops::event_handler() {
static uint8_t prev_hit_state; // = 0 static endstop_mask_t prev_hit_state; // = 0
if (hit_state == prev_hit_state) return; if (hit_state == prev_hit_state) return;
prev_hit_state = hit_state; prev_hit_state = hit_state;
if (hit_state) { if (hit_state) {
@ -364,15 +364,14 @@ void Endstops::event_handler() {
char chrX = ' ', chrY = ' ', chrZ = ' ', chrP = ' '; char chrX = ' ', chrY = ' ', chrZ = ' ', chrP = ' ';
#define _SET_STOP_CHAR(A,C) (chr## A = C) #define _SET_STOP_CHAR(A,C) (chr## A = C)
#else #else
#define _SET_STOP_CHAR(A,C) ; #define _SET_STOP_CHAR(A,C) NOOP
#endif #endif
#define _ENDSTOP_HIT_ECHO(A,C) do{ \ #define _ENDSTOP_HIT_ECHO(A,C) do{ \
SERIAL_ECHOPAIR(" " STRINGIFY(A) ":", planner.triggered_position_mm(_AXIS(A))); \ SERIAL_ECHOPAIR(" " STRINGIFY(A) ":", planner.triggered_position_mm(_AXIS(A))); _SET_STOP_CHAR(A,C); }while(0)
_SET_STOP_CHAR(A,C); }while(0)
#define _ENDSTOP_HIT_TEST(A,C) \ #define _ENDSTOP_HIT_TEST(A,C) \
if (TEST(hit_state, A ##_MIN) || TEST(hit_state, A ##_MAX)) \ if (TERN0(HAS_##A##_MIN, TEST(hit_state, A##_MIN)) || TERN0(HAS_##A##_MAX, TEST(hit_state, A##_MAX))) \
_ENDSTOP_HIT_ECHO(A,C) _ENDSTOP_HIT_ECHO(A,C)
#define ENDSTOP_HIT_TEST_X() _ENDSTOP_HIT_TEST(X,'X') #define ENDSTOP_HIT_TEST_X() _ENDSTOP_HIT_TEST(X,'X')
@ -659,7 +658,7 @@ void Endstops::update() {
* still exist. The only way to reduce them further is to increase the number of samples. * still exist. The only way to reduce them further is to increase the number of samples.
* To reduce the chance to 1% (1/128th) requires 7 samples (adding 7ms of delay). * To reduce the chance to 1% (1/128th) requires 7 samples (adding 7ms of delay).
*/ */
static esbits_t old_live_state; static endstop_mask_t old_live_state;
if (old_live_state != live_state) { if (old_live_state != live_state) {
endstop_poll_count = ENDSTOP_NOISE_THRESHOLD; endstop_poll_count = ENDSTOP_NOISE_THRESHOLD;
old_live_state = live_state; old_live_state = live_state;

View File

@ -28,24 +28,52 @@
#include "../inc/MarlinConfig.h" #include "../inc/MarlinConfig.h"
#include <stdint.h> #include <stdint.h>
#define __ES_ITEM(N) N,
#define _ES_ITEM(K,N) TERN_(K,DEFER4(__ES_ITEM)(N))
enum EndstopEnum : char { enum EndstopEnum : char {
X_MIN, Y_MIN, Z_MIN, Z_MIN_PROBE, _ES_ITEM(HAS_X_MIN, X_MIN)
X_MAX, Y_MAX, Z_MAX, _ES_ITEM(HAS_X_MAX, X_MAX)
X2_MIN, X2_MAX, _ES_ITEM(HAS_Y_MIN, Y_MIN)
Y2_MIN, Y2_MAX, _ES_ITEM(HAS_Y_MAX, Y_MAX)
Z2_MIN, Z2_MAX, _ES_ITEM(HAS_Z_MIN, Z_MIN)
Z3_MIN, Z3_MAX, _ES_ITEM(HAS_Z_MAX, Z_MAX)
Z4_MIN, Z4_MAX #if ENABLED(X_DUAL_ENDSTOPS)
_ES_ITEM(HAS_X_MIN, X2_MIN)
_ES_ITEM(HAS_X_MAX, X2_MAX)
#endif
#if ENABLED(Y_DUAL_ENDSTOPS)
_ES_ITEM(HAS_Y_MIN, Y2_MIN)
_ES_ITEM(HAS_Y_MAX, Y2_MAX)
#endif
#if ENABLED(Z_MULTI_ENDSTOPS)
_ES_ITEM(HAS_Z_MIN, Z2_MIN)
_ES_ITEM(HAS_Z_MAX, Z2_MAX)
#if NUM_Z_STEPPER_DRIVERS >= 3
_ES_ITEM(HAS_Z_MIN, Z3_MIN)
_ES_ITEM(HAS_Z_MAX, Z3_MAX)
#endif
#if NUM_Z_STEPPER_DRIVERS >= 4
_ES_ITEM(HAS_Z_MIN, Z4_MIN)
_ES_ITEM(HAS_Z_MAX, Z4_MAX)
#endif
#endif
_ES_ITEM(HAS_Z_MIN_PROBE_PIN, Z_MIN_PROBE)
NUM_ENDSTOP_STATES
}; };
#define X_ENDSTOP (x_home_dir(active_extruder) < 0 ? X_MIN : X_MAX) #define X_ENDSTOP (x_home_dir(active_extruder) < 0 ? X_MIN : X_MAX)
#define Y_ENDSTOP (Y_HOME_DIR < 0 ? Y_MIN : Y_MAX) #define Y_ENDSTOP (Y_HOME_DIR < 0 ? Y_MIN : Y_MAX)
#define Z_ENDSTOP (Z_HOME_DIR < 0 ? TERN(HOMING_Z_WITH_PROBE, Z_MIN, Z_MIN_PROBE) : Z_MAX) #define Z_ENDSTOP (Z_HOME_DIR < 0 ? TERN(HOMING_Z_WITH_PROBE, Z_MIN, Z_MIN_PROBE) : Z_MAX)
#undef __ES_ITEM
#undef _ES_ITEM
class Endstops { class Endstops {
public: public:
#if HAS_EXTRA_ENDSTOPS
typedef uint16_t esbits_t; typedef IF<(NUM_ENDSTOP_STATES > 8), uint16_t, uint8_t>::type endstop_mask_t;
#if ENABLED(X_DUAL_ENDSTOPS) #if ENABLED(X_DUAL_ENDSTOPS)
static float x2_endstop_adj; static float x2_endstop_adj;
#endif #endif
@ -61,17 +89,14 @@ class Endstops {
#if ENABLED(Z_MULTI_ENDSTOPS) && NUM_Z_STEPPER_DRIVERS >= 4 #if ENABLED(Z_MULTI_ENDSTOPS) && NUM_Z_STEPPER_DRIVERS >= 4
static float z4_endstop_adj; static float z4_endstop_adj;
#endif #endif
#else
typedef uint8_t esbits_t;
#endif
private: private:
static bool enabled, enabled_globally; static bool enabled, enabled_globally;
static esbits_t live_state; static endstop_mask_t live_state;
static volatile uint8_t hit_state; // Use X_MIN, Y_MIN, Z_MIN and Z_MIN_PROBE as BIT index static volatile endstop_mask_t hit_state; // Use X_MIN, Y_MIN, Z_MIN and Z_MIN_PROBE as BIT index
#if ENDSTOP_NOISE_THRESHOLD #if ENDSTOP_NOISE_THRESHOLD
static esbits_t validated_live_state; static endstop_mask_t validated_live_state;
static uint8_t endstop_poll_count; // Countdown from threshold for polling static uint8_t endstop_poll_count; // Countdown from threshold for polling
#endif #endif
@ -107,12 +132,12 @@ class Endstops {
/** /**
* Get Endstop hit state. * Get Endstop hit state.
*/ */
FORCE_INLINE static uint8_t trigger_state() { return hit_state; } FORCE_INLINE static endstop_mask_t trigger_state() { return hit_state; }
/** /**
* Get current endstops state * Get current endstops state
*/ */
FORCE_INLINE static esbits_t state() { FORCE_INLINE static endstop_mask_t state() {
return return
#if ENDSTOP_NOISE_THRESHOLD #if ENDSTOP_NOISE_THRESHOLD
validated_live_state validated_live_state

View File

@ -260,12 +260,12 @@ xyze_int8_t Stepper::count_direction{0};
#define DUAL_ENDSTOP_APPLY_STEP(A,V) \ #define DUAL_ENDSTOP_APPLY_STEP(A,V) \
if (separate_multi_axis) { \ if (separate_multi_axis) { \
if (A##_HOME_DIR < 0) { \ if (A##_HOME_DIR < 0) { \
if (!(TEST(endstops.state(), A##_MIN) && count_direction[_AXIS(A)] < 0) && !locked_##A##_motor) A##_STEP_WRITE(V); \ if (TERN0(HAS_##A##_MIN, !(TEST(endstops.state(), A##_MIN) && count_direction[_AXIS(A)] < 0) && !locked_##A##_motor)) A##_STEP_WRITE(V); \
if (!(TEST(endstops.state(), A##2_MIN) && count_direction[_AXIS(A)] < 0) && !locked_##A##2_motor) A##2_STEP_WRITE(V); \ if (TERN0(HAS_##A##2_MIN, !(TEST(endstops.state(), A##2_MIN) && count_direction[_AXIS(A)] < 0) && !locked_##A##2_motor)) A##2_STEP_WRITE(V); \
} \ } \
else { \ else { \
if (!(TEST(endstops.state(), A##_MAX) && count_direction[_AXIS(A)] > 0) && !locked_##A##_motor) A##_STEP_WRITE(V); \ if (TERN0(HAS_##A##_MAX, !(TEST(endstops.state(), A##_MAX) && count_direction[_AXIS(A)] > 0) && !locked_##A##_motor)) A##_STEP_WRITE(V); \
if (!(TEST(endstops.state(), A##2_MAX) && count_direction[_AXIS(A)] > 0) && !locked_##A##2_motor) A##2_STEP_WRITE(V); \ if (TERN0(HAS_##A##2_MAX, !(TEST(endstops.state(), A##2_MAX) && count_direction[_AXIS(A)] > 0) && !locked_##A##2_motor)) A##2_STEP_WRITE(V); \
} \ } \
} \ } \
else { \ else { \