scan a 4x4 keypad by polling it continuously.

Dependents:   Keyboard_LCD tarea6_teclado keypad

Committer:
DimiterK
Date:
Sat Nov 06 14:36:22 2010 +0000
Revision:
0:1fa357ea3fcc

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DimiterK 0:1fa357ea3fcc 1 /* Copyright (c) 2010 Dimiter Kentri
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
DimiterK 0:1fa357ea3fcc 22 /*Simple example
DimiterK 0:1fa357ea3fcc 23
DimiterK 0:1fa357ea3fcc 24 #include "mbed.h"
DimiterK 0:1fa357ea3fcc 25 #include "keypad.h"
DimiterK 0:1fa357ea3fcc 26
DimiterK 0:1fa357ea3fcc 27 Serial pc(USBTX, USBRX);
DimiterK 0:1fa357ea3fcc 28 Keypad telepad(p13,p14,p15,p16,p17,p18,p19,p20);
DimiterK 0:1fa357ea3fcc 29
DimiterK 0:1fa357ea3fcc 30 int main(void){
DimiterK 0:1fa357ea3fcc 31 char key;
DimiterK 0:1fa357ea3fcc 32 pc.printf("Enter codes\n\r");
DimiterK 0:1fa357ea3fcc 33 while(1){
DimiterK 0:1fa357ea3fcc 34 key = telepad.getKey();
DimiterK 0:1fa357ea3fcc 35 if(key != KEY_RELEASED){
DimiterK 0:1fa357ea3fcc 36 pc.printf("%c\n",key);
DimiterK 0:1fa357ea3fcc 37 wait(0.6);
DimiterK 0:1fa357ea3fcc 38 }
DimiterK 0:1fa357ea3fcc 39 }
DimiterK 0:1fa357ea3fcc 40 } */
DimiterK 0:1fa357ea3fcc 41
DimiterK 0:1fa357ea3fcc 42 #include "mbed.h"
DimiterK 0:1fa357ea3fcc 43 #include "keypad.h"
DimiterK 0:1fa357ea3fcc 44
DimiterK 0:1fa357ea3fcc 45 using namespace mbed;
DimiterK 0:1fa357ea3fcc 46
DimiterK 0:1fa357ea3fcc 47 Keypad::Keypad(PinName col1, PinName col2, PinName col3, PinName col4, PinName row1,PinName row2, PinName row3, PinName row4): _col1(col1),
DimiterK 0:1fa357ea3fcc 48 _col2(col2),_col3(col3),_col4(col4),_rows(row1,row2,row3,row4) {
DimiterK 0:1fa357ea3fcc 49
DimiterK 0:1fa357ea3fcc 50 }
DimiterK 0:1fa357ea3fcc 51
DimiterK 0:1fa357ea3fcc 52 int Keypad::getKeyIndex(){
DimiterK 0:1fa357ea3fcc 53 for(int k=0; k<4; k++) {
DimiterK 0:1fa357ea3fcc 54 _rows = 0;
DimiterK 0:1fa357ea3fcc 55 }
DimiterK 0:1fa357ea3fcc 56 for(int i=0; i<4; i++) {
DimiterK 0:1fa357ea3fcc 57 _rows = 1 << i;
DimiterK 0:1fa357ea3fcc 58 for(int j=0; j<4;j++){
DimiterK 0:1fa357ea3fcc 59 if (_col1)
DimiterK 0:1fa357ea3fcc 60 return 1+(i*4);
DimiterK 0:1fa357ea3fcc 61 else if (_col2)
DimiterK 0:1fa357ea3fcc 62 return 2+(i*4);
DimiterK 0:1fa357ea3fcc 63 else if (_col3)
DimiterK 0:1fa357ea3fcc 64 return 3+(i*4);
DimiterK 0:1fa357ea3fcc 65 else if (_col4)
DimiterK 0:1fa357ea3fcc 66 return 4+(i*4);
DimiterK 0:1fa357ea3fcc 67 }
DimiterK 0:1fa357ea3fcc 68 }
DimiterK 0:1fa357ea3fcc 69 return -1;
DimiterK 0:1fa357ea3fcc 70 }
DimiterK 0:1fa357ea3fcc 71
DimiterK 0:1fa357ea3fcc 72 char Keypad::getKey(){
DimiterK 0:1fa357ea3fcc 73 int k = getKeyIndex();
DimiterK 0:1fa357ea3fcc 74 if(k != -1)
DimiterK 0:1fa357ea3fcc 75 return keys[k-1];
DimiterK 0:1fa357ea3fcc 76 else
DimiterK 0:1fa357ea3fcc 77 return 0;
DimiterK 0:1fa357ea3fcc 78 }