Fix HID joystick report sending for ChibiOS.

Add one analog axis to the onekey:joystick keymap.
Fix pin state save and restore during joystick analog read for STM32
MCUs.
This commit is contained in:
a-chol 2020-03-30 00:51:43 +02:00
parent def06b4976
commit 887e96cbb2
3 changed files with 33 additions and 7 deletions

View file

@ -1,3 +1,3 @@
#pragma once
#define JOYSTICK_AXES_COUNT 0
#define JOYSTICK_AXES_COUNT 1
#define JOYSTICK_BUTTON_COUNT 1

View file

@ -1,5 +1,11 @@
#include QMK_KEYBOARD_H
#include "joystick.h"
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
LAYOUT( JS_BUTTON0 )
};
//joystick config
joystick_config_t joystick_axes[JOYSTICK_AXES_COUNT] = {
[0] = JOYSTICK_AXIS_IN(A0, 0, 512, 1024)
};

View file

@ -17,6 +17,7 @@ bool process_joystick(uint16_t keycode, keyrecord_t *record) {
return true;
}
__attribute__((weak))
void joystick_task(void) {
if (process_joystick_analogread() && (joystick_status.status & JS_UPDATED)) {
send_joystick_packet(&joystick_status);
@ -40,20 +41,39 @@ bool process_joystick_buttons(uint16_t keycode, keyrecord_t *record) {
return true;
}
uint8_t savePinState(uint8_t pin) {
uint16_t savePinState(uint32_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);
#else
return 0;
#endif
}
void restorePinState(uint8_t pin, uint8_t restoreState) {
void restorePinState(uint32_t pin, uint16_t restoreState) {
#ifdef __AVR__
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));
#else
return;
#endif
@ -69,7 +89,7 @@ bool process_joystick_analogread_quantum() {
}
// save previous input pin status as well
uint8_t inputSavedState = savePinState(joystick_axes[axis_index].input_pin);
uint16_t inputSavedState = savePinState(joystick_axes[axis_index].input_pin);
// disable pull-up resistor
writePinLow(joystick_axes[axis_index].input_pin);
@ -81,7 +101,7 @@ bool process_joystick_analogread_quantum() {
wait_us(10);
// save and apply output pin status
uint8_t outputSavedState = 0;
uint16_t outputSavedState = 0;
if (joystick_axes[axis_index].output_pin != JS_VIRTUAL_AXIS) {
// save previous output pin status
outputSavedState = savePinState(joystick_axes[axis_index].output_pin);
@ -90,7 +110,7 @@ bool process_joystick_analogread_quantum() {
writePinHigh(joystick_axes[axis_index].output_pin);
}
uint8_t groundSavedState = 0;
uint16_t groundSavedState = 0;
if (joystick_axes[axis_index].ground_pin != JS_VIRTUAL_AXIS) {
// save previous output pin status
groundSavedState = savePinState(joystick_axes[axis_index].ground_pin);
@ -126,7 +146,7 @@ bool process_joystick_analogread_quantum() {
//clamp the result in the valid range
ranged_val = ranged_val<-127 ? -127 : ranged_val;
ranged_val = ranged_val>127 ? 127 : ranged_val;
if (ranged_val != joystick_status.axes[axis_index]) {
joystick_status.axes[axis_index] = ranged_val;
joystick_status.status |= JS_UPDATED;