Sense multiple keypresses from a 4x4 keypad

Dependencies:   Hotboards_keypad mbed

Fork of Hotboards_MultiKey by Hotboards MX

Committer:
icserny
Date:
Mon May 30 09:51:22 2016 +0000
Revision:
2:5710f598e683
Parent:
1:bd23ec9647fd
First version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Hotboards 1:bd23ec9647fd 1 /* @file MultiKey.cpp
icserny 2:5710f598e683 2 || @vesion 1.2
icserny 2:5710f598e683 3 || @modified by Istvan Cserny
icserny 2:5710f598e683 4 || @contact https://developer.mbed.org/users/icserny/
Hotboards 1:bd23ec9647fd 5 || @version 1.1
Hotboards 1:bd23ec9647fd 6 || @modified by Diego (http://hotboards.org)
Hotboards 1:bd23ec9647fd 7 || @version 1.0
Hotboards 1:bd23ec9647fd 8 || @author Mark Stanley
Hotboards 1:bd23ec9647fd 9 || @contact mstanley@technologist.com
Hotboards 1:bd23ec9647fd 10 ||
Hotboards 1:bd23ec9647fd 11 || @description
Hotboards 1:bd23ec9647fd 12 || | The latest version, 3.0, of the keypad library supports up to 10
Hotboards 1:bd23ec9647fd 13 || | active keys all being pressed at the same time. This sketch is an
Hotboards 1:bd23ec9647fd 14 || | example of how you can get multiple key presses from a keypad or
Hotboards 1:bd23ec9647fd 15 || | keyboard.
RomanValenciaP 0:d39ee4a4aec6 16
Hotboards 1:bd23ec9647fd 17 */
RomanValenciaP 0:d39ee4a4aec6 18 #include "mbed.h"
RomanValenciaP 0:d39ee4a4aec6 19 #include "Hotboards_keypad.h"
RomanValenciaP 0:d39ee4a4aec6 20 #include <string>
RomanValenciaP 0:d39ee4a4aec6 21
RomanValenciaP 0:d39ee4a4aec6 22 using std::string;
RomanValenciaP 0:d39ee4a4aec6 23
RomanValenciaP 0:d39ee4a4aec6 24 // Defines the keys array with it's respective number of rows & cols,
RomanValenciaP 0:d39ee4a4aec6 25 // and with the value of each key
RomanValenciaP 0:d39ee4a4aec6 26 char keys[ 4 ][ 4 ] =
RomanValenciaP 0:d39ee4a4aec6 27 {
RomanValenciaP 0:d39ee4a4aec6 28 { '1' , '2' , '3' , 'A' },
RomanValenciaP 0:d39ee4a4aec6 29 { '4' , '5' , '6' , 'B' },
RomanValenciaP 0:d39ee4a4aec6 30 { '7' , '8' , '9' , 'C' },
RomanValenciaP 0:d39ee4a4aec6 31 { '*' , '0' , '#' , 'D' }
RomanValenciaP 0:d39ee4a4aec6 32 };
RomanValenciaP 0:d39ee4a4aec6 33
RomanValenciaP 0:d39ee4a4aec6 34 // Defines the pins connected to the rows
icserny 2:5710f598e683 35 DigitalInOut rowPins[ 4 ] = { PTB8 , PTB9 , PTB10 , PTB11 };
RomanValenciaP 0:d39ee4a4aec6 36 // Defines the pins connected to the cols
icserny 2:5710f598e683 37 DigitalInOut colPins[ 4 ] = { PTE2 , PTE3 , PTE4 , PTE5 };
RomanValenciaP 0:d39ee4a4aec6 38
RomanValenciaP 0:d39ee4a4aec6 39 // Creates a new keyboard with the values entered before
RomanValenciaP 0:d39ee4a4aec6 40 Keypad kpd( makeKeymap( keys ) , rowPins , colPins , 4 , 4 );
RomanValenciaP 0:d39ee4a4aec6 41
RomanValenciaP 0:d39ee4a4aec6 42 // Configures the serial port
RomanValenciaP 0:d39ee4a4aec6 43 Serial pc( USBTX , USBRX );
RomanValenciaP 0:d39ee4a4aec6 44
RomanValenciaP 0:d39ee4a4aec6 45 int i;
RomanValenciaP 0:d39ee4a4aec6 46
RomanValenciaP 0:d39ee4a4aec6 47 int main()
RomanValenciaP 0:d39ee4a4aec6 48 {
RomanValenciaP 0:d39ee4a4aec6 49 string msg;
RomanValenciaP 0:d39ee4a4aec6 50 while(1)
RomanValenciaP 0:d39ee4a4aec6 51 {
RomanValenciaP 0:d39ee4a4aec6 52 // Fills kpd.key[ ] array with up-to 10 active keys.
RomanValenciaP 0:d39ee4a4aec6 53 // Returns true if there are ANY active keys.
RomanValenciaP 0:d39ee4a4aec6 54 if( kpd.getKeys( ) )
RomanValenciaP 0:d39ee4a4aec6 55 {
RomanValenciaP 0:d39ee4a4aec6 56 // Scan the whole key list.
icserny 2:5710f598e683 57 for( i = 0 ; i < LIST_MAX ; i++ )
RomanValenciaP 0:d39ee4a4aec6 58 {
RomanValenciaP 0:d39ee4a4aec6 59 // Only find keys that have changed state.
RomanValenciaP 0:d39ee4a4aec6 60 if( kpd.key[ i ].stateChanged )
RomanValenciaP 0:d39ee4a4aec6 61 {
RomanValenciaP 0:d39ee4a4aec6 62 // Report active key state : IDLE, PRESSED, HOLD, or RELEASED
RomanValenciaP 0:d39ee4a4aec6 63 switch( kpd.key[ i ].kstate )
RomanValenciaP 0:d39ee4a4aec6 64 {
RomanValenciaP 0:d39ee4a4aec6 65 case PRESSED:
RomanValenciaP 0:d39ee4a4aec6 66 msg = " PRESSED. ";
RomanValenciaP 0:d39ee4a4aec6 67 break;
RomanValenciaP 0:d39ee4a4aec6 68 case HOLD:
RomanValenciaP 0:d39ee4a4aec6 69 msg = " HOLD. ";
RomanValenciaP 0:d39ee4a4aec6 70 break;
RomanValenciaP 0:d39ee4a4aec6 71 case RELEASED:
RomanValenciaP 0:d39ee4a4aec6 72 msg = " RELEASED. ";
RomanValenciaP 0:d39ee4a4aec6 73 break;
RomanValenciaP 0:d39ee4a4aec6 74 case IDLE:
RomanValenciaP 0:d39ee4a4aec6 75 msg = " IDLE. ";
RomanValenciaP 0:d39ee4a4aec6 76 break;
RomanValenciaP 0:d39ee4a4aec6 77 default:
RomanValenciaP 0:d39ee4a4aec6 78 break;
RomanValenciaP 0:d39ee4a4aec6 79 }
RomanValenciaP 0:d39ee4a4aec6 80 // Print the current state of the key pressed
icserny 2:5710f598e683 81 pc.printf( "Key " );
RomanValenciaP 0:d39ee4a4aec6 82 pc.printf( "%c" , kpd.key[ i ].kchar );
RomanValenciaP 0:d39ee4a4aec6 83 pc.printf( "%s" , msg.c_str() );
RomanValenciaP 0:d39ee4a4aec6 84 pc.printf( "\n\r" );
RomanValenciaP 0:d39ee4a4aec6 85 }
RomanValenciaP 0:d39ee4a4aec6 86 }
RomanValenciaP 0:d39ee4a4aec6 87 }
RomanValenciaP 0:d39ee4a4aec6 88 }
RomanValenciaP 0:d39ee4a4aec6 89 }