Roman Valencia / Mbed 2 deprecated MultiKey

Dependencies:   Hotboards_keypad mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #include "mbed.h"
00003 #include "Hotboards_keypad.h"
00004 #include <string>
00005 
00006 using std::string;
00007 
00008 // Defines the keys array with it's respective number of rows & cols,
00009 // and with the value of each key
00010 char keys[ 4 ][ 4 ] =
00011 {
00012     { '1' , '2' , '3' , 'A' },
00013     { '4' , '5' , '6' , 'B' },
00014     { '7' , '8' , '9' , 'C' },
00015     { '*' , '0' , '#' , 'D' }
00016 };
00017 
00018 // Defines the pins connected to the rows
00019 DigitalInOut rowPins[ 4 ] = { PA_6 , PA_7 , PB_6 , PC_7 };
00020 // Defines the pins connected to the cols
00021 DigitalInOut colPins[ 4 ] = { PA_8 , PB_10 , PB_4 , PB_5 };
00022 
00023 // Creates a new keyboard with the values entered before
00024 Keypad kpd( makeKeymap( keys ) , rowPins , colPins , 4 , 4 );
00025 
00026 // Configures the serial port
00027 Serial pc( USBTX , USBRX );
00028 
00029 int i;
00030 
00031 int main()
00032 {
00033     string msg;
00034     while(1)
00035     {
00036         // Fills kpd.key[ ] array with up-to 10 active keys.
00037         // Returns true if there are ANY active keys.        
00038         if( kpd.getKeys( ) )
00039         {
00040             // Scan the whole key list.
00041             for( i = 0 ; i <= LIST_MAX ; i++ )
00042             {
00043                 // Only find keys that have changed state.
00044                 if( kpd.key[ i ].stateChanged )
00045                 {
00046                     // Report active key state : IDLE, PRESSED, HOLD, or RELEASED
00047                     switch( kpd.key[ i ].kstate )
00048                     {
00049                         case PRESSED:
00050                             msg = " PRESSED. ";
00051                             break;
00052                         case HOLD:
00053                             msg = " HOLD. ";
00054                             break;
00055                         case RELEASED:
00056                             msg = " RELEASED. ";
00057                             break;
00058                         case IDLE:
00059                             msg = " IDLE. ";
00060                             break;
00061                         default:
00062                             break;
00063                     }
00064                     // Print the current state of the key pressed
00065                     pc.printf( "Key " );
00066                     pc.printf( "%c" , kpd.key[ i ].kchar );
00067                     pc.printf( "%s" , msg.c_str() );
00068                     pc.printf( "\n\r" );
00069                 }
00070             }
00071         }
00072     }
00073 }