Hotboards MX / Mbed 2 deprecated Hotboards_MultiKey

Dependencies:   Hotboards_keypad mbed

Fork of MultiKey by Roman Valencia

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* @file MultiKey.cpp
00002 || @version 1.1
00003 || @modified by Diego (http://hotboards.org)
00004 || @version 1.0
00005 || @author Mark Stanley
00006 || @contact mstanley@technologist.com
00007 ||
00008 || @description
00009 || | The latest version, 3.0, of the keypad library supports up to 10
00010 || | active keys all being pressed at the same time. This sketch is an
00011 || | example of how you can get multiple key presses from a keypad or
00012 || | keyboard.
00013 
00014 */
00015 #include "mbed.h"
00016 #include "Hotboards_keypad.h"
00017 #include <string>
00018 
00019 using std::string;
00020 
00021 // Defines the keys array with it's respective number of rows & cols,
00022 // and with the value of each key
00023 char keys[ 4 ][ 4 ] =
00024 {
00025     { '1' , '2' , '3' , 'A' },
00026     { '4' , '5' , '6' , 'B' },
00027     { '7' , '8' , '9' , 'C' },
00028     { '*' , '0' , '#' , 'D' }
00029 };
00030 
00031 // Defines the pins connected to the rows
00032 DigitalInOut rowPins[ 4 ] = { PA_6 , PA_7 , PB_6 , PC_7 };
00033 // Defines the pins connected to the cols
00034 DigitalInOut colPins[ 4 ] = { PA_8 , PB_10 , PB_4 , PB_5 };
00035 
00036 // Creates a new keyboard with the values entered before
00037 Keypad kpd( makeKeymap( keys ) , rowPins , colPins , 4 , 4 );
00038 
00039 // Configures the serial port
00040 Serial pc( USBTX , USBRX );
00041 
00042 int i;
00043 
00044 int main()
00045 {
00046     string msg;
00047     while(1)
00048     {
00049         // Fills kpd.key[ ] array with up-to 10 active keys.
00050         // Returns true if there are ANY active keys.        
00051         if( kpd.getKeys( ) )
00052         {
00053             // Scan the whole key list.
00054             for( i = 0 ; i <= LIST_MAX ; i++ )
00055             {
00056                 // Only find keys that have changed state.
00057                 if( kpd.key[ i ].stateChanged )
00058                 {
00059                     // Report active key state : IDLE, PRESSED, HOLD, or RELEASED
00060                     switch( kpd.key[ i ].kstate )
00061                     {
00062                         case PRESSED:
00063                             msg = " PRESSED. ";
00064                             break;
00065                         case HOLD:
00066                             msg = " HOLD. ";
00067                             break;
00068                         case RELEASED:
00069                             msg = " RELEASED. ";
00070                             break;
00071                         case IDLE:
00072                             msg = " IDLE. ";
00073                             break;
00074                         default:
00075                             break;
00076                     }
00077                     // Print the current state of the key pressed
00078                     pc.printf( "Key " );
00079                     pc.printf( "%c" , kpd.key[ i ].kchar );
00080                     pc.printf( "%s" , msg.c_str() );
00081                     pc.printf( "\n\r" );
00082                 }
00083             }
00084         }
00085     }
00086 }