-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathKeypad_I2C.cpp
More file actions
72 lines (63 loc) · 1.73 KB
/
Keypad_I2C.cpp
File metadata and controls
72 lines (63 loc) · 1.73 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
/********************************************************************************************
* Arduino library for matrix keypad using key encoder and I2C
* Created 07/09/2014
* Christophe Caiveau f4goj@free.fr
*
* Use this library freely
*
* Instance :
* Keypad_I2C kpd(int i2c_addr, int int_pin); // i2c_addr I2C address of the keypad adapter
* // int_pin interrupt input pin D2 or D3
*
* Functions :
* kpd.begin();
* kpd.getKey();
* kpd.getKeyNum();
*
* MM74C922N datasheet at http://www.ti.com/lit/ds/symlink/mm54c923.pdf
* PCF8574 datasheet at http://www.nxp.com/documents/data_sheet/PCF8574.pdf
*
*******************************************************************************************/
#include <Keypad_I2C.h>
#include <Wire.h>
volatile int newkey;
char Keypad_I2C::keymap[17] = {'1', '2', '3', 'A',
'4', '5', '6', 'B',
'7', '8', '9', 'C',
'*', '0', '#', 'D'};
Keypad_I2C::Keypad_I2C(int i2c_addr, int int_pin) {
I2C_ADDR = i2c_addr;
INT_PIN = int_pin;
}
void Keypad_I2C::begin() {
begin_priv();
}
void Keypad_I2C::begin_priv() {
pinMode(INT_PIN, INPUT);
Wire.begin();
attachInterrupt(1, keyread, FALLING);
}
void Keypad_I2C::keyread(){
newkey = 1;
}
char Keypad_I2C::getKey (){
if(newkey == 1){
Wire.requestFrom(I2C_ADDR, 1);
keycode = Wire.read() & 0x0f;
newkey = 0;
return keymap[keycode];
}
return 0;
}
char Keypad_I2C::getKeyNum (){
if(newkey == 1){
Wire.requestFrom(I2C_ADDR, 1);
keycode = Wire.read() & 0x0f;
newkey = 0;
int tempkey = keymap[keycode] - '0';
if( tempkey >= 0 && tempkey <= 9){
return keymap[keycode];
}
}
return 0;
}