Sound Byte Libs 29c5ff3
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
encoder_step.hpp
Go to the documentation of this file.
1// sbl/components/control/encoder_step.hpp — Encoder pulse-to-step converter
2//
3// Many rotary encoders (e.g., EC11) produce multiple electrical pulses
4// per physical detent. EncoderStep accumulates raw pulses and emits
5// ±1 step only when a full detent boundary is crossed.
6//
7// The residual is preserved across calls, so no pulses are lost.
8//
9// Usage:
10// sbl::components::control::EncoderStep enc(4); // EC11: 4 pulses/detent
11// // In your control loop:
12// int8_t step = enc.process(encoder.delta());
13// if (step != 0) { /* one detent click: +1 or -1 */ }
14
15#pragma once
16
17#include <cstdint>
18
19namespace sbl {
20namespace components {
21namespace control {
22
24public:
25 /// @param divisor Pulses per detent (e.g., 4 for EC11). Must be >= 1.
26 explicit EncoderStep(int8_t divisor = 4)
27 : divisor_(divisor < 1 ? 1 : divisor) {}
28
29 /// Feed raw encoder delta, returns step (-1, 0, or +1).
30 int8_t process(int32_t raw_delta) {
31 accum_ += static_cast<int8_t>(raw_delta);
32 int8_t step = 0;
33 if (accum_ >= divisor_) {
34 step = 1;
35 accum_ -= divisor_;
36 } else if (accum_ <= -divisor_) {
37 step = -1;
38 accum_ += divisor_;
39 }
40 return step;
41 }
42
43 /// Current accumulator value.
44 int8_t accumulator() const { return accum_; }
45
46 /// Reset accumulator to zero.
47 void reset() { accum_ = 0; }
48
49private:
50 int8_t divisor_;
51 int8_t accum_ = 0;
52};
53
54} // namespace control
55} // namespace components
56} // namespace sbl
int8_t accumulator() const
Current accumulator value.
void reset()
Reset accumulator to zero.
int8_t process(int32_t raw_delta)
Feed raw encoder delta, returns step (-1, 0, or +1).
Root namespace for all Sound Byte Libs functionality.
Definition aliases.hpp:24