diff --git a/build_uf2/Pico_Game_Controller.uf2 b/build_uf2/Pico_Game_Controller.uf2 index 5b0abf8..26bc47e 100644 Binary files a/build_uf2/Pico_Game_Controller.uf2 and b/build_uf2/Pico_Game_Controller.uf2 differ diff --git a/src/controller_config.h b/src/controller_config.h index 4214193..abca538 100644 --- a/src/controller_config.h +++ b/src/controller_config.h @@ -6,7 +6,7 @@ #define ENC_GPIO_SIZE 2 // Number of encoders #define ENC_PPR 600 // Encoder PPR #define MOUSE_SENS 1 // Mouse sensitivity multiplier -#define ENC_DEBOUNCE false // Encoder Debouncing +#define ENC_DEBOUNCE false // Encoder Debouncing #define SW_DEBOUNCE_TIME_US 4000 // Switch debounce delay in us #define ENC_PULSE (ENC_PPR * 4) // 4 pulses per PPR #define REACTIVE_TIMEOUT_MAX 1000000 // HID to reactive timeout in us diff --git a/src/debounce/constant_switch.c b/src/debounce/constant_switch.c new file mode 100644 index 0000000..a06d79a --- /dev/null +++ b/src/debounce/constant_switch.c @@ -0,0 +1,17 @@ +/** + * Debouncing algorithm which waits for switch to output a constant n amount of time before outputting + * @author SpeedyPotato + **/ + +uint16_t constant_switch() { + uint16_t translate_buttons = 0; + for (int i = SW_GPIO_SIZE - 1; i >= 0; i--) { + if (!gpio_get(SW_GPIO[i]) && + time_us_64() - sw_timestamp[i] >= SW_DEBOUNCE_TIME_US) { + translate_buttons = (translate_buttons << 1) | 1; + } else { + translate_buttons <<= 1; + } + } + return translate_buttons; +} \ No newline at end of file diff --git a/src/debounce/debounce_include.h b/src/debounce/debounce_include.h new file mode 100644 index 0000000..1999583 --- /dev/null +++ b/src/debounce/debounce_include.h @@ -0,0 +1,11 @@ +/** + * 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. These are saved in report.buttons as truth. + * Create debounce mode as desired and then add the #include here. + **/ +extern uint64_t sw_timestamp[SW_GPIO_SIZE]; + +#include "constant_switch.c" +#include "minimum_hold.c" \ No newline at end of file diff --git a/src/debounce/minimum_hold.c b/src/debounce/minimum_hold.c new file mode 100644 index 0000000..a284cd0 --- /dev/null +++ b/src/debounce/minimum_hold.c @@ -0,0 +1,16 @@ +/** + * Debouncing algorithm which sends a report immediately when a switch is triggered and holds it for n amount of time. + * @author SpeedyPotato + **/ + +uint16_t minimum_hold() { + 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; + } + } + return translate_buttons; +} \ No newline at end of file diff --git a/src/pico_game_controller.c b/src/pico_game_controller.c index 9bf19c4..68fa3a6 100644 --- a/src/pico_game_controller.c +++ b/src/pico_game_controller.c @@ -17,10 +17,12 @@ #include "hardware/pio.h" #include "pico/multicore.h" #include "pico/stdlib.h" -#include "rgb/rgb_include.h" #include "tusb.h" #include "usb_descriptors.h" +#include "rgb/rgb_include.h" +#include "debounce/debounce_include.h" + PIO pio, pio_1; uint32_t enc_val[ENC_GPIO_SIZE]; uint32_t prev_enc_val[ENC_GPIO_SIZE]; @@ -35,6 +37,7 @@ uint64_t reactive_timeout_timestamp; void (*ws2812b_mode)(); void (*loop_mode)(); +uint16_t (*debounce_mode)(); bool joy_mode_check = true; union { @@ -95,18 +98,6 @@ struct report { **/ void joy_mode() { if (tud_hid_ready()) { - uint16_t translate_buttons = 0; - for (int i = SW_GPIO_SIZE - 1; i >= 0; i--) { - if (!gpio_get(SW_GPIO[i]) && - time_us_64() - sw_timestamp[i] >= SW_DEBOUNCE_TIME_US) { - translate_buttons = - (translate_buttons << 1) | (!gpio_get(SW_GPIO[i]) ? 1 : 0); - } else { - translate_buttons <<= 1; - } - } - report.buttons = translate_buttons; - // find the delta between previous and current enc_val for (int i = 0; i < ENC_GPIO_SIZE; i++) { cur_enc_val[i] += @@ -133,8 +124,7 @@ void key_mode() { /*------------- Keyboard -------------*/ uint8_t nkro_report[32] = {0}; for (int i = 0; i < SW_GPIO_SIZE; i++) { - if (!gpio_get(SW_GPIO[i]) && - time_us_64() - sw_timestamp[i] >= SW_DEBOUNCE_TIME_US) { + if ((report.buttons >> i) % 2 == 1) { uint8_t bit = SW_KEYCODE[i] % 8; uint8_t byte = (SW_KEYCODE[i] / 8) + 1; if (SW_KEYCODE[i] >= 240 && SW_KEYCODE[i] <= 247) { @@ -284,6 +274,9 @@ void init() { ws2812b_mode = &ws2812b_color_cycle; } + // Debouncing Mode + debounce_mode = &minimum_hold; + // Disable RGB if (gpio_get(SW_GPIO[8])) { multicore_launch_core1(core1_entry); @@ -301,6 +294,7 @@ int main(void) { while (1) { tud_task(); // tinyusb device task update_inputs(); + report.buttons = debounce_mode(); loop_mode(); update_lights(); } diff --git a/src/rgb/rgb_include.h b/src/rgb/rgb_include.h index 02d2264..b4de733 100644 --- a/src/rgb/rgb_include.h +++ b/src/rgb/rgb_include.h @@ -5,6 +5,8 @@ * To add a lighting mode, create a function which accepts a uint32_t as a parameter. * Create lighting mode as desired and then add the #include here. **/ +extern uint32_t enc_val[ENC_GPIO_SIZE]; + #include "ws2812b_util.c" #include "color_cycle.c" #include "turbocharger.c" \ No newline at end of file diff --git a/src/rgb/turbocharger.c b/src/rgb/turbocharger.c index 81f2dec..4b92197 100644 --- a/src/rgb/turbocharger.c +++ b/src/rgb/turbocharger.c @@ -30,7 +30,6 @@ * By strategically positioning led 0 at the top, this avoids lighting areas * from suddenly appeaering. **/ -extern uint32_t enc_val[ENC_GPIO_SIZE]; #define TURBO_LIGHTS_CLAMP 0.1f #define TURBO_LIGHTS_THRESHOLD 0.05f