]> git.donarmstrong.com Git - qmk_firmware.git/blob - users/bbaserdem/bbaserdem.c
Merge branch 'master' of github.com:qmk/qmk_firmware into hf/shinydox
[qmk_firmware.git] / users / bbaserdem / bbaserdem.c
1 #include "bbaserdem.h"
2
3 /*---------------*\
4 |*-----MOUSE-----*|
5 \*---------------*/
6 #ifdef MOUSEKEY_ENABLE
7 #include "mousekey.h"
8 #endif
9
10 /*-------------*\
11 |*-----RGB-----*|
12 \*-------------*/
13 #ifdef RGBLIGHT_ENABLE
14 #include "rgblight.h"
15 #endif
16
17 /*-----------------*\
18 |*-----SECRETS-----*|
19 \*-----------------*/
20 // Enabled by adding a non-tracked secrets.h to this dir.
21 #if (__has_include("secrets.h"))
22 #include "secrets.h"
23 #endif
24
25 /*---------------*\
26 |*-----MUSIC-----*|
27 \*---------------*/
28 #ifdef AUDIO_ENABLE
29 float tone_game[][2]    = SONG(ZELDA_PUZZLE);
30 float tone_return[][2]  = SONG(ZELDA_TREASURE);
31 float tone_linux[][2]   = SONG(UNICODE_LINUX);
32 float tone_windows[][2] = SONG(UNICODE_WINDOWS);
33 #endif
34
35 /*-------------------*\
36 |*-----TAP-DANCE-----*|
37 \*-------------------*/
38 #ifdef TAP_DANCE_ENABLE
39 qk_tap_dance_action_t tap_dance_actions[] = {
40     // Shift on double tap of semicolon
41     [SCL] = ACTION_TAP_DANCE_DOUBLE( KC_SCLN, KC_COLN )
42 };
43 #endif
44
45 /* In keymaps, instead of writing _user functions, write _keymap functions
46  * The __attribute__((weak)) allows for empty definitions here, and during
47  * compilation, if these functions are defined elsewhere, they are written
48  * over. This allows to include custom code from keymaps in the generic code
49  * in this file.
50  */
51 __attribute__ ((weak)) void matrix_init_keymap(void) { }
52 __attribute__ ((weak)) void matrix_scan_keymap(void) { }
53 __attribute__ ((weak)) bool process_record_keymap(uint16_t keycode, keyrecord_t *record) {
54     return true;
55 }
56 __attribute__ ((weak)) uint32_t layer_state_set_keymap (uint32_t state) {
57     return state;
58 }
59 __attribute__ ((weak)) void led_set_keymap(uint8_t usb_led) { }
60
61 /* ----------------------- *\
62  * -----RGB Functions----- *
63 \* ----------------------- */
64
65
66 #ifdef RGBLIGHT_ENABLE
67 // Storage variables
68 extern  rgblight_config_t rgblight_config;
69 bool    base_sta;   // Keeps track if in saveable state
70 bool    base_tog;   // Whether base state is active or not
71 int     base_hue;   // Hue value of base state
72 int     base_sat;   // Saturation value of base state
73 int     base_val;   // Brightness value of base state
74 uint8_t base_mod;   // Animation mode of the base state
75
76 // Save the current state of the rgb mode
77 void rgblight_saveBase(void) {
78     base_hue = rgblight_config.hue;
79     base_sat = rgblight_config.sat;
80     base_val = rgblight_config.val;
81     base_mod = rgblight_config.mode;
82     base_tog = rgblight_config.enable;
83     base_sta = false;   // If saving, that means base layer is being left
84 }
85
86 // Load the base state back 
87 void rgblight_loadBase(void) {
88     // Don't do anything if not enabled
89     if ( !base_sta ) {
90         if ( base_tog ) {
91             rgblight_enable();
92             rgblight_mode( base_mod );
93             rgblight_sethsv( base_hue, base_sat, base_val );
94         } else {
95             rgblight_disable();
96         }
97     }
98     // Mark that base is loaded, and to be saved before leaving
99     base_sta = true;
100 }
101
102 // Set to plain HSV color
103 void rgblight_colorStatic( int hu, int sa, int va ) {
104     // First, it must be enabled or color change is not written
105     rgblight_enable();
106     rgblight_mode(1);
107     rgblight_sethsv(hu,sa,va);
108 }
109 /* HSV values, thank you @drashna!
110  * white        (  0,   0, 255)
111  * red          (  0, 255, 255)
112  * coral        ( 16, 176, 255)
113  * orange       ( 39, 255, 255)
114  * goldenrod    ( 43, 218, 218)
115  * gold         ( 51, 255, 255)
116  * yellow       ( 60, 255, 255)
117  * chartreuse   ( 90, 255, 255)
118  * green        (120, 255, 255)
119  * springgreen  (150, 255, 255)
120  * turquoise    (174,  90, 112)
121  * teal         (180, 255, 128)
122  * cyan         (180, 255, 255)
123  * azure        (186, 102, 255)
124  * blue         (240, 255, 255)
125  * purple       (270, 255, 255)
126  * magenta      (300, 255, 255)
127  * pink         (330, 128, 255)
128  */
129 // Set RGBLIGHT state depending on layer
130 void rgblight_change( uint8_t last_layer ) {
131     // Save state, if saving is requested
132     /*
133     if ( base_sta ) {
134         rgblight_saveBase();
135     }
136     */
137     // Change RGB light
138     switch ( last_layer ) {
139         case _DV:
140             // Load base layer
141             rgblight_loadBase();
142             break;
143         case _AL:
144             // Do yellow for alternate
145             rgblight_colorStatic( 60,255,255);
146             break;
147         case _GA:
148             // Do purple for game
149             rgblight_colorStatic(285,255,255);
150             break;
151         case _NU:
152             // Do azure for number
153             rgblight_colorStatic(186,200,255);
154             break;
155         case _SE:
156             // Do red for settings
157             rgblight_colorStatic( 16,255,255);
158             break;
159         case _MO:
160             // Do green for mouse
161             rgblight_colorStatic(120,255,255);
162             break;
163 #ifdef AUDIO_ENABLE
164         case _MU:
165             // Do orange for music
166             rgblight_colorStatic( 39,255,255);
167             break;
168 #endif
169         default:
170             // Something went wrong
171             rgblight_colorStatic(  0,255,255);
172             break;
173     }
174 }
175
176 #endif
177
178 /*---------------------*\
179 |*-----MATRIX INIT-----*|
180 \*---------------------*/
181 void matrix_init_user (void) {
182
183     // Keymap specific things, do it first thing to allow for delays etc
184     matrix_init_keymap();
185
186     // Correct unicode
187     set_unicode_input_mode(UC_LNX);
188
189     // Make beginning layer DVORAK
190     set_single_persistent_default_layer(_DV);
191
192 //--RGB light initialize base layer
193 #ifdef RGBLIGHT_ENABLE
194     // Base hue is white, and RGB disabled
195     base_hue = 100;
196     base_sat = 0;
197     base_val = 255;
198     base_mod = 2;
199     base_tog = false;
200     rgblight_enable();
201     rgblight_mode(base_mod);
202     rgblight_sethsv(base_hue,base_sat,base_val);
203     rgblight_disable();
204     rgblight_loadBase();
205 #endif
206
207 }
208
209 /*---------------------*\
210 |*-----MATRIX SCAN-----*|
211 \*---------------------*/
212 void matrix_scan_user (void) {
213     // Keymap specific, do it first
214     matrix_scan_keymap();
215     // Moved RGB check to layer_state_set_user
216 }
217
218 /*------------------*\
219 |*-----KEYCODES-----*|
220 \*------------------*/
221 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
222
223     // Shift check
224     bool is_capital = ( keyboard_report->mods & (MOD_BIT(KC_LSFT)|MOD_BIT(KC_RSFT)) );
225     static bool lock_flag = false;
226     uint8_t layer = biton32 (layer_state);
227
228     switch (keycode) {
229         // Secrets implementation
230         case SECRET1 ... SECRET3:
231 #if (__has_include("secrets.h"))
232             if( !record->event.pressed ) {
233                 send_string_P( secret[ keycode - SECRET1 ] );
234             }
235 #endif
236             return false;
237             break;
238         // If these keys are pressed, load base layer config, and mark saving
239         case RGB_TOG:
240         case RGB_MOD:
241         case RGB_VAI:
242         case RGB_VAD:
243         case RGB_SAI:
244         case RGB_SAD:
245         case RGB_HUI:
246         case RGB_HUD:
247 #ifdef RGBLIGHT_ENABLE
248             if ( !base_sta ) {
249                 rgblight_loadBase(); 
250             }
251 #endif
252             return true;
253             break;
254
255         // Lock functionality: These layers are locked if the LOCKED buttons are
256         // pressed. Otherwise, they are momentary toggles
257         case K_LOCK:
258             if (record->event.pressed) {
259                 lock_flag = !lock_flag;
260             }
261             return false;
262             break;
263         case K_MOUSE:
264 #ifdef MOUSEKEY_ENABLE
265             if (record->event.pressed) {
266                 layer_on(_MO);
267                 lock_flag = false;
268             } else {
269                 if ( lock_flag ) {
270                     lock_flag = false;
271                 } else {
272                     layer_off(_MO);
273                 }
274             }
275 #endif
276             return false;
277             break;
278         case K_NUMBR:
279             if (record->event.pressed) {
280                 layer_on(_NU);
281                 lock_flag = false;
282             } else {
283                 if ( lock_flag ) {
284                     lock_flag = false;
285                 } else {
286                     layer_off(_NU);
287                 }
288             }
289             return false;
290             break;
291             
292         // Layer switches with sound
293         case K_GAMES:
294             if (record->event.pressed) {
295                 // On press, turn off layer if active
296                 if ( layer == _GA ) {
297 #ifdef AUDIO_ENABLE
298                     stop_all_notes();
299                     PLAY_SONG(tone_return);
300 #endif
301                     layer_off(_GA);
302                 }
303             } else {
304                 // After click, turn on layer if accessed from setting
305                 if ( layer == _SE ) {
306 #ifdef AUDIO_ENABLE
307                     stop_all_notes();
308                     PLAY_SONG(tone_game);
309 #endif
310                     layer_on(_GA);
311                     layer_off(_SE);
312                 }
313             }
314             return false;
315             break;
316         case MU_TOG:
317 #ifdef AUDIO_ENABLE
318             if (record->event.pressed) {
319                 // On press, turn off layer if active
320                 if ( layer == _SE ) {
321                     layer_off(_SE);
322                     layer_on(_MU);
323                 } else {
324                     layer_off(_MU);
325                 }
326             }
327 #endif
328             return true;
329             break;
330
331 //------UNICODE
332         // Unicode switches with sound
333         case UNI_LI:
334 #ifdef UNICODE_ENABLE
335             if (record->event.pressed) {
336 #ifdef AUDIO_ENABLE
337                 stop_all_notes();
338                 PLAY_SONG(tone_linux);
339 #endif
340                 set_unicode_input_mode(UC_LNX);
341             }
342 #endif
343             return false;
344             break;
345         case UNI_WN:
346 #ifdef UNICODE_ENABLE
347             if (record->event.pressed) {
348 #ifdef AUDIO_ENABLE
349                 stop_all_notes();
350                 PLAY_SONG(tone_windows);
351 #endif
352                 set_unicode_input_mode(UC_WIN);
353             }
354 #endif
355             return false;
356             break;
357
358         // Turkish letters, with capital functionality
359         case TUR_A:
360 #ifdef UNICODE_ENABLE
361             if (record->event.pressed) {
362                 if ( is_capital ) {
363                     unicode_input_start();
364                     register_hex(0x00c2);
365                     unicode_input_finish();
366                 } else {
367                     unicode_input_start();
368                     register_hex(0x00e2);
369                     unicode_input_finish();
370                 }
371             }
372 #endif
373             return false;
374             break;
375         case TUR_O:
376 #ifdef UNICODE_ENABLE
377             if (record->event.pressed) {
378                 if ( is_capital ) {
379                     unicode_input_start();
380                     register_hex(0x00d6);
381                     unicode_input_finish();
382                 } else {
383                     unicode_input_start();
384                     register_hex(0x00f6);
385                     unicode_input_finish();
386                 }
387             }
388 #endif
389             return false;
390             break;
391         case TUR_U:
392 #ifdef UNICODE_ENABLE
393             if (record->event.pressed) {
394                 if ( is_capital ) {
395                     unicode_input_start();
396                     register_hex(0x00dc);
397                     unicode_input_finish();
398                 } else {
399                     unicode_input_start();
400                     register_hex(0x00fc);
401                     unicode_input_finish();
402                 }
403             }
404 #endif
405             return false;
406             break;
407         case TUR_I:
408 #ifdef UNICODE_ENABLE
409             if (record->event.pressed) {
410                 if ( is_capital ) {
411                     unicode_input_start();
412                     register_hex(0x0130);
413                     unicode_input_finish();
414                 } else {
415                     unicode_input_start();
416                     register_hex(0x0131);
417                     unicode_input_finish();
418                 }
419             }
420 #endif
421             return false;
422             break;
423         case TUR_G:
424 #ifdef UNICODE_ENABLE
425             if (record->event.pressed) {
426                 if ( is_capital ) {
427                     unicode_input_start();
428                     register_hex(0x011e);
429                     unicode_input_finish();
430                 } else {
431                     unicode_input_start();
432                     register_hex(0x011f);
433                     unicode_input_finish();
434                 }
435             }
436 #endif
437             return false;
438             break;
439         case TUR_C:
440 #ifdef UNICODE_ENABLE
441             if (record->event.pressed) {
442                 if ( is_capital ) {
443                     unicode_input_start();
444                     register_hex(0x00c7);
445                     unicode_input_finish();
446                 } else {
447                     unicode_input_start();
448                     register_hex(0x00e7);
449                     unicode_input_finish();
450                 }
451             }
452 #endif
453             return false;
454             break;
455         case TUR_S:
456 #ifdef UNICODE_ENABLE
457             if (record->event.pressed) {
458                 if ( is_capital ) {
459                     unicode_input_start();
460                     register_hex(0x015e);
461                     unicode_input_finish();
462                 } else {
463                     unicode_input_start();
464                     register_hex(0x015f);
465                     unicode_input_finish();
466                 }
467             }
468 #endif
469             return false;
470             break;
471
472 //-------Diagonal mouse movements
473         case MO_NE:
474 #ifdef MOUSEKEY_ENABLE
475             if( record->event.pressed ) {
476                 mousekey_on(MO_N);
477                 mousekey_on(MO_E);
478                 mousekey_send();
479             } else {
480                 mousekey_off(MO_N);
481                 mousekey_off(MO_E);
482                 mousekey_send();
483             }
484 #endif
485             return false;
486             break;
487         case MO_NW:
488 #ifdef MOUSEKEY_ENABLE
489             if( record->event.pressed ) {
490                 mousekey_on(MO_N);
491                 mousekey_on(MO_W);
492                 mousekey_send();
493             } else {
494                 mousekey_off(MO_N);
495                 mousekey_off(MO_W);
496                 mousekey_send();
497             }
498 #endif
499             return false;
500             break;
501         case MO_SE:
502 #ifdef MOUSEKEY_ENABLE
503             if( record->event.pressed ) {
504                 mousekey_on(MO_S);
505                 mousekey_on(MO_E);
506                 mousekey_send();
507             } else {
508                 mousekey_off(MO_S);
509                 mousekey_off(MO_E);
510                 mousekey_send();
511             }
512 #endif
513             return false;
514             break;
515         case MO_SW:
516 #ifdef MOUSEKEY_ENABLE
517             if( record->event.pressed ) {
518                 mousekey_on(MO_S);
519                 mousekey_on(MO_W);
520                 mousekey_send();
521             } else {
522                 mousekey_off(MO_S);
523                 mousekey_off(MO_W);
524                 mousekey_send();
525             }
526 #endif
527             return false;
528             break;
529         case MO_S_NE:
530 #ifdef MOUSEKEY_ENABLE
531             if( record->event.pressed ) {
532                 mousekey_on(MO_S_N);
533                 mousekey_on(MO_S_E);
534                 mousekey_send();
535             } else {
536                 mousekey_off(MO_S_N);
537                 mousekey_off(MO_S_E);
538                 mousekey_send();
539             }
540 #endif
541             return false;
542             break;
543         case MO_S_NW:
544 #ifdef MOUSEKEY_ENABLE
545             if( record->event.pressed ) {
546                 mousekey_on(MO_S_N);
547                 mousekey_on(MO_S_W);
548                 mousekey_send();
549             } else {
550                 mousekey_off(MO_S_N);
551                 mousekey_off(MO_S_W);
552                 mousekey_send();
553             }
554 #endif
555             return false;
556             break;
557         case MO_S_SE:
558 #ifdef MOUSEKEY_ENABLE
559             if( record->event.pressed ) {
560                 mousekey_on(MO_S_S);
561                 mousekey_on(MO_S_E);
562                 mousekey_send();
563             } else {
564                 mousekey_off(MO_S_S);
565                 mousekey_off(MO_S_E);
566                 mousekey_send();
567             }
568 #endif
569             return false;
570             break;
571         case MO_S_SW:
572 #ifdef MOUSEKEY_ENABLE
573             if( record->event.pressed ) {
574                 mousekey_on(MO_S_S);
575                 mousekey_on(MO_S_W);
576                 mousekey_send();
577             } else {
578                 mousekey_off(MO_S_S);
579                 mousekey_off(MO_S_W);
580                 mousekey_send();
581             }
582 #endif
583             return false;
584             break;
585 //------DOUBLE PRESS, with added left navigation
586         case DBL_SPC:
587             if( record->event.pressed ) {
588                 SEND_STRING("  "SS_TAP(X_LEFT));
589             }
590             return false;
591             break;
592         case DBL_ANG:
593             if( record->event.pressed ) {
594                 SEND_STRING("<>"SS_TAP(X_LEFT));
595             }
596             return false;
597             break;
598         case DBL_PAR:
599             if( record->event.pressed ) {
600                 SEND_STRING("()"SS_TAP(X_LEFT));
601             }
602             return false;
603             break;
604         case DBL_SQR:
605             if( record->event.pressed ) {
606                 SEND_STRING("[]"SS_TAP(X_LEFT));
607             }
608             return false;
609             break;
610         case DBL_BRC:
611             if( record->event.pressed ) {
612                 SEND_STRING("{}"SS_TAP(X_LEFT));
613             }
614             return false;
615             break;
616         case DBL_QUO:
617             if( record->event.pressed ) {
618                 SEND_STRING("\'\'"SS_TAP(X_LEFT));
619             }
620             return false;
621             break;
622         case DBL_DQT:
623             if( record->event.pressed ) {
624                 SEND_STRING("\"\""SS_TAP(X_LEFT));
625             }
626             return false;
627             break;
628         case DBL_GRV:
629             if( record->event.pressed ) {
630                 SEND_STRING("``"SS_TAP(X_LEFT));
631             }
632             return false;
633             break;
634 // END OF KEYCODES
635     }
636     return process_record_keymap(keycode, record);
637 }
638
639 /*----------------------*\
640 |*-----LAYER CHANGE-----*|
641 \*----------------------*/
642
643 uint32_t layer_state_set_user(uint32_t state) {
644
645     state = layer_state_set_keymap (state);
646 #ifdef RGBLIGHT_ENABLE
647     // Change RGB lighting depending on the last layer activated
648     rgblight_change( biton32(state) );
649 #endif
650     return state;
651 }