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
main.cpp@0:a2b95a8cb722, 2016-03-08 (annotated)
- Committer:
- RomanValenciaP
- Date:
- Tue Mar 08 20:46:03 2016 +0000
- Revision:
- 0:a2b95a8cb722
- Child:
- 1:88d227ecb9b3
first release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RomanValenciaP | 0:a2b95a8cb722 | 1 | |
RomanValenciaP | 0:a2b95a8cb722 | 2 | #include "mbed.h" |
RomanValenciaP | 0:a2b95a8cb722 | 3 | #include "Hotboards_keypad.h" |
RomanValenciaP | 0:a2b95a8cb722 | 4 | |
RomanValenciaP | 0:a2b95a8cb722 | 5 | // Defines the keys array with it's respective number of rows & cols, |
RomanValenciaP | 0:a2b95a8cb722 | 6 | // and with the value of each key |
RomanValenciaP | 0:a2b95a8cb722 | 7 | char keys[ 4 ][ 4 ] = |
RomanValenciaP | 0:a2b95a8cb722 | 8 | { |
RomanValenciaP | 0:a2b95a8cb722 | 9 | { '1' , '2' , '3' , 'A' }, |
RomanValenciaP | 0:a2b95a8cb722 | 10 | { '4' , '5' , '6' , 'B' }, |
RomanValenciaP | 0:a2b95a8cb722 | 11 | { '7' , '8' , '9' , 'C' }, |
RomanValenciaP | 0:a2b95a8cb722 | 12 | { '*' , '0' , '#' , 'D' } |
RomanValenciaP | 0:a2b95a8cb722 | 13 | }; |
RomanValenciaP | 0:a2b95a8cb722 | 14 | |
RomanValenciaP | 0:a2b95a8cb722 | 15 | // Defines the pins connected to the rows |
RomanValenciaP | 0:a2b95a8cb722 | 16 | DigitalInOut rowPins[ 4 ] = { PA_6 , PA_7 , PB_6 , PC_7 }; |
RomanValenciaP | 0:a2b95a8cb722 | 17 | // Defines the pins connected to the cols |
RomanValenciaP | 0:a2b95a8cb722 | 18 | DigitalInOut colPins[ 4 ] = { PA_8 , PB_10 , PB_4 , PB_5 }; |
RomanValenciaP | 0:a2b95a8cb722 | 19 | |
RomanValenciaP | 0:a2b95a8cb722 | 20 | // Creates a new keyboard with the values entered before |
RomanValenciaP | 0:a2b95a8cb722 | 21 | Keypad kpd( makeKeymap( keys ) , rowPins , colPins , 4 , 4 ); |
RomanValenciaP | 0:a2b95a8cb722 | 22 | |
RomanValenciaP | 0:a2b95a8cb722 | 23 | // Configures the serial port |
RomanValenciaP | 0:a2b95a8cb722 | 24 | Serial pc( USBTX , USBRX ); |
RomanValenciaP | 0:a2b95a8cb722 | 25 | |
RomanValenciaP | 0:a2b95a8cb722 | 26 | // For this example we will use the Nucleo LED1 on pin PA_5 |
RomanValenciaP | 0:a2b95a8cb722 | 27 | DigitalOut led1( LED1 ); |
RomanValenciaP | 0:a2b95a8cb722 | 28 | bool blink = false; |
RomanValenciaP | 0:a2b95a8cb722 | 29 | bool ledPin_state; |
RomanValenciaP | 0:a2b95a8cb722 | 30 | |
RomanValenciaP | 0:a2b95a8cb722 | 31 | |
RomanValenciaP | 0:a2b95a8cb722 | 32 | // Taking care of some special events. |
RomanValenciaP | 0:a2b95a8cb722 | 33 | void kpdEvent( KeypadEvent key ) |
RomanValenciaP | 0:a2b95a8cb722 | 34 | { |
RomanValenciaP | 0:a2b95a8cb722 | 35 | switch( kpd.getState( ) ) |
RomanValenciaP | 0:a2b95a8cb722 | 36 | { |
RomanValenciaP | 0:a2b95a8cb722 | 37 | case PRESSED: |
RomanValenciaP | 0:a2b95a8cb722 | 38 | if( key == '#' ) |
RomanValenciaP | 0:a2b95a8cb722 | 39 | { |
RomanValenciaP | 0:a2b95a8cb722 | 40 | led1 = !led1; |
RomanValenciaP | 0:a2b95a8cb722 | 41 | ledPin_state = led1; // Remember LED state, lit or unlit. |
RomanValenciaP | 0:a2b95a8cb722 | 42 | } |
RomanValenciaP | 0:a2b95a8cb722 | 43 | break; |
RomanValenciaP | 0:a2b95a8cb722 | 44 | case RELEASED: |
RomanValenciaP | 0:a2b95a8cb722 | 45 | if( key == '*' ) |
RomanValenciaP | 0:a2b95a8cb722 | 46 | { |
RomanValenciaP | 0:a2b95a8cb722 | 47 | led1 = ledPin_state; |
RomanValenciaP | 0:a2b95a8cb722 | 48 | blink = false; // Restore LED state from before it started blinking. |
RomanValenciaP | 0:a2b95a8cb722 | 49 | } |
RomanValenciaP | 0:a2b95a8cb722 | 50 | break; |
RomanValenciaP | 0:a2b95a8cb722 | 51 | case HOLD: |
RomanValenciaP | 0:a2b95a8cb722 | 52 | if( key == '*' ) |
RomanValenciaP | 0:a2b95a8cb722 | 53 | { |
RomanValenciaP | 0:a2b95a8cb722 | 54 | blink = true; // Blink the LED when holding the * key. |
RomanValenciaP | 0:a2b95a8cb722 | 55 | } |
RomanValenciaP | 0:a2b95a8cb722 | 56 | break; |
RomanValenciaP | 0:a2b95a8cb722 | 57 | } |
RomanValenciaP | 0:a2b95a8cb722 | 58 | } |
RomanValenciaP | 0:a2b95a8cb722 | 59 | |
RomanValenciaP | 0:a2b95a8cb722 | 60 | int main() |
RomanValenciaP | 0:a2b95a8cb722 | 61 | { |
RomanValenciaP | 0:a2b95a8cb722 | 62 | led1 = 1; // Turn the LED on. |
RomanValenciaP | 0:a2b95a8cb722 | 63 | ledPin_state = led1; // Store initial LED state. HIGH when LED is on. |
RomanValenciaP | 0:a2b95a8cb722 | 64 | kpd.addEventListener( kpdEvent ); // Add an event listener for this keypad |
RomanValenciaP | 0:a2b95a8cb722 | 65 | while(1) |
RomanValenciaP | 0:a2b95a8cb722 | 66 | { |
RomanValenciaP | 0:a2b95a8cb722 | 67 | char key = kpd.getKey( ); |
RomanValenciaP | 0:a2b95a8cb722 | 68 | if( key ) |
RomanValenciaP | 0:a2b95a8cb722 | 69 | { |
RomanValenciaP | 0:a2b95a8cb722 | 70 | pc.printf( "%c" , key ); |
RomanValenciaP | 0:a2b95a8cb722 | 71 | } |
RomanValenciaP | 0:a2b95a8cb722 | 72 | if( blink ) |
RomanValenciaP | 0:a2b95a8cb722 | 73 | { |
RomanValenciaP | 0:a2b95a8cb722 | 74 | led1 = !led1; // Change the ledPin from Hi2Lo or Lo2Hi |
RomanValenciaP | 0:a2b95a8cb722 | 75 | wait_ms( 100 ); |
RomanValenciaP | 0:a2b95a8cb722 | 76 | } |
RomanValenciaP | 0:a2b95a8cb722 | 77 | } |
RomanValenciaP | 0:a2b95a8cb722 | 78 | } |