vial: initial encoder support

This commit is contained in:
Ilya Zhuravlev 2020-12-06 19:23:57 -05:00
parent 71280fe884
commit 10e15c09a4
6 changed files with 110 additions and 9 deletions

View File

@ -375,6 +375,10 @@ $(KEYMAP_OUTPUT)/vial_generated_keyboard_definition.h: $(KEYMAP_PATH)/vial.json
python3 util/vial_generate_definition.py $(KEYMAP_PATH)/vial.json $(KEYMAP_OUTPUT)/vial_generated_keyboard_definition.h
endif
ifeq ($(strip $(VIAL_ENCODERS_ENABLE)), yes)
OPT_DEFS += -DVIAL_ENCODERS_ENABLE
endif
ifeq ($(strip $(DYNAMIC_KEYMAP_ENABLE)), yes)
OPT_DEFS += -DDYNAMIC_KEYMAP_ENABLE
SRC += $(QUANTUM_DIR)/dynamic_keymap.c

View File

@ -22,10 +22,6 @@
#include "dynamic_keymap.h"
#include "via.h" // for default VIA_EEPROM_ADDR_END
#ifndef DYNAMIC_KEYMAP_LAYER_COUNT
# define DYNAMIC_KEYMAP_LAYER_COUNT 4
#endif
#ifndef DYNAMIC_KEYMAP_MACRO_COUNT
# define DYNAMIC_KEYMAP_MACRO_COUNT 16
#endif
@ -57,9 +53,20 @@
# endif
#endif
// Dynamic macro starts after dynamic keymaps
// Encoders are located right after the dynamic keymap
#define VIAL_ENCODERS_EEPROM_ADDR (DYNAMIC_KEYMAP_EEPROM_ADDR + (DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2))
#ifdef VIAL_ENCODERS_ENABLE
#define NUMBER_OF_ENCODERS (sizeof(encoders_pad_a) / sizeof(pin_t))
static pin_t encoders_pad_a[] = ENCODERS_PAD_A;
#define VIAL_ENCODERS_SIZE (NUMBER_OF_ENCODERS * DYNAMIC_KEYMAP_LAYER_COUNT * 2 * 2)
#else
#define VIAL_ENCODERS_SIZE 0
#endif
// Dynamic macro starts after encoders, or dynamic keymaps if encoders aren't enabled
#ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR
# define DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR (DYNAMIC_KEYMAP_EEPROM_ADDR + (DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2))
# define DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR (VIAL_ENCODERS_EEPROM_ADDR + VIAL_ENCODERS_SIZE)
#endif
// Sanity check that dynamic keymaps fit in available EEPROM
@ -67,9 +74,7 @@
// The keyboard should override DYNAMIC_KEYMAP_LAYER_COUNT to reduce it,
// or DYNAMIC_KEYMAP_EEPROM_MAX_ADDR to increase it, *only if* the microcontroller has
// more than the default.
#if DYNAMIC_KEYMAP_EEPROM_MAX_ADDR - DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR < 100
# error Dynamic keymaps are configured to use more EEPROM than is available.
#endif
_Static_assert(DYNAMIC_KEYMAP_EEPROM_MAX_ADDR - DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR >= 100, "Dynamic keymaps are configured to use more EEPROM than is available.");
// Dynamic macros are stored after the keymaps and use what is available
// up to and including DYNAMIC_KEYMAP_EEPROM_MAX_ADDR.
@ -99,6 +104,31 @@ void dynamic_keymap_set_keycode(uint8_t layer, uint8_t row, uint8_t column, uint
eeprom_update_byte(address + 1, (uint8_t)(keycode & 0xFF));
}
#ifdef VIAL_ENCODERS_ENABLE
static void *dynamic_keymap_encoder_to_eeprom_address(uint8_t layer, uint8_t idx, uint8_t dir) {
return ((void *)VIAL_ENCODERS_EEPROM_ADDR) + (layer * NUMBER_OF_ENCODERS * 2 * 2) + (idx * 2 * 2) + dir * 2;
}
uint16_t dynamic_keymap_get_encoder(uint8_t layer, uint8_t idx, uint8_t dir) {
if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || idx >= NUMBER_OF_ENCODERS || dir > 1)
return 0;
void *address = dynamic_keymap_encoder_to_eeprom_address(layer, idx, dir);
uint16_t keycode = eeprom_read_byte(address) << 8;
keycode |= eeprom_read_byte(address + 1);
return keycode;
}
void dynamic_keymap_set_encoder(uint8_t layer, uint8_t idx, uint8_t dir, uint16_t keycode) {
if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || idx >= NUMBER_OF_ENCODERS || dir > 1)
return;
void *address = dynamic_keymap_encoder_to_eeprom_address(layer, idx, dir);
eeprom_update_byte(address, (uint8_t)(keycode >> 8));
eeprom_update_byte(address + 1, (uint8_t)(keycode & 0xFF));
}
#endif
void dynamic_keymap_reset(void) {
// Reset the keymaps in EEPROM to what is in flash.
// All keyboards using dynamic keymaps should define a layout
@ -109,6 +139,13 @@ void dynamic_keymap_reset(void) {
dynamic_keymap_set_keycode(layer, row, column, pgm_read_word(&keymaps[layer][row][column]));
}
}
#ifdef VIAL_ENCODERS_ENABLE
for (int idx = 0; idx < NUMBER_OF_ENCODERS; ++idx) {
dynamic_keymap_set_encoder(layer, idx, 0, KC_TRNS);
dynamic_keymap_set_encoder(layer, idx, 1, KC_TRNS);
}
#endif
}
}

