1 #include QMK_KEYBOARD_H
2 #include "bootloader.h"
5 #include "split_util.h"
14 extern keymap_config_t keymap_config;
16 #ifdef RGBLIGHT_ENABLE
17 //Following line allows macro to read current RGB settings
18 extern rgblight_config_t rgblight_config;
21 extern uint8_t is_master;
24 static uint16_t key_timer;
25 static uint16_t tap_timer;
26 static uint16_t delay_registered_code;
27 static uint8_t delay_mat_row;
28 static uint8_t delay_mat_col;
29 static bool delay_key_stat;
30 static bool delay_key_pressed;
31 static bool tapping_key;
33 // Each layer gets a name for readability, which is then used in the keymap matrix below.
34 // The underscores don't mean anything - you can have a layer called STUFF or any other name.
35 // Layer names don't all need to be of the same length, obviously, and you can also skip them
36 // entirely and just use numbers.
43 bool RGBAnimation = false; //Flag for LED Layer color Refresh.
45 enum custom_keycodes {
65 // Fillers to make layering more clear
66 #define _______ KC_TRNS
69 #define M_SAMPLE M(KC_SAMPLEMACRO)
72 const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
75 * ,-----------------------------------------. ,-----------------------------------------.
76 * | C+z | ; | [ | ( | < | { | | | | | | | |
77 * |------+------+------+------+------+------| |------+------+------+------+------+------|
78 * | KANA | P | K | R | A | F | | | | | | | |
79 * |------+------+------+------+------+------| |------+------+------+------+------+------|
80 * | BS | D | T | H | E | O | | | | | | | |
81 * |------+------+------+------+------+------+------+------+------+------+------+------+------+------|
82 * | Shift| Y | S | N | I | U |Space | | | | | | | |
83 * |------+------+------+------+------+------+------+------+------+------+------+------+------+------|
84 * | Ctrl | Alt | Gui | Sym | Num | OPT | Ent | | | | | | | |
85 * `-------------------------------------------------------------------------------------------------'
88 LCTL(KC_Z), KC_SCLN, KC_LBRC, KC_LPRN, KC_LT, KC_LCBR, _______, _______, _______, _______, _______, _______, \
89 KANA, KC_P, KC_K, KC_R, KC_A, KC_F, _______, _______, _______, _______, _______, _______, \
90 KC_BSPC, KC_D, KC_T, KC_H, KC_E, KC_O, _______, _______, _______, _______, _______, _______, \
91 OSM(MOD_LSFT), KC_Y, KC_S, KC_N, KC_I, KC_U, KC_SPC, _______, _______, _______, _______, _______, _______, _______, \
92 OSM(MOD_LCTL), OSM(MOD_LALT), OSM(MOD_LGUI), MO(_SYM), MO(_NUM), OPT_TAP_SP, KC_ENT, _______, _______, _______, _______, _______, _______, _______ \
96 * ,-----------------------------------------. ,-----------------------------------------.
97 * | Esc | : | ] | ) | > | } | | | | | | | |
98 * |------+------+------+------+------+------| |------+------+------+------+------+------|
99 * | EISU| J | M | B | ' | Tab | | | | | | | |
100 * |------+------+------+------+------+------| |------+------+------+------+------+------|
101 * | . | V | C | L | Z | Q | | | | | | | |
102 * |------+------+------+------+------+------+------+------+------+------+------+------+------+------|
103 * | | X | G | W | - | Del | Esc | | | | | | | |
104 * |------+------+------+------+------+------+------+------+------+------+------+------+------+------|
105 * | | | | , | DTOP | | | | | | | | | |
106 * `-------------------------------------------------------------------------------------------------'
109 KC_ESC, KC_COLN,KC_RBRC, KC_RPRN,KC_GT, KC_RCBR, _______, _______, _______, _______, _______, _______, \
110 EISU, KC_J, KC_M, KC_B, KC_QUOT, KC_TAB, _______, _______, _______, _______, _______, _______, \
111 KC_DOT, KC_V, KC_C, KC_L, KC_Z, KC_Q, _______, _______, _______, _______, _______, _______, \
112 _______, KC_X, KC_G, KC_W, KC_MINUS, KC_DEL, KC_ESC, _______, _______, _______, _______, _______, _______, _______, \
113 _______, _______,_______, KC_COMM,DESKTOP, _______, _______, _______, _______, _______, _______, _______, _______, _______ \
117 * ,-----------------------------------------. ,-----------------------------------------.
118 * |RGBRST| Hue | | RST | Mac | Win | | | | | | | |
119 * |------+------+------+------+------+------| |------+------+------+------+------+------|
120 * | RGB1 | VAL+ | F7 | F8 | F9 | | | | | | | | |
121 * |------+------+------+------+------+------| |------+------+------+------+------+------|
122 * | RGB2 | VAL- | F4 | F5 | F6 | F12 | | | | | | | |
123 * |------+------+------+------+------+------+------+------+------+------+------+------+------+------|
124 * | RGB3 | F10 | F1 | F2 | F3 | F11 | | | | | | | | |
125 * |------+------+------+------+------+------+------+------+------+------+------+------+------+------|
126 * |RGBOFF| | | | | | | | | | | | | |
127 * `-------------------------------------------------------------------------------------------------'
130 RGBRST,RGB_HUI, _______, RESET, MAC, WIN, _______, _______, _______, _______, _______, _______, \
131 RGB1, RGB_VAI, KC_F7, KC_F8, KC_F9, _______, _______, _______, _______, _______, _______, _______, \
132 RGB2, RGB_VAD, KC_F4, KC_F5, KC_F6, KC_F12, _______, _______, _______, _______, _______, _______, \
133 RGB3, KC_F10, KC_F1, KC_F2, KC_F3, KC_F11, _______, _______, _______, _______, _______, _______, _______, _______, \
134 RGBOFF,_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______ \
138 * ,-----------------------------------------. ,-----------------------------------------.
139 * | Ins | GRV | | PU | PD | ^ | | | | | | | |
140 * |------+------+------+------+------+------| |------+------+------+------+------+------|
141 * | | \ | # | = | ? | % | | | | | | | |
142 * |------+------+------+------+------+------| |------+------+------+------+------+------|
143 * | | $ | upA | @ | ! | | | | | | | | | |
144 * |------+------+------+------+------+------+------+------+------+------+------+------+------+------|
145 * | CL | <- | dwA | -> | _ | & | | | | | | | | |
146 * |------+------+------+------+------+------+------+------+------+------+------+------+------+------|
147 * | | | PS | | ~ | | | | | | | | | |
148 * `-------------------------------------------------------------------------------------------------'
151 KC_INS, KC_GRV, _______, KC_PGUP, KC_PGDN, KC_CIRC, _______, _______, _______, _______, _______, _______, \
152 _______, KC_BSLS, KC_HASH, KC_EQL, KC_QUES, KC_PERC, _______, _______, _______, _______, _______, _______, \
153 _______, KC_DLR, KC_UP, KC_AT, KC_EXLM, KC_PIPE, _______, _______, _______, _______, _______, _______, \
154 KC_CAPS, KC_LEFT, KC_DOWN, KC_RIGHT,KC_UNDS, KC_AMPR, _______, _______, _______, _______, _______, _______, _______, _______, \
155 _______, _______, KC_PSCR, _______, KC_TILD, _______, _______, _______, _______, _______, _______, _______, _______, _______ \
159 * ,-----------------------------------------. ,-----------------------------------------.
160 * | | | Func | home | End | | | | | | | | |
161 * |------+------+------+------+------+------| |------+------+------+------+------+------|
162 * | | * | 7 | 8 | 9 | - | | | | | | | |
163 * |------+------+------+------+------+------| |------+------+------+------+------+------|
164 * | . | / | 4 | 5 | 6 | + | | | | | | | |
165 * |------+------+------+------+------+------+------+------+------+------+------+------+------+------|
166 * | LN | 0 | 1 | 2 | 3 |C+S+F1| | | | | | | | |
167 * |------+------+------+------+------+------+------+------+------+------+------+------+------+------|
168 * | | | | , | | | | | | | | | | |
169 * `-------------------------------------------------------------------------------------------------'
172 _______, _______, OSL(_FUNC), KC_HOME, KC_END, _______, _______, _______, _______, _______, _______, _______, \
173 _______, KC_ASTR, KC_P7, KC_P8, KC_P9, KC_MINS, _______, _______, _______, _______, _______, _______, \
174 KC_PDOT, KC_SLSH, KC_P4, KC_P5, KC_P6, KC_PLUS, _______, _______, _______, _______, _______, _______, \
175 KC_NLCK, KC_P0, KC_P1, KC_P2, KC_P3, LCTL(S(KC_F1)), _______, _______, _______, _______, _______, _______, _______, _______, \
176 _______, _______, KC_PDOT, KC_COMM, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______ \
180 #error "undefined keymaps"
186 float tone_qwerty[][2] = SONG(QWERTY_SOUND);
187 float tone_dvorak[][2] = SONG(DVORAK_SOUND);
188 float tone_colemak[][2] = SONG(COLEMAK_SOUND);
189 float tone_plover[][2] = SONG(PLOVER_SOUND);
190 float tone_plover_gb[][2] = SONG(PLOVER_GOODBYE_SOUND);
191 float music_scale[][2] = SONG(MUSIC_SCALE_SOUND);
194 // define variables for reactive RGB
195 //bool TOG_STATUS = false;
196 int RGB_current_mode;
198 void persistent_default_layer_set(uint16_t default_layer) {
199 eeconfig_update_default_layer(default_layer);
200 default_layer_set(default_layer);
203 bool find_mairix(uint16_t keycode, uint8_t *row, uint8_t *col){
204 for(uint8_t i=0; i<MATRIX_ROWS; i++){
205 for(uint8_t j=0; j<MATRIX_COLS; j++){
206 if( pgm_read_word(&(keymaps[_BASE][i][j]))==keycode){
216 void unregister_delay_code(void){
217 if(delay_registered_code){
218 unregister_code(delay_registered_code);
219 if (delay_registered_code & QK_LSFT){
220 unregister_code(KC_LSFT);
222 if (delay_registered_code & QK_LCTL){
223 unregister_code(KC_LCTL);
225 if (delay_registered_code & QK_LALT){
226 unregister_code(KC_LALT);
228 if (delay_registered_code & QK_LGUI){
229 unregister_code(KC_LGUI);
231 delay_registered_code=0;
235 void register_delay_code(uint8_t layer){
237 unregister_delay_code();
239 uint16_t code = pgm_read_word(&(keymaps[layer][delay_mat_row][delay_mat_col]));
241 register_code(KC_LSFT);
244 register_code(KC_LCTL);
247 register_code(KC_LALT);
250 register_code(KC_LGUI);
253 delay_registered_code = code;
254 delay_key_stat = false;
259 #ifdef RGBLIGHT_ENABLE
264 struct keybuf keybufs[256];
265 unsigned char keybuf_begin, keybuf_end;
270 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
272 #ifdef RGBLIGHT_ENABLE
273 col = record->event.key.col;
274 row = record->event.key.row;
275 if (record->event.pressed && ((row < 5 && is_master) || (row >= 5 && !is_master))) {
276 int end = keybuf_end;
277 keybufs[end].col = col;
278 keybufs[end].row = row % 5;
279 keybufs[end].frame = 0;
284 if(tap_timer&&keycode!=OPT_TAP_SP){
288 if(keycode==delay_registered_code){
289 if (!record->event.pressed){
290 unregister_delay_code();
318 if (record->event.pressed) {
319 register_delay_code(_BASE);
320 if(find_mairix(keycode, &delay_mat_row, &delay_mat_col)){
321 key_timer = timer_read();
322 delay_key_stat = true;
323 delay_key_pressed = true;
326 delay_key_pressed = false;
331 if (record->event.pressed) {
333 register_delay_code(_OPT);
335 tap_timer = timer_read();
338 if(tapping_key==false && timer_elapsed(tap_timer) < TAPPING_TERM){
346 if (record->event.pressed) {
347 if(keymap_config.swap_lalt_lgui==false){
348 register_code(KC_LANG2);
350 SEND_STRING(SS_LALT("`"));
353 unregister_code(KC_LANG2);
358 if (record->event.pressed) {
359 if(keymap_config.swap_lalt_lgui==false){
360 register_code(KC_LANG1);
362 SEND_STRING(SS_LALT("`"));
365 unregister_code(KC_LANG1);
370 if (record->event.pressed) {
371 if(keymap_config.swap_lalt_lgui==false){
372 register_code(KC_F11);
374 SEND_STRING(SS_LGUI("d"));
377 unregister_code(KC_F11);
382 #ifdef RGBLIGHT_ENABLE
383 if (record->event.pressed) {
384 eeconfig_update_rgblight_default();
386 RGB_current_mode = rgblight_config.mode;
387 RGBAnimation = false;
392 #ifdef RGBLIGHT_ENABLE
393 if (record->event.pressed) {
399 #ifdef RGBLIGHT_ENABLE
400 if (record->event.pressed) {
402 rgblight_mode(RGBLIGHT_MODE_RAINBOW_MOOD);
403 RGB_current_mode = rgblight_config.mode;
408 #ifdef RGBLIGHT_ENABLE
409 if (record->event.pressed) {
411 rgblight_mode(RGBLIGHT_MODE_RAINBOW_SWIRL + 1);
412 RGB_current_mode = rgblight_config.mode;
417 #ifdef RGBLIGHT_ENABLE
418 if (record->event.pressed) {
420 rgblight_mode(RGBLIGHT_MODE_KNIGHT);
421 RGB_current_mode = rgblight_config.mode;
426 if (record->event.pressed) {
427 keymap_config.swap_lalt_lgui = false;
428 keymap_config.swap_ralt_rgui = false;
430 PLAY_SONG(ag_norm_song);
435 if (record->event.pressed) {
436 keymap_config.swap_lalt_lgui = true;
437 keymap_config.swap_ralt_rgui = true;
439 PLAY_SONG(ag_swap_song);
448 //keyboard start-up code. Runs once when the firmware starts up.
449 void matrix_init_user(void) {
453 #ifdef RGBLIGHT_ENABLE
454 RGB_current_mode = rgblight_config.mode;
456 //SSD1306 OLED init, make sure to add #define SSD1306OLED in config.h
458 iota_gfx_init(!has_usb()); // turns on the display
467 _delay_ms(20); // gets rid of tick
476 void music_on_user(void)
481 void music_scale_user(void)
483 PLAY_SONG(music_scale);
489 //assign the right code to your layers for OLED display
498 #define L_NFNLAYER 192
499 #define L_MOUSECURSOR 256
502 #ifdef RGBLIGHT_ENABLE
503 unsigned char rgb[7][5][3];
504 void led_ripple_effect(char r, char g, char b) {
505 static int scan_count = -10;
506 static int keys[] = { 6, 6, 6, 7, 7 };
507 static int keys_sum[] = { 0, 6, 12, 18, 25 };
509 if (scan_count == -1) {
510 rgblight_enable_noeeprom();
511 rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
512 } else if (scan_count >= 0 && scan_count < 5) {
513 for (unsigned char c=keybuf_begin; c!=keybuf_end; c++) {
518 int dist_y = abs(y - keybufs[i].row);
519 for (int x=0; x<keys[y]; x++) {
520 int dist = abs(x - keybufs[i].col) + dist_y;
521 if (dist <= keybufs[i].frame) {
522 int elevation = MAX(0, (8 + dist - keybufs[i].frame)) << 2;
524 if ((rgb[x][y][0] != 255) && r) { rgb[x][y][0] = MIN(255, elevation + rgb[x][y][0]); }
525 if ((rgb[x][y][1] != 255) && g) { rgb[x][y][1] = MIN(255, elevation + rgb[x][y][1]); }
526 if ((rgb[x][y][2] != 255) && b) { rgb[x][y][2] = MIN(255, elevation + rgb[x][y][2]); }
531 } else if (scan_count == 5) {
532 for (unsigned char c=keybuf_begin; c!=keybuf_end; c++) {
534 if (keybufs[i].frame < 18) {
540 } else if (scan_count >= 6 && scan_count <= 10) {
541 int y = scan_count - 6;
542 for (int x=0; x<keys[y]; x++) {
543 int at = keys_sum[y] + ((y & 1) ? x : (keys[y] - x - 1));
544 led[at].r = rgb[x][y][0];
545 led[at].g = rgb[x][y][1];
546 led[at].b = rgb[x][y][2];
549 } else if (scan_count == 11) {
550 memset(rgb, 0, sizeof(rgb));
553 if (scan_count >= 12) { scan_count = 0; }
557 uint8_t layer_state_old;
559 //runs every scan cycle (a lot)
560 void matrix_scan_user(void) {
562 iota_gfx_task(); // this is what updates the display continuously
565 if(delay_key_stat && (timer_elapsed(key_timer) > DELAY_TIME)){
566 register_delay_code(_BASE);
567 if(!delay_key_pressed){
568 unregister_delay_code();
572 if(layer_state_old != layer_state){
573 switch (layer_state) {
577 register_delay_code(_OPT);
580 register_delay_code(_NUM);
583 register_delay_code(_SYM);
586 register_delay_code(_FUNC);
589 layer_state_old = layer_state;
592 #ifdef RGBLIGHT_ENABLE
594 switch (layer_state) {
596 led_ripple_effect(0,112,127);
599 led_ripple_effect(127,0,100);
602 led_ripple_effect(127,23,0);
605 led_ripple_effect(0,127,0);
608 led_ripple_effect(127,0,61);
615 //SSD1306 OLED update loop, make sure to add #define SSD1306OLED in config.h
618 void matrix_update(struct CharacterMatrix *dest,
619 const struct CharacterMatrix *source) {
620 if (memcmp(dest->display, source->display, sizeof(dest->display))) {
621 memcpy(dest->display, source->display, sizeof(dest->display));
627 void render_status(struct CharacterMatrix *matrix) {
630 static char logo[4][1][17]=
633 {0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,0}
636 {0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,0x90,0x91,0x92,0x93,0x94,0}
639 {0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf,0xb0,0xb1,0xb2,0xb3,0xb4,0}
642 {0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,0xd0,0xd1,0xd2,0xd3,0}
646 static char indctr[8][2][4]=
690 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) { rown = 4; } else { rown = 0; }
691 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) { rowa = 4; } else { rowa = 0; }
692 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) { rows = 4; } else { rows = 0; }
693 if (layer_state == L_FUNC) { rowf = 4; }
695 matrix_write(matrix, indctr[rown] [0]);
696 matrix_write(matrix, indctr[rowf] [1]);
697 matrix_write(matrix, logo [0] [0]);
698 matrix_write(matrix, indctr[rown+1][0]);
699 matrix_write(matrix, indctr[rowf+1][1]);
700 matrix_write(matrix, logo [1] [0]);
701 matrix_write(matrix, indctr[rowa+2][0]);
702 matrix_write(matrix, indctr[rows+2][1]);
703 matrix_write(matrix, logo [2] [0]);
704 matrix_write(matrix, indctr[rowa+3][0]);
705 matrix_write(matrix, indctr[rows+3][1]);
706 matrix_write(matrix, logo [3] [0]);
710 void iota_gfx_task_user(void) {
711 struct CharacterMatrix matrix;
719 matrix_clear(&matrix);
721 render_status(&matrix);
723 matrix_update(&display, &matrix);