diff options
author | Ryan <fauxpark@gmail.com> | 2022-03-27 05:38:09 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-26 18:38:09 +0000 |
commit | c05e8afe454bf3706d69314c251dc5266c557007 (patch) | |
tree | 604b8b446d111f9d5307888e9ea61acf8fada14d /quantum | |
parent | 71ffb41c9b7c87cbeb2b19bac058717436bcda23 (diff) |
Joystick feature updates (#16732)
* Joystick feature updates
* Move new functions to joystick.h
* Docs
Diffstat (limited to 'quantum')
-rw-r--r-- | quantum/joystick.c | 37 | ||||
-rw-r--r-- | quantum/joystick.h | 9 | ||||
-rw-r--r-- | quantum/process_keycode/process_joystick.c | 36 | ||||
-rw-r--r-- | quantum/quantum.h | 4 |
4 files changed, 50 insertions, 36 deletions
diff --git a/quantum/joystick.c b/quantum/joystick.c index 7b87201aef..86b2c64036 100644 --- a/quantum/joystick.c +++ b/quantum/joystick.c @@ -1,13 +1,38 @@ #include "joystick.h" -joystick_t joystick_status = {.buttons = {0}, - .axes = - { +// clang-format off +joystick_t joystick_status = { + .buttons = {0}, + .axes = { #if JOYSTICK_AXES_COUNT > 0 - 0 + 0 #endif - }, - .status = 0}; + }, + .status = 0 +}; +// clang-format on // array defining the reading of analog values for each axis __attribute__((weak)) joystick_config_t joystick_axes[JOYSTICK_AXES_COUNT] = {}; + +// to be implemented in the hid protocol library +void send_joystick_packet(joystick_t *joystick); + +void joystick_flush(void) { + if ((joystick_status.status & JS_UPDATED) > 0) { + send_joystick_packet(&joystick_status); + joystick_status.status &= ~JS_UPDATED; + } +} + +void register_joystick_button(uint8_t button) { + joystick_status.buttons[button / 8] |= 1 << (button % 8); + joystick_status.status |= JS_UPDATED; + joystick_flush(); +} + +void unregister_joystick_button(uint8_t button) { + joystick_status.buttons[button / 8] &= ~(1 << (button % 8)); + joystick_status.status |= JS_UPDATED; + joystick_flush(); +} diff --git a/quantum/joystick.h b/quantum/joystick.h index 9156491aca..002df3a6d9 100644 --- a/quantum/joystick.h +++ b/quantum/joystick.h @@ -1,8 +1,7 @@ #pragma once -#include "quantum.h" - #include <stdint.h> +#include "gpio.h" #ifndef JOYSTICK_BUTTON_COUNT # define JOYSTICK_BUTTON_COUNT 8 @@ -58,5 +57,7 @@ typedef struct { extern joystick_t joystick_status; -// to be implemented in the hid protocol library -void send_joystick_packet(joystick_t *joystick); +void joystick_flush(void); + +void register_joystick_button(uint8_t button); +void unregister_joystick_button(uint8_t button); diff --git a/quantum/process_keycode/process_joystick.c b/quantum/process_keycode/process_joystick.c index 2fb092c573..8c3e71616f 100644 --- a/quantum/process_keycode/process_joystick.c +++ b/quantum/process_keycode/process_joystick.c @@ -6,41 +6,25 @@ #include <string.h> #include <math.h> -bool process_joystick_buttons(uint16_t keycode, keyrecord_t *record); - bool process_joystick(uint16_t keycode, keyrecord_t *record) { - if (process_joystick_buttons(keycode, record) && (joystick_status.status & JS_UPDATED) > 0) { - send_joystick_packet(&joystick_status); - joystick_status.status &= ~JS_UPDATED; + switch (keycode) { + case JS_BUTTON0 ... JS_BUTTON_MAX: + if (record->event.pressed) { + register_joystick_button(keycode - JS_BUTTON0); + } else { + unregister_joystick_button(keycode - JS_BUTTON0); + } + return false; } - return true; } __attribute__((weak)) void joystick_task(void) { - if (process_joystick_analogread() && (joystick_status.status & JS_UPDATED)) { - send_joystick_packet(&joystick_status); - joystick_status.status &= ~JS_UPDATED; + if (process_joystick_analogread()) { + joystick_flush(); } } -bool process_joystick_buttons(uint16_t keycode, keyrecord_t *record) { - if (keycode < JS_BUTTON0 || keycode > JS_BUTTON_MAX) { - return true; - } else { - uint8_t button_idx = (keycode - JS_BUTTON0); - if (record->event.pressed) { - joystick_status.buttons[button_idx / 8] |= 1 << (button_idx % 8); - } else { - joystick_status.buttons[button_idx / 8] &= ~(1 << (button_idx % 8)); - } - - joystick_status.status |= JS_UPDATED; - } - - return true; -} - uint16_t savePinState(pin_t pin) { #ifdef __AVR__ uint8_t pinNumber = pin & 0xF; diff --git a/quantum/quantum.h b/quantum/quantum.h index 020e455941..f87e5f1916 100644 --- a/quantum/quantum.h +++ b/quantum/quantum.h @@ -200,6 +200,10 @@ extern layer_state_t layer_state; # include "dynamic_keymap.h" #endif +#ifdef JOYSTICK_ENABLE +# include "joystick.h" +#endif + #ifdef VIA_ENABLE # include "via.h" #endif |