Control Surface main
MIDI Control Surface library for Arduino
ButtonMatrix.hpp
Go to the documentation of this file.
1/* ✔ */
2
3#pragma once
4
6AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
7
8#include <AH/Hardware/Hardware-Types.hpp>
9
11
22template <class Derived, uint8_t NumRows, uint8_t NumCols>
24 public:
39 const PinList<NumCols> &colPins);
40
44 void begin();
45
50 void update();
51
57 bool getPrevState(uint8_t col, uint8_t row);
58
61 void setDebounceTime(unsigned long debounceTime) {
62 this->debounceTime = debounceTime;
63 }
65 unsigned long getDebounceTime() const { return debounceTime; }
66
67 protected:
79 void onButtonChanged(uint8_t row, uint8_t col, bool state) = delete;
80
81 private:
82 static inline uint8_t positionToBits(uint8_t col, uint8_t row);
83 static inline uint8_t bitsToIndex(uint8_t bits);
84 static inline uint8_t bitsToBitmask(uint8_t bits);
85 void setPrevState(uint8_t col, uint8_t row, bool state);
86
87 unsigned long debounceTime = BUTTON_DEBOUNCE_TIME;
88 unsigned long prevRefresh = 0;
89 uint8_t prevStates[(NumCols * NumRows + 7) / 8];
90
93};
94
96
97#include "ButtonMatrix.ipp" // Template implementations
98
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:53
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:52
A class that reads the states of a button matrix.
ButtonMatrix(const PinList< NumRows > &rowPins, const PinList< NumCols > &colPins)
Construct a new ButtonMatrix object.
void setPrevState(uint8_t col, uint8_t row, bool state)
void setDebounceTime(unsigned long debounceTime)
Configure the debounce time interval.
bool getPrevState(uint8_t col, uint8_t row)
Get the state of the button in the given column and row.
static uint8_t bitsToIndex(uint8_t bits)
unsigned long getDebounceTime() const
Get the debounce time.
static uint8_t bitsToBitmask(uint8_t bits)
void onButtonChanged(uint8_t row, uint8_t col, bool state)=delete
The callback function that is called whenever a button changes state.
void begin()
Initialize (enable internal pull-up resistors on column pins).
static uint8_t positionToBits(uint8_t col, uint8_t row)
void update()
Scan the matrix, read all button states, and call the onButtonChanged callback.
const PinList< NumRows > rowPins
const PinList< NumCols > colPins
constexpr unsigned long BUTTON_DEBOUNCE_TIME
The debounce time for momentary push buttons in milliseconds.