Browse Source

♻️ Refactor axis homing/trusted state bits

Scott Lahteine 4 years ago
parent
commit
458677c63a
2 changed files with 22 additions and 19 deletions
  1. 11
    9
      Marlin/src/module/motion.cpp
  2. 11
    10
      Marlin/src/module/motion.h

+ 11
- 9
Marlin/src/module/motion.cpp View File

@@ -1181,18 +1181,20 @@ void prepare_line_to_destination() {
1181 1181
 
1182 1182
 #if HAS_ENDSTOPS
1183 1183
 
1184
-  uint8_t axis_homed, axis_trusted; // = 0
1185
-
1186
-  uint8_t axes_should_home(uint8_t axis_bits/*=0x07*/) {
1187
-    #define SHOULD_HOME(A) TERN(HOME_AFTER_DEACTIVATE, axis_is_trusted, axis_was_homed)(A)
1188
-    // Clear test bits that are trusted
1189
-    if (TEST(axis_bits, X_AXIS) && SHOULD_HOME(X_AXIS)) CBI(axis_bits, X_AXIS);
1190
-    if (TEST(axis_bits, Y_AXIS) && SHOULD_HOME(Y_AXIS)) CBI(axis_bits, Y_AXIS);
1191
-    if (TEST(axis_bits, Z_AXIS) && SHOULD_HOME(Z_AXIS)) CBI(axis_bits, Z_AXIS);
1184
+  linear_axis_bits_t axis_homed, axis_trusted; // = 0
1185
+
1186
+  linear_axis_bits_t axes_should_home(linear_axis_bits_t axis_bits/*=linear_bits*/) {
1187
+    auto set_should = [](linear_axis_bits_t &b, AxisEnum a) {
1188
+      if (TEST(b, a) && TERN(HOME_AFTER_DEACTIVATE, axis_is_trusted, axis_was_homed)(a))
1189
+        CBI(b, a);
1190
+    };
1191
+    set_should(axis_bits, X_AXIS);  // Clear test bits that are trusted
1192
+    set_should(axis_bits, Y_AXIS);
1193
+    set_should(axis_bits, Z_AXIS);
1192 1194
     return axis_bits;
1193 1195
   }
1194 1196
 
1195
-  bool homing_needed_error(uint8_t axis_bits/*=0x07*/) {
1197
+  bool homing_needed_error(linear_axis_bits_t axis_bits/*=linear_bits*/) {
1196 1198
     if ((axis_bits = axes_should_home(axis_bits))) {
1197 1199
       PGM_P home_first = GET_TEXT(MSG_HOME_FIRST);
1198 1200
       char msg[strlen_P(home_first)+1];

+ 11
- 10
Marlin/src/module/motion.h View File

@@ -326,7 +326,8 @@ void do_z_clearance(const_float_t zclear, const bool lower_allowed=false);
326 326
 /**
327 327
  * Homing and Trusted Axes
328 328
  */
329
-constexpr uint8_t xyz_bits = _BV(X_AXIS) | _BV(Y_AXIS) | _BV(Z_AXIS);
329
+typedef IF<(LINEAR_AXES>8), uint16_t, uint8_t>::type linear_axis_bits_t;
330
+constexpr linear_axis_bits_t linear_bits = _BV(LINEAR_AXES) - 1;
330 331
 
331 332
 void set_axis_is_at_home(const AxisEnum axis);
332 333
 
@@ -340,23 +341,23 @@ void set_axis_is_at_home(const AxisEnum axis);
340 341
    *   Flags that the position is trusted in each linear axis. Set when homed.
341 342
    *   Cleared whenever a stepper powers off, potentially losing its position.
342 343
    */
343
-  extern uint8_t axis_homed, axis_trusted;
344
+  extern linear_axis_bits_t axis_homed, axis_trusted;
344 345
   void homeaxis(const AxisEnum axis);
345 346
   void set_axis_never_homed(const AxisEnum axis);
346
-  uint8_t axes_should_home(uint8_t axis_bits=0x07);
347
-  bool homing_needed_error(uint8_t axis_bits=0x07);
347
+  linear_axis_bits_t axes_should_home(linear_axis_bits_t axis_bits=linear_bits);
348
+  bool homing_needed_error(linear_axis_bits_t axis_bits=linear_bits);
348 349
   FORCE_INLINE void set_axis_unhomed(const AxisEnum axis)   { CBI(axis_homed, axis); }
349 350
   FORCE_INLINE void set_axis_untrusted(const AxisEnum axis) { CBI(axis_trusted, axis); }
350 351
   FORCE_INLINE void set_all_unhomed()                       { axis_homed = axis_trusted = 0; }
351 352
   FORCE_INLINE void set_axis_homed(const AxisEnum axis)     { SBI(axis_homed, axis); }
352 353
   FORCE_INLINE void set_axis_trusted(const AxisEnum axis)   { SBI(axis_trusted, axis); }
353
-  FORCE_INLINE void set_all_homed()                         { axis_homed = axis_trusted = xyz_bits; }
354
+  FORCE_INLINE void set_all_homed()                         { axis_homed = axis_trusted = linear_bits; }
354 355
 #else
355
-  constexpr uint8_t axis_homed = xyz_bits, axis_trusted = xyz_bits; // Zero-endstop machines are always homed and trusted
356
+  constexpr linear_axis_bits_t axis_homed = linear_bits, axis_trusted = linear_bits; // Zero-endstop machines are always homed and trusted
356 357
   FORCE_INLINE void homeaxis(const AxisEnum axis)           {}
357 358
   FORCE_INLINE void set_axis_never_homed(const AxisEnum)    {}
358
-  FORCE_INLINE uint8_t axes_should_home(uint8_t=0x07)       { return false; }
359
-  FORCE_INLINE bool homing_needed_error(uint8_t=0x07)       { return false; }
359
+  FORCE_INLINE linear_axis_bits_t axes_should_home(linear_axis_bits_t=linear_bits) { return false; }
360
+  FORCE_INLINE bool homing_needed_error(linear_axis_bits_t=linear_bits) { return false; }
360 361
   FORCE_INLINE void set_axis_unhomed(const AxisEnum axis)   {}
361 362
   FORCE_INLINE void set_axis_untrusted(const AxisEnum axis) {}
362 363
   FORCE_INLINE void set_all_unhomed()                       {}
@@ -369,9 +370,9 @@ FORCE_INLINE bool axis_was_homed(const AxisEnum axis)       { return TEST(axis_h
369 370
 FORCE_INLINE bool axis_is_trusted(const AxisEnum axis)      { return TEST(axis_trusted, axis); }
370 371
 FORCE_INLINE bool axis_should_home(const AxisEnum axis)     { return (axes_should_home() & _BV(axis)) != 0; }
371 372
 FORCE_INLINE bool no_axes_homed()                           { return !axis_homed; }
372
-FORCE_INLINE bool all_axes_homed()                          { return xyz_bits == (axis_homed & xyz_bits); }
373
+FORCE_INLINE bool all_axes_homed()                          { return linear_bits == (axis_homed & linear_bits); }
373 374
 FORCE_INLINE bool homing_needed()                           { return !all_axes_homed(); }
374
-FORCE_INLINE bool all_axes_trusted()                        { return xyz_bits == (axis_trusted & xyz_bits); }
375
+FORCE_INLINE bool all_axes_trusted()                        { return linear_bits == (axis_trusted & linear_bits); }
375 376
 
376 377
 #if ENABLED(NO_MOTION_BEFORE_HOMING)
377 378
   #define MOTION_CONDITIONS (IsRunning() && !homing_needed_error())

Loading…
Cancel
Save