keypad driver

Dependents:   class_keypad_lcd_password

Fork of Keypadlatest by tatiuc embedded

Committer:
mijimy
Date:
Tue Jul 04 08:44:57 2017 +0000
Revision:
2:909929293a23
Parent:
1:dd892be4b7c7
keypad.h adjust;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
grantphillips 0:4bbd88022a6f 1 /* Keypad Library v1.0
grantphillips 0:4bbd88022a6f 2 * Copyright (c) 2016 Grant Phillips
grantphillips 0:4bbd88022a6f 3 * grant.phillips@nmmu.ac.za
grantphillips 0:4bbd88022a6f 4 *
grantphillips 0:4bbd88022a6f 5 * This is a modified version of Riaan Ehlers' library which was written for the
grantphillips 0:4bbd88022a6f 6 * Microchip PIC18 series microcontrollers.
grantphillips 0:4bbd88022a6f 7 *
grantphillips 0:4bbd88022a6f 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
grantphillips 0:4bbd88022a6f 9 * of this software and associated documentation files (the "Software"), to deal
grantphillips 0:4bbd88022a6f 10 * in the Software without restriction, including without limitation the rights
grantphillips 0:4bbd88022a6f 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
grantphillips 0:4bbd88022a6f 12 * copies of the Software, and to permit persons to whom the Software is
grantphillips 0:4bbd88022a6f 13 * furnished to do so, subject to the following conditions:
grantphillips 0:4bbd88022a6f 14 *
grantphillips 0:4bbd88022a6f 15 * The above copyright notice and this permission notice shall be included in
grantphillips 0:4bbd88022a6f 16 * all copies or substantial portions of the Software.
grantphillips 0:4bbd88022a6f 17 *
grantphillips 0:4bbd88022a6f 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
grantphillips 0:4bbd88022a6f 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
grantphillips 0:4bbd88022a6f 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
grantphillips 0:4bbd88022a6f 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
grantphillips 0:4bbd88022a6f 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
grantphillips 0:4bbd88022a6f 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
grantphillips 0:4bbd88022a6f 24 * THE SOFTWARE.
grantphillips 0:4bbd88022a6f 25 */
grantphillips 0:4bbd88022a6f 26
grantphillips 0:4bbd88022a6f 27 #ifndef Keypad_H
grantphillips 0:4bbd88022a6f 28 #define Keypad_H
grantphillips 0:4bbd88022a6f 29
grantphillips 0:4bbd88022a6f 30 #include "mbed.h"
grantphillips 0:4bbd88022a6f 31
grantphillips 0:4bbd88022a6f 32 /** Class library for a polling-based 4x4 keypad.
grantphillips 0:4bbd88022a6f 33 *
grantphillips 0:4bbd88022a6f 34 * Example:
grantphillips 0:4bbd88022a6f 35 * @code
grantphillips 0:4bbd88022a6f 36 * #include "mbed.h"
grantphillips 0:4bbd88022a6f 37 * #include "Keypad.h"
mijimy 2:909929293a23 38 * char kpdLayout[4][4] = {{'1' ,'2' ,'3' ,'4'}, //row0
mijimy 2:909929293a23 39 * {'4' ,'5' ,'6' ,'B'}, //row1
mijimy 2:909929293a23 40 * {'7' ,'8' ,'9' ,'C'}, //row2
mijimy 2:909929293a23 41 * {'*' ,'0' ,'#' ,'D'}}; //row3
mijimy 2:909929293a23 42
grantphillips 0:4bbd88022a6f 43 * Keypad kpad(PE_15, PE_14, PE_13, PE_12, PE_11, PE_10, PE_9, PE_8);
grantphillips 0:4bbd88022a6f 44 *
grantphillips 0:4bbd88022a6f 45 * int main() {
grantphillips 0:4bbd88022a6f 46 * char key;
grantphillips 0:4bbd88022a6f 47 * int released = 1;
grantphillips 0:4bbd88022a6f 48 *
grantphillips 0:4bbd88022a6f 49 * while(1){
grantphillips 0:4bbd88022a6f 50 * key = kpad.ReadKey(); //read the current key pressed
grantphillips 0:4bbd88022a6f 51 *
grantphillips 0:4bbd88022a6f 52 * if(key == '\0')
grantphillips 0:4bbd88022a6f 53 * released = 1; //set the flag when all keys are released
grantphillips 0:4bbd88022a6f 54 *
grantphillips 0:4bbd88022a6f 55 * if((key != '\0') && (released == 1)) { //if a key is pressed AND previous key was released
grantphillips 0:4bbd88022a6f 56 * printf("%c\n", key);
grantphillips 0:4bbd88022a6f 57 * released = 0; //clear the flag to indicate that key is still pressed
grantphillips 0:4bbd88022a6f 58 * }
grantphillips 0:4bbd88022a6f 59 * }
grantphillips 0:4bbd88022a6f 60 * }
grantphillips 0:4bbd88022a6f 61 * @endcode
grantphillips 0:4bbd88022a6f 62 */
grantphillips 0:4bbd88022a6f 63
grantphillips 0:4bbd88022a6f 64 class Keypad {
grantphillips 0:4bbd88022a6f 65 public:
grantphillips 0:4bbd88022a6f 66 /** Create a Keypad object.
grantphillips 0:4bbd88022a6f 67 * @param col1..4 DigitalOut (or BusOut) compatible pins for keypad Column data lines
grantphillips 0:4bbd88022a6f 68 * @param row1..4 DigitalIn (or BusIn) compatible pins for keypad Row data lines
grantphillips 0:4bbd88022a6f 69 */
grantphillips 0:4bbd88022a6f 70 Keypad(PinName col1, PinName col2, PinName col3, PinName col4, PinName row1, PinName row2, PinName row3, PinName row4);
grantphillips 0:4bbd88022a6f 71
grantphillips 0:4bbd88022a6f 72 /** Returns the letter of the key pressed.
grantphillips 0:4bbd88022a6f 73 * @param
grantphillips 0:4bbd88022a6f 74 * None
grantphillips 0:4bbd88022a6f 75 * @return
grantphillips 0:4bbd88022a6f 76 * The character of the key pressed. Returns '\0' if no key is pressed.
grantphillips 0:4bbd88022a6f 77 */
grantphillips 0:4bbd88022a6f 78 char ReadKey();
grantphillips 0:4bbd88022a6f 79
grantphillips 0:4bbd88022a6f 80
grantphillips 0:4bbd88022a6f 81 private:
grantphillips 0:4bbd88022a6f 82 BusOut _cols;
mijimy 1:dd892be4b7c7 83 BusIn _rows ;
mijimy 1:dd892be4b7c7 84
grantphillips 0:4bbd88022a6f 85 };
grantphillips 0:4bbd88022a6f 86
grantphillips 0:4bbd88022a6f 87 #endif