Arduino Helpers master
Utility library for Arduino
ButtonMatrix.hpp
Go to the documentation of this file.
1/* ✔ */
2
3#pragma once
4
6
8
19template <class Derived, uint8_t NumRows, uint8_t NumCols>
21 public:
37
41 void begin();
42
47 void update();
48
54 bool getPrevState(uint8_t col, uint8_t row);
55
58 void setDebounceTime(unsigned long debounceTime) {
59 this->debounceTime = debounceTime;
60 }
62 unsigned long getDebounceTime() const { return debounceTime; }
63
64 protected:
76 void onButtonChanged(uint8_t row, uint8_t col, bool state) = delete;
77
78 private:
79 static inline uint8_t positionToBits(uint8_t col, uint8_t row);
80 static inline uint8_t bitsToIndex(uint8_t bits);
81 static inline uint8_t bitsToBitmask(uint8_t bits);
82 void setPrevState(uint8_t col, uint8_t row, bool state);
83
85 unsigned long prevRefresh = 0;
86 uint8_t prevStates[(NumCols * NumRows + 7) / 8];
87
90};
91
93
94#include "ButtonMatrix.ipp" // Template implementations
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
constexpr unsigned long BUTTON_DEBOUNCE_TIME
The debounce time for momentary push buttons in milliseconds.
Definition: Settings.hpp:76
A class that reads the states of a button matrix.
static uint8_t bitsToBitmask(uint8_t bits)
ButtonMatrix(const PinList< NumRows > &rowPins, const PinList< NumCols > &colPins)
Construct a new ButtonMatrix object.
Definition: ButtonMatrix.ipp:9
void setPrevState(uint8_t col, uint8_t row, bool state)
void setDebounceTime(unsigned long debounceTime)
Configure the debounce time interval.
unsigned long prevRefresh
bool getPrevState(uint8_t col, uint8_t row)
Get the state of the button in the given column and row.
uint8_t prevStates[(NumCols *NumRows+7)/8]
unsigned long getDebounceTime() const
Get the debounce time.
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).
void update()
Scan the matrix, read all button states, and call the onButtonChanged callback.
const PinList< NumRows > rowPins
static uint8_t bitsToIndex(uint8_t bits)
static uint8_t positionToBits(uint8_t col, uint8_t row)
const PinList< NumCols > colPins
unsigned long debounceTime