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@2:252f5ac72f2d, 2016-05-30 (annotated)
- Committer:
- icserny
- Date:
- Mon May 30 10:08:56 2016 +0000
- Revision:
- 2:252f5ac72f2d
- Parent:
- 1:88d227ecb9b3
First version;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Hotboards | 1:88d227ecb9b3 | 1 | /* @file Hotboards_keypad.cpp |
icserny | 2:252f5ac72f2d | 2 | || @version 1.2 |
icserny | 2:252f5ac72f2d | 3 | || @modified by Istvan Cserny |
icserny | 2:252f5ac72f2d | 4 | || @contact https://developer.mbed.org/users/icserny/ |
Hotboards | 1:88d227ecb9b3 | 5 | || @version 1.1 |
Hotboards | 1:88d227ecb9b3 | 6 | || @modified by Diego (http://hotboards.org) |
Hotboards | 1:88d227ecb9b3 | 7 | || @author Alexander Brevig |
Hotboards | 1:88d227ecb9b3 | 8 | || @contact alexanderbrevig@gmail.com |
Hotboards | 1:88d227ecb9b3 | 9 | || |
Hotboards | 1:88d227ecb9b3 | 10 | || @description |
Hotboards | 1:88d227ecb9b3 | 11 | || | Demonstrates using the KeypadEvent. |
icserny | 2:252f5ac72f2d | 12 | || |
icserny | 2:252f5ac72f2d | 13 | || Hardware requirements: |
icserny | 2:252f5ac72f2d | 14 | || - FRDM-KL25Z board |
icserny | 2:252f5ac72f2d | 15 | || - 4x4 keypad connected to PTB8,9,10,11 and PTE2,3,4,5 |
Hotboards | 1:88d227ecb9b3 | 16 | */ |
RomanValenciaP | 0:a2b95a8cb722 | 17 | #include "mbed.h" |
RomanValenciaP | 0:a2b95a8cb722 | 18 | #include "Hotboards_keypad.h" |
RomanValenciaP | 0:a2b95a8cb722 | 19 | |
RomanValenciaP | 0:a2b95a8cb722 | 20 | // Defines the keys array with it's respective number of rows & cols, |
RomanValenciaP | 0:a2b95a8cb722 | 21 | // and with the value of each key |
RomanValenciaP | 0:a2b95a8cb722 | 22 | char keys[ 4 ][ 4 ] = |
RomanValenciaP | 0:a2b95a8cb722 | 23 | { |
RomanValenciaP | 0:a2b95a8cb722 | 24 | { '1' , '2' , '3' , 'A' }, |
RomanValenciaP | 0:a2b95a8cb722 | 25 | { '4' , '5' , '6' , 'B' }, |
RomanValenciaP | 0:a2b95a8cb722 | 26 | { '7' , '8' , '9' , 'C' }, |
RomanValenciaP | 0:a2b95a8cb722 | 27 | { '*' , '0' , '#' , 'D' } |
RomanValenciaP | 0:a2b95a8cb722 | 28 | }; |
RomanValenciaP | 0:a2b95a8cb722 | 29 | |
RomanValenciaP | 0:a2b95a8cb722 | 30 | // Defines the pins connected to the rows |
icserny | 2:252f5ac72f2d | 31 | DigitalInOut rowPins[ 4 ] = { PTB8 , PTB9 , PTB10 , PTB11 }; |
RomanValenciaP | 0:a2b95a8cb722 | 32 | // Defines the pins connected to the cols |
icserny | 2:252f5ac72f2d | 33 | DigitalInOut colPins[ 4 ] = { PTE2 , PTE3 , PTE4 , PTE5 }; |
RomanValenciaP | 0:a2b95a8cb722 | 34 | |
RomanValenciaP | 0:a2b95a8cb722 | 35 | // Creates a new keyboard with the values entered before |
RomanValenciaP | 0:a2b95a8cb722 | 36 | Keypad kpd( makeKeymap( keys ) , rowPins , colPins , 4 , 4 ); |
RomanValenciaP | 0:a2b95a8cb722 | 37 | |
RomanValenciaP | 0:a2b95a8cb722 | 38 | // Configures the serial port |
RomanValenciaP | 0:a2b95a8cb722 | 39 | Serial pc( USBTX , USBRX ); |
RomanValenciaP | 0:a2b95a8cb722 | 40 | |
RomanValenciaP | 0:a2b95a8cb722 | 41 | // For this example we will use the Nucleo LED1 on pin PA_5 |
RomanValenciaP | 0:a2b95a8cb722 | 42 | DigitalOut led1( LED1 ); |
RomanValenciaP | 0:a2b95a8cb722 | 43 | bool blink = false; |
RomanValenciaP | 0:a2b95a8cb722 | 44 | bool ledPin_state; |
RomanValenciaP | 0:a2b95a8cb722 | 45 | |
RomanValenciaP | 0:a2b95a8cb722 | 46 | |
RomanValenciaP | 0:a2b95a8cb722 | 47 | // Taking care of some special events. |
RomanValenciaP | 0:a2b95a8cb722 | 48 | void kpdEvent( KeypadEvent key ) |
RomanValenciaP | 0:a2b95a8cb722 | 49 | { |
RomanValenciaP | 0:a2b95a8cb722 | 50 | switch( kpd.getState( ) ) |
RomanValenciaP | 0:a2b95a8cb722 | 51 | { |
RomanValenciaP | 0:a2b95a8cb722 | 52 | case PRESSED: |
RomanValenciaP | 0:a2b95a8cb722 | 53 | if( key == '#' ) |
RomanValenciaP | 0:a2b95a8cb722 | 54 | { |
RomanValenciaP | 0:a2b95a8cb722 | 55 | led1 = !led1; |
RomanValenciaP | 0:a2b95a8cb722 | 56 | ledPin_state = led1; // Remember LED state, lit or unlit. |
RomanValenciaP | 0:a2b95a8cb722 | 57 | } |
RomanValenciaP | 0:a2b95a8cb722 | 58 | break; |
RomanValenciaP | 0:a2b95a8cb722 | 59 | case RELEASED: |
RomanValenciaP | 0:a2b95a8cb722 | 60 | if( key == '*' ) |
RomanValenciaP | 0:a2b95a8cb722 | 61 | { |
RomanValenciaP | 0:a2b95a8cb722 | 62 | led1 = ledPin_state; |
RomanValenciaP | 0:a2b95a8cb722 | 63 | blink = false; // Restore LED state from before it started blinking. |
RomanValenciaP | 0:a2b95a8cb722 | 64 | } |
RomanValenciaP | 0:a2b95a8cb722 | 65 | break; |
RomanValenciaP | 0:a2b95a8cb722 | 66 | case HOLD: |
RomanValenciaP | 0:a2b95a8cb722 | 67 | if( key == '*' ) |
RomanValenciaP | 0:a2b95a8cb722 | 68 | { |
RomanValenciaP | 0:a2b95a8cb722 | 69 | blink = true; // Blink the LED when holding the * key. |
RomanValenciaP | 0:a2b95a8cb722 | 70 | } |
RomanValenciaP | 0:a2b95a8cb722 | 71 | break; |
RomanValenciaP | 0:a2b95a8cb722 | 72 | } |
RomanValenciaP | 0:a2b95a8cb722 | 73 | } |
RomanValenciaP | 0:a2b95a8cb722 | 74 | |
RomanValenciaP | 0:a2b95a8cb722 | 75 | int main() |
RomanValenciaP | 0:a2b95a8cb722 | 76 | { |
RomanValenciaP | 0:a2b95a8cb722 | 77 | led1 = 1; // Turn the LED on. |
RomanValenciaP | 0:a2b95a8cb722 | 78 | ledPin_state = led1; // Store initial LED state. HIGH when LED is on. |
RomanValenciaP | 0:a2b95a8cb722 | 79 | kpd.addEventListener( kpdEvent ); // Add an event listener for this keypad |
RomanValenciaP | 0:a2b95a8cb722 | 80 | while(1) |
RomanValenciaP | 0:a2b95a8cb722 | 81 | { |
RomanValenciaP | 0:a2b95a8cb722 | 82 | char key = kpd.getKey( ); |
RomanValenciaP | 0:a2b95a8cb722 | 83 | if( key ) |
RomanValenciaP | 0:a2b95a8cb722 | 84 | { |
RomanValenciaP | 0:a2b95a8cb722 | 85 | pc.printf( "%c" , key ); |
RomanValenciaP | 0:a2b95a8cb722 | 86 | } |
RomanValenciaP | 0:a2b95a8cb722 | 87 | if( blink ) |
RomanValenciaP | 0:a2b95a8cb722 | 88 | { |
RomanValenciaP | 0:a2b95a8cb722 | 89 | led1 = !led1; // Change the ledPin from Hi2Lo or Lo2Hi |
RomanValenciaP | 0:a2b95a8cb722 | 90 | wait_ms( 100 ); |
RomanValenciaP | 0:a2b95a8cb722 | 91 | } |
RomanValenciaP | 0:a2b95a8cb722 | 92 | } |
RomanValenciaP | 0:a2b95a8cb722 | 93 | } |