Sound Byte Libs 0.5.1-121-g3358a44
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/hw/components/input/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::input::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#ifndef SBL_HW_COMPONENTS_INPUT_ENCODER_STEP_HPP_
16#define SBL_HW_COMPONENTS_INPUT_ENCODER_STEP_HPP_
17
18#include <cstdint>
19
20namespace sbl {
21namespace components {
22namespace input {
23
25public:
26 /// @param divisor Pulses per detent (e.g., 4 for EC11). Must be >= 1.
27 explicit EncoderStep(int8_t divisor = 4)
28 : divisor_(divisor < 1 ? 1 : divisor) {}
29
30 /// Feed raw encoder delta, returns step (-1, 0, or +1).
31 int8_t process(int32_t raw_delta) {
32 accum_ += static_cast<int8_t>(raw_delta);
33 int8_t step = 0;
34 if (accum_ >= divisor_) {
35 step = 1;
36 accum_ -= divisor_;
37 } else if (accum_ <= -divisor_) {
38 step = -1;
39 accum_ += divisor_;
40 }
41 return step;
42 }
43
44 /// Current accumulator value.
45 int8_t accumulator() const { return accum_; }
46
47 /// Reset accumulator to zero.
48 void reset() { accum_ = 0; }
49
50private:
51 int8_t divisor_;
52 int8_t accum_ = 0;
53};
54
55} // namespace input
56} // namespace components
57} // namespace sbl
58
59#endif // SBL_HW_COMPONENTS_INPUT_ENCODER_STEP_HPP_
int8_t process(int32_t raw_delta)
Feed raw encoder delta, returns step (-1, 0, or +1).
int8_t accumulator() const
Current accumulator value.
void reset()
Reset accumulator to zero.
Root namespace for all Sound Byte Libs code.
Definition assert.hpp:51