Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧑‍💻 General and Axis-based bitfield flags #23989

Merged
merged 2 commits into from
Apr 3, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
🧑‍💻 General and Axis-based bitfield flags
thinkyhead committed Apr 3, 2022
commit 159bff063fb690ed2f8f727e5c3d7d72c0ebfce2
53 changes: 53 additions & 0 deletions Marlin/src/core/types.h
Original file line number Diff line number Diff line change
@@ -76,6 +76,59 @@ struct IF<true, L, R> { typedef L type; };

#define AXIS_COLLISION(L) (AXIS4_NAME == L || AXIS5_NAME == L || AXIS6_NAME == L || AXIS7_NAME == L || AXIS8_NAME == L || AXIS9_NAME == L)

// General Flags for some number of states
template<size_t N>
struct Flags {
typedef typename IF<(N>8), uint16_t, uint8_t>::type bits_t;
typedef struct { bool b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1; } N8;
typedef struct { bool b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1, b8:1, b9:1, b10:1, b11:1, b12:1, b13:1, b14:1, b15:1; } N16;
union {
bits_t b;
typename IF<(N>8), N16, N8>::type flag;
};
void reset() { b = 0; }
void set(const int n, const bool onoff) { onoff ? set(n) : clear(n); }
void set(const int n) { b |= (bits_t)_BV(n); }
void clear(const int n) { b &= ~(bits_t)_BV(n); }
bool test(const int n) const { return TEST(b, n); }
bool operator[](const int n) { return test(n); }
const bool operator[](const int n) const { return test(n); }
const int size() const { return sizeof(b); }
};

// Specialization for a single bool flag
template<>
struct Flags<1> {
bool b;
void reset() { b = false; }
void set(const int n, const bool onoff) { onoff ? set(n) : clear(n); }
void set(const int) { b = true; }
void clear(const int) { b = false; }
bool test(const int) const { return b; }
bool operator[](const int) { return b; }
const bool operator[](const int) const { return b; }
const int size() const { return sizeof(b); }
};

typedef Flags<8> flags_8_t;
typedef Flags<16> flags_16_t;

// Flags for some axis states, with per-axis aliases xyzijkuvwe
typedef struct AxisFlags {
union {
struct Flags<LOGICAL_AXES> flags;
struct { bool LOGICAL_AXIS_LIST(e:1, x:1, y:1, z:1, i:1, j:1, k:1, u:1, v:1, w:1); };
};
void reset() { flags.reset(); }
void set(const int n) { flags.set(n); }
void set(const int n, const bool onoff) { flags.set(n, onoff); }
void clear(const int n) { flags.clear(n); }
bool test(const int n) const { return flags.test(n); }
bool operator[](const int n) { return flags[n]; }
const bool operator[](const int n) const { return flags[n]; }
const int size() const { return sizeof(flags); }
} axis_flags_t;

