Demonstrates handling events of a 4x4 keypad Forked from https://developer.mbed.org/users/Hotboards/code/Hotboards_EventKeypad/

Dependencies:   Hotboards_keypad mbed

Fork of Hotboards_EventKeypad by Hotboards MX

Committer:
Hotboards
Date:
Tue Mar 08 21:23:41 2016 +0000
Revision:
1:88d227ecb9b3
Parent:
0:a2b95a8cb722
Child:
2:252f5ac72f2d
first release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Hotboards 1:88d227ecb9b3 1 /* @file Hotboards_keypad.cpp
Hotboards 1:88d227ecb9b3 2 || @version 1.1
Hotboards 1:88d227ecb9b3 3 || @modified by Diego (http://hotboards.org)
Hotboards 1:88d227ecb9b3 4 || @author Alexander Brevig
Hotboards 1:88d227ecb9b3 5 || @contact alexanderbrevig@gmail.com
Hotboards 1:88d227ecb9b3 6 ||
Hotboards 1:88d227ecb9b3 7 || @description
Hotboards 1:88d227ecb9b3 8 || | Demonstrates using the KeypadEvent.
RomanValenciaP 0:a2b95a8cb722 9
Hotboards 1:88d227ecb9b3 10 */
RomanValenciaP 0:a2b95a8cb722 11 #include "mbed.h"
RomanValenciaP 0:a2b95a8cb722 12 #include "Hotboards_keypad.h"
RomanValenciaP 0:a2b95a8cb722 13
RomanValenciaP 0:a2b95a8cb722 14 // Defines the keys array with it's respective number of rows & cols,
RomanValenciaP 0:a2b95a8cb722 15 // and with the value of each key
RomanValenciaP 0:a2b95a8cb722 16 char keys[ 4 ][ 4 ] =
RomanValenciaP 0:a2b95a8cb722 17 {
RomanValenciaP 0:a2b95a8cb722 18 { '1' , '2' , '3' , 'A' },
RomanValenciaP 0:a2b95a8cb722 19 { '4' , '5' , '6' , 'B' },
RomanValenciaP 0:a2b95a8cb722 20 { '7' , '8' , '9' , 'C' },
RomanValenciaP 0:a2b95a8cb722 21 { '*' , '0' , '#' , 'D' }
RomanValenciaP 0:a2b95a8cb722 22 };
RomanValenciaP 0:a2b95a8cb722 23
RomanValenciaP 0:a2b95a8cb722 24 // Defines the pins connected to the rows
RomanValenciaP 0:a2b95a8cb722 25 DigitalInOut rowPins[ 4 ] = { PA_6 , PA_7 , PB_6 , PC_7 };
RomanValenciaP 0:a2b95a8cb722 26 // Defines the pins connected to the cols
RomanValenciaP 0:a2b95a8cb722 27 DigitalInOut colPins[ 4 ] = { PA_8 , PB_10 , PB_4 , PB_5 };
RomanValenciaP 0:a2b95a8cb722 28
RomanValenciaP 0:a2b95a8cb722 29 // Creates a new keyboard with the values entered before
RomanValenciaP 0:a2b95a8cb722 30 Keypad kpd( makeKeymap( keys ) , rowPins , colPins , 4 , 4 );
RomanValenciaP 0:a2b95a8cb722 31
RomanValenciaP 0:a2b95a8cb722 32 // Configures the serial port
RomanValenciaP 0:a2b95a8cb722 33 Serial pc( USBTX , USBRX );
RomanValenciaP 0:a2b95a8cb722 34
RomanValenciaP 0:a2b95a8cb722 35 // For this example we will use the Nucleo LED1 on pin PA_5
RomanValenciaP 0:a2b95a8cb722 36 DigitalOut led1( LED1 );
RomanValenciaP 0:a2b95a8cb722 37 bool blink = false;
RomanValenciaP 0:a2b95a8cb722 38 bool ledPin_state;
RomanValenciaP 0:a2b95a8cb722 39
RomanValenciaP 0:a2b95a8cb722 40
RomanValenciaP 0:a2b95a8cb722 41 // Taking care of some special events.
RomanValenciaP 0:a2b95a8cb722 42 void kpdEvent( KeypadEvent key )
RomanValenciaP 0:a2b95a8cb722 43 {
RomanValenciaP 0:a2b95a8cb722 44 switch( kpd.getState( ) )
RomanValenciaP 0:a2b95a8cb722 45 {
RomanValenciaP 0:a2b95a8cb722 46 case PRESSED:
RomanValenciaP 0:a2b95a8cb722 47 if( key == '#' )
RomanValenciaP 0:a2b95a8cb722 48 {
RomanValenciaP 0:a2b95a8cb722 49 led1 = !led1;
RomanValenciaP 0:a2b95a8cb722 50 ledPin_state = led1; // Remember LED state, lit or unlit.
RomanValenciaP 0:a2b95a8cb722 51 }
RomanValenciaP 0:a2b95a8cb722 52 break;
RomanValenciaP 0:a2b95a8cb722 53 case RELEASED:
RomanValenciaP 0:a2b95a8cb722 54 if( key == '*' )
RomanValenciaP 0:a2b95a8cb722 55 {
RomanValenciaP 0:a2b95a8cb722 56 led1 = ledPin_state;
RomanValenciaP 0:a2b95a8cb722 57 blink = false; // Restore LED state from before it started blinking.
RomanValenciaP 0:a2b95a8cb722 58 }
RomanValenciaP 0:a2b95a8cb722 59 break;
RomanValenciaP 0:a2b95a8cb722 60 case HOLD:
RomanValenciaP 0:a2b95a8cb722 61 if( key == '*' )
RomanValenciaP 0:a2b95a8cb722 62 {
RomanValenciaP 0:a2b95a8cb722 63 blink = true; // Blink the LED when holding the * key.
RomanValenciaP 0:a2b95a8cb722 64 }
RomanValenciaP 0:a2b95a8cb722 65 break;
RomanValenciaP 0:a2b95a8cb722 66 }
RomanValenciaP 0:a2b95a8cb722 67 }
RomanValenciaP 0:a2b95a8cb722 68
RomanValenciaP 0:a2b95a8cb722 69 int main()
RomanValenciaP 0:a2b95a8cb722 70 {
RomanValenciaP 0:a2b95a8cb722 71 led1 = 1; // Turn the LED on.
RomanValenciaP 0:a2b95a8cb722 72 ledPin_state = led1; // Store initial LED state. HIGH when LED is on.
RomanValenciaP 0:a2b95a8cb722 73 kpd.addEventListener( kpdEvent ); // Add an event listener for this keypad
RomanValenciaP 0:a2b95a8cb722 74 while(1)
RomanValenciaP 0:a2b95a8cb722 75 {
RomanValenciaP 0:a2b95a8cb722 76 char key = kpd.getKey( );
RomanValenciaP 0:a2b95a8cb722 77 if( key )
RomanValenciaP 0:a2b95a8cb722 78 {
RomanValenciaP 0:a2b95a8cb722 79 pc.printf( "%c" , key );
RomanValenciaP 0:a2b95a8cb722 80 }
RomanValenciaP 0:a2b95a8cb722 81 if( blink )
RomanValenciaP 0:a2b95a8cb722 82 {
RomanValenciaP 0:a2b95a8cb722 83 led1 = !led1; // Change the ledPin from Hi2Lo or Lo2Hi
RomanValenciaP 0:a2b95a8cb722 84 wait_ms( 100 );
RomanValenciaP 0:a2b95a8cb722 85 }
RomanValenciaP 0:a2b95a8cb722 86 }
RomanValenciaP 0:a2b95a8cb722 87 }