working extended keymap

This commit is contained in:
Jack Humbert 2015-03-20 13:36:13 -04:00
parent afa0f9d0aa
commit 708f95a472
8 changed files with 542 additions and 6 deletions

View file

@ -27,6 +27,7 @@ static action_t keycode_to_action(uint8_t keycode);
/* converts key to action */ /* converts key to action */
__attribute__ ((weak))
action_t action_for_key(uint8_t layer, keypos_t key) action_t action_for_key(uint8_t layer, keypos_t key)
{ {
uint8_t keycode = keymap_key_to_keycode(layer, key); uint8_t keycode = keymap_key_to_keycode(layer, key);
@ -117,8 +118,6 @@ void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
{ {
} }
/* translates keycode to action */ /* translates keycode to action */
static action_t keycode_to_action(uint8_t keycode) static action_t keycode_to_action(uint8_t keycode)
{ {

View file

@ -47,16 +47,28 @@ TOP_DIR = ../..
# Directory keyboard dependent files exist # Directory keyboard dependent files exist
TARGET_DIR = . TARGET_DIR = .
# # project specific files
# SRC = keymap_common.c \
# matrix_handwire.c \
# led.c \
# backlight.c
# ifdef KEYMAP
# SRC := keymap_$(KEYMAP).c $(SRC)
# else
# SRC := keymap_jack.c $(SRC)
# endif
# project specific files # project specific files
SRC = keymap_common.c \ SRC = extended_keymap_common.c \
matrix.c \ matrix_handwire.c \
led.c \ led.c \
backlight.c backlight.c
ifdef KEYMAP ifdef KEYMAP
SRC := keymap_$(KEYMAP).c $(SRC) SRC := extended_keymap_$(KEYMAP).c $(SRC)
else else
SRC := keymap_jack.c $(SRC) SRC := extended_keymap_jack.c $(SRC)
endif endif
CONFIG_H = config.h CONFIG_H = config.h

View file

@ -0,0 +1,171 @@
/*
Copyright 2012,2013 Jun Wako <wakojun@gmail.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 "extended_keymap_common.h"
#include "report.h"
#include "keycode.h"
#include "action_layer.h"
#include "action.h"
#include "action_macro.h"
#include "debug.h"
static action_t keycode_to_action(uint16_t keycode);
/* converts key to action */
action_t action_for_key(uint8_t layer, keypos_t key)
{
uint16_t keycode = keymap_key_to_keycode(layer, key);
// Handle mods in keymap
if (keycode > 0x00FF) {
action_t action;
action.code = ACTION_MODS_KEY(keycode >> 8, keycode & 0xFF);
return action;
}
switch (keycode) {
case KC_FN0 ... KC_FN31:
return keymap_fn_to_action(keycode);
#ifdef BOOTMAGIC_ENABLE
case KC_CAPSLOCK:
case KC_LOCKING_CAPS:
if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
return keycode_to_action(KC_LCTL);
}
return keycode_to_action(keycode);
case KC_LCTL:
if (keymap_config.swap_control_capslock) {
return keycode_to_action(KC_CAPSLOCK);
}
return keycode_to_action(KC_LCTL);
case KC_LALT:
if (keymap_config.swap_lalt_lgui) {
if (keymap_config.no_gui) {
return keycode_to_action(ACTION_NO);
}
return keycode_to_action(KC_LGUI);
}
return keycode_to_action(KC_LALT);
case KC_LGUI:
if (keymap_config.swap_lalt_lgui) {
return keycode_to_action(KC_LALT);
}
if (keymap_config.no_gui) {
return keycode_to_action(ACTION_NO);
}
return keycode_to_action(KC_LGUI);
case KC_RALT:
if (keymap_config.swap_ralt_rgui) {
if (keymap_config.no_gui) {
return keycode_to_action(ACTION_NO);
}
return keycode_to_action(KC_RGUI);
}
return keycode_to_action(KC_RALT);
case KC_RGUI:
if (keymap_config.swap_ralt_rgui) {
return keycode_to_action(KC_RALT);
}
if (keymap_config.no_gui) {
return keycode_to_action(ACTION_NO);
}
return keycode_to_action(KC_RGUI);
case KC_GRAVE:
if (keymap_config.swap_grave_esc) {
return keycode_to_action(KC_ESC);
}
return keycode_to_action(KC_GRAVE);
case KC_ESC:
if (keymap_config.swap_grave_esc) {
return keycode_to_action(KC_GRAVE);
}
return keycode_to_action(KC_ESC);
case KC_BSLASH:
if (keymap_config.swap_backslash_backspace) {
return keycode_to_action(KC_BSPACE);
}
return keycode_to_action(KC_BSLASH);
case KC_BSPACE:
if (keymap_config.swap_backslash_backspace) {
return keycode_to_action(KC_BSLASH);
}
return keycode_to_action(KC_BSPACE);
#endif
default:
return keycode_to_action(keycode);
}
}
/* Macro */
__attribute__ ((weak))
const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
{
return MACRO_NONE;
}
/* Function */
__attribute__ ((weak))
void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
{
}
/* translates keycode to action */
static action_t keycode_to_action(uint16_t keycode)
{
action_t action;
switch (keycode) {
case KC_A ... KC_EXSEL:
case KC_LCTRL ... KC_RGUI:
action.code = ACTION_KEY(keycode);
break;
case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
break;
case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
break;
case KC_MS_UP ... KC_MS_ACCEL2:
action.code = ACTION_MOUSEKEY(keycode);
break;
case KC_TRNS:
action.code = ACTION_TRANSPARENT;
break;
default:
action.code = ACTION_NO;
break;
}
return action;
}
/* translates key to keycode */
uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
{
// return pgm_read_byte(&keymaps[(layer)][(key.row)][(key.col)]);
// This limits it to a byte
return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
}
/* translates Fn keycode to action */
action_t keymap_fn_to_action(uint16_t keycode)
{
return (action_t){ .code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]) };
}

View file

@ -0,0 +1,72 @@
/*
Copyright 2012,2013 Jun Wako <wakojun@gmail.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/>.
*/
#ifndef KEYMAP_H
#define KEYMAP_H
#include <stdint.h>
#include <stdbool.h>
#include "action.h"
#include <avr/pgmspace.h>
#include "keycode.h"
#include "keymap.h"
#include "action_macro.h"
#include "report.h"
#include "host.h"
#include "print.h"
#include "debug.h"
#ifdef BOOTMAGIC_ENABLE
/* NOTE: Not portable. Bit field order depends on implementation */
typedef union {
uint16_t raw;
struct {
bool swap_control_capslock:1;
bool capslock_to_control:1;
bool swap_lalt_lgui:1;
bool swap_ralt_rgui:1;
bool no_gui:1;
bool swap_grave_esc:1;
bool swap_backslash_backspace:1;
bool nkro:1;
};
} keymap_config_t;
keymap_config_t keymap_config;
#endif
/* translates key to keycode */
uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key);
/* translates Fn keycode to action */
action_t keymap_fn_to_action(uint16_t keycode);
extern const uint16_t keymaps[][MATRIX_ROWS][MATRIX_COLS];
extern const uint16_t fn_actions[];
// Ability to use mods in layouts
#define LCTL(kc) kc | 0x0100
#define LSFT(kc) kc | 0x0200
#define LALT(kc) kc | 0x0400
#define LGUI(kc) kc | 0x0800
#define RCTL(kc) kc | 0x1100
#define RSFT(kc) kc | 0x1200
#define RALT(kc) kc | 0x1400
#define RGUI(kc) kc | 0x1800
#define S(kc) LSFT(kc)
#endif

