Skip to content

Commit 657aa98

Browse files
thinkyheadEvilGremlin
authored andcommitted
πŸ§‘β€πŸ’» Replace axis_bits_t with AxisBits class (MarlinFirmware#25761)
1 parent da8418c commit 657aa98

File tree

9 files changed

+239
-113
lines changed

9 files changed

+239
-113
lines changed

β€ŽMarlin/src/core/types.h

+150-2
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,6 @@ enum AxisEnum : uint8_t {
199199
, ALL_AXES_ENUM = 0xFE, NO_AXIS_ENUM = 0xFF
200200
};
201201

202-
typedef bits_t(NUM_AXIS_ENUMS) axis_bits_t;
203-
204202
//
205203
// Loop over axes
206204
//
@@ -789,6 +787,156 @@ struct XYZEval {
789787
FI bool operator!=(const XYZEval<T> &rs) const { return !operator==(rs); }
790788
};
791789

790+
#include <string.h> // for memset
791+
792+
class AxisBits;
793+
794+
class AxisBits {
795+
public:
796+
typedef bits_t(NUM_AXIS_ENUMS) el;
797+
union {
798+
el bits;
799+
struct {
800+
union {
801+
bool NUM_AXIS_LIST(x:1, y:1, z:1, i:1, j:1, k:1, u:1, v:1, w:1);
802+
bool NUM_AXIS_LIST(X:1, Y:1, Z:1, I:1, J:1, K:1, U:1, V:1, W:1);
803+
bool NUM_AXIS_LIST(a:1, b:1, c:1, _i:1, _j:1, _k:1, _u:1, _v:1, _w:1);
804+
bool NUM_AXIS_LIST(A:1, B:1, C:1, _I:1, _J:1, _K:1, _U:1, _V:1, _W:1);
805+
};
806+
#if HAS_EXTRUDERS
807+
union { bool e:1; bool e0:1; };
808+
#define _EN_ITEM(N) bool e##N:1;
809+
REPEAT_S(1,EXTRUDERS,_EN_ITEM)
810+
#undef _EN_ITEM
811+
#endif
812+
#if ANY(IS_CORE, MARKFORGED_XY, MARKFORGED_YX)
813+
bool hx:1, hy:1, hz:1;
814+
#endif
815+
};
816+
};
817+
818+
AxisBits() { bits = 0; }
819+
820+
// Constructor, setter, and operator= for bit mask
821+
AxisBits(const el p) { set(p); }
822+
void set(const el p) { bits = el(p); }
823+
FI AxisBits& operator=(const el p) { set(p); return *this; }
824+
825+
#define MSET(pE,pX,pY,pZ,pI,pJ,pK,pU,pV,pW) LOGICAL_AXIS_CODE(e=pE, x=pX, y=pY, z=pZ, i=pI, j=pJ, k=pK, u=pU, v=pV, w=pW)
826+
827+
// Constructor, setter, and operator= for XYZE type
828+
AxisBits(const xyze_bool_t &p) { set(p); }
829+
void set(const xyze_bool_t &p) {
830+
MSET(p.e, p.x, p.y, p.z, p.i, p.j, p.k, p.u, p.v, p.w);
831+
}
832+
FI AxisBits& operator=(const xyze_bool_t &p) { set(p); return *this; }
833+
834+
// Constructor, setter, and operator= for bool array
835+
AxisBits(const bool (&p)[LOGICAL_AXES]) { set(p); }
836+
void set(const bool (&p)[LOGICAL_AXES]) {
837+
MSET(p[E_AXIS], p[X_AXIS], p[Y_AXIS], p[Z_AXIS],
838+
p[I_AXIS], p[J_AXIS], p[K_AXIS],
839+
p[U_AXIS], p[V_AXIS], p[W_AXIS]);
840+
}
841+
FI AxisBits& operator=(const bool (&p)[LOGICAL_AXES]) { set(p); return *this; }
842+
843+
// Constructor, setter, and operator= for undersized bool arrays
844+
#if LOGICAL_AXES > 1
845+
AxisBits(const bool (&p)[1]) { set(p); }
846+
FI void set(const bool (&p)[1]) {
847+
MSET(0, p[X_AXIS], 0, 0, 0, 0, 0, 0, 0, 0);
848+
}
849+
FI AxisBits& operator=(const bool (&p)[1]) { set(p); return *this; }
850+
#endif
851+
#if LOGICAL_AXES > 2
852+
AxisBits(const bool (&p)[2]) { set(p); }
853+
FI void set(const bool (&p)[2]) {
854+
MSET(0, p[X_AXIS], p[Y_AXIS], 0, 0, 0, 0, 0, 0, 0);
855+
}
856+
FI AxisBits& operator=(const bool (&p)[2]) { set(p); return *this; }
857+
#endif
858+
#if LOGICAL_AXES > 3
859+
AxisBits(const bool (&p)[3]) { set(p); }
860+
FI void set(const bool (&p)[3]) {
861+
MSET(0, p[X_AXIS], p[Y_AXIS], p[Z_AXIS], 0, 0, 0, 0, 0, 0);
862+
}
863+
FI AxisBits& operator=(const bool (&p)[3]) { set(p); return *this; }
864+
#endif
865+
#if LOGICAL_AXES > 4
866+
AxisBits(const bool (&p)[4]) { set(p); }
867+
FI void set(const bool (&p)[4]) {
868+
MSET(0, p[X_AXIS], p[Y_AXIS], p[Z_AXIS], p[I_AXIS], 0, 0, 0, 0, 0);
869+
}
870+
FI AxisBits& operator=(const bool (&p)[4]) { set(p); return *this; }
871+
#endif
872+
#if LOGICAL_AXES > 5
873+
AxisBits(const bool (&p)[5]) { set(p); }
874+
FI void set(const bool (&p)[5]) {
875+
MSET(0, p[X_AXIS], p[Y_AXIS], p[Z_AXIS], p[I_AXIS], p[J_AXIS], 0, 0, 0, 0);
876+
}
877+
FI AxisBits& operator=(const bool (&p)[5]) { set(p); return *this; }
878+
#endif
879+
#if LOGICAL_AXES > 6
880+
AxisBits(const bool (&p)[6]) { set(p); }
881+
FI void set(const bool (&p)[6]) {
882+
MSET(0, p[X_AXIS], p[Y_AXIS], p[Z_AXIS], p[I_AXIS], p[J_AXIS], p[K_AXIS], 0, 0, 0);
883+
}
884+
FI AxisBits& operator=(const bool (&p)[6]) { set(p); return *this; }
885+
#endif
886+
#if LOGICAL_AXES > 7
887+
AxisBits(const bool (&p)[7]) { set(p); }
888+
FI void set(const bool (&p)[7]) {
889+
MSET(0, p[X_AXIS], p[Y_AXIS], p[Z_AXIS], p[I_AXIS], p[J_AXIS], p[K_AXIS], p[U_AXIS], 0, 0);
890+
}
891+
FI AxisBits& operator=(const bool (&p)[7]) { set(p); return *this; }
892+
#endif
893+
#if LOGICAL_AXES > 8
894+
AxisBits(const bool (&p)[8]) { set(p); }
895+
FI void set(const bool (&p)[8]) {
896+
MSET(0, p[X_AXIS], p[Y_AXIS], p[Z_AXIS], p[I_AXIS], p[J_AXIS], p[K_AXIS], p[U_AXIS], p[V_AXIS], 0);
897+
}
898+
FI AxisBits& operator=(const bool (&p)[8]) { set(p); return *this; }
899+
#endif
900+
#if LOGICAL_AXES > 9
901+
AxisBits(const bool (&p)[9]) { set(p); }
902+
FI void set(const bool (&p)[9]) {
903+
MSET(0, p[X_AXIS], p[Y_AXIS], p[Z_AXIS], p[I_AXIS], p[J_AXIS], p[K_AXIS], p[U_AXIS], p[V_AXIS], p[W_AXIS]);
904+
}
905+
FI AxisBits& operator=(const bool (&p)[9]) { set(p); return *this; }
906+
#endif
907+
#undef MSET
908+
909+
FI const bool toggle(const AxisEnum n) { return TBI(bits, n); }
910+
911+
// Accessor via an AxisEnum (or any integer) [index]
912+
FI const bool operator[](const int n) const { return TEST(bits, n); }
913+
FI const bool operator[](const AxisEnum n) const { return TEST(bits, n); }
914+
915+
FI AxisBits& operator|=(const el &p) { bits |= el(p); return *this; }
916+
FI AxisBits& operator&=(const el &p) { bits &= el(p); return *this; }
917+
FI AxisBits& operator^=(const el &p) { bits ^= el(p); return *this; }
918+
919+
FI AxisBits& operator|=(const AxisBits &p) { bits |= p.bits; return *this; }
920+
FI AxisBits& operator&=(const AxisBits &p) { bits &= p.bits; return *this; }
921+
FI AxisBits& operator^=(const AxisBits &p) { bits ^= p.bits; return *this; }
922+
923+
FI bool operator==(const AxisBits &p) const { return p.bits == bits; }
924+
FI bool operator!=(const AxisBits &p) const { return p.bits != bits; }
925+
926+
FI el operator|(const el &p) const { return bits | el(p); }
927+
FI el operator&(const el &p) const { return bits & el(p); }
928+
FI el operator^(const el &p) const { return bits ^ el(p); }
929+
930+
FI AxisBits operator|(const AxisBits &p) const { return AxisBits(bits | p.bits); }
931+
FI AxisBits operator&(const AxisBits &p) const { return AxisBits(bits & p.bits); }
932+
FI AxisBits operator^(const AxisBits &p) const { return AxisBits(bits ^ p.bits); }
933+
934+
FI operator bool() const { return !!bits; }
935+
FI operator uint16_t() const { return uint16_t(bits & 0xFFFF); }
936+
FI operator uint32_t() const { return uint32_t(bits); }
937+
938+
};
939+
792940
#undef _RECIP
793941
#undef _ABS
794942
#undef _LS

β€ŽMarlin/src/feature/backlash.cpp

+18-18
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include "../module/motion.h"
3030
#include "../module/planner.h"
3131

32-
axis_bits_t Backlash::last_direction_bits;
32+
AxisBits Backlash::last_direction_bits;
3333
xyz_long_t Backlash::residual_error{0};
3434

3535
#ifdef BACKLASH_DISTANCE_MM
@@ -63,25 +63,25 @@ Backlash backlash;
6363
* spread over multiple segments, smoothing out artifacts even more.
6464
*/
6565

66-
void Backlash::add_correction_steps(const int32_t &da, const int32_t &db, const int32_t &dc, const axis_bits_t dm, block_t * const block) {
67-
axis_bits_t changed_dir = last_direction_bits ^ dm;
66+
void Backlash::add_correction_steps(const int32_t &da, const int32_t &db, const int32_t &dc, const AxisBits dm, block_t * const block) {
67+
AxisBits changed_dir = last_direction_bits ^ dm;
6868
// Ignore direction change unless steps are taken in that direction
6969
#if DISABLED(CORE_BACKLASH) || EITHER(MARKFORGED_XY, MARKFORGED_YX)
70-
if (!da) CBI(changed_dir, X_AXIS);
71-
if (!db) CBI(changed_dir, Y_AXIS);
72-
if (!dc) CBI(changed_dir, Z_AXIS);
70+
if (!da) changed_dir.x = false;
71+
if (!db) changed_dir.y = false;
72+
if (!dc) changed_dir.z = false;
7373
#elif CORE_IS_XY
74-
if (!(da + db)) CBI(changed_dir, X_AXIS);
75-
if (!(da - db)) CBI(changed_dir, Y_AXIS);
76-
if (!dc) CBI(changed_dir, Z_AXIS);
74+
if (!(da + db)) changed_dir.x = false;
75+
if (!(da - db)) changed_dir.y = false;
76+
if (!dc) changed_dir.z = false;
7777
#elif CORE_IS_XZ
78-
if (!(da + dc)) CBI(changed_dir, X_AXIS);
79-
if (!(da - dc)) CBI(changed_dir, Z_AXIS);
80-
if (!db) CBI(changed_dir, Y_AXIS);
78+
if (!(da + dc)) changed_dir.x = false;
79+
if (!(da - dc)) changed_dir.z = false;
80+
if (!db) changed_dir.y = false;
8181
#elif CORE_IS_YZ
82-
if (!(db + dc)) CBI(changed_dir, Y_AXIS);
83-
if (!(db - dc)) CBI(changed_dir, Z_AXIS);
84-
if (!da) CBI(changed_dir, X_AXIS);
82+
if (!(db + dc)) changed_dir.y = false;
83+
if (!(db - dc)) changed_dir.z = false;
84+
if (!da) changed_dir.x = false;
8585
#endif
8686
last_direction_bits ^= changed_dir;
8787

@@ -99,10 +99,10 @@ void Backlash::add_correction_steps(const int32_t &da, const int32_t &db, const
9999

100100
LOOP_NUM_AXES(axis) {
101101
if (distance_mm[axis]) {
102-
const bool reverse = TEST(dm, axis);
102+
const bool reverse = dm[axis];
103103

104104
// When an axis changes direction, add axis backlash to the residual error
105-
if (TEST(changed_dir, axis))
105+
if (changed_dir[axis])
106106
residual_error[axis] += (reverse ? -f_corr : f_corr) * distance_mm[axis] * planner.settings.axis_steps_per_mm[axis];
107107

108108
// Decide how much of the residual error to correct in this segment
@@ -147,7 +147,7 @@ void Backlash::add_correction_steps(const int32_t &da, const int32_t &db, const
147147
int32_t Backlash::get_applied_steps(const AxisEnum axis) {
148148
if (axis >= NUM_AXES) return 0;
149149

150-
const bool reverse = TEST(last_direction_bits, axis);
150+
const bool reverse = last_direction_bits[axis];
151151

152152
const int32_t residual_error_axis = residual_error[axis];
153153

β€ŽMarlin/src/feature/backlash.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Backlash {
2929
static constexpr uint8_t all_on = 0xFF, all_off = 0x00;
3030

3131
private:
32-
static axis_bits_t last_direction_bits;
32+
static AxisBits last_direction_bits;
3333
static xyz_long_t residual_error;
3434

3535
#if ENABLED(BACKLASH_GCODE)
@@ -72,7 +72,7 @@ class Backlash {
7272
return has_measurement(X_AXIS) || has_measurement(Y_AXIS) || has_measurement(Z_AXIS);
7373
}
7474

75-
static void add_correction_steps(const int32_t &da, const int32_t &db, const int32_t &dc, const axis_bits_t dm, block_t * const block);
75+
static void add_correction_steps(const int32_t &da, const int32_t &db, const int32_t &dc, const AxisBits dm, block_t * const block);
7676
static int32_t get_applied_steps(const AxisEnum axis);
7777

7878
#if ENABLED(BACKLASH_GCODE)

β€ŽMarlin/src/feature/runout.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ class FilamentSensorBase {
411411
// Only trigger on extrusion with XYZ movement to allow filament change and retract/recover.
412412
const uint8_t e = b->extruder;
413413
const int32_t steps = b->steps.e;
414-
const float mm = (TEST(b->direction_bits, E_AXIS) ? -steps : steps) * planner.mm_per_step[E_AXIS_N(e)];
414+
const float mm = (b->direction_bits.e ? -steps : steps) * planner.mm_per_step[E_AXIS_N(e)];
415415
if (e < NUM_RUNOUT_SENSORS) mm_countdown.runout[e] -= mm;
416416
#if ENABLED(FILAMENT_SWITCH_AND_MOTION)
417417
if (e < NUM_MOTION_SENSORS) mm_countdown.motion[e] -= mm;

β€ŽMarlin/src/module/ft_motion.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -484,33 +484,33 @@ void FxdTiCtrl::loadBlockData(block_t * const current_block) {
484484
const float totalLength = current_block->millimeters,
485485
oneOverLength = 1.0f / totalLength;
486486

487-
const axis_bits_t direction = current_block->direction_bits;
487+
const AxisBits direction = current_block->direction_bits;
488488

489489
#if HAS_X_AXIS
490490
x_startPosn = x_endPosn_prevBlock;
491491
float x_moveDist = current_block->steps.a / planner.settings.axis_steps_per_mm[X_AXIS];
492-
if (TEST(direction, X_AXIS)) x_moveDist *= -1.0f;
492+
if (direction.x) x_moveDist *= -1.0f;
493493
x_Ratio = x_moveDist * oneOverLength;
494494
#endif
495495

496496
#if HAS_Y_AXIS
497497
y_startPosn = y_endPosn_prevBlock;
498498
float y_moveDist = current_block->steps.b / planner.settings.axis_steps_per_mm[Y_AXIS];
499-
if (TEST(direction, Y_AXIS)) y_moveDist *= -1.0f;
499+
if (direction.y) y_moveDist *= -1.0f;
500500
y_Ratio = y_moveDist * oneOverLength;
501501
#endif
502502

503503
#if HAS_Z_AXIS
504504
z_startPosn = z_endPosn_prevBlock;
505505
float z_moveDist = current_block->steps.c / planner.settings.axis_steps_per_mm[Z_AXIS];
506-
if (TEST(direction, Z_AXIS)) z_moveDist *= -1.0f;
506+
if (direction.z) z_moveDist *= -1.0f;
507507
z_Ratio = z_moveDist * oneOverLength;
508508
#endif
509509

510510
#if HAS_EXTRUDERS
511511
e_startPosn = e_endPosn_prevBlock;
512512
float extrusion = current_block->steps.e / planner.settings.axis_steps_per_mm[E_AXIS_N(current_block->extruder)];
513-
if (TEST(direction, E_AXIS_N(current_block->extruder))) extrusion *= -1.0f;
513+
if (direction.e) extrusion *= -1.0f;
514514
e_Ratio = extrusion * oneOverLength;
515515
#endif
516516

β€ŽMarlin/src/module/planner.cpp

+28-32
Original file line numberDiff line numberDiff line change
@@ -1968,54 +1968,50 @@ bool Planner::_populate_block(
19681968
#endif // PREVENT_COLD_EXTRUSION || PREVENT_LENGTHY_EXTRUDE
19691969

19701970
// Compute direction bit-mask for this block
1971-
axis_bits_t dm = 0;
1971+
AxisBits dm;
19721972
#if ANY(CORE_IS_XY, MARKFORGED_XY, MARKFORGED_YX)
1973-
if (da < 0) SBI(dm, X_HEAD); // Save the toolhead's true direction in X
1974-
if (db < 0) SBI(dm, Y_HEAD); // ...and Y
1975-
TERN_(HAS_Z_AXIS, if (dc < 0) SBI(dm, Z_AXIS));
1973+
dm.hx = (da < 0); // Save the toolhead's true direction in X
1974+
dm.hy = (db < 0); // ...and Y
1975+
TERN_(HAS_Z_AXIS, dm.z = (dc < 0));
19761976
#endif
19771977
#if IS_CORE
19781978
#if CORE_IS_XY
1979-
if (da + db < 0) SBI(dm, A_AXIS); // Motor A direction
1980-
if (CORESIGN(da - db) < 0) SBI(dm, B_AXIS); // Motor B direction
1979+
dm.a = (da + db < 0); // Motor A direction
1980+
dm.b = (CORESIGN(da - db) < 0); // Motor B direction
19811981
#elif CORE_IS_XZ
1982-
if (da < 0) SBI(dm, X_HEAD); // Save the toolhead's true direction in X
1983-
if (db < 0) SBI(dm, Y_AXIS);
1984-
if (dc < 0) SBI(dm, Z_HEAD); // ...and Z
1985-
if (da + dc < 0) SBI(dm, A_AXIS); // Motor A direction
1986-
if (CORESIGN(da - dc) < 0) SBI(dm, C_AXIS); // Motor C direction
1982+
dm.hx = (da < 0); // Save the toolhead's true direction in X
1983+
dm.y = (db < 0);
1984+
dm.hz = (dc < 0); // ...and Z
1985+
dm.a = (da + dc < 0); // Motor A direction
1986+
dm.c = (CORESIGN(da - dc) < 0); // Motor C direction
19871987
#elif CORE_IS_YZ
1988-
if (da < 0) SBI(dm, X_AXIS);
1989-
if (db < 0) SBI(dm, Y_HEAD); // Save the toolhead's true direction in Y
1990-
if (dc < 0) SBI(dm, Z_HEAD); // ...and Z
1991-
if (db + dc < 0) SBI(dm, B_AXIS); // Motor B direction
1992-
if (CORESIGN(db - dc) < 0) SBI(dm, C_AXIS); // Motor C direction
1988+
dm.x = (da < 0);
1989+
dm.hy = (db < 0); // Save the toolhead's true direction in Y
1990+
dm.hz = (dc < 0); // ...and Z
1991+
dm.b = (db + dc < 0); // Motor B direction
1992+
dm.c = (CORESIGN(db - dc) < 0); // Motor C direction
19931993
#endif
19941994
#elif ENABLED(MARKFORGED_XY)
1995-
if (da + db < 0) SBI(dm, A_AXIS); // Motor A direction
1996-
if (db < 0) SBI(dm, B_AXIS); // Motor B direction
1995+
dm.a = (da + db < 0); // Motor A direction
1996+
dm.b = (db < 0); // Motor B direction
19971997
#elif ENABLED(MARKFORGED_YX)
1998-
if (da < 0) SBI(dm, A_AXIS); // Motor A direction
1999-
if (db + da < 0) SBI(dm, B_AXIS); // Motor B direction
1998+
dm.a = (da < 0); // Motor A direction
1999+
dm.b = (db + da < 0); // Motor B direction
20002000
#else
20012001
XYZ_CODE(
2002-
if (da < 0) SBI(dm, X_AXIS),
2003-
if (db < 0) SBI(dm, Y_AXIS),
2004-
if (dc < 0) SBI(dm, Z_AXIS)
2002+
dm.x = (da < 0),
2003+
dm.y = (db < 0),
2004+
dm.z = (dc < 0)
20052005
);
20062006
#endif
20072007

20082008
SECONDARY_AXIS_CODE(
2009-
if (di < 0) SBI(dm, I_AXIS),
2010-
if (dj < 0) SBI(dm, J_AXIS),
2011-
if (dk < 0) SBI(dm, K_AXIS),
2012-
if (du < 0) SBI(dm, U_AXIS),
2013-
if (dv < 0) SBI(dm, V_AXIS),
2014-
if (dw < 0) SBI(dm, W_AXIS)
2009+
dm.i = (di < 0), dm.j = (dj < 0), dm.k = (dk < 0),
2010+
dm.u = (du < 0), dm.v = (dv < 0), dm.w = (dw < 0)
20152011
);
20162012

20172013
#if HAS_EXTRUDERS
2018-
if (de < 0) SBI(dm, E_AXIS);
2014+
dm.e = (de < 0);
20192015
const float esteps_float = de * e_factor[extruder];
20202016
const uint32_t esteps = ABS(esteps_float) + 0.5f;
20212017
#else
@@ -2435,11 +2431,11 @@ bool Planner::_populate_block(
24352431

24362432
#ifdef XY_FREQUENCY_LIMIT
24372433

2438-
static axis_bits_t old_direction_bits; // = 0
2434+
static AxisBits old_direction_bits; // = 0
24392435

24402436
if (xy_freq_limit_hz) {
24412437
// Check and limit the xy direction change frequency
2442-
const axis_bits_t direction_change = block->direction_bits ^ old_direction_bits;
2438+
const AxisBits direction_change = block->direction_bits ^ old_direction_bits;
24432439
old_direction_bits = block->direction_bits;
24442440
segment_time_us = LROUND(float(segment_time_us) / speed_factor);
24452441

0 commit comments

Comments
Β (0)