Deluge Firmware
1.3.0
Build date: 2026.10.14
Toggle main menu visibility
Loading...
Searching...
No Matches
encoder_acceleration.h
1
#pragma once
2
3
#include <algorithm>
4
#include <cstdint>
5
6
namespace
deluge::hid::encoders {
7
8
class
EncoderAcceleration
{
9
public
:
10
struct
Tuning
{
11
// - acceleration and inertia (1 - acceleration) control how fast the knob speeds up
12
// - speedScale controls how we go from "raw speed" to speed used as offset multiplier
13
// - min and max speed clamp the max effective speed
14
//
15
// current values have been tuned to be slow enough to feel easy to control, but fast
16
// enough to go from 0 to 50 with one fast turn of the encoder. speedScale and min/max
17
// could potentially be user-configurable in a small range.
18
double
acceleration = 0.1;
19
double
speed_scale = 0.15;
20
double
min_multiplier = 1.0;
21
double
max_multiplier = 3.0;
22
double
reset_after = 0.3;
23
};
24
25
double
advance(int8_t offset,
double
time) {
return
advance(offset, time,
Tuning
{}); }
26
double
advance(int8_t offset,
double
time, Tuning tuning) {
27
const
double
elapsed = time - last_time_;
28
if
(elapsed >= tuning.reset_after || elapsed < 0.0 || (offset > 0) != (last_offset_ > 0)) {
29
// too much time passed, or the knob direction changed, reset the speed
30
speed_ = 0.0;
31
}
32
else
if
(elapsed > 0.0) {
33
// moving in the same direction, update speed
34
speed_ = speed_ * (1.0 - tuning.acceleration) + tuning.acceleration / elapsed;
35
}
36
// last_time and last_offset keep track of our time and direction. Only the sign of the offset
37
// matters: fast rotation mixes single- and multi-tick batches in the same direction.
38
last_time_ = time;
39
last_offset_ = offset;
40
return
std::clamp(speed_ * tuning.speed_scale, tuning.min_multiplier, tuning.max_multiplier);
41
}
42
void
reset() { *
this
= {}; }
43
44
private
:
45
double
speed_ = 0.0;
46
double
last_time_ = 0.0;
47
int8_t last_offset_ = 0;
48
};
49
50
inline
constexpr
EncoderAcceleration::Tuning
kGoldEncoderAcceleration{};
51
inline
constexpr
EncoderAcceleration::Tuning
kHorizontalMenuAcceleration{.max_multiplier = 5.0};
52
53
}
// namespace deluge::hid::encoders
deluge::hid::encoders::EncoderAcceleration
Definition
encoder_acceleration.h:8
deluge::hid::encoders::EncoderAcceleration::Tuning
Definition
encoder_acceleration.h:10