summaryrefslogtreecommitdiff
path: root/keyboards/planck/keymaps/rootiest/keymap.c
blob: 634bf2829e08317a46e9a29c7f19fac2112664a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
/* Copyright 2021 Chris Laprade
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include QMK_KEYBOARD_H
#include "raw_hid.h"
#include "print.h"
#include "muse.h"

/*
 * Define keyboard name to be used by UI.
 * This allows for using a different name
 * than the one defined by QMK for your board.
 */
#define KEEB_MODEL_NAME "The Rootiest BoogieBoard"

// Give names to the layers so they are easier to reference
enum custom_layers {
    _BASE,
    _QWERTY = _BASE,
    _COLEMAK,
    _DVORAK,
    _SYMBLS,
    _RAISE = _SYMBLS,
    _NUMBRS,
    _LOWER = _NUMBRS,
    _PLOVER,
    _FEATURS,
    _ADJUST = _FEATURS,
    _NUMPD,
    _TABULA,
    _MOUSY,
};

#define LOWER MO(_NUMBRS)
#define RAISE MO(_SYMBLS)

// unicode hexes
#define UC_DEG UC(0x00B0)   // °
#define UC_DEGF UC(0x2109)  // ℉

// Time (ms) to wait between frames for the wake rgb animation
#define WAKE_ANIMATION_TIMER_FREQUENCY 50

// Is a display connected
// #define USING_OLED_DISPLAY false

// wait DELAY ms before unregistering media keys
#define TAP_CODE_DELAY 10

// Modifier remover
#define WITHOUT_MODS(...)                      \
    do {                                       \
        const uint8_t _real_mods = get_mods(); \
        clear_mods();                          \
        {__VA_ARGS__} set_mods(_real_mods);    \
    } while (0)

// A whole boatload of audio "songs" defined for use by the keyboard
#ifdef AUDIO_ENABLE
float planck_song[][2]    = SONG(PLANCK_SOUND);
float hello_song[][2]     = SONG(ONE_UP_SOUND2);
float bye_song[][2]       = SONG(GOODBYE_SOUND);
float num_song[][2]       = SONG(DVORAK_SOUND);
float qwerty_song[][2]    = SONG(QWERTY_SOUND);
float colemak_song[][2]   = SONG(COLEMAK_SOUND);
float dvorak_song[][2]    = SONG(DVORAK_SOUND);
float funk_song[][2]      = SONG(COLEMAK_SOUND);
float workman_song[][2]   = SONG(WORKMAN_SOUND);
float term_song[][2]      = SONG(TERMINAL_SOUND);
float lover_song[][2]     = SONG(PLOVER_SOUND);
float ode_song[][2]       = SONG(ODE_TO_JOY);
float rock_song[][2]      = SONG(ROCK_A_BYE_BABY);
float clue_song[][2]      = SONG(CLUEBOARD_SOUND);
float camp_song[][2]      = SONG(CAMPANELLA);
float imp_march_song[][2] = SONG(IMPERIAL_MARCH);
float gameover_song[][2]  = SONG(MARIO_GAMEOVER);
float puzzle_song[][2]    = SONG(ZELDA_PUZZLE2);
float caps_on[][2]        = SONG(CAPS_LOCK_ON_SOUND);
float caps_off[][2]       = SONG(CAPS_LOCK_OFF_SOUND);
float slctl_on[][2]       = SONG(SCROLL_LOCK_ON_SOUND);
float slctl_off[][2]      = SONG(SCROLL_LOCK_OFF_SOUND);
float slalt_on[][2]       = SONG(NUM_LOCK_ON_SOUND);
float slalt_off[][2]      = SONG(NUM_LOCK_OFF_SOUND);
float leader_started[][2] = SONG(LEAD_START_SOUND);
float leader_succeed[][2] = SONG(LEAD_SUCCESS_SOUND);
float leader_fail[][2]    = SONG(LEAD_FAIL_SOUND);
float plover_song[][2]    = SONG(PLOVER_SOUND);
float plover_gb_song[][2] = SONG(PLOVER_GOODBYE_SOUND);
#endif

// Declare stored memory config
typedef union {
    uint32_t raw;
    struct {
        bool    do_wakeup_animation : 1;
        uint8_t rgbcon_tracker : 8;
        bool    do_wakeup_audio : 1;
    };
} user_config_t;

user_config_t user_config;

// Initialize variable holding the binary
// representation of active modifiers.
uint8_t mod_state = 0;

// Declare custom keycodes
enum custom_keycodes {
    MO_BASE = SAFE_RANGE,
    MO_SYMBLS,
    MO_NUMBRS,
    MO_FEATURS,
    QWERTY,
    COLEMAK,
    DVORAK,
    PLOVER,
    BACKLIT,
    MUSIC_ON,
    MUSIC_OFF,
    EXT_PLV,
    MY_RGBCON,
    MY_DEGREES,
    ALT_TAB,
    PRINT_WPM_KEY,
    IRONY,
    WAKE_ANI_TOG,
    WAKE_AUD_TOG,
    DO_RESET,
    KC_PRVWD,
    KC_NXTWD,
    KC_LSTRT,
    KC_LEND,
    KC_DLINE,
    KC_REDO,
    KC_SAVE,
    KC_SLCTALL,
    KC_ALTF4,
    KC_KILL,
    KC_LCUT,
    KC_LCOPY,
    KC_NXTAB,
    KC_PRVTAB,
};

// Declare unicode map array
enum unicode_names { BANG, SNEK };
const uint32_t PROGMEM unicode_map[] = {
    //[UCD_BANG]  = 0x203D,   // ‽
    //[UCD_IRONY] = 0x2E2E,   // ⸮
    [SNEK] = 0x1F40D,  // 🐍
};

enum combo_events { ZC_COPY, XV_PASTE };

const uint16_t PROGMEM copy_combo[]  = {KC_Z, KC_C, COMBO_END};
const uint16_t PROGMEM paste_combo[] = {KC_X, KC_V, COMBO_END};

combo_t key_combos[COMBO_COUNT] = {
    [ZC_COPY]  = COMBO_ACTION(copy_combo),
    [XV_PASTE] = COMBO_ACTION(paste_combo),
};

// Tap Dance key declarations
enum {
    TD_DEG_DEGF,
    TD_SMILEY,
    TD_LSHFT_CAPS,
    TD_LCTL_STICKY,
    TD_LALT_STICKY,
    TD_LOWER,
    TD_RAISE,
};

// Declare available Tap-Dance states
typedef enum {
    TD_NONE,
    TD_SINGLE_TAP,
    TD_DOUBLE_TAP,
    TD_TRIPLE_TAP,
    TD_DOUBLE_SINGLE_TAP,
    TD_SINGLE_HOLD,
    TD_DOUBLE_HOLD,
    TD_TRIPLE_HOLD,
    TD_UNKNOWN,
} td_state_t;

// Tap-Dance struct
typedef struct {
    bool       is_press_action;
    td_state_t state;
} td_tap_t;

// Whether or not to do the wake animation+sound
bool do_wake_animation;

// Variable to keep track of the rgb mode assigned by the RGB_CON key
static uint8_t rgbcon_tracker = 0;

// Used by the on-board WPM tracker
char wpm_str[12];

// Variables used for the alt-tab key
bool     is_alt_tab_active = false;
uint16_t alt_tab_timer     = 0;

// Variables used by the Irony key
#define IRONY_HOLD_DELAY 500
uint16_t irony_pressed_time;
bool     irony_active  = false;
bool     irony_shifted = false;
char     irony_str[4]  = "⸮";
char     bang_str[4]   = "‽";

// Variables used for the rgb wakeup animation
static uint16_t wake_rgb_timer;
static uint8_t  wake_rgb_count = 0;
bool            waking_up      = false;
bool            do_wake_audio  = false;

// Muse variables
bool     muse_mode      = false;
bool     musical_mode   = false;
uint8_t  last_muse_note = 0;
uint16_t muse_counter   = 0;
uint8_t  muse_offset    = 70;
uint16_t muse_tempo     = 50;

// Used by Leader key chords
bool did_leader_succeed;
LEADER_EXTERNS();

// Tap-Dance stuffs, initializing functions that are coded further below
td_state_t cur_dance(qk_tap_dance_state_t* state);
void       sml_finished(qk_tap_dance_state_t* state, void* user_data);
void       sml_reset(qk_tap_dance_state_t* state, void* user_data);
void       scap_finished(qk_tap_dance_state_t* state, void* user_data);
void       scap_reset(qk_tap_dance_state_t* state, void* user_data);
void       slctl_finished(qk_tap_dance_state_t* state, void* user_data);
void       slctl_reset(qk_tap_dance_state_t* state, void* user_data);
void       slalt_finished(qk_tap_dance_state_t* state, void* user_data);
void       slalt_reset(qk_tap_dance_state_t* state, void* user_data);
bool       lctl_sticky = false;
bool       lalt_sticky = false;

// This function is called when lock indicators (caps-lock led) are changed/toggled/updated
bool led_update_user(led_t led_state) {
    rgblight_set_layer_state(10, led_state.caps_lock);
#ifdef AUDIO_ENABLE
    static uint8_t caps_state = 0;
    if (caps_state != led_state.caps_lock) {
        // When the caps-lock led state changes play sounds
        led_state.caps_lock ? PLAY_SONG(caps_on) : PLAY_SONG(caps_off);
        caps_state = led_state.caps_lock;
    }
#endif
    return true;
}

