User | Revision | Line number | New contents of line |
grantphillips |
0:4bbd88022a6f
|
1
|
#include "Keypad.h"
|
grantphillips |
0:4bbd88022a6f
|
2
|
#include "mbed.h"
|
grantphillips |
0:4bbd88022a6f
|
3
|
|
grantphillips |
0:4bbd88022a6f
|
4
|
|
grantphillips |
0:4bbd88022a6f
|
5
|
// Keypad layout:
|
grantphillips |
0:4bbd88022a6f
|
6
|
// [row][col] Col0 Col1 Col2 Col3
|
grantphillips |
0:4bbd88022a6f
|
7
|
char const kpdLayout[4][4] = {{'1' ,'2' ,'3' ,'A'}, //row0
|
grantphillips |
0:4bbd88022a6f
|
8
|
{'4' ,'5' ,'6' ,'B'}, //row1
|
grantphillips |
0:4bbd88022a6f
|
9
|
{'7' ,'8' ,'9' ,'C'}, //row2
|
grantphillips |
0:4bbd88022a6f
|
10
|
{'*' ,'0' ,'#' ,'D'}}; //row3
|
grantphillips |
0:4bbd88022a6f
|
11
|
|
grantphillips |
0:4bbd88022a6f
|
12
|
//NIBBLE LOW=0000, HIGH= 0111 1011 1101 1110 Col (x)
|
grantphillips |
0:4bbd88022a6f
|
13
|
//const char KpdInMask[4] ={0xe0,0xd0,0xb0,0x70};
|
grantphillips |
0:4bbd88022a6f
|
14
|
const char KpdInMask[4] ={0x0e,0x0d,0x0b,0x07};
|
grantphillips |
0:4bbd88022a6f
|
15
|
|
grantphillips |
0:4bbd88022a6f
|
16
|
//NIBBLE HIGH=1111, LOW= 0111 1011 1101 1110 Rows (y)
|
grantphillips |
0:4bbd88022a6f
|
17
|
//const char KpdOutMask[4]={0xfe,0xfd,0xfb,0xf7};
|
grantphillips |
0:4bbd88022a6f
|
18
|
const char KpdOutMask[4]={0x0e,0x0d,0x0b,0x07};
|
grantphillips |
0:4bbd88022a6f
|
19
|
|
grantphillips |
0:4bbd88022a6f
|
20
|
|
grantphillips |
0:4bbd88022a6f
|
21
|
Keypad::Keypad(PinName col1, PinName col2, PinName col3, PinName col4, PinName row1, PinName row2, PinName row3, PinName row4) : _cols(col1,col2,col3,col4), _rows(row1,row2,row3,row4) { }
|
grantphillips |
0:4bbd88022a6f
|
22
|
char Keypad::ReadKey() {
|
grantphillips |
0:4bbd88022a6f
|
23
|
char KeyValue, Done=0;
|
grantphillips |
0:4bbd88022a6f
|
24
|
uint16_t y, x;
|
grantphillips |
0:4bbd88022a6f
|
25
|
|
grantphillips |
0:4bbd88022a6f
|
26
|
//delay_ms(ContactBounceTime); //warning no contact bounce protection
|
grantphillips |
0:4bbd88022a6f
|
27
|
//call read_key more than once with delay
|
grantphillips |
0:4bbd88022a6f
|
28
|
//between if key stay constant then key is pressed
|
grantphillips |
0:4bbd88022a6f
|
29
|
y = 0;
|
grantphillips |
0:4bbd88022a6f
|
30
|
while((y < 4) && (!Done))
|
grantphillips |
0:4bbd88022a6f
|
31
|
{
|
grantphillips |
0:4bbd88022a6f
|
32
|
_cols = KpdOutMask[y]; //write mask value to the column outputs
|
grantphillips |
0:4bbd88022a6f
|
33
|
wait(0.01);
|
grantphillips |
0:4bbd88022a6f
|
34
|
|
grantphillips |
0:4bbd88022a6f
|
35
|
KeyValue = _rows; //read mask value from the row inputs
|
grantphillips |
0:4bbd88022a6f
|
36
|
|
grantphillips |
0:4bbd88022a6f
|
37
|
if(KeyValue == KpdInMask[0])
|
grantphillips |
0:4bbd88022a6f
|
38
|
x = 0;
|
grantphillips |
0:4bbd88022a6f
|
39
|
else if(KeyValue == KpdInMask[1])
|
grantphillips |
0:4bbd88022a6f
|
40
|
x = 1;
|
grantphillips |
0:4bbd88022a6f
|
41
|
else if(KeyValue == KpdInMask[2])
|
grantphillips |
0:4bbd88022a6f
|
42
|
x = 2;
|
grantphillips |
0:4bbd88022a6f
|
43
|
else if(KeyValue == KpdInMask[3])
|
grantphillips |
0:4bbd88022a6f
|
44
|
x = 3;
|
grantphillips |
0:4bbd88022a6f
|
45
|
else
|
grantphillips |
0:4bbd88022a6f
|
46
|
{
|
grantphillips |
0:4bbd88022a6f
|
47
|
KeyValue='\0'; //more than one key was pressed or no key in this row.
|
grantphillips |
0:4bbd88022a6f
|
48
|
x=9;
|
grantphillips |
0:4bbd88022a6f
|
49
|
}
|
grantphillips |
0:4bbd88022a6f
|
50
|
if(x != 9)
|
grantphillips |
0:4bbd88022a6f
|
51
|
{
|
grantphillips |
0:4bbd88022a6f
|
52
|
Done = 1; //valid key found
|
grantphillips |
0:4bbd88022a6f
|
53
|
KeyValue = kpdLayout[x][y]; //convert to a character eg. '1','2','3','#','*'
|
grantphillips |
0:4bbd88022a6f
|
54
|
}
|
grantphillips |
0:4bbd88022a6f
|
55
|
y++;
|
grantphillips |
0:4bbd88022a6f
|
56
|
}
|
grantphillips |
0:4bbd88022a6f
|
57
|
return(KeyValue);
|
grantphillips |
0:4bbd88022a6f
|
58
|
} |