-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSHButtonMatrix.h
More file actions
92 lines (68 loc) · 1.84 KB
/
SHButtonMatrix.h
File metadata and controls
92 lines (68 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#ifndef __SHBUTTONMATRIX_H__
#define __SHBUTTONMATRIX_H__
#include <Arduino.h>
#include "SHDebouncer.h"
typedef void(*SHButtonMatrixChanged) (int, byte);
class SHButtonMatrix {
private:
FastDigitalPin button;
uint8_t buttonState;
int lastPressedButton;
unsigned long buttonLastStateChanged;
SHButtonMatrixChanged shButtonChangedCallback;
SHDebouncer debouncer;
byte rowCount;
byte colCount;
byte * colPins;
byte * rowPins;
public:
void begin(byte cols, byte rows, byte * col, byte * row, SHButtonMatrixChanged changedcallback) {
debouncer.begin(10);
rowCount = rows;
colCount = cols;
colPins = col;
rowPins = row;
for (int x = 0; x < rowCount; x++) {
pinMode(rowPins[x], INPUT);
}
for (int x = 0; x < colCount; x++) {
pinMode(colPins[x], INPUT_PULLUP);
}
shButtonChangedCallback = changedcallback;
}
void read() {
if (debouncer.Debounce()) {
int pressedButton = -1;
if (buttonLastStateChanged - millis() > 50) {
for (int colIndex = 0; colIndex < colCount; colIndex++) {
byte curCol = colPins[colIndex];
pinMode(curCol, OUTPUT);
digitalWrite(curCol, LOW);
for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
byte rowCol = rowPins[rowIndex];
pinMode(rowCol, INPUT_PULLUP);
if (digitalRead(rowCol) == LOW) {
pressedButton = rowIndex * colCount + colIndex + 1;
}
//pinMode(rowCol, INPUT);
}
pinMode(curCol, INPUT);
}
if (pressedButton != lastPressedButton) {
if (lastPressedButton != -1) {
shButtonChangedCallback(lastPressedButton, 0);
}
if (pressedButton != -1) {
shButtonChangedCallback(pressedButton, 1);
}
buttonLastStateChanged = millis();
}
lastPressedButton = pressedButton;
}
//String s = String(lastPressedButton);
//FlowSerialDebugPrintLn(s);
}
return;
}
};
#endif