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