Button support and faster clock/fps

This commit is contained in:
whowechina
2025-03-24 22:24:36 +08:00
parent 3045fad715
commit 3d8974dc9a
4 changed files with 121 additions and 3 deletions
+1 -1
View File
@@ -22,7 +22,7 @@
#define ADC_KEY_CHN { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, \
27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16 }
#define BUTTON_DEF { 13, 12, 11, 10 }
#else
#endif
+84
View File
@@ -0,0 +1,84 @@
/*
* Controller Buttons
* WHowe <github.com/whowechina>
*
*/
#include "button.h"
#include <stdint.h>
#include <stdbool.h>
#include "hardware/gpio.h"
#include "hardware/timer.h"
#include "hardware/pwm.h"
#include "config.h"
#include "board_defs.h"
static const uint8_t button_gpio[] = BUTTON_DEF;
#define BUTTON_NUM (sizeof(button_gpio))
static bool sw_val[BUTTON_NUM]; /* true if pressed */
static uint64_t sw_freeze_time[BUTTON_NUM];
static uint16_t button_reading;
void button_init()
{
for (int i = 0; i < BUTTON_NUM; i++) {
sw_val[i] = false;
sw_freeze_time[i] = 0;
int8_t gpio = button_gpio[i];
gpio_init(gpio);
gpio_set_function(gpio, GPIO_FUNC_SIO);
gpio_set_dir(gpio, GPIO_IN);
gpio_pull_up(gpio);
}
/* make valid initial reading */
button_reading = 0;
for (int i = 0; i < BUTTON_NUM; i++) {
sw_val[i] = !gpio_get(button_gpio[i]);
if (sw_val[i]) {
button_reading |= (1 << i);
}
}
}
uint8_t button_num()
{
return BUTTON_NUM;
}
/* If a switch flips, it freezes for a while */
#define DEBOUNCE_FREEZE_TIME_US 3000
void button_update()
{
uint64_t now = time_us_64();
uint16_t buttons = 0;
for (int i = BUTTON_NUM - 1; i >= 0; i--) {
bool sw_pressed = !gpio_get(button_gpio[i]);
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_val[i]) {
buttons |= 1;
}
}
button_reading = buttons;
}
uint16_t button_read()
{
return button_reading;
}
+18
View File
@@ -0,0 +1,18 @@
/*
* Controller Buttons
* WHowe <github.com/whowechina>
*/
#ifndef BUTTON_H
#define BUTTON_H
#include <stdint.h>
#include <stdbool.h>
#include "hardware/flash.h"
void button_init();
uint8_t button_num();
void button_update();
uint16_t button_read();
#endif
+18 -2
View File
@@ -13,6 +13,7 @@
#include "pico/multicore.h"
#include "pico/bootrom.h"
#include "hardware/clocks.h"
#include "hardware/gpio.h"
#include "hardware/sync.h"
#include "hardware/structs/ioqspi.h"
@@ -140,7 +141,6 @@ static void core0_loop()
cli_run();
savedata_loop();
hammer_update();
proc_midi();
@@ -148,13 +148,27 @@ static void core0_loop()
cli_fps_count(0);
sleep_until(next_frame);
next_frame += 1000;
next_frame += 300;
}
}
/* if certain key pressed when booting, enter update mode */
static void update_check()
{
const uint8_t pins[] = BUTTON_DEF;
for (int i = 0; i < count_of(pins); i++) {
uint8_t gpio = pins[i];
gpio_init(gpio);
gpio_set_function(gpio, GPIO_FUNC_SIO);
gpio_set_dir(gpio, GPIO_IN);
gpio_pull_up(gpio);
}
if (!gpio_get(pins[0]) && !gpio_get(pins[1])) {
sleep_ms(100);
reset_usb_boot(0, 2);
return;
}
}
void init()
@@ -164,6 +178,8 @@ void init()
update_check();
set_sys_clock_khz(166000, true);
tusb_init();
stdio_init_all();