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.hpp
Go to the documentation of this file.
1/**
2 * @file encoder.hpp
3 * @brief Quadrature encoder component - Gray code state machine
4 *
5 * Encoder tracks rotational position from A/B quadrature signals.
6 * Decodes with a 16-entry transition table indexed by the old and new A/B states.
7 * The encoder's push-button is a separate Button component (composable).
8 *
9 * Usage:
10 * @code
11 * sbl::control::input::Encoder enc;
12 * enc.update(gpio_a, gpio_b); // Feed A/B each tick
13 * int32_t d = enc.delta(); // Steps since last check (+CW/-CCW)
14 * int32_t pos = enc.position(); // Cumulative position
15 * @endcode
16 */
17
18#ifndef SBL_CONTROL_INPUT_ENCODER_HPP_
19#define SBL_CONTROL_INPUT_ENCODER_HPP_
20
21#include <cstdint>
22
23namespace sbl::control::input {
24
25/**
26 * @brief Quadrature encoder with Gray code state machine and position tracking
27 *
28 * @context All public methods are ISR-safe: pure state machine with no hardware I/O.
29 */
30class Encoder {
31public:
32 /** @brief Construct at position 0 with both phases low */
33 Encoder() : state_(0), delta_(0), position_(0) {}
34
35 /**
36 * @brief Feed quadrature signals
37 *
38 * Call once per update cycle with current A and B readings.
39 * The state machine detects CW/CCW transitions and accumulates delta.
40 *
41 * @param a Channel A reading
42 * @param b Channel B reading
43 */
44 void update(bool a, bool b) {
45 // Encode new state as 2-bit: [B:A]
46 uint8_t new_state = (b ? 2u : 0u) | (a ? 1u : 0u);
47
48 // Build 4-bit lookup index: [old_B:old_A:new_B:new_A]
49 uint8_t idx = (state_ << 2) | new_state;
50
51 // Transition table: +1 = CW, -1 = CCW, 0 = no change or invalid
52 // Index is [old_state(2 bits) : new_state(2 bits)]
53 static constexpr int8_t TRANSITION_TABLE[16] = {
54 // 00->00, 00->01, 00->10, 00->11
55 0,
56 +1,
57 -1,
58 0,
59 // 01->00, 01->01, 01->10, 01->11
60 -1,
61 0,
62 0,
63 +1,
64 // 10->00, 10->01, 10->10, 10->11
65 +1,
66 0,
67 0,
68 -1,
69 // 11->00, 11->01, 11->10, 11->11
70 0,
71 -1,
72 +1,
73 0,
74 };
75
76 int8_t step = TRANSITION_TABLE[idx];
77 delta_ += step;
78 position_ += step;
79
80 state_ = new_state;
81 }
82
83 /**
84 * @brief Steps since last delta() call
85 *
86 * Positive = CW, negative = CCW. Resets on read.
87 */
88 int32_t delta() {
89 int32_t d = delta_;
90 delta_ = 0;
91 return d;
92 }
93
94 /** @brief Cumulative position (does not reset) */
95 int32_t position() const { return position_; }
96
97 /** @brief Reset all state */
98 void reset() {
99 state_ = 0;
100 delta_ = 0;
101 position_ = 0;
102 }
103
104private:
105 uint8_t state_; /**< Previous [B:A] state */
106 int32_t delta_; /**< Accumulated since last delta() call */
107 int32_t position_; /**< Cumulative position */
108};
109
110} // namespace sbl::control::input
111
112#endif // SBL_CONTROL_INPUT_ENCODER_HPP_
Quadrature encoder with Gray code state machine and position tracking.
Definition encoder.hpp:30
int32_t position() const
Cumulative position (does not reset)
Definition encoder.hpp:95
int32_t delta()
Steps since last delta() call.
Definition encoder.hpp:88
void update(bool a, bool b)
Feed quadrature signals.
Definition encoder.hpp:44
Encoder()
Construct at position 0 with both phases low.
Definition encoder.hpp:33
void reset()
Reset all state.
Definition encoder.hpp:98
Inputs: debounce, decoding, smoothing and scaling of raw samples.
Definition button.hpp:29