From 77e88674986ee14bd1799b1ab19b4c94af083bac Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Fri, 16 Feb 2024 14:14:46 +0000 Subject: [PATCH] Fix joystick initialization (#22953) --- builddefs/common_features.mk | 5 +- docs/feature_joystick.md | 8 --- quantum/joystick.c | 101 +++++++++++++++++++++-------------- quantum/joystick.h | 30 +++++------ quantum/keyboard.c | 3 ++ 5 files changed, 79 insertions(+), 68 deletions(-) diff --git a/builddefs/common_features.mk b/builddefs/common_features.mk index 513a3678a9..dab4ea083a 100644 --- a/builddefs/common_features.mk +++ b/builddefs/common_features.mk @@ -815,15 +815,12 @@ ifeq ($(strip $(JOYSTICK_ENABLE)), yes) $(call CATASTROPHIC_ERROR,Invalid JOYSTICK_DRIVER,JOYSTICK_DRIVER="$(JOYSTICK_DRIVER)" is not a valid joystick driver) endif OPT_DEFS += -DJOYSTICK_ENABLE + OPT_DEFS += -DJOYSTICK_$(strip $(shell echo $(JOYSTICK_DRIVER) | tr '[:lower:]' '[:upper:]')) SRC += $(QUANTUM_DIR)/process_keycode/process_joystick.c SRC += $(QUANTUM_DIR)/joystick.c ifeq ($(strip $(JOYSTICK_DRIVER)), analog) ANALOG_DRIVER_REQUIRED = yes - OPT_DEFS += -DANALOG_JOYSTICK_ENABLE - endif - ifeq ($(strip $(JOYSTICK_DRIVER)), digital) - OPT_DEFS += -DDIGITAL_JOYSTICK_ENABLE endif endif diff --git a/docs/feature_joystick.md b/docs/feature_joystick.md index 7b699aef17..0e4529b2eb 100644 --- a/docs/feature_joystick.md +++ b/docs/feature_joystick.md @@ -50,10 +50,6 @@ Axes can be configured using one of the following macros: * `JOYSTICK_AXIS_IN(input_pin, low, rest, high)` The ADC samples the provided pin. `low`, `high` and `rest` correspond to the minimum, maximum, and resting (or centered) analog values of the axis, respectively. - * `JOYSTICK_AXIS_IN_OUT(input_pin, output_pin, low, rest, high)` - Same as `JOYSTICK_AXIS_IN()`, but the provided `output_pin` will be pulled high before `input_pin` is read. - * `JOYSTICK_AXIS_IN_OUT_GROUND(input_pin, output_pin, ground_pin, low, rest, high)` - Same as `JOYSTICK_AXIS_IN_OUT()`, but the provided `ground_pin` will be pulled low before reading from `input_pin`. * `JOYSTICK_AXIS_VIRTUAL` No ADC reading is performed. The value should be provided by user code. @@ -160,12 +156,8 @@ Describes a single axis. #### Members :id=api-joystick-config-t-members - - `pin_t output_pin` - A pin to set as output high when reading the analog value, or `JS_VIRTUAL_AXIS`. - `pin_t input_pin` The pin to read the analog value from, or `JS_VIRTUAL_AXIS`. - - `pin_t ground_pin` - A pin to set as output low when reading the analog value, or `JS_VIRTUAL_AXIS`. - `uint16_t min_digit` The minimum analog value. - `uint16_t mid_digit` diff --git a/quantum/joystick.c b/quantum/joystick.c index 02818e4acd..3e9edeb8e2 100644 --- a/quantum/joystick.c +++ b/quantum/joystick.c @@ -15,10 +15,12 @@ */ #include "joystick.h" - -#include "analog.h" #include "wait.h" +#if defined(JOYSTICK_ANALOG) +# include "analog.h" +#endif + joystick_t joystick_state = { .buttons = {0}, .axes = @@ -31,21 +33,47 @@ joystick_t joystick_state = { }; // array defining the reading of analog values for each axis -__attribute__((weak)) joystick_config_t joystick_axes[JOYSTICK_AXIS_COUNT] = {}; +__attribute__((weak)) joystick_config_t joystick_axes[JOYSTICK_AXIS_COUNT] = { +#if JOYSTICK_AXIS_COUNT > 0 + [0 ...(JOYSTICK_AXIS_COUNT - 1)] = JOYSTICK_AXIS_VIRTUAL +#endif +}; -__attribute__((weak)) void joystick_task(void) { - joystick_read_axes(); +__attribute__((weak)) void joystick_axis_init(uint8_t axis) { + if (axis >= JOYSTICK_AXIS_COUNT) return; + +#if defined(JOYSTICK_ANALOG) + setPinInput(joystick_axes[axis].input_pin); +#endif +} + +__attribute__((weak)) uint16_t joystick_axis_sample(uint8_t axis) { + if (axis >= JOYSTICK_AXIS_COUNT) return 0; + +#if defined(JOYSTICK_ANALOG) + return analogReadPin(joystick_axes[axis].input_pin); +#else + // default to resting position + return joystick_axes[axis].mid_digit; +#endif +} + +static inline bool is_virtual_axis(uint8_t axis) { + return joystick_axes[axis].input_pin == NO_PIN; } void joystick_flush(void) { - if (joystick_state.dirty) { - host_joystick_send(&joystick_state); - joystick_state.dirty = false; - } + if (!joystick_state.dirty) return; + + // TODO: host.h? + void host_joystick_send(joystick_t * joystick); + host_joystick_send(&joystick_state); + joystick_state.dirty = false; } void register_joystick_button(uint8_t button) { if (button >= JOYSTICK_BUTTON_COUNT) return; + joystick_state.buttons[button / 8] |= 1 << (button % 8); joystick_state.dirty = true; joystick_flush(); @@ -53,6 +81,7 @@ void register_joystick_button(uint8_t button) { void unregister_joystick_button(uint8_t button) { if (button >= JOYSTICK_BUTTON_COUNT) return; + joystick_state.buttons[button / 8] &= ~(1 << (button % 8)); joystick_state.dirty = true; joystick_flush(); @@ -61,37 +90,7 @@ void unregister_joystick_button(uint8_t button) { int16_t joystick_read_axis(uint8_t axis) { if (axis >= JOYSTICK_AXIS_COUNT) return 0; - // disable pull-up resistor - writePinLow(joystick_axes[axis].input_pin); - - // if pin was a pull-up input, we need to uncharge it by turning it low - // before making it a low input - setPinOutput(joystick_axes[axis].input_pin); - - wait_us(10); - - if (joystick_axes[axis].output_pin != JS_VIRTUAL_AXIS) { - setPinOutput(joystick_axes[axis].output_pin); - writePinHigh(joystick_axes[axis].output_pin); - } - - if (joystick_axes[axis].ground_pin != JS_VIRTUAL_AXIS) { - setPinOutput(joystick_axes[axis].ground_pin); - writePinLow(joystick_axes[axis].ground_pin); - } - - wait_us(10); - - setPinInput(joystick_axes[axis].input_pin); - - wait_us(10); - -#if defined(ANALOG_JOYSTICK_ENABLE) && (defined(__AVR__) || defined(PROTOCOL_CHIBIOS)) - int16_t axis_val = analogReadPin(joystick_axes[axis].input_pin); -#else - // default to resting position - int16_t axis_val = joystick_axes[axis].mid_digit; -#endif + int16_t axis_val = joystick_axis_sample(axis); // test the converted value against the lower range int32_t ref = joystick_axes[axis].mid_digit; @@ -111,10 +110,22 @@ int16_t joystick_read_axis(uint8_t axis) { return ranged_val; } +void joystick_init_axes(void) { +#if JOYSTICK_AXIS_COUNT > 0 + for (int i = 0; i < JOYSTICK_AXIS_COUNT; ++i) { + if (is_virtual_axis(i)) { + continue; + } + + joystick_axis_init(i); + } +#endif +} + void joystick_read_axes(void) { #if JOYSTICK_AXIS_COUNT > 0 for (int i = 0; i < JOYSTICK_AXIS_COUNT; ++i) { - if (joystick_axes[i].input_pin == JS_VIRTUAL_AXIS) { + if (is_virtual_axis(i)) { continue; } @@ -133,3 +144,11 @@ void joystick_set_axis(uint8_t axis, int16_t value) { joystick_state.dirty = true; } } + +void joystick_init(void) { + joystick_init_axes(); +} + +void joystick_task(void) { + joystick_read_axes(); +} diff --git a/quantum/joystick.h b/quantum/joystick.h index 5de4ba66c6..5a69ceac64 100644 --- a/quantum/joystick.h +++ b/quantum/joystick.h @@ -52,24 +52,15 @@ #define JOYSTICK_MAX_VALUE ((1L << (JOYSTICK_AXIS_RESOLUTION - 1)) - 1) -// configure on input_pin of the joystick_axes array entry to JS_VIRTUAL_AXIS -// to prevent it from being read from the ADC. This allows outputing forged axis value. -// -#define JS_VIRTUAL_AXIS 0xFF - +// configure on input_pin of the joystick_axes array entry to NO_PIN +// to prevent it from being read from the ADC. This allows outputting forged axis value. #define JOYSTICK_AXIS_VIRTUAL \ - { JS_VIRTUAL_AXIS, JS_VIRTUAL_AXIS, JS_VIRTUAL_AXIS, 0, 1023 } + { NO_PIN, 0, JOYSTICK_MAX_VALUE / 2, JOYSTICK_MAX_VALUE } #define JOYSTICK_AXIS_IN(INPUT_PIN, LOW, REST, HIGH) \ - { JS_VIRTUAL_AXIS, INPUT_PIN, JS_VIRTUAL_AXIS, LOW, REST, HIGH } -#define JOYSTICK_AXIS_IN_OUT(INPUT_PIN, OUTPUT_PIN, LOW, REST, HIGH) \ - { OUTPUT_PIN, INPUT_PIN, JS_VIRTUAL_AXIS, LOW, REST, HIGH } -#define JOYSTICK_AXIS_IN_OUT_GROUND(INPUT_PIN, OUTPUT_PIN, GROUND_PIN, LOW, REST, HIGH) \ - { OUTPUT_PIN, INPUT_PIN, GROUND_PIN, LOW, REST, HIGH } + { INPUT_PIN, LOW, REST, HIGH } typedef struct { - pin_t output_pin; pin_t input_pin; - pin_t ground_pin; // the AVR ADC offers 10 bit precision, with significant bits on the higher part uint16_t min_digit; @@ -87,6 +78,14 @@ typedef struct { extern joystick_t joystick_state; +/** + * \brief Handle the initialization of the subsystem. + */ +void joystick_init(void); + +/** + * \brief Handle various subsystem background tasks. + */ void joystick_task(void); /** @@ -117,6 +116,9 @@ void unregister_joystick_button(uint8_t button); */ int16_t joystick_read_axis(uint8_t axis); +/** + * \brief Sample and process the all axis. + */ void joystick_read_axes(void); /** @@ -127,6 +129,4 @@ void joystick_read_axes(void); */ void joystick_set_axis(uint8_t axis, int16_t value); -void host_joystick_send(joystick_t *joystick); - /** \} */ diff --git a/quantum/keyboard.c b/quantum/keyboard.c index 9bdf6e6344..ab25b02547 100644 --- a/quantum/keyboard.c +++ b/quantum/keyboard.c @@ -460,6 +460,9 @@ void keyboard_init(void) { #ifdef DIP_SWITCH_ENABLE dip_switch_init(); #endif +#ifdef JOYSTICK_ENABLE + joystick_init(); +#endif #ifdef SLEEP_LED_ENABLE sleep_led_init(); #endif