Control Surface  1.2.0
MIDI Control Surface library for Arduino
SmartMIDIFilteredAnalog.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 #include <AH/Hardware/Button.hpp>
5 #include <AH/STL/algorithm> // std::fill
7 #include <Def/Def.hpp>
9 
11 
13 
14 namespace Bankable {
15 
27 template <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  previousBank = address.getSelection();
63  std::fill(std::begin(previousValues), std::end(previousValues),
64  initial);
65  }
66  void update() override {
67  if (filteredAnalog.update()) {
68  address.lock();
69  auto activeBank = address.getSelection();
70  auto previousValue = previousValues[activeBank];
71  auto value = filteredAnalog.getValue();
72  if (activeBank == previousBank && state == Higher) {
73  if (value <= previousValue)
74  state = Active;
75  } else if (activeBank == previousBank && state == Lower) {
76  if (value >= previousValue)
77  state = Active;
78  } else if (activeBank != previousBank) {
79  if (value == previousValue || previousValue == initial)
80  state = Active;
81  else if (value > previousValue)
82  state = Higher;
83  else
84  state = Lower;
85  previousBank = activeBank;
86  }
87  if (activeBank == previousBank && state == Active) {
88  sender.send(value, address.getActiveAddress());
89  previousValues[activeBank] = value;
90  }
91  address.unlock();
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 
136  private:
137  BankAddress address;
138  AH::FilteredAnalog<Sender::precision()> filteredAnalog;
139  static_assert(
140  Sender::precision() <= 14,
141  "Sender precision must be 14 or less, because larger values are "
142  "reserved.");
143  constexpr static analog_t initial = 1u << 14;
144  AH::Array<analog_t, NumBanks> previousValues = {{}};
145  State state = Active;
146  setting_t previousBank = 0;
147 
148  public:
149  Sender sender;
150 };
151 
152 } // namespace Bankable
153 
155 
Bankable::SmartMIDIFilteredAnalog::activate
void activate()
Activate the potentiometer in the current bank, regardless of its current and previous position.
Definition: SmartMIDIFilteredAnalog.hpp:105
Bankable::SmartMIDIFilteredAnalog::Lower
@ Lower
The value of the potentiometer is lower than the previously recorded value for the current bank.
Definition: SmartMIDIFilteredAnalog.hpp:54
AH::Updatable<>
Bankable
A namespace for MIDI elements that can be added to a Bank, to change their address or channel.
Definition: BankAddresses.hpp:7
Button.hpp
Bankable::SmartMIDIFilteredAnalog::filteredAnalog
AH::FilteredAnalog< Sender::precision()> filteredAnalog
Definition: SmartMIDIFilteredAnalog.hpp:138
AH::pin_t
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
Definition: Hardware-Types.hpp:17
MIDIOutputElement.hpp
Def.hpp
Bankable::SmartMIDIFilteredAnalog::getValue
analog_t getValue() const
Get the value of the analog input (this is the value after first applying the mapping function).
Definition: SmartMIDIFilteredAnalog.hpp:134
BitArray.hpp
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:9
AH_DIAGNOSTIC_POP
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:36
AH::FilteredAnalog
A class that reads and filters an analog input.
Definition: FilteredAnalog.hpp:249
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:10
Bankable::SmartMIDIFilteredAnalog::getState
State getState() const
Get the state of the smart potentiometer, to know whether the position has to be lower or higher in o...
Definition: SmartMIDIFilteredAnalog.hpp:99
Bankable::SmartMIDIFilteredAnalog::map
void map(MappingFunction fn)
Specify a mapping function that is applied to the raw analog value before sending.
Definition: SmartMIDIFilteredAnalog.hpp:119
AH::Array< analog_t, NumBanks >
Bankable::SmartMIDIFilteredAnalog
A class for potentiometers and faders that send MIDI events.
Definition: SmartMIDIFilteredAnalog.hpp:28
Bankable::SmartMIDIFilteredAnalog::SmartMIDIFilteredAnalog
SmartMIDIFilteredAnalog(BankAddress bankAddress, pin_t analogPin, const Sender &sender)
Construct a new SmartMIDIFilteredAnalog.
Definition: SmartMIDIFilteredAnalog.hpp:41
Bankable::SmartMIDIFilteredAnalog::Higher
@ Higher
The value of the potentiometer is higher than the previously recorded value for the current bank.
Definition: SmartMIDIFilteredAnalog.hpp:58
AH::analog_t
uint16_t analog_t
The type returned from analogRead and similar functions.
Definition: Hardware-Types.hpp:15
Bankable::SmartMIDIFilteredAnalog::sender
Sender sender
Definition: SmartMIDIFilteredAnalog.hpp:149
Bankable::SmartMIDIFilteredAnalog::getRawValue
analog_t getRawValue() const
Get the raw value of the analog input (this is the value without applying the filter or the mapping f...
Definition: SmartMIDIFilteredAnalog.hpp:128
setting_t
uint8_t setting_t
The type used for Selectors.
Definition: Def.hpp:50
AH_DIAGNOSTIC_WERROR
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:35
Bankable::SmartMIDIFilteredAnalog::Active
@ Active
The potentiometer is active, the position changes will be sent over MIDI.
Definition: SmartMIDIFilteredAnalog.hpp:50
BankableAddresses.hpp
Bankable::SmartMIDIFilteredAnalog< NumBanks, SingleAddress, ContinuousCCSender >::State
State
State of the smart potentiometer.
Definition: SmartMIDIFilteredAnalog.hpp:47
Bankable::SmartMIDIFilteredAnalog::invert
void invert()
Invert the analog value.
Definition: SmartMIDIFilteredAnalog.hpp:122
Bankable::SmartMIDIFilteredAnalog::update
void update() override
Update this updatable.
Definition: SmartMIDIFilteredAnalog.hpp:66
Bankable::SmartMIDIFilteredAnalog::begin
void begin() override
Initialize this updatable.
Definition: SmartMIDIFilteredAnalog.hpp:61
Bankable::SmartMIDIFilteredAnalog::address
BankAddress address
Definition: SmartMIDIFilteredAnalog.hpp:137
MappingFunction
analog_t(*)(analog_t) MappingFunction
Definition: Def.hpp:20