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