Control Surface master
MIDI Control Surface library for Arduino
SmartMIDIFilteredAnalog.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <AH/STL/algorithm> // std::fill
7#include <Def/Def.hpp>
9
11
13
14namespace Bankable {
15
27template <uint8_t NumBanks, class BankAddress, class Sender>
29 protected:
41 SmartMIDIFilteredAnalog(BankAddress bankAddress, pin_t analogPin,
42 const Sender &sender)
43 : address(bankAddress), filteredAnalog(analogPin), sender(sender) {}
44
45 public:
47 enum State {
59 };
60
61 void begin() override {
62 filteredAnalog.resetToCurrentValue();
63 previousBank = address.getSelection();
64 std::fill(std::begin(previousValues), std::end(previousValues),
65 initial);
66 }
67
68 void update() override {
69 auto activeBank = address.getSelection();
70 if (filteredAnalog.update() || activeBank != previousBank) {
71 auto previousValue = previousValues[activeBank];
72 auto value = filteredAnalog.getValue();
73 if (activeBank == previousBank && state == Higher) {
74 if (value <= previousValue)
75 state = Active;
76 } else if (activeBank == previousBank && state == Lower) {
77 if (value >= previousValue)
78 state = Active;
79 } else if (activeBank != previousBank) {
80 if (value == previousValue || previousValue == initial)
81 state = Active;
82 else if (value > previousValue)
83 state = Higher;
84 else
85 state = Lower;
86 previousBank = activeBank;
87 }
88 if (activeBank == previousBank && state == Active) {
89 sender.send(value, address.getActiveAddress());
90 previousValues[activeBank] = value;
91 }
92 }
93 }
94
99 State getState() const { return state; }
100
105 void activate() { state = Active; }
106
119 void map(MappingFunction fn) { filteredAnalog.map(fn); }
120
122 void invert() { filteredAnalog.invert(); }
123
128 analog_t getRawValue() const { return filteredAnalog.getRawValue(); }
129
134 analog_t getValue() const { return filteredAnalog.getValue(); }
135
140 return previousValues[bank];
141 }
142
147 return getPreviousValue(address.getSelection());
148 }
149
150 protected:
151 BankAddress address;
152 AH::FilteredAnalog<Sender::precision()> filteredAnalog;
153 static_assert(
154 Sender::precision() <= 14,
155 "Sender precision must be 14 or less, because larger values are "
156 "reserved.");
157 constexpr static analog_t initial = 1u << 14;
158 AH::Array<analog_t, NumBanks> previousValues = {{}};
159 State state = Active;
160 setting_t previousBank = 0;
161
162 public:
163 Sender sender;
164};
165
166} // namespace Bankable
167
169
uint8_t setting_t
The type used for Selectors.
Definition: Def.hpp:51
analog_t(*)(analog_t) MappingFunction
Definition: Def.hpp:21
#define END_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:36
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:35
A class that reads and filters an analog input.
A super class for object that have to be updated regularly.
Definition: Updatable.hpp:173
A class for potentiometers and faders that send MIDI events.
analog_t getValue() const
Get the value of the analog input (this is the value after first applying the mapping function).
void update() override
Update this updatable.
void map(MappingFunction fn)
Specify a mapping function that is applied to the raw analog value before sending.
State
State of the smart potentiometer.
@ Active
The potentiometer is active, the position changes will be sent over MIDI.
@ Higher
The value of the potentiometer is higher than the previously recorded value for the current bank.
@ Lower
The value of the potentiometer is lower than the previously recorded value for the current bank.
void begin() override
Initialize this updatable.
void invert()
Invert the analog value.
void activate()
Activate the potentiometer in the current bank, regardless of its current and previous position.
State getState() const
Get the state of the smart potentiometer, to know whether the position has to be lower or higher in o...
AH::FilteredAnalog< Sender::precision()> filteredAnalog
analog_t getRawValue() const
Get the raw value of the analog input (this is the value without applying the filter or the mapping f...
analog_t getPreviousValue(setting_t bank) const
Get the previous value of the analog input of the given bank.
SmartMIDIFilteredAnalog(BankAddress bankAddress, pin_t analogPin, const Sender &sender)
Construct a new SmartMIDIFilteredAnalog.
analog_t getPreviousValue() const
Get the previous value of the analog input of the active bank.
uint16_t analog_t
The type returned from analogRead and similar functions.
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
A namespace for MIDI elements that can be added to a Bank, to change their address or channel.