summary refs log tree commit diff
path: root/quantum
diff options
context:
space:
mode:
authorQMK Bot <hello@qmk.fm>2021-02-07 23:16:47 +0000
committerQMK Bot <hello@qmk.fm>2021-02-07 23:16:47 +0000
commit3a98bd75c88c5cc64aed72b9d2e3a8857d949fed (patch)
tree7edcbbfa0c0efd16e456d6012f90833922d3bb3c /quantum
parentb8031a1613290ada50f4cb746216fcae90d8935d (diff)
parent99bffc2a21ebed07fd767ad2a9a7e1aadd491ef3 (diff)
Merge remote-tracking branch 'origin/master' into develop
Diffstat (limited to 'quantum')
-rw-r--r--quantum/bitwise.c123
-rw-r--r--quantum/bitwise.h40
-rw-r--r--quantum/command.c786
-rw-r--r--quantum/command.h163
-rw-r--r--quantum/led.h54
-rw-r--r--quantum/matrix.h79
-rw-r--r--quantum/ring_buffer.h44
-rw-r--r--quantum/split_common/transport.h2
-rw-r--r--quantum/util.h26
9 files changed, 1316 insertions, 1 deletions
diff --git a/quantum/bitwise.c b/quantum/bitwise.c
new file mode 100644
index 0000000000..861cca0054
--- /dev/null
+++ b/quantum/bitwise.c
@@ -0,0 +1,123 @@
+/*
+Copyright 2011 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 "util.h"
+
+// bit population - return number of on-bit
+__attribute__((noinline)) uint8_t bitpop(uint8_t bits) {
+    uint8_t c;
+    for (c = 0; bits; c++) bits &= bits - 1;
+    return c;
+    /*
+        const uint8_t bit_count[] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
+        return bit_count[bits>>4] + bit_count[bits&0x0F]
+    */
+}
+
+uint8_t bitpop16(uint16_t bits) {
+    uint8_t c;
+    for (c = 0; bits; c++) bits &= bits - 1;
+    return c;
+}
+
+uint8_t bitpop32(uint32_t bits) {
+    uint8_t c;
+    for (c = 0; bits; c++) bits &= bits - 1;
+    return c;
+}
+
+// most significant on-bit - return highest location of on-bit
+// NOTE: return 0 when bit0 is on or all bits are off
+__attribute__((noinline)) uint8_t biton(uint8_t bits) {
+    uint8_t n = 0;
+    if (bits >> 4) {
+        bits >>= 4;
+        n += 4;
+    }
+    if (bits >> 2) {
+        bits >>= 2;
+        n += 2;
+    }
+    if (bits >> 1) {
+        bits >>= 1;
+        n += 1;
+    }
+    return n;
+}
+
+uint8_t biton16(uint16_t bits) {
+    uint8_t n = 0;
+    if (bits >> 8) {
+        bits >>= 8;
+        n += 8;
+    }
+    if (bits >> 4) {
+        bits >>= 4;
+        n += 4;
+    }
+    if (bits >> 2) {
+        bits >>= 2;
+        n += 2;
+    }
+    if (bits >> 1) {
+        bits >>= 1;
+        n += 1;
+    }
+    return n;
+}
+
+uint8_t biton32(uint32_t bits) {
+    uint8_t n = 0;
+    if (bits >> 16) {
+        bits >>= 16;
+        n += 16;
+    }
+    if (bits >> 8) {
+        bits >>= 8;
+        n += 8;
+    }
+    if (bits >> 4) {
+        bits >>= 4;
+        n += 4;
+    }
+    if (bits >> 2) {
+        bits >>= 2;
+        n += 2;
+    }
+    if (bits >> 1) {
+        bits >>= 1;
+        n += 1;
+    }
+    return n;
+}
+
+__attribute__((noinline)) uint8_t bitrev(uint8_t bits) {
+    bits = (bits & 0x0f) << 4 | (bits & 0xf0) >> 4;
+    bits = (bits & 0b00110011) << 2 | (bits & 0b11001100) >> 2;
+    bits = (bits & 0b01010101) << 1 | (bits & 0b10101010) >> 1;
+    return bits;
+}
+
+uint16_t bitrev16(uint16_t bits) {
+    bits = bitrev(bits & 0x00ff) << 8 | bitrev((bits & 0xff00) >> 8);
+    return bits;
+}
+
+uint32_t bitrev32(uint32_t bits) {
+    bits = (uint32_t)bitrev16(bits & 0x0000ffff) << 16 | bitrev16((bits & 0xffff0000) >> 16);
+    return bits;
+}
diff --git a/quantum/bitwise.h b/quantum/bitwise.h
new file mode 100644
index 0000000000..276bc7437b
--- /dev/null
+++ b/quantum/bitwise.h
@@ -0,0 +1,40 @@
+/*
+Copyright 2011 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/>.
+*/
+
+#pragma once
+
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+uint8_t bitpop(uint8_t bits);
+uint8_t bitpop16(uint16_t bits);
+uint8_t bitpop32(uint32_t bits);
+
+uint8_t biton(uint8_t bits);
+uint8_t biton16(uint16_t bits);
+uint8_t biton32(uint32_t bits);
+
+uint8_t  bitrev(uint8_t bits);
+uint16_t bitrev16(uint16_t bits);
+uint32_t bitrev32(uint32_t bits);
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/quantum/command.c b/quantum/command.c
new file mode 100644
index 0000000000..34c4b36b1c
--- /dev/null
+++ b/quantum/command.c
@@ -0,0 +1,786 @@
+/*
+Copyright 2011 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 <stdint.h>
+#include <stdbool.h>
+#include "wait.h"
+#include "keycode.h"
+#include "host.h"
+#include "keymap.h"
+#include "print.h"
+#include "debug.h"
+#include "util.h"
+#include "timer.h"
+#include "keyboard.h"
+#include "bootloader.h"
+#include "action_layer.h"
+#include "action_util.h"
+#include "eeconfig.h"
+#include "sleep_led.h"
+#include "led.h"
+#include "command.h"
+#include "quantum.h"
+#include "version.h"
+
+#ifdef BACKLIGHT_ENABLE
+#    include "backlight.h"
+#endif
+
+#if defined(MOUSEKEY_ENABLE) && !defined(MK_3_SPEED)
+#    include "mousekey.h"
+#endif
+
+#ifdef AUDIO_ENABLE
+#    include "audio.h"
+#endif /* AUDIO_ENABLE */
+
+static bool command_common(uint8_t code);
+static void command_common_help(void);
+static void print_version(void);
+static void print_status(void);
+static bool command_console(uint8_t code);
+static void command_console_help(void);
+#if defined(MOUSEKEY_ENABLE) && !defined(MK_3_SPEED)
+static bool mousekey_console(uint8_t code);
+static void mousekey_console_help(void);
+#endif
+
+static void switch_default_layer(uint8_t layer);
+
+command_state_t command_state = ONESHOT;
+
+bool command_proc(uint8_t code) {
+    switch (command_state) {
+        case ONESHOT:
+            if (!IS_COMMAND()) return false;
+            return (command_extra(code) || command_common(code));
+            break;
+        case CONSOLE:
+            if (IS_COMMAND())
+                return (command_extra(code) || command_common(code));
+            else
+                return (command_console_extra(code) || command_console(code));
+            break;
+#if defined(MOUSEKEY_ENABLE) && !defined(MK_3_SPEED)
+        case MOUSEKEY:
+            mousekey_console(code);
+            break;
+#endif
+        default:
+            command_state = ONESHOT;
+            return false;
+    }
+    return true;
+}
+
+/* TODO: Refactoring is needed. */
+/* This allows to define extra commands. return false when not processed. */
+bool command_extra(uint8_t code) __attribute__((weak));
+bool command_extra(uint8_t code) {
+    (void)code;
+    return false;
+}
+
+bool command_console_extra(uint8_t code) __attribute__((weak));
+bool command_console_extra(uint8_t code) {
+    (void)code;
+    return false;
+}
+
+/***********************************************************
+ * Command common
+ ***********************************************************/
+static void command_common_help(void) {
+    print("\n\t- Magic -\n" STR(MAGIC_KEY_DEBUG) ":	Debug Message Toggle\n" STR(MAGIC_KEY_DEBUG_MATRIX) ":	Matrix Debug Mode Toggle - Show keypresses in matrix grid\n" STR(MAGIC_KEY_DEBUG_KBD) ":	Keyboard Debug Toggle - Show keypress report\n" STR(MAGIC_KEY_DEBUG_MOUSE) ":	Debug Mouse Toggle\n" STR(MAGIC_KEY_VERSION) ":	Version\n" STR(MAGIC_KEY_STATUS) ":	Status\n" STR(MAGIC_KEY_CONSOLE) ":	Activate Console Mode\n"
+
+#if MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM
+          STR(MAGIC_KEY_LAYER0) ":	Switch to Layer 0\n" STR(MAGIC_KEY_LAYER1) ":	Switch to Layer 1\n" STR(MAGIC_KEY_LAYER2) ":	Switch to Layer 2\n" STR(MAGIC_KEY_LAYER3) ":	Switch to Layer 3\n" STR(MAGIC_KEY_LAYER4) ":	Switch to Layer 4\n" STR(MAGIC_KEY_LAYER5) ":	Switch to Layer 5\n" STR(MAGIC_KEY_LAYER6) ":	Switch to Layer 6\n" STR(MAGIC_KEY_LAYER7) ":	Switch to Layer 7\n" STR(MAGIC_KEY_LAYER8) ":	Switch to Layer 8\n" STR(MAGIC_KEY_LAYER9) ":	Switch to Layer 9\n"
+#endif
+
+#if MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS
+                                                                                                                                                                                                                                                                                                                                                                                                                                                                               "F1-F10:	Switch to Layer 0-9 (F10 = L0)\n"
+#endif
+
+#if MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS
+                                                                                                                                                                                                                                                                                                                                                                                                                                                                               "0-9:	Switch to Layer 0-9\n"
+#endif
+
+          STR(MAGIC_KEY_LAYER0_ALT) ":	Switch to Layer 0 (alternate)\n"
+
+          STR(MAGIC_KEY_BOOTLOADER) ":	Jump to Bootloader\n" STR(MAGIC_KEY_BOOTLOADER_ALT) ":	Jump to Bootloader (alternate)\n"
+
+#ifdef KEYBOARD_LOCK_ENABLE
+          STR(MAGIC_KEY_LOCK) ":	Lock Keyboard\n"
+#endif
+
+          STR(MAGIC_KEY_EEPROM) ":	Print EEPROM Settings\n" STR(MAGIC_KEY_EEPROM_CLEAR) ":	Clear EEPROM\n"
+
+#ifdef NKRO_ENABLE
+          STR(MAGIC_KEY_NKRO) ":	NKRO Toggle\n"
+#endif
+
+#ifdef SLEEP_LED_ENABLE
+          STR(MAGIC_KEY_SLEEP_LED) ":	Sleep LED Test\n"
+#endif
+    );
+}
+
+static void print_version(void) {
+    // print version & information
+    print("\n\t- Version -\n");
+    print("VID: " STR(VENDOR_ID) "(" STR(MANUFACTURER) ") "
+                                                       "PID: " STR(PRODUCT_ID) "(" STR(PRODUCT) ") "
+                                                                                                "VER: " STR(DEVICE_VER) "\n");
+    print("BUILD:  (" __DATE__ ")\n");
+#ifndef SKIP_VERSION
+#    ifdef PROTOCOL_CHIBIOS
+    print("CHIBIOS: " STR(CHIBIOS_VERSION) ", CONTRIB: " STR(CHIBIOS_CONTRIB_VERSION) "\n");
+#    endif
+#endif
+
+    /* build options */
+    print("OPTIONS:"
+
+#ifdef PROTOCOL_LUFA
+          " LUFA"
+#endif
+#ifdef PROTOCOL_VUSB
+          " VUSB"
+#endif
+#ifdef BOOTMAGIC_ENABLE
+          " BOOTMAGIC"
+#endif
+#ifdef MOUSEKEY_ENABLE
+          " MOUSEKEY"
+#endif
+#ifdef EXTRAKEY_ENABLE
+          " EXTRAKEY"
+#endif
+#ifdef CONSOLE_ENABLE
+          " CONSOLE"
+#endif
+#ifdef COMMAND_ENABLE
+          " COMMAND"
+#endif
+#ifdef NKRO_ENABLE
+          " NKRO"
+#endif
+#ifdef LTO_ENABLE
+          " LTO"
+#endif
+
+          " " STR(BOOTLOADER_SIZE) "\n");
+
+    print("GCC: " STR(__GNUC__) "." STR(__GNUC_MINOR__) "." STR(__GNUC_PATCHLEVEL__)
+#if defined(__AVR__)
+              " AVR-LIBC: " __AVR_LIBC_VERSION_STRING__ " AVR_ARCH: avr" STR(__AVR_ARCH__)
+#endif
+                  "\n");
+
+    return;
+}
+
+static void print_status(void) {
+    print("\n\t- Status -\n");
+
+    print_val_hex8(host_keyboard_leds());
+#ifndef PROTOCOL_VUSB
+    // these aren't set on the V-USB protocol, so we just ignore them for now
+    print_val_hex8(keyboard_protocol);
+    print_val_hex8(keyboard_idle);
+#endif
+#ifdef NKRO_ENABLE
+    print_val_hex8(keymap_config.nkro);
+#endif
+    print_val_hex32(timer_read32());
+    return;
+}
+
+static void print_eeconfig(void) {
+// Print these variables if NO_PRINT or USER_PRINT are not defined.
+#if !defined(NO_PRINT) && !defined(USER_PRINT)
+
+    print("default_layer: ");
+    print_dec(eeconfig_read_default_layer());
+    print("\n");
+
+    debug_config_t dc;
+    dc.raw = eeconfig_read_debug();
+    print("debug_config.raw: ");
+    print_hex8(dc.raw);
+    print("\n");
+    print(".enable: ");
+    print_dec(dc.enable);
+    print("\n");
+    print(".matrix: ");
+    print_dec(dc.matrix);
+    print("\n");
+    print(".keyboard: ");
+    print_dec(dc.keyboard);
+    print("\n");
+    print(".mouse: ");
+    print_dec(dc.mouse);
+    print("\n");
+
+    keymap_config_t kc;
+    kc.raw = eeconfig_read_keymap();
+    print("keymap_config.raw: ");
+    print_hex8(kc.raw);
+    print("\n");
+    print(".swap_control_capslock: ");
+    print_dec(kc.swap_control_capslock);
+    print("\n");
+    print(".capslock_to_control: ");
+    print_dec(kc.capslock_to_control);
+    print("\n");
+    print(".swap_lctl_lgui: ");
+    print_dec(kc.swap_lctl_lgui);
+    print("\n");
+    print(".swap_rctl_rgui: ");
+    print_dec(kc.swap_rctl_rgui);
+    print("\n");
+    print(".swap_lalt_lgui: ");
+    print_dec(kc.swap_lalt_lgui);
+    print("\n");
+    print(".swap_ralt_rgui: ");
+    print_dec(kc.swap_ralt_rgui);
+    print("\n");
+    print(".no_gui: ");
+    print_dec(kc.no_gui);
+    print("\n");
+    print(".swap_grave_esc: ");
+    print_dec(kc.swap_grave_esc);
+    print("\n");
+    print(".swap_backslash_backspace: ");
+    print_dec(kc.swap_backslash_backspace);
+    print("\n");
+    print(".nkro: ");
+    print_dec(kc.nkro);
+    print("\n");
+
+#    ifdef BACKLIGHT_ENABLE
+    backlight_config_t bc;
+    bc.raw = eeconfig_read_backlight();
+    print("backlight_config.raw: ");
+    print_hex8(bc.raw);
+    print("\n");
+    print(".enable: ");
+    print_dec(bc.enable);
+    print("\n");
+    print(".level: ");
+    print_dec(bc.level);
+    print("\n");
+#    endif /* BACKLIGHT_ENABLE */
+
+#endif /* !NO_PRINT */
+}
+
+static bool command_common(uint8_t code) {
+#ifdef KEYBOARD_LOCK_ENABLE
+    static host_driver_t *host_driver = 0;
+#endif
+
+    switch (code) {
+#ifdef SLEEP_LED_ENABLE
+
+        // test breathing sleep LED
+        case MAGIC_KC(MAGIC_KEY_SLEEP_LED):
+            print("Sleep LED Test\n");
+            sleep_led_toggle();
+            led_set(host_keyboard_leds());
+            break;
+#endif
+
+        // print stored eeprom config
+        case MAGIC_KC(MAGIC_KEY_EEPROM):
+            print("eeconfig:\n");
+            print_eeconfig();
+            break;
+
+        // clear eeprom
+        case MAGIC_KC(MAGIC_KEY_EEPROM_CLEAR):
+            print("Clearing EEPROM\n");
+            eeconfig_init();
+            break;
+
+#ifdef KEYBOARD_LOCK_ENABLE
+
+        // lock/unlock keyboard
+        case MAGIC_KC(MAGIC_KEY_LOCK):
+            if (host_get_driver()) {
+                host_driver = host_get_driver();
+                clear_keyboard();
+                host_set_driver(0);
+                print("Locked.\n");
+            } else {
+                host_set_driver(host_driver);
+                print("Unlocked.\n");
+            }
+            break;
+#endif
+
+        // print help
+        case MAGIC_KC(MAGIC_KEY_HELP):
+        case MAGIC_KC(MAGIC_KEY_HELP_ALT):
+            command_common_help();
+            break;
+
+        // activate console
+        case MAGIC_KC(MAGIC_KEY_CONSOLE):
+            debug_matrix   = false;
+            debug_keyboard = false;
+            debug_mouse    = false;
+            debug_enable   = false;
+            command_console_help();
+            print("C> ");
+            command_state = CONSOLE;
+            break;
+
+        // jump to bootloader
+        case MAGIC_KC(MAGIC_KEY_BOOTLOADER):
+        case MAGIC_KC(MAGIC_KEY_BOOTLOADER_ALT):
+            print("\n\nJumping to bootloader... ");
+            reset_keyboard();
+            break;
+
+        // debug toggle
+        case MAGIC_KC(MAGIC_KEY_DEBUG):
+            debug_enable = !debug_enable;
+            if (debug_enable) {
+                print("\ndebug: on\n");
+            } else {
+                print("\ndebug: off\n");
+                debug_matrix   = false;
+                debug_keyboard = false;
+                debug_mouse    = false;
+            }
+            break;
+
+        // debug matrix toggle
+        case MAGIC_KC(MAGIC_KEY_DEBUG_MATRIX):
+            debug_matrix = !debug_matrix;
+            if (debug_matrix) {
+                print("\nmatrix: on\n");
+                debug_enable = true;
+            } else {
+                print("\nmatrix: off\n");
+            }
+            break;
+
+        // debug keyboard toggle
+        case MAGIC_KC(MAGIC_KEY_DEBUG_KBD):
+            debug_keyboard = !debug_keyboard;
+            if (debug_keyboard) {
+                print("\nkeyboard: on\n");
+                debug_enable = true;
+            } else {
+                print("\nkeyboard: off\n");
+            }
+            break;
+
+        // debug mouse toggle
+        case MAGIC_KC(MAGIC_KEY_DEBUG_MOUSE):
+            debug_mouse = !debug_mouse;
+            if (debug_mouse) {
+                print("\nmouse: on\n");
+                debug_enable = true;
+            } else {
+                print("\nmouse: off\n");
+            }
+            break;
+
+        // print version
+        case MAGIC_KC(MAGIC_KEY_VERSION):
+            print_version();
+            break;
+
+        // print status
+        case MAGIC_KC(MAGIC_KEY_STATUS):
+            print_status();
+            break;
+
+#ifdef NKRO_ENABLE
+
+        // NKRO toggle
+        case MAGIC_KC(MAGIC_KEY_NKRO):
+            clear_keyboard();  // clear to prevent stuck keys
+            keymap_config.nkro = !keymap_config.nkro;
+            if (keymap_config.nkro) {
+                print("NKRO: on\n");
+            } else {
+                print("NKRO: off\n");
+            }
+            break;
+#endif
+
+            // switch layers
+
+        case MAGIC_KC(MAGIC_KEY_LAYER0_ALT):
+            switch_default_layer(0);
+            break;
+
+#if MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM
+
+        case MAGIC_KC(MAGIC_KEY_LAYER0):
+            switch_default_layer(0);
+            break;
+
+        case MAGIC_KC(MAGIC_KEY_LAYER1):
+            switch_default_layer(1);
+            break;
+
+        case MAGIC_KC(MAGIC_KEY_LAYER2):
+            switch_default_layer(2);
+            break;
+
+        case MAGIC_KC(MAGIC_KEY_LAYER3):
+            switch_default_layer(3);
+            break;
+
+        case MAGIC_KC(MAGIC_KEY_LAYER4):
+            switch_default_layer(4);
+            break;
+
+        case MAGIC_KC(MAGIC_KEY_LAYER5):
+            switch_default_layer(5);
+            break;
+
+        case MAGIC_KC(MAGIC_KEY_LAYER6):
+            switch_default_layer(6);
+            break;
+
+        case MAGIC_KC(MAGIC_KEY_LAYER7):
+            switch_default_layer(7);
+            break;
+
+        case MAGIC_KC(MAGIC_KEY_LAYER8):
+            switch_default_layer(8);
+            break;
+
+        case MAGIC_KC(MAGIC_KEY_LAYER9):
+            switch_default_layer(9);
+            break;
+#endif
+
+#if MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS
+
+        case KC_F1 ... KC_F9:
+            switch_default_layer((code - KC_F1) + 1);
+            break;
+        case KC_F10:
+            switch_default_layer(0);
+            break;
+#endif
+
+#if MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS
+
+        case KC_1 ... KC_9:
+            switch_default_layer((code - KC_1) + 1);
+            break;
+        case KC_0:
+            switch_default_layer(0);
+            break;
+#endif
+
+        default:
+            print("?");
+            return false;
+    }
+    return true;
+}
+
+/***********************************************************
+ * Command console
+ ***********************************************************/
+static void command_console_help(void) {
+    print("\n\t- Console -\n"
+          "ESC/q:	quit\n"
+#ifdef MOUSEKEY_ENABLE
+          "m:	mousekey\n"
+#endif
+    );
+}
+
+static bool command_console(uint8_t code) {
+    switch (code) {
+        case KC_H:
+        case KC_SLASH: /* ? */
+            command_console_help();
+            break;
+        case KC_Q:
+        case KC_ESC:
+            command_state = ONESHOT;
+            return false;
+#if defined(MOUSEKEY_ENABLE) && !defined(MK_3_SPEED)
+        case KC_M:
+            mousekey_console_help();
+            print("M> ");
+            command_state = MOUSEKEY;
+            return true;
+#endif
+        default:
+            print("?");
+            return false;
+    }
+    print("C> ");
+    return true;
+}
+
+#if defined(MOUSEKEY_ENABLE) && !defined(MK_3_SPEED)
+/***********************************************************
+ * Mousekey console
+ ***********************************************************/
+static uint8_t mousekey_param = 0;
+
+static void mousekey_param_print(void) {
+// Print these variables if NO_PRINT or USER_PRINT are not defined.
+#    if !defined(NO_PRINT) && !defined(USER_PRINT)
+    print("\n\t- Values -\n");
+    print("1: delay(*10ms): ");
+    print_dec(mk_delay);
+    print("\n");
+    print("2: interval(ms): ");
+    print_dec(mk_interval);
+    print("\n");
+    print("3: max_speed: ");
+    print_dec(mk_max_speed);
+    print("\n");
+    print("4: time_to_max: ");
+    print_dec(mk_time_to_max);
+    print("\n");
+    print("5: wheel_max_speed: ");
+    print_dec(mk_wheel_max_speed);
+    print("\n");
+    print("6: wheel_time_to_max: ");
+    print_dec(mk_wheel_time_to_max);
+    print("\n");
+#    endif /* !NO_PRINT */
+}
+
+//#define PRINT_SET_VAL(v)  print(#v " = "); print_dec(v); print("\n");
+#    define PRINT_SET_VAL(v) xprintf(#    v " = %d\n", (v))
+static void mousekey_param_inc(uint8_t param, uint8_t inc) {
+    switch (param) {
+        case 1:
+            if (mk_delay + inc < UINT8_MAX)
+                mk_delay += inc;
+            else
+                mk_delay = UINT8_MAX;
+            PRINT_SET_VAL(mk_delay);
+            break;
+        case 2:
+            if (mk_interval + inc < UINT8_MAX)
+                mk_interval += inc;
+            else
+                mk_interval = UINT8_MAX;
+            PRINT_SET_VAL(mk_interval);
+            break;
+        case 3:
+            if (mk_max_speed + inc < UINT8_MAX)
+                mk_max_speed += inc;
+            else
+                mk_max_speed = UINT8_MAX;
+            PRINT_SET_VAL(mk_max_speed);
+            break;
+        case 4:
+            if (mk_time_to_max + inc < UINT8_MAX)
+                mk_time_to_max += inc;
+            else
+                mk_time_to_max = UINT8_MAX;
+            PRINT_SET_VAL(mk_time_to_max);
+            break;
+        case 5:
+            if (mk_wheel_max_speed + inc < UINT8_MAX)
+                mk_wheel_max_speed += inc;
+            else
+                mk_wheel_max_speed = UINT8_MAX;
+            PRINT_SET_VAL(mk_wheel_max_speed);
+            break;
+        case 6:
+            if (mk_wheel_time_to_max + inc < UINT8_MAX)
+                mk_wheel_time_to_max += inc;
+            else
+                mk_wheel_time_to_max = UINT8_MAX;
+            PRINT_SET_VAL(mk_wheel_time_to_max);
+            break;
+    }
+}
+
+static void mousekey_param_dec(uint8_t param, uint8_t dec) {
+    switch (param) {
+        case 1:
+            if (mk_delay > dec)
+                mk_delay -= dec;
+            else
+                mk_delay = 0;
+            PRINT_SET_VAL(mk_delay);
+            break;
+        case 2:
+            if (mk_interval > dec)
+                mk_interval -= dec;
+            else
+                mk_interval = 0;
+            PRINT_SET_VAL(mk_interval);
+            break;
+        case 3:
+            if (mk_max_speed > dec)
+                mk_max_speed -= dec;
+            else
+                mk_max_speed = 0;
+            PRINT_SET_VAL(mk_max_speed);
+            break;
+        case 4:
+            if (mk_time_to_max > dec)
+                mk_time_to_max -= dec;
+            else
+                mk_time_to_max = 0;
+            PRINT_SET_VAL(mk_time_to_max);
+            break;
+        case 5:
+            if (mk_wheel_max_speed > dec)
+                mk_wheel_max_speed -= dec;
+            else
+                mk_wheel_max_speed = 0;
+            PRINT_SET_VAL(mk_wheel_max_speed);
+            break;
+        case 6:
+            if (mk_wheel_time_to_max > dec)
+                mk_wheel_time_to_max -= dec;
+            else
+                mk_wheel_time_to_max = 0;
+            PRINT_SET_VAL(mk_wheel_time_to_max);
+            break;
+    }
+}
+
+static void mousekey_console_help(void) {
+    print("\n\t- Mousekey -\n"
+          "ESC/q:	quit\n"
+          "1:	delay(*10ms)\n"
+          "2:	interval(ms)\n"
+          "3:	max_speed\n"
+          "4:	time_to_max\n"
+          "5:	wheel_max_speed\n"
+          "6:	wheel_time_to_max\n"
+          "\n"
+          "p:	print values\n"
+          "d:	set defaults\n"
+          "up:	+1\n"
+          "down:	-1\n"
+          "pgup:	+10\n"
+          "pgdown:	-10\n"
+          "\n"
+          "speed = delta * max_speed * (repeat / time_to_max)\n");
+    xprintf("where delta: cursor=%d, wheel=%d\n"
+            "See http://en.wikipedia.org/wiki/Mouse_keys\n",
+            MOUSEKEY_MOVE_DELTA, MOUSEKEY_WHEEL_DELTA);
+}
+
+static bool mousekey_console(uint8_t code) {
+    switch (code) {
+        case KC_H:
+        case KC_SLASH: /* ? */
+            mousekey_console_help();
+            break;
+        case KC_Q:
+        case KC_ESC:
+            if (mousekey_param) {
+                mousekey_param = 0;
+            } else {
+                print("C> ");
+                command_state = CONSOLE;
+                return false;
+            }
+            break;
+        case KC_P:
+            mousekey_param_print();
+            break;
+        case KC_1:
+        case KC_2:
+        case KC_3:
+        case KC_4:
+        case KC_5:
+        case KC_6:
+            mousekey_param = numkey2num(code);
+            break;
+        case KC_UP:
+            mousekey_param_inc(mousekey_param, 1);
+            break;
+        case KC_DOWN:
+            mousekey_param_dec(mousekey_param, 1);
+            break;
+        case KC_PGUP:
+            mousekey_param_inc(mousekey_param, 10);
+            break;
+        case KC_PGDN:
+            mousekey_param_dec(mousekey_param, 10);
+            break;
+        case KC_D:
+            mk_delay             = MOUSEKEY_DELAY / 10;
+            mk_interval          = MOUSEKEY_INTERVAL;
+            mk_max_speed         = MOUSEKEY_MAX_SPEED;
+            mk_time_to_max       = MOUSEKEY_TIME_TO_MAX;
+            mk_wheel_max_speed   = MOUSEKEY_WHEEL_MAX_SPEED;
+            mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
+            print("set default\n");
+            break;
+        default:
+            print("?");
+            return false;
+    }
+    if (mousekey_param) {
+        xprintf("M%d> ", mousekey_param);
+    } else {
+        print("M>");
+    }
+    return true;
+}
+#endif
+
+/***********************************************************
+ * Utilities
+ ***********************************************************/
+uint8_t numkey2num(uint8_t code) {
+    switch (code) {
+        case KC_1:
+            return 1;
+        case KC_2:
+            return 2;
+        case KC_3:
+            return 3;
+        case KC_4:
+            return 4;
+        case KC_5:
+            return 5;
+        case KC_6:
+            return 6;
+        case KC_7:
+            return 7;
+        case KC_8:
+            return 8;
+        case KC_9:
+            return 9;
+        case KC_0:
+            return 0;
+    }
+    return 0;
+}
+
+static void switch_default_layer(uint8_t layer) {
+    xprintf("L%d\n", layer);
+    default_layer_set(1UL << layer);
+    clear_keyboard();
+}
diff --git a/quantum/command.h b/quantum/command.h
new file mode 100644
index 0000000000..4f77be085c
--- /dev/null
+++ b/quantum/command.h
@@ -0,0 +1,163 @@
+/*
+Copyright 2011 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/>.
+*/
+
+#pragma once
+
+/* FIXME: Add doxygen comments for the behavioral defines in here. */
+
+/* TODO: Refactoring */
+typedef enum { ONESHOT, CONSOLE, MOUSEKEY } command_state_t;
+extern command_state_t command_state;
+
+/* This allows to extend commands. Return false when command is not processed. */
+bool command_extra(uint8_t code);
+bool command_console_extra(uint8_t code);
+
+#ifdef COMMAND_ENABLE
+uint8_t numkey2num(uint8_t code);
+bool    command_proc(uint8_t code);
+#else
+#    define command_proc(code) false
+#endif
+
+#ifndef IS_COMMAND
+#    define IS_COMMAND() (get_mods() == MOD_MASK_SHIFT)
+#endif
+
+#ifndef MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS
+#    define MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS true
+#endif
+
+#ifndef MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS
+#    define MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS true
+#endif
+
+#ifndef MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM
+#    define MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM false
+#endif
+
+#ifndef MAGIC_KEY_HELP
+#    define MAGIC_KEY_HELP H
+#endif
+
+#ifndef MAGIC_KEY_HELP_ALT
+#    define MAGIC_KEY_HELP_ALT SLASH
+#endif
+
+#ifndef MAGIC_KEY_DEBUG
+#    define MAGIC_KEY_DEBUG D
+#endif
+
+#ifndef MAGIC_KEY_DEBUG_MATRIX
+#    define MAGIC_KEY_DEBUG_MATRIX X
+#endif
+
+#ifndef MAGIC_KEY_DEBUG_KBD
+#    define MAGIC_KEY_DEBUG_KBD K
+#endif
+
+#ifndef MAGIC_KEY_DEBUG_MOUSE
+#    define MAGIC_KEY_DEBUG_MOUSE M
+#endif
+
+#ifndef MAGIC_KEY_VERSION
+#    define MAGIC_KEY_VERSION V
+#endif
+
+#ifndef MAGIC_KEY_STATUS
+#    define MAGIC_KEY_STATUS S
+#endif
+
+#ifndef MAGIC_KEY_CONSOLE
+#    define MAGIC_KEY_CONSOLE C
+#endif
+
+#ifndef MAGIC_KEY_LAYER0
+#    define MAGIC_KEY_LAYER0 0
+#endif
+
+#ifndef MAGIC_KEY_LAYER0_ALT
+#    define MAGIC_KEY_LAYER0_ALT GRAVE
+#endif
+
+#ifndef MAGIC_KEY_LAYER1
+#    define MAGIC_KEY_LAYER1 1
+#endif
+
+#ifndef MAGIC_KEY_LAYER2
+#    define MAGIC_KEY_LAYER2 2
+#endif
+
+#ifndef MAGIC_KEY_LAYER3
+#    define MAGIC_KEY_LAYER3 3
+#endif
+
+#ifndef MAGIC_KEY_LAYER4
+#    define MAGIC_KEY_LAYER4 4
+#endif
+
+#ifndef MAGIC_KEY_LAYER5
+#    define MAGIC_KEY_LAYER5 5
+#endif
+
+#ifndef MAGIC_KEY_LAYER6
+#    define MAGIC_KEY_LAYER6 6
+#endif
+
+#ifndef MAGIC_KEY_LAYER7
+#    define MAGIC_KEY_LAYER7 7
+#endif
+
+#ifndef MAGIC_KEY_LAYER8
+#    define MAGIC_KEY_LAYER8 8
+#endif
+
+#ifndef MAGIC_KEY_LAYER9
+#    define MAGIC_KEY_LAYER9 9
+#endif
+
+#ifndef MAGIC_KEY_BOOTLOADER
+#    define MAGIC_KEY_BOOTLOADER B
+#endif
+
+#ifndef MAGIC_KEY_BOOTLOADER_ALT
+#    define MAGIC_KEY_BOOTLOADER_ALT ESC
+#endif
+
+#ifndef MAGIC_KEY_LOCK
+#    define MAGIC_KEY_LOCK CAPS
+#endif
+
+#ifndef MAGIC_KEY_EEPROM
+#    define MAGIC_KEY_EEPROM E
+#endif
+
+#ifndef MAGIC_KEY_EEPROM_CLEAR
+#    define MAGIC_KEY_EEPROM_CLEAR BSPACE
+#endif
+
+#ifndef MAGIC_KEY_NKRO
+#    define MAGIC_KEY_NKRO N
+#endif
+
+#ifndef MAGIC_KEY_SLEEP_LED
+#    define MAGIC_KEY_SLEEP_LED Z
+
+#endif
+
+#define XMAGIC_KC(key) KC_##key
+#define MAGIC_KC(key) XMAGIC_KC(key)
diff --git a/quantum/led.h b/quantum/led.h
new file mode 100644
index 0000000000..0fe38ea035
--- /dev/null
+++ b/quantum/led.h
@@ -0,0 +1,54 @@
+/*
+Copyright 2011 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/>.
+*/
+
+#pragma once
+
+#include <stdint.h>
+#include <stdbool.h>
+
+/* FIXME: Add doxygen comments here. */
+
+/* keyboard LEDs */
+#define USB_LED_NUM_LOCK 0
+#define USB_LED_CAPS_LOCK 1
+#define USB_LED_SCROLL_LOCK 2
+#define USB_LED_COMPOSE 3
+#define USB_LED_KANA 4
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef union {
+    uint8_t raw;
+    struct {
+        bool    num_lock : 1;
+        bool    caps_lock : 1;
+        bool    scroll_lock : 1;
+        bool    compose : 1;
+        bool    kana : 1;
+        uint8_t reserved : 3;
+    };
+} led_t;
+
+void led_set(uint8_t usb_led);
+
+void led_init_ports(void);
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/quantum/matrix.h b/quantum/matrix.h
new file mode 100644
index 0000000000..ce57010a47
--- /dev/null
+++ b/quantum/matrix.h
@@ -0,0 +1,79 @@
+/*
+Copyright 2011 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/>.
+*/
+
+#pragma once
+
+#include <stdint.h>
+#include <stdbool.h>
+
+#if (MATRIX_COLS <= 8)
+typedef uint8_t matrix_row_t;
+#elif (MATRIX_COLS <= 16)
+typedef uint16_t matrix_row_t;
+#elif (MATRIX_COLS <= 32)
+typedef uint32_t matrix_row_t;
+#else
+#    error "MATRIX_COLS: invalid value"
+#endif
+
+#define MATRIX_ROW_SHIFTER ((matrix_row_t)1)
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* number of matrix rows */
+uint8_t matrix_rows(void);
+/* number of matrix columns */
+uint8_t matrix_cols(void);
+/* should be called at early stage of startup before matrix_init.(optional) */
+void matrix_setup(void);
+/* intialize matrix for scaning. */
+void matrix_init(void);
+/* scan all key states on matrix */
+uint8_t matrix_scan(void);
+/* whether modified from previous scan. used after matrix_scan. */
+bool matrix_is_modified(void) __attribute__((deprecated));
+/* whether a switch is on */
+bool matrix_is_on(uint8_t row, uint8_t col);
+/* matrix state on row */
+matrix_row_t matrix_get_row(uint8_t row);
+/* print matrix for debug */
+void matrix_print(void);
+/* delay between changing matrix pin state and reading values */
+void matrix_output_select_delay(void);
+void matrix_output_unselect_delay(void);
+/* only for backwards compatibility. delay between changing matrix pin state and reading values */
+void matrix_io_delay(void);
+
+/* power control */
+void matrix_power_up(void);
+void matrix_power_down(void);
+
+/* executes code for Quantum */
+void matrix_init_quantum(void);
+void matrix_scan_quantum(void);
+
+void matrix_init_kb(void);
+void matrix_scan_kb(void);
+
+void matrix_init_user(void);
+void matrix_scan_user(void);
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/quantum/ring_buffer.h b/quantum/ring_buffer.h
new file mode 100644
index 0000000000..284745ca8e
--- /dev/null
+++ b/quantum/ring_buffer.h
@@ -0,0 +1,44 @@
+#pragma once
+
+#include <util/atomic.h>
+#include <stdint.h>
+#include <stdbool.h>
+
+#ifndef RBUF_SIZE
+#    define RBUF_SIZE 32
+#endif
+
+static uint8_t     rbuf[RBUF_SIZE];
+static uint8_t     rbuf_head = 0;
+static uint8_t     rbuf_tail = 0;
+static inline bool rbuf_enqueue(uint8_t data) {
+    bool ret = false;
+    ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
+        uint8_t next = (rbuf_head + 1) % RBUF_SIZE;
+        if (next != rbuf_tail) {
+            rbuf[rbuf_head] = data;
+            rbuf_head       = next;
+            ret             = true;
+        }
+    }
+    return ret;
+}
+static inline uint8_t rbuf_dequeue(void) {
+    uint8_t val = 0;
+    ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
+        if (rbuf_head != rbuf_tail) {
+            val       = rbuf[rbuf_tail];
+            rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE;
+        }
+    }
+
+    return val;
+}
+static inline bool rbuf_has_data(void) {
+    bool has_data;
+    ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { has_data = (rbuf_head != rbuf_tail); }
+    return has_data;
+}
+static inline void rbuf_clear(void) {
+    ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { rbuf_head = rbuf_tail = 0; }
+}
diff --git a/quantum/split_common/transport.h b/quantum/split_common/transport.h
index f3e752bf9b..c667bfab85 100644
--- a/quantum/split_common/transport.h
+++ b/quantum/split_common/transport.h
@@ -1,6 +1,6 @@
 #pragma once
 
-#include "common/matrix.h"
+#include "matrix.h"
 
 void transport_master_init(void);
 void transport_slave_init(void);
diff --git a/quantum/util.h b/quantum/util.h
new file mode 100644
index 0000000000..bef3b9abe3
--- /dev/null
+++ b/quantum/util.h
@@ -0,0 +1,26 @@
+/*
+Copyright 2011 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/>.
+*/
+#pragma once
+
+#include "bitwise.h"
+
+// convert to L string
+#define LSTR(s) XLSTR(s)
+#define XLSTR(s) L## #s
+// convert to string
+#define STR(s) XSTR(s)
+#define XSTR(s) #s