// Define key layout/layers
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {                                             // Define all the layers
    [_BASE] = LAYOUT_planck_mit(                                                                           //
        KC_GESC, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_BSPC,                      //
        LT(_TABULA, KC_TAB), KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT,       //
        TD(TD_LSHFT_CAPS), KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_SFTENT,  //
        TD(TD_LCTL_STICKY), KC_LEAD, KC_LGUI, TD(TD_LALT_STICKY), MO(_SYMBLS), KC_SPC, MO(_NUMBRS), KC_LEFT, KC_DOWN, KC_UP, KC_RGHT),
    /*
    Base Layer [0]
     * ,-----------------------------------------------------------------------------------.
     * |Gr/ESC|   Q  |   W  |   E  |   R  |   T  |   Y  |   U  |   I  |   O  |   P  |BckSpc|
     * |------+------+------+------+------+------+------+------+------+------+------+------|
     * |Tabula|   A  |   S  |   D  |   F  |   G  |   H  |   J  |   K  |   L  |   ;  |   '  |
     * |------+------+------+------+------+------+------+------+------+------+------+------|
     * |SH/CAP|   Z  |   X  |   C  |   V  |   B  |   N  |   M  |   ,  |   .  |   /  |Enter |
     * |------+------+------+------+------+------+------+------+------+------+------+------|
     * | LCtrl|Leader|  OS  |  Alt |Symbol|    Space    |Number| Left | Dwn  |  Up  | Right|
     * `-----------------------------------------------------------------------------------'
    */

    [_COLEMAK] = LAYOUT_planck_mit(                                                                        //
        KC_GESC, KC_Q, KC_W, KC_F, KC_P, KC_G, KC_J, KC_L, KC_U, KC_Y, KC_SCLN, KC_BSPC,                   //
        LT(_TABULA, KC_TAB), KC_A, KC_R, KC_S, KC_T, KC_D, KC_H, KC_N, KC_E, KC_I, KC_O, KC_QUOT,          //
        TD(TD_LSHFT_CAPS), KC_Z, KC_X, KC_C, KC_V, KC_B, KC_K, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_SFTENT,  //
        TD(TD_LCTL_STICKY), KC_LEAD, KC_LGUI, TD(TD_LALT_STICKY), MO(_SYMBLS), KC_SPC, MO(_NUMBRS), KC_LEFT, KC_DOWN, KC_UP, KC_RGHT),
    /*
    Colemak Layer [1]
     * ,-----------------------------------------------------------------------------------.
     * |Gr/ESC|   Q  |   W  |   F  |   P  |   G  |   J  |   L  |   U  |   Y  |   ;  | Bksp |
     * |------+------+------+------+------+------+------+------+------+------+------+------|
     * |Tabula|   A  |   R  |   S  |   T  |   D  |   H  |   N  |   E  |   I  |   O  |  "   |
     * |------+------+------+------+------+------+------+------+------+------+------+------|
     * |SH/CAP|   Z  |   X  |   C  |   V  |   B  |   K  |   M  |   ,  |   .  |   /  |Enter |
     * |------+------+------+------+------+------+------+------+------+------+------+------|
     * | LCtrl|Leader|  OS  |  Alt |Symbol|    Space    |Number| Left | Dwn  |  Up  | Right|
     * `-----------------------------------------------------------------------------------'
    */

    [_DVORAK] = LAYOUT_planck_mit(                                                                    //
        KC_GESC, KC_QUOT, KC_COMM, KC_DOT, KC_P, KC_Y, KC_F, KC_G, KC_C, KC_R, KC_L, KC_BSPC,         //
        LT(_TABULA, KC_TAB), KC_A, KC_O, KC_E, KC_U, KC_I, KC_D, KC_H, KC_T, KC_TRNS, KC_S, KC_SLSH,  //
        TD(TD_LSHFT_CAPS), KC_SCLN, KC_Q, KC_J, KC_K, KC_X, KC_B, KC_M, KC_W, KC_V, KC_Z, KC_SFTENT,  //
        TD(TD_LCTL_STICKY), KC_LEAD, KC_LGUI, TD(TD_LALT_STICKY), MO(_SYMBLS), KC_SPC, MO(_NUMBRS), KC_LEFT, KC_DOWN, KC_UP, KC_RGHT),
    /* Dvorak Layer [2]
     * ,-----------------------------------------------------------------------------------.
     * |Gr/ESC|   "  |   ,  |   .  |   P  |   Y  |   F  |   G  |   C  |   R  |   L  | Bksp |
     * |------+------+------+------+------+------+------+------+------+------+------+------|
     * |Tabula|   A  |   O  |   E  |   U  |   I  |   D  |   H  |   T  |   N  |   S  |  /   |
     * |------+------+------+------+------+------+------+------+------+------+------+------|
     * |SH/CAP|   ;  |   Q  |   J  |   K  |   X  |   B  |   M  |   W  |   V  |   Z  |Enter |
     * |------+------+------+------+------+------+------+------+------+------+------+------|
     * | LCtrl|Leader|  OS  |  Alt |Symbol|    Space    |Number| Left | Dwn  |  Up  | Right|
     * `-----------------------------------------------------------------------------------'
     */
    [_SYMBLS] = LAYOUT_planck_mit(                                                                                     //
        KC_MPLY, KC_EXLM, KC_AT, KC_HASH, KC_DLR, KC_PERC, KC_CIRC, KC_AMPR, KC_ASTR, KC_LPRN, KC_RPRN, KC_BSPC,       //
        KC_DEL, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_UNDS, KC_PLUS, KC_LCBR, KC_RCBR, KC_PIPE,                 //
        KC_TRNS, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, LSFT(KC_LCTL), LSFT(KC_LCTL), KC_HOME, KC_END, KC_TRNS,  //
        TG(_NUMPD), KC_TRNS, KC_TRNS, KC_TRNS, TO(_BASE), KC_TRNS, KC_TRNS, KC_MNXT, KC_VOLD, KC_VOLU, KC_MPLY),
    /*
    Symbols Layer [3]
    * ,-----------------------------------------------------------------------------------.
    * | Play |   !  |   @  |   #  |   $  |   %  |   ^  |   &  |   *  |   (  |   )  |BckSpc|
    * |------+------+------+------+------+------+------+------+------+------+------+------|
    * |Delete|  F1  |  F2  |  F3  |  F4  |  F5  |  F6  |   _  |   +  |   {  |   }  |   |  |
    * |------+------+------+------+------+------+------+------+------+------+------+------|
    * | Shift|  F7  |  F8  |  F9  |  F10 |  F11 |  F12 |LShLCt|LShLCt| Home |  End |------|
    * |------+------+------+------+------+------+------+------+------+------+------+------|
    * | TG(7)|------|------|------| Base |    -----    | MO(4)| Next | Vol- | Vol+ | Play |
    * `-----------------------------------------------------------------------------------'
    */

    [_NUMBRS] = LAYOUT_planck_mit(                                                                          //
        KC_MPLY, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_BSPC,                       //
        KC_DEL, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_MINS, KC_EQL, KC_LBRC, KC_RBRC, KC_BSLS,       //
        KC_TRNS, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_NUHS, KC_NUBS, KC_PGUP, KC_PGDN, KC_TRNS,  //
        TG(_NUMPD), KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, TO(_BASE), KC_MNXT, KC_VOLD, KC_VOLU, KC_MPLY),

    /*
    Numbers Layer [4]
    * ,-----------------------------------------------------------------------------------.
    * | Play |   1  |   2  |   3  |   4  |   5  |   6  |   7  |   8  |   9  |   0  |BckSpc|
    * |------+------+------+------+------+------+------+------+------+------+------+------|
    * |Delete|  F1  |  F2  |  F3  |  F4  |  F5  |  F6  |   -  |   =  |   [  |   ]  |   \  |
    * |------+------+------+------+------+------+------+------+------+------+------+------|
    * | Shift|  F7  |  F8  |  F9  |  F10 |  F11 |  F12 |   #  |   /  | PgUp | PgDwn|------|
    * |------+------+------+------+------+------+------+------+------+------+------+------|
    * | TG(7)|------|------|------| MO(3)|    -----    | Base | Next | Vol- | Vol+ | Play |
    * `-----------------------------------------------------------------------------------'
    */

    [_PLOVER] = LAYOUT_planck_mit(                                                        //
        KC_1, KC_1, KC_1, KC_1, KC_1, KC_1, KC_1, KC_1, KC_1, KC_1, KC_1, KC_1,           //
        XXXXXXX, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC,     //
        XXXXXXX, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT,  //
        EXT_PLV, XXXXXXX, XXXXXXX, KC_C, KC_V, XXXXXXX, KC_N, KC_M, XXXXXXX, XXXXXXX, XXXXXXX),
    /* Plover Layer [5]
     * ,-----------------------------------------------------------------------------------.
     * |   #  |   #  |   #  |   #  |   #  |   #  |   #  |   #  |   #  |   #  |   #  |   #  |
     * |------+------+------+------+------+------+------+------+------+------+------+------|
     * |      |   S  |   T  |   P  |   H  |   *  |   *  |   F  |   P  |   L  |   T  |   D  |
     * |------+------+------+------+------+------+------+------+------+------+------+------|
     * |      |   S  |   K  |   W  |   R  |   *  |   *  |   R  |   B  |   G  |   S  |   Z  |
     * |------+------+------+------+------+------+------+------+------+------+------+------|
     * | Exit |      |      |   A  |   O  |             |   E  |   U  |      |      |      |
     * `-----------------------------------------------------------------------------------'
     */

    [_FEATURS] = LAYOUT_planck_mit(                                                                                                         //
        LCTL(LALT(KC_DEL)), DO_RESET, DEBUG, RGB_TOG, RGB_MOD, RGB_HUI, RGB_HUD, RGB_SAI, RGB_SAD, TD(TD_DEG_DEGF), TD(TD_SMILEY), KC_DEL,  //
        RGB_VAI, RGB_VAD, MU_MOD, AU_ON, AU_OFF, AG_NORM, AG_SWAP, DF(_BASE), DF(_COLEMAK), DF(_DVORAK), TO(_PLOVER), MY_RGBCON,            //
        KC_TRNS, MUV_DE, MUV_IN, MU_ON, MU_OFF, MI_ON, MI_OFF, TERM_ON, TERM_OFF, CK_ON, CK_OFF, KC_ENTER,                                  //
        KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, TG(_MOUSY), KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS),
    /*
    Special Features Layer [6]
    * ,-----------------------------------------------------------------------------------.
    * |C+A+DL| Reset| Debug|RGBTog|RGBMod| Hue+ | Hue- | Sat+ | Sat- |Unicod| Emoji|Delete|
    * |------+------+------+------+------+------+------+------+------+------+------+------|
    * |RGBVAI|RGBVAD|MU_MOD| AU_ON|AU_OFF|RALTGU|SALTGU| Base |Colemk|Dvorak|Plover|RGBCON|
    * |------+------+------+------+------+------+------+------+------+------+------+------|
    * |------|MUV_DE|MUV_IN| MU_ON|MU_OFF| MI_ON|MI_OFF|TERMON|TRMOFF|CLK-ON|CLKOFF|Enter |
    * |------+------+------+------+------+------+------+------+------+------+------+------|
    * |------|------|------|------|------|    TG(8)    |------|------|------|------|------|
    * `-----------------------------------------------------------------------------------'
    */

    [_NUMPD] = LAYOUT_planck_mit(                                                                              //
        KC_ESC, KC_HOME, KC_UP, KC_END, KC_PERC, KC_LCBR, KC_RCBR, KC_TAB, KC_P7, KC_P8, KC_P9, KC_BSPC,       //
        KC_TILD, KC_LEFT, KC_DOWN, KC_RGHT, KC_BSLS, KC_LBRC, KC_RBRC, KC_PSLS, KC_P4, KC_P5, KC_P6, KC_PMNS,  //
        KC_LSFT, KC_SLSH, KC_CUT, KC_COPY, KC_PASTE, KC_LT, KC_GT, KC_PAST, KC_P1, KC_P2, KC_P3, KC_PPLS,      //
        TO(_BASE), KC_LEAD, KC_LGUI, KC_LALT, KC_LCPO, KC_SPC, KC_RCPC, KC_HASH, KC_P0, KC_PDOT, KC_PENT),
    /*
    Numpad Layer [7]
    * ,-----------------------------------------------------------------------------------.
    * | Esc  | Home |  Up  |  End |   %  |   {  |   }  |  Tab |   7  |   8  |   9  |BckSpc|
    * |------+------+------+------+------+------+------+------+------+------+------+------|
    * |   ~  | Left | Down | Right|   \  |   [  |   ]  |   /  |   4  |   5  |   6  |   -  |
    * |------+------+------+------+------+------+------+------+------+------+------+------|
    * | Shift|   /  |Ctrl+X|Ctrl+C|Ctrl+V|   <  |   >  |   *  |   1  |   2  |   3  |   +  |
    * |------+------+------+------+------+------+------+------+------+------+------+------|
    * | Base |Leader|  Alt |  OS  |LCtl/(|    Space    |)/RCtl|   #  |   0  |   .  |Enter |
    * `-----------------------------------------------------------------------------------'
    */
    [_TABULA] = LAYOUT_planck_mit(                                                                                                   //
        KC_ESC, KC_ALTF4, VLK_TOG, PRINT_WPM_KEY, WAKE_ANI_TOG, WAKE_AUD_TOG, KC_REDO, UC_MOD, UC_M_WC, CG_TOGG, AG_TOGG, KC_DLINE,  //
        KC_NXTAB, KC_SLCTALL, KC_SAVE, KC_TRNS, KC_FIND, SH_TG, SH_TG, IRONY, KC_LCUT, KC_LCOPY, KC_TRNS, KC_KILL,                   //
        KC_LSFT, KC_UNDO, KC_CUT, KC_COPY, KC_PASTE, KC_PRVWD, KC_NXTWD, TG(_MOUSY), KC_TRNS, KC_HOME, KC_END, KC_SFTENT,            //
        TO(_BASE), KC_LCTL, KC_LGUI, KC_LALT, KC_LSPO, ALT_TAB, KC_RSPC, KC_PRVWD, KC_BRID, KC_BRIU, KC_NXTWD),
    /* Tabular Layer [8]
     * ,-----------------------------------------------------------------------------------.
     * |  Esc |Alt+F4|Veloci|  WPM |WakANI|WakAUD|Ctrl+Y|UCMode|UCWinC|CtGUTg|AltGTg| DLine|
     * |------+------+------+------+------+------+------+------+------+------+------+------|
     * |  Tab |Ctrl+A|Ctrl+S|------|Ctrl+F| SWAP | SWAP |  ⸮^‽ |CutLin|CpyLin|------|DelLin|
     * |------+------+------+------+------+------+------+------+------+------+------+------|
     * | Shift|Ctrl+Z|Ctrl+X|Ctrl+C|Ctrl+V|PrVWin|NxtWin| TG(8)|------| Home |  End |Enter |
     * |------+------+------+------+------+------+------+------+------+------+------+------|
     * | Base | LCtrl|  Alt |  OS  |LSft/(|   Alt+Tab   |)/RSft|PrvSel|ScrBr-|ScrBr+|NxtSel|
     * `-----------------------------------------------------------------------------------'
     */

    [_MOUSY] = LAYOUT_planck_mit(                                                                                                  //
        KC_ESC, KC_BTN1, KC_MS_U, KC_BTN2, KC_TRNS, KC_TRNS, KC_TRNS, UC_MOD, UC_M_WC, CG_TOGG, AG_TOGG, KC_BSPC,  //
        KC_TAB, KC_MS_L, KC_MS_D, KC_MS_R, KC_TRNS, KC_TRNS, KC_TRNS, IRONY, VLK_TOG, KC_TRNS, KC_TRNS, KC_TRNS,                   //
        KC_LSFT, KC_UNDO, KC_CUT, KC_COPY, KC_PASTE, KC_PRVWD, KC_NXTWD, KC_TRNS, KC_HOME, KC_BTN3, KC_END, KC_SFTENT,             //
        TO(_BASE), KC_LCTL, KC_LGUI, KC_LALT, KC_BTN1, ALT_TAB, KC_BTN2, KC_WH_L, KC_WH_D, KC_WH_U, KC_WH_R)};