View file

@ -0,0 +1,36 @@
#include "extended_keymap_common.h"
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
[0] = { /* Jack */
{KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_BSPC},
{KC_ESC, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT},
{KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_ENT},
{KC_RSFT, KC_LCTL, KC_LALT, KC_LGUI, KC_FN2, KC_SPC, KC_NO, KC_FN1, KC_LEFT, KC_DOWN, KC_UP, KC_RGHT}
},
[1] = { /* Jack colemak */
{KC_TAB, KC_Q, KC_W, KC_F, KC_P, KC_G, KC_J, KC_L, KC_U, KC_Y, KC_SCLN, KC_BSPC},
{KC_ESC, KC_A, KC_R, KC_S, KC_T, KC_D, KC_H, KC_N, KC_E, KC_I, KC_O, KC_QUOT},
{KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_K, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_ENT},
{KC_FN3, KC_LCTL, KC_LALT, KC_LGUI, KC_FN2, KC_SPC, KC_NO, KC_FN1, KC_LEFT, KC_DOWN, KC_UP, KC_RGHT}
},
[2] = { /* Jack RAISE */
{KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_BSPC},
{KC_TRNS, KC_FN3, KC_FN4, KC_PAUSE, KC_TRNS, KC_TRNS, KC_TRNS, KC_MINS, KC_EQL, KC_LBRC, KC_RBRC, KC_BSLS},
{KC_TRNS, KC_F11, KC_F12, KC_F13, KC_F14, KC_F15, KC_F16, KC_F17, KC_F18, KC_F19, KC_F20, KC_TRNS},
{KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_NO, KC_FN1, KC_MNXT, KC_VOLD, KC_VOLU, KC_MPLY}
},
[3] = { /* Jack LOWER */
{S(KC_GRV), S(KC_1), S(KC_2), S(KC_3), S(KC_4), S(KC_5), S(KC_6), S(KC_7), S(KC_8), S(KC_9), S(KC_0), KC_BSPC},
{KC_TRNS, KC_FN3, KC_FN4, KC_PAUSE, KC_TRNS, KC_TRNS, KC_TRNS, S(KC_MINS), S(KC_EQL), S(KC_LBRC), S(KC_RBRC), S(KC_BSLS)},
{KC_TRNS, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_TRNS},
{KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_FN2, KC_TRNS, KC_NO, KC_TRNS, KC_MNXT, KC_VOLD, KC_VOLU, KC_MPLY}
}
};
const uint16_t PROGMEM fn_actions[] = {
[1] = ACTION_LAYER_MOMENTARY(2), // to Fn overlay
[2] = ACTION_LAYER_MOMENTARY(3), // to Fn overlay
[3] = ACTION_DEFAULT_LAYER_SET(0),
[4] = ACTION_DEFAULT_LAYER_SET(1),
};

