Skip to content

Commit

Permalink
Protect AVR-specific code and provide untried implementation of hid j…
Browse files Browse the repository at this point in the history
…oystick interface for chibios
  • Loading branch information
alecool committed Nov 30, 2018
1 parent afb6397 commit 0e8c2e4
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
4 changes: 3 additions & 1 deletion common_features.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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:= \
Expand Down
8 changes: 7 additions & 1 deletion quantum/process_keycode/process_joystick.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include <quantum/joystick.h>
#include <quantum/quantum_keycodes.h>

#include <drivers/avr/analog.h>
#ifdef __AVR__
# include <drivers/avr/analog.h>
#endif

#include <string.h>

Expand Down Expand Up @@ -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;
Expand Down
73 changes: 73 additions & 0 deletions tmk_core/protocol/chibios/usb_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
extern keymap_config_t keymap_config;
#endif

#ifdef JOYSTICK_ENABLE
# include <quantum/joystick.h>
#endif

/* ---------------------------------------------------------
* Global interface variables and declarations
* ---------------------------------------------------------
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
3 changes: 2 additions & 1 deletion tmk_core/protocol/usb_descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down

0 comments on commit 0e8c2e4

Please sign in to comment.