diff --git a/build_uf2/Pico_Game_Controller.uf2 b/build_uf2/Pico_Game_Controller.uf2 index 793d01f..de2ee72 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 ec9358c..45bfdd3 100644 --- a/src/controller_config.h +++ b/src/controller_config.h @@ -1,13 +1,12 @@ #ifndef CONTROLLER_CONFIG_H #define CONTROLLER_CONFIG_H -#define SW_GPIO_SIZE 11 // Number of switches -#define LED_GPIO_SIZE 10 // Number of switches -#define ENC_GPIO_SIZE 2 // Number of encoders -#define ENC_PPR 600 // Encoder PPR -#define ENC_DEBOUNCE true // Encoder Debouncing -#define ENC_PULSE (ENC_PPR * 4) // 4 pulses per PPR -#define ENC_ROLLOVER (ENC_PULSE * 2) // Delta Rollover threshold +#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 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 diff --git a/src/pico_game_controller.c b/src/pico_game_controller.c index 33cef6f..c18305a 100644 --- a/src/pico_game_controller.c +++ b/src/pico_game_controller.c @@ -9,6 +9,7 @@ #include #include "bsp/board.h" +#include "controller_config.h" #include "encoders.pio.h" #include "hardware/clocks.h" #include "hardware/dma.h" @@ -19,7 +20,6 @@ #include "tusb.h" #include "usb_descriptors.h" #include "ws2812.pio.h" -#include "controller_config.h" PIO pio, pio_1; uint32_t enc_val[ENC_GPIO_SIZE]; @@ -153,35 +153,16 @@ void joy_mode() { // find the delta between previous and current enc_val for (int i = 0; i < ENC_GPIO_SIZE; i++) { - int delta; - int changeType; // -1 for negative 1 for positive - if (enc_val[i] > prev_enc_val[i]) { // if the new value is bigger its - // a positive change - delta = enc_val[i] - prev_enc_val[i]; - changeType = 1; - } else { // otherwise its a negative change - delta = prev_enc_val[i] - enc_val[i]; - changeType = -1; - } - // Overflow / Underflow - if (delta > ENC_ROLLOVER) { - // Reverse the change type due to overflow / underflow - changeType *= -1; - delta = UINT32_MAX - delta + 1; // this should give us how much we - // overflowed / underflowed by - } - - cur_enc_val[i] = - cur_enc_val[i] + ((ENC_REV[i] ? 1 : -1) * delta * changeType); - while (cur_enc_val[i] < 0) { - cur_enc_val[i] = ENC_PULSE - cur_enc_val[i]; - } + cur_enc_val[i] += + ((ENC_REV[i] ? 1 : -1) * (enc_val[i] - prev_enc_val[i])); + while (cur_enc_val[i] < 0) cur_enc_val[i] = ENC_PULSE + cur_enc_val[i]; + cur_enc_val[i] %= ENC_PULSE; prev_enc_val[i] = enc_val[i]; } - report.joy0 = ((double)cur_enc_val[0] / ENC_PULSE) * 256; - report.joy1 = ((double)cur_enc_val[1] / ENC_PULSE) * 256; + report.joy0 = ((double)cur_enc_val[0] / ENC_PULSE) * (UINT8_MAX + 1); + report.joy1 = ((double)cur_enc_val[1] / ENC_PULSE) * (UINT8_MAX + 1); tud_hid_n_report(0x00, REPORT_ID_JOYSTICK, &report, sizeof(report)); }