Hotboards MX / Mbed 2 deprecated Hotboards_EventKeypad

Dependencies:   Hotboards_keypad mbed

Fork of EventKeypad by Roman Valencia

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* @file Hotboards_keypad.cpp
00002  || @version 1.1
00003  || @modified by Diego (http://hotboards.org)
00004  || @author Alexander Brevig
00005  || @contact alexanderbrevig@gmail.com
00006  ||
00007  || @description
00008  || | Demonstrates using the KeypadEvent.
00009 
00010  */
00011 #include "mbed.h"
00012 #include "Hotboards_keypad.h"
00013 
00014 // Defines the keys array with it's respective number of rows & cols,
00015 // and with the value of each key
00016 char keys[ 4 ][ 4 ] =
00017 {
00018     { '1' , '2' , '3' , 'A' },
00019     { '4' , '5' , '6' , 'B' },
00020     { '7' , '8' , '9' , 'C' },
00021     { '*' , '0' , '#' , 'D' }
00022 };
00023 
00024 // Defines the pins connected to the rows
00025 DigitalInOut rowPins[ 4 ] = { PA_6 , PA_7 , PB_6 , PC_7 };
00026 // Defines the pins connected to the cols
00027 DigitalInOut colPins[ 4 ] = { PA_8 , PB_10 , PB_4 , PB_5 };
00028 
00029 // Creates a new keyboard with the values entered before
00030 Keypad kpd( makeKeymap( keys ) , rowPins , colPins , 4 , 4 );
00031 
00032 // Configures the serial port
00033 Serial pc( USBTX , USBRX );
00034 
00035 // For this example we will use the Nucleo LED1 on pin PA_5
00036 DigitalOut led1( LED1 );
00037 bool blink = false;
00038 bool ledPin_state;
00039 
00040 
00041 // Taking care of some special events.
00042 void kpdEvent( KeypadEvent key )
00043 {
00044     switch( kpd.getState( ) )
00045     {
00046         case PRESSED:
00047             if( key == '#' )
00048             {
00049                 led1 = !led1;
00050                 ledPin_state = led1; // Remember LED state, lit or unlit.
00051             }
00052             break;
00053         case RELEASED:
00054             if( key == '*' )
00055             {
00056                 led1 = ledPin_state;
00057                 blink = false; // Restore LED state from before it started blinking.
00058             }
00059             break;
00060         case HOLD:
00061             if( key == '*' )
00062             {
00063                 blink = true; // Blink the LED when holding the * key.
00064             }
00065             break;
00066     }
00067 }
00068 
00069 int main()
00070 {
00071     led1 = 1; // Turn the LED on.
00072     ledPin_state = led1; // Store initial LED state. HIGH when LED is on.
00073     kpd.addEventListener( kpdEvent ); // Add an event listener for this keypad
00074     while(1)
00075     {
00076         char key = kpd.getKey( );
00077         if( key )
00078         {
00079             pc.printf( "%c" , key );
00080         }
00081         if( blink )
00082         {
00083             led1 = !led1; // Change the ledPin from Hi2Lo or Lo2Hi
00084             wait_ms( 100 );
00085         }
00086     }
00087 }