-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathI2CKeyPad .cpp
273 lines (216 loc) · 5.59 KB
/
I2CKeyPad .cpp
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//
// FILE: I2CKeyPad.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.5.0
// PURPOSE: Arduino library for 4x4 KeyPad connected to an I2C PCF8574
// URL: https://github.com/RobTillaart/I2CKeyPad
#include "I2CKeyPad.h"
I2CKeyPad::I2CKeyPad(const uint8_t deviceAddress, TwoWire *wire)
{
_lastKey = I2C_KEYPAD_NOKEY;
_address = deviceAddress;
_wire = wire;
_mode = I2C_KEYPAD_4x4;
_debounceThreshold = 0;
_lastTimeRead = 0;
}
bool I2CKeyPad::begin()
{
// enable interrupts
_read(0xF0);
return isConnected();
}
bool I2CKeyPad::isConnected()
{
_wire->beginTransmission(_address);
return (_wire->endTransmission() == 0);
}
uint8_t I2CKeyPad::getKey()
{
uint32_t now = millis();
if (_debounceThreshold > 0)
{
if (now - _debounceThreshold < _lastTimeRead)
{
return I2C_KEYPAD_THRESHOLD;
}
}
uint8_t key = 0;
if (_mode == I2C_KEYPAD_5x3) key = _getKey5x3();
else if (_mode == I2C_KEYPAD_6x2) key = _getKey6x2();
else if (_mode == I2C_KEYPAD_8x1) key = _getKey8x1();
else key = _getKey4x4(); // default.
if (key == I2C_KEYPAD_FAIL) return key; // propagate error.
// valid keys + NOKEY
_lastKey = key;
_lastTimeRead = now;
return key;
}
uint8_t I2CKeyPad::getLastKey()
{
return _lastKey;
}
// to check "press any key"
bool I2CKeyPad::isPressed()
{
uint8_t a = _read(0xF0);
if (a == 0xFF) return false;
return (a != 0xF0);
}
uint8_t I2CKeyPad::getChar()
{
uint8_t key = getKey();
if (key != I2C_KEYPAD_THRESHOLD)
{
return _keyMap[key];
}
return I2C_KEYPAD_THRESHOLD;
}
uint8_t I2CKeyPad::getLastChar()
{
return _keyMap[_lastKey];
}
void I2CKeyPad::loadKeyMap(char * keyMap)
{
_keyMap = keyMap;
}
void I2CKeyPad::setKeyPadMode(uint8_t mode)
{
if ((mode == I2C_KEYPAD_5x3) ||
(mode == I2C_KEYPAD_6x2) ||
(mode == I2C_KEYPAD_8x1))
{
_mode = mode;
return;
}
_mode = I2C_KEYPAD_4x4;
}
uint8_t I2CKeyPad::getKeyPadMode()
{
return _mode;
}
void I2CKeyPad::setDebounceThreshold(uint16_t value)
{
_debounceThreshold = value;
}
uint16_t I2CKeyPad::getDebounceThreshold()
{
return _debounceThreshold;
}
uint32_t I2CKeyPad::getLastTimeRead()
{
return _lastTimeRead;
}
//////////////////////////////////////////////////////
//
// PROTECTED
//
uint8_t I2CKeyPad::_read(uint8_t mask)
{
// improve the odds that IO will not interrupted.
yield();
_wire->beginTransmission(_address);
_wire->write(mask);
if (_wire->endTransmission() != 0)
{
// set communication error
return 0xFF;
}
_wire->requestFrom(_address, (uint8_t)1);
return _wire->read();
}
uint8_t I2CKeyPad::_getKey4x4()
{
// key = row + 4 x column
uint8_t key = 0;
// mask = 4 rows as input pull up, 4 columns as output
uint8_t rows = _read(0xF0);
// check if single line has gone low.
if (rows == 0xF0) return I2C_KEYPAD_NOKEY;
else if (rows == 0xE0) key = 0;
else if (rows == 0xD0) key = 1;
else if (rows == 0xB0) key = 2;
else if (rows == 0x70) key = 3;
else return I2C_KEYPAD_FAIL;
// 4 columns as input pull up, 4 rows as output
uint8_t cols = _read(0x0F);
// check if single line has gone low.
if (cols == 0x0F) return I2C_KEYPAD_NOKEY;
else if (cols == 0x0E) key += 0;
else if (cols == 0x0D) key += 4;
else if (cols == 0x0B) key += 8;
else if (cols == 0x07) key += 12;
else return I2C_KEYPAD_FAIL;
return key; // 0..15
}
// not tested
uint8_t I2CKeyPad::_getKey5x3()
{
// key = row + 5 x column
uint8_t key = 0;
// mask = 5 rows as input pull up, 3 columns as output
uint8_t rows = _read(0xF8);
// check if single line has gone low.
if (rows == 0xF8) return I2C_KEYPAD_NOKEY;
else if (rows == 0xF0) key = 0;
else if (rows == 0xE8) key = 1;
else if (rows == 0xD8) key = 2;
else if (rows == 0xB8) key = 3;
else if (rows == 0x78) key = 4;
else return I2C_KEYPAD_FAIL;
// 3 columns as input pull up, 5 rows as output
uint8_t cols = _read(0x07);
// check if single line has gone low.
if (cols == 0x07) return I2C_KEYPAD_NOKEY;
else if (cols == 0x06) key += 0;
else if (cols == 0x05) key += 5;
else if (cols == 0x03) key += 10;
else return I2C_KEYPAD_FAIL;
return key; // 0..14
}
// not tested
uint8_t I2CKeyPad::_getKey6x2()
{
// key = row + 6 x column
uint8_t key = 0;
// mask = 6 rows as input pull up, 2 columns as output
uint8_t rows = _read(0xFC);
// check if single line has gone low.
if (rows == 0xFC) return I2C_KEYPAD_NOKEY;
else if (rows == 0xF8) key = 0;
else if (rows == 0xF4) key = 1;
else if (rows == 0xEC) key = 2;
else if (rows == 0xDC) key = 3;
else if (rows == 0xBC) key = 4;
else if (rows == 0x7C) key = 5;
else return I2C_KEYPAD_FAIL;
// 2 columns as input pull up, 6 rows as output
uint8_t cols = _read(0x03);
// check if single line has gone low.
if (cols == 0x03) return I2C_KEYPAD_NOKEY;
else if (cols == 0x02) key += 0;
else if (cols == 0x01) key += 6;
else return I2C_KEYPAD_FAIL;
return key; // 0..11
}
// not tested
uint8_t I2CKeyPad::_getKey8x1()
{
// key = row
uint8_t key = 0;
// mask = 8 rows as input pull up, 0 columns as output
uint8_t rows = _read(0xFF);
// check if single line has gone low.
if (rows == 0xFF) return I2C_KEYPAD_NOKEY;
else if (rows == 0xFE) key = 0;
else if (rows == 0xFD) key = 1;
else if (rows == 0xFB) key = 2;
else if (rows == 0xF7) key = 3;
else if (rows == 0xEF) key = 4;
else if (rows == 0xDF) key = 5;
else if (rows == 0xBF) key = 6;
else if (rows == 0x7F) key = 7;
else return I2C_KEYPAD_FAIL;
return key; // 0..7
}
// -- END OF FILE --