/* MousePad Layer [9]
 * ,-----------------------------------------------------------------------------------.
 * | Esc  |MsBtn1| MsUp |MsBtn2|------|------|------|UCMode|UCWinC|CtGUTg|AltGTg|BckSpc|
 * |------+------+------+------+------+------+------+------+------+------+------+------|
 * |   ~  |MsLeft|MsDown|MsRigt|------|------|------|  ⸮^‽ |Veloci|------|------|------|
 * |------+------+------+------+------+------+------+------+------+------+------+------|
 * | Shift|------|Ctrl+X|Ctrl+C|Ctrl+V|------|------|------| Home |MsBtn3|  End |Enter |
 * |------+------+------+------+------+------+------+------+------+------+------+------|
 * | Base | LCtrl|  Alt |  OS  |MsBtn1|   Alt+Tab   |MsBtn2|MsWhlL|MsWhlD|MsWhlU|MsWhlR|
 * `-----------------------------------------------------------------------------------'
 */

// Define RGB layers | assign leds and their values for each rgb layer
const rgblight_segment_t PROGMEM my_warning_layer[]  = RGBLIGHT_LAYER_SEGMENTS({1, 9, HSV_RED}, {0, 1, HSV_RED});
const rgblight_segment_t PROGMEM my_allgood_layer[]  = RGBLIGHT_LAYER_SEGMENTS({1, 9, HSV_GREEN}, {0, 1, HSV_GREEN});
const rgblight_segment_t PROGMEM my_capslock_layer[] = RGBLIGHT_LAYER_SEGMENTS({1, 1, HSV_RED}, {8, 1, HSV_RED});
const rgblight_segment_t PROGMEM my_number_layer[]   = RGBLIGHT_LAYER_SEGMENTS({1, 1, HSV_MAGENTA}, {8, 1, HSV_MAGENTA});
const rgblight_segment_t PROGMEM my_symbol_layer[]   = RGBLIGHT_LAYER_SEGMENTS({1, 1, HSV_GREEN}, {8, 1, HSV_GREEN});
const rgblight_segment_t PROGMEM my_tabula_layer[]   = RGBLIGHT_LAYER_SEGMENTS({3, 4, HSV_CORAL}, {1, 1, HSV_CORAL}, {8, 1, HSV_CORAL});
const rgblight_segment_t PROGMEM my_mousy_layer[]    = RGBLIGHT_LAYER_SEGMENTS({3, 4, HSV_TURQUOISE}, {1, 1, HSV_TURQUOISE}, {8, 1, HSV_TURQUOISE}, {7, 1, HSV_MAGENTA});
const rgblight_segment_t PROGMEM my_numpad_layer[]   = RGBLIGHT_LAYER_SEGMENTS({3, 4, HSV_GOLD}, {1, 1, HSV_GOLD}, {8, 1, HSV_GOLD}, {7, 1, HSV_BLUE});
const rgblight_segment_t PROGMEM my_features_layer[] = RGBLIGHT_LAYER_SEGMENTS({3, 4, HSV_BLUE}, {1, 1, HSV_BLUE}, {8, 1, HSV_BLUE});
const rgblight_segment_t PROGMEM my_base_layer[]     = RGBLIGHT_LAYER_SEGMENTS({0, 0, HSV_BLACK});
const rgblight_segment_t PROGMEM my_colemak_layer[]  = RGBLIGHT_LAYER_SEGMENTS({1, 1, HSV_GREEN});
const rgblight_segment_t PROGMEM my_dvorak_layer[]   = RGBLIGHT_LAYER_SEGMENTS({1, 1, HSV_ORANGE});
const rgblight_segment_t PROGMEM my_plover_layer[]   = RGBLIGHT_LAYER_SEGMENTS({1, 1, HSV_GOLD});

