diff options
author | Gautham Yerroju <gautham.yerroju@gmail.com> | 2020-06-13 01:24:13 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-13 01:24:13 -0700 |
commit | 4057d44989e2ee83ca3b171bdb1a29bc9f3ae0cd (patch) | |
tree | f74840f3af9af8ed245748fb430e363594a3d8c3 /keyboards/kyria | |
parent | 2bfcb6bfc5766ede2b63041af3a163408caa6ad9 (diff) |
[Keymap] Add Kyria keymap (#9224)
* Add Kyria keymap
* clean split hand detection code
* rename "joystick" to "thumbstick"
* thumbstick overhaul
* removed angle correction, seems buggy
* save some memory
* Remove deprecated config option
* Use the correct types for getting host led states
* Fix include path
* Made .h files for encoder and oled code
* Increase speed cap on thumbstick
Diffstat (limited to 'keyboards/kyria')
-rw-r--r-- | keyboards/kyria/keymaps/gotham/README.md | 56 | ||||
-rw-r--r-- | keyboards/kyria/keymaps/gotham/config.h | 48 | ||||
-rw-r--r-- | keyboards/kyria/keymaps/gotham/encoder_utils.c | 94 | ||||
-rw-r--r-- | keyboards/kyria/keymaps/gotham/encoder_utils.h | 37 | ||||
-rw-r--r-- | keyboards/kyria/keymaps/gotham/keycodes.h | 16 | ||||
-rw-r--r-- | keyboards/kyria/keymaps/gotham/keymap.c | 126 | ||||
-rw-r--r-- | keyboards/kyria/keymaps/gotham/oled_utils.c | 103 | ||||
-rw-r--r-- | keyboards/kyria/keymaps/gotham/oled_utils.h | 25 | ||||
-rw-r--r-- | keyboards/kyria/keymaps/gotham/rules.mk | 21 | ||||
-rw-r--r-- | keyboards/kyria/keymaps/gotham/thumbstick.c | 195 | ||||
-rw-r--r-- | keyboards/kyria/keymaps/gotham/thumbstick.h | 98 |
11 files changed, 819 insertions, 0 deletions
diff --git a/keyboards/kyria/keymaps/gotham/README.md b/keyboards/kyria/keymaps/gotham/README.md new file mode 100644 index 0000000000..1598e95a73 --- /dev/null +++ b/keyboards/kyria/keymaps/gotham/README.md @@ -0,0 +1,56 @@ +# Gotham's Keymap for [Kyria](https://github.com/splitkb/kyria) + +## Keymap +This is my personal keymap for Kyria with some mods. + +More information about the Kyria keyboard can be found [here](https://blog.splitkb.com/blog/introducing-the-kyria) + +### Rotary Encoders + +Press the encoder on each half to cycle between: +- Volume +- Word Nav (Ctrl + Left / Right) +- Left / Right +- Up / Down +- Page Up / Page Down + +### OLEDs + +Master-side OLED displays dynamic data: +- Current layer +- Current mode of each rotary encoder +- Current mode of thumbstick + +Slave-side OLED currently only displays a static content. + +### Thumbstick + +A PSP 2000 thumbstick is attached to the right half. It will currently only function when the USB cable is connected to the right half. When I figure out how to transfer data between halves using serial link, I will make this work regardless of which side is the master. + +#### Thumbstick Configuration + +- __THUMBSTICK_ENABLE:__ Enable thumbstick. +- __THUMBSTICK_PIN_X/Y (mandatory):__ The QMK pins to use for the respective axis. The values are from the [QMK's ADC driver](https://docs.qmk.fm/#/adc_driver). I used F0 and F1, for example. +- __THUMBSTICK_FLIP_X/Y:__ Mirror the direction of the respective axis. Use to compensate for actual orientation of thumbstick. +- __THUMBSTICK_DEBUG:__ Print raw and calculated values from analogReadPin to console. Will only work with CONSOLE_ENABLE turned on. + +#### Thumbstick Fine-tuning + +More tunables are described here. Values like deadzone threshold are hardware-specific. The theoretical range for analog readings is [0, 1023], but emperical readings don't extend the entire range. To find the right values, turn on CONSOLE_ENABLE in rules.mk and THUMBSTICK_DEBUG in config.h to look at the raw values from the pins using hid_listen (or QMK Toolbox). + +- __THUMBSTICK_DEAD_ZONE 90:__ Values below this are ignored (deadzone). +- __THUMBSTICK_FINE_ZONE 180:__ Values below this enable fine movement. + +- __THUMBSTICK_MODE <mode>:__ One of THUMBSTICK_MODE_MOUSE, THUMBSTICK_MODE_ARROWS and THUMBSTICK_MODE_SCROLL. This is just the default mode, it can be changed by calling ```void thumbstick_mode_cycle(bool reverse)``` within code. + +- __THUMBSTICK_SPEED 127:__ Cursor speed in THUMBSTICK_MODE_MOUSE. +- __THUMBSTICK_FINE_SPEED 64:__ Fine cursor speed in THUMBSTICK_MODE_MOUSE (kicks in when slightly nudging the thumbstick). +- __THUMBSTICK_SCROLL_SPEED 1:__ Scrolling speed in THUMBSTICK_MODE_SCROLL. + +- __THUMBSTICK_EIGHT_AXIS true:__ 8-axis toggle for ARROW and SCROLL modes. Disable to fall back to 4 axes (think D-pads vs analog stick). +- __THUMBSTICK_AXIS_SEPARATION 0.5f:__ Float value between 0 and 1, used to discretize the circular range into distinct zones for 8-axis. Imagine the top-right quadrant on a graph, and picture the diagonal. This value indicates the angular "distance" from the diagonal to either axis. Moving from the diagonal to each of the axes, this value changes from 0 to 1. So, a value of 0.5 will "sweep" from the center to half-way towards each axis, creating a zone across the diagonal. Smaller values make narrower diagonal zones, and vice versa. + +#### Thanks + +- @pyrho and u/\_GEIST\_ for the inspiration and initial reference code. +- @zvecr and @drashna for code review and more pointers. diff --git a/keyboards/kyria/keymaps/gotham/config.h b/keyboards/kyria/keymaps/gotham/config.h new file mode 100644 index 0000000000..1b84d996fe --- /dev/null +++ b/keyboards/kyria/keymaps/gotham/config.h @@ -0,0 +1,48 @@ +/* Copyright 2019 Thomas Baart <thomas@splitkb.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#pragma once + +#define EE_HANDS +#define IGNORE_MOD_TAP_INTERRUPT + +// Fix for Elite C rev3 +#define SPLIT_USB_DETECT +// Speed up slave half startup +#define SPLIT_USB_TIMEOUT 1000 + +#ifdef OLED_DRIVER_ENABLE +# define OLED_DISPLAY_128X64 +# define OLED_TIMEOUT 10000 +#endif + +#ifdef RGBLIGHT_ENABLE +# define RGBLIGHT_EFFECT_BREATHING +# define RGBLIGHT_EFFECT_RAINBOW_MOOD +# define RGBLIGHT_EFFECT_RAINBOW_SWIRL +# define RGBLIGHT_EFFECT_KNIGHT +#endif + +#ifdef ENCODER_ENABLE +# define ENCODER_DIRECTION_FLIP +# define ENCODER_RESOLUTION 2 +#endif + +#ifdef THUMBSTICK_ENABLE +# define THUMBSTICK_FLIP_X +# define THUMBSTICK_PIN_X F0 +# define THUMBSTICK_PIN_Y F1 +#endif diff --git a/keyboards/kyria/keymaps/gotham/encoder_utils.c b/keyboards/kyria/keymaps/gotham/encoder_utils.c new file mode 100644 index 0000000000..1c2962339d --- /dev/null +++ b/keyboards/kyria/keymaps/gotham/encoder_utils.c @@ -0,0 +1,94 @@ +#include "encoder_utils.h" + +void encoder_utils_init(void) { + encoder_left_mode = ENC_MODE_VOLUME; + encoder_right_mode = ENC_MODE_LEFT_RIGHT; +} + +void set_encoder_mode(bool left, encoder_mode_t mode) { + if (left) { + encoder_left_mode = mode; + } else { + encoder_right_mode = mode; + } +} + +encoder_mode_t get_encoder_mode(bool left) { + if (left) { + return encoder_left_mode; + } else { + return encoder_right_mode; + } +} + +void cycle_encoder_mode(bool left, bool reverse) { + encoder_mode_t mode = get_encoder_mode(left); + if (reverse) { + mode = (mode == 0) ? (_ENC_MODE_LAST - 1) : (mode - 1); + } else { + mode = (mode == (_ENC_MODE_LAST - 1)) ? 0 : (mode + 1); + } + set_encoder_mode(left, mode); +} + +void encoder_action_volume(uint8_t clockwise) { + if (clockwise) { + tap_code(KC_VOLU); + } else { + tap_code(KC_VOLD); + } +} + +void encoder_action_word_nav(uint8_t clockwise) { + if (clockwise) { + tap_code16(C(KC_RIGHT)); + } else { + tap_code16(C(KC_LEFT)); + } +} + +void encoder_action_left_right(uint8_t clockwise) { + if (clockwise) { + tap_code(KC_RIGHT); + } else { + tap_code(KC_LEFT); + } +} + +void encoder_action_up_down(uint8_t clockwise) { + if (clockwise) { + tap_code(KC_UP); + } else { + tap_code(KC_DOWN); + } +} + +void encoder_action_paging(uint8_t clockwise) { + if (clockwise) { + tap_code(KC_PGUP); + } else { + tap_code(KC_PGDN); + } +} + +void encoder_action(encoder_mode_t mode, uint8_t clockwise) { + switch (mode) { + case ENC_MODE_VOLUME: + encoder_action_volume(clockwise); + break; + case ENC_MODE_WORD_NAV: + encoder_action_word_nav(clockwise); + break; + case ENC_MODE_LEFT_RIGHT: + encoder_action_left_right(clockwise); + break; + case ENC_MODE_UP_DOWN: + encoder_action_up_down(clockwise); + break; + case ENC_MODE_PAGING: + encoder_action_paging(clockwise); + break; + default: + encoder_action_volume(clockwise); + } +} diff --git a/keyboards/kyria/keymaps/gotham/encoder_utils.h b/keyboards/kyria/keymaps/gotham/encoder_utils.h new file mode 100644 index 0000000000..60db6c6bd8 --- /dev/null +++ b/keyboards/kyria/keymaps/gotham/encoder_utils.h @@ -0,0 +1,37 @@ +#pragma once + +#include <stdbool.h> + +#include "quantum.h" + +typedef enum { + ENC_MODE_VOLUME = 0, + ENC_MODE_WORD_NAV, + ENC_MODE_LEFT_RIGHT, + ENC_MODE_UP_DOWN, + ENC_MODE_PAGING, + _ENC_MODE_LAST // Do not use, except for looping through enum values +} encoder_mode_t; + +encoder_mode_t encoder_left_mode; +encoder_mode_t encoder_right_mode; + +void encoder_utils_init(void); + +void set_encoder_mode(bool left, encoder_mode_t mode); + +encoder_mode_t get_encoder_mode(bool left); + +void cycle_encoder_mode(bool left, bool reverse); + +void encoder_action_volume(uint8_t clockwise); + +void encoder_action_word_nav(uint8_t clockwise); + +void encoder_action_left_right(uint8_t clockwise); + +void encoder_action_up_down(uint8_t clockwise); + +void encoder_action_paging(uint8_t clockwise); + +void encoder_action(encoder_mode_t mode, uint8_t clockwise); diff --git a/keyboards/kyria/keymaps/gotham/keycodes.h b/keyboards/kyria/keymaps/gotham/keycodes.h new file mode 100644 index 0000000000..0730d49250 --- /dev/null +++ b/keyboards/kyria/keymaps/gotham/keycodes.h @@ -0,0 +1,16 @@ +#pragma once + +#include "quantum.h" + +enum layers { _QWERTY = 0, _LOWER, _RAISE, _ADJUST }; + +enum custom_keycodes { + ENC_MODE_L = SAFE_RANGE, + ENC_MODE_R, + TMB_MODE, +}; + +#define ESC_RAISE LT(_RAISE, KC_ESC) +#define BSLS_RAISE LT(_RAISE, KC_BSLS) +#define SFT_QUOT MT(MOD_RSFT, KC_QUOT) +#define CTL_MINS MT(MOD_RCTL, KC_MINS) diff --git a/keyboards/kyria/keymaps/gotham/keymap.c b/keyboards/kyria/keymaps/gotham/keymap.c new file mode 100644 index 0000000000..572ea067eb --- /dev/null +++ b/keyboards/kyria/keymaps/gotham/keymap.c @@ -0,0 +1,126 @@ +/* Copyright 2019 Thomas Baart <thomas@splitkb.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include QMK_KEYBOARD_H + +#include "keycodes.h" + +#ifdef ENCODER_ENABLE +# include "encoder_utils.h" +#endif + +#ifdef OLED_DRIVER_ENABLE +# include "oled_utils.h" +#endif + +#ifdef THUMBSTICK_ENABLE +# include "thumbstick.h" +#endif + +// clang-format off +const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { +/* + * Base Layer: QWERTY +*/ + [_QWERTY] = LAYOUT( + ESC_RAISE, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, BSLS_RAISE, + KC_LSFT, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, SFT_QUOT, + KC_LCTL, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_LGUI, KC_NO, TMB_MODE, KC_NO, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, CTL_MINS, + ENC_MODE_L, KC_LALT, LT(_LOWER, KC_SPC), LT(_RAISE, KC_TAB), KC_LSFT, KC_EQL, LT(_RAISE, KC_ENT), LT(_LOWER, KC_BSPC), KC_DEL, ENC_MODE_R + ), +/* + * Lower Layer: Symbols, Navigation + */ + [_LOWER] = LAYOUT( + _______, KC_HASH, KC_DLR, KC_LCBR, KC_RCBR, KC_PIPE, _______, _______, _______, _______, _______, KC_PIPE, + _______, KC_EXLM, KC_AT, KC_LPRN, KC_RPRN, KC_GRV, KC_PGUP, KC_LEFT, KC_UP, KC_RGHT, _______, KC_QUOT, + _______, KC_PERC, KC_CIRC, KC_LBRC, KC_RBRC, KC_TILD, _______, _______, _______, _______, KC_PGDN, KC_HOME, KC_DOWN, KC_END, _______, KC_MINS, + _______, _______, _______, KC_SCLN, KC_EQL, KC_EQL, KC_SCLN, _______, _______, _______ + ), +/* + * Raise Layer: Number keys, media, more symbols + */ + [_RAISE] = LAYOUT( + _______, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, _______, + _______, _______, KC_MPRV, KC_MPLY, KC_MNXT, KC_VOLU, KC_MINS, KC_PLUS, KC_ASTR, KC_SLSH, KC_PERC, _______, + _______, _______, _______, _______, KC_MUTE, KC_VOLD, _______, _______, _______, _______, KC_AMPR, KC_PIPE, KC_COMM, KC_DOT, KC_SLSH, _______, + _______, _______, _______, _______, _______, _______, _______, _______, _______, _______ + ), +/* + * Adjust Layer: Function keys, RGB + */ + [_ADJUST] = LAYOUT( + _______, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, _______, + _______, RGB_TOG, RGB_SAI, RGB_HUI, RGB_VAI, RGB_MOD, _______, _______, _______, KC_F11, KC_F12, _______, + _______, _______, RGB_SAD, RGB_HUD, RGB_VAD, RGB_RMOD,_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, + _______, _______, _______, _______, _______, _______, _______, _______, _______, _______ + ), +}; +// clang-format on + +void matrix_init_user(void) { +#ifdef ENCODER_ENABLE + encoder_utils_init(); +#endif +} + +layer_state_t layer_state_set_user(layer_state_t state) { return update_tri_layer_state(state, _LOWER, _RAISE, _ADJUST); } + +bool process_record_user(uint16_t keycode, keyrecord_t *record) { + switch (keycode) { +#ifdef ENCODER_ENABLE + case ENC_MODE_L: + if (record->event.pressed) { + cycle_encoder_mode(true, false); + } + break; + case ENC_MODE_R: + if (record->event.pressed) { + cycle_encoder_mode(false, false); + } + break; +#endif +#ifdef THUMBSTICK_ENABLE + case TMB_MODE: + if (record->event.pressed) { + thumbstick_mode_cycle(false); + } +#endif + } + return true; +} + +#ifdef OLED_DRIVER_ENABLE +oled_rotation_t oled_init_user(oled_rotation_t rotation) { return OLED_ROTATION_180; } + +void oled_task_user(void) { render_status(); } +#endif + +#ifdef ENCODER_ENABLE +void encoder_update_user(uint8_t index, bool clockwise) { + if (index == 0) { + encoder_action(get_encoder_mode(true), clockwise); +# ifdef OLED_DRIVER_ENABLE + oled_on(); +# endif + } else if (index == 1) { + encoder_action(get_encoder_mode(false), clockwise); +# ifdef OLED_DRIVER_ENABLE + oled_on(); +# endif + } +} +#endif diff --git a/keyboards/kyria/keymaps/gotham/oled_utils.c b/keyboards/kyria/keymaps/gotham/oled_utils.c new file mode 100644 index 0000000000..9d76c97491 --- /dev/null +++ b/keyboards/kyria/keymaps/gotham/oled_utils.c @@ -0,0 +1,103 @@ +#include "oled_utils.h" + + void render_qmk_logo(void) { + static const char PROGMEM qmk_logo[] = { + 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94, + 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, + 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0x00 + }; + + oled_write_P(qmk_logo, false); + } + +void render_layer(void) { + oled_write_P(PSTR("\nLayer: "), false); + switch (get_highest_layer(layer_state)) { + case _QWERTY: + oled_write_P(PSTR("Default\n"), false); + break; + case _LOWER: + oled_write_P(PSTR("Lower\n"), false); + break; + case _RAISE: + oled_write_P(PSTR("Raise\n"), false); + break; + case _ADJUST: + oled_write_P(PSTR("Adjust\n"), false); + break; + default: + oled_write_P(PSTR("???\n"), false); + } +} + +#ifdef ENCODER_ENABLE +void render_encoder(encoder_mode_t mode) { + switch (mode) { + case ENC_MODE_VOLUME: + oled_write_P(PSTR("Volume\n"), false); + break; + case ENC_MODE_WORD_NAV: + oled_write_P(PSTR("Word Nav\n"), false); + break; + case ENC_MODE_LEFT_RIGHT: + oled_write_P(PSTR("Left / Right\n"), false); + break; + case ENC_MODE_UP_DOWN: + oled_write_P(PSTR("Up / Down\n"), false); + break; + case ENC_MODE_PAGING: + oled_write_P(PSTR("PgUp / PgDwn\n"), false); + break; + default: + oled_write_P(PSTR("???\n"), false); + } +} +#endif + +#ifdef THUMBSTICK_ENABLE +void render_thumbstick(thumbstick_mode_t mode) { + switch (mode) { + case THUMBSTICK_MODE_MOUSE: + oled_write_P(PSTR("Mouse"), false); + break; + case THUMBSTICK_MODE_ARROWS: + oled_write_P(PSTR("Arrows"), false); + break; + case THUMBSTICK_MODE_SCROLL: + oled_write_P(PSTR("Scroll"), false); + break; + default: + oled_write_P(PSTR("???\n"), false); + } +} +#endif + +void render_status(void) { + if (is_keyboard_master()) { + // Host Keyboard Layer Status + render_layer(); +#ifdef ENCODER_ENABLE + // Encoder state + oled_write_P(PSTR("L-Enc: "), false); + render_encoder(encoder_left_mode); + oled_write_P(PSTR("R-Enc: "), false); + render_encoder(encoder_right_mode); +#endif +#ifdef THUMBSTICK_ENABLE + if (!isLeftHand) { + // Thumbstick state + oled_write_P(PSTR("Joystick: "), false); + render_thumbstick(thumbstick_state.config.mode); + } +#endif + // Host Keyboard LED Status + led_t led_state = host_keyboard_led_state(); + oled_write_P(led_state.num_lock ? PSTR("NUMLCK ") : PSTR(" "), false); + oled_write_P(led_state.caps_lock ? PSTR("CAPLCK ") : PSTR(" "), false); + oled_write_P(led_state.scroll_lock ? PSTR("SCRLCK ") : PSTR(" "), false); + } else { + // QMK Logo and version information + render_qmk_logo(); + oled_write_P(PSTR("\n Kyria v1.0\n"), false); + } +} diff --git a/keyboards/kyria/keymaps/gotham/oled_utils.h b/keyboards/kyria/keymaps/gotham/oled_utils.h new file mode 100644 index 0000000000..7e3bf44c82 --- /dev/null +++ b/keyboards/kyria/keymaps/gotham/oled_utils.h @@ -0,0 +1,25 @@ +#pragma once + +#include "keycodes.h" + +#ifdef ENCODER_ENABLE +# include "encoder_utils.h" +#endif + +#ifdef THUMBSTICK_ENABLE +# include "thumbstick.h" +#endif + +void render_kyria_logo(void); + +void render_layer(void); + +#ifdef ENCODER_ENABLE +void render_encoder(encoder_mode_t mode); +#endif + +#ifdef THUMBSTICK_ENABLE +void render_thumbstick(thumbstick_mode_t mode); +#endif + +void render_status(void); diff --git a/keyboards/kyria/keymaps/gotham/rules.mk b/keyboards/kyria/keymaps/gotham/rules.mk new file mode 100644 index 0000000000..0bd8badb4d --- /dev/null +++ b/keyboards/kyria/keymaps/gotham/rules.mk @@ -0,0 +1,21 @@ +CONSOLE_ENABLE = yes # Console for debug +ENCODER_ENABLE = yes # ENables the use of one or more encoders +RGBLIGHT_ENABLE = yes # Enable keyboard RGB underglow +MOUSEKEY_ENABLE = no # Mouse keys +OLED_DRIVER_ENABLE = yes # Enables the use of OLED displays +THUMBSTICK_ENABLE = yes # Enables analog thumbstick code + +ifeq ($(strip $(ENCODER_ENABLE)), yes) + SRC += encoder_utils.c +endif + +ifeq ($(strip $(OLED_DRIVER_ENABLE)), yes) + SRC += oled_utils.c +endif + +ifeq ($(strip $(THUMBSTICK_ENABLE)), yes) + POINTING_DEVICE_ENABLE = yes + OPT_DEFS += -DTHUMBSTICK_ENABLE + SRC += analog.c + SRC += thumbstick.c +endif diff --git a/keyboards/kyria/keymaps/gotham/thumbstick.c b/keyboards/kyria/keymaps/gotham/thumbstick.c new file mode 100644 index 0000000000..ec08fb07d4 --- /dev/null +++ b/keyboards/kyria/keymaps/gotham/thumbstick.c @@ -0,0 +1,195 @@ +#include "thumbstick.h" + +void thumbstick_init(void) { + thumbstickTimer = 0; + thumbstickScrollTimer = 0; + + thumbstick_state.config.mode = THUMBSTICK_MODE_MOUSE; + thumbstick_state.config.deadZone = THUMBSTICK_DEAD_ZONE; + thumbstick_state.config.fineZone = THUMBSTICK_FINE_ZONE; + thumbstick_state.config.speed = THUMBSTICK_SPEED; + thumbstick_state.config.fineSpeed = THUMBSTICK_FINE_SPEED; + thumbstick_state.config.axisSeparation = THUMBSTICK_AXIS_SEPARATION; + thumbstick_state.config.eightAxis = THUMBSTICK_EIGHT_AXIS; + +#if defined THUMBSTICK_DEBUG + rawX = 0; + rawY = 0; + distX = 0; + distY = 0; + thumbstickLogTimer = 0; +#endif +} + +// Axis-level wrapper to read raw value, do logging and calculate speed +int16_t thumbstick_get_component(uint8_t pin) { + uint16_t analogValue = analogReadPin(pin); + // Compute direction + bool directionIsPositive = (analogValue > THUMBSTICK_RANGE_CENTER); + // Compute distance from the center + uint16_t distance = directionIsPositive ? (analogValue - THUMBSTICK_RANGE_CENTER) : (THUMBSTICK_RANGE_CENTER - analogValue); +#if defined THUMBSTICK_DEBUG + if (pin == THUMBSTICK_PIN_X) { + rawX = analogValue; + distX = distance; + } else { + rawY = analogValue; + distY = distance; + } +#endif + // Compute component (range of [0 to 1023]) + return directionIsPositive ? distance : -(int16_t)distance; +} + +void thumbstick_mode_set(thumbstick_mode_t mode) { thumbstick_state.config.mode = mode; } + +thumbstick_mode_t thumbstick_mode_get(void) { return thumbstick_state.config.mode; } + +void thumbstick_mode_cycle(bool reverse) { + thumbstick_mode_t mode = thumbstick_mode_get(); + if (reverse) { + mode = (mode == 0) ? (_THUMBSTICK_MODE_LAST - 1) : (mode - 1); + } else { + mode = (mode == (_THUMBSTICK_MODE_LAST - 1)) ? 0 : (mode + 1); + } + thumbstick_mode_set(mode); +} + +// Get mouse speed +int16_t thumbstick_get_mouse_speed(int16_t component) { + int16_t maxSpeed; + uint16_t distance = abs(component); + if (distance > THUMBSTICK_FINE_ZONE) { + maxSpeed = THUMBSTICK_SPEED; + } else if (distance > THUMBSTICK_DEAD_ZONE) { + maxSpeed = THUMBSTICK_FINE_SPEED; + } else { + return 0; + } + return (float)maxSpeed * component / THUMBSTICK_RANGE_CENTER; +} + +// Fix direction within one of 8 axes (or 4 if 8-axis is disabled) +thumbstick_direction_t thumbstick_get_discretized_direction(thumbstick_vector_t vector, float axisSeparation, bool eightAxis) { + thumbstick_direction_t direction; + uint16_t absX = abs(vector.x); + uint16_t absY = abs(vector.y); + uint16_t maxComponent = (absX > absY) ? absX : absY; + bool insideDeadZone = (maxComponent <= THUMBSTICK_DEAD_ZONE); + bool outsideDiagonalZone = ((abs(absX - absY) / (float)maxComponent) >= axisSeparation); + if (insideDeadZone) { + direction.up = direction.down = direction.left = direction.right = false; + } else { + direction.up = (vector.y < 0); + direction.down = (vector.y > 0); + direction.left = (vector.x < 0); + direction.right = (vector.x > 0); + // Let only the dominant direction remain under the right conditions + if (outsideDiagonalZone || !eightAxis) { + if (absX > absY) { + direction.up = direction.down = false; + } else { + direction.left = direction.right = false; + } + } + } + return direction; +} + +thumbstick_direction_t scrollDirection; // Declaring global to save stack space +void thumbstick_process(void) { + if (timer_elapsed(thumbstickTimer) > THUMBSTICK_TIMEOUT) { + thumbstickTimer = timer_read(); +#ifndef THUMBSTICK_FLIP_X + thumbstick_state.vector.x = thumbstick_get_component(THUMBSTICK_PIN_X); +#else + thumbstick_state.vector.x = -thumbstick_get_component(THUMBSTICK_PIN_X); +#endif +#ifndef THUMBSTICK_FLIP_Y + thumbstick_state.vector.y = thumbstick_get_component(THUMBSTICK_PIN_Y); +#else + thumbstick_state.vector.y = -thumbstick_get_component(THUMBSTICK_PIN_Y); +#endif + switch (thumbstick_state.config.mode) { + case THUMBSTICK_MODE_MOUSE: + thumbstick_state.report.x = thumbstick_get_mouse_speed(thumbstick_state.vector.x); + thumbstick_state.report.y = thumbstick_get_mouse_speed(thumbstick_state.vector.y); + break; + case THUMBSTICK_MODE_ARROWS: + thumbstick_state.direction = thumbstick_get_discretized_direction(thumbstick_state.vector, thumbstick_state.config.axisSeparation, thumbstick_state.config.eightAxis); + break; + case THUMBSTICK_MODE_SCROLL: + if (timer_elapsed(thumbstickScrollTimer) > THUMBSTICK_SCROLL_TIMEOUT) { + thumbstickScrollTimer = timer_read(); + scrollDirection = thumbstick_get_discretized_direction(thumbstick_state.vector, thumbstick_state.config.axisSeparation, false); + thumbstick_state.report.v = (scrollDirection.up || scrollDirection.down) ? (scrollDirection.up ? THUMBSTICK_SCROLL_SPEED : -THUMBSTICK_SCROLL_SPEED) : 0; + thumbstick_state.report.h = (scrollDirection.left || scrollDirection.right) ? (scrollDirection.left ? -THUMBSTICK_SCROLL_SPEED : THUMBSTICK_SCROLL_SPEED) : 0; + } else { + thumbstick_state.report.v = thumbstick_state.report.h = 0; + } + break; + default: + break; + } + } +} + +void update_keycode_status(uint16_t keycode, bool last, bool current) { + if (last != current) { + if (current) { + register_code16(keycode); + } else { + unregister_code16(keycode); + } + } +} + +void pointing_device_init(void) { thumbstick_init(); } + +void pointing_device_task(void) { + report_mouse_t report = pointing_device_get_report(); + + if (!isLeftHand) { + thumbstick_process(); + switch (thumbstick_state.config.mode) { + case THUMBSTICK_MODE_MOUSE: + report.x = thumbstick_state.report.x; + report.y = thumbstick_state.report.y; +#ifdef THUMBSTICK_DEBUG + if (timer_elapsed(thumbstickLogTimer) > 100) { + thumbstickLogTimer = timer_read(); + uprintf("Raw (%d, %d); Dist (%u, %u); Vec (%d, %d);\n", rawX, rawY, distX, distY, thumbstick_state.vector.x, thumbstick_state.vector.y); + } +#endif + break; + case THUMBSTICK_MODE_ARROWS: + update_keycode_status(KC_UP, thumbstick_state.lastDirection.up, thumbstick_state.direction.up); + update_keycode_status(KC_DOWN, thumbstick_state.lastDirection.down, thumbstick_state.direction.down); + update_keycode_status(KC_LEFT, thumbstick_state.lastDirection.left, thumbstick_state.direction.left); + update_keycode_status(KC_RIGHT, thumbstick_state.lastDirection.right, thumbstick_state.direction.right); + thumbstick_state.lastDirection = thumbstick_state.direction; +#ifdef THUMBSTICK_DEBUG + if (timer_elapsed(thumbstickLogTimer) > 100) { + thumbstickLogTimer = timer_read(); + uprintf("Up %d; Down %d; Left: %d; Right %d; Vec (%d, %d);\n", direction.up, direction.down, direction.left, direction.right, thumbstick_state.vector.x, thumbstick_state.vector.y); + } +#endif + break; + case THUMBSTICK_MODE_SCROLL: + report.v = thumbstick_state.report.v; + report.h = thumbstick_state.report.h; +#ifdef THUMBSTICK_DEBUG + if (timer_elapsed(thumbstickLogTimer) > 100) { + thumbstickLogTimer = timer_read(); + uprintf("Scroll (%d, %d)\n", report.h, report.v); + } +#endif + break; + default: + break; + } + } + + pointing_device_set_report(report); + pointing_device_send(); +} diff --git a/keyboards/kyria/keymaps/gotham/thumbstick.h b/keyboards/kyria/keymaps/gotham/thumbstick.h new file mode 100644 index 0000000000..aff939c746 --- /dev/null +++ b/keyboards/kyria/keymaps/gotham/thumbstick.h @@ -0,0 +1,98 @@ +#pragma once + +typedef enum { + THUMBSTICK_MODE_MOUSE = 0, + THUMBSTICK_MODE_ARROWS, + THUMBSTICK_MODE_SCROLL, + _THUMBSTICK_MODE_LAST // Do not use, except for looping through enum values +} thumbstick_mode_t; + +// Parameters +#define THUMBSTICK_DEAD_ZONE 90 // Values below this are ignored (deadzone) +#define THUMBSTICK_FINE_ZONE 180 // Values below this enable fine movement + +#define THUMBSTICK_MODE THUMBSTICK_MODE_MOUSE +#define THUMBSTICK_SPEED 256 +#define THUMBSTICK_FINE_SPEED 96 +#define THUMBSTICK_SCROLL_SPEED 1 + +#define THUMBSTICK_EIGHT_AXIS true +#define THUMBSTICK_AXIS_SEPARATION 0.5f + +// Implicit and derived constants +#define THUMBSTICK_TIMEOUT 10 // Mouse report throttling time in ms +#define THUMBSTICK_SCROLL_TIMEOUT 200 // Mouse scroll throttling time in ms +#define THUMBSTICK_RANGE_START 0 +#define THUMBSTICK_RANGE_STOP 1023 +#define THUMBSTICK_RANGE_CENTER (THUMBSTICK_RANGE_STOP - THUMBSTICK_RANGE_START + 1) / 2 +#define THUMBSTICK_RANGE_MOVEMENT (THUMBSTICK_RANGE_CENTER - THUMBSTICK_DEAD_ZONE) + +#include "timer.h" +#include "analog.h" +#include "split_util.h" +#include "pointing_device.h" + +#if defined THUMBSTICK_DEBUG +# include "print.h" +uint16_t rawX; +uint16_t rawY; +uint16_t distX; +uint16_t distY; +uint16_t thumbstickLogTimer; +#endif + +typedef struct { + thumbstick_mode_t mode; + uint16_t deadZone; + uint16_t fineZone; + uint16_t speed; + uint16_t fineSpeed; + float axisSeparation; + bool eightAxis; +} thumbstick_config_t; + +typedef struct { + int16_t x; + int16_t y; +} thumbstick_vector_t; + +typedef struct { + bool up; + bool right; + bool down; + bool left; +} thumbstick_direction_t; + +typedef struct { + thumbstick_config_t config; + thumbstick_vector_t vector; + thumbstick_direction_t direction; + thumbstick_direction_t lastDirection; + report_mouse_t report; +} thumbstick_state_t; + +uint16_t thumbstickTimer; +uint16_t thumbstickScrollTimer; + +thumbstick_state_t thumbstick_state; + +void thumbstick_mode_set(thumbstick_mode_t mode); + +thumbstick_mode_t thumbstick_mode_get(void); + +void thumbstick_mode_cycle(bool reverse); + +void thumbstick_init(void); + +// Axis-level wrapper to read raw value, do logging and calculate speed +int16_t thumbstick_get_component(uint8_t pin); + +// Get mouse speed +int16_t thumbstick_get_mouse_speed(int16_t component); + +// Fix direction within one of 8 axes (or 4 if 8-axis is disabled) +thumbstick_direction_t thumbstick_get_discretized_direction(thumbstick_vector_t vector, float axisSeparation, bool eightAxis); + +void thumbstick_process(void); + +void update_keycode_status(uint16_t keycode, bool last, bool current); |