Sound Byte Libs 0.5.1-340-gab5b79a
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/**
2 * @file encoder_step.hpp
3 * @brief Encoder pulse-to-step converter
4 *
5 * Many rotary encoders (e.g., EC11) produce multiple electrical pulses
6 * per physical detent. EncoderStep accumulates raw pulses and emits
7 * +/-1 step only when a full detent boundary is crossed.
8 *
9 * The residual is preserved across calls, so no pulses are lost.
10 *
11 * Usage:
12 * @code
13 * sbl::control::input::EncoderStep enc(4); // EC11: 4 pulses/detent
14 * // In the control loop:
15 * int8_t step = enc.process(encoder.delta());
16 * if (step != 0) { ... } // one detent click: +1 or -1
17 * @endcode
18 */
19
20#ifndef SBL_CONTROL_INPUT_ENCODER_STEP_HPP_
21#define SBL_CONTROL_INPUT_ENCODER_STEP_HPP_
22
23#include <cstdint>
24
25namespace sbl::control::input {
26
28public:
29 /** @param divisor Pulses per detent (e.g., 4 for EC11). Must be >= 1. */
30 explicit EncoderStep(int8_t divisor = 4) : divisor_(divisor < 1 ? static_cast<int8_t>(1) : divisor) {}
31
32 /** Feed raw encoder delta, returns step (-1, 0, or +1). */
33 int8_t process(int32_t raw_delta) {
34 accum_ = static_cast<int8_t>(accum_ + static_cast<int8_t>(raw_delta));
35 int8_t step = 0;
36 if (accum_ >= divisor_) {
37 step = 1;
38 accum_ = static_cast<int8_t>(accum_ - divisor_);
39 } else if (accum_ <= -divisor_) {
40 step = -1;
41 accum_ = static_cast<int8_t>(accum_ + divisor_);
42 }
43 return step;
44 }
45
46 /** Current accumulator value. */
47 int8_t accumulator() const { return accum_; }
48
49 /** Reset accumulator to zero. */
50 void reset() { accum_ = 0; }
51
52private:
53 int8_t divisor_;
54 int8_t accum_ = 0;
55};
56
57} // namespace sbl::control::input
58
59#endif // SBL_CONTROL_INPUT_ENCODER_STEP_HPP_
int8_t process(int32_t raw_delta)
Inputs: debounce, decoding, smoothing and scaling of raw samples.
Definition button.hpp:29