View File

@ -18,10 +18,18 @@
#include <stdint.h>
#include <stdbool.h>
#ifndef DYNAMIC_KEYMAP_LAYER_COUNT
# define DYNAMIC_KEYMAP_LAYER_COUNT 4
#endif
uint8_t dynamic_keymap_get_layer_count(void);
void * dynamic_keymap_key_to_eeprom_address(uint8_t layer, uint8_t row, uint8_t column);
uint16_t dynamic_keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t column);
void dynamic_keymap_set_keycode(uint8_t layer, uint8_t row, uint8_t column, uint16_t keycode);
#ifdef VIAL_ENCODERS_ENABLE
uint16_t dynamic_keymap_get_encoder(uint8_t layer, uint8_t idx, uint8_t dir);
void dynamic_keymap_set_encoder(uint8_t layer, uint8_t idx, uint8_t dir, uint16_t keycode);
#endif
void dynamic_keymap_reset(void);
// These get/set the keycodes as stored in the EEPROM buffer
// Data is big-endian 16-bit values (the keycodes)

View File

@ -60,6 +60,11 @@ __attribute__((weak)) void encoder_update_user(int8_t index, bool clockwise) {}
__attribute__((weak)) void encoder_update_kb(int8_t index, bool clockwise) { encoder_update_user(index, clockwise); }
#ifdef VIAL_ENCODERS_ENABLE
#include "vial.h"
#define encoder_update_kb vial_encoder_update
#endif
void encoder_init(void) {
#if defined(SPLIT_KEYBOARD) && defined(ENCODERS_PAD_A_RIGHT) && defined(ENCODERS_PAD_B_RIGHT)
if (!isLeftHand) {

View File

@ -20,11 +20,15 @@
#include "protocol/usb_descriptor.h"
#include "vial_generated_keyboard_definition.h"
#include "dynamic_keymap.h"
#include "quantum.h"
enum {
vial_get_keyboard_id = 0x00,
vial_get_size = 0x01,
vial_get_def = 0x02,
vial_get_encoder = 0x03,
vial_set_encoder = 0x04,
};
void vial_handle_cmd(uint8_t *msg, uint8_t length) {
@ -66,5 +70,43 @@ void vial_handle_cmd(uint8_t *msg, uint8_t length) {
memcpy_P(msg, &keyboard_definition[start], end - start);
break;
}
#ifdef VIAL_ENCODERS_ENABLE
case vial_get_encoder: {
uint8_t layer = msg[2];
uint8_t idx = msg[3];
uint16_t keycode = dynamic_keymap_get_encoder(layer, idx, 0);
msg[0] = keycode >> 8;
msg[1] = keycode & 0xFF;
keycode = dynamic_keymap_get_encoder(layer, idx, 1);
msg[2] = keycode >> 8;
msg[3] = keycode & 0xFF;
break;
}
case vial_set_encoder: {
dynamic_keymap_set_encoder(msg[2], msg[3], msg[4], (msg[5] << 8) | msg[6]);
break;
}
#endif
}
}
#ifdef VIAL_ENCODERS_ENABLE
void vial_encoder_update(uint8_t index, bool clockwise) {
uint16_t code;
layer_state_t layers = layer_state | default_layer_state;
/* check top layer first */
for (int8_t i = MAX_LAYER - 1; i >= 0; i--) {
if (layers & (1UL << i)) {
code = dynamic_keymap_get_encoder(i, index, clockwise);
if (code != KC_TRNS) {
tap_code16(code);
return;
}
}
}
/* fall back to layer 0 */
code = dynamic_keymap_get_encoder(0, index, clockwise);
tap_code16(code);
}
#endif

View File

@ -17,7 +17,12 @@
#pragma once
#include <inttypes.h>
#include <stdbool.h>
#define VIAL_PROTOCOL_VERSION ((uint32_t)0x00000000)
void vial_handle_cmd(uint8_t *data, uint8_t length);
#ifdef VIAL_ENCODERS_ENABLE
void vial_encoder_update(uint8_t index, bool clockwise);
#endif