Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Keypad by
Keypad.cpp@1:dd892be4b7c7, 2017-06-21 (annotated)
- Committer:
- mijimy
- Date:
- Wed Jun 21 09:35:40 2017 +0000
- Revision:
- 1:dd892be4b7c7
- Parent:
- 0:4bbd88022a6f
PULLUP IN IN ROW
Who changed what in which revision?
| 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 |
| mijimy | 1:dd892be4b7c7 | 7 | char const kpdLayout[4][4] = {{'1' ,'2' ,'3' ,'4'}, //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 | |
| grantphillips | 0:4bbd88022a6f | 23 | char Keypad::ReadKey() { |
| grantphillips | 0:4bbd88022a6f | 24 | char KeyValue, Done=0; |
| grantphillips | 0:4bbd88022a6f | 25 | uint16_t y, x; |
| mijimy | 1:dd892be4b7c7 | 26 | _rows.mode(PullUp); |
| mijimy | 1:dd892be4b7c7 | 27 | |
| grantphillips | 0:4bbd88022a6f | 28 | //delay_ms(ContactBounceTime); //warning no contact bounce protection |
| grantphillips | 0:4bbd88022a6f | 29 | //call read_key more than once with delay |
| grantphillips | 0:4bbd88022a6f | 30 | //between if key stay constant then key is pressed |
| grantphillips | 0:4bbd88022a6f | 31 | y = 0; |
| grantphillips | 0:4bbd88022a6f | 32 | while((y < 4) && (!Done)) |
| grantphillips | 0:4bbd88022a6f | 33 | { |
| grantphillips | 0:4bbd88022a6f | 34 | _cols = KpdOutMask[y]; //write mask value to the column outputs |
| grantphillips | 0:4bbd88022a6f | 35 | wait(0.01); |
| grantphillips | 0:4bbd88022a6f | 36 | |
| grantphillips | 0:4bbd88022a6f | 37 | KeyValue = _rows; //read mask value from the row inputs |
| grantphillips | 0:4bbd88022a6f | 38 | |
| grantphillips | 0:4bbd88022a6f | 39 | if(KeyValue == KpdInMask[0]) |
| grantphillips | 0:4bbd88022a6f | 40 | x = 0; |
| grantphillips | 0:4bbd88022a6f | 41 | else if(KeyValue == KpdInMask[1]) |
| grantphillips | 0:4bbd88022a6f | 42 | x = 1; |
| grantphillips | 0:4bbd88022a6f | 43 | else if(KeyValue == KpdInMask[2]) |
| grantphillips | 0:4bbd88022a6f | 44 | x = 2; |
| grantphillips | 0:4bbd88022a6f | 45 | else if(KeyValue == KpdInMask[3]) |
| grantphillips | 0:4bbd88022a6f | 46 | x = 3; |
| grantphillips | 0:4bbd88022a6f | 47 | else |
| grantphillips | 0:4bbd88022a6f | 48 | { |
| grantphillips | 0:4bbd88022a6f | 49 | KeyValue='\0'; //more than one key was pressed or no key in this row. |
| grantphillips | 0:4bbd88022a6f | 50 | x=9; |
| grantphillips | 0:4bbd88022a6f | 51 | } |
| grantphillips | 0:4bbd88022a6f | 52 | if(x != 9) |
| grantphillips | 0:4bbd88022a6f | 53 | { |
| grantphillips | 0:4bbd88022a6f | 54 | Done = 1; //valid key found |
| grantphillips | 0:4bbd88022a6f | 55 | KeyValue = kpdLayout[x][y]; //convert to a character eg. '1','2','3','#','*' |
| grantphillips | 0:4bbd88022a6f | 56 | } |
| grantphillips | 0:4bbd88022a6f | 57 | y++; |
| grantphillips | 0:4bbd88022a6f | 58 | } |
| grantphillips | 0:4bbd88022a6f | 59 | return(KeyValue); |
| grantphillips | 0:4bbd88022a6f | 60 | } |
