Key Event Example
#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();
}
}