Keypad

Updated on 2-Jan-2014

Library for keypad

Please refer this http:mbed.org/users/yoonghm/code/keypad/docs/tip/ for more information on the library.

Features

  • Support various keypad size, such as 4x4, 4x3, 3x4. Unused pin in the constructor could be disabled using NC.
  • Compatible with RTOS library.

How it works

After each key press, a user defined callback function could be called. Inside the callback function, any data processing with input key index can be done. User can write additional code within the callback to block subsequent interrupt.

/media/uploads/yoonghm/keypad_example.jpg

Sample Code

#include "mbed.h"
#include "rtos.h"

#include "Keypad.h"

Serial        PC(USBTX, USBRX);
RtosTimer    *LedTimer;
DigitalOut    Led1(LED1);
DigitalOut    Led2(LED2);

// Define your own keypad values
char Keytable[] = { 
    '1', '2',  // r0
    '3', '4'   // r1
  // c0   c1
 };

int32_t       Index = -1;
int           State;

uint32_t cbAfterInput(uint32_t _index)
{
    Index = _index;
    return 0;
}

void Blink(void const *arg) {
    if (State == 1)  {
        Led1 = 1;
        Led2 = 0;
        State = 2;
    }
    else if (State == 2)  {
        LedTimer->stop();
        Led1 = 0;
        Led2 = 1;
        LedTimer->start(2000); // longer timer alarm
        State = 1;
    }
}

int main()
{
    PC.printf("I am Demo Keypad\r\n");
    State = 1;
    LedTimer = new RtosTimer(&Blink, osTimerPeriodic, NULL);
    LedTimer->start(500); // short timer alarm    
    
    //             r0   r1   r2   r3   c0   c1   c2   c3
    Keypad keypad(p21, p22,  NC,  NC, p23, p24,  NC,  NC);
    keypad.attach(&cbAfterInput);
    keypad.start();  // energize the columns c0-c3 of the keypad

    while (1) {
        __wfi();
        if (Index > -1) {
            PC.printf("Interrupted");
            PC.printf("Index:%d => Key:%c\r\n", Index, Keytable[Index]);
            Index = -1;
        }
    }
}