Skip to content

Commit

Permalink
chording, leader, unicode separated
Browse files Browse the repository at this point in the history
  • Loading branch information
jackhumbert committed Jun 25, 2016
1 parent e2d095f commit 287c8d1
Show file tree
Hide file tree
Showing 10 changed files with 245 additions and 211 deletions.
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ SRC += $(KEYBOARD_FILE) \
$(KEYMAP_FILE) \
$(QUANTUM_DIR)/quantum.c \
$(QUANTUM_DIR)/keymap.c \
$(QUANTUM_DIR)/keycode_config.c
$(QUANTUM_DIR)/keycode_config.c \
$(QUANTUM_DIR)/process_keycode/process_leader.c

ifdef SUBPROJECT
SRC += $(SUBPROJECT_FILE)
Expand All @@ -146,20 +147,27 @@ ifndef CUSTOM_MATRIX
endif

ifeq ($(strip $(MIDI_ENABLE)), yes)
OPT_DEFS += -DMIDI_ENABLE
SRC += $(QUANTUM_DIR)/process_keycode/process_audio.c
endif

ifeq ($(strip $(AUDIO_ENABLE)), yes)
OPT_DEFS += -DAUDIO_ENABLE
SRC += $(QUANTUM_DIR)/process_keycode/process_music.c
SRC += $(QUANTUM_DIR)/audio/audio.c
SRC += $(QUANTUM_DIR)/audio/voices.c
SRC += $(QUANTUM_DIR)/audio/luts.c
endif

ifeq ($(strip $(UNICODE_ENABLE)), yes)
OPT_DEFS += -DUNICODE_ENABLE
SRC += $(QUANTUM_DIR)/process_keycode/process_music.c
endif

ifeq ($(strip $(RGBLIGHT_ENABLE)), yes)
OPT_DEFS += -DRGBLIGHT_ENABLE
SRC += $(QUANTUM_DIR)/light_ws2812.c
SRC += $(QUANTUM_DIR)/rgblight.c
OPT_DEFS += -DRGBLIGHT_ENABLE
endif

# Optimize size but this may cause error "relocation truncated to fit"
Expand Down
60 changes: 60 additions & 0 deletions quantum/process_keycode/process_chording.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "process_chording.h"

bool keys_chord(uint8_t keys[]) {
uint8_t keys_size = sizeof(keys)/sizeof(keys[0]);
bool pass = true;
uint8_t in = 0;
for (uint8_t i = 0; i < chord_key_count; i++) {
bool found = false;
for (uint8_t j = 0; j < keys_size; j++) {
if (chord_keys[i] == (keys[j] & 0xFF)) {
in++; // detects key in chord
found = true;
break;
}
}
if (found)
continue;
if (chord_keys[i] != 0) {
pass = false; // makes sure rest are blank
}
}
return (pass && (in == keys_size));
}

bool process_chording(uint16_t keycode, keyrecord_t *record) {
if (keycode >= QK_CHORDING && keycode <= QK_CHORDING_MAX) {
if (record->event.pressed) {
if (!chording) {
chording = true;
for (uint8_t i = 0; i < CHORDING_MAX; i++)
chord_keys[i] = 0;
chord_key_count = 0;
chord_key_down = 0;
}
chord_keys[chord_key_count] = (keycode & 0xFF);
chord_key_count++;
chord_key_down++;
return false;
} else {
if (chording) {
chord_key_down--;
if (chord_key_down == 0) {
chording = false;
// Chord Dictionary
if (keys_chord((uint8_t[]){KC_ENTER, KC_SPACE})) {
register_code(KC_A);
unregister_code(KC_A);
return false;
}
for (uint8_t i = 0; i < chord_key_count; i++) {
register_code(chord_keys[i]);
unregister_code(chord_keys[i]);
return false;
}
}
}
}
}
return true;
}
16 changes: 16 additions & 0 deletions quantum/process_keycode/process_chording.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef PROCESS_CHORDING_H
#define PROCESS_CHORDING_H

#include "quantum.h"

// Chording stuff
#define CHORDING_MAX 4
bool chording = false;

uint8_t chord_keys[CHORDING_MAX] = {0};
uint8_t chord_key_count = 0;
uint8_t chord_key_down = 0;

bool process_chording(uint16_t keycode, keyrecord_t *record);

#endif
38 changes: 38 additions & 0 deletions quantum/process_keycode/process_leader.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "process_leader.h"

__attribute__ ((weak))
void leader_start(void) {}

__attribute__ ((weak))
void leader_end(void) {}

// Leader key stuff
bool leading = false;
uint16_t leader_time = 0;

