@@ -55,8 +55,8 @@ FTMotion ftMotion;
5555ft_config_t FTMotion::cfg;
5656bool FTMotion::busy; // = false
5757ft_command_t FTMotion::stepperCmdBuff[FTM_STEPPERCMD_BUFF_SIZE] = {0U }; // Stepper commands buffer.
58- uint32_t FTMotion::stepperCmdBuff_produceIdx = 0 , // Index of next stepper command write to the buffer.
59- FTMotion::stepperCmdBuff_consumeIdx = 0 ; // Index of next stepper command read from the buffer.
58+ int32_t FTMotion::stepperCmdBuff_produceIdx = 0 , // Index of next stepper command write to the buffer.
59+ FTMotion::stepperCmdBuff_consumeIdx = 0 ; // Index of next stepper command read from the buffer.
6060
6161bool FTMotion::sts_stepperBusy = false ; // The stepper buffer has items and is in use.
6262
@@ -123,9 +123,7 @@ uint32_t FTMotion::interpIdx = 0, // Index of current data point b
123123 float FTMotion::e_advanced_z1 = 0 .0f ; // (ms) Unit delay of advanced extruder position.
124124#endif
125125
126- #if DISABLED(FTM_UNIFIED_BWS)
127- constexpr uint32_t last_batchIdx = (FTM_WINDOW_SIZE) - (FTM_BATCH_SIZE);
128- #endif
126+ constexpr uint32_t last_batchIdx = TERN(FTM_UNIFIED_BWS, 0 , (FTM_WINDOW_SIZE) - (FTM_BATCH_SIZE));
129127
130128// -----------------------------------------------------------------
131129// Function definitions.
@@ -149,12 +147,16 @@ void FTMotion::runoutBlock() {
149147 ratio.reset ();
150148
151149 max_intervals = cfg.modeHasShaper () ? shaper_intervals : 0 ;
152- if (max_intervals <= TERN (FTM_UNIFIED_BWS, FTM_BW_SIZE, min_max_intervals - (FTM_BATCH_SIZE))) max_intervals = min_max_intervals;
153- #if DISABLED(FTM_UNIFIED_BWS)
154- max_intervals += FTM_WINDOW_SIZE - ((FTM_BATCH_SIZE > last_batchIdx)? 0 :makeVector_batchIdx);
155- #else
156- max_intervals += FTM_BW_SIZE - makeVector_batchIdx;
157- #endif
150+ if (max_intervals <= TERN (FTM_UNIFIED_BWS, FTM_BW_SIZE, min_max_intervals - (FTM_BATCH_SIZE)))
151+ max_intervals = min_max_intervals;
152+
153+ max_intervals += (
154+ #if ENABLED(FTM_UNIFIED_BWS)
155+ FTM_BW_SIZE - makeVector_batchIdx
156+ #else
157+ FTM_WINDOW_SIZE - ((last_batchIdx < (FTM_BATCH_SIZE)) ? 0 : makeVector_batchIdx)
158+ #endif
159+ );
158160 blockProcRdy = blockDataIsRunout = true ;
159161 runoutEna = blockProcDn = false ;
160162}
@@ -457,7 +459,7 @@ void FTMotion::reset() {
457459 endPosn_prevBlock.reset ();
458460
459461 makeVector_idx = makeVector_idx_z1 = 0 ;
460- makeVector_batchIdx = TERN (FTM_UNIFIED_BWS, 0 , (FTM_BATCH_SIZE > last_batchIdx)? FTM_BATCH_SIZE:last_batchIdx );
462+ makeVector_batchIdx = TERN (FTM_UNIFIED_BWS, 0 , _MAX ( last_batchIdx, FTM_BATCH_SIZE) );
461463
462464 steps.reset ();
463465 interpIdx = interpIdx_z1 = 0 ;
@@ -472,10 +474,11 @@ void FTMotion::reset() {
472474}
473475
474476// Private functions.
477+
475478// Auxiliary function to get number of step commands in the buffer.
476- uint32_t FTMotion::stepperCmdBuffItems () {
477- const uint32_t udiff = stepperCmdBuff_produceIdx - stepperCmdBuff_consumeIdx;
478- return stepperCmdBuff_produceIdx < stepperCmdBuff_consumeIdx ? (FTM_STEPPERCMD_BUFF_SIZE) + udiff : udiff;
479+ int32_t FTMotion::stepperCmdBuffItems () {
480+ const int32_t udiff = stepperCmdBuff_produceIdx - stepperCmdBuff_consumeIdx;
481+ return (udiff < 0 ) ? udiff += (FTM_STEPPERCMD_BUFF_SIZE) : udiff;
479482}
480483
481484// Initializes storage variables before startup.
@@ -686,7 +689,7 @@ void FTMotion::makeVector() {
686689
687690 // Filled up the queue with regular and shaped steps
688691 if (++makeVector_batchIdx == TERN (FTM_UNIFIED_BWS, FTM_BW_SIZE, FTM_WINDOW_SIZE)) {
689- makeVector_batchIdx = TERN (FTM_UNIFIED_BWS, 0 , last_batchIdx) ;
692+ makeVector_batchIdx = last_batchIdx;
690693 batchRdy = true ;
691694 }
692695
0 commit comments