Class library for a polling-based 4x4 keypad.
Dependents: LCD kbdlcdkl25z control_onoff control_onoff ... more
Revision 0:4bbd88022a6f, committed 2016-04-05
- Comitter:
- grantphillips
- Date:
- Tue Apr 05 17:37:55 2016 +0000
- Commit message:
- v1.00
Changed in this revision
Keypad.cpp | Show annotated file Show diff for this revision Revisions of this file |
Keypad.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Keypad.cpp Tue Apr 05 17:37:55 2016 +0000 @@ -0,0 +1,59 @@ +#include "Keypad.h" +#include "mbed.h" + + +// Keypad layout: +// [row][col] Col0 Col1 Col2 Col3 +char const kpdLayout[4][4] = {{'1' ,'2' ,'3' ,'A'}, //row0 + {'4' ,'5' ,'6' ,'B'}, //row1 + {'7' ,'8' ,'9' ,'C'}, //row2 + {'*' ,'0' ,'#' ,'D'}}; //row3 + +//NIBBLE LOW=0000, HIGH= 0111 1011 1101 1110 Col (x) +//const char KpdInMask[4] ={0xe0,0xd0,0xb0,0x70}; +const char KpdInMask[4] ={0x0e,0x0d,0x0b,0x07}; + +//NIBBLE HIGH=1111, LOW= 0111 1011 1101 1110 Rows (y) +//const char KpdOutMask[4]={0xfe,0xfd,0xfb,0xf7}; +const char KpdOutMask[4]={0x0e,0x0d,0x0b,0x07}; + + +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) { } + +char Keypad::ReadKey() { + char KeyValue, Done=0; + uint16_t y, x; + + //delay_ms(ContactBounceTime); //warning no contact bounce protection + //call read_key more than once with delay + //between if key stay constant then key is pressed + y = 0; + while((y < 4) && (!Done)) + { + _cols = KpdOutMask[y]; //write mask value to the column outputs + wait(0.01); + + KeyValue = _rows; //read mask value from the row inputs + + if(KeyValue == KpdInMask[0]) + x = 0; + else if(KeyValue == KpdInMask[1]) + x = 1; + else if(KeyValue == KpdInMask[2]) + x = 2; + else if(KeyValue == KpdInMask[3]) + x = 3; + else + { + KeyValue='\0'; //more than one key was pressed or no key in this row. + x=9; + } + if(x != 9) + { + Done = 1; //valid key found + KeyValue = kpdLayout[x][y]; //convert to a character eg. '1','2','3','#','*' + } + y++; + } + return(KeyValue); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Keypad.h Tue Apr 05 17:37:55 2016 +0000 @@ -0,0 +1,82 @@ +/* Keypad Library v1.0 + * Copyright (c) 2016 Grant Phillips + * grant.phillips@nmmu.ac.za + * + * This is a modified version of Riaan Ehlers' library which was written for the + * Microchip PIC18 series microcontrollers. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef Keypad_H +#define Keypad_H + +#include "mbed.h" + +/** Class library for a polling-based 4x4 keypad. + * + * Example: + * @code + * #include "mbed.h" + * #include "Keypad.h" + * + * Keypad kpad(PE_15, PE_14, PE_13, PE_12, PE_11, PE_10, PE_9, PE_8); + * + * int main() { + * char key; + * int released = 1; + * + * while(1){ + * key = kpad.ReadKey(); //read the current key pressed + * + * if(key == '\0') + * released = 1; //set the flag when all keys are released + * + * if((key != '\0') && (released == 1)) { //if a key is pressed AND previous key was released + * printf("%c\n", key); + * released = 0; //clear the flag to indicate that key is still pressed + * } + * } + * } + * @endcode + */ + +class Keypad { + public: + /** Create a Keypad object. + * @param col1..4 DigitalOut (or BusOut) compatible pins for keypad Column data lines + * @param row1..4 DigitalIn (or BusIn) compatible pins for keypad Row data lines + */ + Keypad(PinName col1, PinName col2, PinName col3, PinName col4, PinName row1, PinName row2, PinName row3, PinName row4); + + /** Returns the letter of the key pressed. + * @param + * None + * @return + * The character of the key pressed. Returns '\0' if no key is pressed. + */ + char ReadKey(); + + + private: + BusOut _cols; + BusIn _rows; +}; + +#endif \ No newline at end of file