Line data Source code
1 : #pragma once
2 :
3 : #include <AH/Containers/Array.hpp>
4 : #include <AH/Hardware/ButtonMatrix.hpp>
5 : #include <Banks/BankableAddresses.hpp>
6 : #include <Def/Def.hpp>
7 : #include <MIDI_Outputs/Abstract/MIDIOutputElement.hpp>
8 :
9 : BEGIN_CS_NAMESPACE
10 :
11 : namespace Bankable {
12 :
13 : /**
14 : * @brief MIDIButtonMatrix
15 : * @todo Documentation
16 : * @see AH::ButtonMatrix
17 : *
18 : * @tparam BankAddress
19 : * The bankable address object containing the addresses of all buttons,
20 : * as well as a reference to the bank this element belongs to.
21 : * @tparam Sender
22 : * The MIDI Sender class.
23 : * @tparam NumRows
24 : * The number of rows of the button matrix.
25 : * @tparam NumCols
26 : * The number of columns of the button matrix.
27 : */
28 : template <class BankAddress, class Sender, uint8_t NumRows, uint8_t NumCols>
29 : class MIDIButtonMatrix
30 : : public MIDIOutputElement,
31 : public AH::ButtonMatrix<
32 : MIDIButtonMatrix<BankAddress, Sender, NumRows, NumCols>, NumRows,
33 : NumCols> {
34 : using ButtonMatrix = AH::ButtonMatrix<MIDIButtonMatrix, NumRows, NumCols>;
35 : friend class AH::ButtonMatrix<MIDIButtonMatrix, NumRows, NumCols>;
36 :
37 : protected:
38 : /**
39 : * @brief Create a new Bankable MIDIButtonMatrix.
40 : *
41 : * @param bankAddress
42 : * The bankable MIDI address to send to.
43 : * @param rowPins
44 : * A list of pin numbers connected to the rows of the button
45 : * matrix.
46 : * **⚠** These pins will be driven LOW as outputs (Lo-Z).
47 : * @param colPins
48 : * A list of pin numbers connected to the columns of the button
49 : * matrix.
50 : * These pins will be used as inputs (Hi-Z), and the
51 : * internal pull-up resistor will be enabled.
52 : * @param sender
53 : * The MIDI sender to use.
54 : */
55 6 : MIDIButtonMatrix(BankAddress bankAddress, const PinList<NumRows> &rowPins,
56 : const PinList<NumCols> &colPins, const Sender &sender)
57 6 : : ButtonMatrix(rowPins, colPins), address(bankAddress), sender(sender) {
58 6 : }
59 :
60 : public:
61 2 : void begin() override { ButtonMatrix::begin(); }
62 :
63 8 : void update() override { ButtonMatrix::update(); }
64 :
65 : private:
66 8 : void onButtonChanged(uint8_t row, uint8_t col, bool state) {
67 8 : if (state == LOW) {
68 4 : if (!activeButtons)
69 4 : address.lock(); // Don't allow changing of the bank setting
70 4 : activeButtons++;
71 4 : sender.sendOn(address.getActiveAddress(row, col));
72 : } else {
73 4 : sender.sendOff(address.getActiveAddress(row, col));
74 4 : activeButtons--;
75 4 : if (!activeButtons)
76 4 : address.unlock();
77 : }
78 8 : }
79 :
80 : protected:
81 : BankAddress address;
82 : uint16_t activeButtons = 0;
83 :
84 : public:
85 : Sender sender;
86 : };
87 :
88 : } // namespace Bankable
89 :
90 : END_CS_NAMESPACE
|