// Define the array of rgb layers. Later layers take precedence
const rgblight_segment_t* const PROGMEM my_rgb_layers[] = RGBLIGHT_LAYERS_LIST(my_base_layer,      // Base Layer
                                                                               my_colemak_layer,   // Overrides previous layer
                                                                               my_dvorak_layer,    // Overrides previous layers
                                                                               my_symbol_layer,    // Overrides previous layers
                                                                               my_number_layer,    // ...etc                                                                              my_features_layer,  // Overrides  layers
                                                                               my_plover_layer,    //
                                                                               my_features_layer,  //
                                                                               my_numpad_layer,    //
                                                                               my_tabula_layer,    //
                                                                               my_mousy_layer,     //
                                                                               my_capslock_layer,  //
                                                                               my_warning_layer,   //
                                                                               my_allgood_layer);  // CapsLock Layer);

// Configure encoders
bool encoder_update_user(uint8_t index, bool clockwise) {
    if (musical_mode) {
        if (clockwise) {
            tap_code16(MU_MOD);
        } else {
            tap_code16(MU_MOD);
        }
    } else {
        if (muse_mode) {
            if (IS_LAYER_ON(_RAISE)) {
                if (clockwise) {
                    muse_offset++;
                } else {
                    muse_offset--;
                }
            } else {
                if (clockwise) {
                    muse_tempo += 1;
                } else {
                    muse_tempo -= 1;
                }
            }
        } else {
            if (index == 0) { /* First encoder */
                uint16_t held_keycode_timer = timer_read();
                switch (biton32(layer_state)) {
                    case 0:                                 // Base Layer
                        if ((get_mods() & MOD_MASK_GUI)) {  // GUI-ed
                            if (clockwise) {
                                tap_code(KC_RIGHT);

                            } else {
                                tap_code(KC_LEFT);
                            }
                        } else if ((get_mods() & MOD_MASK_ALT)) {  // Alt-ed
                            if (clockwise) {
                                tap_code16(LALT(KC_TAB));  // Alt+Tabbing
                            } else {
                                tap_code16(LSA(KC_TAB));
                            }
                        } else if ((get_mods() & MOD_MASK_SHIFT)) {  // Shifted
                            const uint8_t _real_mods = get_mods();
                            unregister_code16(KC_LSFT);
                            unregister_code16(KC_RSFT);
                            clear_mods();
                            if (clockwise) {
                                tap_code16(KC_MS_WH_DOWN);
                            } else {
                                tap_code16(KC_MS_WH_UP);
                            }
                            set_mods(_real_mods);
                        } else if ((get_mods() & MOD_MASK_CTRL)) {  // Ctrl-ed
                            if (clockwise) {
                                tap_code16(RCTL(KC_TAB));  // Ctrl+Tabbing
                            } else {
                                tap_code16(RCS(KC_TAB));
                            }
                        } else {  // Normal or unspecified modifiers
                            if (clockwise) {
                                // Volume control requires extra timer to function correctly
                                register_code(KC_VOLU);
                                while (timer_elapsed(held_keycode_timer) < TAP_CODE_DELAY) {
                                    // no-op
                                }
                                unregister_code(KC_VOLD);
                            } else {
                                register_code(KC_VOLD);
                                while (timer_elapsed(held_keycode_timer) < TAP_CODE_DELAY) {
                                    // no-op
                                }
                                unregister_code(KC_VOLU);
                            }
                        }
                        return false;
                        break;
                    case 3:  // Symbols Layer
                        if (clockwise) {
                            tap_code(KC_WH_D);  // Mouse wheeling
                        } else {
                            tap_code(KC_WH_U);
                        }
                        return false;
                        break;
                    case 4:  // Numbers Layer
                        if (clockwise) {
                            tap_code(KC_WH_D);  // Mouse wheeling
                        } else {
                            tap_code(KC_WH_U);
                        }
                        return false;
                        break;
                    case 6:  // Features Layer
                        if (clockwise) {
                            tap_code16(KC_DOWN);
                        } else {
                            tap_code16(KC_UP);
                        }
                    default:  // Any other layer
                        if ((get_mods() & MOD_MASK_CSAG)) {
                            if (clockwise) {
                                WITHOUT_MODS({ SEND_STRING(SS_TAP(X_RIGHT)); });
                            } else {
                                WITHOUT_MODS({ SEND_STRING(SS_TAP(X_LEFT)); });
                            }
                        } else {
                            if (clockwise) {
                                tap_code(KC_DOWN);  // Simple Up/Down
                            } else {
                                tap_code(KC_UP);
                            }
                        }
                        return false;
                        break;
                }
            } else if (index == 1) { /* Second encoder (if we had one) */
                if (clockwise) {
                    tap_code16(LCTL(KC_LEFT));  // Ctrl+Left/Right
                } else {
                    tap_code16(LCTL(KC_RIGHT));
                }
            }
        }
    }
    return true;
}

// OLED CONFIGURATION
/*
static void render_logo(void) {
    static const char PROGMEM qmk_logo[] = {0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94, 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0x00};

    oled_write_P(qmk_logo, false);
}
*/

// Process Combo events
void process_combo_event(uint16_t combo_index, bool pressed) {
    switch (combo_index) {
        case ZC_COPY:
            if (pressed) {
                tap_code16(LCTL(KC_C));
            }
            break;
        case XV_PASTE:
            if (pressed) {
                tap_code16(LCTL(KC_V));
            }
            break;
    }
}

