Skip to content

Commit 61ed9e3

Browse files
committed
Simplify encoder acceleration function
1 parent ab91718 commit 61ed9e3

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

ats-mini/ats-mini.ino

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -243,38 +243,33 @@ void setup()
243243
int16_t accelerateEncoder(int8_t dir)
244244
{
245245
const uint32_t speedThresholds[] = {350, 60, 45, 35, 25}; // ms between clicks
246-
const uint16_t accelFactors[] = {1, 2, 4, 8, 16}; // corresponding multipliers
247-
static uint16_t lastAccelFactor = accelFactors[0];
248-
static uint32_t lastSpeed = speedThresholds[0];
246+
const uint16_t accelFactors[] = {1, 2, 4, 8, 16}; // corresponding multipliers
249247
static uint32_t lastEncoderTime = 0;
248+
static uint32_t lastSpeed = speedThresholds[0];
249+
static uint16_t lastAccelFactor = accelFactors[0];
250250
static int8_t lastEncoderDir = 0;
251251

252252
uint32_t currentTime = millis();
253-
uint16_t accelFactor;
254-
255-
uint32_t speed = ((currentTime - lastEncoderTime) * 7 + lastSpeed * 3) / 10;
253+
lastSpeed = ((currentTime - lastEncoderTime) * 7 + lastSpeed * 3) / 10;
256254

257255
// Reset acceleration on timeout or direction change
258-
if (speed > speedThresholds[0] || lastEncoderDir != dir) {
259-
accelFactor = accelFactors[0];
260-
speed = speedThresholds[0];
256+
if (lastSpeed > speedThresholds[0] || lastEncoderDir != dir) {
257+
lastSpeed = speedThresholds[0];
258+
lastAccelFactor = accelFactors[0];
261259
} else {
262260
// Lookup acceleration factor
263-
accelFactor = lastAccelFactor;
264261
for (int8_t i = LAST_ITEM(speedThresholds); i >= 0; i--) {
265-
if (speed <= speedThresholds[i] && accelFactor < accelFactors[i]) {
266-
accelFactor = accelFactors[i];
262+
if (lastSpeed <= speedThresholds[i] && lastAccelFactor < accelFactors[i]) {
263+
lastAccelFactor = accelFactors[i];
267264
break;
268265
}
269266
}
270267
}
271268
lastEncoderTime = currentTime;
272269
lastEncoderDir = dir;
273-
lastSpeed = speed;
274-
lastAccelFactor = accelFactor;
275270

276271
// Apply acceleration with direction
277-
return(dir * accelFactor);
272+
return(dir * lastAccelFactor);
278273
}
279274

280275
//

0 commit comments

Comments
 (0)