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

main.cpp

Committer:
icserny
Date:
2016-05-30
Revision:
2:252f5ac72f2d
Parent:
1:88d227ecb9b3

File content as of revision 2:252f5ac72f2d:

/* @file Hotboards_keypad.cpp
 || @version 1.2
 || @modified by Istvan Cserny
 || @contact https://developer.mbed.org/users/icserny/
 || @version 1.1
 || @modified by Diego (http://hotboards.org)
 || @author Alexander Brevig
 || @contact alexanderbrevig@gmail.com
 ||
 || @description
 || | Demonstrates using the KeypadEvent.
 ||
 ||  Hardware requirements:
 ||   - FRDM-KL25Z board
 ||   - 4x4 keypad connected to PTB8,9,10,11 and PTE2,3,4,5
 */
#include "mbed.h"
#include "Hotboards_keypad.h"

// Defines the keys array with it's respective number of rows & cols,
// and with the value of each key
char keys[ 4 ][ 4 ] =
{
    { '1' , '2' , '3' , 'A' },
    { '4' , '5' , '6' , 'B' },
    { '7' , '8' , '9' , 'C' },
    { '*' , '0' , '#' , 'D' }
};

// Defines the pins connected to the rows
DigitalInOut rowPins[ 4 ] = { PTB8 , PTB9 , PTB10 , PTB11 };
// Defines the pins connected to the cols
DigitalInOut colPins[ 4 ] = { PTE2 , PTE3 , PTE4 , PTE5 };

// Creates a new keyboard with the values entered before
Keypad kpd( makeKeymap( keys ) , rowPins , colPins , 4 , 4 );

// Configures the serial port
Serial pc( USBTX , USBRX );

// For this example we will use the Nucleo LED1 on pin PA_5
DigitalOut led1( LED1 );
bool blink = false;
bool ledPin_state;


// Taking care of some special events.
void kpdEvent( KeypadEvent key )
{
    switch( kpd.getState( ) )
    {
        case PRESSED:
            if( key == '#' )
            {
                led1 = !led1;
                ledPin_state = led1; // Remember LED state, lit or unlit.
            }
            break;
        case RELEASED:
            if( key == '*' )
            {
                led1 = ledPin_state;
                blink = false; // Restore LED state from before it started blinking.
            }
            break;
        case HOLD:
            if( key == '*' )
            {
                blink = true; // Blink the LED when holding the * key.
            }
            break;
    }
}

int main()
{
    led1 = 1; // Turn the LED on.
    ledPin_state = led1; // Store initial LED state. HIGH when LED is on.
    kpd.addEventListener( kpdEvent ); // Add an event listener for this keypad
    while(1)
    {
        char key = kpd.getKey( );
        if( key )
        {
            pc.printf( "%c" , key );
        }
        if( blink )
        {
            led1 = !led1; // Change the ledPin from Hi2Lo or Lo2Hi
            wait_ms( 100 );
        }
    }
}