pt aliff

Fork of keypad by Dimiter K

Committer:
aliffhilmie93
Date:
Mon Jun 04 16:01:47 2018 +0000
Revision:
1:ced7b1445bc4
Parent:
0:1fa357ea3fcc
pt aliff

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aliffhilmie93 1:ced7b1445bc4 1 /* Copyright (c) 2015 Rune Langøy
DimiterK 0:1fa357ea3fcc 2
DimiterK 0:1fa357ea3fcc 3 Permission is hereby granted, free of charge, to any person obtaining a copy
DimiterK 0:1fa357ea3fcc 4 of this software and associated documentation files (the "Software"), to deal
DimiterK 0:1fa357ea3fcc 5 in the Software without restriction, including without limitation the rights
DimiterK 0:1fa357ea3fcc 6 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
DimiterK 0:1fa357ea3fcc 7 copies of the Software, and to permit persons to whom the Software is
DimiterK 0:1fa357ea3fcc 8 furnished to do so, subject to the following conditions:
DimiterK 0:1fa357ea3fcc 9
DimiterK 0:1fa357ea3fcc 10 The above copyright notice and this permission notice shall be included in
DimiterK 0:1fa357ea3fcc 11 all copies or substantial portions of the Software.
DimiterK 0:1fa357ea3fcc 12
DimiterK 0:1fa357ea3fcc 13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
DimiterK 0:1fa357ea3fcc 14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
DimiterK 0:1fa357ea3fcc 15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
DimiterK 0:1fa357ea3fcc 16 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
DimiterK 0:1fa357ea3fcc 17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
DimiterK 0:1fa357ea3fcc 18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
DimiterK 0:1fa357ea3fcc 19 THE SOFTWARE.
DimiterK 0:1fa357ea3fcc 20 */
DimiterK 0:1fa357ea3fcc 21
aliffhilmie93 1:ced7b1445bc4 22 /*
aliffhilmie93 1:ced7b1445bc4 23 Example is available in keypad.h
DimiterK 0:1fa357ea3fcc 24
aliffhilmie93 1:ced7b1445bc4 25 */
DimiterK 0:1fa357ea3fcc 26
DimiterK 0:1fa357ea3fcc 27 #include "mbed.h"
DimiterK 0:1fa357ea3fcc 28 #include "keypad.h"
DimiterK 0:1fa357ea3fcc 29
DimiterK 0:1fa357ea3fcc 30 using namespace mbed;
DimiterK 0:1fa357ea3fcc 31
aliffhilmie93 1:ced7b1445bc4 32 Keypad::Keypad(PinName col0, PinName col1, PinName col2, PinName col3,
aliffhilmie93 1:ced7b1445bc4 33 PinName row0,PinName row1, PinName row2, PinName row3):
aliffhilmie93 1:ced7b1445bc4 34 _cols(col0,col1,col2,col3) ,_rows(row0,row1,row2,row3) { }
aliffhilmie93 1:ced7b1445bc4 35
aliffhilmie93 1:ced7b1445bc4 36 void Keypad::enablePullUp()
aliffhilmie93 1:ced7b1445bc4 37 {
aliffhilmie93 1:ced7b1445bc4 38 _cols.mode(PullUp);
aliffhilmie93 1:ced7b1445bc4 39 }
DimiterK 0:1fa357ea3fcc 40
aliffhilmie93 1:ced7b1445bc4 41 bool Keypad::getKeyPressed()
aliffhilmie93 1:ced7b1445bc4 42 {
aliffhilmie93 1:ced7b1445bc4 43 _rows = 0; // Ground all keys
aliffhilmie93 1:ced7b1445bc4 44 if(_cols.read()==0xff) //Chk if key is pressed
aliffhilmie93 1:ced7b1445bc4 45 return false;
aliffhilmie93 1:ced7b1445bc4 46
aliffhilmie93 1:ced7b1445bc4 47 return true;
aliffhilmie93 1:ced7b1445bc4 48 }
aliffhilmie93 1:ced7b1445bc4 49
aliffhilmie93 1:ced7b1445bc4 50 int Keypad::getKeyIndex()
aliffhilmie93 1:ced7b1445bc4 51 {
aliffhilmie93 1:ced7b1445bc4 52 if (!getKeyPressed())
aliffhilmie93 1:ced7b1445bc4 53 return -1;
aliffhilmie93 1:ced7b1445bc4 54
aliffhilmie93 1:ced7b1445bc4 55 //Scan rows and cols and return switch index
DimiterK 0:1fa357ea3fcc 56 for(int i=0; i<4; i++) {
aliffhilmie93 1:ced7b1445bc4 57 _rows = ~(0x01 << i);
aliffhilmie93 1:ced7b1445bc4 58 for(int j=0; j<4; j++)
aliffhilmie93 1:ced7b1445bc4 59 if ( !( (_cols.read()>> j )& 0x1 ))
aliffhilmie93 1:ced7b1445bc4 60 return j + (i*4);
aliffhilmie93 1:ced7b1445bc4 61 }
DimiterK 0:1fa357ea3fcc 62 return -1;
aliffhilmie93 1:ced7b1445bc4 63 }
DimiterK 0:1fa357ea3fcc 64
aliffhilmie93 1:ced7b1445bc4 65 char Keypad::getKey()
aliffhilmie93 1:ced7b1445bc4 66 {
aliffhilmie93 1:ced7b1445bc4 67 int k = getKeyIndex();
aliffhilmie93 1:ced7b1445bc4 68 if(k != -1)
aliffhilmie93 1:ced7b1445bc4 69 return keys[k];
aliffhilmie93 1:ced7b1445bc4 70
aliffhilmie93 1:ced7b1445bc4 71 return 0;
DimiterK 0:1fa357ea3fcc 72 }