1
0
mirror of https://github.com/whowechina/popn_pico.git synced 2024-11-12 01:30:47 +01:00

Firmware debounce working.

This commit is contained in:
whowechina 2022-09-04 21:23:11 +08:00
parent 9809983d6b
commit 1a7b441f18
3 changed files with 29 additions and 33 deletions

View File

@ -1,11 +1,9 @@
add_executable(pico_popn main.c buttons.c usb_descriptors.c)
add_compile_options(-Wfatal-errors -O3)
include_directories(${CMAKE_CURRENT_LIST_DIR})
add_executable(${PROJECT_NAME} main.c buttons.c usb_descriptors.c)
target_link_libraries(pico_popn PRIVATE
pico_multicore
pico_stdlib
tinyusb_device
tinyusb_board)
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Werror -Wfatal-errors -O3)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries(${PROJECT_NAME} PRIVATE
pico_multicore pico_stdlib tinyusb_device tinyusb_board)
pico_add_extra_outputs(pico_popn)
# Make a UF2 binary
pico_add_extra_outputs(${PROJECT_NAME})

View File

@ -16,7 +16,7 @@
static const struct button {
int8_t sw_gpio;
int8_t led_gpio;
int8_t led_gpio; /* led related to the button */
} BUTTON_DEFS[] = {
{0, 1},
{2, 3},
@ -33,15 +33,15 @@ static const struct button {
#define BUTTON_NUM (ARRAY_SIZE(BUTTON_DEFS))
static bool sw_on[BUTTON_NUM];
static uint64_t sw_time[BUTTON_NUM];
static bool sw_val[BUTTON_NUM]; /* true for pressed */
static uint64_t sw_freeze_time[BUTTON_NUM];
void button_init()
{
for (int i = 0; i < BUTTON_NUM; i++)
{
sw_on[i] = false;
sw_time[i] = 0;
sw_val[i] = false;
sw_freeze_time[i] = 0;
gpio_init(BUTTON_DEFS[i].sw_gpio);
gpio_set_function(BUTTON_DEFS[i].sw_gpio, GPIO_FUNC_SIO);
gpio_set_dir(BUTTON_DEFS[i].sw_gpio, GPIO_IN);
@ -60,29 +60,29 @@ uint8_t button_num()
return BUTTON_NUM;
}
static void button_update()
{
for (int i = 0; i < BUTTON_NUM; i++)
{
bool sw_pressed = !gpio_get(BUTTON_DEFS[i].sw_gpio);
if (!sw_on[i] && sw_pressed) {
sw_time[i] = time_us_64();
}
sw_on[i] = sw_pressed;
}
}
/* If a switch flips, it freezes for a while */
#define DEBOUNCE_FREEZE_TIME_US 1000
uint16_t button_read()
{
button_update();
uint64_t now = time_us_64();
uint16_t buttons = 0;
for (int i = 0; i < BUTTON_NUM; i++) {
for (int i = BUTTON_NUM - 1; i >= 0; i--) {
bool sw_pressed = !gpio_get(BUTTON_DEFS[i].sw_gpio);
if (now >= sw_freeze_time[i]) {
if (sw_pressed != sw_val[i]) {
sw_val[i] = sw_pressed;
sw_freeze_time[i] = now + DEBOUNCE_FREEZE_TIME_US;
}
}
buttons <<= 1;
if (sw_on[i]) {
if (sw_val[i]) {
buttons |= 1;
}
}
return buttons;
}
@ -90,7 +90,7 @@ void button_auto_light()
{
for (int i = 0; i < BUTTON_NUM; i++) {
if (BUTTON_DEFS[i].led_gpio >= 0) {
gpio_put(BUTTON_DEFS[i].led_gpio, sw_on[i]);
gpio_put(BUTTON_DEFS[i].led_gpio, sw_val[i]);
}
}
}

View File

@ -7,8 +7,6 @@
#include <stdbool.h>
#include "bsp/board.h"
#include "hardware/clocks.h"
#include "pico/stdlib.h"
#include "tusb.h"
#include "usb_descriptors.h"