Control Surface  1.1.0
MIDI Control Surface library for Arduino
ButtonMatrix.hpp
Go to the documentation of this file.
1 /* ✔ */
2 
3 #pragma once
4 
6 AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
7 
8 #include <AH/Hardware/Hardware-Types.hpp>
9 
11 
12 /**
13  * @brief A class that reads the states of a button matrix.
14  *
15  * @tparam nb_rows
16  * The number of rows in the button matrix.
17  * @tparam nb_cols
18  * The number of columns in the button matrix.
19  *
20  * @ingroup AH_HardwareUtils
21  */
22 template <uint8_t nb_rows, uint8_t nb_cols>
23 class ButtonMatrix {
24  public:
25  /**
26  * @brief Construct a new ButtonMatrix object.
27  *
28  * @param rowPins
29  * A list of pin numbers connected to the rows of the button
30  * matrix.
31  * **⚠** These pins will be driven LOW as outputs (Lo-Z).
32  * @param colPins
33  * A list of pin numbers connected to the columns of the button
34  * matrix.
35  * These pins will be used as inputs (Hi-Z), and the internal
36  * pull-up resistor will be enabled.
37  */
38  ButtonMatrix(const PinList<nb_rows> &rowPins,
39  const PinList<nb_cols> &colPins);
40  /**
41  * @brief Destructor.
42  */
43  virtual ~ButtonMatrix() = default;
44 
45  /**
46  * @brief Initialize (enable internal pull-up resistors on column pins).
47  */
48  void begin();
49 
50  /**
51  * @brief Scan the matrix, read all button states, and call the
52  * onButtonChanged callback.
53  */
54  void update();
55 
56  /**
57  * Get the state of the button in the given column and row.
58  *
59  * @note No bounds checking is performed.
60  */
61  bool getPrevState(uint8_t col, uint8_t row);
62 
63  private:
64  /**
65  * @brief The callback function that is called whenever a button changes
66  * state.
67  *
68  * @param row
69  * The row of the button that changed state.
70  * @param col
71  * The column of the button that changed state.
72  * @param state
73  * The new state of the button.
74  */
75  virtual void onButtonChanged(uint8_t row, uint8_t col, bool state) = 0;
76 
77  static inline uint8_t positionToBits(uint8_t col, uint8_t row);
78  static inline uint8_t bitsToIndex(uint8_t bits);
79  static inline uint8_t bitsToBitmask(uint8_t bits);
80  void setPrevState(uint8_t col, uint8_t row, bool state);
81 
82  unsigned long prevRefresh = 0;
83  uint8_t prevStates[(nb_cols * nb_rows + 7) / 8];
84 
87 };
88 
90 
91 #include "ButtonMatrix.ipp" // Template implementations
92 
Warnings.hpp
AH_DIAGNOSTIC_POP
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:17
AH::Array< pin_t, N >
AH::ButtonMatrix::rowPins
const PinList< nb_rows > rowPins
Definition: ButtonMatrix.hpp:85
AH_DIAGNOSTIC_WERROR
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:16
ButtonMatrix.ipp
AH::ButtonMatrix::colPins
const PinList< nb_cols > colPins
Definition: ButtonMatrix.hpp:86
BEGIN_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
Definition: AH/Settings/NamespaceSettings.hpp:9
END_AH_NAMESPACE
#define END_AH_NAMESPACE
Definition: AH/Settings/NamespaceSettings.hpp:10
AH::ButtonMatrix
A class that reads the states of a button matrix.
Definition: ButtonMatrix.hpp:23