Skip to content

Commit

Permalink
Ignore axis-related code when axis number=0
Browse files Browse the repository at this point in the history
The analog joystick feature does not compile accross all platforms (see qmk#4226 (review))

All joystick related code is put between #if JOYSTICK_AXES_COUNT > 0

This also reduces the size of the compiled code when you only want to use joystick buttons.

\### How to reproduce bug:

1. `qmk new-keyboard`
2. select the MCU `GD32VF103` (other options don't matter)
3. `rules.mk` Add `JOYSTICK_ENABLE = yes`
4. `config.h` : Add
    ```c
    #define JOYSTICK_BUTTON_COUNT 1
    #define JOYSTICK_AXES_COUNT 0
    ```
5. (optional) Change a key to `JS_BUTTON0` in keymap
6. run `qmk compile` against new keyboard

Errors will show related to the ADC.

\### Disclaimer

1. I do not have the proper HW to debug on all keyboards, but it does work from the RP2040 branch which has a similar problem
   See qmk#14877 (comment)
2. I guess `ifeq ($(shell expr $(JOYSTICK_AXES_COUNT) \> 0), 1)` ain't so pretty,
   but it didn't work with `#ifneq ($(JOYSTICK_AXES_COUNT), 0) ??
  • Loading branch information
amrsoll committed May 1, 2022
1 parent 59885d0 commit 26e2c0f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 28 deletions.
14 changes: 8 additions & 6 deletions builddefs/common_features.mk
Original file line number Diff line number Diff line change
Expand Up @@ -764,12 +764,14 @@ ifeq ($(strip $(JOYSTICK_ENABLE)), yes)
SRC += $(QUANTUM_DIR)/process_keycode/process_joystick.c
SRC += $(QUANTUM_DIR)/joystick.c

ifeq ($(strip $(JOYSTICK_DRIVER)), analog)
OPT_DEFS += -DANALOG_JOYSTICK_ENABLE
SRC += analog.c
endif
ifeq ($(strip $(JOYSTICK_DRIVER)), digital)
OPT_DEFS += -DDIGITAL_JOYSTICK_ENABLE
ifeq ($(shell expr $(JOYSTICK_AXES_COUNT) \> 0), 1)
ifeq ($(strip $(JOYSTICK_DRIVER)), analog)
OPT_DEFS += -DANALOG_JOYSTICK_ENABLE
SRC += analog.c
endif
ifeq ($(strip $(JOYSTICK_DRIVER)), digital)
OPT_DEFS += -DDIGITAL_JOYSTICK_ENABLE
endif
endif
endif

Expand Down
52 changes: 30 additions & 22 deletions quantum/process_keycode/process_joystick.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#include "joystick.h"
#include "process_joystick.h"

#include "analog.h"
#if JOYSTICK_AXES_COUNT > 0
# ifdef ANALOG_JOYSTICK_ENABLE
# include "analog.h"
# endif
#endif

#include <string.h>
#include <math.h>
Expand All @@ -26,33 +30,37 @@ __attribute__((weak)) void joystick_task(void) {
}

uint16_t savePinState(pin_t pin) {
#ifdef __AVR__
uint8_t pinNumber = pin & 0xF;
return ((PORTx_ADDRESS(pin) >> pinNumber) & 0x1) << 1 | ((DDRx_ADDRESS(pin) >> pinNumber) & 0x1);
#elif defined(PROTOCOL_CHIBIOS)
/*
The pin configuration is backed up in the following format :
bit 15 9 8 7 6 5 4 3 2 1 0
|unused|ODR|IDR|PUPDR|OSPEEDR|OTYPER|MODER|
*/
return ((PAL_PORT(pin)->MODER >> (2 * PAL_PAD(pin))) & 0x3) | (((PAL_PORT(pin)->OTYPER >> (1 * PAL_PAD(pin))) & 0x1) << 2) | (((PAL_PORT(pin)->OSPEEDR >> (2 * PAL_PAD(pin))) & 0x3) << 3) | (((PAL_PORT(pin)->PUPDR >> (2 * PAL_PAD(pin))) & 0x3) << 5) | (((PAL_PORT(pin)->IDR >> (1 * PAL_PAD(pin))) & 0x1) << 7) | (((PAL_PORT(pin)->ODR >> (1 * PAL_PAD(pin))) & 0x1) << 8);
#if JOYSTICK_AXES_COUNT > 0
# ifdef __AVR__
uint8_t pinNumber = pin & 0xF;
return ((PORTx_ADDRESS(pin) >> pinNumber) & 0x1) << 1 | ((DDRx_ADDRESS(pin) >> pinNumber) & 0x1);
# elif defined(PROTOCOL_CHIBIOS)
/*
The pin configuration is backed up in the following format :
bit 15 9 8 7 6 5 4 3 2 1 0
|unused|ODR|IDR|PUPDR|OSPEEDR|OTYPER|MODER|
*/
return ((PAL_PORT(pin)->MODER >> (2 * PAL_PAD(pin))) & 0x3) | (((PAL_PORT(pin)->OTYPER >> (1 * PAL_PAD(pin))) & 0x1) << 2) | (((PAL_PORT(pin)->OSPEEDR >> (2 * PAL_PAD(pin))) & 0x3) << 3) | (((PAL_PORT(pin)->PUPDR >> (2 * PAL_PAD(pin))) & 0x3) << 5) | (((PAL_PORT(pin)->IDR >> (1 * PAL_PAD(pin))) & 0x1) << 7) | (((PAL_PORT(pin)->ODR >> (1 * PAL_PAD(pin))) & 0x1) << 8);
# endif
#else
return 0;
#endif
}

void restorePinState(pin_t pin, uint16_t restoreState) {
#if defined(PROTOCOL_LUFA)
uint8_t pinNumber = pin & 0xF;
PORTx_ADDRESS(pin) = (PORTx_ADDRESS(pin) & ~_BV(pinNumber)) | (((restoreState >> 1) & 0x1) << pinNumber);
DDRx_ADDRESS(pin) = (DDRx_ADDRESS(pin) & ~_BV(pinNumber)) | ((restoreState & 0x1) << pinNumber);
#elif defined(PROTOCOL_CHIBIOS)
PAL_PORT(pin)->MODER = (PAL_PORT(pin)->MODER & ~(0x3 << (2 * PAL_PAD(pin)))) | (restoreState & 0x3) << (2 * PAL_PAD(pin));
PAL_PORT(pin)->OTYPER = (PAL_PORT(pin)->OTYPER & ~(0x1 << (1 * PAL_PAD(pin)))) | ((restoreState >> 2) & 0x1) << (1 * PAL_PAD(pin));
PAL_PORT(pin)->OSPEEDR = (PAL_PORT(pin)->OSPEEDR & ~(0x3 << (2 * PAL_PAD(pin)))) | ((restoreState >> 3) & 0x3) << (2 * PAL_PAD(pin));
PAL_PORT(pin)->PUPDR = (PAL_PORT(pin)->PUPDR & ~(0x3 << (2 * PAL_PAD(pin)))) | ((restoreState >> 5) & 0x3) << (2 * PAL_PAD(pin));
PAL_PORT(pin)->IDR = (PAL_PORT(pin)->IDR & ~(0x1 << (1 * PAL_PAD(pin)))) | ((restoreState >> 7) & 0x1) << (1 * PAL_PAD(pin));
PAL_PORT(pin)->ODR = (PAL_PORT(pin)->ODR & ~(0x1 << (1 * PAL_PAD(pin)))) | ((restoreState >> 8) & 0x1) << (1 * PAL_PAD(pin));
#if JOYSTICK_AXES_COUNT > 0
# if defined(PROTOCOL_LUFA)
uint8_t pinNumber = pin & 0xF;
PORTx_ADDRESS(pin) = (PORTx_ADDRESS(pin) & ~_BV(pinNumber)) | (((restoreState >> 1) & 0x1) << pinNumber);
DDRx_ADDRESS(pin) = (DDRx_ADDRESS(pin) & ~_BV(pinNumber)) | ((restoreState & 0x1) << pinNumber);
# elif defined(PROTOCOL_CHIBIOS)
PAL_PORT(pin)->MODER = (PAL_PORT(pin)->MODER & ~(0x3 << (2 * PAL_PAD(pin)))) | (restoreState & 0x3) << (2 * PAL_PAD(pin));
PAL_PORT(pin)->OTYPER = (PAL_PORT(pin)->OTYPER & ~(0x1 << (1 * PAL_PAD(pin)))) | ((restoreState >> 2) & 0x1) << (1 * PAL_PAD(pin));
PAL_PORT(pin)->OSPEEDR = (PAL_PORT(pin)->OSPEEDR & ~(0x3 << (2 * PAL_PAD(pin)))) | ((restoreState >> 3) & 0x3) << (2 * PAL_PAD(pin));
PAL_PORT(pin)->PUPDR = (PAL_PORT(pin)->PUPDR & ~(0x3 << (2 * PAL_PAD(pin)))) | ((restoreState >> 5) & 0x3) << (2 * PAL_PAD(pin));
PAL_PORT(pin)->IDR = (PAL_PORT(pin)->IDR & ~(0x1 << (1 * PAL_PAD(pin)))) | ((restoreState >> 7) & 0x1) << (1 * PAL_PAD(pin));
PAL_PORT(pin)->ODR = (PAL_PORT(pin)->ODR & ~(0x1 << (1 * PAL_PAD(pin)))) | ((restoreState >> 8) & 0x1) << (1 * PAL_PAD(pin));
# endif
#else
return;
#endif
Expand Down

0 comments on commit 26e2c0f

Please sign in to comment.