// Runs every time a key is pressed or released
bool process_record_user(uint16_t keycode, keyrecord_t* record) {
#ifdef CONSOLE_ENABLE
    dprintf("KL: kc: 0x%04X, col: %u, row: %u, pressed: %b, time: %u, interrupt: %b, count: %u\n", keycode, record->event.key.col, record->event.key.row, record->event.pressed, record->event.time, record->tap.interrupted, record->tap.count);
#endif
    // Store the current modifier state in the variable for later reference
    mod_state = get_mods();
    switch (keycode) {
        case QWERTY:
            if (record->event.pressed) {
                set_single_persistent_default_layer(_QWERTY);
                PLAY_SONG(qwerty_song);
            }
            return false;
            break;
        case COLEMAK:
            if (record->event.pressed) {
                set_single_persistent_default_layer(_COLEMAK);
                PLAY_SONG(colemak_song);
            }
            return false;
            break;
        case DVORAK:
            if (record->event.pressed) {
                set_single_persistent_default_layer(_DVORAK);
                PLAY_SONG(dvorak_song);
            }
            return false;
            break;
        case PLOVER:
            if (record->event.pressed) {
#ifdef AUDIO_ENABLE
                stop_all_notes();
                PLAY_SONG(plover_song);
#endif
                layer_off(_RAISE);
                layer_off(_LOWER);
                layer_off(_ADJUST);
                layer_on(_PLOVER);
                if (!eeconfig_is_enabled()) {
                    eeconfig_init();
                }
                keymap_config.raw  = eeconfig_read_keymap();
                keymap_config.nkro = 1;
                eeconfig_update_keymap(keymap_config.raw);
            }
            return false;
            break;
        case EXT_PLV:
            if (record->event.pressed) {
#ifdef AUDIO_ENABLE
                PLAY_SONG(plover_gb_song);
#endif
                layer_off(_PLOVER);
            }
            return false;
            break;
        case MO_SYMBLS:
            if (record->event.pressed) {
                layer_on(_SYMBLS);
            } else {
                layer_off(_SYMBLS);
            }
            return false;
        case MO_NUMBRS:
            if (record->event.pressed) {
                layer_on(_NUMBRS);
            } else {
                layer_off(_NUMBRS);
            }
            return false;
        case MO_FEATURS:
            if (record->event.pressed) {
                layer_on(_FEATURS);
            } else {
                layer_off(_FEATURS);
            }
            return false;
        case MUSIC_ON:
            if (record->event.pressed) {
                musical_mode = true;
                register_code16(MU_ON);
            } else {
                unregister_code16(MU_ON);
            }
            break;
        case MUSIC_OFF:
            if (record->event.pressed) {
                musical_mode = false;
                register_code16(MU_OFF);
            } else {
                unregister_code16(MU_OFF);
            }
            break;
        case KC_PRVWD:  // Control+Left
            if (record->event.pressed) {
                if (keymap_config.swap_lctl_lgui) {
                    register_mods(mod_config(MOD_LALT));
                    register_code(KC_LEFT);
                } else {
                    register_mods(mod_config(MOD_LCTL));
                    register_code(KC_LEFT);
                }
            } else {
                if (keymap_config.swap_lctl_lgui) {
                    unregister_mods(mod_config(MOD_LALT));
                    unregister_code(KC_LEFT);
                } else {
                    unregister_mods(mod_config(MOD_LCTL));
                    unregister_code(KC_LEFT);
                }
            }
            break;
        case KC_NXTWD:  // Control+Right
            if (record->event.pressed) {
                if (keymap_config.swap_lctl_lgui) {
                    register_mods(mod_config(MOD_LALT));
                    register_code(KC_RIGHT);
                } else {
                    register_mods(mod_config(MOD_LCTL));
                    register_code(KC_RIGHT);
                }
            } else {
                if (keymap_config.swap_lctl_lgui) {
                    unregister_mods(mod_config(MOD_LALT));
                    unregister_code(KC_RIGHT);
                } else {
                    unregister_mods(mod_config(MOD_LCTL));
                    unregister_code(KC_RIGHT);
                }
            }
            break;
        case KC_PRVTAB:  // Control+Shift+Tab
            if (record->event.pressed) {
                register_mods(mod_config(MOD_LCTL));
                register_mods(mod_config(MOD_LSFT));
                register_code(KC_TAB);
            } else {
                unregister_mods(mod_config(MOD_LCTL));
                unregister_mods(mod_config(MOD_LSFT));
                unregister_code(KC_TAB);
            }
            break;
        case KC_NXTAB:  // Control+Tab
            if (record->event.pressed) {
                if (keymap_config.swap_lctl_lgui) {
                    register_mods(mod_config(MOD_LCTL));
                    register_code(KC_TAB);
                } else {
                    register_mods(mod_config(MOD_LSFT));
                    register_code(KC_TAB);
                }
            } else {
                if (keymap_config.swap_lctl_lgui) {
                    unregister_mods(mod_config(MOD_LALT));
                    unregister_code(KC_RIGHT);
                } else {
                    unregister_mods(mod_config(MOD_LCTL));
                    unregister_code(KC_RIGHT);
                }
            }
            break;
        case KC_LSTRT:  // Basically just Home
            if (record->event.pressed) {
                if (keymap_config.swap_lctl_lgui) {
                    // CMD-arrow on Mac, but we have CTL and GUI swapped
                    register_mods(mod_config(MOD_LCTL));
                    register_code(KC_LEFT);
                } else {
                    register_code(KC_HOME);
                }
            } else {
                if (keymap_config.swap_lctl_lgui) {
                    unregister_mods(mod_config(MOD_LCTL));
                    unregister_code(KC_LEFT);
                } else {
                    unregister_code(KC_HOME);
                }
            }
            break;
        case KC_LEND:  // Basically just End
            if (record->event.pressed) {
                if (keymap_config.swap_lctl_lgui) {
                    // CMD-arrow on Mac, but we have CTL and GUI swapped
                    register_mods(mod_config(MOD_LCTL));
                    register_code(KC_RIGHT);
                } else {
                    register_code(KC_END);
                }
            } else {
                if (keymap_config.swap_lctl_lgui) {
                    unregister_mods(mod_config(MOD_LCTL));
                    unregister_code(KC_RIGHT);
                } else {
                    unregister_code(KC_END);
                }
            }
            break;
        case KC_DLINE:  // Control+BackSpace
            if (record->event.pressed) {
                register_mods(mod_config(MOD_LCTL));
                register_code(KC_BSPC);
            } else {
                unregister_mods(mod_config(MOD_LCTL));
                unregister_code(KC_BSPC);
            }
            break;
        case KC_COPY:  // Copy: Control+C
            if (record->event.pressed) {
                register_mods(mod_config(MOD_LCTL));
                register_code(KC_C);
            } else {
                unregister_mods(mod_config(MOD_LCTL));
                unregister_code(KC_C);
            }
            return false;
        case KC_PASTE:  // Paste: Control+V
            if (record->event.pressed) {
                register_mods(mod_config(MOD_LCTL));
                register_code(KC_V);
            } else {
                unregister_mods(mod_config(MOD_LCTL));
                unregister_code(KC_V);
            }
            return false;
        case KC_CUT:  // Cut: Control+X
            if (record->event.pressed) {
                register_mods(mod_config(MOD_LCTL));
                register_code(KC_X);
            } else {
                unregister_mods(mod_config(MOD_LCTL));
                unregister_code(KC_X);
            }
            return false;
            break;
        case KC_UNDO:  // Undo: Control+Z
            if (record->event.pressed) {
                register_mods(mod_config(MOD_LCTL));
                register_code(KC_Z);
            } else {
                unregister_mods(mod_config(MOD_LCTL));
                unregister_code(KC_Z);
            }
            return false;
        case KC_REDO:  // Redo: Control+Y
            if (record->event.pressed) {
                register_mods(mod_config(MOD_LCTL));
                register_code(KC_Y);
            } else {
                unregister_mods(mod_config(MOD_LCTL));
                unregister_code(KC_Y);
            }
            break;
        case KC_SAVE:  // Save: Control+S
            if (record->event.pressed) {
                register_mods(mod_config(MOD_LCTL));
                register_code(KC_S);
            } else {
                unregister_mods(mod_config(MOD_LCTL));
                unregister_code(KC_S);
            }
            return false;
        case KC_FIND:  // Find: Control+F
            if (record->event.pressed) {
                register_mods(mod_config(MOD_LCTL));
                register_code(KC_F);
            } else {
                unregister_mods(mod_config(MOD_LCTL));
                unregister_code(KC_F);
            }
            return false;
        case KC_SLCTALL:  // Select All: Control+A
            if (record->event.pressed) {
                register_mods(mod_config(MOD_LCTL));
                register_code(KC_A);
            } else {
                unregister_mods(mod_config(MOD_LCTL));
                unregister_code(KC_A);
            }
            return false;
        case KC_KILL:  // Kill: Delete Line
            if (record->event.pressed) {
                tap_code(KC_HOME);
                register_mods(mod_config(MOD_LSFT));
                tap_code(KC_END);
                unregister_mods(mod_config(MOD_LSFT));
                tap_code(KC_DELETE);
            } else {
            }
            return false;
        case KC_LCUT:  // Cut Line
            if (record->event.pressed) {
                tap_code(KC_HOME);
                register_mods(mod_config(MOD_LSFT));
                tap_code(KC_END);
                unregister_mods(mod_config(MOD_LSFT));
                register_mods(mod_config(MOD_LCTL));
                tap_code(KC_X);
                unregister_mods(mod_config(MOD_LCTL));
            } else {
            }
            return false;
        case KC_LCOPY:  // Copy Line
            if (record->event.pressed) {
                tap_code(KC_HOME);
                register_mods(mod_config(MOD_LSFT));
                tap_code(KC_END);
                unregister_mods(mod_config(MOD_LSFT));
                register_mods(mod_config(MOD_LCTL));
                tap_code(KC_C);
                unregister_mods(mod_config(MOD_LCTL));
                tap_code(KC_END);
            } else {
            }
            return false;
        case KC_ALTF4:  // Close Window: Alt+F4
            if (record->event.pressed) {
                register_mods(mod_config(MOD_LALT));
                register_code(KC_F4);
            } else {
                unregister_mods(mod_config(MOD_LALT));
                unregister_code(KC_F4);
            }
            return false;
        case ALT_TAB:  // Change Window: Super ⭍ Alt+Tab
            if (record->event.pressed) {
                if (!is_alt_tab_active) {
                    is_alt_tab_active = true;
                    register_code(KC_LALT);
                }
                alt_tab_timer = timer_read();
                register_code(KC_TAB);
#ifdef CONSOLE_ENABLE
                dprint("I've tabbed to another window!\n");
#endif
            } else {
                unregister_code(KC_TAB);
            }
            break;
        case KC_BSPC: {
            // Initialize a boolean variable that keeps track
            // of the delete key status: registered or not?
            static bool delkey_registered;
            if (record->event.pressed) {
                // Detect the activation of either shift keys
                if (mod_state & MOD_MASK_SHIFT) {
                    // First temporarily canceling both shifts so that
                    // shift isn't applied to the KC_DEL keycode
                    del_mods(MOD_MASK_SHIFT);
                    register_code(KC_DEL);
                    // Update the boolean variable to reflect the status of KC_DEL
                    delkey_registered = true;
                    // Reapplying modifier state so that the held shift key(s)
                    // still work even after having tapped the Backspace/Delete key.
                    set_mods(mod_state);
                    return false;
                }
            } else {  // on release of KC_BSPC
                // In case KC_DEL is still being sent even after the release of KC_BSPC
                if (delkey_registered) {
                    unregister_code(KC_DEL);
                    delkey_registered = false;
                    return false;
                }
            }
        }
            // Let QMK process the KC_BSPC keycode as usual outside of shift
            return true;

        case DO_RESET:  // Reset button with LED indication
            if (record->event.pressed) {
                rgblight_set_effect_range(0, 9);
                rgblight_sethsv_noeeprom(HSV_RED);
                rgblight_mode_noeeprom(RGBLIGHT_MODE_STATIC_LIGHT);
                rgblight_blink_layer(11, 5000);
                reset_keyboard();
            }
            break;
        case WAKE_ANI_TOG:  // Toggle the Wakeup RGB animation
            if (record->event.pressed) {
                user_config.do_wakeup_animation ^= 1;   // Toggles the status
                eeconfig_update_user(user_config.raw);  // Writes the new status to EEPROM
                if (user_config.do_wakeup_animation) {
                    print("Wake animation enabled.\n");
                    PLAY_SONG(slctl_on);

                } else {
                    print("Wake animation disabled.\n");
                    PLAY_SONG(slctl_off);
                }
            }
            break;
        case WAKE_AUD_TOG:  // Toggle the wake-up music
            if (record->event.pressed) {
                user_config.do_wakeup_audio ^= 1;       // Toggles the status
                eeconfig_update_user(user_config.raw);  // Writes the new status to EEPROM
                if (user_config.do_wakeup_audio) {
                    print("Wake music enabled.\n");
                    PLAY_SONG(slctl_on);

                } else {
                    print("Wake music disabled.\n");
                    PLAY_SONG(slctl_off);
                }
            }
            break;
        case IRONY:  // Outputs Irony/Interrobang symbols
            if ((get_mods() & MOD_MASK_SHIFT)) {
                irony_shifted = true;
            } else {
                irony_shifted = false;
            }
            if (record->event.pressed) {
                if (irony_shifted) {
                    send_unicode_string(bang_str);
                } else {
                    send_unicode_string(irony_str);
                }
                irony_active       = true;
                irony_pressed_time = timer_read();
            } else {
                irony_active       = false;
                irony_pressed_time = 0;
                irony_shifted      = false;
            }
            return false;
        case TG(_NUMPD):  // Toggle the NumPad layer
            if (record->event.pressed) {
#ifdef AUDIO_ENABLE
                PLAY_SONG(hello_song);
#endif
                print("I've activated the NumPad!\n");
            } else {
            }
            break;
        case TG(_TABULA):  // Toggle the Tabula layer
            if (record->event.pressed) {
#ifdef AUDIO_ENABLE
                PLAY_SONG(lover_song);
#endif
                print("I've activated Tabular!\n");
            } else {
            }
            break;
        case TG(_MOUSY):  // Toggle the MouseyPad layer
            if (record->event.pressed) {
#ifdef AUDIO_ENABLE
                PLAY_SONG(funk_song);
#endif
                print("I've activated the MousePad!\n");
            } else {
            }
            break;
        case TO(_BASE):  // Return to the base layer
            if (record->event.pressed) {
#ifdef AUDIO_ENABLE
                PLAY_SONG(planck_song);
#endif
                print("I've returned to the Base Layer!\n");
            } else {
            }
            break;
        case PRINT_WPM_KEY:  // Prints the current average words-per-minute to the console
            sprintf(wpm_str, "Current WPM: %hu", get_current_wpm());
            printf("%s\n", wpm_str);
            break;

        case MY_RGBCON:  // Cycles through custom RGB animation presets
            if (record->event.pressed) {
                // when keycode RGB-CON is pressed
                user_config.rgbcon_tracker = rgbcon_tracker + 1;  // Toggles the status
                eeconfig_update_user(user_config.raw);
                switch (rgbcon_tracker) {
                    case 0:
                        rgblight_set_effect_range(0, 9);
                        rgblight_sethsv(HSV_BLACK);
                        rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
                        print("Changed RGB mode to: Disabled RGB\n");
                        rgbcon_tracker++;
                        break;
                    case 1:
                        rgblight_set_effect_range(0, 9);
                        rgblight_sethsv(HSV_WHITE);
                        rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
                        print("Changed RGB mode to: Static White\n");
                        rgbcon_tracker++;
                        break;
                    case 2:
                        rgblight_set_effect_range(0, 9);
                        rgblight_sethsv(HSV_CYAN);
                        rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
                        print("Changed RGB mode to: Static Cyan\n");
                        rgbcon_tracker++;
                        break;
                    case 3:
                        rgblight_set_effect_range(0, 9);
                        rgblight_sethsv(HSV_WHITE);
                        rgblight_mode(RGBLIGHT_MODE_BREATHING);
                        print("Changed RGB mode to: Breathing Lights\n");
#ifdef AUDIO_ENABLE
                        print("Played Marching song!\n");
                        PLAY_SONG(imp_march_song);
#endif
                        rgbcon_tracker++;
                        break;
                    case 4:
                        rgblight_set_effect_range(0, 9);
                        rgblight_sethsv(HSV_RED);
                        rgblight_mode(RGBLIGHT_MODE_RAINBOW_SWIRL);
                        print("Changed RGB mode to: Rainbow Swirl\n");
                        rgbcon_tracker++;
                        break;
                    case 5:
                        rgblight_set_effect_range(0, 9);
                        rgblight_sethsv(HSV_CYAN);
                        rgblight_mode(RGBLIGHT_MODE_RAINBOW_MOOD);
                        print("Changed RGB mode to: Rainbow Mood\n");
#ifdef AUDIO_ENABLE
                        print("Played Game Over song!\n");
                        PLAY_SONG(gameover_song);
#endif
                        rgbcon_tracker = 0;
                        break;
                    case 6:
                        rgblight_set_effect_range(0, 9);
                        rgblight_sethsv(HSV_BLACK);
                        rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
                        print("Changed RGB mode to: Disabled RGB\n");
                        rgbcon_tracker = 1;
                        break;
                }
            } else {
            }
            break;
    }
    return true;
};

