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
25
namespace
sbl::control::input
{
26
27
class
EncoderStep
{
28
public
:
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
52
private
:
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_
sbl::control::input::EncoderStep
Definition
encoder_step.hpp:27
sbl::control::input::EncoderStep::process
int8_t process(int32_t raw_delta)
Definition
encoder_step.hpp:33
sbl::control::input::EncoderStep::reset
void reset()
Definition
encoder_step.hpp:50
sbl::control::input::EncoderStep::accumulator
int8_t accumulator() const
Definition
encoder_step.hpp:47
sbl::control::input::EncoderStep::EncoderStep
EncoderStep(int8_t divisor=4)
Definition
encoder_step.hpp:30
sbl::control::input
Inputs: debounce, decoding, smoothing and scaling of raw samples.
Definition
button.hpp:29
src
sbl
control
input
encoder_step.hpp
Generated by
1.9.8