Control Surface  1.2.0
MIDI Control Surface library for Arduino
BankableMIDIInput.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Bank.hpp"
4 #include "BankConfig.hpp"
6 #include <AH/Debug/Debug.hpp>
7 #include <Def/MIDIAddress.hpp>
8 
10 
20 template <setting_t N>
21 class BankableMIDIInput : public DoublyLinkable<BankableMIDIInput<N>> {
22  friend class Bank<N>;
23 
24  protected:
34  bank.add(this);
35  }
36 
46  : BankableMIDIInput<N>(config.bank, config.type) {}
47 
48  public:
52  virtual ~BankableMIDIInput() { bank.remove(this); }
53 
59  setting_t getSelection() const { return bank.getSelection(); }
60 
71  uint8_t getBankIndex(const MIDIAddress &target,
72  const MIDIAddress &base) const {
73  switch (type) {
74  case CHANGE_ADDRESS:
75  return (target.getAddress() - base.getAddress()) /
76  bank.getTracksPerBank();
77  case CHANGE_CHANNEL:
78  return (target.getRawChannel() - base.getRawChannel()) /
79  bank.getTracksPerBank();
80  case CHANGE_CABLENB:
81  return (target.getRawCableNumber() - base.getRawCableNumber()) /
82  bank.getTracksPerBank();
83  default: return 0;
84  }
85  }
86 
87  protected:
113  bool matchBankable(uint8_t toMatch, uint8_t base) const {
114  uint8_t diff = toMatch - base;
115  return toMatch >= base && diff < N * bank.getTracksPerBank() &&
116  diff % bank.getTracksPerBank() == 0;
117  }
118 
132  bool matchBankableInRange(uint8_t toMatch, uint8_t base,
133  uint8_t length) const {
134  uint8_t diff = toMatch - base;
135  return toMatch >= base && diff < N * bank.getTracksPerBank() &&
136  diff % bank.getTracksPerBank() < length;
137  }
138 
143  uint8_t getRangeIndex(MIDIAddress target, MIDIAddress base) const {
144  uint8_t diff = target.getAddress() - base.getAddress();
145  if (type == CHANGE_ADDRESS)
146  diff %= bank.getTracksPerBank();
147  return diff;
148  }
149 
161  static bool inRange(uint8_t toMatch, uint8_t base, uint8_t length) {
162  return (base <= toMatch) && (toMatch - base < length);
163  }
164 
174  bool matchBankable(const MIDIAddress &toMatch,
175  const MIDIAddress &base) const {
176  if (!toMatch.isValid() || !base.isValid())
177  return false;
178  switch (type) {
179  case CHANGE_ADDRESS: {
180  return toMatch.getChannel() == base.getChannel() &&
181  toMatch.getCableNumber() == base.getCableNumber() &&
182  matchBankable(toMatch.getAddress(), base.getAddress());
183  }
184  case CHANGE_CHANNEL: {
185  return toMatch.getAddress() == base.getAddress() &&
186  toMatch.getCableNumber() == base.getCableNumber() &&
187  matchBankable(toMatch.getRawChannel(),
188  base.getRawChannel());
189  }
190  case CHANGE_CABLENB: {
191  return toMatch.getAddress() == base.getAddress() &&
192  toMatch.getChannel() == base.getChannel() &&
194  base.getRawCableNumber());
195  }
196  default: return false;
197  }
198  }
199 
212  const MIDIAddress &base,
213  uint8_t length) const {
214  if (!toMatch.isValid() || !base.isValid())
215  return false;
216  switch (type) {
217  case CHANGE_ADDRESS: {
218  return toMatch.getChannel() == base.getChannel() &&
219  toMatch.getCableNumber() == base.getCableNumber() &&
221  base.getAddress(), length);
222  }
223  case CHANGE_CHANNEL: {
224  return inRange(toMatch.getAddress(), base.getAddress(),
225  length) &&
226  toMatch.getCableNumber() == base.getCableNumber() &&
227  matchBankable(toMatch.getRawChannel(),
228  base.getRawChannel());
229  }
230  case CHANGE_CABLENB: {
231  return inRange(toMatch.getAddress(), base.getAddress(),
232  length) &&
233  toMatch.getChannel() == base.getChannel() &&
235  base.getRawCableNumber());
236  }
237  default: return false;
238  }
239  }
240 
241  private:
243  const BankType type;
244 
255  virtual void onBankSettingChange() {}
256 };
257 
MIDIAddress::getRawChannel
constexpr uint8_t getRawChannel() const
Get the channel as an integer [0, 15].
Definition: MIDIAddress.hpp:228
Bank
A class that groups Bankable BankableMIDIOutputs and BankableMIDIInputs, and allows the user to chang...
Definition: Bank.hpp:75
MIDIAddress.hpp
MIDIAddress
A type-safe utility class for saving a MIDI address consisting of a 7-bit address,...
Definition: MIDIAddress.hpp:91
BankableMIDIInput::matchBankableAddressInRange
bool matchBankableAddressInRange(const MIDIAddress &toMatch, const MIDIAddress &base, uint8_t length) const
Check whether a given address is part of the bank relative to the base address and within a range wit...
Definition: BankableMIDIInput.hpp:211
BankConfig.hpp
MIDIAddress::getCableNumber
constexpr Cable getCableNumber() const
Get the cable number [CABLE_1, CABLE_16].
Definition: MIDIAddress.hpp:231
BankableMIDIInput::getRangeIndex
uint8_t getRangeIndex(MIDIAddress target, MIDIAddress base) const
If matchBankableAddressInRange returned true, get the index of the message in the range.
Definition: BankableMIDIInput.hpp:143
BankableMIDIInput::getBankIndex
uint8_t getBankIndex(const MIDIAddress &target, const MIDIAddress &base) const
Calculate the bank setting of a given MIDI address, relative to a base address.
Definition: BankableMIDIInput.hpp:71
BankableMIDIInput::matchBankable
bool matchBankable(const MIDIAddress &toMatch, const MIDIAddress &base) const
Check whether a given address is part of the bank relative to the base address.
Definition: BankableMIDIInput.hpp:174
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:9
BankableMIDIInput::BankableMIDIInput
BankableMIDIInput(Bank< N > &bank, BankType type)
Create a new BankableMIDIInput object, and add it to the bank.
Definition: BankableMIDIInput.hpp:33
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:10
LinkedList.hpp
BankableMIDIInput::BankableMIDIInput
BankableMIDIInput(BankConfig< N > config)
Create a new BankableMIDIInput object, and add it to the bank.
Definition: BankableMIDIInput.hpp:45
BankableMIDIInput::getSelection
setting_t getSelection() const
Get the current bank setting.
Definition: BankableMIDIInput.hpp:59
BankableMIDIInput::bank
Bank< N > & bank
Definition: BankableMIDIInput.hpp:242
BankableMIDIInput::matchBankable
bool matchBankable(uint8_t toMatch, uint8_t base) const
Check if the given address is part of the bank relative to the base address.
Definition: BankableMIDIInput.hpp:113
CHANGE_CHANNEL
@ CHANGE_CHANNEL
Change the offset of the channel number of the element.
Definition: BankConfig.hpp:21
BankableMIDIInput::inRange
static bool inRange(uint8_t toMatch, uint8_t base, uint8_t length)
Check whether a given address is within a range of given length starting from the given base address.
Definition: BankableMIDIInput.hpp:161
MIDIAddress::isValid
constexpr bool isValid() const
Check if the MIDI address is valid.
Definition: MIDIAddress.hpp:268
CHANGE_CABLENB
@ CHANGE_CABLENB
Change the offset of the cable number of the element.
Definition: BankConfig.hpp:25
BankType
BankType
An enumeration of the different bank types.
Definition: BankConfig.hpp:12
BankableMIDIInput::matchBankableInRange
bool matchBankableInRange(uint8_t toMatch, uint8_t base, uint8_t length) const
Check if the given address is part of the bank relative to the base address.
Definition: BankableMIDIInput.hpp:132
setting_t
uint8_t setting_t
The type used for Selectors.
Definition: Def.hpp:50
DoublyLinkable
A class that can be inherited from to allow inserting into a DoublyLinkedList.
Definition: LinkedList.hpp:320
Bank.hpp
MIDIAddress::getAddress
constexpr uint8_t getAddress() const
Get the address [0, 127].
Definition: MIDIAddress.hpp:223
MIDIAddress::getChannel
constexpr Channel getChannel() const
Get the channel [CHANNEL_1, CHANNEL_16].
Definition: MIDIAddress.hpp:226
Debug.hpp
BankableMIDIInput
A base class for all MIDIInputElements that can be banked.
Definition: BankableMIDIInput.hpp:21
MIDIAddress::getRawCableNumber
constexpr uint8_t getRawCableNumber() const
Get the cable number as an integer [0, 15].
Definition: MIDIAddress.hpp:235
BankConfig
Definition: BankConfig.hpp:51
CHANGE_ADDRESS
@ CHANGE_ADDRESS
Change the offset of the address (i.e.
Definition: BankConfig.hpp:17
BankableMIDIInput::onBankSettingChange
virtual void onBankSettingChange()
A function to be executed each time the bank setting changes.
Definition: BankableMIDIInput.hpp:255
BankableMIDIInput::type
const BankType type
Definition: BankableMIDIInput.hpp:243
BankableMIDIInput::~BankableMIDIInput
virtual ~BankableMIDIInput()
Destructor: remove element from the bank.
Definition: BankableMIDIInput.hpp:52