]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/quantum.c
9c0f9691ff117699829b7cc30491a70bd481735f
[qmk_firmware.git] / quantum / quantum.c
1 #include "quantum.h"
2
3 __attribute__ ((weak))
4 bool process_action_kb(keyrecord_t *record) {
5   return true;
6 }
7
8 __attribute__ ((weak))
9 bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
10   return process_record_user(keycode, record);
11 }
12
13 __attribute__ ((weak))
14 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
15   return true;
16 }
17
18 __attribute__ ((weak))
19 void leader_start(void) {}
20
21 __attribute__ ((weak))
22 void leader_end(void) {}
23
24 uint8_t starting_note = 0x0C;
25 int offset = 7;
26
27
28 #ifdef AUDIO_ENABLE
29   bool music_activated = false;
30
31   // music sequencer
32   static bool music_sequence_recording = false;
33   static bool music_sequence_playing = false;
34   static float music_sequence[16] = {0};
35   static uint8_t music_sequence_count = 0;
36   static uint8_t music_sequence_position = 0;
37
38   static uint16_t music_sequence_timer = 0;
39   static uint16_t music_sequence_interval = 100;
40
41 #endif
42
43 #ifdef MIDI_ENABLE
44   bool midi_activated = false;
45 #endif
46
47 // Leader key stuff
48 bool leading = false;
49 uint16_t leader_time = 0;
50
51 uint16_t leader_sequence[5] = {0, 0, 0, 0, 0};
52 uint8_t leader_sequence_size = 0;
53
54 // Chording stuff
55 #define CHORDING_MAX 4
56 bool chording = false;
57
58 uint8_t chord_keys[CHORDING_MAX] = {0};
59 uint8_t chord_key_count = 0;
60 uint8_t chord_key_down = 0;
61
62 #ifdef UNICODE_ENABLE
63   static uint8_t input_mode;
64 #endif
65
66 // Shift / paren setup
67
68 #ifndef LSPO_KEY
69   #define LSPO_KEY KC_9
70 #endif
71 #ifndef RSPC_KEY
72   #define RSPC_KEY KC_0
73 #endif
74
75 static bool shift_interrupted[2] = {0, 0};
76
77 bool keys_chord(uint8_t keys[]) {
78   uint8_t keys_size = sizeof(keys)/sizeof(keys[0]);
79   bool pass = true;
80   uint8_t in = 0;
81   for (uint8_t i = 0; i < chord_key_count; i++) {
82     bool found = false;
83     for (uint8_t j = 0; j < keys_size; j++) {
84       if (chord_keys[i] == (keys[j] & 0xFF)) {
85         in++; // detects key in chord
86         found = true;
87         break;
88       }
89     }
90     if (found)
91       continue;
92     if (chord_keys[i] != 0)  {
93       pass = false; // makes sure rest are blank
94     }
95   }
96   return (pass && (in == keys_size));
97 }
98
99 #ifdef UNICODE_ENABLE
100
101 uint16_t hex_to_keycode(uint8_t hex)
102 {
103   if (hex == 0x0) {
104     return KC_0;
105   } else if (hex < 0xA) {
106     return KC_1 + (hex - 0x1);
107   } else {
108     return KC_A + (hex - 0xA);
109   }
110 }
111
112 void set_unicode_mode(uint8_t os_target)
113 {
114   input_mode = os_target;
115 }
116
117 #endif
118
119 bool process_record_quantum(keyrecord_t *record) {
120
121   /* This gets the keycode from the key pressed */
122   keypos_t key = record->event.key;
123   uint16_t keycode;
124
125   #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
126     uint8_t layer;
127
128     if (record->event.pressed) {
129       layer = layer_switch_get_layer(key);
130       update_source_layers_cache(key, layer);
131     } else {
132       layer = read_source_layers_cache(key);
133     }
134     keycode = keymap_key_to_keycode(layer, key);
135   #else
136     keycode = keymap_key_to_keycode(layer_switch_get_layer(key), key);
137   #endif
138
139   if (!process_record_kb(keycode, record))
140     return false;
141
142     // This is how you use actions here
143     // if (keycode == KC_LEAD) {
144     //   action_t action;
145     //   action.code = ACTION_DEFAULT_LAYER_SET(0);
146     //   process_action(record, action);
147     //   return false;
148     // }
149
150   #ifdef MIDI_ENABLE
151     if (keycode == MI_ON && record->event.pressed) {
152       midi_activated = true;
153       music_scale_user();
154       return false;
155     }
156
157     if (keycode == MI_OFF && record->event.pressed) {
158       midi_activated = false;
159       midi_send_cc(&midi_device, 0, 0x7B, 0);
160       return false;
161     }
162
163     if (midi_activated) {
164       if (record->event.key.col == (MATRIX_COLS - 1) && record->event.key.row == (MATRIX_ROWS - 1)) {
165           if (record->event.pressed) {
166               starting_note++; // Change key
167               midi_send_cc(&midi_device, 0, 0x7B, 0);
168           }
169           return false;
170       }
171       if (record->event.key.col == (MATRIX_COLS - 2) && record->event.key.row == (MATRIX_ROWS - 1)) {
172           if (record->event.pressed) {
173               starting_note--; // Change key
174               midi_send_cc(&midi_device, 0, 0x7B, 0);
175           }
176           return false;
177       }
178       if (record->event.key.col == (MATRIX_COLS - 3) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
179           offset++; // Change scale
180           midi_send_cc(&midi_device, 0, 0x7B, 0);
181           return false;
182       }
183       if (record->event.key.col == (MATRIX_COLS - 4) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
184           offset--; // Change scale
185           midi_send_cc(&midi_device, 0, 0x7B, 0);
186           return false;
187       }
188       // basic
189       // uint8_t note = (starting_note + SCALE[record->event.key.col + offset])+12*(MATRIX_ROWS - record->event.key.row);
190       // advanced
191       // uint8_t note = (starting_note + record->event.key.col + offset)+12*(MATRIX_ROWS - record->event.key.row);
192       // guitar
193       uint8_t note = (starting_note + record->event.key.col + offset)+5*(MATRIX_ROWS - record->event.key.row);
194       // violin
195       // uint8_t note = (starting_note + record->event.key.col + offset)+7*(MATRIX_ROWS - record->event.key.row);
196
197       if (record->event.pressed) {
198         // midi_send_noteon(&midi_device, record->event.key.row, starting_note + SCALE[record->event.key.col], 127);
199         midi_send_noteon(&midi_device, 0, note, 127);
200       } else {
201         // midi_send_noteoff(&midi_device, record->event.key.row, starting_note + SCALE[record->event.key.col], 127);
202         midi_send_noteoff(&midi_device, 0, note, 127);
203       }
204
205       if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through
206         return false;
207     }
208   #endif
209
210   #ifdef AUDIO_ENABLE
211     if (keycode == AU_ON && record->event.pressed) {
212       audio_on();
213       return false;
214     }
215
216     if (keycode == AU_OFF && record->event.pressed) {
217       audio_off();
218       return false;
219     }
220
221     if (keycode == AU_TOG && record->event.pressed) {
222         if (is_audio_on())
223         {
224             audio_off();
225         }
226         else
227         {
228             audio_on();
229         }
230       return false;
231     }
232
233     if (keycode == MU_ON && record->event.pressed) {
234         music_on();
235         return false;
236     }
237
238     if (keycode == MU_OFF && record->event.pressed) {
239         music_off();
240         return false;
241     }
242
243     if (keycode == MU_TOG && record->event.pressed) {
244         if (music_activated)
245         {
246             music_off();
247         }
248         else
249         {
250             music_on();
251         }
252         return false;
253     }
254
255     if (keycode == MUV_IN && record->event.pressed) {
256         voice_iterate();
257         music_scale_user();
258         return false;
259     }
260
261     if (keycode == MUV_DE && record->event.pressed) {
262         voice_deiterate();
263         music_scale_user();
264         return false;
265     }
266
267     if (music_activated) {
268
269       if (keycode == KC_LCTL && record->event.pressed) { // Start recording
270         stop_all_notes();
271         music_sequence_recording = true;
272         music_sequence_playing = false;
273         music_sequence_count = 0;
274         return false;
275       }
276
277       if (keycode == KC_LALT && record->event.pressed) { // Stop recording/playing
278         stop_all_notes();
279         music_sequence_recording = false;
280         music_sequence_playing = false;
281         return false;
282       }
283
284       if (keycode == KC_LGUI && record->event.pressed) { // Start playing
285         stop_all_notes();
286         music_sequence_recording = false;
287         music_sequence_playing = true;
288         music_sequence_position = 0;
289         music_sequence_timer = 0;
290         return false;
291       }
292
293       if (keycode == KC_UP) {
294         if (record->event.pressed)
295             music_sequence_interval-=10;
296         return false;
297       }
298
299       if (keycode == KC_DOWN) {
300         if (record->event.pressed)
301             music_sequence_interval+=10;
302         return false;
303       }
304
305       float freq = ((float)220.0)*pow(2.0, -5.0)*pow(2.0,(starting_note + SCALE[record->event.key.col + offset])/12.0+(MATRIX_ROWS - record->event.key.row));
306       if (record->event.pressed) {
307         play_note(freq, 0xF);
308         if (music_sequence_recording) {
309           music_sequence[music_sequence_count] = freq;
310           music_sequence_count++;
311         }
312       } else {
313         stop_note(freq);
314       }
315
316       if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through
317         return false;
318     }
319   #endif
320
321 #ifndef DISABLE_LEADER
322   // Leader key set-up
323   if (record->event.pressed) {
324     if (!leading && keycode == KC_LEAD) {
325       leader_start();
326       leading = true;
327       leader_time = timer_read();
328       leader_sequence_size = 0;
329       leader_sequence[0] = 0;
330       leader_sequence[1] = 0;
331       leader_sequence[2] = 0;
332       leader_sequence[3] = 0;
333       leader_sequence[4] = 0;
334       return false;
335     }
336     if (leading && timer_elapsed(leader_time) < LEADER_TIMEOUT) {
337       leader_sequence[leader_sequence_size] = keycode;
338       leader_sequence_size++;
339       return false;
340     }
341   }
342 #endif
343
344 #define DISABLE_CHORDING
345 #ifndef DISABLE_CHORDING
346
347   if (keycode >= QK_CHORDING && keycode <= QK_CHORDING_MAX) {
348     if (record->event.pressed) {
349       if (!chording) {
350         chording = true;
351         for (uint8_t i = 0; i < CHORDING_MAX; i++)
352           chord_keys[i] = 0;
353         chord_key_count = 0;
354         chord_key_down = 0;
355       }
356       chord_keys[chord_key_count] = (keycode & 0xFF);
357       chord_key_count++;
358       chord_key_down++;
359       return false;
360     } else {
361       if (chording) {
362         chord_key_down--;
363         if (chord_key_down == 0) {
364           chording = false;
365           // Chord Dictionary
366           if (keys_chord((uint8_t[]){KC_ENTER, KC_SPACE})) {
367             register_code(KC_A);
368             unregister_code(KC_A);
369             return false;
370           }
371           for (uint8_t i = 0; i < chord_key_count; i++) {
372             register_code(chord_keys[i]);
373             unregister_code(chord_keys[i]);
374             return false;
375           }
376         }
377       }
378     }
379   }
380
381 #endif
382
383 #ifdef UNICODE_ENABLE
384
385   if (keycode > QK_UNICODE && record->event.pressed) {
386     uint16_t unicode = keycode & 0x7FFF;
387     switch(input_mode) {
388       case UC_OSX:
389         register_code(KC_LALT);
390         break;
391       case UC_LNX:
392         register_code(KC_LCTL);
393         register_code(KC_LSFT);
394         register_code(KC_U);
395         unregister_code(KC_U);
396         break;
397       case UC_WIN:
398         register_code(KC_LALT);
399         register_code(KC_PPLS);
400         unregister_code(KC_PPLS);
401         break;
402     }
403     for(int i = 3; i >= 0; i--) {
404         uint8_t digit = ((unicode >> (i*4)) & 0xF);
405         register_code(hex_to_keycode(digit));
406         unregister_code(hex_to_keycode(digit));
407     }
408     switch(input_mode) {
409       case UC_OSX:
410       case UC_WIN:
411         unregister_code(KC_LALT);
412         break;
413       case UC_LNX:
414         unregister_code(KC_LCTL);
415         unregister_code(KC_LSFT);
416         break;
417     }
418   }
419
420 #endif
421
422   // Shift / paren setup
423
424   switch(keycode) {
425     case RESET:
426       if (record->event.pressed) {
427         clear_keyboard();
428         #ifdef AUDIO_ENABLE
429           stop_all_notes();
430           shutdown_user();
431         #endif
432         _delay_ms(250);
433         #ifdef ATREUS_ASTAR
434             *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
435         #endif
436         bootloader_jump();
437         return false;
438       }
439       break;
440     case DEBUG:
441       if (record->event.pressed) {
442           print("\nDEBUG: enabled.\n");
443           debug_enable = true;
444           return false;
445       }
446       break;
447     case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_UNSWAP_ALT_GUI:
448       if (record->event.pressed) {
449         // MAGIC actions (BOOTMAGIC without the boot)
450         if (!eeconfig_is_enabled()) {
451             eeconfig_init();
452         }
453         /* keymap config */
454         keymap_config.raw = eeconfig_read_keymap();
455         if (keycode == MAGIC_SWAP_CONTROL_CAPSLOCK) {
456             keymap_config.swap_control_capslock = 1;
457         } else if (keycode == MAGIC_CAPSLOCK_TO_CONTROL) {
458             keymap_config.capslock_to_control = 1;
459         } else if (keycode == MAGIC_SWAP_LALT_LGUI) {
460             keymap_config.swap_lalt_lgui = 1;
461         } else if (keycode == MAGIC_SWAP_RALT_RGUI) {
462             keymap_config.swap_ralt_rgui = 1;
463         } else if (keycode == MAGIC_NO_GUI) {
464             keymap_config.no_gui = 1;
465         } else if (keycode == MAGIC_SWAP_GRAVE_ESC) {
466             keymap_config.swap_grave_esc = 1;
467         } else if (keycode == MAGIC_SWAP_BACKSLASH_BACKSPACE) {
468             keymap_config.swap_backslash_backspace = 1;
469         } else if (keycode == MAGIC_HOST_NKRO) {
470             keymap_config.nkro = 1;
471         } else if (keycode == MAGIC_SWAP_ALT_GUI) {
472             keymap_config.swap_lalt_lgui = 1;
473             keymap_config.swap_ralt_rgui = 1;
474         }
475         /* UNs */
476         else if (keycode == MAGIC_UNSWAP_CONTROL_CAPSLOCK) {
477             keymap_config.swap_control_capslock = 0;
478         } else if (keycode == MAGIC_UNCAPSLOCK_TO_CONTROL) {
479             keymap_config.capslock_to_control = 0;
480         } else if (keycode == MAGIC_UNSWAP_LALT_LGUI) {
481             keymap_config.swap_lalt_lgui = 0;
482         } else if (keycode == MAGIC_UNSWAP_RALT_RGUI) {
483             keymap_config.swap_ralt_rgui = 0;
484         } else if (keycode == MAGIC_UNNO_GUI) {
485             keymap_config.no_gui = 0;
486         } else if (keycode == MAGIC_UNSWAP_GRAVE_ESC) {
487             keymap_config.swap_grave_esc = 0;
488         } else if (keycode == MAGIC_UNSWAP_BACKSLASH_BACKSPACE) {
489             keymap_config.swap_backslash_backspace = 0;
490         } else if (keycode == MAGIC_UNHOST_NKRO) {
491             keymap_config.nkro = 0;
492         } else if (keycode == MAGIC_UNSWAP_ALT_GUI) {
493             keymap_config.swap_lalt_lgui = 0;
494             keymap_config.swap_ralt_rgui = 0;
495         }
496         eeconfig_update_keymap(keymap_config.raw);
497         return false;
498       }
499       break;
500     case KC_LSPO: {
501       if (record->event.pressed) {
502         shift_interrupted[0] = false;
503         register_mods(MOD_BIT(KC_LSFT));
504       }
505       else {
506         if (!shift_interrupted[0]) {
507           register_code(LSPO_KEY);
508           unregister_code(LSPO_KEY);
509         }
510         unregister_mods(MOD_BIT(KC_LSFT));
511       }
512       return false;
513       break;
514     }
515
516     case KC_RSPC: {
517       if (record->event.pressed) {
518         shift_interrupted[1] = false;
519         register_mods(MOD_BIT(KC_RSFT));
520       }
521       else {
522         if (!shift_interrupted[1]) {
523           register_code(RSPC_KEY);
524           unregister_code(RSPC_KEY);
525         }
526         unregister_mods(MOD_BIT(KC_RSFT));
527       }
528       return false;
529       break;
530     }
531     default: {
532       shift_interrupted[0] = true;
533       shift_interrupted[1] = true;
534       break;
535     }
536   }
537
538   return process_action_kb(record);
539 }
540
541 const bool ascii_to_qwerty_shift_lut[0x80] PROGMEM = {
542     0, 0, 0, 0, 0, 0, 0, 0,
543     0, 0, 0, 0, 0, 0, 0, 0,
544     0, 0, 0, 0, 0, 0, 0, 0,
545     0, 0, 0, 0, 0, 0, 0, 0,
546     0, 1, 1, 1, 1, 1, 1, 0,
547     1, 1, 1, 1, 0, 0, 0, 0,
548     0, 0, 0, 0, 0, 0, 0, 0,
549     0, 0, 1, 0, 1, 0, 1, 1,
550     1, 1, 1, 1, 1, 1, 1, 1,
551     1, 1, 1, 1, 1, 1, 1, 1,
552     1, 1, 1, 1, 1, 1, 1, 1,
553     1, 1, 1, 0, 0, 0, 1, 1,
554     0, 0, 0, 0, 0, 0, 0, 0,
555     0, 0, 0, 0, 0, 0, 0, 0,
556     0, 0, 0, 0, 0, 0, 0, 0,
557     0, 0, 0, 1, 1, 1, 1, 0
558 };
559
560 const uint8_t ascii_to_qwerty_keycode_lut[0x80] PROGMEM = {
561     0, 0, 0, 0, 0, 0, 0, 0,
562     KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
563     0, 0, 0, 0, 0, 0, 0, 0,
564     0, 0, 0, KC_ESC, 0, 0, 0, 0,
565     KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
566     KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
567     KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
568     KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
569     KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
570     KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
571     KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
572     KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
573     KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
574     KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
575     KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
576     KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
577 };
578
579 /* for users whose OSes are set to Colemak */
580 #if 0
581 #include "keymap_colemak.h"
582
583 const bool ascii_to_colemak_shift_lut[0x80] PROGMEM = {
584     0, 0, 0, 0, 0, 0, 0, 0,
585     0, 0, 0, 0, 0, 0, 0, 0,
586     0, 0, 0, 0, 0, 0, 0, 0,
587     0, 0, 0, 0, 0, 0, 0, 0,
588     0, 1, 1, 1, 1, 1, 1, 0,
589     1, 1, 1, 1, 0, 0, 0, 0,
590     0, 0, 0, 0, 0, 0, 0, 0,
591     0, 0, 1, 0, 1, 0, 1, 1,
592     1, 1, 1, 1, 1, 1, 1, 1,
593     1, 1, 1, 1, 1, 1, 1, 1,
594     1, 1, 1, 1, 1, 1, 1, 1,
595     1, 1, 1, 0, 0, 0, 1, 1,
596     0, 0, 0, 0, 0, 0, 0, 0,
597     0, 0, 0, 0, 0, 0, 0, 0,
598     0, 0, 0, 0, 0, 0, 0, 0,
599     0, 0, 0, 1, 1, 1, 1, 0
600 };
601
602 const uint8_t ascii_to_colemak_keycode_lut[0x80] PROGMEM = {
603     0, 0, 0, 0, 0, 0, 0, 0,
604     KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
605     0, 0, 0, 0, 0, 0, 0, 0,
606     0, 0, 0, KC_ESC, 0, 0, 0, 0,
607     KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
608     KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
609     KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
610     KC_8, KC_9, CM_SCLN, CM_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
611     KC_2, CM_A, CM_B, CM_C, CM_D, CM_E, CM_F, CM_G,
612     CM_H, CM_I, CM_J, CM_K, CM_L, CM_M, CM_N, CM_O,
613     CM_P, CM_Q, CM_R, CM_S, CM_T, CM_U, CM_V, CM_W,
614     CM_X, CM_Y, CM_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
615     KC_GRV, CM_A, CM_B, CM_C, CM_D, CM_E, CM_F, CM_G,
616     CM_H, CM_I, CM_J, CM_K, CM_L, CM_M, CM_N, CM_O,
617     CM_P, CM_Q, CM_R, CM_S, CM_T, CM_U, CM_V, CM_W,
618     CM_X, CM_Y, CM_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
619 };
620
621 #endif
622
623 void send_string(const char *str) {
624     while (1) {
625         uint8_t keycode;
626         uint8_t ascii_code = pgm_read_byte(str);
627         if (!ascii_code) break;
628         keycode = pgm_read_byte(&ascii_to_qwerty_keycode_lut[ascii_code]);
629         if (pgm_read_byte(&ascii_to_qwerty_shift_lut[ascii_code])) {
630             register_code(KC_LSFT);
631             register_code(keycode);
632             unregister_code(keycode);
633             unregister_code(KC_LSFT);
634         }
635         else {
636             register_code(keycode);
637             unregister_code(keycode);
638         }
639         ++str;
640     }
641 }
642
643 void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
644   if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
645     layer_on(layer3);
646   } else {
647     layer_off(layer3);
648   }
649 }
650
651 void matrix_init_quantum() {
652   #ifdef BACKLIGHT_ENABLE
653     backlight_init_ports();
654   #endif
655   matrix_init_kb();
656 }
657
658 void matrix_scan_quantum() {
659   #ifdef AUDIO_ENABLE
660   if (music_sequence_playing) {
661     if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) {
662       music_sequence_timer = timer_read();
663       stop_note(music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)]);
664       play_note(music_sequence[music_sequence_position], 0xF);
665       music_sequence_position = (music_sequence_position + 1) % music_sequence_count;
666     }
667   }
668
669   #endif
670
671   matrix_scan_kb();
672 }
673
674 #ifdef AUDIO_ENABLE
675   bool is_music_on(void) {
676       return (music_activated != 0);
677   }
678
679   void music_toggle(void) {
680       if (!music_activated) {
681           music_on();
682       } else {
683           music_off();
684       }
685   }
686
687   void music_on(void) {
688       music_activated = 1;
689       music_on_user();
690   }
691
692   void music_off(void) {
693       music_activated = 0;
694       stop_all_notes();
695   }
696
697 #endif
698
699
700 #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
701
702 static const uint8_t backlight_pin = BACKLIGHT_PIN;
703
704 #if BACKLIGHT_PIN == B7
705 #  define COM1x1 COM1C1
706 #  define OCR1x  OCR1C
707 #elif BACKLIGHT_PIN == B6
708 #  define COM1x1 COM1B1
709 #  define OCR1x  OCR1B
710 #elif BACKLIGHT_PIN == B5
711 #  define COM1x1 COM1A1
712 #  define OCR1x  OCR1A
713 #else
714 #  error "Backlight pin not supported - use B5, B6, or B7"
715 #endif
716
717 __attribute__ ((weak))
718 void backlight_init_ports(void)
719 {
720
721   // Setup backlight pin as output and output low.
722   // DDRx |= n
723   _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
724   // PORTx &= ~n
725   _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
726
727   // Use full 16-bit resolution.
728   ICR1 = 0xFFFF;
729
730   // I could write a wall of text here to explain... but TL;DW
731   // Go read the ATmega32u4 datasheet.
732   // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
733
734   // Pin PB7 = OCR1C (Timer 1, Channel C)
735   // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
736   // (i.e. start high, go low when counter matches.)
737   // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
738   // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
739
740   TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
741   TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
742
743   backlight_init();
744   #ifdef BACKLIGHT_BREATHING
745     breathing_defaults();
746   #endif
747 }
748
749 __attribute__ ((weak))
750 void backlight_set(uint8_t level)
751 {
752   // Prevent backlight blink on lowest level
753   // PORTx &= ~n
754   _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
755
756   if ( level == 0 ) {
757     // Turn off PWM control on backlight pin, revert to output low.
758     TCCR1A &= ~(_BV(COM1x1));
759     OCR1x = 0x0;
760   } else if ( level == BACKLIGHT_LEVELS ) {
761     // Turn on PWM control of backlight pin
762     TCCR1A |= _BV(COM1x1);
763     // Set the brightness
764     OCR1x = 0xFFFF;
765   } else {
766     // Turn on PWM control of backlight pin
767     TCCR1A |= _BV(COM1x1);
768     // Set the brightness
769     OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
770   }
771
772   #ifdef BACKLIGHT_BREATHING
773     breathing_intensity_default();
774   #endif
775 }
776
777
778 #ifdef BACKLIGHT_BREATHING
779
780 #define BREATHING_NO_HALT  0
781 #define BREATHING_HALT_OFF 1
782 #define BREATHING_HALT_ON  2
783
784 static uint8_t breath_intensity;
785 static uint8_t breath_speed;
786 static uint16_t breathing_index;
787 static uint8_t breathing_halt;
788
789 void breathing_enable(void)
790 {
791     if (get_backlight_level() == 0)
792     {
793         breathing_index = 0;
794     }
795     else
796     {
797         // Set breathing_index to be at the midpoint (brightest point)
798         breathing_index = 0x20 << breath_speed;
799     }
800
801     breathing_halt = BREATHING_NO_HALT;
802
803     // Enable breathing interrupt
804     TIMSK1 |= _BV(OCIE1A);
805 }
806
807 void breathing_pulse(void)
808 {
809     if (get_backlight_level() == 0)
810     {
811         breathing_index = 0;
812     }
813     else
814     {
815         // Set breathing_index to be at the midpoint + 1 (brightest point)
816         breathing_index = 0x21 << breath_speed;
817     }
818
819     breathing_halt = BREATHING_HALT_ON;
820
821     // Enable breathing interrupt
822     TIMSK1 |= _BV(OCIE1A);
823 }
824
825 void breathing_disable(void)
826 {
827     // Disable breathing interrupt
828     TIMSK1 &= ~_BV(OCIE1A);
829     backlight_set(get_backlight_level());
830 }
831
832 void breathing_self_disable(void)
833 {
834     if (get_backlight_level() == 0)
835     {
836         breathing_halt = BREATHING_HALT_OFF;
837     }
838     else
839     {
840         breathing_halt = BREATHING_HALT_ON;
841     }
842
843     //backlight_set(get_backlight_level());
844 }
845
846 void breathing_toggle(void)
847 {
848     if (!is_breathing())
849     {
850         if (get_backlight_level() == 0)
851         {
852             breathing_index = 0;
853         }
854         else
855         {
856             // Set breathing_index to be at the midpoint + 1 (brightest point)
857             breathing_index = 0x21 << breath_speed;
858         }
859
860         breathing_halt = BREATHING_NO_HALT;
861     }
862
863     // Toggle breathing interrupt
864     TIMSK1 ^= _BV(OCIE1A);
865
866     // Restore backlight level
867     if (!is_breathing())
868     {
869         backlight_set(get_backlight_level());
870     }
871 }
872
873 bool is_breathing(void)
874 {
875     return (TIMSK1 && _BV(OCIE1A));
876 }
877
878 void breathing_intensity_default(void)
879 {
880     //breath_intensity = (uint8_t)((uint16_t)100 * (uint16_t)get_backlight_level() / (uint16_t)BACKLIGHT_LEVELS);
881     breath_intensity = ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2));
882 }
883
884 void breathing_intensity_set(uint8_t value)
885 {
886     breath_intensity = value;
887 }
888
889 void breathing_speed_default(void)
890 {
891     breath_speed = 4;
892 }
893
894 void breathing_speed_set(uint8_t value)
895 {
896     bool is_breathing_now = is_breathing();
897     uint8_t old_breath_speed = breath_speed;
898
899     if (is_breathing_now)
900     {
901         // Disable breathing interrupt
902         TIMSK1 &= ~_BV(OCIE1A);
903     }
904
905     breath_speed = value;
906
907     if (is_breathing_now)
908     {
909         // Adjust index to account for new speed
910         breathing_index = (( (uint8_t)( (breathing_index) >> old_breath_speed ) ) & 0x3F) << breath_speed;
911
912         // Enable breathing interrupt
913         TIMSK1 |= _BV(OCIE1A);
914     }
915
916 }
917
918 void breathing_speed_inc(uint8_t value)
919 {
920     if ((uint16_t)(breath_speed - value) > 10 )
921     {
922         breathing_speed_set(0);
923     }
924     else
925     {
926         breathing_speed_set(breath_speed - value);
927     }
928 }
929
930 void breathing_speed_dec(uint8_t value)
931 {
932     if ((uint16_t)(breath_speed + value) > 10 )
933     {
934         breathing_speed_set(10);
935     }
936     else
937     {
938         breathing_speed_set(breath_speed + value);
939     }
940 }
941
942 void breathing_defaults(void)
943 {
944     breathing_intensity_default();
945     breathing_speed_default();
946     breathing_halt = BREATHING_NO_HALT;
947 }
948
949 /* Breathing Sleep LED brighness(PWM On period) table
950  * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
951  *
952  * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
953  * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
954  */
955 static const uint8_t breathing_table[64] PROGMEM = {
956   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   1,   2,   4,   6,  10,
957  15,  23,  32,  44,  58,  74,  93, 113, 135, 157, 179, 199, 218, 233, 245, 252,
958 255, 252, 245, 233, 218, 199, 179, 157, 135, 113,  93,  74,  58,  44,  32,  23,
959  15,  10,   6,   4,   2,   1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
960 };
961
962 ISR(TIMER1_COMPA_vect)
963 {
964     // OCR1x = (pgm_read_byte(&breathing_table[ ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F ] )) * breath_intensity;
965
966
967     uint8_t local_index = ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F;
968
969     if (((breathing_halt == BREATHING_HALT_ON) && (local_index == 0x20)) || ((breathing_halt == BREATHING_HALT_OFF) && (local_index == 0x3F)))
970     {
971         // Disable breathing interrupt
972         TIMSK1 &= ~_BV(OCIE1A);
973     }
974
975     OCR1x = (uint16_t)(((uint16_t)pgm_read_byte(&breathing_table[local_index]) * 257)) >> breath_intensity;
976
977 }
978
979
980
981 #endif // breathing
982
983 #else // backlight
984
985 __attribute__ ((weak))
986 void backlight_init_ports(void)
987 {
988
989 }
990
991 __attribute__ ((weak))
992 void backlight_set(uint8_t level)
993 {
994
995 }
996
997 #endif // backlight
998
999
1000
1001 __attribute__ ((weak))
1002 void led_set_user(uint8_t usb_led) {
1003
1004 }
1005
1006 __attribute__ ((weak))
1007 void led_set_kb(uint8_t usb_led) {
1008     led_set_user(usb_led);
1009 }
1010
1011 __attribute__ ((weak))
1012 void led_init_ports(void)
1013 {
1014
1015 }
1016
1017 __attribute__ ((weak))
1018 void led_set(uint8_t usb_led)
1019 {
1020
1021   // Example LED Code
1022   //
1023     // // Using PE6 Caps Lock LED
1024     // if (usb_led & (1<<USB_LED_CAPS_LOCK))
1025     // {
1026     //     // Output high.
1027     //     DDRE |= (1<<6);
1028     //     PORTE |= (1<<6);
1029     // }
1030     // else
1031     // {
1032     //     // Output low.
1033     //     DDRE &= ~(1<<6);
1034     //     PORTE &= ~(1<<6);
1035     // }
1036
1037   led_set_kb(usb_led);
1038 }
1039
1040
1041 //------------------------------------------------------------------------------
1042 // Override these functions in your keymap file to play different tunes on
1043 // different events such as startup and bootloader jump
1044
1045 __attribute__ ((weak))
1046 void startup_user() {}
1047
1048 __attribute__ ((weak))
1049 void shutdown_user() {}
1050
1051 __attribute__ ((weak))
1052 void music_on_user() {}
1053
1054 __attribute__ ((weak))
1055 void audio_on_user() {}
1056
1057 __attribute__ ((weak))
1058 void music_scale_user() {}
1059
1060 //------------------------------------------------------------------------------