Skip to content

Commit

Permalink
Add VIA compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jaygreco committed May 31, 2020
1 parent 70295f8 commit ad8c651
Show file tree
Hide file tree
Showing 9 changed files with 404 additions and 19 deletions.
3 changes: 2 additions & 1 deletion config.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
/* USB Device descriptor parameter */
#define VENDOR_ID 0xFEED
#define PRODUCT_ID 0x6060

#define DEVICE_VER 0x0001
#define MANUFACTURER nullbits
#define PRODUCT nibble
#define PRODUCT NIBBLE
#define DESCRIPTION QMK keyboard firmware

/* key matrix size */
Expand Down
2 changes: 1 addition & 1 deletion keymaps/default/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#define _FN 1

enum custom_keycodes {
KC_CUST,
KC_CUST = SAFE_RANGE,
};

const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
Expand Down
6 changes: 6 additions & 0 deletions keymaps/via/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Custom config starts after VIA's EEPROM usage,
// dynamic keymaps start after this.
// Custom config Usage:
// 1 for enabled encoder modes (1 byte)
// 6 for 3x custom encoder settings, left, right, and press (18 bytes)
#define VIA_EEPROM_CUSTOM_CONFIG_SIZE 19
99 changes: 99 additions & 0 deletions keymaps/via/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@

#include "remote_kb.h"

#define KC_DISC_MUTE KC_F23
#define KC_DISC_DEAF KC_F24
#define NUM_CUST_KEYCODES (_NUM_CUST_KCS - SAFE_RANGE)
#define VIA_KEYCODE_RANGE 0x5F80

enum custom_keycodes {
PROG = SAFE_RANGE,
DISC_MUTE,
DISC_DEAF,
SUPER_ALT_TAB,
_NUM_CUST_KCS,
};

// Macro variables
bool is_alt_tab_active = false;
uint16_t alt_tab_timer = 0;
bool muted = false;
bool deafened = false;

