Control Surface new-input
MIDI Control Surface library for Arduino
Bank.hpp
Go to the documentation of this file.
1 /* ✔ */
2 
3 #pragma once
4 
6 #include <AH/Debug/Debug.hpp>
7 #include <AH/Error/Error.hpp>
9 
11 
12 template <setting_t N>
14 
16 class OutputBank {
17  public:
28  OutputBank(uint8_t tracksPerBank = 1, setting_t initialSelection = 0,
29  int8_t selectionOffset = 0)
30  : tracksPerBank(tracksPerBank), bankSetting(initialSelection),
32  if (tracksPerBank == 0)
33  FATAL_ERROR(F("A Bank must have a non-zero number of tracks."),
34  0x4573);
35  }
36 
41  void select(setting_t setting) { bankSetting = setting; }
42 
44  setting_t getSelection() const { return bankSetting; }
46  int8_t getSelectionOffset() const { return selectionOffset; }
47 
50  uint8_t getTracksPerBank() const { return tracksPerBank; }
51 
53  int8_t getOffsetOfSetting(setting_t s) const {
54  return (s + getSelectionOffset()) * getTracksPerBank();
55  }
58  int8_t getOffset() const { return getOffsetOfSetting(getSelection()); }
59 
60  private:
61  uint8_t tracksPerBank;
64 };
65 
69  : public DoublyLinkable<BankSettingChangeCallback> {
70  template <setting_t N>
71  friend class Bank;
72 
73  private:
81  virtual void onBankSettingChange() {}
82 };
83 
90 template <setting_t NumBanks>
91 class Bank : public Selectable<NumBanks>, public OutputBank {
92 
93  public:
105  int8_t selectionOffset = 0)
106  : Selectable<NumBanks>(initialSelection),
108 
116  void select(setting_t bankSetting) override;
117 
119  constexpr static uint8_t getNumberOfBanks() { return NumBanks; }
120 
121  public:
127 
133 
134  private:
140 };
141 
143 
144 // ---------------------------- Implementations ----------------------------- //
145 
147 
148 template <setting_t N>
150  inputBankables.append(bankable);
151 }
152 
153 template <setting_t N>
155  inputBankables.remove(bankable);
156 }
157 
158 template <setting_t N>
159 void Bank<N>::select(setting_t bankSetting) {
160  bankSetting = this->validateSetting(bankSetting);
161  OutputBank::select(bankSetting);
162  for (BankSettingChangeCallback &e : inputBankables)
163  e.onBankSettingChange();
164 }
165 
uint8_t setting_t
The type used for Selectors.
Definition: Def.hpp:51
#define END_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Callback class for Bankable objects that need to be notified when the active setting of their Bank ch...
Definition: Bank.hpp:69
virtual void onBankSettingChange()
A function to be executed each time the bank setting changes.
Definition: Bank.hpp:81
A class that groups Bankable MIDI Output Elements and Bankable MIDI Input Elements,...
Definition: Bank.hpp:91
constexpr static uint8_t getNumberOfBanks()
Get the number of banks.
Definition: Bank.hpp:119
void add(BankSettingChangeCallback *bankable)
Add a Bankable MIDI Input Element to the bank.
Definition: Bank.hpp:149
void remove(BankSettingChangeCallback *bankable)
Remove a Bankable MIDI Input Element from the bank.
Definition: Bank.hpp:154
void select(setting_t bankSetting) override
Select the given bank setting.
Definition: Bank.hpp:159
Bank(uint8_t tracksPerBank=1, setting_t initialSelection=0, int8_t selectionOffset=0)
Construct a new Bank object.
Definition: Bank.hpp:104
DoublyLinkedList< BankSettingChangeCallback > inputBankables
A linked list of all Bankable MIDI Input Elements that have been added to this bank,...
Definition: Bank.hpp:139
A class that can be inherited from to allow inserting into a DoublyLinkedList.
Definition: LinkedList.hpp:321
A class for doubly linked lists.
Definition: LinkedList.hpp:25
A class for changing the address of BankableMIDIOutputs.
Definition: Bank.hpp:16
uint8_t getTracksPerBank() const
Get the number of tracks per bank.
Definition: Bank.hpp:50
setting_t getSelection() const
Get the current bank setting (zero-based).
Definition: Bank.hpp:44
int8_t getOffset() const
Get the address offset (number of banks times the index of the selected bank after applying the offse...
Definition: Bank.hpp:58
OutputBank(uint8_t tracksPerBank=1, setting_t initialSelection=0, int8_t selectionOffset=0)
Create a new OutputBank object.
Definition: Bank.hpp:28
uint8_t tracksPerBank
Definition: Bank.hpp:61
void select(setting_t setting)
Select the given bank setting.
Definition: Bank.hpp:41
int8_t getOffsetOfSetting(setting_t s) const
The same as getOffset, but for a given setting.
Definition: Bank.hpp:53
int8_t selectionOffset
Definition: Bank.hpp:63
setting_t bankSetting
Definition: Bank.hpp:62
int8_t getSelectionOffset() const
Get the offset of the bank setting.
Definition: Bank.hpp:46
setting_t initialSelection
Definition: Selectable.hpp:40
#define FATAL_ERROR(msg, errc)
Print the error message and error code, and stop the execution.
Definition: Error.hpp:60