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

Use Flags<> for runout #25938

Merged
Merged
Changes from all commits
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
46 changes: 19 additions & 27 deletions Marlin/src/feature/runout.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
#define HAS_FILAMENT_SWITCH 1
#endif

typedef Flags<8> runout_flags_t;

void event_filament_runout(const uint8_t extruder);

template<class RESPONSE_T, class SENSOR_T>
Expand Down Expand Up @@ -130,39 +132,29 @@ class TFilamentMonitor : public FilamentMonitorBase {
TERN_(HAS_FILAMENT_RUNOUT_DISTANCE, cli()); // Prevent RunoutResponseDelayed::block_completed from accumulating here
response.run();
sensor.run();
const uint8_t runout_flags = response.has_run_out();
const runout_flags_t runout_flags = response.has_run_out();
TERN_(HAS_FILAMENT_RUNOUT_DISTANCE, sei());
#if MULTI_FILAMENT_SENSOR
#if ENABLED(WATCH_ALL_RUNOUT_SENSORS)
const bool ran_out = !!runout_flags; // any sensor triggers
const bool ran_out = bool(runout_flags); // any sensor triggers
uint8_t extruder = 0;
if (ran_out) {
uint8_t bitmask = runout_flags;
while (!(bitmask & 1)) {
bitmask >>= 1;
extruder++;
}
}
if (ran_out) while (!runout_flags.test(extruder)) extruder++;
#else
const bool ran_out = TEST(runout_flags, active_extruder); // suppress non active extruders
const bool ran_out = runout_flags[active_extruder]; // suppress non active extruders
uint8_t extruder = active_extruder;
#endif
#else
const bool ran_out = !!runout_flags;
const bool ran_out = bool(runout_flags);
uint8_t extruder = active_extruder;
#endif

#if ENABLED(FILAMENT_RUNOUT_SENSOR_DEBUG)
if (runout_flags) {
if (ran_out) {
#if ENABLED(FILAMENT_RUNOUT_SENSOR_DEBUG)
SERIAL_ECHOPGM("Runout Sensors: ");
for (uint8_t i = 0; i < 8; ++i) SERIAL_ECHO('0' + TEST(runout_flags, i));
SERIAL_ECHOPGM(" -> ", extruder);
if (ran_out) SERIAL_ECHOPGM(" RUN OUT");
SERIAL_EOL();
}
#endif
for (uint8_t i = 0; i < 8; ++i) SERIAL_ECHO('0' + char(runout_flags[i]));
SERIAL_ECHOLNPGM(" -> ", extruder, " RUN OUT");
#endif

if (ran_out) {
filament_ran_out = true;
event_filament_runout(extruder);
planner.synchronize();
Expand Down Expand Up @@ -387,11 +379,11 @@ class FilamentSensorBase {
#endif
}

static uint8_t has_run_out() {
uint8_t runout_flags = 0;
for (uint8_t i = 0; i < NUM_RUNOUT_SENSORS; ++i) if (mm_countdown.runout[i] < 0) SBI(runout_flags, i);
static runout_flags_t has_run_out() {
runout_flags_t runout_flags{0};
for (uint8_t i = 0; i < NUM_RUNOUT_SENSORS; ++i) if (mm_countdown.runout[i] < 0) runout_flags.set(i);
#if ENABLED(FILAMENT_SWITCH_AND_MOTION)
for (uint8_t i = 0; i < NUM_MOTION_SENSORS; ++i) if (mm_countdown.motion[i] < 0) SBI(runout_flags, i);
for (uint8_t i = 0; i < NUM_MOTION_SENSORS; ++i) if (mm_countdown.motion[i] < 0) runout_flags.set(i);
#endif
return runout_flags;
}
Expand Down Expand Up @@ -439,9 +431,9 @@ class FilamentSensorBase {
for (uint8_t i = 0; i < NUM_RUNOUT_SENSORS; ++i) if (runout_count[i] >= 0) runout_count[i]--;
}

static uint8_t has_run_out() {
uint8_t runout_flags = 0;
for (uint8_t i = 0; i < NUM_RUNOUT_SENSORS; ++i) if (runout_count[i] < 0) SBI(runout_flags, i);
static runout_flags_t has_run_out() {
runout_flags_t runout_flags{0};
for (uint8_t i = 0; i < NUM_RUNOUT_SENSORS; ++i) if (runout_count[i] < 0) runout_flags.set(i);
return runout_flags;
}

Expand Down