Deluge Firmware 1.3.0
Build date: 2026.10.14
Loading...
Searching...
No Matches
encoder_quadrature.h
1#pragma once
2
3#include <cstdint>
4
5namespace deluge::hid::encoders {
6
23public:
25 void initialize(bool a, bool b) { committed_ = sampled_ = encode(a, b); }
26
31 int8_t observe(bool a, bool b, bool invert = false) {
32 const uint8_t next = encode(a, b);
33 if ((sampled_ ^ next) == 0b11) {
34 return 0;
35 }
36 const int8_t movement = step(next);
37 return invert ? -movement : movement;
38 }
39
43 int8_t observeAEdge(bool a, bool b, bool invert = false) {
44 const uint8_t next = encode(a, b);
45 int8_t movement = 0;
46 if ((sampled_ ^ next) == 0b11) {
47 movement += step(sampled_ ^ 0b01);
48 }
49 movement += step(next);
50 return invert ? -movement : movement;
51 }
52
53private:
56 int8_t step(uint8_t next) {
57 if ((committed_ ^ next) != 0b11) {
58 // Either back where we were last confirmed (a bounce), or only one pin away from it
59 // (unconfirmed). Neither reports movement.
60 sampled_ = next;
61 return 0;
62 }
63 // Both pins now differ from the confirmed state, so the edge to `sampled_` was real.
64 const int8_t movement = transition(committed_, sampled_);
66 sampled_ = next;
67 return movement;
68 }
69
71 static constexpr uint8_t encode(bool a, bool b) { return (static_cast<uint8_t>(a) << 1) | b; }
72
74 static constexpr int8_t transition(uint8_t from, uint8_t to) {
75 constexpr int8_t table[16] = {0, 1, -1, 0, -1, 0, 0, 1, 1, 0, 0, -1, 0, -1, 1, 0};
76 return table[(from << 2) | to];
77 }
78
80 uint8_t committed_ = 0;
81
83 uint8_t sampled_ = 0;
84};
85
86} // namespace deluge::hid::encoders
Definition encoder_quadrature.h:22
static constexpr int8_t transition(uint8_t from, uint8_t to)
Map every previous/current state pair to positive, negative, or no movement.
Definition encoder_quadrature.h:74
void initialize(bool a, bool b)
Record the initial electrical state without reporting it as movement.
Definition encoder_quadrature.h:25
static constexpr uint8_t encode(bool a, bool b)
Pack the two Boolean pin levels into the lookup-table state order AB.
Definition encoder_quadrature.h:71
uint8_t sampled_
Most recently observed packed A/B state.
Definition encoder_quadrature.h:83
uint8_t committed_
Last state whose edge has been confirmed and reported.
Definition encoder_quadrature.h:80
int8_t step(uint8_t next)
Definition encoder_quadrature.h:56
int8_t observeAEdge(bool a, bool b, bool invert=false)
Definition encoder_quadrature.h:43
int8_t observe(bool a, bool b, bool invert=false)
Definition encoder_quadrature.h:31