mirror of
https://github.com/speedypotato/Pico-Game-Controller.git
synced 2026-10-01 03:07:55 +03:00
feat: make debounce algorithms symmetric
This commit is contained in:
@@ -2,11 +2,18 @@
|
||||
* Simple header file to include all files in the folder
|
||||
* @author SpeedyPotato
|
||||
*
|
||||
* To add a debounce mode, return a uint16_t representing the button states.
|
||||
* A debounce mode function modifies sw_cooked_val to update button states.
|
||||
* These are saved in report.buttons as truth. Create debounce mode as desired
|
||||
* and then add the #include here.
|
||||
*
|
||||
* At the start of the debounce function, sw_cooked_val is the state of the
|
||||
* buttons from the previous cycle. You should change it to be the new state
|
||||
* by the end of the function. sw_prev_raw_val contains the state of the GPIO
|
||||
* pins on the previous cycle. sw_timestamp is for you to use.
|
||||
**/
|
||||
extern uint64_t sw_timestamp[SW_GPIO_SIZE];
|
||||
extern bool sw_prev_raw_val[SW_GPIO_SIZE];
|
||||
extern bool sw_cooked_val[SW_GPIO_SIZE];
|
||||
extern uint64_t sw_timestamp[SW_GPIO_SIZE];
|
||||
|
||||
#include "deferred.c"
|
||||
#include "eager.c"
|
||||
#include "eager.c"
|
||||
|
||||
@@ -4,15 +4,16 @@
|
||||
* @author SpeedyPotato
|
||||
**/
|
||||
|
||||
uint16_t debounce_deferred() {
|
||||
uint16_t translate_buttons = 0;
|
||||
void debounce_deferred() {
|
||||
for (int i = SW_GPIO_SIZE - 1; i >= 0; i--) {
|
||||
if (!gpio_get(SW_GPIO[i]) &&
|
||||
bool sw_raw_val = !gpio_get(SW_GPIO[i]);
|
||||
|
||||
if (sw_raw_val != sw_prev_raw_val[i]) {
|
||||
sw_timestamp[i] = time_us_64();
|
||||
} else if (sw_timestamp[i] != 0 &&
|
||||
time_us_64() - sw_timestamp[i] >= SW_DEBOUNCE_TIME_US) {
|
||||
translate_buttons = (translate_buttons << 1) | 1;
|
||||
} else {
|
||||
translate_buttons <<= 1;
|
||||
sw_cooked_val[i] = sw_raw_val;
|
||||
sw_timestamp[i] = 0;
|
||||
}
|
||||
}
|
||||
return translate_buttons;
|
||||
}
|
||||
}
|
||||
|
||||
+9
-10
@@ -4,15 +4,14 @@
|
||||
* @author SpeedyPotato
|
||||
**/
|
||||
|
||||
uint16_t debounce_eager() {
|
||||
uint16_t translate_buttons = 0;
|
||||
for (int i = SW_GPIO_SIZE - 1; i >= 0; i--) {
|
||||
if (time_us_64() - sw_timestamp[i] <= SW_DEBOUNCE_TIME_US ||
|
||||
!gpio_get(SW_GPIO[i])) {
|
||||
translate_buttons = (translate_buttons << 1) | 1;
|
||||
} else {
|
||||
translate_buttons <<= 1;
|
||||
void debounce_eager() {
|
||||
for (int i = 0; i < SW_GPIO_SIZE; i++) {
|
||||
bool sw_raw_val = !gpio_get(SW_GPIO[i]);
|
||||
|
||||
if (time_us_64() - sw_timestamp[i] >= SW_DEBOUNCE_TIME_US &&
|
||||
sw_cooked_val[i] != sw_raw_val) {
|
||||
sw_cooked_val[i] = sw_raw_val;
|
||||
sw_timestamp[i] = time_us_64();
|
||||
}
|
||||
}
|
||||
return translate_buttons;
|
||||
}
|
||||
}
|
||||
|
||||
+13
-11
@@ -29,7 +29,8 @@ uint32_t enc_val[ENC_GPIO_SIZE];
|
||||
uint32_t prev_enc_val[ENC_GPIO_SIZE];
|
||||
int cur_enc_val[ENC_GPIO_SIZE];
|
||||
|
||||
bool prev_sw_val[SW_GPIO_SIZE];
|
||||
bool sw_prev_raw_val[SW_GPIO_SIZE];
|
||||
bool sw_cooked_val[SW_GPIO_SIZE];
|
||||
uint64_t sw_timestamp[SW_GPIO_SIZE];
|
||||
|
||||
bool kbm_report;
|
||||
@@ -38,7 +39,7 @@ uint64_t reactive_timeout_timestamp;
|
||||
|
||||
void (*ws2812b_mode)();
|
||||
void (*loop_mode)();
|
||||
uint16_t (*debounce_mode)();
|
||||
void (*debounce_mode)();
|
||||
bool joy_mode_check = true;
|
||||
|
||||
union {
|
||||
@@ -156,16 +157,16 @@ void key_mode() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Update Input States
|
||||
* Updates input states and stores true state into report.buttons.
|
||||
* Note: Switches are pull up, negate value
|
||||
**/
|
||||
void update_inputs() {
|
||||
for (int i = 0; i < SW_GPIO_SIZE; i++) {
|
||||
// If switch gets pressed, record timestamp
|
||||
if (prev_sw_val[i] == false && !gpio_get(SW_GPIO[i]) == true) {
|
||||
sw_timestamp[i] = time_us_64();
|
||||
}
|
||||
prev_sw_val[i] = !gpio_get(SW_GPIO[i]);
|
||||
report.buttons = 0;
|
||||
for (int i = SW_GPIO_SIZE - 1; i >= 0; i--) {
|
||||
sw_prev_raw_val[i] = !gpio_get(SW_GPIO[i]);
|
||||
|
||||
report.buttons <<= 1;
|
||||
report.buttons |= sw_cooked_val[i];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -241,7 +242,8 @@ void init() {
|
||||
|
||||
// Setup Button GPIO
|
||||
for (int i = 0; i < SW_GPIO_SIZE; i++) {
|
||||
prev_sw_val[i] = false;
|
||||
sw_prev_raw_val[i] = false;
|
||||
sw_cooked_val[i] = false;
|
||||
sw_timestamp[i] = 0;
|
||||
gpio_init(SW_GPIO[i]);
|
||||
gpio_set_function(SW_GPIO[i], GPIO_FUNC_SIO);
|
||||
@@ -293,8 +295,8 @@ int main(void) {
|
||||
|
||||
while (1) {
|
||||
tud_task(); // tinyusb device task
|
||||
debounce_mode();
|
||||
update_inputs();
|
||||
report.buttons = debounce_mode();
|
||||
loop_mode();
|
||||
update_lights();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user