fix debouncing

This commit is contained in:
Olaf Rempel 2015-05-02 11:23:10 +02:00
parent f7952dcacb
commit dd00a86a85

60
input.c
View File

@ -24,13 +24,35 @@
/* *********************************************************************** */
#define INPUT_DEBOUNCE_TIMER 60
/* *********************************************************************** */
static uint8_t pcint_old;
static uint8_t quad_state;
static uint8_t quad_state_old;
static uint8_t debounce_timer[2];
static uint8_t input_old_state[2];
/* *********************************************************************** */
static void input_send_event(uint8_t channel, uint8_t state)
{
if ((channel != EVENT_NUM_INPUT_BUTTON) && (channel != EVENT_NUM_INPUT_DOOR))
{
return;
}
if ((debounce_timer[channel -2] == 0) && (state != input_old_state[channel -2]))
{
event_queue(EVENT_TYPE_INPUT, channel, state);
debounce_timer[channel -2] = INPUT_DEBOUNCE_TIMER;
input_old_state[channel -2] = state;
}
} /* input_send_event */
ISR(PCINT1_vect)
{
uint8_t pcint_new = PINC & PCMSK1;
@ -80,28 +102,12 @@ ISR(PCINT1_vect)
/* quadruple decoder switch changed */
else if (pcint & (1<<PINC2))
{
if (debounce_timer[0] == 0)
{
debounce_timer[0] = 60; /* ~60ms */
event_queue(EVENT_TYPE_INPUT,
EVENT_NUM_INPUT_BUTTON,
!!(pcint_new & (1<<PINC2))
);
}
input_send_event(EVENT_NUM_INPUT_BUTTON, !!(pcint_new & (1<<PINC2)));
}
/* door switch changed */
else if (pcint & (1<<PINC3))
{
if (debounce_timer[1] == 0)
{
debounce_timer[1] = 60; /* ~60ms */
event_queue(EVENT_TYPE_INPUT,
EVENT_NUM_INPUT_DOOR,
!!(pcint_new & (1<<PINC3))
);
}
input_send_event(EVENT_NUM_INPUT_DOOR, !!(pcint_new & (1<<PINC3)));
}
/* one revolution completed? */
@ -142,11 +148,19 @@ void input_tick(void)
if (debounce_timer[0] > 0)
{
debounce_timer[0]--;
if (debounce_timer[0] == 0)
{
input_send_event(EVENT_NUM_INPUT_BUTTON, !!(PINC & (1<<PINC2)));
}
}
if (debounce_timer[1] > 0)
{
debounce_timer[1]--;
if (debounce_timer[1] == 0)
{
input_send_event(EVENT_NUM_INPUT_DOOR, !!(PINC & (1<<PINC3)));
}
}
} /* input_tick */
@ -168,14 +182,8 @@ void input_init(void)
quad_state = PINC & ((1<<PINC0) | (1<<PINC1));
/* send button state */
event_queue(EVENT_TYPE_INPUT,
EVENT_NUM_INPUT_BUTTON,
!!(pcint_old & (1<<PINC2))
);
input_send_event(EVENT_NUM_INPUT_BUTTON, !!(PINC & (1<<PINC2)));
/* send door state */
event_queue(EVENT_TYPE_INPUT,
EVENT_NUM_INPUT_DOOR,
!!(pcint_old & (1<<PINC3))
);
input_send_event(EVENT_NUM_INPUT_DOOR, !!(PINC & (1<<PINC3)));
} /* input_init */