Deluge Firmware
1.3.0
Build date: 2026.11.28
Toggle main menu visibility
Loading...
Searching...
No Matches
encoders.h
1
/*
2
* Copyright © 2020-2023 Synthstrom Audible Limited
3
*
4
* This file is part of The Synthstrom Audible Deluge Firmware.
5
*
6
* The Synthstrom Audible Deluge Firmware is free software: you can redistribute it and/or modify it under the
7
* terms of the GNU General Public License as published by the Free Software Foundation,
8
* either version 3 of the License, or (at your option) any later version.
9
*
10
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
* See the GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License along with this program.
15
* If not, see <https://www.gnu.org/licenses/>.
16
*/
17
18
#pragma once
19
20
#include "OSLikeStuff/scheduler_api.h"
21
#include "hid/encoder_acceleration.h"
22
#include "hid/encoder_quadrature.h"
23
#include <atomic>
24
#include <cstddef>
25
#include <cstdint>
26
27
namespace
deluge::hid::encoders {
28
38
class
DetentedEncoder {
39
public
:
41
static
constexpr
bool
kRestA
=
true
;
42
static
constexpr
bool
kRestB =
true
;
43
44
DetentedEncoder() =
default
;
45
DetentedEncoder(
const
DetentedEncoder&) =
delete
;
46
DetentedEncoder& operator=(
const
DetentedEncoder&) =
delete
;
47
48
void
apply_edges(int8_t edges) {
49
edge_accumulator
+= edges;
50
int32_t ticks = 0;
51
while
(
edge_accumulator
>= positiveThreshold()) {
52
edge_accumulator
-= positiveThreshold();
53
last_direction_
= 1;
54
++ticks;
55
}
56
while
(
edge_accumulator
<= -negativeThreshold()) {
57
edge_accumulator
+= negativeThreshold();
58
last_direction_
= -1;
59
--ticks;
60
}
61
pos
.fetch_add(ticks, std::memory_order_relaxed);
62
}
63
65
bool
pending
()
const
{
return
pos
.load(std::memory_order_relaxed) != 0; }
66
68
int32_t
take
() {
return
pos
.exchange(0, std::memory_order_relaxed); }
69
72
void
restore
(int32_t val) {
pos
.fetch_add(val, std::memory_order_relaxed); }
73
74
private
:
75
int32_t positiveThreshold()
const
{
return
last_direction_
== 0 ? 3 : (
last_direction_
== 1 ? 4 : 2); }
76
int32_t negativeThreshold()
const
{
return
last_direction_
== 0 ? 3 : (
last_direction_
== -1 ? 4 : 2); }
77
78
int8_t
last_direction_
= 0;
79
int32_t
edge_accumulator
= 0;
80
std::atomic_int32_t
pos
= 0;
81
};
82
88
inline
void
initializeDecoderAtBoot(
QuadratureDecoder
& decoder,
bool
detented,
bool
pinA,
bool
pinB) {
89
if
(detented) {
90
decoder.
initialize
(
DetentedEncoder::kRestA
, DetentedEncoder::kRestB);
91
}
92
else
{
93
decoder.
initialize
(pinA, pinB);
94
}
95
}
96
99
class
ContinuousEncoder {
100
public
:
101
ContinuousEncoder() =
default
;
102
ContinuousEncoder(
const
ContinuousEncoder&) =
delete
;
103
ContinuousEncoder& operator=(
const
ContinuousEncoder&) =
delete
;
104
105
void
apply_edges(int8_t edges) {
pos
.fetch_add(edges, std::memory_order_relaxed); }
106
108
bool
pending
()
const
{
return
pos
.load(std::memory_order_relaxed) != 0; }
109
111
int8_t
take
() {
return
pos
.exchange(0, std::memory_order_relaxed); }
112
114
double
calcNextKnobSpeed
(int8_t offset);
115
116
private
:
117
std::atomic_int8_t
pos
= 0;
118
EncoderAcceleration
acceleration_;
119
};
120
121
// ── Named encoder globals ─────────────────────────────────────────────────
122
123
extern
DetentedEncoder
scrollY;
124
extern
DetentedEncoder
scrollX;
125
extern
DetentedEncoder
tempo;
126
extern
DetentedEncoder
select;
127
extern
ContinuousEncoder
mod0;
128
extern
ContinuousEncoder
mod1;
129
130
// ── Array-index access for iteration ─────────────────────────────────────
131
132
constexpr
size_t
kNumFunctionEncoders = 4;
133
constexpr
size_t
kNumModEncoders = 2;
134
135
DetentedEncoder
& functionEncoderAt(
size_t
i);
136
ContinuousEncoder
& modEncoderAt(
size_t
i);
137
138
// ── Shared timestamp ──────────────────────────────────────────────────────
139
142
extern
uint32_t timeModEncoderLastTurned[];
143
144
// ── Lifecycle ─────────────────────────────────────────────────────────────
145
146
void
init();
147
void
poll_encoder_pins();
148
151
extern
TaskID EncoderTaskID;
152
153
}
// namespace deluge::hid::encoders
deluge::hid::encoders::ContinuousEncoder
Definition
encoders.h:99
deluge::hid::encoders::ContinuousEncoder::pending
bool pending() const
True if any ticks are waiting to be consumed.
Definition
encoders.h:108
deluge::hid::encoders::ContinuousEncoder::take
int8_t take()
Returns the accumulated tick count and resets pos to 0.
Definition
encoders.h:111
deluge::hid::encoders::ContinuousEncoder::pos
std::atomic_int8_t pos
Written by the IRQ (apply_edges), drained by the encoder task.
Definition
encoders.h:117
deluge::hid::encoders::ContinuousEncoder::calcNextKnobSpeed
double calcNextKnobSpeed(int8_t offset)
Returns multiplier for encoder offset.
Definition
encoders.cpp:32
deluge::hid::encoders::DetentedEncoder
Definition
encoders.h:38
deluge::hid::encoders::DetentedEncoder::restore
void restore(int32_t val)
Definition
encoders.h:72
deluge::hid::encoders::DetentedEncoder::pending
bool pending() const
True if any detent steps are waiting to be consumed.
Definition
encoders.h:65
deluge::hid::encoders::DetentedEncoder::take
int32_t take()
Returns the accumulated signed detent count and resets pos to 0.
Definition
encoders.h:68
deluge::hid::encoders::DetentedEncoder::edge_accumulator
int32_t edge_accumulator
Accumulates quadrature edges until we have a full detent click.
Definition
encoders.h:79
deluge::hid::encoders::DetentedEncoder::last_direction_
int8_t last_direction_
Direction of the last reported detent, not the last electrical edge.
Definition
encoders.h:78
deluge::hid::encoders::DetentedEncoder::pos
std::atomic_int32_t pos
Written by the IRQ (apply_edges), drained by the encoder task.
Definition
encoders.h:80
deluge::hid::encoders::DetentedEncoder::kRestA
static constexpr bool kRestA
Black encoders rest in a detent with both contacts open (A and B pulled high), as 1....
Definition
encoders.h:41
deluge::hid::encoders::EncoderAcceleration
Definition
encoder_acceleration.h:8
deluge::hid::encoders::QuadratureDecoder
Definition
encoder_quadrature.h:22
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