Sound Byte Libs 29c5ff3
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 * @ingroup components
5 *
6 * Encoder tracks rotational position from A/B quadrature signals.
7 * Uses a 16-entry transition table for robust decoding.
8 * The encoder's push-button is a separate Button component (composable).
9 *
10 * Usage:
11 * sbl::components::control::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 */
16
17#ifndef SBL_COMPONENTS_CONTROL_ENCODER_HPP_
18#define SBL_COMPONENTS_CONTROL_ENCODER_HPP_
19
20#include <cstdint>
21
22namespace sbl {
23namespace components {
24namespace control {
25
26/**
27 * @brief Quadrature encoder with Gray code state machine and position tracking
28 *
29 * @note All public methods are ISR-safe — pure state machine with no hardware I/O.
30 */
31class Encoder {
32public:
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 table[16] = {
54 // 00→00, 00→01, 00→10, 00→11
55 0, +1, -1, 0,
56 // 01→00, 01→01, 01→10, 01→11
57 -1, 0, 0, +1,
58 // 10→00, 10→01, 10→10, 10→11
59 +1, 0, 0, -1,
60 // 11→00, 11→01, 11→10, 11→11
61 0, -1, +1, 0,
62 };
63
64 int8_t step = table[idx];
65 delta_ += step;
66 position_ += step;
67
68 state_ = new_state;
69 }
70
71 /**
72 * @brief Steps since last delta() call
73 *
74 * Positive = CW, negative = CCW. Resets on read.
75 */
76 int32_t delta() {
77 int32_t d = delta_;
78 delta_ = 0;
79 return d;
80 }
81
82 /** @brief Cumulative position (does not reset) */
83 int32_t position() const { return position_; }
84
85 /** @brief Reset all state */
86 void reset() {
87 state_ = 0;
88 delta_ = 0;
89 position_ = 0;
90 }
91
92private:
93 uint8_t state_; ///< Previous [B:A] state
94 int32_t delta_; ///< Accumulated since last delta() call
95 int32_t position_; ///< Cumulative position
96};
97
98} // namespace control
99} // namespace components
100} // namespace sbl
101
102#endif // SBL_COMPONENTS_CONTROL_ENCODER_HPP_
Quadrature encoder with Gray code state machine and position tracking.
Definition encoder.hpp:31
int32_t position() const
Cumulative position (does not reset)
Definition encoder.hpp:83
int32_t delta()
Steps since last delta() call.
Definition encoder.hpp:76
void reset()
Reset all state.
Definition encoder.hpp:86
void update(bool a, bool b)
Feed quadrature signals.
Definition encoder.hpp:44
Root namespace for all Sound Byte Libs functionality.
Definition aliases.hpp:24