4x4 Keypad easy to use library that pollis the interface width pullups

Dependents:   4x4KeyBoardExample xoxokey 4x4KeyBoardExample ProgettoCassaforte ... more

Fork of keypad by Dimiter K

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers keypad.cpp Source File

keypad.cpp

00001 /*   Copyright (c) 2015 Rune Langøy
00002 
00003 Permission is hereby granted, free of charge, to any person obtaining a copy
00004 of this software and associated documentation files (the "Software"), to deal
00005 in the Software without restriction, including without limitation the rights
00006 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00007 copies of the Software, and to permit persons to whom the Software is
00008 furnished to do so, subject to the following conditions:
00009 
00010 The above copyright notice and this permission notice shall be included in
00011 all copies or substantial portions of the Software.
00012 
00013 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00014 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00015 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00016 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00017 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00018 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00019 THE SOFTWARE.
00020 */
00021 
00022 /*
00023   Example is available in keypad.h
00024 
00025 */
00026 
00027 #include "mbed.h"
00028 #include "keypad.h"
00029 
00030 using namespace mbed;
00031 
00032 Keypad::Keypad(PinName col0, PinName col1, PinName col2, PinName col3,
00033                PinName row0,PinName row1, PinName row2, PinName row3):
00034     _cols(col0,col1,col2,col3) ,_rows(row0,row1,row2,row3)    {    }
00035 
00036 void Keypad::enablePullUp()
00037 {
00038     _cols.mode(PullUp);
00039 }
00040 
00041 bool Keypad::getKeyPressed()
00042 {
00043     _rows = 0;              // Ground all keys
00044     if(_cols.read()==0xff)  //Chk if key is pressed
00045         return false;
00046 
00047     return true;
00048 }
00049 
00050 int Keypad::getKeyIndex()
00051 {
00052     if (!getKeyPressed())
00053         return -1;
00054 
00055     //Scan rows and cols and return switch index
00056     for(int i=0; i<4; i++) {
00057         _rows = ~(0x01 << i);
00058         for(int j=0; j<4; j++)
00059             if (  !( (_cols.read()>> j )& 0x1 ))
00060                 return  j + (i*4);
00061     }
00062     return -1;
00063 }
00064 
00065 char Keypad::getKey()
00066 {
00067     int k = getKeyIndex();
00068     if(k != -1)
00069         return  keys[k];
00070 
00071     return 0;
00072 }