diff --git a/src/debounce/debounce_include.h b/src/debounce/debounce_include.h index 2e66197..581698d 100644 --- a/src/debounce/debounce_include.h +++ b/src/debounce/debounce_include.h @@ -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" \ No newline at end of file +#include "eager.c" diff --git a/src/debounce/deferred.c b/src/debounce/deferred.c index 9113255..0a1cfe5 100644 --- a/src/debounce/deferred.c +++ b/src/debounce/deferred.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; -} \ No newline at end of file +} diff --git a/src/debounce/eager.c b/src/debounce/eager.c index bf0bec6..e2b97dc 100644 --- a/src/debounce/eager.c +++ b/src/debounce/eager.c @@ -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; -} \ No newline at end of file +} diff --git a/src/pico_game_controller.c b/src/pico_game_controller.c index f14fcec..0e1668c 100644 --- a/src/pico_game_controller.c +++ b/src/pico_game_controller.c @@ -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(); }