// Runs *after* a key is pressed
void post_process_record_user(uint16_t keycode, keyrecord_t* record) {
    switch (keycode) {
        case DEBUG:
            // Blink the warning layer when the debug key is pressed
            rgblight_blink_layer_repeat(debug_enable ? 11 : 12, 1500, 3);

            // Update the console with the debug mode status
            if (debug_enable) {
                print("Debug mode enabled.\n");
                PLAY_SONG(slctl_on);

            } else {
                print("Debug mode disabled.\n");
                PLAY_SONG(slctl_off);
            }
            break;
    }
}

// RGB Default Layer assignments
layer_state_t default_layer_state_set_user(layer_state_t state) {
    state = update_tri_layer_state(state, _SYMBLS, _NUMBRS, _FEATURS);

    // Sets the default RGB layer states
    rgblight_set_layer_state(0, layer_state_cmp(state, _BASE));
    rgblight_set_layer_state(1, layer_state_cmp(state, _COLEMAK));
    rgblight_set_layer_state(2, layer_state_cmp(state, _DVORAK));
    return state;
}

// RGB Layer assignments
layer_state_t layer_state_set_user(layer_state_t state) {
    state = update_tri_layer_state(state, _SYMBLS, _NUMBRS, _FEATURS);

    // Sets the RGB layer states
    rgblight_set_layer_state(5, layer_state_cmp(state, _PLOVER));
    rgblight_set_layer_state(6, layer_state_cmp(state, _FEATURS));
    rgblight_set_layer_state(3, layer_state_cmp(state, _SYMBLS));
    rgblight_set_layer_state(4, layer_state_cmp(state, _NUMBRS));
    rgblight_set_layer_state(7, layer_state_cmp(state, _NUMPD));
    rgblight_set_layer_state(8, layer_state_cmp(state, _TABULA));
    rgblight_set_layer_state(9, layer_state_cmp(state, _MOUSY));
    return state;
}

// Runs the wakeup rgb animation + music
void rgb_wakeup_sequence(void) {
    if (waking_up) {
        if ((timer_elapsed(wake_rgb_timer) > WAKE_ANIMATION_TIMER_FREQUENCY)) {
            if (wake_rgb_count < 1) {
                rgblight_sethsv_noeeprom(HSV_OFF);
                rgblight_set_effect_range(0, 9);
            } else if (wake_rgb_count < 2 && wake_rgb_count > 0) {
                rgblight_sethsv_noeeprom(HSV_BLACK);
                rgblight_set_effect_range(0, 2);
                rgblight_sethsv_noeeprom(HSV_WHITE);
                rgblight_set_effect_range(2, 9);
            } else if (wake_rgb_count < 3 && wake_rgb_count > 1) {
                rgblight_sethsv_noeeprom(HSV_BLACK);
                rgblight_set_effect_range(0, 2);
                rgblight_sethsv_noeeprom(HSV_WHITE);
                rgblight_set_effect_range(2, 9);
            } else if (wake_rgb_count < 4 && wake_rgb_count > 2) {
                rgblight_sethsv_noeeprom(HSV_BLACK);
                rgblight_set_effect_range(0, 3);
                rgblight_sethsv_noeeprom(HSV_WHITE);
                rgblight_set_effect_range(3, 9);
            } else if (wake_rgb_count < 5 && wake_rgb_count > 3) {
                rgblight_sethsv_noeeprom(HSV_BLACK);
                rgblight_set_effect_range(0, 4);
                rgblight_sethsv_noeeprom(HSV_WHITE);
                rgblight_set_effect_range(4, 9);
            } else if (wake_rgb_count < 6 && wake_rgb_count > 4) {
                rgblight_sethsv_noeeprom(HSV_BLACK);
                rgblight_set_effect_range(0, 5);
                rgblight_sethsv_noeeprom(HSV_WHITE);
                rgblight_set_effect_range(5, 9);
            } else if (wake_rgb_count < 7 && wake_rgb_count > 5) {
                rgblight_sethsv_noeeprom(HSV_BLACK);
                rgblight_set_effect_range(0, 6);
                rgblight_sethsv_noeeprom(HSV_WHITE);
                rgblight_set_effect_range(6, 9);
            } else if (wake_rgb_count < 8 && wake_rgb_count > 6) {
                rgblight_sethsv_noeeprom(HSV_BLACK);
                rgblight_set_effect_range(0, 7);
                rgblight_sethsv_noeeprom(HSV_WHITE);
                rgblight_set_effect_range(7, 9);
            } else if (wake_rgb_count < 9 && wake_rgb_count > 7) {
                rgblight_sethsv_noeeprom(HSV_BLACK);
                rgblight_set_effect_range(0, 8);
                rgblight_sethsv_noeeprom(HSV_WHITE);
                rgblight_set_effect_range(8, 9);
            } else if (wake_rgb_count < 10 && wake_rgb_count > 8) {
                rgblight_sethsv_noeeprom(HSV_BLACK);
                rgblight_set_effect_range(0, 0);
                rgblight_sethsv_noeeprom(HSV_WHITE);
                rgblight_set_effect_range(0, 9);
            } else if (wake_rgb_count < 11 && wake_rgb_count > 9) {
                rgblight_sethsv_noeeprom(HSV_BLACK);
                rgblight_set_effect_range(0, 8);
                rgblight_sethsv_noeeprom(HSV_WHITE);
                rgblight_set_effect_range(8, 9);
            } else if (wake_rgb_count < 12 && wake_rgb_count > 10) {
                rgblight_sethsv_noeeprom(HSV_BLACK);
                rgblight_set_effect_range(0, 7);
                rgblight_sethsv_noeeprom(HSV_WHITE);
                rgblight_set_effect_range(7, 9);
            } else if (wake_rgb_count < 13 && wake_rgb_count > 11) {
                rgblight_sethsv_noeeprom(HSV_BLACK);
                rgblight_set_effect_range(0, 6);
                rgblight_sethsv_noeeprom(HSV_WHITE);
                rgblight_set_effect_range(6, 9);
            } else if (wake_rgb_count < 14 && wake_rgb_count > 12) {
                rgblight_sethsv_noeeprom(HSV_BLACK);
                rgblight_set_effect_range(0, 5);
                rgblight_sethsv_noeeprom(HSV_WHITE);
                rgblight_set_effect_range(5, 9);
            } else if (wake_rgb_count < 15 && wake_rgb_count > 13) {
                rgblight_sethsv_noeeprom(HSV_BLACK);
                rgblight_set_effect_range(0, 4);
                rgblight_sethsv_noeeprom(HSV_WHITE);
                rgblight_set_effect_range(4, 9);
            } else if (wake_rgb_count < 16 && wake_rgb_count > 14) {
                rgblight_sethsv_noeeprom(HSV_BLACK);
                rgblight_set_effect_range(0, 3);
                rgblight_sethsv_noeeprom(HSV_WHITE);
                rgblight_set_effect_range(3, 9);
            } else if (wake_rgb_count < 17 && wake_rgb_count > 15) {
                rgblight_sethsv_noeeprom(HSV_BLACK);
                rgblight_set_effect_range(0, 2);
                rgblight_sethsv_noeeprom(HSV_WHITE);
                rgblight_set_effect_range(2, 9);
            } else if (wake_rgb_count < 18 && wake_rgb_count > 16) {
                rgblight_sethsv_noeeprom(HSV_BLACK);
                rgblight_set_effect_range(0, 1);
                rgblight_sethsv_noeeprom(HSV_WHITE);
                rgblight_set_effect_range(1, 9);
            } else if (wake_rgb_count > 17) {
                // Final frame of wake-up rgb animation
                rgblight_sethsv_noeeprom(HSV_BLACK);
                rgblight_set_effect_range(0, 9);
                waking_up = false;
                print("I have awoken!\n");
#ifdef AUDIO_ENABLE
                // Play the wake-up sound *after* we finish the animation
                if (do_wake_audio) {
                    PLAY_SONG(puzzle_song);
                }
#endif
            }
            rgblight_mode_noeeprom(RGBLIGHT_MODE_STATIC_LIGHT);
            wake_rgb_count++;
            wake_rgb_timer = timer_read();
        }
    }
}

