diff --git a/build_uf2/Pico_Game_Controller.uf2 b/build_uf2/Pico_Game_Controller.uf2 index c95ea12..cfd24d2 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 478a74e..d948388 100644 --- a/src/controller_config.h +++ b/src/controller_config.h @@ -1,16 +1,16 @@ #ifndef CONTROLLER_CONFIG_H #define CONTROLLER_CONFIG_H -#define SW_GPIO_SIZE 11 // Number of switches -#define LED_GPIO_SIZE 10 // Number of switch LEDs -#define ENC_GPIO_SIZE 2 // Number of encoders -#define ENC_PPR 600 // Encoder PPR -#define ENC_DEBOUNCE true // 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 500000 // Cycles before HID falls back to reactive -#define WS2812B_LED_SIZE 10 // Number of WS2812B LEDs -#define WS2812B_LED_ZONES 2 // Number of WS2812B LED Zones +#define SW_GPIO_SIZE 11 // Number of switches +#define LED_GPIO_SIZE 10 // Number of switch LEDs +#define ENC_GPIO_SIZE 2 // Number of encoders +#define ENC_PPR 600 // Encoder PPR +#define ENC_DEBOUNCE true // 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 +#define WS2812B_LED_SIZE 10 // Number of WS2812B LEDs +#define WS2812B_LED_ZONES 2 // Number of WS2812B LED Zones #define WS2812B_LEDS_PER_ZONE \ WS2812B_LED_SIZE / WS2812B_LED_ZONES // Number of LEDs per zone diff --git a/src/pico_game_controller.c b/src/pico_game_controller.c index adb91a5..db2cf5a 100644 --- a/src/pico_game_controller.c +++ b/src/pico_game_controller.c @@ -31,7 +31,7 @@ uint64_t sw_timestamp[SW_GPIO_SIZE]; bool kbm_report; -unsigned long reactive_timeout_count = REACTIVE_TIMEOUT_MAX; +uint64_t reactive_timeout_timestamp; void (*loop_mode)(); bool joy_mode_check = true; @@ -94,7 +94,7 @@ void ws2812b_color_cycle(uint32_t counter) { * @param counter Current number of WS2812B cycles **/ void ws2812b_update(uint32_t counter) { - if (reactive_timeout_count >= REACTIVE_TIMEOUT_MAX) { + if (time_us_64() - reactive_timeout_timestamp >= REACTIVE_TIMEOUT_MAX) { ws2812b_color_cycle(counter); } else { for (int i = 0; i < WS2812B_LED_ZONES; i++) { @@ -111,11 +111,8 @@ void ws2812b_update(uint32_t counter) { * HID/Reactive Lights **/ void update_lights() { - if (reactive_timeout_count < REACTIVE_TIMEOUT_MAX) { - reactive_timeout_count++; - } for (int i = 0; i < LED_GPIO_SIZE; i++) { - if (reactive_timeout_count >= REACTIVE_TIMEOUT_MAX) { + if (time_us_64() - reactive_timeout_timestamp >= REACTIVE_TIMEOUT_MAX) { if (!gpio_get(SW_GPIO[i])) { gpio_put(LED_GPIO[i], 1); } else { @@ -276,6 +273,8 @@ void init() { dma_channel_set_irq0_enabled(i, true); } + reactive_timeout_timestamp = time_us_64(); + // Set up WS2812B pio_1 = pio1; uint offset2 = pio_add_program(pio_1, &ws2812_program); @@ -365,12 +364,12 @@ void tud_hid_set_report_cb(uint8_t itf, uint8_t report_id, uint16_t bufsize) { (void)itf; if (report_id == 2 && report_type == HID_REPORT_TYPE_OUTPUT && - buffer[0] == 2 && bufsize >= sizeof(lights_report)) // light data + bufsize >= sizeof(lights_report)) // light data { size_t i = 0; for (i; i < sizeof(lights_report); i++) { - lights_report.raw[i] = buffer[i + 1]; + lights_report.raw[i] = buffer[i]; } - reactive_timeout_count = 0; + reactive_timeout_timestamp = time_us_64(); } }