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 #define M_SAMPLE M(KC_SAMPLEMACRO)
68 const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
71 * ,-----------------------------------------. ,-----------------------------------------.
72 * | C+z | ; | [ | ( | < | { | | | | | | | |
73 * |------+------+------+------+------+------| |------+------+------+------+------+------|
74 * | KANA | P | K | R | A | F | | | | | | | |
75 * |------+------+------+------+------+------| |------+------+------+------+------+------|
76 * | BS | D | T | H | E | O | | | | | | | |
77 * |------+------+------+------+------+------+------+------+------+------+------+------+------+------|
78 * | Shift| Y | S | N | I | U |Space | | | | | | | |
79 * |------+------+------+------+------+------+------+------+------+------+------+------+------+------|
80 * | Ctrl | Alt | Gui | Sym | Num | OPT | Ent | | | | | | | |
81 * `-------------------------------------------------------------------------------------------------'
84 LCTL(KC_Z), KC_SCLN, KC_LBRC, KC_LPRN, KC_LT, KC_LCBR, _______, _______, _______, _______, _______, _______, \
85 KANA, KC_P, KC_K, KC_R, KC_A, KC_F, _______, _______, _______, _______, _______, _______, \
86 KC_BSPC, KC_D, KC_T, KC_H, KC_E, KC_O, _______, _______, _______, _______, _______, _______, \
87 OSM(MOD_LSFT), KC_Y, KC_S, KC_N, KC_I, KC_U, KC_SPC, _______, _______, _______, _______, _______, _______, _______, \
88 OSM(MOD_LCTL), OSM(MOD_LALT), OSM(MOD_LGUI), MO(_SYM), MO(_NUM), OPT_TAP_SP, KC_ENT, _______, _______, _______, _______, _______, _______, _______ \
92 * ,-----------------------------------------. ,-----------------------------------------.
93 * | Esc | : | ] | ) | > | } | | | | | | | |
94 * |------+------+------+------+------+------| |------+------+------+------+------+------|
95 * | EISU| J | M | B | ' | Tab | | | | | | | |
96 * |------+------+------+------+------+------| |------+------+------+------+------+------|
97 * | . | V | C | L | Z | Q | | | | | | | |
98 * |------+------+------+------+------+------+------+------+------+------+------+------+------+------|
99 * | | X | G | W | - | Del | Esc | | | | | | | |
100 * |------+------+------+------+------+------+------+------+------+------+------+------+------+------|
101 * | | | | , | DTOP | | | | | | | | | |
102 * `-------------------------------------------------------------------------------------------------'
105 KC_ESC, KC_COLN,KC_RBRC, KC_RPRN,KC_GT, KC_RCBR, _______, _______, _______, _______, _______, _______, \
106 EISU, KC_J, KC_M, KC_B, KC_QUOT, KC_TAB, _______, _______, _______, _______, _______, _______, \
107 KC_DOT, KC_V, KC_C, KC_L, KC_Z, KC_Q, _______, _______, _______, _______, _______, _______, \
108 _______, KC_X, KC_G, KC_W, KC_MINUS, KC_DEL, KC_ESC, _______, _______, _______, _______, _______, _______, _______, \
109 _______, _______,_______, KC_COMM,DESKTOP, _______, _______, _______, _______, _______, _______, _______, _______, _______ \
113 * ,-----------------------------------------. ,-----------------------------------------.
114 * |RGBRST| Hue | | RST | Mac | Win | | | | | | | |
115 * |------+------+------+------+------+------| |------+------+------+------+------+------|
116 * | RGB1 | VAL+ | F7 | F8 | F9 | | | | | | | | |
117 * |------+------+------+------+------+------| |------+------+------+------+------+------|
118 * | RGB2 | VAL- | F4 | F5 | F6 | F12 | | | | | | | |
119 * |------+------+------+------+------+------+------+------+------+------+------+------+------+------|
120 * | RGB3 | F10 | F1 | F2 | F3 | F11 | | | | | | | | |
121 * |------+------+------+------+------+------+------+------+------+------+------+------+------+------|
122 * |RGBOFF| | | | | | | | | | | | | |
123 * `-------------------------------------------------------------------------------------------------'
126 RGBRST,RGB_HUI, _______, RESET, MAC, WIN, _______, _______, _______, _______, _______, _______, \
127 RGB1, RGB_VAI, KC_F7, KC_F8, KC_F9, _______, _______, _______, _______, _______, _______, _______, \
128 RGB2, RGB_VAD, KC_F4, KC_F5, KC_F6, KC_F12, _______, _______, _______, _______, _______, _______, \
129 RGB3, KC_F10, KC_F1, KC_F2, KC_F3, KC_F11, _______, _______, _______, _______, _______, _______, _______, _______, \
130 RGBOFF,_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______ \
134 * ,-----------------------------------------. ,-----------------------------------------.
135 * | Ins | GRV | | PU | PD | ^ | | | | | | | |
136 * |------+------+------+------+------+------| |------+------+------+------+------+------|
137 * | | \ | # | = | ? | % | | | | | | | |
138 * |------+------+------+------+------+------| |------+------+------+------+------+------|
139 * | | $ | upA | @ | ! | | | | | | | | | |
140 * |------+------+------+------+------+------+------+------+------+------+------+------+------+------|
141 * | CL | <- | dwA | -> | _ | & | | | | | | | | |
142 * |------+------+------+------+------+------+------+------+------+------+------+------+------+------|
143 * | | | PS | | ~ | | | | | | | | | |
144 * `-------------------------------------------------------------------------------------------------'
147 KC_INS, KC_GRV, _______, KC_PGUP, KC_PGDN, KC_CIRC, _______, _______, _______, _______, _______, _______, \
148 _______, KC_BSLS, KC_HASH, KC_EQL, KC_QUES, KC_PERC, _______, _______, _______, _______, _______, _______, \
149 _______, KC_DLR, KC_UP, KC_AT, KC_EXLM, KC_PIPE, _______, _______, _______, _______, _______, _______, \
150 KC_CAPS, KC_LEFT, KC_DOWN, KC_RIGHT,KC_UNDS, KC_AMPR, _______, _______, _______, _______, _______, _______, _______, _______, \
151 _______, _______, KC_PSCR, _______, KC_TILD, _______, _______, _______, _______, _______, _______, _______, _______, _______ \
155 * ,-----------------------------------------. ,-----------------------------------------.
156 * | | | Func | home | End | | | | | | | | |
157 * |------+------+------+------+------+------| |------+------+------+------+------+------|
158 * | | * | 7 | 8 | 9 | - | | | | | | | |
159 * |------+------+------+------+------+------| |------+------+------+------+------+------|
160 * | . | / | 4 | 5 | 6 | + | | | | | | | |
161 * |------+------+------+------+------+------+------+------+------+------+------+------+------+------|
162 * | LN | 0 | 1 | 2 | 3 |C+S+F1| | | | | | | | |
163 * |------+------+------+------+------+------+------+------+------+------+------+------+------+------|
164 * | | | | , | | | | | | | | | | |
165 * `-------------------------------------------------------------------------------------------------'
168 _______, _______, OSL(_FUNC), KC_HOME, KC_END, _______, _______, _______, _______, _______, _______, _______, \
169 _______, KC_ASTR, KC_P7, KC_P8, KC_P9, KC_MINS, _______, _______, _______, _______, _______, _______, \
170 KC_PDOT, KC_SLSH, KC_P4, KC_P5, KC_P6, KC_PLUS, _______, _______, _______, _______, _______, _______, \
171 KC_NLCK, KC_P0, KC_P1, KC_P2, KC_P3, LCTL(S(KC_F1)), _______, _______, _______, _______, _______, _______, _______, _______, \
172 _______, _______, KC_PDOT, KC_COMM, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______ \
176 #error "undefined keymaps"
182 float tone_qwerty[][2] = SONG(QWERTY_SOUND);
183 float tone_dvorak[][2] = SONG(DVORAK_SOUND);
184 float tone_colemak[][2] = SONG(COLEMAK_SOUND);
185 float tone_plover[][2] = SONG(PLOVER_SOUND);
186 float tone_plover_gb[][2] = SONG(PLOVER_GOODBYE_SOUND);
187 float music_scale[][2] = SONG(MUSIC_SCALE_SOUND);
190 // define variables for reactive RGB
191 //bool TOG_STATUS = false;
192 int RGB_current_mode;
194 void persistent_default_layer_set(uint16_t default_layer) {
195 eeconfig_update_default_layer(default_layer);
196 default_layer_set(default_layer);
199 bool find_mairix(uint16_t keycode, uint8_t *row, uint8_t *col){
200 for(uint8_t i=0; i<MATRIX_ROWS; i++){
201 for(uint8_t j=0; j<MATRIX_COLS; j++){
202 if( pgm_read_word(&(keymaps[_BASE][i][j]))==keycode){
212 void unregister_delay_code(void){
213 if(delay_registered_code){
214 unregister_code(delay_registered_code);
215 if (delay_registered_code & QK_LSFT){
216 unregister_code(KC_LSFT);
218 if (delay_registered_code & QK_LCTL){
219 unregister_code(KC_LCTL);
221 if (delay_registered_code & QK_LALT){
222 unregister_code(KC_LALT);
224 if (delay_registered_code & QK_LGUI){
225 unregister_code(KC_LGUI);
227 delay_registered_code=0;
231 void register_delay_code(uint8_t layer){
233 unregister_delay_code();
235 uint16_t code = pgm_read_word(&(keymaps[layer][delay_mat_row][delay_mat_col]));
237 register_code(KC_LSFT);
240 register_code(KC_LCTL);
243 register_code(KC_LALT);
246 register_code(KC_LGUI);
249 delay_registered_code = code;
250 delay_key_stat = false;
255 #ifdef RGBLIGHT_ENABLE
260 struct keybuf keybufs[256];
261 unsigned char keybuf_begin, keybuf_end;
266 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
268 #ifdef RGBLIGHT_ENABLE
269 col = record->event.key.col;
270 row = record->event.key.row;
271 if (record->event.pressed && ((row < 5 && is_master) || (row >= 5 && !is_master))) {
272 int end = keybuf_end;
273 keybufs[end].col = col;
274 keybufs[end].row = row % 5;
275 keybufs[end].frame = 0;
280 if(tap_timer&&keycode!=OPT_TAP_SP){
284 if(keycode==delay_registered_code){
285 if (!record->event.pressed){
286 unregister_delay_code();
314 if (record->event.pressed) {
315 register_delay_code(_BASE);
316 if(find_mairix(keycode, &delay_mat_row, &delay_mat_col)){
317 key_timer = timer_read();
318 delay_key_stat = true;
319 delay_key_pressed = true;
322 delay_key_pressed = false;
327 if (record->event.pressed) {
329 register_delay_code(_OPT);
331 tap_timer = timer_read();
334 if(tapping_key==false && timer_elapsed(tap_timer) < TAPPING_TERM){
342 if (record->event.pressed) {
343 if(keymap_config.swap_lalt_lgui==false){
344 register_code(KC_LANG2);
346 SEND_STRING(SS_LALT("`"));
349 unregister_code(KC_LANG2);
354 if (record->event.pressed) {
355 if(keymap_config.swap_lalt_lgui==false){
356 register_code(KC_LANG1);
358 SEND_STRING(SS_LALT("`"));
361 unregister_code(KC_LANG1);
366 if (record->event.pressed) {
367 if(keymap_config.swap_lalt_lgui==false){
368 register_code(KC_F11);
370 SEND_STRING(SS_LGUI("d"));
373 unregister_code(KC_F11);
378 #ifdef RGBLIGHT_ENABLE
379 if (record->event.pressed) {
380 eeconfig_update_rgblight_default();
382 RGB_current_mode = rgblight_config.mode;
383 RGBAnimation = false;
388 #ifdef RGBLIGHT_ENABLE
389 if (record->event.pressed) {
395 #ifdef RGBLIGHT_ENABLE
396 if (record->event.pressed) {
398 rgblight_mode(RGBLIGHT_MODE_RAINBOW_MOOD);
399 RGB_current_mode = rgblight_config.mode;
404 #ifdef RGBLIGHT_ENABLE
405 if (record->event.pressed) {
407 rgblight_mode(RGBLIGHT_MODE_RAINBOW_SWIRL + 1);
408 RGB_current_mode = rgblight_config.mode;
413 #ifdef RGBLIGHT_ENABLE
414 if (record->event.pressed) {
416 rgblight_mode(RGBLIGHT_MODE_KNIGHT);
417 RGB_current_mode = rgblight_config.mode;
422 if (record->event.pressed) {
423 keymap_config.swap_lalt_lgui = false;
424 keymap_config.swap_ralt_rgui = false;
426 PLAY_SONG(ag_norm_song);
431 if (record->event.pressed) {
432 keymap_config.swap_lalt_lgui = true;
433 keymap_config.swap_ralt_rgui = true;
435 PLAY_SONG(ag_swap_song);
444 //keyboard start-up code. Runs once when the firmware starts up.
445 void matrix_init_user(void) {
449 #ifdef RGBLIGHT_ENABLE
450 RGB_current_mode = rgblight_config.mode;
452 //SSD1306 OLED init, make sure to add #define SSD1306OLED in config.h
454 iota_gfx_init(!has_usb()); // turns on the display
463 _delay_ms(20); // gets rid of tick
472 void music_on_user(void)
477 void music_scale_user(void)
479 PLAY_SONG(music_scale);
485 //assign the right code to your layers for OLED display
494 #define L_NFNLAYER 192
495 #define L_MOUSECURSOR 256
498 #ifdef RGBLIGHT_ENABLE
499 unsigned char rgb[7][5][3];
500 void led_ripple_effect(char r, char g, char b) {
501 static int scan_count = -10;
502 static int keys[] = { 6, 6, 6, 7, 7 };
503 static int keys_sum[] = { 0, 6, 12, 18, 25 };
505 if (scan_count == -1) {
506 rgblight_enable_noeeprom();
507 rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
508 } else if (scan_count >= 0 && scan_count < 5) {
509 for (unsigned char c=keybuf_begin; c!=keybuf_end; c++) {
514 int dist_y = abs(y - keybufs[i].row);
515 for (int x=0; x<keys[y]; x++) {
516 int dist = abs(x - keybufs[i].col) + dist_y;
517 if (dist <= keybufs[i].frame) {
518 int elevation = MAX(0, (8 + dist - keybufs[i].frame)) << 2;
520 if ((rgb[x][y][0] != 255) && r) { rgb[x][y][0] = MIN(255, elevation + rgb[x][y][0]); }
521 if ((rgb[x][y][1] != 255) && g) { rgb[x][y][1] = MIN(255, elevation + rgb[x][y][1]); }
522 if ((rgb[x][y][2] != 255) && b) { rgb[x][y][2] = MIN(255, elevation + rgb[x][y][2]); }
527 } else if (scan_count == 5) {
528 for (unsigned char c=keybuf_begin; c!=keybuf_end; c++) {
530 if (keybufs[i].frame < 18) {
536 } else if (scan_count >= 6 && scan_count <= 10) {
537 int y = scan_count - 6;
538 for (int x=0; x<keys[y]; x++) {
539 int at = keys_sum[y] + ((y & 1) ? x : (keys[y] - x - 1));
540 led[at].r = rgb[x][y][0];
541 led[at].g = rgb[x][y][1];
542 led[at].b = rgb[x][y][2];
545 } else if (scan_count == 11) {
546 memset(rgb, 0, sizeof(rgb));
549 if (scan_count >= 12) { scan_count = 0; }
553 uint8_t layer_state_old;
555 //runs every scan cycle (a lot)
556 void matrix_scan_user(void) {
558 iota_gfx_task(); // this is what updates the display continuously
561 if(delay_key_stat && (timer_elapsed(key_timer) > DELAY_TIME)){
562 register_delay_code(_BASE);
563 if(!delay_key_pressed){
564 unregister_delay_code();
568 if(layer_state_old != layer_state){
569 switch (layer_state) {
573 register_delay_code(_OPT);
576 register_delay_code(_NUM);
579 register_delay_code(_SYM);
582 register_delay_code(_FUNC);
585 layer_state_old = layer_state;
588 #ifdef RGBLIGHT_ENABLE
590 switch (layer_state) {
593 led_ripple_effect(0,112,127);
595 rgblight_setrgb(0,112,127);
600 led_ripple_effect(127,0,100);
602 rgblight_setrgb(127,0,100);
607 led_ripple_effect(127,23,0);
609 rgblight_setrgb(127,23,0);
614 led_ripple_effect(0,127,0);
616 rgblight_setrgb(0,127,0);
621 led_ripple_effect(127,0,61);
623 rgblight_setrgb(127,0,61);
631 //SSD1306 OLED update loop, make sure to add #define SSD1306OLED in config.h
634 void matrix_update(struct CharacterMatrix *dest,
635 const struct CharacterMatrix *source) {
636 if (memcmp(dest->display, source->display, sizeof(dest->display))) {
637 memcpy(dest->display, source->display, sizeof(dest->display));
643 void render_status(struct CharacterMatrix *matrix) {
646 static char logo[4][1][17]=
649 {0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,0}
652 {0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,0x90,0x91,0x92,0x93,0x94,0}
655 {0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf,0xb0,0xb1,0xb2,0xb3,0xb4,0}
658 {0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,0xd0,0xd1,0xd2,0xd3,0}
662 static char indctr[8][2][4]=
706 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) { rown = 4; } else { rown = 0; }
707 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) { rowa = 4; } else { rowa = 0; }
708 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) { rows = 4; } else { rows = 0; }
709 if (layer_state == L_FUNC) { rowf = 4; }
711 matrix_write(matrix, indctr[rown] [0]);
712 matrix_write(matrix, indctr[rowf] [1]);
713 matrix_write(matrix, logo [0] [0]);
714 matrix_write(matrix, indctr[rown+1][0]);
715 matrix_write(matrix, indctr[rowf+1][1]);
716 matrix_write(matrix, logo [1] [0]);
717 matrix_write(matrix, indctr[rowa+2][0]);
718 matrix_write(matrix, indctr[rows+2][1]);
719 matrix_write(matrix, logo [2] [0]);
720 matrix_write(matrix, indctr[rowa+3][0]);
721 matrix_write(matrix, indctr[rows+3][1]);
722 matrix_write(matrix, logo [3] [0]);
726 void iota_gfx_task_user(void) {
727 struct CharacterMatrix matrix;
735 matrix_clear(&matrix);
737 render_status(&matrix);
739 matrix_update(&display, &matrix);