// Spits out some unicode special characters in response to a tap-dance
void send_degree_symbol(qk_tap_dance_state_t* state, void* user_data) {
    switch (state->count) {
        case 4:
            // ℃
            unicode_input_start();
            register_hex(0x2103);
            unicode_input_finish();
            print("You pressed the Degrees key 4 times!\n");
            reset_tap_dance(state);
            break;
        case 3:
            //℉
            unicode_input_start();
            register_hex(0x2109);
            unicode_input_finish();
            print("You pressed the Degrees key 3 times!\n");
            reset_tap_dance(state);
            break;
        case 2:
            // €
            unicode_input_start();
            register_hex(0x20AC);
            unicode_input_finish();
            print("You pressed the Degrees key 2 times!\n");
            reset_tap_dance(state);
            break;
        case 1:
            // °
            unicode_input_start();
            register_hex(0x00B0);
            unicode_input_finish();
            print("You pressed the Degrees key 1 time!\n");
            reset_tap_dance(state);
            break;
    }
}

// Handles per-key configuration of Retro-Tapping
bool get_retro_tapping(uint16_t keycode, keyrecord_t* record) {
    switch (keycode) {
        default:
            return false;
    }
}
// Handles per-key configuration of Mod-Tap-Interrupt
bool get_ignore_mod_tap_interrupt(uint16_t keycode, keyrecord_t* record) {
    switch (keycode) {
        default:
            return false;
    }
}
// Handles per-key configuration of Tapping Force-Hold
bool get_tapping_force_hold(uint16_t keycode, keyrecord_t* record) {
    switch (keycode) {
        default:
            return false;
    }
}
// Handles per-key configuration of Permissive-Hold
bool get_permissive_hold(uint16_t keycode, keyrecord_t* record) {
    switch (keycode) {
        default:
            return false;
    }
}
/*
 * The following two functions make leader keys "musical"
 * by playing sound at different stages of the leader chord
 */
// Called when you tap the Leader key
void leader_start(void) {
#ifdef AUDIO_ENABLE
    PLAY_SONG(leader_started);
#endif
}
// Called when either the leader sequence is completed, or the leader timeout is hit
void leader_end(void) {
    if (did_leader_succeed) {
#ifdef AUDIO_ENABLE
        PLAY_SONG(leader_succeed);
#endif
    } else {
#ifdef AUDIO_ENABLE
        PLAY_SONG(leader_fail);
#endif
    }
}

// Monitors and labels the current state of any tap-dances
td_state_t cur_dance(qk_tap_dance_state_t* state) {
    if (state->count == 1) {
        if (state->interrupted || !state->pressed) return TD_SINGLE_TAP;
        // Key has not been interrupted, but the key is still held. Means you want to send a 'HOLD'.
        else
            return TD_SINGLE_HOLD;
    } else if (state->count == 2) {
        if (state->interrupted)
            return TD_DOUBLE_SINGLE_TAP;
        else if (state->pressed)
            return TD_DOUBLE_HOLD;
        else
            return TD_DOUBLE_TAP;
    }
    if (state->count == 3) {
        if (state->interrupted || !state->pressed)
            return TD_TRIPLE_TAP;
        else
            return TD_TRIPLE_HOLD;
    } else
        return TD_UNKNOWN;
}

// Create an instance of 'td_tap_t' for each tap dance.
static td_tap_t sml_state   = {.is_press_action = true, .state = TD_NONE};
static td_tap_t scap_state  = {.is_press_action = true, .state = TD_NONE};
static td_tap_t slctl_state = {.is_press_action = true, .state = TD_NONE};
static td_tap_t slalt_state = {.is_press_action = true, .state = TD_NONE};

// Left-Shift->Sticky-Caps tap-dance finished
void scap_finished(qk_tap_dance_state_t* state, void* user_data) {
    scap_state.state = cur_dance(state);
    switch (scap_state.state) {
        case TD_SINGLE_HOLD:
            register_code(KC_LSFT);
            break;
        default:
            if (host_keyboard_leds() & (1 << USB_LED_CAPS_LOCK)) {
                tap_code(KC_CAPS);
                reset_tap_dance(state);
                break;
            } else {
                if ((state->count) >= TAPPING_TOGGLE) {
                    tap_code(KC_CAPS);
                    reset_tap_dance(state);
                    break;
                } else {
                    register_code(KC_LSFT);
                    break;
                }
            }
    }
}

// Left-Shift->Sticky-Caps tap-dance reset
void scap_reset(qk_tap_dance_state_t* state, void* user_data) {
    unregister_code(KC_LSHIFT);
    scap_state.state = TD_NONE;
}

// Sticky-Left-Control tap-dance finished
void slctl_finished(qk_tap_dance_state_t* state, void* user_data) {
    slctl_state.state = cur_dance(state);
    switch (slctl_state.state) {
        case TD_SINGLE_HOLD:
            register_code(KC_LCTL);
            break;
        default:
            if (lctl_sticky) {
                unregister_code(KC_LCTL);
                lctl_sticky = false;
                PLAY_SONG(slctl_off);
                reset_tap_dance(state);
                break;
            } else {
                if ((state->count) >= TAPPING_TOGGLE) {
                    register_code(KC_LCTL);
                    lctl_sticky = true;
                    PLAY_SONG(slctl_on);
                    reset_tap_dance(state);
                    break;
                } else {
                    register_code(KC_LCTL);
                    reset_tap_dance(state);
                    break;
                }
            }
    }
}

// Sticky-Left-Control tap-dance reset
void slctl_reset(qk_tap_dance_state_t* state, void* user_data) {
    if (!lctl_sticky) {
        unregister_code(KC_LCTL);
        slctl_state.state = TD_NONE;
    } else {
        slctl_state.state = TD_NONE;
    }
}

// Sticky-Left-Alt tap-dance finished
void slalt_finished(qk_tap_dance_state_t* state, void* user_data) {
    slalt_state.state = cur_dance(state);
    switch (slalt_state.state) {
        case TD_SINGLE_HOLD:
            register_code(KC_LALT);
            break;
        default:
            if (lalt_sticky) {
                unregister_code(KC_LALT);
                lalt_sticky = false;
                PLAY_SONG(slalt_off);
                reset_tap_dance(state);
                break;
            } else {
                if ((state->count) >= TAPPING_TOGGLE) {
                    register_code(KC_LALT);
                    lalt_sticky = true;
                    PLAY_SONG(slalt_on);
                    reset_tap_dance(state);
                    break;
                } else {
                    register_code(KC_LALT);
                    reset_tap_dance(state);
                    break;
                }
            }
    }
}

// Sticky-Left-Alt tap-dance reset
void slalt_reset(qk_tap_dance_state_t* state, void* user_data) {
    if (!lalt_sticky) {
        unregister_code(KC_LALT);
        slalt_state.state = TD_NONE;
    } else {
        slalt_state.state = TD_NONE;
    }
}

// Smiley key tap-dance finished
void sml_finished(qk_tap_dance_state_t* state, void* user_data) {
    sml_state.state = cur_dance(state);
    switch (sml_state.state) {
        default:
            switch (state->count) {
                default:
                    // 👍
                    send_unicode_string("👍");
                    print("You pressed the Emoji key at least 11 times!\n");
                    reset_tap_dance(state);
                    break;
                case 10:
                    // 👎
                    send_unicode_string("👎");
                    print("You pressed the Emoji key 10 times!\n");
                    reset_tap_dance(state);
                    break;
                case 9:
                    //🍌
                    send_unicode_string("🍌");
                    print("You pressed the Emoji key 9 times!\n");
                    reset_tap_dance(state);
                    break;
                case 8:
                    // 🍑
                    send_unicode_string("🍑");
                    print("You pressed the Emoji key 8 times!\n");
                    reset_tap_dance(state);
                    break;
                case 7:
                    // 🐕
                    send_unicode_string("🐕");
                    print("You pressed the Emoji key 7 times!\n");
                    reset_tap_dance(state);
                    break;
                case 6:
                    // 🐈
                    send_unicode_string("🐈");
                    print("You pressed the Emoji key 6 times!\n");
                    reset_tap_dance(state);
                    break;
                case 5:
                    // 🐍
                    send_unicode_string("🐍");
                    print("You pressed the Emoji key 5 times!\n");
                    reset_tap_dance(state);
                    break;
                case 4:
                    //🐒
                    send_unicode_string("🐒");
                    print("You pressed the Emoji key 4 times!\n");
                    reset_tap_dance(state);
                    break;
                case 3:
                    // 💩
                    send_unicode_string("💩");
                    print("You pressed the Emoji key 3 times!\n");
                    reset_tap_dance(state);
                    break;
                case 2:
                    // 🙁
                    send_unicode_string("🙁");
                    print("You pressed the Emoji key 2 times!\n");
                    reset_tap_dance(state);
                    break;
                case 1:
                    // 🙂
                    send_unicode_string("🙂");
                    print("You pressed the Emoji key 1 time!\n");
                    reset_tap_dance(state);
                    break;
            }
            break;
        case TD_SINGLE_HOLD:
            // 👍
            send_unicode_string("👍");
            print("You single-held the Emoji key!\n");
            reset_tap_dance(state);
            break;
        case TD_DOUBLE_HOLD:
            // 👎
            send_unicode_string("👎");
            print("You double-held the Emoji key!\n");
            reset_tap_dance(state);
            break;
        case TD_TRIPLE_HOLD:
            //🤯
            send_unicode_string("🤯");
            print("You triple-held the Emoji key!\n");
            reset_tap_dance(state);
            break;
        case TD_NONE:
            reset_tap_dance(state);
            break;
    }
}
void sml_reset(qk_tap_dance_state_t* state, void* user_data) { sml_state.state = TD_NONE; }

