Skip to content

Commit

Permalink
⚡️ Use AxisFlags for step_needed
Browse files Browse the repository at this point in the history
Co-Authored-By: tombrazier <68918209+tombrazier@users.noreply.github.com>
  • Loading branch information
thinkyhead and tombrazier committed Apr 10, 2023
1 parent df2862f commit a665aad
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions Marlin/src/module/stepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1661,21 +1661,24 @@ void Stepper::pulse_phase_isr() {
bool firstStep = true;
USING_TIMED_PULSE();
#endif
xyze_bool_t step_needed{0};

// Direct Stepping page?
const bool is_page = current_block->is_page();

do {
AxisFlags step_needed{0};

#define _APPLY_STEP(AXIS, INV, ALWAYS) AXIS ##_APPLY_STEP(INV, ALWAYS)
#define _STEP_STATE(AXIS) STEP_STATE_## AXIS

// Determine if a pulse is needed using Bresenham
#define PULSE_PREP(AXIS) do{ \
delta_error[_AXIS(AXIS)] += advance_dividend[_AXIS(AXIS)]; \
step_needed[_AXIS(AXIS)] = (delta_error[_AXIS(AXIS)] >= 0); \
if (step_needed[_AXIS(AXIS)]) \
delta_error[_AXIS(AXIS)] -= advance_divisor; \
int32_t de = delta_error[_AXIS(AXIS)] + advance_dividend[_AXIS(AXIS)]; \
if (de >= 0) { \
step_needed.set(_AXIS(AXIS)); \
de -= advance_divisor; \
} \
delta_error[_AXIS(AXIS)] = de; \
}while(0)

// With input shaping, direction changes can happen with almost only
Expand All @@ -1699,7 +1702,7 @@ void Stepper::pulse_phase_isr() {
#define HYSTERESIS(AXIS) _HYSTERESIS(AXIS)

#define PULSE_PREP_SHAPING(AXIS, DELTA_ERROR, DIVIDEND) do{ \
if (step_needed[_AXIS(AXIS)]) { \
if (step_needed.test(_AXIS(AXIS))) { \
DELTA_ERROR += (DIVIDEND); \
if ((MAXDIR(AXIS) && DELTA_ERROR <= -(64 + HYSTERESIS(AXIS))) || (MINDIR(AXIS) && DELTA_ERROR >= (64 + HYSTERESIS(AXIS)))) { \
{ USING_TIMED_PULSE(); START_TIMED_PULSE(); AWAIT_LOW_PULSE(); } \
Expand All @@ -1708,23 +1711,23 @@ void Stepper::pulse_phase_isr() {
SET_STEP_DIR(AXIS); \
DIR_WAIT_AFTER(); \
} \
step_needed[_AXIS(AXIS)] = DELTA_ERROR <= -(64 + HYSTERESIS(AXIS)) || DELTA_ERROR >= (64 + HYSTERESIS(AXIS)); \
if (step_needed[_AXIS(AXIS)]) \
step_needed.set(_AXIS(AXIS), DELTA_ERROR <= -(64 + HYSTERESIS(AXIS)) || DELTA_ERROR >= (64 + HYSTERESIS(AXIS))); \
if (step_needed.test(_AXIS(AXIS))) \
DELTA_ERROR += MAXDIR(AXIS) ? -128 : 128; \
} \
}while(0)

// Start an active pulse if needed
#define PULSE_START(AXIS) do{ \
if (step_needed[_AXIS(AXIS)]) { \
if (step_needed.test(_AXIS(AXIS))) { \
count_position[_AXIS(AXIS)] += count_direction[_AXIS(AXIS)]; \
_APPLY_STEP(AXIS, _STEP_STATE(AXIS), 0); \
} \
}while(0)

// Stop an active pulse if needed
#define PULSE_STOP(AXIS) do { \
if (step_needed[_AXIS(AXIS)]) { \
if (step_needed.test(_AXIS(AXIS))) { \
_APPLY_STEP(AXIS, !_STEP_STATE(AXIS), 0); \
} \
}while(0)
Expand All @@ -1743,8 +1746,8 @@ void Stepper::pulse_phase_isr() {
}while(0)

#define PAGE_PULSE_PREP(AXIS) do{ \
step_needed[_AXIS(AXIS)] = \
pgm_read_byte(&segment_table[page_step_state.sd[_AXIS(AXIS)]][page_step_state.segment_steps & 0x7]); \
step_needed.set(_AXIS(AXIS), \
pgm_read_byte(&segment_table[page_step_state.sd[_AXIS(AXIS)]][page_step_state.segment_steps & 0x7])); \
}while(0)

switch (page_step_state.segment_steps) {
Expand Down Expand Up @@ -1784,8 +1787,8 @@ void Stepper::pulse_phase_isr() {
page_step_state.bd[_AXIS(AXIS)] += VALUE;

#define PAGE_PULSE_PREP(AXIS) do{ \
step_needed[_AXIS(AXIS)] = \
pgm_read_byte(&segment_table[page_step_state.sd[_AXIS(AXIS)]][page_step_state.segment_steps & 0x3]); \
step_needed.set(_AXIS(AXIS), \
pgm_read_byte(&segment_table[page_step_state.sd[_AXIS(AXIS)]][page_step_state.segment_steps & 0x3])); \
}while(0)

switch (page_step_state.segment_steps) {
Expand All @@ -1812,10 +1815,10 @@ void Stepper::pulse_phase_isr() {

#elif STEPPER_PAGE_FORMAT == SP_4x1_512

#define PAGE_PULSE_PREP(AXIS, BITS) do{ \
step_needed[_AXIS(AXIS)] = (steps >> BITS) & 0x1; \
if (step_needed[_AXIS(AXIS)]) \
page_step_state.bd[_AXIS(AXIS)]++; \
#define PAGE_PULSE_PREP(AXIS, NBIT) do{ \
step_needed.set(_AXIS(AXIS), TEST(steps, NBIT)); \
if (step_needed.test(_AXIS(AXIS))) \
page_step_state.bd[_AXIS(AXIS)]++; \
}while(0)

uint8_t steps = page_step_state.page[page_step_state.segment_idx >> 1];
Expand Down Expand Up @@ -1880,8 +1883,8 @@ void Stepper::pulse_phase_isr() {

#if HAS_SHAPING
// record an echo if a step is needed in the primary bresenham
const bool x_step = TERN0(INPUT_SHAPING_X, shaping_x.enabled && step_needed[X_AXIS]),
y_step = TERN0(INPUT_SHAPING_Y, shaping_y.enabled && step_needed[Y_AXIS]);
const bool x_step = TERN0(INPUT_SHAPING_X, shaping_x.enabled && step_needed.x),
y_step = TERN0(INPUT_SHAPING_Y, shaping_y.enabled && step_needed.y);
if (x_step || y_step)
ShapingQueue::enqueue(x_step, TERN0(INPUT_SHAPING_X, shaping_x.forward), y_step, TERN0(INPUT_SHAPING_Y, shaping_y.forward));

Expand Down Expand Up @@ -1995,23 +1998,23 @@ void Stepper::pulse_phase_isr() {
#if HAS_SHAPING

void Stepper::shaping_isr() {
xy_bool_t step_needed{0};
AxisFlags step_needed{0};

// Clear the echoes that are ready to process. If the buffers are too full and risk overflow, also apply echoes early.
TERN_(INPUT_SHAPING_X, step_needed[X_AXIS] = !ShapingQueue::peek_x() || ShapingQueue::free_count_x() < steps_per_isr);
TERN_(INPUT_SHAPING_Y, step_needed[Y_AXIS] = !ShapingQueue::peek_y() || ShapingQueue::free_count_y() < steps_per_isr);
TERN_(INPUT_SHAPING_X, step_needed.x = !ShapingQueue::peek_x() || ShapingQueue::free_count_x() < steps_per_isr);
TERN_(INPUT_SHAPING_Y, step_needed.y = !ShapingQueue::peek_y() || ShapingQueue::free_count_y() < steps_per_isr);

if (bool(step_needed)) while (true) {
#if ENABLED(INPUT_SHAPING_X)
if (step_needed[X_AXIS]) {
if (step_needed.x) {
const bool forward = ShapingQueue::dequeue_x();
PULSE_PREP_SHAPING(X, shaping_x.delta_error, shaping_x.factor2 * (forward ? 1 : -1));
PULSE_START(X);
}
#endif

#if ENABLED(INPUT_SHAPING_Y)
if (step_needed[Y_AXIS]) {
if (step_needed.y) {
const bool forward = ShapingQueue::dequeue_y();
PULSE_PREP_SHAPING(Y, shaping_y.delta_error, shaping_y.factor2 * (forward ? 1 : -1));
PULSE_START(Y);
Expand All @@ -2034,8 +2037,8 @@ void Stepper::pulse_phase_isr() {
#endif
}

TERN_(INPUT_SHAPING_X, step_needed[X_AXIS] = !ShapingQueue::peek_x() || ShapingQueue::free_count_x() < steps_per_isr);
TERN_(INPUT_SHAPING_Y, step_needed[Y_AXIS] = !ShapingQueue::peek_y() || ShapingQueue::free_count_y() < steps_per_isr);
TERN_(INPUT_SHAPING_X, step_needed.x = !ShapingQueue::peek_x() || ShapingQueue::free_count_x() < steps_per_isr);
TERN_(INPUT_SHAPING_Y, step_needed.y = !ShapingQueue::peek_y() || ShapingQueue::free_count_y() < steps_per_isr);

if (!bool(step_needed)) break;

Expand Down

0 comments on commit a665aad

Please sign in to comment.