xmpl_key_events.c - Implementing Key Events

Key Event Example

/* $Id: pgXmplKeyEvents.html,v 1.1.1.4 2013/04/09 21:11:37 awachtler Exp $ */
/* Example for key event processing with a single  key */

#include "ioutil.h"
#include "timer.h"
#include "xmpl.h"

#define SHORT_PRESS_TICKS  (MSEC(100))
#define MEDIUM_PRESS_TICKS (MSEC(300))
#define LONG_PRESS_TICKS   (MSEC(800))

#define NONE_PRESS   (0)
#define SHORT_PRESS  (1)
#define MEDIUM_PRESS (2)
#define LONG_PRESS   (3)
#define CONT_PRESS   (4)

volatile uint8_t KeyEvent = 0;

static uint8_t debounce_key0(void)
{
uint8_t ret, tmp;
static uint16_t ocnt=0, ccnt=0;

    ret = NONE_PRESS;
    tmp = (KEY_GET() & 1);
    if (tmp != 0)
    {
        ocnt ++;
        if(ocnt >= LONG_PRESS_TICKS)
        {
            ocnt = LONG_PRESS_TICKS;
            ccnt++;
            if (ccnt >= MEDIUM_PRESS_TICKS)
            {
                ccnt = 0;
                ret = CONT_PRESS;
            }
        }
    }
    else
    {
        if(ocnt >= LONG_PRESS_TICKS)
        {
            ret = LONG_PRESS;
        }
        else if(ocnt >= MEDIUM_PRESS_TICKS)
        {
            ret = MEDIUM_PRESS;
        }
        else if(ocnt >= SHORT_PRESS_TICKS)
        {
            ret = SHORT_PRESS;
        }
        ccnt = 0;
        ocnt = 0;
    }

    return ret;
}

int main(void)
{
    uint8_t tmp;

    KEY_INIT();
    LED_INIT();
    TIMER_INIT();
    sei();
    while(1)
    {
        if (KeyEvent == SHORT_PRESS)
        {
            LED_TOGGLE(0);
        }

        if (KeyEvent == MEDIUM_PRESS)
        {
            LED_TOGGLE(1);
        }

        if (KeyEvent == LONG_PRESS)
        {
            LED_SET_VALUE(0);
        }
        if (KeyEvent == CONT_PRESS)
        {
            tmp = LED_GET_VALUE();
            tmp ++;
            LED_SET_VALUE(tmp);
        }
        KeyEvent = NONE_PRESS;
        SLEEP_ON_IDLE();
    }
}

ISR(TIMER_IRQ_vect)
{
    if (KeyEvent == NONE_PRESS)
    {
        KeyEvent = debounce_key0();
    }
}
/* EOF */


This documentation for µracoli was generated on Tue Apr 9 2013 by  doxygen 1.7.1