Arduino Helpers
master
Utility library for Arduino
src
AH
Hardware
ButtonMatrix.hpp
Go to the documentation of this file.
1
/* ✔ */
2
3
#pragma once
4
5
#include <
AH/Hardware/Hardware-Types.hpp
>
6
7
BEGIN_AH_NAMESPACE
8
19
template
<
class
Derived, u
int
8_t NumRows, u
int
8_t NumCols>
20
class
ButtonMatrix
{
21
public
:
35
ButtonMatrix
(
const
PinList<NumRows>
&
rowPins
,
36
const
PinList<NumCols>
&
colPins
);
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
84
unsigned
long
debounceTime
=
BUTTON_DEBOUNCE_TIME
;
85
unsigned
long
prevRefresh
= 0;
86
uint8_t
prevStates
[(NumCols * NumRows + 7) / 8];
87
88
const
PinList<NumRows>
rowPins
;
89
const
PinList<NumCols>
colPins
;
90
};
91
92
END_AH_NAMESPACE
93
94
#include "
ButtonMatrix.ipp
"
// Template implementations
ButtonMatrix.ipp
Hardware-Types.hpp
END_AH_NAMESPACE
#define END_AH_NAMESPACE
Definition:
NamespaceSettings.hpp:14
BEGIN_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
Definition:
NamespaceSettings.hpp:11
BUTTON_DEBOUNCE_TIME
constexpr unsigned long BUTTON_DEBOUNCE_TIME
The debounce time for momentary push buttons in milliseconds.
Definition:
Settings.hpp:76
ButtonMatrix
A class that reads the states of a button matrix.
Definition:
ButtonMatrix.hpp:20
ButtonMatrix::bitsToBitmask
static uint8_t bitsToBitmask(uint8_t bits)
Definition:
ButtonMatrix.ipp:69
ButtonMatrix::ButtonMatrix
ButtonMatrix(const PinList< NumRows > &rowPins, const PinList< NumCols > &colPins)
Construct a new ButtonMatrix object.
Definition:
ButtonMatrix.ipp:9
ButtonMatrix::setPrevState
void setPrevState(uint8_t col, uint8_t row, bool state)
Definition:
ButtonMatrix.ipp:81
ButtonMatrix::setDebounceTime
void setDebounceTime(unsigned long debounceTime)
Configure the debounce time interval.
Definition:
ButtonMatrix.hpp:58
ButtonMatrix::prevRefresh
unsigned long prevRefresh
Definition:
ButtonMatrix.hpp:85
ButtonMatrix::getPrevState
bool getPrevState(uint8_t col, uint8_t row)
Get the state of the button in the given column and row.
Definition:
ButtonMatrix.ipp:74
ButtonMatrix::prevStates
uint8_t prevStates[(NumCols *NumRows+7)/8]
Definition:
ButtonMatrix.hpp:86
ButtonMatrix::getDebounceTime
unsigned long getDebounceTime() const
Get the debounce time.
Definition:
ButtonMatrix.hpp:62
ButtonMatrix::onButtonChanged
void onButtonChanged(uint8_t row, uint8_t col, bool state)=delete
The callback function that is called whenever a button changes state.
ButtonMatrix::begin
void begin()
Initialize (enable internal pull-up resistors on column pins).
Definition:
ButtonMatrix.ipp:43
ButtonMatrix::update
void update()
Scan the matrix, read all button states, and call the onButtonChanged callback.
Definition:
ButtonMatrix.ipp:16
ButtonMatrix::rowPins
const PinList< NumRows > rowPins
Definition:
ButtonMatrix.hpp:88
ButtonMatrix::bitsToIndex
static uint8_t bitsToIndex(uint8_t bits)
Definition:
ButtonMatrix.ipp:63
ButtonMatrix::positionToBits
static uint8_t positionToBits(uint8_t col, uint8_t row)
Definition:
ButtonMatrix.ipp:55
ButtonMatrix::colPins
const PinList< NumCols > colPins
Definition:
ButtonMatrix.hpp:89
ButtonMatrix::debounceTime
unsigned long debounceTime
Definition:
ButtonMatrix.hpp:84
Array< pin_t, N >
Generated by
1.9.4