Arduino Filters master
Filter 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:
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
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
constexpr unsigned long BUTTON_DEBOUNCE_TIME
The debounce time for momentary push buttons in milliseconds.
Definition: Settings.hpp:76
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:36
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:35
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