#include QMK_KEYBOARD_H
#ifdef AUDIO_ENABLE
#include "audio.h"
#endif
enum mitosis_layers
{
_QWERTY,
_SHIFTED,
_FUNCTIONPC,
_FUNCTIONMAC,
_FUNCSHIFT,
_ADJUST
};
enum mitosis_keycodes
{
FNKEY = SAFE_RANGE,
SHIFT,
MACSLEEP,
FNMAC,
FNPC,
AUDIOTEST,
DYNAMIC_MACRO_RANGE,
};
enum mitosis_macros
{
VOLU,
VOLD,
ESCM
};
#include "dynamic_macro.h"
#define LONGPRESS_DELAY 150
#define LAYER_TOGGLE_DELAY 900
#define __MOD__ KC_TRNS
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
[_QWERTY] = LAYOUT(
KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P,
KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN,
SFT_T(KC_Z), KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, SFT_T(KC_SLSH),
KC_LCTL, M(ESCM), KC_TAB, KC_QUOT, KC_LEFT, KC_DOWN, KC_UP, KC_RIGHT,
KC_LALT, KC_LGUI, KC_SPC, SHIFT, FNKEY, KC_BSPC, KC_ENT, MO(_ADJUST)
),
[_SHIFTED] = LAYOUT(
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
__MOD__, KC_DEL, _______, _______, _______, _______, _______, _______,
__MOD__, __MOD__, _______, __MOD__, __MOD__, _______, _______, XXXXXXX
),
[_FUNCTIONPC] = LAYOUT(
KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0,
LCTL(KC_A), LCTL(KC_S), _______, LCTL(KC_F),_______, _______, KC_MINS, KC_EQL, KC_LBRC, KC_RBRC,
LCTL(KC_Z), LCTL(KC_X), LCTL(KC_C), LCTL(KC_V),_______, _______, KC_BSLS, _______, _______, KC_QUOT,
__MOD__, KC_DEL, _______, KC_GRV, KC_HOME, KC_PGDN, KC_PGUP, KC_END,
__MOD__, __MOD__, _______, __MOD__, __MOD__, _______, KC_PSCR, XXXXXXX
),
[_FUNCTIONMAC] = LAYOUT(
KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0,
LGUI(KC_A), LGUI(KC_S), _______, LGUI(KC_F),_______, _______, KC_MINS, KC_EQL, KC_LBRC, KC_RBRC,
LGUI(KC_Z), LGUI(KC_X), LGUI(KC_C), LGUI(KC_V),_______, _______, KC_BSLS, _______, _______, KC_QUOT,
__MOD__, KC_DEL, _______, KC_GRV, KC_HOME, KC_PGDN, KC_PGUP, KC_END,
__MOD__, __MOD__, _______, __MOD__, __MOD__, _______, KC_PSCR, XXXXXXX
),
[_FUNCSHIFT] = LAYOUT(
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, _______, _______, KC_LCBR, KC_RCBR,
KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, _______, _______, _______, _______,
__MOD__, KC_DEL, _______, KC_TILD, _______, _______, _______, _______,
__MOD__, __MOD__, _______, __MOD__, __MOD__, _______, _______, _______
),
[_ADJUST] = LAYOUT(
RESET, FNPC, _______, _______, _______, _______, DYN_REC_START1, DYN_REC_START2, _______, _______,
FNMAC, _______, AU_ON, AU_OFF, _______, _______, _______, _______, MACSLEEP, _______,
MUV_DE, MUV_IN, MU_ON, MU_OFF, _______, _______, KC_MUTE, KC_MPRV, KC_MNXT, KC_MPLY,
__MOD__, _______, _______, _______, _______, DYN_MACRO_PLAY1, DYN_MACRO_PLAY2, _______,
__MOD__, __MOD__, _______, __MOD__, __MOD__, _______, _______, __MOD__
)
};
#ifdef AUDIO_ENABLE
float tone_startup[][2] = SONG(STARTUP_SOUND);
float tone_qwerty[][2] = SONG(QWERTY_SOUND);
float tone_dyn_macro_rec[][2] = SONG(DVORAK_SOUND);
float tone_dyn_macro_play[][2] = SONG(COLEMAK_SOUND);
float tone_fnpc[][2] = SONG(PLOVER_SOUND);
float tone_fnmac[][2] = SONG(PLOVER_GOODBYE_SOUND);
float music_scale[][2] = SONG(MUSIC_SCALE_SOUND);
float tone_goodbye[][2] = SONG(GOODBYE_SOUND);
#endif
static uint16_t function_layer = _FUNCTIONMAC;
static uint16_t key_timer;
void persistent_function_layer_set(uint16_t new_function_layer) {
function_layer = new_function_layer;
}
const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
{
switch(id) {
case VOLU:
if (record->event.pressed) {
key_timer = timer_read(); } else { if (timer_elapsed(key_timer) > LONGPRESS_DELAY) { return MACRO(T(MPLY), END);
} else {
return MACRO(T(VOLU), END);
}
}
break;
case VOLD:
if (record->event.pressed) {
key_timer = timer_read();
} else {
if (timer_elapsed(key_timer) > LONGPRESS_DELAY) {
return MACRO(T(MNXT), END);
} else {
return MACRO(T(VOLD), END);
}
}
break;
case ESCM:
if (record->event.pressed) {
key_timer = timer_read();
} else {
if (timer_elapsed(key_timer) > LONGPRESS_DELAY) {
return MACRO(T(APP), END);
} else {
return MACRO(T(ESC), END);
}
}
break;
}
return MACRO_NONE;
};
static bool singular_key = false;
bool process_record_user(uint16_t keycode, keyrecord_t *record) {
uint16_t macro_kc = (keycode == MO(_ADJUST) ? DYN_REC_STOP : keycode);
if (!process_record_dynamic_macro(macro_kc, record)) {
return false;
}
uint8_t layer;
layer = biton32(layer_state);
switch (keycode) {
case MACSLEEP:
if (record->event.pressed) {
register_code(KC_RSFT);
register_code(KC_RCTL);
register_code(KC_POWER);
unregister_code(KC_POWER);
unregister_code(KC_RCTL);
unregister_code(KC_RSFT);
}
return false;
break;
case FNKEY:
if (record->event.pressed) {
key_timer = timer_read();
singular_key = true;
layer_on(function_layer);
} else {
if (timer_elapsed(key_timer) < LAYER_TOGGLE_DELAY || !singular_key) {
layer_off(function_layer);
}
}
update_tri_layer(function_layer, _SHIFTED, _FUNCSHIFT);
return false;
break;
case SHIFT:
if (record->event.pressed) {
key_timer = timer_read();
singular_key = true;
layer_on(_SHIFTED);
register_code(KC_LSFT);
} else {
if (timer_elapsed(key_timer) < LAYER_TOGGLE_DELAY || !singular_key) {
layer_off(_SHIFTED);
unregister_code(KC_LSFT);
}
}
update_tri_layer(function_layer, _SHIFTED, _FUNCSHIFT);
return false;
break;
case FNPC:
if (record->event.pressed) {
persistent_function_layer_set(_FUNCTIONPC);
#ifdef AUDIO_ENABLE
PLAY_SONG(tone_fnpc);
#endif
}
return false;
break;
case FNMAC:
if (record->event.pressed) {
persistent_function_layer_set(_FUNCTIONMAC);
#ifdef AUDIO_ENABLE
PLAY_SONG(tone_fnmac);
#endif
}
return false;
break;
case AUDIOTEST:
if (record->event.pressed) {
#ifdef AUDIO_ENABLE
PLAY_SONG(music_scale);
register_code(KC_M);
unregister_code(KC_M);
#endif
register_code(KC_A);
} else {
unregister_code(KC_A);
}
return false;
break;
default:
singular_key = false;
break;
}
if (layer == _FUNCSHIFT) {
if ( (keycode >= KC_F1 && keycode <= KC_F12)
|| keycode == KC_RBRC ) {
if (record->event.pressed) {
unregister_mods(MOD_LSFT);
} else {
register_mods(MOD_LSFT);
}
}
}
return true;
};
#ifdef AUDIO_ENABLE
void startup_user()
{
_delay_ms(20); PLAY_SONG(tone_startup);
}
void shutdown_user()
{
PLAY_SONG(tone_goodbye);
_delay_ms(150);
stop_all_notes();
}
void music_on_user(void)
{
music_scale_user();
}
void music_scale_user(void)
{
PLAY_SONG(music_scale);
}
#endif
void matrix_scan_user(void) {
uint8_t layer = biton32(layer_state);
switch (layer) {
case _QWERTY:
set_led_off;
break;
case _FUNCTIONMAC:
set_led_blue;
break;
case _FUNCTIONPC:
set_led_cyan;
break;
case _SHIFTED:
set_led_red;
break;
case _FUNCSHIFT:
set_led_green;
break;
case _ADJUST:
set_led_white;
break;
default:
break;
}
};