// Tap Dance definitions
qk_tap_dance_action_t tap_dance_actions[] = {
    // Tap once for °, twice for ℉, thrice for ℃
    [TD_DEG_DEGF]    = ACTION_TAP_DANCE_FN(send_degree_symbol),                                    //
    [TD_LSHFT_CAPS]  = ACTION_TAP_DANCE_FN_ADVANCED_TIME(NULL, scap_finished, scap_reset, 200),    //
    [TD_LCTL_STICKY] = ACTION_TAP_DANCE_FN_ADVANCED_TIME(NULL, slctl_finished, slctl_reset, 200),  //
    [TD_LALT_STICKY] = ACTION_TAP_DANCE_FN_ADVANCED_TIME(NULL, slalt_finished, slalt_reset, 200),  //
    [TD_SMILEY]      = ACTION_TAP_DANCE_FN_ADVANCED_TIME(NULL, sml_finished, sml_reset, 500),
};

// Dip-Switch controls
void dip_switch_update_user(uint8_t index, bool active) {
    switch (index) {
        case 0: {
#ifdef AUDIO_ENABLE
            static bool play_sound = false;
#endif
            if (active) {
#ifdef AUDIO_ENABLE
                if (play_sound) {
                    PLAY_SONG(plover_song);
                }
#endif
                layer_on(_ADJUST);
            } else {
#ifdef AUDIO_ENABLE
                if (play_sound) {
                    PLAY_SONG(plover_gb_song);
                }
#endif
                layer_off(_ADJUST);
            }
#ifdef AUDIO_ENABLE
            play_sound = true;
#endif
            break;
        }
        case 1:
            if (active) {
                muse_mode = true;
            } else {
                muse_mode = false;
            }
    }
}

// Runs at every complete matrix scan
void matrix_scan_user(void) {
    // Some code for controlling MIDI output
#ifdef AUDIO_ENABLE
    if (muse_mode) {
        if (muse_counter == 0) {
            uint8_t muse_note = muse_offset + SCALE[muse_clock_pulse()];
            if (muse_note != last_muse_note) {
                stop_note(compute_freq_for_midi_note(last_muse_note));
                play_note(compute_freq_for_midi_note(muse_note), 0xF);
                last_muse_note = muse_note;
            }
        }
        muse_counter = (muse_counter + 1) % muse_tempo;
    } else {
        if (muse_counter) {
            stop_all_notes();
            muse_counter = 0;
        }
    }
#endif
    // Check the shift-state and hold-time for the Irony key
    if (irony_active) {
        if ((get_mods() & MOD_MASK_SHIFT)) {
            irony_shifted = true;
        } else {
            irony_shifted = false;
        }
        if (timer_elapsed(irony_pressed_time) >= IRONY_HOLD_DELAY) {
            if (irony_shifted) {
                send_unicode_string(bang_str);
            } else {
                send_unicode_string(irony_str);
            }
        }
    }

    // Monitor and respond to the current Alt+Tab state
    if (is_alt_tab_active) {
        if (timer_elapsed(alt_tab_timer) > 1000) {
            unregister_code(KC_LALT);
            is_alt_tab_active = false;
        }
    }
    // Monitor and perform leader-key chords
    LEADER_DICTIONARY() {
        did_leader_succeed = leading = false;

        SEQ_ONE_KEY(KC_E) {
            SEND_STRING(SS_LCTL(SS_LSFT("t")));
            did_leader_succeed = true;
        }
        SEQ_ONE_KEY(KC_C) {
            SEND_STRING(SS_LGUI("r") SS_DELAY(250) "calc\n");
            did_leader_succeed = true;
        }
        else SEQ_ONE_KEY(KC_V) {
            SEND_STRING(SS_LCTL("v"));
            did_leader_succeed = true;
        }
        else SEQ_TWO_KEYS(KC_E, KC_D) {
            SEND_STRING(SS_LGUI("r") "cmd\n" SS_LCTL("c"));
            did_leader_succeed = true;
        }
        else SEQ_TWO_KEYS(KC_A, KC_C) {
            SEND_STRING(SS_LCTL("a") SS_LCTL("c"));
            did_leader_succeed = true;
        }
        else SEQ_THREE_KEYS(KC_C, KC_A, KC_T) {
            send_unicode_string("😸");
            did_leader_succeed = true;
        }
        else SEQ_THREE_KEYS(KC_B, KC_A, KC_T) {
            send_unicode_string("🦇");
            did_leader_succeed = true;
        }
        else SEQ_THREE_KEYS(KC_D, KC_O, KC_G) {
            send_unicode_string("🐶");
            did_leader_succeed = true;
        }
        else SEQ_FIVE_KEYS(KC_S, KC_M, KC_I, KC_L, KC_E) {
            send_unicode_string("🙂");
            did_leader_succeed = true;
        }
        else SEQ_FOUR_KEYS(KC_H, KC_A, KC_P, KC_Y) {
            send_unicode_string("🙂");
            did_leader_succeed = true;
        }
        else SEQ_FIVE_KEYS(KC_H, KC_A, KC_P, KC_P, KC_Y) {
            send_unicode_string("🙂");
            did_leader_succeed = true;
        }
        else SEQ_THREE_KEYS(KC_S, KC_A, KC_D) {
            send_unicode_string("🙁");
            did_leader_succeed = true;
        }
        else SEQ_THREE_KEYS(KC_Y, KC_E, KC_S) {
            send_unicode_string("👍");
            did_leader_succeed = true;
        }
        else SEQ_TWO_KEYS(KC_N, KC_O) {
            send_unicode_string("👎");
            did_leader_succeed = true;
        }
        else SEQ_THREE_KEYS(KC_W, KC_O, KC_W) {
            send_unicode_string("🤯");
            did_leader_succeed = true;
        }
        else SEQ_THREE_KEYS(KC_P, KC_O, KC_O) {
            send_unicode_string("💩");
            did_leader_succeed = true;
        }
        else SEQ_FOUR_KEYS(KC_P, KC_O, KC_O, KC_P) {
            send_unicode_string("💩");
            did_leader_succeed = true;
        }
        else SEQ_FOUR_KEYS(KC_B, KC_O, KC_A, KC_T) {
            send_unicode_string("⛵");
            did_leader_succeed = true;
        }
        leader_end();
    }
    // Run the wake-up RGB animation if performing wake-up
    if (do_wake_animation) {
        rgb_wakeup_sequence();
    }
}

// Music mask controls

bool music_mask_user(uint16_t keycode) {
    switch (keycode) {
        case RAISE:
        case LOWER:
            return false;
        default:
            return true;
    }
}

void suspend_power_down_user(void) {
    // Runs during start of system suspend
    print("Going to sleep.");
}

void suspend_wakeup_init_user(void) {
    // Runs during wake from system suspend
}

void keyboard_post_init_user(void) {
    // Print welcome message to console
    printf("Welcome to %s!\n", KEEB_MODEL_NAME);
    // Read the user config from EEPROM
    user_config.raw   = eeconfig_read_user();
    do_wake_animation = user_config.do_wakeup_animation;
    do_wake_audio     = user_config.do_wakeup_audio;
    rgbcon_tracker    = user_config.rgbcon_tracker;

    // Tell the console the status of saved config
    if (user_config.do_wakeup_animation) {
        print("Wake animation enabled.\n");
    } else {
        print("Wake animation disabled.\n");
    }
    if (user_config.do_wakeup_audio) {
        print("Wake music enabled.\n");
    } else {
        print("Wake music disabled.\n");
    }
    switch (user_config.rgbcon_tracker) {
        case 2:
            print("RGB mode: Static White\n");
            break;
        case 3:
            print("RGB mode: Static Cyan\n");
            break;
        case 4:
            print("RGB mode: Breathing Lights\n");
            break;
        case 5:
            print("RGB mode: Rainbow Swirl\n");
            break;
        case 6:
            print("RGB mode: Rainbow Mood\n");
            break;
        default:
            print("RGB mode: Disabled RGB\n");
            break;
    }

    // Enable the LED layers
    rgblight_enable_noeeprom();  // Enables RGB, without saving settings
    rgblight_layers = my_rgb_layers;

    /*
     * Initialize the LED crawl wake animation here
     * To perform it on just the first wake
     */
    wake_rgb_timer = timer_read();
    waking_up      = true;

    // Initialize OLED display
    /*
        if (USING_OLED_DISPLAY) {
            print("Initializing display!\n");
            render_logo();
        }
    */
}

// EEPROM is getting reset!
void eeconfig_init_user(void) {
    user_config.raw                 = 0;
    eeconfig_update_user(user_config.raw);  // Write default value to EEPROM now
}

// Communicate 2-way with host via HID_RAW
#ifdef RAW_ENABLE
void raw_hid_receive(uint8_t* data, uint8_t length) {
    //  Sample code below simply echoes back to the console any data received by the raw_hid process

#    ifdef CONSOLE_ENABLE
    dprint("Received USB data from host system:\n");
    dprintf("%s\n", data);
#    endif
}
#endif