]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/quantum.c
Cleans up quantum/keymap situation, removes extra lufa folders (#416)
[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[3] = {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       return false;
339     }
340     if (leading && timer_elapsed(leader_time) < LEADER_TIMEOUT) {
341       leader_sequence[leader_sequence_size] = keycode;
342       leader_sequence_size++;
343       return false;
344     }
345   }
346 #endif
347
348 #define DISABLE_CHORDING
349 #ifndef DISABLE_CHORDING
350
351   if (keycode >= QK_CHORDING && keycode <= QK_CHORDING_MAX) {
352     if (record->event.pressed) {
353       if (!chording) {
354         chording = true;
355         for (uint8_t i = 0; i < CHORDING_MAX; i++)
356           chord_keys[i] = 0;
357         chord_key_count = 0;
358         chord_key_down = 0;
359       }
360       chord_keys[chord_key_count] = (keycode & 0xFF);
361       chord_key_count++;
362       chord_key_down++;
363       return false;
364     } else {
365       if (chording) {
366         chord_key_down--;
367         if (chord_key_down == 0) {
368           chording = false;
369           // Chord Dictionary
370           if (keys_chord((uint8_t[]){KC_ENTER, KC_SPACE})) {
371             register_code(KC_A);
372             unregister_code(KC_A);
373             return false;
374           }
375           for (uint8_t i = 0; i < chord_key_count; i++) {
376             register_code(chord_keys[i]);
377             unregister_code(chord_keys[i]);
378             return false;
379           }
380         }
381       }
382     }
383   }
384
385 #endif
386
387 #ifdef UNICODE_ENABLE
388
389   if (keycode > QK_UNICODE && record->event.pressed) {
390     uint16_t unicode = keycode & 0x7FFF;
391     switch(input_mode) {
392       case UC_OSX:
393         register_code(KC_LALT);
394         break;
395       case UC_LNX:
396         register_code(KC_LCTL);
397         register_code(KC_LSFT);
398         register_code(KC_U);
399         unregister_code(KC_U);
400         break;
401       case UC_WIN:
402         register_code(KC_LALT);
403         register_code(KC_PPLS);
404         unregister_code(KC_PPLS);
405         break;
406     }
407     for(int i = 3; i >= 0; i--) {
408         uint8_t digit = ((unicode >> (i*4)) & 0xF);
409         register_code(hex_to_keycode(digit));
410         unregister_code(hex_to_keycode(digit));
411     }
412     switch(input_mode) {
413       case UC_OSX:
414       case UC_WIN:
415         unregister_code(KC_LALT);
416         break;
417       case UC_LNX:
418         unregister_code(KC_LCTL);
419         unregister_code(KC_LSFT);
420         break;
421     }
422   }
423
424 #endif
425
426   // Shift / paren setup
427
428   switch(keycode) {
429     case RESET:
430       if (record->event.pressed) {
431         clear_keyboard();
432         #ifdef AUDIO_ENABLE
433           stop_all_notes();
434           shutdown_user();
435         #endif
436         _delay_ms(250);
437         #ifdef ATREUS_ASTAR
438             *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
439         #endif
440         bootloader_jump();
441         return false;
442       }
443       break;
444     case DEBUG:
445       if (record->event.pressed) {
446           print("\nDEBUG: enabled.\n");
447           debug_enable = true;
448           return false;
449       }
450       break;
451     case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_UNSWAP_ALT_GUI:
452       if (record->event.pressed) {
453         // MAGIC actions (BOOTMAGIC without the boot)
454         if (!eeconfig_is_enabled()) {
455             eeconfig_init();
456         }
457         /* keymap config */
458         keymap_config.raw = eeconfig_read_keymap();
459         if (keycode == MAGIC_SWAP_CONTROL_CAPSLOCK) {
460             keymap_config.swap_control_capslock = 1;
461         } else if (keycode == MAGIC_CAPSLOCK_TO_CONTROL) {
462             keymap_config.capslock_to_control = 1;
463         } else if (keycode == MAGIC_SWAP_LALT_LGUI) {
464             keymap_config.swap_lalt_lgui = 1;
465         } else if (keycode == MAGIC_SWAP_RALT_RGUI) {
466             keymap_config.swap_ralt_rgui = 1;
467         } else if (keycode == MAGIC_NO_GUI) {
468             keymap_config.no_gui = 1;
469         } else if (keycode == MAGIC_SWAP_GRAVE_ESC) {
470             keymap_config.swap_grave_esc = 1;
471         } else if (keycode == MAGIC_SWAP_BACKSLASH_BACKSPACE) {
472             keymap_config.swap_backslash_backspace = 1;
473         } else if (keycode == MAGIC_HOST_NKRO) {
474             keymap_config.nkro = 1;
475         } else if (keycode == MAGIC_SWAP_ALT_GUI) {
476             keymap_config.swap_lalt_lgui = 1;
477             keymap_config.swap_ralt_rgui = 1;
478         }
479         /* UNs */
480         else if (keycode == MAGIC_UNSWAP_CONTROL_CAPSLOCK) {
481             keymap_config.swap_control_capslock = 0;
482         } else if (keycode == MAGIC_UNCAPSLOCK_TO_CONTROL) {
483             keymap_config.capslock_to_control = 0;
484         } else if (keycode == MAGIC_UNSWAP_LALT_LGUI) {
485             keymap_config.swap_lalt_lgui = 0;
486         } else if (keycode == MAGIC_UNSWAP_RALT_RGUI) {
487             keymap_config.swap_ralt_rgui = 0;
488         } else if (keycode == MAGIC_UNNO_GUI) {
489             keymap_config.no_gui = 0;
490         } else if (keycode == MAGIC_UNSWAP_GRAVE_ESC) {
491             keymap_config.swap_grave_esc = 0;
492         } else if (keycode == MAGIC_UNSWAP_BACKSLASH_BACKSPACE) {
493             keymap_config.swap_backslash_backspace = 0;
494         } else if (keycode == MAGIC_UNHOST_NKRO) {
495             keymap_config.nkro = 0;
496         } else if (keycode == MAGIC_UNSWAP_ALT_GUI) {
497             keymap_config.swap_lalt_lgui = 0;
498             keymap_config.swap_ralt_rgui = 0;
499         }
500         eeconfig_update_keymap(keymap_config.raw);
501         return false;
502       }
503       break;
504     case KC_LSPO: {
505       if (record->event.pressed) {
506         shift_interrupted[0] = false;
507         register_mods(MOD_LSFT);
508       }
509       else {
510         if (!shift_interrupted[0]) {
511           register_code(LSPO_KEY);
512           unregister_code(LSPO_KEY);
513         }
514         unregister_mods(MOD_LSFT);
515       }
516       return false;
517       break;
518     }
519
520     case KC_RSPC: {
521       if (record->event.pressed) {
522         shift_interrupted[1] = false;
523         register_mods(MOD_RSFT);
524       }
525       else {
526         if (!shift_interrupted[1]) {
527           register_code(RSPC_KEY);
528           unregister_code(RSPC_KEY);
529         }
530         unregister_mods(MOD_RSFT);
531       }
532       return false;
533       break;
534     }
535     default: {
536       shift_interrupted[0] = true;
537       shift_interrupted[1] = true;
538       break;
539     }
540   }
541
542   return process_action_kb(record);
543 }
544
545 const bool ascii_to_qwerty_shift_lut[0x80] PROGMEM = {
546     0, 0, 0, 0, 0, 0, 0, 0,
547     0, 0, 0, 0, 0, 0, 0, 0,
548     0, 0, 0, 0, 0, 0, 0, 0,
549     0, 0, 0, 0, 0, 0, 0, 0,
550     0, 1, 1, 1, 1, 1, 1, 0,
551     1, 1, 1, 1, 0, 0, 0, 0,
552     0, 0, 0, 0, 0, 0, 0, 0,
553     0, 0, 1, 0, 1, 0, 1, 1,
554     1, 1, 1, 1, 1, 1, 1, 1,
555     1, 1, 1, 1, 1, 1, 1, 1,
556     1, 1, 1, 1, 1, 1, 1, 1,
557     1, 1, 1, 0, 0, 0, 1, 1,
558     0, 0, 0, 0, 0, 0, 0, 0,
559     0, 0, 0, 0, 0, 0, 0, 0,
560     0, 0, 0, 0, 0, 0, 0, 0,
561     0, 0, 0, 1, 1, 1, 1, 0
562 };
563
564 const uint8_t ascii_to_qwerty_keycode_lut[0x80] PROGMEM = {
565     0, 0, 0, 0, 0, 0, 0, 0,
566     KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
567     0, 0, 0, 0, 0, 0, 0, 0,
568     0, 0, 0, KC_ESC, 0, 0, 0, 0,
569     KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
570     KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
571     KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
572     KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
573     KC_2, 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_6, KC_MINS,
577     KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
578     KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
579     KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
580     KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
581 };
582
583 /* for users whose OSes are set to Colemak */
584 #if 0
585 #include "keymap_colemak.h"
586
587 const bool ascii_to_colemak_shift_lut[0x80] PROGMEM = {
588     0, 0, 0, 0, 0, 0, 0, 0,
589     0, 0, 0, 0, 0, 0, 0, 0,
590     0, 0, 0, 0, 0, 0, 0, 0,
591     0, 0, 0, 0, 0, 0, 0, 0,
592     0, 1, 1, 1, 1, 1, 1, 0,
593     1, 1, 1, 1, 0, 0, 0, 0,
594     0, 0, 0, 0, 0, 0, 0, 0,
595     0, 0, 1, 0, 1, 0, 1, 1,
596     1, 1, 1, 1, 1, 1, 1, 1,
597     1, 1, 1, 1, 1, 1, 1, 1,
598     1, 1, 1, 1, 1, 1, 1, 1,
599     1, 1, 1, 0, 0, 0, 1, 1,
600     0, 0, 0, 0, 0, 0, 0, 0,
601     0, 0, 0, 0, 0, 0, 0, 0,
602     0, 0, 0, 0, 0, 0, 0, 0,
603     0, 0, 0, 1, 1, 1, 1, 0
604 };
605
606 const uint8_t ascii_to_colemak_keycode_lut[0x80] PROGMEM = {
607     0, 0, 0, 0, 0, 0, 0, 0,
608     KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
609     0, 0, 0, 0, 0, 0, 0, 0,
610     0, 0, 0, KC_ESC, 0, 0, 0, 0,
611     KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
612     KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
613     KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
614     KC_8, KC_9, CM_SCLN, CM_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
615     KC_2, 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_6, KC_MINS,
619     KC_GRV, CM_A, CM_B, CM_C, CM_D, CM_E, CM_F, CM_G,
620     CM_H, CM_I, CM_J, CM_K, CM_L, CM_M, CM_N, CM_O,
621     CM_P, CM_Q, CM_R, CM_S, CM_T, CM_U, CM_V, CM_W,
622     CM_X, CM_Y, CM_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
623 };
624
625 #endif
626
627 void send_string(const char *str) {
628     while (1) {
629         uint8_t keycode;
630         uint8_t ascii_code = pgm_read_byte(str);
631         if (!ascii_code) break;
632         keycode = pgm_read_byte(&ascii_to_qwerty_keycode_lut[ascii_code]);
633         if (pgm_read_byte(&ascii_to_qwerty_shift_lut[ascii_code])) {
634             register_code(KC_LSFT);
635             register_code(keycode);
636             unregister_code(keycode);
637             unregister_code(KC_LSFT);
638         }
639         else {
640             register_code(keycode);
641             unregister_code(keycode);
642         }
643         ++str;
644     }
645 }
646
647 void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
648   if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
649     layer_on(layer3);
650   } else {
651     layer_off(layer3);
652   }
653 }
654
655 void matrix_init_quantum() {
656   matrix_init_kb();
657 }
658
659 void matrix_scan_quantum() {
660   #ifdef AUDIO_ENABLE
661   if (music_sequence_playing) {
662     if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) {
663       music_sequence_timer = timer_read();
664       stop_note(music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)]);
665       play_note(music_sequence[music_sequence_position], 0xF);
666       music_sequence_position = (music_sequence_position + 1) % music_sequence_count;
667     }
668   }
669
670   #endif
671
672   matrix_scan_kb();
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 // Override these functions in your keymap file to play different tunes on
701 // different events such as startup and bootloader jump
702
703 __attribute__ ((weak))
704 void startup_user() {}
705
706 __attribute__ ((weak))
707 void shutdown_user() {}
708
709 __attribute__ ((weak))
710 void music_on_user() {}
711
712 __attribute__ ((weak))
713 void audio_on_user() {}
714
715 __attribute__ ((weak))
716 void music_scale_user() {}
717
718 //------------------------------------------------------------------------------