const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
[_BASE] = LAYOUT(
KC_GESC, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, KC_HOME, \
Expand Down Expand Up @@ -42,10 +61,90 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {

};

void map_via_keycode(uint16_t * keycode) {
if (abs(*keycode - VIA_KEYCODE_RANGE) < NUM_CUST_KEYCODES) { //make into macro?
dprintf("VIA custom keycode found, mapping to QMK keycode.\n");
uint16_t new_keycode = (*keycode - VIA_KEYCODE_RANGE) + SAFE_RANGE;
dprintf("VIA KC: %u QMK KC: %u\n", *keycode, new_keycode);
*keycode = new_keycode;
}
}

bool process_record_user(uint16_t keycode, keyrecord_t *record) {
#ifdef VIA_ENABLE
map_via_keycode(&keycode);
#endif
process_record_remote_kb(keycode, record);
switch(keycode) {
case PROG:
if (record->event.pressed) {
rgblight_disable_noeeprom();
bootloader_jump();
}
break;

case DISC_MUTE:
if (record->event.pressed) {
tap_code(KC_DISC_MUTE);
if (!rgblight_is_enabled()) break;

if (muted) {
rgblight_enable_noeeprom();
} else {
rgblight_timer_disable();
uint8_t val = rgblight_get_val();
rgblight_sethsv_range(255, 255, val, 0, 1);
}
muted = !muted;
}
break;

case DISC_DEAF:
if (record->event.pressed) {
tap_code(KC_DISC_DEAF);
if (!rgblight_is_enabled()) break;

if (deafened) {
rgblight_enable_noeeprom();
} else {
rgblight_timer_disable();
uint8_t val = rgblight_get_val();
rgblight_sethsv_range(255, 255, val, 0, RGBLED_NUM-1);
}
deafened = !deafened;
}
break;

case SUPER_ALT_TAB:
if (record->event.pressed) {
if (!is_alt_tab_active) {
is_alt_tab_active = true;
register_code(KC_LALT);
}
alt_tab_timer = timer_read();
register_code(KC_TAB);
} else {
unregister_code(KC_TAB);
}
break;

default:
break;
}
return true;
}

void matrix_init_user(void) {
uart_init(SERIAL_UART_BAUD);
}

void matrix_scan_user(void) {
matrix_scan_remote_kb();

if (is_alt_tab_active) {
if (timer_elapsed(alt_tab_timer) > 1000) {
unregister_code(KC_LALT);
is_alt_tab_active = false;
}
}
}
18 changes: 12 additions & 6 deletions keymaps/via/nibble.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
"productId": "0x6060",
"lighting": "qmk_rgblight",
"customFeatures": ["rotary-encoder"],
"customKeycodes": [
{"name": "Program Keyboard", "title": "Program Keyboard", "shortName": "Prog"},
{"name": "Discord Mute", "title": "Discord Mute", "shortName": "Disc Mute"},
{"name": "Discord Deafen", "title": "Discord Deafen", "shortName": "Disc Deaf"},
{"name": "Super Alt Tab", "title": "Super Alt Tab", "shortName": "S.A.T."}
],
"matrix": {"rows": 5,"cols": 16},
"layouts": {
"labels": [
Expand All @@ -12,12 +18,12 @@
["Right Modifiers", "3x1U", "2x1.5U"]
],
"keymap": [
[{"x":1},"0,1","0,2","0,3","0,4","0,5","0,6","0,7","0,8","0,9","0,10","0,11","0,12","0,13",{"w":2},"0,14","0,15"],
["1,0",{"w":1.5},"1,1","1,2","1,3","1,4","1,5","1,6","1,7","1,8","1,9","1,10","1,11","1,12","1,13",{"w":1.5},"1,14","1,15"],
["2,0",{"w":1.75},"2,1","2,2","2,3","2,4","2,5","2,6","2,7","2,8","2,9","2,10","2,11","2,12",{"w":2.25},"2,14","2,15"],
["3,0",{"w":2.25},"3,1","3,2","3,3","3,4","3,5","3,6","3,7","3,8","3,9","3,10","3,11",{"w":1.75},"3,12","3,14","3,15"],
["4,0",{"w":1.25},"4,1\n\n\n0,0",{"w":1.25},"4,2\n\n\n0,0",{"w":1.25},"4,3\n\n\n0,0",{"w":6.25},"4,8\n\n\n1,0","4,9\n\n\n2,0","4,10\n\n\n2,0","4,11\n\n\n2,0","4,12","4,14","4,15"],
[{"x":1,"w":1.5},"4,1\n\n\n0,1",{"w":1.5},"4,2\n\n\n0,1",{"x":0.75,"w":2.75},"4,8\n\n\n1,1",{"w":1.25},"4,9\n\n\n1,1",{"w":2.25},"4,8\n\n\n1,1",{"w":1.5},"4,10\n\n\n2,1",{"w":1.5},"4,11\n\n\n2,1"]
[{"x":1, "c": "#b0ebff", "a": 7},"0,1",{"c": "#cccccc"},"0,2","0,3","0,4","0,5","0,6","0,7","0,8","0,9","0,10","0,11","0,12","0,13",{"w":2, "c": "#b0ebff"},"0,14","0,15"],
[{"c":"#7d9da8"},"1,0",{"w":1.5,"c": "#b0ebff"},"1,1",{"c": "#cccccc"},"1,2","1,3","1,4","1,5","1,6","1,7","1,8","1,9","1,10","1,11","1,12","1,13",{"w":1.5,"c": "#b0ebff"},"1,14","1,15"],
[{"c":"#7d9da8"},"2,0",{"w":1.75,"c": "#b0ebff"},"2,1",{"c": "#cccccc"},"2,2","2,3","2,4","2,5","2,6","2,7","2,8","2,9","2,10","2,11","2,12",{"w":2.25, "c": "#b0ebff"},"2,14","2,15"],
[{"c":"#7d9da8"},"3,0",{"w":2.25,"c": "#b0ebff"},"3,1",{"c": "#cccccc"},"3,2","3,3","3,4","3,5","3,6","3,7","3,8","3,9","3,10","3,11",{"w":1.75, "c": "#b0ebff"},"3,12","3,14","3,15"],
[{"c":"#7d9da8"},"4,0",{"w":1.25,"c": "#b0ebff"},"4,1\n\n\n0,0",{"w":1.25},"4,2\n\n\n0,0",{"w":1.25},"4,3\n\n\n0,0",{"w":6.25, "c": "#cccccc"},"4,8\n\n\n1,0",{"c": "#b0ebff"},"4,9\n\n\n2,0","4,10\n\n\n2,0","4,11\n\n\n2,0","4,12","4,14","4,15"],
[{"x":1,"w":1.5},"4,1\n\n\n0,1",{"w":1.5},"4,2\n\n\n0,1",{"x":0.75,"w":2.75, "c": "#cccccc"},"4,8\n\n\n1,1",{"w":1.25},"4,9\n\n\n1,1",{"w":2.25},"4,8\n\n\n1,1",{"w":1.5, "c": "#b0ebff"},"4,10\n\n\n2,1",{"w":1.5},"4,11\n\n\n2,1"]
]
}
}
26 changes: 16 additions & 10 deletions nibble.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,10 @@
#include QMK_KEYBOARD_H
#include "bitc_led.h"

