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