//
// Enumerated axis indices
//
4 changes: 2 additions & 2 deletions Marlin/src/feature/fancheck.cpp
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@
#if HAS_AUTO_FAN && EXTRUDER_AUTO_FAN_SPEED != 255 && DISABLED(FOURWIRES_FANS)
bool FanCheck::measuring = false;
#endif
bool FanCheck::tacho_state[TACHO_COUNT];
Flags<TACHO_COUNT> FanCheck::tacho_state;
uint16_t FanCheck::edge_counter[TACHO_COUNT];
uint8_t FanCheck::rps[TACHO_COUNT];
FanCheck::TachoError FanCheck::error = FanCheck::TachoError::NONE;
@@ -103,7 +103,7 @@ void FanCheck::update_tachometers() {

if (status != tacho_state[f]) {
if (measuring) ++edge_counter[f];
tacho_state[f] = status;
tacho_state.set(f, status);
}
}
}
2 changes: 1 addition & 1 deletion Marlin/src/feature/fancheck.h
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@ class FanCheck {
#else
static constexpr bool measuring = true;
#endif
static bool tacho_state[TACHO_COUNT];
static Flags<TACHO_COUNT> tacho_state;
static uint16_t edge_counter[TACHO_COUNT];
static uint8_t rps[TACHO_COUNT];
static TachoError error;
12 changes: 6 additions & 6 deletions Marlin/src/feature/fwretract.cpp
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ FWRetract fwretract; // Single instance - this calls the constructor
// private:

#if HAS_MULTI_EXTRUDER
bool FWRetract::retracted_swap[EXTRUDERS]; // Which extruders are swap-retracted
Flags<EXTRUDERS> FWRetract::retracted_swap; // Which extruders are swap-retracted
#endif

// public:
@@ -56,7 +56,7 @@ fwretract_settings_t FWRetract::settings; // M207 S F Z W, M208 S F
bool FWRetract::autoretract_enabled; // M209 S - Autoretract switch
#endif

bool FWRetract::retracted[EXTRUDERS]; // Which extruders are currently retracted
Flags<EXTRUDERS> FWRetract::retracted; // Which extruders are currently retracted

float FWRetract::current_retract[EXTRUDERS], // Retract value used by planner
FWRetract::current_hop;
@@ -73,9 +73,9 @@ void FWRetract::reset() {
settings.swap_retract_recover_feedrate_mm_s = RETRACT_RECOVER_FEEDRATE_SWAP;
current_hop = 0.0;

retracted.reset();
EXTRUDER_LOOP() {
retracted[e] = false;
E_TERN_(retracted_swap[e] = false);
E_TERN_(retracted_swap.clear(e));
current_retract[e] = 0.0;
}
}
@@ -173,11 +173,11 @@ void FWRetract::retract(const bool retracting E_OPTARG(bool swapping/*=false*/))

TERN_(RETRACT_SYNC_MIXING, mixer.T(old_mixing_tool)); // Restore original mixing tool

retracted[active_extruder] = retracting; // Active extruder now retracted / recovered
retracted.set(active_extruder, retracting); // Active extruder now retracted / recovered

// If swap retract/recover update the retracted_swap flag too
#if HAS_MULTI_EXTRUDER
if (swapping) retracted_swap[active_extruder] = retracting;
if (swapping) retracted_swap.set(active_extruder, retracting);
#endif

/* // debugging
8 changes: 3 additions & 5 deletions Marlin/src/feature/fwretract.h
Original file line number Diff line number Diff line change
@@ -43,7 +43,7 @@ typedef struct {
class FWRetract {
private:
#if HAS_MULTI_EXTRUDER
static bool retracted_swap[EXTRUDERS]; // Which extruders are swap-retracted
static Flags<EXTRUDERS> retracted_swap; // Which extruders are swap-retracted
#endif

public:
@@ -55,17 +55,15 @@ class FWRetract {
static constexpr bool autoretract_enabled = false;
#endif

static bool retracted[EXTRUDERS]; // Which extruders are currently retracted
static Flags<EXTRUDERS> retracted; // Which extruders are currently retracted
static float current_retract[EXTRUDERS], // Retract value used by planner
current_hop; // Hop value used by planner

FWRetract() { reset(); }

static void reset();

static void refresh_autoretract() {
EXTRUDER_LOOP() retracted[e] = false;
}
static void refresh_autoretract() { retracted.reset(); }

static void enable_autoretract(const bool enable) {
#if ENABLED(FWRETRACT_AUTORETRACT)
2 changes: 1 addition & 1 deletion Marlin/src/feature/powerloss.cpp
Original file line number Diff line number Diff line change
@@ -514,7 +514,7 @@ void PrintJobRecovery::resume() {
EXTRUDER_LOOP() {
if (info.retract[e] != 0.0) {
fwretract.current_retract[e] = info.retract[e];
fwretract.retracted[e] = true;
fwretract.retracted.set(e);
}
}
fwretract.current_hop = info.retract_hop;
8 changes: 4 additions & 4 deletions Marlin/src/gcode/control/M17_M18_M84.cpp
Original file line number Diff line number Diff line change
@@ -33,8 +33,8 @@
#include "../../core/debug_out.h"
#include "../../libs/hex_print.h"

inline axis_flags_t selected_axis_bits() {
axis_flags_t selected{0};
inline stepper_flags_t selected_axis_bits() {
stepper_flags_t selected{0};
#if HAS_EXTRUDERS
if (parser.seen('E')) {
if (E_TERN0(parser.has_value())) {
@@ -61,7 +61,7 @@ inline axis_flags_t selected_axis_bits() {
}

// Enable specified axes and warn about other affected axes
void do_enable(const axis_flags_t to_enable) {
void do_enable(const stepper_flags_t to_enable) {
const ena_mask_t was_enabled = stepper.axis_enabled.bits,
shall_enable = to_enable.bits & ~was_enabled;

@@ -147,7 +147,7 @@ void GcodeSuite::M17() {
}
}

void try_to_disable(const axis_flags_t to_disable) {
void try_to_disable(const stepper_flags_t to_disable) {
ena_mask_t still_enabled = to_disable.bits & stepper.axis_enabled.bits;

DEBUG_ECHOLNPGM("Enabled: ", hex_word(stepper.axis_enabled.bits), " To Disable: ", hex_word(to_disable.bits), " | ", hex_word(still_enabled));
9 changes: 6 additions & 3 deletions Marlin/src/lcd/menu/menu_tramming.cpp
Original file line number Diff line number Diff line change
@@ -44,7 +44,7 @@
#include "../../core/debug_out.h"

static float z_measured[G35_PROBE_COUNT];
static bool z_isvalid[G35_PROBE_COUNT];
static Flags<G35_PROBE_COUNT> z_isvalid;
static uint8_t tram_index = 0;
static int8_t reference_index; // = 0

@@ -61,7 +61,10 @@ static bool probe_single_point() {
move_to_tramming_wait_pos();

DEBUG_ECHOLNPGM("probe_single_point(", tram_index, ") = ", z_probed_height, "mm");
return (z_isvalid[tram_index] = !isnan(z_probed_height));

const bool v = !isnan(z_probed_height);
z_isvalid.set(tram_index, v);
return v;
}

static void _menu_single_probe() {
@@ -95,7 +98,7 @@ void goto_tramming_wizard() {
ui.defer_status_screen();

// Initialize measured point flags
ZERO(z_isvalid);
z_isvalid.reset();
reference_index = -1;

// Inject G28, wait for homing to complete,
2 changes: 1 addition & 1 deletion Marlin/src/module/stepper.cpp
Original file line number Diff line number Diff line change
@@ -153,7 +153,7 @@ Stepper stepper; // Singleton
#endif
#endif

axis_flags_t Stepper::axis_enabled; // {0}
stepper_flags_t Stepper::axis_enabled; // {0}

// private:

4 changes: 2 additions & 2 deletions Marlin/src/module/stepper.h
Original file line number Diff line number Diff line change
@@ -261,7 +261,7 @@ typedef struct {
};
constexpr ena_mask_t linear_bits() { return _BV(NUM_AXES) - 1; }
constexpr ena_mask_t e_bits() { return (_BV(EXTRUDERS) - 1) << NUM_AXES; }
} axis_flags_t;
} stepper_flags_t;

// All the stepper enable pins
constexpr pin_t ena_pins[] = {
@@ -596,7 +596,7 @@ class Stepper {
static void refresh_motor_power();
#endif

static axis_flags_t axis_enabled; // Axis stepper(s) ENABLED states
static stepper_flags_t axis_enabled; // Axis stepper(s) ENABLED states

static bool axis_is_enabled(const AxisEnum axis E_OPTARG(const uint8_t eindex=0)) {
return TEST(axis_enabled.bits, INDEX_OF_AXIS(axis, eindex));
8 changes: 4 additions & 4 deletions Marlin/src/module/temperature.cpp
Original file line number Diff line number Diff line change
@@ -1283,7 +1283,7 @@ void Temperature::min_temp_error(const heater_id_t heater_id) {
static hotend_pid_t work_pid[HOTENDS];
static float temp_iState[HOTENDS] = { 0 },
temp_dState[HOTENDS] = { 0 };
static bool pid_reset[HOTENDS] = { false };
static Flags<HOTENDS> pid_reset;
const float pid_error = temp_hotend[ee].target - temp_hotend[ee].celsius;

float pid_output;
@@ -1293,17 +1293,17 @@ void Temperature::min_temp_error(const heater_id_t heater_id) {
|| TERN0(HEATER_IDLE_HANDLER, heater_idle[ee].timed_out)
) {
pid_output = 0;
pid_reset[ee] = true;
pid_reset.set(ee);
}
else if (pid_error > PID_FUNCTIONAL_RANGE) {
pid_output = PID_MAX;
pid_reset[ee] = true;
pid_reset.set(ee);
}
else {
if (pid_reset[ee]) {
temp_iState[ee] = 0.0;
work_pid[ee].Kd = 0.0;
pid_reset[ee] = false;
pid_reset.clear(ee);
}

work_pid[ee].Kd = work_pid[ee].Kd + PID_K2 * (PID_PARAM(Kd, ee) * (temp_dState[ee] - temp_hotend[ee].celsius) - work_pid[ee].Kd);
8 changes: 4 additions & 4 deletions Marlin/src/module/tool_change.cpp
Original file line number Diff line number Diff line change
@@ -46,7 +46,7 @@
#endif

#if ENABLED(TOOLCHANGE_FS_INIT_BEFORE_SWAP)
bool toolchange_extruder_ready[EXTRUDERS];
Flags<EXTRUDERS> toolchange_extruder_ready;
#endif

#if EITHER(MAGNETIC_PARKING_EXTRUDER, TOOL_SENSOR) \
@@ -1057,7 +1057,7 @@ void tool_change(const uint8_t new_tool, bool no_move/*=false*/) {
if (new_tool == old_tool && !first_tool_is_primed && enable_first_prime) {
tool_change_prime();
first_tool_is_primed = true;
TERN_(TOOLCHANGE_FS_INIT_BEFORE_SWAP, toolchange_extruder_ready[old_tool] = true); // Primed and initialized
TERN_(TOOLCHANGE_FS_INIT_BEFORE_SWAP, toolchange_extruder_ready.set(old_tool)); // Primed and initialized
}
#endif

@@ -1216,7 +1216,7 @@ void tool_change(const uint8_t new_tool, bool no_move/*=false*/) {

#if ENABLED(TOOLCHANGE_FS_INIT_BEFORE_SWAP)
if (!toolchange_extruder_ready[new_tool]) {
toolchange_extruder_ready[new_tool] = true;
toolchange_extruder_ready.set(new_tool);
fr = toolchange_settings.prime_speed; // Next move is a prime
unscaled_e_move(0, MMM_TO_MMS(fr)); // Init planner with 0 length move
}
@@ -1401,7 +1401,7 @@ void tool_change(const uint8_t new_tool, bool no_move/*=false*/) {

// Migrate the retracted state
#if ENABLED(FWRETRACT)
fwretract.retracted[migration_extruder] = fwretract.retracted[active_extruder];
fwretract.retracted.set(migration_extruder, fwretract.retracted[active_extruder]);
#endif

// Migrate the temperature to the new hotend
2 changes: 1 addition & 1 deletion Marlin/src/module/tool_change.h
Original file line number Diff line number Diff line change
@@ -50,7 +50,7 @@
#endif

#if ENABLED(TOOLCHANGE_FS_INIT_BEFORE_SWAP)
extern bool toolchange_extruder_ready[EXTRUDERS];
extern Flags<EXTRUDERS> toolchange_extruder_ready;
#endif

#if ENABLED(TOOLCHANGE_MIGRATION_FEATURE)