void matrix_init_kb(void) {
matrix_init_user();
}
#ifdef VIA_ENABLE
#include "via_extras.h"

void matrix_scan_kb(void) {
matrix_scan_user();
}

bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
return process_record_user(keycode, record);
}
#else

void change_RGB(bool clockwise) {
bool shift = get_mods() & MOD_MASK_SHIFT;
Expand Down Expand Up @@ -70,6 +63,19 @@ void encoder_update_kb(uint8_t index, bool clockwise) {
}
}
}
#endif

void matrix_init_kb(void) {
matrix_init_user();
}

void matrix_scan_kb(void) {
matrix_scan_user();
}

bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
return process_record_user(keycode, record);
}

void led_set_kb(uint8_t usb_led) {
if (usb_led & (1<<USB_LED_CAPS_LOCK))
Expand Down
140 changes: 140 additions & 0 deletions nibble_encoder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#include QMK_KEYBOARD_H
#include "tmk_core/common/eeprom.h"

#define EEPROM_ENABLED_ENCODER_MODES (VIA_EEPROM_CUSTOM_CONFIG_ADDR)
#define EEPROM_CUSTOM_ENCODER (VIA_EEPROM_CUSTOM_CONFIG_ADDR+1)

enum encoder_modes {
ENC_MODE_VOLUME,
ENC_MODE_MEDIA,
ENC_MODE_SCROLL,
ENC_MODE_BRIGHTNESS,
ENC_MODE_BACKLIGHT,
ENC_MODE_CUSTOM0,
ENC_MODE_CUSTOM1,
ENC_MODE_CUSTOM2,
_NUM_ENCODER_MODES,
};

enum custom_encoder_behavior {
ENC_CUSTOM_CW = 0,
ENC_CUSTOM_CCW,
ENC_CUSTOM_PRESS
};

uint8_t encoder_value = 32;
uint8_t encoder_mode = ENC_MODE_VOLUME;
uint8_t enabled_encoder_modes = 0x1F;

uint16_t retrieve_custom_encoder_config(uint8_t encoder_idx, uint8_t behavior) {
#ifdef DYNAMIC_KEYMAP_ENABLE
void* addr = (void*)(EEPROM_CUSTOM_ENCODER + (encoder_idx * 6) + (behavior * 2));
uint16_t keycode = eeprom_read_byte(addr) << 8;
keycode |= eeprom_read_byte(addr + 1);
return keycode;
#else
return 0;
#endif
}

void set_custom_encoder_config(uint8_t encoder_idx, uint8_t behavior, uint16_t new_code) {
#ifdef DYNAMIC_KEYMAP_ENABLE
void* addr = (void*)(EEPROM_CUSTOM_ENCODER + (encoder_idx * 6) + (behavior * 2));
eeprom_update_byte(addr, (uint8_t)(new_code >> 8));
eeprom_update_byte(addr + 1, (uint8_t)(new_code & 0xFF));
#endif
}

void pre_encoder_mode_change(void) {
dprintf("Changing encoder mode.\n", encoder_mode);
}

void post_encoder_mode_change(void) {
dprintf("Encoder mode: %u\n", encoder_mode);
}

//???
void change_encoder_mode(bool clockwise) {
pre_encoder_mode_change();
if(enabled_encoder_modes == 0){
enabled_encoder_modes = 0x1F;
}
do {
if(!clockwise){
if (encoder_mode == 0){
encoder_mode = _NUM_ENCODER_MODES - 1;
} else{
encoder_mode = encoder_mode - 1;
}
} else {
encoder_mode = (encoder_mode + 1) % _NUM_ENCODER_MODES;
}
} while(((1 << encoder_mode) & enabled_encoder_modes) == 0);
post_encoder_mode_change();
}

uint16_t handle_encoder_cw(void) {
dprintf("Encoder mode: %u\n", encoder_mode);
uint16_t mapped_code = 0;
switch(encoder_mode){
default:
break;
case ENC_MODE_VOLUME:
mapped_code = KC_VOLU;
break;
case ENC_MODE_MEDIA:
mapped_code = KC_MEDIA_NEXT_TRACK;
break;
case ENC_MODE_SCROLL:
mapped_code = KC_WH_D;
break;
case ENC_MODE_BACKLIGHT:
mapped_code = RGB_VAI;
break;
#ifdef DYNAMIC_KEYMAP_ENABLE
case ENC_MODE_CUSTOM0:
mapped_code = retrieve_custom_encoder_config(0, ENC_CUSTOM_CW);
break;
case ENC_MODE_CUSTOM1:
mapped_code = retrieve_custom_encoder_config(1, ENC_CUSTOM_CW);
break;
case ENC_MODE_CUSTOM2:
mapped_code = retrieve_custom_encoder_config(2, ENC_CUSTOM_CW);
break;
#endif
}
return mapped_code;
}

uint16_t handle_encoder_ccw(void) {
dprintf("Encoder mode: %u\n", encoder_mode);
uint16_t mapped_code = 0;
switch(encoder_mode){
default:
break;
case ENC_MODE_VOLUME:
mapped_code = KC_VOLD;
break;
case ENC_MODE_MEDIA:
mapped_code = KC_MEDIA_PREV_TRACK;
break;
case ENC_MODE_SCROLL:
mapped_code = KC_WH_U;
break;
case ENC_MODE_BACKLIGHT:
mapped_code = RGB_VAD;
break;
#ifdef DYNAMIC_KEYMAP_ENABLE
case ENC_MODE_CUSTOM0:
mapped_code = retrieve_custom_encoder_config(0, ENC_CUSTOM_CCW);
break;
case ENC_MODE_CUSTOM1:
mapped_code = retrieve_custom_encoder_config(1, ENC_CUSTOM_CCW);
break;
case ENC_MODE_CUSTOM2:
mapped_code = retrieve_custom_encoder_config(2, ENC_CUSTOM_CCW);
break;
#endif
}
return mapped_code;
}
1 change: 0 additions & 1 deletion rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ BLUETOOTH_ENABLE = no # Enable Bluetooth with the Adafruit EZ-Key HID
AUDIO_ENABLE = no # Audio output on port C6
FAUXCLICKY_ENABLE = no # Use buzzer to emulate clicky switches
ENCODER_ENABLE = yes # Enable encoder
# DEBUG_ENABLE = yes # Enable debug
RGBLIGHT_ENABLE = yes
LTO_ENABLE = yes
# OLED_DRIVER_ENABLE = yes
Loading

0 comments on commit ad8c651

Please sign in to comment.