uint16_t leader_sequence[5] = {0, 0, 0, 0, 0};
uint8_t leader_sequence_size = 0;

bool process_leader(uint16_t keycode, keyrecord_t *record) {
// Leader key set-up
if (record->event.pressed) {
if (!leading && keycode == KC_LEAD) {
leader_start();
leading = true;
leader_time = timer_read();
leader_sequence_size = 0;
leader_sequence[0] = 0;
leader_sequence[1] = 0;
leader_sequence[2] = 0;
leader_sequence[3] = 0;
leader_sequence[4] = 0;
return false;
}
if (leading && timer_elapsed(leader_time) < LEADER_TIMEOUT) {
leader_sequence[leader_sequence_size] = keycode;
leader_sequence_size++;
return false;
}
}
return true;
}
23 changes: 23 additions & 0 deletions quantum/process_keycode/process_leader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef PROCESS_LEADER_H
#define PROCESS_LEADER_H

#include "quantum.h"

bool process_leader(uint16_t keycode, keyrecord_t *record);

void leader_start(void);
void leader_end(void);

#ifndef LEADER_TIMEOUT
#define LEADER_TIMEOUT 200
#endif
#define SEQ_ONE_KEY(key) if (leader_sequence[0] == (key) && leader_sequence[1] == 0 && leader_sequence[2] == 0 && leader_sequence[3] == 0 && leader_sequence[4] == 0)
#define SEQ_TWO_KEYS(key1, key2) if (leader_sequence[0] == (key1) && leader_sequence[1] == (key2) && leader_sequence[2] == 0 && leader_sequence[3] == 0 && leader_sequence[4] == 0)
#define SEQ_THREE_KEYS(key1, key2, key3) if (leader_sequence[0] == (key1) && leader_sequence[1] == (key2) && leader_sequence[2] == (key3) && leader_sequence[3] == 0 && leader_sequence[4] == 0)
#define SEQ_FOUR_KEYS(key1, key2, key3, key4) if (leader_sequence[0] == (key1) && leader_sequence[1] == (key2) && leader_sequence[2] == (key3) && leader_sequence[3] == (key4) && leader_sequence[4] == 0)
#define SEQ_FIVE_KEYS(key1, key2, key3, key4, key5) if (leader_sequence[0] == (key1) && leader_sequence[1] == (key2) && leader_sequence[2] == (key3) && leader_sequence[3] == (key4) && leader_sequence[4] == (key5))

#define LEADER_EXTERNS() extern bool leading; extern uint16_t leader_time; extern uint16_t leader_sequence[5]; extern uint8_t leader_sequence_size
#define LEADER_DICTIONARY() if (leading && timer_elapsed(leader_time) > LEADER_TIMEOUT)

#endif
57 changes: 57 additions & 0 deletions quantum/process_keycode/process_unicode.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "process_unicode.h"

static uint8_t input_mode;

uint16_t hex_to_keycode(uint8_t hex)
{
if (hex == 0x0) {
return KC_0;
} else if (hex < 0xA) {
return KC_1 + (hex - 0x1);
} else {
return KC_A + (hex - 0xA);
}
}

void set_unicode_mode(uint8_t os_target)
{
input_mode = os_target;
}

bool process_unicode(uint16_t keycode, keyrecord_t *record) {
if (keycode > QK_UNICODE && record->event.pressed) {
uint16_t unicode = keycode & 0x7FFF;
switch(input_mode) {
case UC_OSX:
register_code(KC_LALT);
break;
case UC_LNX:
register_code(KC_LCTL);
register_code(KC_LSFT);
register_code(KC_U);
unregister_code(KC_U);
break;
case UC_WIN:
register_code(KC_LALT);
register_code(KC_PPLS);
unregister_code(KC_PPLS);
break;
}
for(int i = 3; i >= 0; i--) {
uint8_t digit = ((unicode >> (i*4)) & 0xF);
register_code(hex_to_keycode(digit));
unregister_code(hex_to_keycode(digit));
}
switch(input_mode) {
case UC_OSX:
case UC_WIN:
unregister_code(KC_LALT);
break;
case UC_LNX:
unregister_code(KC_LCTL);
unregister_code(KC_LSFT);
break;
}
}
return true;
}
15 changes: 15 additions & 0 deletions quantum/process_keycode/process_unicode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef PROCESS_UNICODE_H
#define PROCESS_UNICODE_H

#include "quantum.h"

#define UC_OSX 0
#define UC_LNX 1
#define UC_WIN 2
#define UC_BSD 3

void set_unicode_input_mode(uint8_t os_target);

bool process_unicode(uint16_t keycode, keyrecord_t *record);

#endif
Loading

0 comments on commit 287c8d1

Please sign in to comment.