Deluge Firmware 1.3.0
Build date: 2026.11.28
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
27namespace deluge::hid::encoders {
28
38class DetentedEncoder {
39public:
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();
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
74private:
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
88inline 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
99class ContinuousEncoder {
100public:
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
116private:
117 std::atomic_int8_t pos = 0;
118 EncoderAcceleration acceleration_;
119};
120
121// ── Named encoder globals ─────────────────────────────────────────────────
122
123extern DetentedEncoder scrollY;
124extern DetentedEncoder scrollX;
125extern DetentedEncoder tempo;
126extern DetentedEncoder select;
127extern ContinuousEncoder mod0;
128extern ContinuousEncoder mod1;
129
130// ── Array-index access for iteration ─────────────────────────────────────
131
132constexpr size_t kNumFunctionEncoders = 4;
133constexpr size_t kNumModEncoders = 2;
134
135DetentedEncoder& functionEncoderAt(size_t i);
136ContinuousEncoder& modEncoderAt(size_t i);
137
138// ── Shared timestamp ──────────────────────────────────────────────────────
139
142extern uint32_t timeModEncoderLastTurned[];
143
144// ── Lifecycle ─────────────────────────────────────────────────────────────
145
146void init();
147void poll_encoder_pins();
148
151extern TaskID EncoderTaskID;
152
153} // namespace deluge::hid::encoders
bool pending() const
True if any ticks are waiting to be consumed.
Definition encoders.h:108
int8_t take()
Returns the accumulated tick count and resets pos to 0.
Definition encoders.h:111
std::atomic_int8_t pos
Written by the IRQ (apply_edges), drained by the encoder task.
Definition encoders.h:117
double calcNextKnobSpeed(int8_t offset)
Returns multiplier for encoder offset.
Definition encoders.cpp:32
void restore(int32_t val)
Definition encoders.h:72
bool pending() const
True if any detent steps are waiting to be consumed.
Definition encoders.h:65
int32_t take()
Returns the accumulated signed detent count and resets pos to 0.
Definition encoders.h:68
int32_t edge_accumulator
Accumulates quadrature edges until we have a full detent click.
Definition encoders.h:79
int8_t last_direction_
Direction of the last reported detent, not the last electrical edge.
Definition encoders.h:78
std::atomic_int32_t pos
Written by the IRQ (apply_edges), drained by the encoder task.
Definition encoders.h:80
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
Definition encoder_acceleration.h:8
Definition encoder_quadrature.h:22
void initialize(bool a, bool b)
Record the initial electrical state without reporting it as movement.
Definition encoder_quadrature.h:25