Skip to content

Commit 894c954

Browse files
committed
♻️ Minimize endstop bits
1 parent 046bac6 commit 894c954

File tree

3 files changed

+68
-44
lines changed

3 files changed

+68
-44
lines changed

Marlin/src/module/endstops.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ Endstops endstops;
5656
// private:
5757

5858
bool Endstops::enabled, Endstops::enabled_globally; // Initialized by settings.load()
59-
volatile uint8_t Endstops::hit_state;
6059

61-
Endstops::esbits_t Endstops::live_state = 0;
60+
volatile Endstops::endstop_mask_t Endstops::hit_state;
61+
Endstops::endstop_mask_t Endstops::live_state = 0;
6262

6363
#if ENDSTOP_NOISE_THRESHOLD
64-
Endstops::esbits_t Endstops::validated_live_state;
64+
Endstops::endstop_mask_t Endstops::validated_live_state;
6565
uint8_t Endstops::endstop_poll_count;
6666
#endif
6767

@@ -356,23 +356,22 @@ void Endstops::resync() {
356356
#endif
357357

358358
void Endstops::event_handler() {
359-
static uint8_t prev_hit_state; // = 0
359+
static endstop_mask_t prev_hit_state; // = 0
360360
if (hit_state == prev_hit_state) return;
361361
prev_hit_state = hit_state;
362362
if (hit_state) {
363363
#if HAS_STATUS_MESSAGE
364364
char chrX = ' ', chrY = ' ', chrZ = ' ', chrP = ' ';
365365
#define _SET_STOP_CHAR(A,C) (chr## A = C)
366366
#else
367-
#define _SET_STOP_CHAR(A,C) ;
367+
#define _SET_STOP_CHAR(A,C) NOOP
368368
#endif
369369

370370
#define _ENDSTOP_HIT_ECHO(A,C) do{ \
371-
SERIAL_ECHOPAIR(" " STRINGIFY(A) ":", planner.triggered_position_mm(_AXIS(A))); \
372-
_SET_STOP_CHAR(A,C); }while(0)
371+
SERIAL_ECHOPAIR(" " STRINGIFY(A) ":", planner.triggered_position_mm(_AXIS(A))); _SET_STOP_CHAR(A,C); }while(0)
373372

374373
#define _ENDSTOP_HIT_TEST(A,C) \
375-
if (TEST(hit_state, A ##_MIN) || TEST(hit_state, A ##_MAX)) \
374+
if (TERN0(HAS_##A##_MIN, TEST(hit_state, A##_MIN)) || TERN0(HAS_##A##_MAX, TEST(hit_state, A##_MAX))) \
376375
_ENDSTOP_HIT_ECHO(A,C)
377376

378377
#define ENDSTOP_HIT_TEST_X() _ENDSTOP_HIT_TEST(X,'X')
@@ -659,7 +658,7 @@ void Endstops::update() {
659658
* still exist. The only way to reduce them further is to increase the number of samples.
660659
* To reduce the chance to 1% (1/128th) requires 7 samples (adding 7ms of delay).
661660
*/
662-
static esbits_t old_live_state;
661+
static endstop_mask_t old_live_state;
663662
if (old_live_state != live_state) {
664663
endstop_poll_count = ENDSTOP_NOISE_THRESHOLD;
665664
old_live_state = live_state;

Marlin/src/module/endstops.h

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,50 +28,75 @@
2828
#include "../inc/MarlinConfig.h"
2929
#include <stdint.h>
3030

31+
#define __ES_ITEM(N) N,
32+
#define _ES_ITEM(K,N) TERN_(K,DEFER4(__ES_ITEM)(N))
33+
3134
enum EndstopEnum : char {
32-
X_MIN, Y_MIN, Z_MIN, Z_MIN_PROBE,
33-
X_MAX, Y_MAX, Z_MAX,
34-
X2_MIN, X2_MAX,
35-
Y2_MIN, Y2_MAX,
36-
Z2_MIN, Z2_MAX,
37-
Z3_MIN, Z3_MAX,
38-
Z4_MIN, Z4_MAX
35+
_ES_ITEM(HAS_X_MIN, X_MIN)
36+
_ES_ITEM(HAS_X_MAX, X_MAX)
37+
_ES_ITEM(HAS_Y_MIN, Y_MIN)
38+
_ES_ITEM(HAS_Y_MAX, Y_MAX)
39+
_ES_ITEM(HAS_Z_MIN, Z_MIN)
40+
_ES_ITEM(HAS_Z_MAX, Z_MAX)
41+
#if ENABLED(X_DUAL_ENDSTOPS)
42+
_ES_ITEM(HAS_X_MIN, X2_MIN)
43+
_ES_ITEM(HAS_X_MAX, X2_MAX)
44+
#endif
45+
#if ENABLED(Y_DUAL_ENDSTOPS)
46+
_ES_ITEM(HAS_Y_MIN, Y2_MIN)
47+
_ES_ITEM(HAS_Y_MAX, Y2_MAX)
48+
#endif
49+
#if ENABLED(Z_MULTI_ENDSTOPS)
50+
_ES_ITEM(HAS_Z_MIN, Z2_MIN)
51+
_ES_ITEM(HAS_Z_MAX, Z2_MAX)
52+
#if NUM_Z_STEPPER_DRIVERS >= 3
53+
_ES_ITEM(HAS_Z_MIN, Z3_MIN)
54+
_ES_ITEM(HAS_Z_MAX, Z3_MAX)
55+
#endif
56+
#if NUM_Z_STEPPER_DRIVERS >= 4
57+
_ES_ITEM(HAS_Z_MIN, Z4_MIN)
58+
_ES_ITEM(HAS_Z_MAX, Z4_MAX)
59+
#endif
60+
#endif
61+
_ES_ITEM(HAS_Z_MIN_PROBE_PIN, Z_MIN_PROBE)
62+
NUM_ENDSTOP_STATES
3963
};
4064

4165
#define X_ENDSTOP (x_home_dir(active_extruder) < 0 ? X_MIN : X_MAX)
4266
#define Y_ENDSTOP (Y_HOME_DIR < 0 ? Y_MIN : Y_MAX)
4367
#define Z_ENDSTOP (Z_HOME_DIR < 0 ? TERN(HOMING_Z_WITH_PROBE, Z_MIN, Z_MIN_PROBE) : Z_MAX)
4468

69+
#undef __ES_ITEM
70+
#undef _ES_ITEM
71+
4572
class Endstops {
4673
public:
47-
#if HAS_EXTRA_ENDSTOPS
48-
typedef uint16_t esbits_t;
49-
#if ENABLED(X_DUAL_ENDSTOPS)
50-
static float x2_endstop_adj;
51-
#endif
52-
#if ENABLED(Y_DUAL_ENDSTOPS)
53-
static float y2_endstop_adj;
54-
#endif
55-
#if ENABLED(Z_MULTI_ENDSTOPS)
56-
static float z2_endstop_adj;
57-
#endif
58-
#if ENABLED(Z_MULTI_ENDSTOPS) && NUM_Z_STEPPER_DRIVERS >= 3
59-
static float z3_endstop_adj;
60-
#endif
61-
#if ENABLED(Z_MULTI_ENDSTOPS) && NUM_Z_STEPPER_DRIVERS >= 4
62-
static float z4_endstop_adj;
63-
#endif
64-
#else
65-
typedef uint8_t esbits_t;
74+
75+
typedef IF<(NUM_ENDSTOP_STATES > 8), uint16_t, uint8_t>::type endstop_mask_t;
76+
77+
#if ENABLED(X_DUAL_ENDSTOPS)
78+
static float x2_endstop_adj;
79+
#endif
80+
#if ENABLED(Y_DUAL_ENDSTOPS)
81+
static float y2_endstop_adj;
82+
#endif
83+
#if ENABLED(Z_MULTI_ENDSTOPS)
84+
static float z2_endstop_adj;
85+
#endif
86+
#if ENABLED(Z_MULTI_ENDSTOPS) && NUM_Z_STEPPER_DRIVERS >= 3
87+
static float z3_endstop_adj;
88+
#endif
89+
#if ENABLED(Z_MULTI_ENDSTOPS) && NUM_Z_STEPPER_DRIVERS >= 4
90+
static float z4_endstop_adj;
6691
#endif
6792

6893
private:
6994
static bool enabled, enabled_globally;
70-
static esbits_t live_state;
71-
static volatile uint8_t hit_state; // Use X_MIN, Y_MIN, Z_MIN and Z_MIN_PROBE as BIT index
95+
static endstop_mask_t live_state;
96+
static volatile endstop_mask_t hit_state; // Use X_MIN, Y_MIN, Z_MIN and Z_MIN_PROBE as BIT index
7297

7398
#if ENDSTOP_NOISE_THRESHOLD
74-
static esbits_t validated_live_state;
99+
static endstop_mask_t validated_live_state;
75100
static uint8_t endstop_poll_count; // Countdown from threshold for polling
76101
#endif
77102

@@ -107,12 +132,12 @@ class Endstops {
107132
/**
108133
* Get Endstop hit state.
109134
*/
110-
FORCE_INLINE static uint8_t trigger_state() { return hit_state; }
135+
FORCE_INLINE static endstop_mask_t trigger_state() { return hit_state; }
111136

112137
/**
113138
* Get current endstops state
114139
*/
115-
FORCE_INLINE static esbits_t state() {
140+
FORCE_INLINE static endstop_mask_t state() {
116141
return
117142
#if ENDSTOP_NOISE_THRESHOLD
118143
validated_live_state

Marlin/src/module/stepper.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,12 @@ xyze_int8_t Stepper::count_direction{0};
260260
#define DUAL_ENDSTOP_APPLY_STEP(A,V) \
261261
if (separate_multi_axis) { \
262262
if (A##_HOME_DIR < 0) { \
263-
if (!(TEST(endstops.state(), A##_MIN) && count_direction[_AXIS(A)] < 0) && !locked_##A##_motor) A##_STEP_WRITE(V); \
264-
if (!(TEST(endstops.state(), A##2_MIN) && count_direction[_AXIS(A)] < 0) && !locked_##A##2_motor) A##2_STEP_WRITE(V); \
263+
if (TERN0(HAS_##A##_MIN, !(TEST(endstops.state(), A##_MIN) && count_direction[_AXIS(A)] < 0) && !locked_##A##_motor)) A##_STEP_WRITE(V); \
264+
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); \
265265
} \
266266
else { \
267-
if (!(TEST(endstops.state(), A##_MAX) && count_direction[_AXIS(A)] > 0) && !locked_##A##_motor) A##_STEP_WRITE(V); \
268-
if (!(TEST(endstops.state(), A##2_MAX) && count_direction[_AXIS(A)] > 0) && !locked_##A##2_motor) A##2_STEP_WRITE(V); \
267+
if (TERN0(HAS_##A##_MAX, !(TEST(endstops.state(), A##_MAX) && count_direction[_AXIS(A)] > 0) && !locked_##A##_motor)) A##_STEP_WRITE(V); \
268+
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); \
269269
} \
270270
} \
271271
else { \

0 commit comments

Comments
 (0)