#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;
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;
TIMER_INIT();
while(1)
{
if (KeyEvent == SHORT_PRESS)
{
}
if (KeyEvent == MEDIUM_PRESS)
{
}
if (KeyEvent == LONG_PRESS)
{
}
if (KeyEvent == CONT_PRESS)
{
tmp ++;
}
KeyEvent = NONE_PRESS;
}
}
ISR(TIMER_IRQ_vect)
{
if (KeyEvent == NONE_PRESS)
{
KeyEvent = debounce_key0();
}
}