hobbielektronika / Mbed 2 deprecated 26_Hotboards_EventKeypad

Dependencies:   Hotboards_keypad mbed

Fork of Hotboards_EventKeypad by Hotboards MX

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