samples: microbit/pong: Filter out spurious button presses

The buttons on the microbit cause quite easily "ghost" key presses.
Add a simple filter that ignores button presses from the same button
that happen less than 100ms from each other.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
This commit is contained in:
Johan Hedberg 2017-05-29 11:54:04 +03:00 committed by Johan Hedberg
parent 0b16e8ada5
commit 190b0d1a10
1 changed files with 15 additions and 6 deletions

View File

@ -376,6 +376,21 @@ void pong_ball_received(s8_t x_pos, s8_t y_pos, s8_t x_vel, s8_t y_vel)
static void button_pressed(struct device *dev, struct gpio_callback *cb,
u32_t pins)
{
/* Filter out spurious presses */
if (pins & BIT(SW0_GPIO_PIN)) {
printk("A pressed\n");
if (k_uptime_delta(&a_timestamp) < K_MSEC(100)) {
printk("Too quick A presses\n");
return;
}
} else {
printk("B pressed\n");
if (k_uptime_delta(&b_timestamp) < K_MSEC(100)) {
printk("Too quick B presses\n");
return;
}
}
if (ended && (k_uptime_get() - ended) > RESTART_THRESHOLD) {
k_delayed_work_cancel(&refresh);
game_init(state == SINGLE || remote_lost);
@ -391,15 +406,12 @@ static void button_pressed(struct device *dev, struct gpio_callback *cb,
}
if (pins & BIT(SW0_GPIO_PIN)) {
printk("A pressed\n");
if (select) {
pong_select_change();
return;
}
if (!started) {
a_timestamp = k_uptime_get();
check_start();
}
@ -412,15 +424,12 @@ static void button_pressed(struct device *dev, struct gpio_callback *cb,
k_sem_give(&disp_update);
}
} else {
printk("B pressed\n");
if (select) {
pong_select_complete();
return;
}
if (!started) {
b_timestamp = k_uptime_get();
check_start();
}