Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
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
12namespace Bankable {
13
25template <uint8_t NumBanks, class BankAddress, class Sender>
27 protected:
39 SmartMIDIFilteredAnalog(BankAddress bankAddress, pin_t analogPin,
40 const Sender &sender)
41 : address(bankAddress), filteredAnalog(analogPin), sender(sender) {}
42
43 public:
58
59 void begin() override {
61 previousBank = address.getSelection();
62 std::fill(std::begin(previousValues), std::end(previousValues),
63 initial);
64 }
65
66 void update() override {
67 auto activeBank = address.getSelection();
68 if (filteredAnalog.update() || activeBank != previousBank) {
69 auto previousValue = previousValues[activeBank];
70 auto value = filteredAnalog.getValue();
71 if (activeBank == previousBank && state == Higher) {
72 if (value <= previousValue)
73 state = Active;
74 } else if (activeBank == previousBank && state == Lower) {
75 if (value >= previousValue)
76 state = Active;
77 } else if (activeBank != previousBank) {
78 if (value == previousValue || previousValue == initial)
79 state = Active;
80 else if (value > previousValue)
81 state = Higher;
82 else
83 state = Lower;
84 previousBank = activeBank;
85 }
86 if (activeBank == previousBank && state == Active) {
87 sender.send(value, address.getActiveAddress());
88 previousValues[activeBank] = value;
89 }
90 }
91 }
92
97 State getState() const { return state; }
98
103 void activate() { state = Active; }
104
118
121
126 analog_t getRawValue() const { return filteredAnalog.getRawValue(); }
127
131 static constexpr analog_t getMaxRawValue() {
133 }
134
139 analog_t getValue() const { return filteredAnalog.getValue(); }
140
144 analog_t getPreviousValue(setting_t bank) const {
145 return previousValues[bank];
146 }
147
151 analog_t getPreviousValue() const {
152 return getPreviousValue(address.getSelection());
153 }
154
155 protected:
156 BankAddress address;
157 using FilteredAnalog = AH::FilteredAnalog<Sender::precision()>;
159 static_assert(
160 Sender::precision() <= 14,
161 "Sender precision must be 14 or less, because larger values are "
162 "reserved.");
163 constexpr static analog_t initial = 1u << 14;
167
168 public:
169 Sender sender;
170};
171
172} // namespace Bankable
173
uint8_t setting_t
The type used for Selectors.
Definition Def.hpp:53
analog_t(*)(analog_t) MappingFunction
Definition Def.hpp:23
#define END_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
void invert()
Invert the analog value.
void resetToCurrentValue()
Reset the filtered value to the value that's currently being measured at the analog input.
void map(MappingFunction fn)
Specify a mapping function/functor that is applied to the analog value after filtering and before app...
AnalogType getRawValue() const
Read the raw value of the analog input without any filtering or mapping applied, but with its bit dep...
AnalogType getValue() const
Get the filtered value of the analog input (with the mapping function applied).
bool update()
Read the analog input value, apply the mapping function, and update the average.
static constexpr AnalogType getMaxRawValue()
Get the maximum value that can be returned from getRawValue.
A class for serial-in/parallel-out shift registers, like the 74HC595 that are connected to the SPI bu...
A super class for object that have to be updated regularly.
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.
static constexpr analog_t getMaxRawValue()
Get the maximum value that can be returned from getRawValue.
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.
AH::Array< analog_t, NumBanks > previousValues
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...
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.
A namespace for MIDI elements that can be added to a Bank, to change their address or channel.
Type for storing pin numbers of Extended Input/Output elements.