Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Joystick feature updates #16732

Merged
merged 3 commits into from
Mar 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions keyboards/handwired/battleship_gamepad/battleship_gamepad.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/

#include "battleship_gamepad.h"
#include "joystick.h"
#include "analog.h"

/* joystick config */
joystick_config_t joystick_axes[JOYSTICK_AXES_COUNT] = {
Expand Down
2 changes: 0 additions & 2 deletions keyboards/handwired/misterdeck/keymaps/default/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

#include QMK_KEYBOARD_H

#include "joystick.h"

enum layer_names {
NORMAL_LAYER = 0
};
Expand Down
2 changes: 0 additions & 2 deletions keyboards/handwired/misterdeck/keymaps/nobuttons/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

#include QMK_KEYBOARD_H

#include "joystick.h"

enum layer_names {
NORMAL_LAYER = 0
};
Expand Down
2 changes: 0 additions & 2 deletions keyboards/handwired/onekey/keymaps/joystick/keymap.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include QMK_KEYBOARD_H

#include "joystick.h"

#ifndef ADC_PIN
# define ADC_PIN F6
#endif
Expand Down
1 change: 0 additions & 1 deletion keyboards/lime/keymaps/default/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include QMK_KEYBOARD_H

#ifdef JOYSTICK_ENABLE
# include "joystick.h"
# include "analog.h"
#endif

Expand Down
15 changes: 9 additions & 6 deletions quantum/joystick.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#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] = {};
3 changes: 1 addition & 2 deletions quantum/joystick.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#pragma once

#include "quantum.h"

#include <stdint.h>
#include "gpio.h"

#ifndef JOYSTICK_BUTTON_COUNT
# define JOYSTICK_BUTTON_COUNT 8
Expand Down
45 changes: 24 additions & 21 deletions quantum/process_keycode/process_joystick.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,42 @@
#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)) {
if (process_joystick_analogread()) {
joystick_flush();
}
}

void joystick_flush(void) {
if ((joystick_status.status & JS_UPDATED) > 0) {
send_joystick_packet(&joystick_status);
joystick_status.status &= ~JS_UPDATED;
}
}

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;
}
void register_joystick_button(uint8_t button) {
joystick_status.buttons[button / 8] |= 1 << (button % 8);
joystick_status.status |= JS_UPDATED;
joystick_flush();
}

return true;
void unregister_joystick_button(uint8_t button) {
joystick_status.buttons[button / 8] &= ~(1 << (button % 8));
joystick_status.status |= JS_UPDATED;
joystick_flush();
}

uint16_t savePinState(pin_t pin) {
Expand Down
4 changes: 4 additions & 0 deletions quantum/process_keycode/process_joystick.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
bool process_joystick(uint16_t keycode, keyrecord_t *record);

void joystick_task(void);
void joystick_flush(void);

void register_joystick_button(uint8_t button);
void unregister_joystick_button(uint8_t button);

bool process_joystick_analogread(void);
bool process_joystick_analogread_quantum(void);
4 changes: 4 additions & 0 deletions quantum/quantum.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down