Control Surface master
MIDI Control Surface library for Arduino
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
19
24template <class Enc, class Sender>
26 public:
28 int16_t speedMultiply, uint8_t pulsesPerStep,
29 const Sender &sender)
30 : encoder(std::forward<Enc>(encoder)), address(address),
31 encstate(speedMultiply, pulsesPerStep), sender(sender) {}
32
33 void begin() override { begin_if_possible(encoder); }
34
35 void update() override {
36 auto encval = encoder.read();
37 if (int16_t delta = encstate.update(encval)) {
38 int16_t oldValue = value;
39 int16_t newValue = oldValue + delta;
40 newValue = constrain(newValue, 0, maxValue);
41 if (oldValue != newValue) {
42 value = newValue;
43 forcedUpdate();
44 }
45 }
46 }
47
50 void forcedUpdate() { sender.send(value, address); }
51
55 uint16_t getValue() const { return value; }
56
60 void setValue(uint16_t value) { this->value = value; }
61
63 static int16_t getMaxValue() { return maxValue; }
64
65 void setSpeedMultiply(int16_t speedMultiply) {
66 encstate.setSpeedMultiply(speedMultiply);
67 }
68 int16_t getSpeedMultiply() const { return encstate.getSpeedMultiply(); }
69
71 MIDIAddress getAddress() const { return this->address; }
73 void setAddress(MIDIAddress address) { this->address = address; }
74
75 private:
78 int16_t value = 0;
79 EncoderState<decltype(encoder.read())> encstate;
80
81 constexpr static int16_t maxValue = uint16_t(1u << Sender::precision()) - 1;
82
83 public:
84 Sender sender;
85};
86
87template <class Sender>
89
90template <class Sender>
93
95
#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
Class to keep track of relative position changes of rotary encoders.
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.
A type-safe utility class for saving a MIDI address consisting of a 7-bit address,...