View file

@ -0,0 +1,44 @@
#include "keymap_common.h"
const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
[0] = KEYMAP(
ESC, Q, W, E, R, T, Y, U, I, O, P, BSPC,
TAB, A, S, D, F, G, H, J, K, L, SCLN, ENT,
LSFT, Z, X, C, V, B, N, M, COMM, DOT, SLSH, RSFT,
LCTL, LALT, DEL, LGUI, FN2, SPC, FN1, F2, F5, F9, F12),
[2] = KEYMAP( /* RAISE */
TRNS, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, BSPC,
GRV, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, MINS, EQL, LBRC, RBRC, BSLS,
TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, QUOT, FN29, TRNS, TRNS,
TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, FN1, TRNS, TRNS, TRNS, TRNS),
[3] = KEYMAP( /* LOWER */
TRNS, FN10, FN11, FN12, FN13, FN14, FN15, FN16, FN17, FN18, FN19, BSPC,
FN22, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, FN20, FN21, FN23, FN24, FN28,
TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, QUOT, FN29, TRNS, TRNS,
TRNS, TRNS, TRNS, TRNS, FN2, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS),
};
const uint16_t PROGMEM fn_actions[] = {
[1] = ACTION_LAYER_MOMENTARY(2), // to Fn overlay
[2] = ACTION_LAYER_MOMENTARY(3), // to Fn overlay
[3] = ACTION_DEFAULT_LAYER_SET(0),
[4] = ACTION_DEFAULT_LAYER_SET(1),
[10] = ACTION_MODS_KEY(MOD_LSFT, KC_1),
[11] = ACTION_MODS_KEY(MOD_LSFT, KC_2),
[12] = ACTION_MODS_KEY(MOD_LSFT, KC_3),
[13] = ACTION_MODS_KEY(MOD_LSFT, KC_4),
[14] = ACTION_MODS_KEY(MOD_LSFT, KC_5),
[15] = ACTION_MODS_KEY(MOD_LSFT, KC_6),
[16] = ACTION_MODS_KEY(MOD_LSFT, KC_7),
[17] = ACTION_MODS_KEY(MOD_LSFT, KC_8),
[18] = ACTION_MODS_KEY(MOD_LSFT, KC_9),
[19] = ACTION_MODS_KEY(MOD_LSFT, KC_0),
[20] = ACTION_MODS_KEY(MOD_LSFT, KC_MINS),
[21] = ACTION_MODS_KEY(MOD_LSFT, KC_EQL),
[22] = ACTION_MODS_KEY(MOD_LSFT, KC_GRV),
[23] = ACTION_MODS_KEY(MOD_LSFT, KC_LBRC),
[24] = ACTION_MODS_KEY(MOD_LSFT, KC_RBRC),
[28] = ACTION_MODS_KEY(MOD_LSFT, KC_BSLS),
[29] = ACTION_MODS_KEY(MOD_LSFT, KC_QUOT),
};

View file

