Class library for a polling-based 4x4 keypad.

Dependents:   LCD kbdlcdkl25z control_onoff control_onoff ... more

Committer:
grantphillips
Date:
Tue Apr 05 17:37:55 2016 +0000
Revision:
0:4bbd88022a6f
v1.00

Who changed what in which revision?

UserRevisionLine numberNew 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
grantphillips 0:4bbd88022a6f 23 char Keypad::ReadKey() {
grantphillips 0:4bbd88022a6f 24 char KeyValue, Done=0;
grantphillips 0:4bbd88022a6f 25 uint16_t y, x;
grantphillips 0:4bbd88022a6f 26
grantphillips 0:4bbd88022a6f 27 //delay_ms(ContactBounceTime); //warning no contact bounce protection
grantphillips 0:4bbd88022a6f 28 //call read_key more than once with delay
grantphillips 0:4bbd88022a6f 29 //between if key stay constant then key is pressed
grantphillips 0:4bbd88022a6f 30 y = 0;
grantphillips 0:4bbd88022a6f 31 while((y < 4) && (!Done))
grantphillips 0:4bbd88022a6f 32 {
grantphillips 0:4bbd88022a6f 33 _cols = KpdOutMask[y]; //write mask value to the column outputs
grantphillips 0:4bbd88022a6f 34 wait(0.01);
grantphillips 0:4bbd88022a6f 35
grantphillips 0:4bbd88022a6f 36 KeyValue = _rows; //read mask value from the row inputs
grantphillips 0:4bbd88022a6f 37
grantphillips 0:4bbd88022a6f 38 if(KeyValue == KpdInMask[0])
grantphillips 0:4bbd88022a6f 39 x = 0;
grantphillips 0:4bbd88022a6f 40 else if(KeyValue == KpdInMask[1])
grantphillips 0:4bbd88022a6f 41 x = 1;
grantphillips 0:4bbd88022a6f 42 else if(KeyValue == KpdInMask[2])
grantphillips 0:4bbd88022a6f 43 x = 2;
grantphillips 0:4bbd88022a6f 44 else if(KeyValue == KpdInMask[3])
grantphillips 0:4bbd88022a6f 45 x = 3;
grantphillips 0:4bbd88022a6f 46 else
grantphillips 0:4bbd88022a6f 47 {
grantphillips 0:4bbd88022a6f 48 KeyValue='\0'; //more than one key was pressed or no key in this row.
grantphillips 0:4bbd88022a6f 49 x=9;
grantphillips 0:4bbd88022a6f 50 }
grantphillips 0:4bbd88022a6f 51 if(x != 9)
grantphillips 0:4bbd88022a6f 52 {
grantphillips 0:4bbd88022a6f 53 Done = 1; //valid key found
grantphillips 0:4bbd88022a6f 54 KeyValue = kpdLayout[x][y]; //convert to a character eg. '1','2','3','#','*'
grantphillips 0:4bbd88022a6f 55 }
grantphillips 0:4bbd88022a6f 56 y++;
grantphillips 0:4bbd88022a6f 57 }
grantphillips 0:4bbd88022a6f 58 return(KeyValue);
grantphillips 0:4bbd88022a6f 59 }