Control Surface  1.1.1
MIDI Control Surface library for Arduino
ButtonMatrix.ipp
Go to the documentation of this file.
1 #include "ButtonMatrix.hpp"
3 #include <string.h>
4 
6 
7 using namespace ExtIO;
8 
9 template <uint8_t nb_rows, uint8_t nb_cols>
11  const PinList<nb_cols> &colPins)
12  : rowPins(rowPins), colPins(colPins) {
13  memset(prevStates, 0xFF, sizeof(prevStates));
14 }
15 
16 template <uint8_t nb_rows, uint8_t nb_cols>
18  unsigned long now = millis();
19  // only update 25 ms after previous change (crude software debounce).
20  // Edit this in Settings/Settings.hpp
21  if (now - prevRefresh < BUTTON_DEBOUNCE_TIME)
22  return;
23 
24  for (size_t row = 0; row < nb_rows; row++) { // scan through all rows
25  pinMode(rowPins[row], OUTPUT); // make the current row Lo-Z 0V
26  for (size_t col = 0; col < nb_cols; col++) { // scan through all columns
27  bool state = digitalRead(colPins[col]); // read the state
28  if (state != getPrevState(col, row)) {
29  // if the state changed since last time
30  // execute the handler
31  onButtonChanged(row, col, state);
32  setPrevState(col, row, state); // remember the state
33  prevRefresh = now;
34  }
35  }
36  pinMode(rowPins[row], INPUT); // make the current row Hi-Z again
37  }
38 }
39 
40 template <uint8_t nb_rows, uint8_t nb_cols>
42  // make all columns input pins and enable
43  // the internal pull-up resistors
44  for (const pin_t &colPin : colPins)
45  pinMode(colPin, INPUT_PULLUP);
46  // make all rows Hi-Z
47  for (const pin_t &rowPin : rowPins)
48  pinMode(rowPin, INPUT);
49 }
50 
51 template <uint8_t nb_rows, uint8_t nb_cols>
53  uint8_t row) {
54  // map from a 2D array of bits to a flat array of bits
55  return col * nb_rows + row;
56 }
57 
58 template <uint8_t nb_rows, uint8_t nb_cols>
59 inline uint8_t ButtonMatrix<nb_rows, nb_cols>::bitsToIndex(uint8_t bits) {
60  return bits >> 3; // bits / 8
61 }
62 
63 template <uint8_t nb_rows, uint8_t nb_cols>
64 inline uint8_t ButtonMatrix<nb_rows, nb_cols>::bitsToBitmask(uint8_t bits) {
65  return 1 << (bits & 7); // bits % 8
66 }
67 
68 template <uint8_t nb_rows, uint8_t nb_cols>
69 bool ButtonMatrix<nb_rows, nb_cols>::getPrevState(uint8_t col, uint8_t row) {
70  uint8_t bits = positionToBits(col, row);
71  return !!(prevStates[bitsToIndex(bits)] & bitsToBitmask(bits));
72 }
73 
74 template <uint8_t nb_rows, uint8_t nb_cols>
75 void ButtonMatrix<nb_rows, nb_cols>::setPrevState(uint8_t col, uint8_t row,
76  bool state) {
77  uint8_t bits = positionToBits(col, row);
78  if (state)
79  prevStates[bitsToIndex(bits)] |= bitsToBitmask(bits);
80  else
81  prevStates[bitsToIndex(bits)] &= ~bitsToBitmask(bits);
82 }
83 
AH::ExtIO::pinMode
void pinMode(pin_t pin, uint8_t mode)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:36
INPUT_PULLUP
const uint8_t INPUT_PULLUP
Definition: ExtendedInputOutput.hpp:51
AH::ExtIO::digitalRead
int digitalRead(pin_t pin)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:60
AH::pin_t
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
Definition: Hardware-Types.hpp:17
AH::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:69
ExtendedInputOutput.hpp
AH::BUTTON_DEBOUNCE_TIME
constexpr unsigned long BUTTON_DEBOUNCE_TIME
The debounce time for momentary push buttons in milliseconds.
Definition: AH/Settings/Settings.hpp:74
AH::Array< pin_t, N >
AH::ButtonMatrix::positionToBits
static uint8_t positionToBits(uint8_t col, uint8_t row)
Definition: ButtonMatrix.ipp:52
AH::ButtonMatrix::begin
void begin()
Initialize (enable internal pull-up resistors on column pins).
Definition: ButtonMatrix.ipp:41
AH::ButtonMatrix::bitsToBitmask
static uint8_t bitsToBitmask(uint8_t bits)
Definition: ButtonMatrix.ipp:64
AH::ButtonMatrix::prevStates
uint8_t prevStates[(nb_cols *nb_rows+7)/8]
Definition: ButtonMatrix.hpp:83
INPUT
const uint8_t INPUT
Definition: ExtendedInputOutput.hpp:49
ButtonMatrix.hpp
BEGIN_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
Definition: AH/Settings/NamespaceSettings.hpp:9
OUTPUT
const uint8_t OUTPUT
Definition: ExtendedInputOutput.hpp:50
END_AH_NAMESPACE
#define END_AH_NAMESPACE
Definition: AH/Settings/NamespaceSettings.hpp:10
AH::ButtonMatrix::setPrevState
void setPrevState(uint8_t col, uint8_t row, bool state)
Definition: ButtonMatrix.ipp:75
AH::ButtonMatrix::bitsToIndex
static uint8_t bitsToIndex(uint8_t bits)
Definition: ButtonMatrix.ipp:59
AH::ButtonMatrix
A class that reads the states of a button matrix.
Definition: ButtonMatrix.hpp:23
AH::ButtonMatrix::update
void update()
Scan the matrix, read all button states, and call the onButtonChanged callback.
Definition: ButtonMatrix.ipp:17