@ -0,0 +1,202 @@
/*
Copyright 2012 Jun Wako <wakojun@gmail.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/>.
*/
/*
* scan matrix
*/
#include <stdint.h>
#include <stdbool.h>
#include <avr/io.h>
#include <util/delay.h>
#include "action_layer.h"
#include "print.h"
#include "debug.h"
#include "util.h"
#include "matrix.h"
#ifndef DEBOUNCE
# define DEBOUNCE 10
#endif
static uint8_t debouncing = DEBOUNCE;
/* matrix state(1:on, 0:off) */
static matrix_row_t matrix[MATRIX_ROWS];
static matrix_row_t matrix_debouncing[MATRIX_ROWS];
static matrix_row_t read_cols(void);
static void init_cols(void);
static void unselect_rows(void);
static void select_row(uint8_t row);
inline
uint8_t matrix_rows(void)
{
return MATRIX_ROWS;
}
inline
uint8_t matrix_cols(void)
{
return MATRIX_COLS;
}
void matrix_init(void)
{
// initialize row and col
unselect_rows();
init_cols();
// initialize matrix state: all keys off
for (uint8_t i=0; i < MATRIX_ROWS; i++) {
matrix[i] = 0;
matrix_debouncing[i] = 0;
}
}
uint8_t matrix_scan(void)
{
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
select_row(i);
_delay_us(30); // without this wait read unstable value.
matrix_row_t cols = read_cols();
if (matrix_debouncing[i] != cols) {
matrix_debouncing[i] = cols;
if (debouncing) {
debug("bounce!: "); debug_hex(debouncing); debug("\n");
}
debouncing = DEBOUNCE;
}
unselect_rows();
}
if (debouncing) {
if (--debouncing) {
_delay_ms(1);
} else {
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
matrix[i] = matrix_debouncing[i];
}
}
}
return 1;
}
bool matrix_is_modified(void)
{
if (debouncing) return false;
return true;
}
inline
bool matrix_is_on(uint8_t row, uint8_t col)
{
return (matrix[row] & ((matrix_row_t)1<<col));
}
inline
matrix_row_t matrix_get_row(uint8_t row)
{
return matrix[row];
}
void matrix_print(void)
{
print("\nr/c 0123456789ABCDEF\n");
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
phex(row); print(": ");
pbin_reverse16(matrix_get_row(row));
print("\n");
}
}
uint8_t matrix_key_count(void)
{
uint8_t count = 0;
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
count += bitpop16(matrix[i]);
}
return count;
}
/* Column pin configuration
* col: 0 1 2 3 4 5 6 7 8 9 10 11
* pin: F0 F1 F4 F5 F6 F7 B6 B5 B4 D7 D5 D4
*/
static void init_cols(void)
{
DDRF &= ~(1<<0 | 1<<1 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
PORTF |= (1<<0 | 1<<1 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
DDRD &= ~(1<<0);
PORTD |= (1<<0);
DDRB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<7);
PORTB |= (1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<7);
}
static matrix_row_t read_cols(void)
{
return (PINB&(1<<0) ? 0 : (1<< 0)) |
(PINB&(1<<1) ? 0 : (1<< 1)) |
(PINB&(1<<2) ? 0 : (1<< 2)) |
(PINB&(1<<3) ? 0 : (1<< 3)) |
(PINB&(1<<7) ? 0 : (1<< 4)) |
(PIND&(1<<0) ? 0 : (1<< 5)) |
(PINF&(1<<7) ? 0 : (1<< 6)) |
(PINF&(1<<6) ? 0 : (1<< 7)) |
(PINF&(1<<5) ? 0 : (1<< 8)) |
(PINF&(1<<4) ? 0 : (1<< 9)) |
(PINF&(1<<1) ? 0 : (1<<10)) |
(PINF&(1<<0) ? 0 : (1<<11));
}
/* Row pin configuration
* row: 0 1 2 3
* pin: B0 B1 B2 B3
*/
static void unselect_rows(void)
{
// Hi-Z(DDR:0, PORT:0) to unselect
DDRB &= ~0b01110000;
PORTB &= ~0b01110000;
DDRD &= ~0b10000000;
PORTD &= ~0b10000000;
}
static void select_row(uint8_t row)
{
switch (row) {
case 0:
DDRB |= (1<<6);
PORTB &= ~(1<<6);
break;
case 1:
DDRB |= (1<<5);
PORTB &= ~(1<<5);
break;
case 2:
DDRB |= (1<<4);
PORTB &= ~(1<<4);
break;
case 3:
DDRD |= (1<<7);
PORTD &= ~(1<<7);
break;
}
}