Control Surface pin-t-adl
MIDI Control Surface library for Arduino
Bankable/Abstract/MIDIAbsoluteEncoder.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <AH/STL/utility> // std::forward
5#include <Def/Def.hpp>
6#include <Def/TypeTraits.hpp>
9
10#ifdef ARDUINO
12#else
13#include <Encoder.h> // Mock
14#endif
15
17
19
20namespace Bankable {
21
26template <class Enc, uint8_t NumBanks, class BankAddress, class Sender>
28 public:
29 GenericMIDIAbsoluteEncoder(const BankAddress &address, Enc &&encoder,
30 int16_t speedMultiply, uint8_t pulsesPerStep,
31 const Sender &sender)
32 : encoder(std::forward<Enc>(encoder)), address(address),
33 encstate(speedMultiply, pulsesPerStep), sender(sender) {}
34
35 void begin() override { begin_if_possible(encoder); }
36
37 void update() override {
38 auto encval = encoder.read();
39 if (int16_t delta = encstate.update(encval)) {
40 address.lock();
41 int16_t oldValue = values[address.getSelection()];
42 int16_t newValue = oldValue + delta;
43 newValue = constrain(newValue, 0, maxValue);
44 if (oldValue != newValue) {
45 values[address.getSelection()] = newValue;
47 }
48 address.unlock();
49 }
50 }
51
54 void forcedUpdate() {
55 sender.send(values[address.getSelection()], address.getActiveAddress());
56 }
58 sender.send(values[bank], address.getActiveAddress(bank));
59 }
60
64 uint16_t getValue(setting_t bank) const { return values[bank]; }
68 uint16_t getValue() const { return getValue(address.getSelection()); }
69
73 void setValue(uint16_t value, setting_t bank) { values[bank] = value; }
77 void setValue(uint16_t value) { setValue(value, address.getSelection()); }
78
80 static int16_t getMaxValue() { return maxValue; }
81
82 void setSpeedMultiply(int16_t speedMultiply) {
83 encstate.setSpeedMultiply(speedMultiply);
84 }
85 int16_t getSpeedMultiply() const { return encstate.getSpeedMultiply(); }
86
87 protected:
89 BankAddress address;
91 EncoderState<decltype(encoder.read())> encstate;
92
93 constexpr static int16_t maxValue = uint16_t(1u << Sender::precision()) - 1;
94
95 public:
96 Sender sender;
97};
98
99template <uint8_t NumBanks, class BankAddress, class Sender>
102
103template <uint8_t NumBanks, class BankAddress, class Sender>
106
107} // namespace Bankable
108
110
uint8_t setting_t
The type used for Selectors.
Definition: Def.hpp:53
#define END_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
std::enable_if< has_method_begin< T >::value >::type begin_if_possible(T &t)
Calls the begin() method of t if that method exists.
Definition: TypeTraits.hpp:23
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:36
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:35
A super class for object that have to be updated regularly.
Definition: Updatable.hpp:173
An abstract class for rotary encoders that send absolute MIDI events.
static int16_t getMaxValue()
Get the maximum possible value that can be returned by getValue.
void forcedUpdate()
Send the current value over MIDI, even if the position of the encoder didn't change.
uint16_t getValue() const
Get the absolute value of the encoder in the active bank.
void setValue(uint16_t value, setting_t bank)
Set the absolute value of the encoder in the given bank.
void setValue(uint16_t value)
Set the absolute value of the encoder in the active bank.
void begin() override
Initialize this updatable.
GenericMIDIAbsoluteEncoder(const BankAddress &address, Enc &&encoder, int16_t speedMultiply, uint8_t pulsesPerStep, const Sender &sender)
uint16_t getValue(setting_t bank) const
Get the absolute value of the encoder in the given bank.
Class to keep track of relative position changes of rotary encoders.
int16_t getSpeedMultiply() const
void setSpeedMultiply(int16_t speedMultiply)
int16_t update(Enc_t encval)
void forcedUpdate()
Send the current value over MIDI, even if the position of the encoder didn't change.
EncoderState< decltype(encoder.read())> encstate
A namespace for MIDI elements that can be added to a Bank, to change their address or channel.
An array wrapper for easy copying, comparing, and iterating.
Definition: Array.hpp:36