Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
Abstract/MIDIAbsoluteEncoder.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <AH/STL/type_traits> // std::make_signed
4#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
22template <class Enc, class Sender>
24 public:
26 int16_t speedMultiply, uint8_t pulsesPerStep,
27 const Sender &sender)
28 : encoder(std::forward<Enc>(encoder)), address(address),
29 encstate(speedMultiply, pulsesPerStep), sender(sender) {}
30
31 void begin() override { begin_if_possible(encoder); }
32
33 void update() override {
34 auto encval = encoder.read();
35 if (int16_t delta = encstate.update(encval)) {
36 int16_t oldValue = value;
37 int16_t newValue = oldValue + delta;
38 newValue = constrain(newValue, 0, maxValue);
39 if (oldValue != newValue) {
40 value = newValue;
42 }
43 }
44 }
45
48 void forcedUpdate() { sender.send(value, address); }
49
53 uint16_t getValue() const { return value; }
54
58 void setValue(uint16_t value) { this->value = value; }
59
61 static int16_t getMaxValue() { return maxValue; }
62
63 void setSpeedMultiply(int16_t speedMultiply) {
64 encstate.setSpeedMultiply(speedMultiply);
65 }
66 int16_t getSpeedMultiply() const { return encstate.getSpeedMultiply(); }
67
69 MIDIAddress getAddress() const { return this->address; }
71 void setAddress(MIDIAddress address) { this->address = address; }
72
74 auto encval = encoder.read();
75 return encstate.update(encval);
76 }
77
78 private:
81 int16_t value = 0;
82 EncoderState<decltype(encoder.read())> encstate;
83
84 constexpr static int16_t maxValue = uint16_t(1u << Sender::precision()) - 1;
85
86 public:
87 Sender sender;
88};
89
90template <class Sender>
92
93template <class Sender>
96
#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.
A super class for object that have to be updated regularly.
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)
An abstract class for rotary encoders that send absolute MIDI events.
void setAddress(MIDIAddress address)
Set the MIDI address.
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.
void setSpeedMultiply(int16_t speedMultiply)
void update() override
Update this updatable.
void setValue(uint16_t value)
Set the absolute value of the encoder.
GenericMIDIAbsoluteEncoder(Enc &&encoder, MIDIAddress address, int16_t speedMultiply, uint8_t pulsesPerStep, const Sender &sender)
void begin() override
Initialize this updatable.
MIDIAddress getAddress() const
Get the MIDI address.
EncoderState< decltype(encoder.read())> encstate
A type-safe utility class for saving a MIDI address consisting of a 7-bit address,...