Class library for a polling-based 4x4 keypad.
Fork of Keypad2 by
Embed:
(wiki syntax)
Show/hide line numbers
Keypad.cpp
00001 #include "Keypad.h" 00002 #include "mbed.h" 00003 00004 00005 // Keypad layout: 00006 // [row][col] Col0 Col1 Col2 Col3 00007 /*char const kpdLayout[4][4] = {{'1' ,'2' ,'3' ,'4'}, //row0 00008 {'4' ,'5' ,'6' ,'B'}, //row1 00009 {'7' ,'8' ,'9' ,'C'}, //row2 00010 {'*' ,'0' ,'#' ,'D'}}; //row3*/ 00011 extern char kpdLayout[4][4]; 00012 //NIBBLE LOW=0000, HIGH= 0111 1011 1101 1110 Col (x) 00013 //const char KpdInMask[4] ={0xe0,0xd0,0xb0,0x70}; 00014 const char KpdInMask[4] ={0x0e,0x0d,0x0b,0x07}; 00015 00016 //NIBBLE HIGH=1111, LOW= 0111 1011 1101 1110 Rows (y) 00017 //const char KpdOutMask[4]={0xfe,0xfd,0xfb,0xf7}; 00018 const char KpdOutMask[4]={0x0e,0x0d,0x0b,0x07}; 00019 00020 00021 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) { } 00022 00023 char Keypad::ReadKey() { 00024 char KeyValue, Done=0; 00025 uint16_t y, x; 00026 _rows.mode(PullUp); 00027 00028 //delay_ms(ContactBounceTime); //warning no contact bounce protection 00029 //call read_key more than once with delay 00030 //between if key stay constant then key is pressed 00031 y = 0; 00032 while((y < 4) && (!Done)) 00033 { 00034 _cols = KpdOutMask[y]; //write mask value to the column outputs 00035 wait(0.01); 00036 00037 KeyValue = _rows; //read mask value from the row inputs 00038 00039 if(KeyValue == KpdInMask[0]) 00040 x = 0; 00041 else if(KeyValue == KpdInMask[1]) 00042 x = 1; 00043 else if(KeyValue == KpdInMask[2]) 00044 x = 2; 00045 else if(KeyValue == KpdInMask[3]) 00046 x = 3; 00047 else 00048 { 00049 KeyValue='\0'; //more than one key was pressed or no key in this row. 00050 x=9; 00051 } 00052 if(x != 9) 00053 { 00054 Done = 1; //valid key found 00055 KeyValue = kpdLayout[x][y]; //convert to a character eg. '1','2','3','#','*' 00056 } 00057 y++; 00058 } 00059 return(KeyValue); 00060 }
Generated on Thu Jul 14 2022 03:48:57 by 1.7.2