Deluge Firmware
1.3.0
Build date: 2026.10.14
Toggle main menu visibility
Loading...
Searching...
No Matches
encoder_quadrature.h
1
#pragma once
2
3
#include <cstdint>
4
5
namespace
deluge::hid::encoders {
6
22
class
QuadratureDecoder
{
23
public
:
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
53
private
:
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_
);
65
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
deluge::hid::encoders::QuadratureDecoder
Definition
encoder_quadrature.h:22
deluge::hid::encoders::QuadratureDecoder::transition
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
deluge::hid::encoders::QuadratureDecoder::initialize
void initialize(bool a, bool b)
Record the initial electrical state without reporting it as movement.
Definition
encoder_quadrature.h:25
deluge::hid::encoders::QuadratureDecoder::encode
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
deluge::hid::encoders::QuadratureDecoder::sampled_
uint8_t sampled_
Most recently observed packed A/B state.
Definition
encoder_quadrature.h:83
deluge::hid::encoders::QuadratureDecoder::committed_
uint8_t committed_
Last state whose edge has been confirmed and reported.
Definition
encoder_quadrature.h:80
deluge::hid::encoders::QuadratureDecoder::step
int8_t step(uint8_t next)
Definition
encoder_quadrature.h:56
deluge::hid::encoders::QuadratureDecoder::observeAEdge
int8_t observeAEdge(bool a, bool b, bool invert=false)
Definition
encoder_quadrature.h:43
deluge::hid::encoders::QuadratureDecoder::observe
int8_t observe(bool a, bool b, bool invert=false)
Definition
encoder_quadrature.h:31