From 0e8c2e4662bbef5bc247d7c9d4ad288e85ba76ec Mon Sep 17 00:00:00 2001 From: achol Date: Sat, 1 Dec 2018 00:23:24 +0100 Subject: [PATCH] Protect AVR-specific code and provide untried implementation of hid joystick interface for chibios --- common_features.mk | 4 +- quantum/process_keycode/process_joystick.c | 8 ++- tmk_core/protocol/chibios/usb_main.c | 73 ++++++++++++++++++++++ tmk_core/protocol/usb_descriptor.h | 3 +- 4 files changed, 85 insertions(+), 3 deletions(-) diff --git a/common_features.mk b/common_features.mk index e4c1beb72bfb..7aab1deb2b42 100644 --- a/common_features.mk +++ b/common_features.mk @@ -244,7 +244,9 @@ ifeq ($(strip $(JOYSTICK_ENABLE)), yes) OPT_DEFS += -DJOYSTICK_ENABLE SRC += $(QUANTUM_DIR)/process_keycode/process_joystick.c SRC += $(QUANTUM_DIR)/joystick.c - SRC += drivers/avr/analog.c + ifeq ($(PLATFORM),AVR) + SRC += drivers/avr/analog.c + endif endif QUANTUM_SRC:= \ diff --git a/quantum/process_keycode/process_joystick.c b/quantum/process_keycode/process_joystick.c index 95d771d8dd88..860dd95b9f8f 100644 --- a/quantum/process_keycode/process_joystick.c +++ b/quantum/process_keycode/process_joystick.c @@ -3,7 +3,9 @@ #include #include -#include +#ifdef __AVR__ +# include +#endif #include @@ -60,7 +62,11 @@ bool process_joystick_analog(){ setPinInput(joystick_axes[axis_index].input_pin); writePinLow(joystick_axes[axis_index].input_pin); +#ifdef __AVR__ int16_t axis_val = analogRead(joystick_axes[axis_index].input_pin & (0xFF >> PORT_SHIFTER)); +#else + int16_t axis_val = 0; +#endif if (axis_val!=joystick_status.axes[axis_index]){ joystick_status.axes[axis_index] = axis_val; joystick_status.status |= JS_UPDATED; diff --git a/tmk_core/protocol/chibios/usb_main.c b/tmk_core/protocol/chibios/usb_main.c index 8223d9722867..f1df4a2e83a3 100644 --- a/tmk_core/protocol/chibios/usb_main.c +++ b/tmk_core/protocol/chibios/usb_main.c @@ -37,6 +37,10 @@ extern keymap_config_t keymap_config; #endif +#ifdef JOYSTICK_ENABLE +# include +#endif + /* --------------------------------------------------------- * Global interface variables and declarations * --------------------------------------------------------- @@ -235,6 +239,9 @@ typedef struct { #endif #ifdef VIRTSER_ENABLE usb_driver_config_t serial_driver; +#endif +#ifdef JOYSTICK_ENABLE + usb_driver_config_t joystick_driver; #endif }; usb_driver_config_t array[0]; @@ -272,6 +279,14 @@ static usb_driver_configs_t drivers = { #define CDC_OUT_MODE USB_EP_MODE_TYPE_BULK .serial_driver = QMK_USB_DRIVER_CONFIG(CDC, CDC_NOTIFICATION_EPNUM, false), #endif + +#ifdef JOYSTICK_ENABLE + #define JOYSTICK_IN_CAPACITY 4 + #define JOYSTICK_OUT_CAPACITY 4 + #define JOYSTICK_IN_MODE USB_EP_MODE_TYPE_BULK + #define JOYSTICK_OUT_MODE USB_EP_MODE_TYPE_BULK + .joystick_driver = QMK_USB_DRIVER_CONFIG(JOYSTICK, 0, false), +#endif }; #define NUM_USB_DRIVERS (sizeof(drivers) / sizeof(usb_driver_config_t)) @@ -882,3 +897,61 @@ void virtser_task(void) { } #endif + +#ifdef JOYSTICK_ENABLE + +typedef struct { + #if JOYSTICK_AXES_COUNT>0 + int8_t axes[JOYSTICK_AXES_COUNT]; + #endif + + #if JOYSTICK_BUTTON_COUNT>0 + uint8_t buttons[(JOYSTICK_BUTTON_COUNT-1)/8+1]; + #endif +} __attribute__ ((packed)) joystick_report_t; + +void send_joystick_packet(joystick_t* joystick) { + joystick_report_t rep = { + #if JOYSTICK_AXES_COUNT>0 + .axes = { + joystick->axes[0] + + #if JOYSTICK_AXES_COUNT >= 2 + ,joystick->axes[1] + #endif + #if JOYSTICK_AXES_COUNT >= 3 + ,joystick->axes[2] + #endif + #if JOYSTICK_AXES_COUNT >= 4 + ,joystick->axes[3] + #endif + #if JOYSTICK_AXES_COUNT >= 5 + ,joystick->axes[4] + #endif + #if JOYSTICK_AXES_COUNT >= 6 + ,joystick->axes[5] + #endif + }, + #endif //JOYSTICK_AXES_COUNT>0 + + #if JOYSTICK_BUTTON_COUNT>0 + .buttons = { + joystick->buttons[0] + + #if JOYSTICK_BUTTON_COUNT>8 + ,joystick->buttons[1] + #endif + #if JOYSTICK_BUTTON_COUNT>16 + ,joystick->buttons[2] + #endif + #if JOYSTICK_BUTTON_COUNT>24 + ,joystick->buttons[3] + #endif + } + #endif //JOYSTICK_BUTTON_COUNT>0 + }; + + chnWrite(&drivers.joystick_driver.driver, (uint8_t*)&rep, sizeof(rep)); +} + +#endif diff --git a/tmk_core/protocol/usb_descriptor.h b/tmk_core/protocol/usb_descriptor.h index 429dd397e3a3..0d379e9dd3b5 100644 --- a/tmk_core/protocol/usb_descriptor.h +++ b/tmk_core/protocol/usb_descriptor.h @@ -215,8 +215,9 @@ enum usb_endpoints { # define CDC_IN_EPADDR (ENDPOINT_DIR_IN | CDC_IN_EPNUM) # define CDC_OUT_EPADDR (ENDPOINT_DIR_OUT | CDC_OUT_EPNUM) #endif -#if defined(JOYSTICK_ENABLE) +#ifdef JOYSTICK_ENABLE JOYSTICK_IN_EPNUM = NEXT_EPNUM, + JOYSTICK_OUT_EPNUM = NEXT_EPNUM, #endif };