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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
#pragma once
#include <Arduino.h>
#include "pins.h"
#define DEFAULT_DEBOUNCE_TIME 25
#define KEY_1 0x800
#define KEY_2 0x400
#define KEY_3 0x200
#define KEY_4 0x100
#define KEY_5 0x080
#define KEY_6 0x040
#define KEY_7 0x020
#define KEY_8 0x010
#define KEY_9 0x008
#define KEY_STAR 0x004
#define KEY_0 0x002
#define KEY_HASH 0x001
class Keypad {
protected:
unsigned int m_mask;
unsigned int m_debounceTime;
bool m_debouncing;
unsigned long m_debounceEnd;
bool m_pressed;
static Keypad buttons[12];
void startDebounce() {
m_debouncing = true;
m_debounceEnd = millis() + m_debounceTime;
}
public:
static unsigned int state;
static void Setup() {
pinMode(LED, OUTPUT);
pinMode(COL_1, INPUT_PULLUP);
pinMode(COL_2, INPUT_PULLUP);
pinMode(COL_3, INPUT_PULLUP);
pinMode(ROW_1, INPUT_PULLUP);
pinMode(ROW_2, INPUT_PULLUP);
pinMode(ROW_3, INPUT_PULLUP);
pinMode(ROW_4, INPUT_PULLUP);
}
static void Update() {
byte col1 = 1 - digitalRead(COL_1);
byte col2 = 1 - digitalRead(COL_2);
byte col3 = 1 - digitalRead(COL_3);
byte row1 = 1 - digitalRead(ROW_1);
byte row2 = 1 - digitalRead(ROW_2);
byte row3 = 1 - digitalRead(ROW_3);
byte row4 = 1 - digitalRead(ROW_4);
#define BUILD_ROW(row, c1, c2, c3) \
( ((row & c1) << 2) | ((row & c2) << 1) | (row & c3) )
byte row1_buttons = BUILD_ROW(row1, col1, col2, col3);
byte row2_buttons = BUILD_ROW(row2, col1, col2, col3);
byte row3_buttons = BUILD_ROW(row3, col1, col2, col3);
byte row4_buttons = BUILD_ROW(row4, col1, col2, col3);
unsigned int pins = 0 |
(row1_buttons << 9) |
(row2_buttons << 6) |
(row3_buttons << 3) |
(row4_buttons << 0);
for (int i=0; i<12; i++) {
buttons[i].update(pins);
}
}
Keypad(unsigned int index, unsigned int debounceTime=DEFAULT_DEBOUNCE_TIME) {
m_mask = 1 << index;
m_debounceTime = debounceTime;
m_debouncing = false;
m_pressed = false;
}
void update(unsigned int pins) {
if (m_debouncing) {
if (millis() > m_debounceEnd) {
/* done debouncing, continue */
m_debouncing = false;
}
else {
/* still debouncing, ignore changes */
return;
}
}
if ((m_mask & pins)) {
/* pressed */
if (m_pressed) {
/* already marked */
}
else {
/* update state */
m_pressed = true;
state = state | m_mask;
startDebounce();
}
}
else {
/* released */
if (!m_pressed) {
/* already marked, ignore */
}
else {
/* update state */
m_pressed = false;
state = state & ~m_mask;
startDebounce();
}
}
}
};
unsigned int Keypad::state = 0;
Keypad Keypad::buttons[12] = {
Keypad(0),
Keypad(1),
Keypad(2),
Keypad(3),
Keypad(4),
Keypad(5),
Keypad(6),
Keypad(7),
Keypad(8),
Keypad(9),
Keypad(10),
Keypad(11),
};
|