rmk 0.8.2

Keyboard firmware written in Rust
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
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
use core::cell::RefCell;
use core::fmt::Debug;

use embassy_futures::select::{Either, select};
use embassy_futures::yield_now;
#[cfg(feature = "_ble")]
use embassy_sync::signal::Signal;
use embassy_time::{Duration, Instant, Timer, with_deadline};
use heapless::Vec;
use rmk_types::action::{Action, KeyAction, MorseMode};
use rmk_types::keycode::KeyCode;
use rmk_types::led_indicator::LedIndicator;
use rmk_types::modifier::ModifierCombination;
use rmk_types::mouse_button::MouseButtons;
use usbd_hid::descriptor::{MediaKeyboardReport, MouseReport, SystemControlReport};
#[cfg(feature = "controller")]
use {
    crate::channel::{CONTROLLER_CHANNEL, ControllerPub, send_controller_event},
    crate::event::ControllerEvent,
};

use crate::channel::{KEY_EVENT_CHANNEL, KEYBOARD_REPORT_CHANNEL};
use crate::combo::Combo;
use crate::config::Hand;
use crate::descriptor::KeyboardReport;
use crate::event::{KeyPos, KeyboardEvent, KeyboardEventPos};
use crate::fork::{ActiveFork, StateBits};
use crate::hid::Report;
use crate::input_device::Runnable;
use crate::input_device::rotary_encoder::Direction;
use crate::keyboard::held_buffer::{HeldBuffer, HeldKey, KeyState};
use crate::keyboard_macros::MacroOperation;
use crate::keymap::KeyMap;
use crate::morse::{MorsePattern, TAP};
#[cfg(all(feature = "split", feature = "_ble"))]
use crate::split::ble::central::update_activity_time;
use crate::{FORK_MAX_NUM, boot};

pub(crate) mod combo;
pub(crate) mod held_buffer;
pub(crate) mod morse;
pub(crate) mod mouse;
pub(crate) mod oneshot;

const HOLD_BUFFER_SIZE: usize = 16;

// Timestamp of the last key action, the value is the number of seconds since the boot
#[cfg(feature = "_ble")]
pub(crate) static LAST_KEY_TIMESTAMP: Signal<crate::RawMutex, u32> = Signal::new();

/// Led states for the keyboard hid report (its value is received by by the light service in a hid report)
/// LedIndicator type would be nicer, but that does not have const expr constructor
pub(crate) static LOCK_LED_STATES: core::sync::atomic::AtomicU8 = core::sync::atomic::AtomicU8::new(0u8);

/// State machine for one shot keys
#[derive(Default)]
enum OneShotState<T> {
    /// First one shot key press
    Initial(T),
    /// One shot key was released before any other key, normal one shot behavior
    Single(T),
    /// Another key was pressed before one shot key was released, treat as a normal modifier/layer
    Held(T),
    /// One shot inactive
    #[default]
    None,
}

impl<T> OneShotState<T> {
    /// Get the current one shot value if any
    pub fn value(&self) -> Option<&T> {
        match self {
            OneShotState::Initial(v) | OneShotState::Single(v) | OneShotState::Held(v) => Some(v),
            OneShotState::None => None,
        }
    }
}

/// State machine for Caps Word
#[derive(Debug, Default)]
enum CapsWordState {
    /// Caps Word is activated (but may have timed out thus becoming inactive)
    Activated {
        /// Time since last key press
        timer: Instant,
        /// Whether the current key should be shifted
        shift_current: bool,
    },
    /// Caps Word is deactivated
    #[default]
    Deactivated,
}

impl CapsWordState {
    /// Caps Word timeout duration
    const TIMEOUT: Duration = Duration::from_secs(5);

    /// Activate Caps Word
    fn activate(&mut self) {
        *self = CapsWordState::Activated {
            timer: Instant::now(),
            shift_current: false,
        };
    }

    /// Deactivate Caps Word
    fn deactivate(&mut self) {
        *self = CapsWordState::Deactivated;
    }

    /// Toggle Caps Word
    fn toggle(&mut self) {
        match self {
            CapsWordState::Activated { .. } => self.deactivate(),
            CapsWordState::Deactivated => self.activate(),
        }
    }

    /// Return whether Caps Word is active (and has not timed out)
    fn is_active(&self) -> bool {
        if let CapsWordState::Activated { timer, .. } = self {
            timer.elapsed() < Self::TIMEOUT
        } else {
            false
        }
    }

    /// Return whether the current key pressed is to be shifted
    fn is_shift_current(&self) -> bool {
        if let CapsWordState::Activated { shift_current, .. } = self {
            *shift_current
        } else {
            false
        }
    }

    /// Check whether to shift the given key, and update the state accordingly
    ///
    /// Note that this function does not check the CapsWord key itself.
    fn check(&mut self, key: KeyCode) {
        if let CapsWordState::Activated { timer, shift_current } = self {
            if key.is_caps_word_continue_key() && timer.elapsed() < Self::TIMEOUT {
                *timer = Instant::now();
                *shift_current = key.is_caps_word_shifted_key();
            } else {
                self.deactivate();
            }
        }
    }
}

impl<const ROW: usize, const COL: usize, const NUM_LAYER: usize, const NUM_ENCODER: usize> Runnable
    for Keyboard<'_, ROW, COL, NUM_LAYER, NUM_ENCODER>
{
    /// Main keyboard processing task, it receives input devices result, processes keys.
    /// The report is sent using `send_report`.
    async fn run(&mut self) {
        loop {
            // TODO: Now the unprocessed_events is only used in one-shot keys and clear peer key.
            // Maybe it can be removed in the future?
            if !self.unprocessed_events.is_empty() {
                // Process unprocessed events
                let e = self.unprocessed_events.remove(0);
                debug!("Unprocessed event: {:?}", e);
                self.process_inner(e).await
            } else if let Some(key) = self.next_buffered_key() {
                // Process buffered held key
                self.process_buffered_key(key).await
            } else {
                // No buffered tap-hold event, wait for new key
                let event = KEY_EVENT_CHANNEL.receive().await;
                // Process the key event
                self.process_inner(event).await
            };
        }
    }
}

pub struct Keyboard<'a, const ROW: usize, const COL: usize, const NUM_LAYER: usize, const NUM_ENCODER: usize = 0> {
    /// Keymap
    pub(crate) keymap: &'a RefCell<KeyMap<'a, ROW, COL, NUM_LAYER, NUM_ENCODER>>,

    /// Unprocessed events
    pub unprocessed_events: Vec<KeyboardEvent, 4>,

    /// Buffered held keys
    pub held_buffer: HeldBuffer,

    /// Timer which records the timestamp of key changes
    pub(crate) timer: [[Option<Instant>; ROW]; COL],

    /// Timer which records the timestamp of rotary encoder changes
    pub(crate) rotary_encoder_timer: [[Option<Instant>; 2]; NUM_ENCODER],

    /// Record the timestamp of last **simple key** press.
    /// It's used in tap-hold prior-idle-time check.
    last_press_time: Instant,

    /// stores the last KeyCode executed, to be repeated if the repeat key os pressed
    /// Used in repeat-key
    last_key_code: KeyCode,

    /// One shot layer state
    osl_state: OneShotState<u8>,

    /// One shot modifier state
    osm_state: OneShotState<ModifierCombination>,

    /// Caps Word state machine
    caps_word: CapsWordState,

    /// The modifiers coming from (last) Action::KeyWithModifier
    with_modifiers: ModifierCombination,

    /// Macro text typing state (affects the effective modifiers)
    macro_texting: bool,
    macro_caps: bool,

    /// The real state before fork activations is stored here
    fork_states: [Option<ActiveFork>; FORK_MAX_NUM], // chosen replacement key of the currently triggered forks and the related modifier suppression
    fork_keep_mask: ModifierCombination, // aggregate here the explicit modifiers pressed since the last fork activations

    /// The held modifiers for the keyboard hid report
    held_modifiers: ModifierCombination,

    /// The held keys for the keyboard hid report, except the modifiers
    held_keycodes: [KeyCode; 6],

    /// Registered key position.
    /// This is still needed besides `held_keycodes` because multiple keys with same keycode can be registered.
    registered_keys: [Option<KeyboardEvent>; 6],

    /// Internal mouse report buf
    mouse_report: MouseReport,

    /// Internal media report buf
    media_report: MediaKeyboardReport,

    /// Internal system control report buf
    system_control_report: SystemControlReport,

    /// Mouse acceleration state
    mouse_accel: u8,
    mouse_repeat: u8,
    mouse_wheel_repeat: u8,

    /// Used for temporarily disabling combos
    combo_on: bool,

    /// Publisher for controller channel
    #[cfg(feature = "controller")]
    controller_pub: ControllerPub,
}

impl<'a, const ROW: usize, const COL: usize, const NUM_LAYER: usize, const NUM_ENCODER: usize>
    Keyboard<'a, ROW, COL, NUM_LAYER, NUM_ENCODER>
{
    pub fn new(keymap: &'a RefCell<KeyMap<'a, ROW, COL, NUM_LAYER, NUM_ENCODER>>) -> Self {
        Keyboard {
            keymap,
            timer: [[None; ROW]; COL],
            rotary_encoder_timer: [[None; 2]; NUM_ENCODER],
            last_press_time: Instant::now(),
            osl_state: OneShotState::default(),
            osm_state: OneShotState::default(),
            caps_word: CapsWordState::default(),
            with_modifiers: ModifierCombination::default(),
            macro_texting: false,
            macro_caps: false,
            fork_states: [None; FORK_MAX_NUM],
            fork_keep_mask: ModifierCombination::default(),
            unprocessed_events: Vec::new(),
            held_buffer: HeldBuffer::new(),
            registered_keys: [None; 6],
            held_modifiers: ModifierCombination::default(),
            held_keycodes: [KeyCode::No; 6],
            mouse_report: MouseReport {
                buttons: 0,
                x: 0,
                y: 0,
                wheel: 0,
                pan: 0,
            },
            media_report: MediaKeyboardReport { usage_id: 0 },
            system_control_report: SystemControlReport { usage_id: 0 },
            last_key_code: KeyCode::No,
            mouse_accel: 0,
            mouse_repeat: 0,
            mouse_wheel_repeat: 0,
            combo_on: true,
            #[cfg(feature = "controller")]
            controller_pub: unwrap!(CONTROLLER_CHANNEL.publisher()),
        }
    }

    /// Send a keyboard report to the host
    async fn send_report(&self, report: Report) {
        KEYBOARD_REPORT_CHANNEL.sender().send(report).await
    }

    /// Get a copy of the next timeout key in the buffer,
    /// which is either a combo component that is waiting for other combo keys,
    /// or a morse key that is in the pressed or released state.
    pub fn next_buffered_key(&mut self) -> Option<HeldKey> {
        self.held_buffer.next_timeout(|k| {
            matches!(k.state, KeyState::Released(_) | KeyState::WaitingCombo)
                || (matches!(k.state, KeyState::Pressed(_)) && k.action.is_morse())
        })
    }

    // Clean up for leak keys, remove non morse keys in ProcessedButReleaseNotReportedYet state from the buffer
    pub(crate) fn clean_buffered_processed_keys(&mut self) {
        self.held_buffer.keys.retain(|k| {
            if k.action.is_morse() {
                true
            } else {
                match k.state {
                    KeyState::ProcessedButReleaseNotReportedYet(_) => {
                        warn!("NEED CLEAN: Processing buffering TAP keys with post tap: {:?}", k.event);
                        false
                    }
                    _ => true,
                }
            }
        });
    }

    /// Process the latest buffered key.
    ///
    /// The given holding key is a copy of the buffered key. Only tap-hold keys are considered now.
    pub async fn process_buffered_key(&mut self, key: HeldKey) {
        debug!(
            "Processing buffered key: \nevent: {:?} state: {:?}",
            key.event, key.state
        );
        match key.state {
            KeyState::WaitingCombo => {
                debug!(
                    "[Combo] Waiting combo, timeout in: {:?}ms",
                    (key.timeout_time.saturating_duration_since(Instant::now())).as_millis()
                );
                match with_deadline(key.timeout_time, KEY_EVENT_CHANNEL.receive()).await {
                    Ok(event) => {
                        // Process new key event
                        debug!("[Combo] Interrupted by a new key event: {:?}", event);
                        self.process_inner(event).await;
                    }
                    Err(_timeout) => {
                        // Timeout, dispatch combo
                        debug!("[Combo] Timeout, dispatch combo");
                        self.dispatch_combos(&key.action, key.event).await;
                    }
                }
            }
            KeyState::Pressed(_) | KeyState::Released(_) => {
                if key.action.is_morse() {
                    // Wait for timeout or new key event
                    info!("Waiting morse key: {:?}", key.action);
                    match with_deadline(key.timeout_time, KEY_EVENT_CHANNEL.receive()).await {
                        Ok(event) => {
                            debug!("Buffered morse key interrupted by a new key event: {:?}", event);
                            self.process_inner(event).await;
                        }
                        Err(_timeout) => {
                            debug!("Buffered morse key timeout");
                            self.handle_morse_timeout(&key).await;
                        }
                    }
                }
            }
            _ => (),
        }
    }

    /// Process key changes at (row, col)
    pub async fn process_inner(&mut self, event: KeyboardEvent) {
        #[cfg(feature = "vial_lock")]
        self.keymap.borrow_mut().matrix_state.update(&event);

        // Matrix should process key pressed event first, record the timestamp of key changes
        if event.pressed {
            self.set_timer_value(event, Some(Instant::now()));
        }
        // Update activity time for BLE split central sleep management
        #[cfg(all(feature = "split", feature = "_ble"))]
        update_activity_time();

        // Process key
        let key_action = &self.keymap.borrow_mut().get_action_with_layer_cache(event);

        if self.combo_on {
            if let (Some(key_action), is_combo) = self.process_combo(key_action, event).await {
                self.process_key_action(&key_action, event, is_combo).await
            }
        } else {
            self.process_key_action(key_action, event, false).await
        }
    }

    async fn process_key_action(&mut self, key_action: &KeyAction, event: KeyboardEvent, is_combo: bool) {
        // First, make the decision for current key and held keys
        let (decision_for_current_key, decisions) = self.make_decisions_for_keys(key_action, event);

        // Fire held keys if needed
        let (keyboard_state_updated, updated_decision_for_cur_key) =
            self.fire_held_keys(decision_for_current_key, decisions).await;

        // Process current key action after all held keys are resolved
        match updated_decision_for_cur_key {
            KeyBehaviorDecision::CleanBuffer | KeyBehaviorDecision::Release => {
                debug!("Clean buffer, then process current key normally");
                let key_action = if keyboard_state_updated && !is_combo {
                    // The key_action needs to be updated due to the morse key might be triggered
                    &self.keymap.borrow_mut().get_action_with_layer_cache(event)
                } else {
                    key_action
                };
                self.process_key_action_inner(key_action, event).await
            }
            KeyBehaviorDecision::Buffer => {
                debug!("Current key is buffered");
                let press_time = Instant::now();
                let timeout_time = if key_action.is_morse() {
                    press_time + Self::morse_timeout(&self.keymap.borrow(), key_action, true)
                } else {
                    press_time
                };
                self.held_buffer.push(HeldKey::new(
                    event,
                    *key_action,
                    KeyState::Pressed(MorsePattern::default()),
                    press_time,
                    timeout_time,
                ));
            }
            KeyBehaviorDecision::Ignore => {
                debug!("Current key is ignored or not buffered, process normally: {:?}", event);
                // Process current key normally
                let key_action = if keyboard_state_updated && !is_combo {
                    // The key_action needs to be updated due to the morse key might be triggered
                    &self.keymap.borrow_mut().get_action_with_layer_cache(event)
                } else {
                    key_action
                };
                self.process_key_action_inner(key_action, event).await
            }
            KeyBehaviorDecision::FlowTap => {
                let action = Self::action_from_pattern(self.keymap.borrow().behavior, key_action, TAP); //tap action
                self.process_key_action_normal(action, event).await;
                // Push back after triggered press
                let now = Instant::now();
                let time_out = now + Self::morse_timeout(&self.keymap.borrow(), key_action, true);
                self.held_buffer.push(HeldKey::new(
                    event,
                    *key_action,
                    KeyState::ProcessedButReleaseNotReportedYet(action),
                    now,
                    time_out,
                ));
            }
        }
    }

    /// Fire held keys according to their decisions.
    ///
    /// This function fires held keys according to their decisions, and returns
    /// whether the keyboard state is updated after firing those keys and
    /// the updated decision for current key.
    async fn fire_held_keys(
        &mut self,
        mut decision_for_current_key: KeyBehaviorDecision,
        decisions: Vec<(KeyboardEventPos, HeldKeyDecision), 16>,
    ) -> (bool, KeyBehaviorDecision) {
        let mut keyboard_state_updated = false;
        // Fire buffered keys
        for (pos, decision) in decisions {
            // Some decisions of held keys have been made, fire those keys
            // debug!("✅ Decision for held key: {:?}: {:?}", pos, decision)
            match decision {
                HeldKeyDecision::UnilateralTap | HeldKeyDecision::FlowTap => {
                    if let Some(mut held_key) = self.held_buffer.remove_if(|k| k.event.pos == pos)
                        && held_key.action.is_morse()
                    {
                        // Unilateral tap of the held key is triggered
                        debug!("Cleaning buffered morse key due to unilateral tap or flow tap");
                        match held_key.state {
                            KeyState::Pressed(_) | KeyState::Holding(_) => {
                                // In this state pattern is not surely finished,
                                // however an other key is pressed so terminate the sequence
                                // with a tap due to UnilateralTap decision; try to resolve as is
                                let pattern = match held_key.state {
                                    KeyState::Pressed(pattern) => pattern.followed_by_tap(), // The HeldKeyDecision turned this into tap!
                                    KeyState::Holding(pattern) => pattern,
                                    _ => unreachable!(),
                                };
                                debug!("Pattern after unilateral tap or flow tap: {:?}", pattern);
                                let action =
                                    Self::action_from_pattern(self.keymap.borrow().behavior, &held_key.action, pattern);
                                self.process_key_action_normal(action, held_key.event).await;
                                held_key.state = KeyState::ProcessedButReleaseNotReportedYet(action);
                                // Push back after triggered tap
                                self.held_buffer.push_without_sort(held_key);
                            }
                            KeyState::Released(pattern) => {
                                // In this state pattern is not surely finished,
                                // however an other key is pressed so terminate the sequence, try to resolve as is
                                debug!("Pattern after released, unilateral tap or flow tap: {:?}", pattern);
                                let action =
                                    Self::action_from_pattern(self.keymap.borrow().behavior, &held_key.action, pattern);
                                held_key.event.pressed = true;
                                self.process_key_action_tap(action, held_key.event).await;
                                // The tap is fully fired, don't push it back to buffer again
                                // Removing from the held buffer is like setting to an idle state
                            }
                            _ => (),
                        }
                    }
                }
                HeldKeyDecision::PermissiveHold | HeldKeyDecision::HoldOnOtherKeyPress => {
                    if let Some(mut held_key) = self.held_buffer.remove_if(|k| k.event.pos == pos) {
                        let action = self.keymap.borrow_mut().get_action_with_layer_cache(held_key.event);

                        if action.is_morse() {
                            // Permissive hold of held key is triggered
                            debug!("Cleaning buffered morse key due to permissive hold or hold on other key press");
                            match held_key.state {
                                KeyState::Pressed(_) | KeyState::Holding(_) => {
                                    // In this state pattern is not surely finished,
                                    // however an other key is pressed so terminate the sequence
                                    // with a hold due to PermissiveHold/HoldOnOtherKeyPress decision; try to resolve as is
                                    let pattern = match held_key.state {
                                        KeyState::Pressed(pattern) => pattern.followed_by_hold(), // The HeldKeyDecision turned this into hold!
                                        KeyState::Holding(pattern) => pattern,
                                        _ => unreachable!(),
                                    };
                                    keyboard_state_updated = true;
                                    debug!("pattern after permissive hold: {:?}", pattern);
                                    let action =
                                        Self::action_from_pattern(self.keymap.borrow().behavior, &action, pattern);
                                    self.process_key_action_normal(action, held_key.event).await;
                                    held_key.state = KeyState::ProcessedButReleaseNotReportedYet(action);
                                    // Push back after triggered hold
                                    self.held_buffer.push_without_sort(held_key);
                                }
                                KeyState::Released(pattern) => {
                                    debug!("pattern after released, permissive hold: {:?}", pattern);
                                    let action =
                                        Self::action_from_pattern(self.keymap.borrow().behavior, &action, pattern);
                                    held_key.event.pressed = true;
                                    self.process_key_action_tap(action, held_key.event).await;
                                    // The tap is fully fired, don't push it back to buffer again
                                    // Removing from the held buffer is like setting to an idle state
                                }
                                _ => (),
                            }
                        }
                    }
                }
                HeldKeyDecision::Release => {
                    // Releasing the current key, will always be tapping, because timeout isn't here
                    if let Some(mut held_key) = self.held_buffer.remove_if(|k| k.event.pos == pos) {
                        let key_action = if keyboard_state_updated {
                            self.keymap.borrow_mut().get_action_with_layer_cache(held_key.event)
                        } else {
                            held_key.action
                        };
                        debug!("Processing current key before releasing: {:?}", held_key.event);
                        if !key_action.is_morse() {
                            match key_action {
                                KeyAction::Single(action) => {
                                    self.process_key_action_normal(action, held_key.event).await;
                                }
                                KeyAction::Tap(action) => {
                                    self.process_key_action_tap(action, held_key.event).await;
                                }
                                _ => unreachable!(),
                            }
                        } else {
                            match held_key.state {
                                KeyState::Pressed(_) | KeyState::Holding(_) => {
                                    debug!("Cleaning buffered Release key");

                                    let pattern = match held_key.state {
                                        KeyState::Pressed(pattern) => pattern.followed_by_tap(), // TODO? should we double check the timeout with Instant::now() >= held_key.timeout_time?
                                        KeyState::Holding(pattern) => pattern,
                                        _ => unreachable!(),
                                    };

                                    debug!("pattern by decided tap release: {:?}", pattern);

                                    let final_action = Self::try_predict_final_action(
                                        self.keymap.borrow().behavior,
                                        &key_action,
                                        pattern,
                                    );
                                    if let Some(action) = final_action {
                                        debug!("tap prediction {:?} -> {:?}", pattern, action);
                                        self.process_key_action_normal(action, held_key.event).await;
                                        held_key.state = KeyState::ProcessedButReleaseNotReportedYet(action);
                                    }
                                }
                                _ => {} // For morse, the releasing will not be processed immediately, so just ignore it
                            }
                            // Push back after triggered hold
                            self.held_buffer.push_without_sort(held_key);
                        }
                    }

                    // After processing current key in `Release` state, mark the `decision_for_current_key` to `CleanBuffer`
                    // That means all normal keys pressed AFTER the press of current releasing key will be fired
                    decision_for_current_key = KeyBehaviorDecision::CleanBuffer;
                }
                HeldKeyDecision::Normal => {
                    // Check if the normal keys in the buffer should be triggered.
                    let trigger_normal = matches!(decision_for_current_key, KeyBehaviorDecision::CleanBuffer);

                    if trigger_normal && let Some(held_key) = self.held_buffer.remove_if(|k| k.event.pos == pos) {
                        debug!("Cleaning buffered normal key");
                        let action = if keyboard_state_updated {
                            self.keymap.borrow_mut().get_action_with_layer_cache(held_key.event)
                        } else {
                            held_key.action
                        };

                        // Note: Morse like actions are not expected here.
                        assert!(!action.is_morse());
                        debug!("Tap Key {:?} now press down, action: {:?}", held_key.event, action);
                        self.process_key_action_inner(&action, held_key.event).await;
                    }
                }
                _ => (),
            }
        }
        (keyboard_state_updated, decision_for_current_key)
    }

    fn get_hand(hand_info: &[[Hand; COL]; ROW], pos: KeyPos) -> Hand {
        let col = pos.col as usize;
        let row = pos.row as usize;
        if col < COL && row < ROW {
            hand_info[row][col]
        } else {
            Hand::Unknown
        }
    }

    /// Make decisions for current key and each held key.
    ///
    /// This function iterates all held keys and makes decision for them if a special mode is triggered, such as permissive hold, etc.
    fn make_decisions_for_keys(
        &mut self,
        key_action: &KeyAction,
        event: KeyboardEvent,
    ) -> (
        KeyBehaviorDecision,
        Vec<(KeyboardEventPos, HeldKeyDecision), HOLD_BUFFER_SIZE>,
    ) {
        // Decision of current key and held keys
        let mut decision_for_current_key = KeyBehaviorDecision::Ignore;
        let mut decisions: Vec<(_, HeldKeyDecision), HOLD_BUFFER_SIZE> = Vec::new();

        // When pressing a morse key, check flow tap first.
        if event.pressed
            && self.keymap.borrow().behavior.morse.enable_flow_tap
            && key_action.is_morse()
            && self.last_press_time.elapsed() < self.keymap.borrow().behavior.morse.prior_idle_time
        {
            // It's in key streak, trigger the first tap action
            debug!("Flow tap detected, trigger tap action for current morse key");

            decision_for_current_key = KeyBehaviorDecision::FlowTap;
        }

        // Whether the held buffer needs to be checked.
        let check_held_buffer = event.pressed
            || self
                .held_buffer
                .find_action(key_action)
                .is_some_and(|k| matches!(k.state, KeyState::Pressed(_) | KeyState::Released(_)));

        if check_held_buffer {
            // First, sort by press time
            self.held_buffer.keys.sort_unstable_by_key(|k| k.press_time);

            // Check all unresolved held keys, calculate their decision one-by-one
            for held_key in self
                .held_buffer
                .keys
                .iter()
                .filter(|k| matches!(k.state, KeyState::Pressed(_) | KeyState::Released(_)))
            {
                // Releasing a key is already buffered
                if !event.pressed && held_key.action == *key_action {
                    debug!("Releasing a held key: {:?}", event);
                    let _ = decisions.push((held_key.event.pos, HeldKeyDecision::Release));
                    decision_for_current_key = KeyBehaviorDecision::Release;
                    continue;
                }

                // Buffered normal keys should be added to the decision list,
                // they will be processed later according to the decision of current key
                if !held_key.action.is_morse() && matches!(held_key.state, KeyState::Pressed(_)) {
                    let _ = decisions.push((held_key.event.pos, HeldKeyDecision::Normal));
                    continue;
                }

                // The remaining keys are not same as the current key, check only morse keys
                if held_key.event.pos != event.pos && held_key.action.is_morse() {
                    let mode = Self::tap_hold_mode(&self.keymap.borrow(), &held_key.action);

                    if event.pressed {
                        // The current key is being pressed

                        if decision_for_current_key == KeyBehaviorDecision::FlowTap
                            && matches!(held_key.state, KeyState::Pressed(_))
                        {
                            debug!("Flow tap triggered, resolve buffered morse key as tapping");
                            // If flow tap of current key is triggered, tapping all held keys
                            let _ = decisions.push((held_key.event.pos, HeldKeyDecision::FlowTap));
                            continue;
                        }

                        // Check morse key mode
                        match mode {
                            MorseMode::PermissiveHold => {
                                // Permissive hold mode checks key releases, so push current key press into buffer.
                                decision_for_current_key = KeyBehaviorDecision::Buffer;
                            }
                            MorseMode::HoldOnOtherPress => {
                                debug!(
                                    "Trigger morse key due to hold on other key press: {:?}",
                                    held_key.action
                                );
                                let _ = decisions.push((held_key.event.pos, HeldKeyDecision::HoldOnOtherKeyPress));
                                decision_for_current_key = KeyBehaviorDecision::CleanBuffer;
                            }
                            _ => {}
                        }
                    } else {
                        let unilateral_tap = Self::is_unilateral_tap_enabled(&self.keymap.borrow(), &held_key.action);

                        // 1. Check unilateral tap of held key
                        // Note: `decision for current key == Release` means that current held key is pressed AFTER the current releasing key,
                        // releasing a key should not trigger unilateral tap of keys which are pressed AFTER the released key
                        if unilateral_tap
                            && event.pos != held_key.event.pos
                            && decision_for_current_key != KeyBehaviorDecision::Release
                            && let KeyboardEventPos::Key(pos1) = held_key.event.pos
                            && let KeyboardEventPos::Key(pos2) = event.pos
                        {
                            let hand_info = &self.keymap.borrow().positional_config.hand;

                            let hand1 = Self::get_hand(hand_info, pos1);
                            let hand2 = Self::get_hand(hand_info, pos2);

                            if hand1 == hand2 && hand1 != Hand::Unknown {
                                //if same hand
                                debug!("Unilateral tap triggered, resolve morse key as tapping");
                                let _ = decisions.push((held_key.event.pos, HeldKeyDecision::UnilateralTap));
                                continue;
                            }
                        }

                        // The current key is being released, check only the held key in permissive hold mode
                        if decision_for_current_key != KeyBehaviorDecision::Release && mode == MorseMode::PermissiveHold
                        {
                            debug!("Permissive hold!");
                            // Check first current releasing key is in the buffer, AND after the current key
                            let _ = decisions.push((held_key.event.pos, HeldKeyDecision::PermissiveHold));
                            decision_for_current_key = KeyBehaviorDecision::CleanBuffer;
                        }
                    }
                }
            }
        }
        (decision_for_current_key, decisions)
    }

    async fn process_key_action_inner(&mut self, original_key_action: &KeyAction, event: KeyboardEvent) {
        // Start forks
        let key_action = self.try_start_forks(original_key_action, event);

        // Clear with_modifier if a new key is pressed
        if self.with_modifiers.into_bits() != 0 && event.pressed {
            self.with_modifiers = ModifierCombination::new();
        }

        #[cfg(feature = "_ble")]
        LAST_KEY_TIMESTAMP.signal(Instant::now().as_secs() as u32);

        #[cfg(feature = "controller")]
        send_controller_event(&mut self.controller_pub, ControllerEvent::Key(event, key_action));

        if !key_action.is_morse() {
            match key_action {
                KeyAction::No | KeyAction::Transparent => (),
                KeyAction::Single(action) => {
                    debug!("Process Single key action: {:?}, {:?}", action, event);
                    self.process_key_action_normal(action, event).await;
                }
                KeyAction::Tap(action) => self.process_key_action_tap(action, event).await,
                _ => unreachable!(),
            }
        } else {
            self.process_key_action_morse(&key_action, event).await;
        }
        self.try_finish_forks(original_key_action, event);
    }

    /// Replaces the incoming key_action if a fork is configured for that key.
    /// The replacement decision is made at key_press time, and the decision
    /// is kept until the key is released.
    fn try_start_forks(&mut self, key_action: &KeyAction, event: KeyboardEvent) -> KeyAction {
        if self.keymap.borrow().behavior.fork.forks.is_empty() {
            return *key_action;
        }

        if !event.pressed {
            for (i, fork) in (&self.keymap.borrow().behavior.fork.forks).into_iter().enumerate() {
                if fork.trigger == *key_action
                    && let Some(active) = self.fork_states[i]
                {
                    // If the originating key of a fork is released, simply release the replacement key
                    // (The fork deactivation is delayed, will happen after the release hid report is sent)
                    debug!("replace input with fork action {:?}", active);
                    return active.replacement;
                }
            }
            return *key_action;
        }

        let mut decision_state = StateBits {
            // "explicit modifiers" includes the effect of one-shot modifiers, held modifiers keys only
            modifiers: self.resolve_explicit_modifiers(event.pressed),
            leds: LedIndicator::from_bits(LOCK_LED_STATES.load(core::sync::atomic::Ordering::Relaxed)),
            mouse: MouseButtons::from_bits(self.mouse_report.buttons),
        };

        let mut triggered_forks = [false; FORK_MAX_NUM]; // used to avoid loops
        let mut chain_starter: Option<usize> = None;
        let mut combined_suppress = ModifierCombination::default();
        let mut replacement = *key_action;

        'bind: loop {
            for (i, fork) in (&self.keymap.borrow().behavior.fork.forks).into_iter().enumerate() {
                if !triggered_forks[i] && self.fork_states[i].is_none() && fork.trigger == replacement {
                    let decision = (fork.match_any & decision_state) != StateBits::default()
                        && (fork.match_none & decision_state) == StateBits::default();

                    replacement = if decision {
                        fork.positive_output
                    } else {
                        fork.negative_output
                    };

                    let suppress = fork.match_any.modifiers & !fork.kept_modifiers;

                    combined_suppress |= suppress;

                    // Suppress the previously activated Action::KeyWithModifiers
                    // (even if they held for a long time, a new keypress arrived
                    // since then, which breaks the key repeat, so losing their
                    // effect likely will not cause problem...)
                    self.with_modifiers &= !suppress;

                    // Reduce the previously aggregated keeps with the match_any mask
                    // (since this is the expected behavior in most cases)
                    self.fork_keep_mask &= !fork.match_any.modifiers;

                    // Then add the user defined keeps (if any)
                    self.fork_keep_mask |= fork.kept_modifiers;

                    if chain_starter.is_none() {
                        chain_starter = Some(i);
                    }

                    if fork.bindable {
                        // If this fork is bindable look for other not yet activated forks,
                        // which can be triggered by that the current replacement key
                        triggered_forks[i] = true; // Avoid triggering the same fork again -> no infinite loops either

                        // For the next fork evaluations, update the decision state
                        // with the suppressed modifiers
                        decision_state.modifiers &= !suppress;
                        continue 'bind;
                    }

                    //return final decision is ready
                    break 'bind;
                }
            }

            // No (more) forks were triggered, so we are done
            break 'bind;
        }

        if let Some(initial) = chain_starter {
            // After the initial fork triggered, we have switched to "bind mode".
            // The later triggered forks will not really activate, only update
            // the replacement decision and modifier suppressions of the initially
            // triggered fork, which is here marked as active:
            self.fork_states[initial] = Some(ActiveFork {
                replacement,
                suppress: combined_suppress,
            });
        }

        // No (or no more) forks were triggered, so we are done
        replacement
    }

    // Release of forked key must deactivate the fork
    // (explicit modifier suppressing effect will be stopped only AFTER the release hid report is sent)
    fn try_finish_forks(&mut self, original_key_action: &KeyAction, event: KeyboardEvent) {
        if !event.pressed {
            for (i, fork) in (&self.keymap.borrow().behavior.fork.forks).into_iter().enumerate() {
                if self.fork_states[i].is_some() && fork.trigger == *original_key_action {
                    // if the originating key of a fork is released the replacement decision is not valid anymore
                    self.fork_states[i] = None;
                }
            }
        }
    }

    /// Trigger a combo that is delayed(if exists).
    ///
    /// A combo is delayed when it's **triggered** but it's a "subset" of another combo, like combo "asd" and combo "asdf".
    /// When "asd" is pressed, combo "asd" is delayed due to there's "asdf" combo. The delayed combo will be triggered when:
    /// - Timeout
    /// - Any of the key in the delayed combo is released
    ///
    /// When multiple combos are delayed, this function will only trigger the longest one, for example,
    /// combo "as", "sd" and "asd" are delayed, this function will only trigger "asd", and clear the combo state of "as"/"sd"
    ///
    /// If the full combo("asdf") is triggered, the delayed combo will be cleared without triggering it.
    async fn trigger_delayed_combo(&mut self, key_action: &KeyAction, event: KeyboardEvent) {
        // First, find the delayed combo and trigger it
        let next_action = self
            .keymap
            .borrow_mut()
            .behavior
            .combo
            .combos
            .iter_mut()
            .filter_map(|c| c.as_mut())
            .filter_map(|c| {
                if c.is_all_pressed() && !c.is_triggered() && c.config.actions.contains(key_action) {
                    // All keys are pressed but the combo is not triggered, trigger it
                    return Some((c.size(), c));
                }
                None
            }) // Find all delayed combos
            .max_by_key(|x| x.0) // Find only the longest one
            .map(|(_, c)| c.trigger()); // Trigger it

        // Clean the held buffer, process the combo output action and clear other combos
        if let Some(action) = next_action {
            self.held_buffer
                .keys
                .retain(|item| item.state != KeyState::WaitingCombo);
            let mut new_event = event;
            new_event.pressed = true;
            self.process_key_action(&action, new_event, true).await;
            debug!("[Combo] {:?} triggered", action);
            embassy_time::Timer::after_millis(20).await;
            // Reset other combos' state
            self.reset_combo(key_action);
        }
    }

    // Reset combos that contain a key_action but not triggered yet
    fn reset_combo(&mut self, key_action: &KeyAction) {
        // Reset other sub-combo states
        self.keymap
            .borrow_mut()
            .behavior
            .combo
            .combos
            .iter_mut()
            .filter_map(|c| c.as_mut())
            .for_each(|c| {
                if c.is_all_pressed() && !c.is_triggered() && c.config.actions.contains(key_action) {
                    info!("Resetting combo: {:?}", c,);
                    c.reset();
                }
            });
    }

    /// Check combo before process keys.
    ///
    /// This function returns key action after processing combo, and a boolean indicates that if current returned key action is a combo output
    async fn process_combo(&mut self, key_action: &KeyAction, event: KeyboardEvent) -> (Option<KeyAction>, bool) {
        let current_layer = self.keymap.borrow().get_activated_layer();

        // First, when releasing a key, check whether there's untriggered combo, if so, triggerer it first
        if !event.pressed {
            self.trigger_delayed_combo(key_action, event).await;
        }

        let max_size_of_updated_combo = self
            .keymap
            .borrow_mut()
            .behavior
            .combo
            .combos
            .iter_mut()
            .filter_map(|c| c.as_mut())
            .map(|c| {
                if c.update(key_action, event, current_layer) {
                    info!("Updated combo: {:?}", c);
                    c.size()
                } else {
                    0
                }
            })
            .max();
        if event.pressed
            && let Some(max_size) = max_size_of_updated_combo
            && max_size > 0
        {
            let pressed_time = self.get_timer_value(event).unwrap_or(Instant::now());
            self.held_buffer.push(HeldKey::new(
                event,
                *key_action,
                KeyState::WaitingCombo,
                pressed_time,
                pressed_time + self.keymap.borrow().behavior.combo.timeout,
            ));

            // Only one combo is updated, and triggered
            let next_action = self
                .keymap
                .borrow_mut()
                .behavior
                .combo
                .combos
                .iter_mut()
                .filter_map(|c| c.as_mut())
                .find_map(|c| {
                    if c.is_all_pressed() && !c.is_triggered() && c.size() == max_size {
                        Some(c.trigger())
                    } else {
                        None
                    }
                });

            if let Some(next_action) = next_action {
                debug!("[Combo] {:?} triggered", next_action);
                self.held_buffer
                    .keys
                    .retain(|item| item.state != KeyState::WaitingCombo);
                self.reset_combo(key_action);
                return (Some(next_action), true);
            }
            (None, false)
        } else {
            if !event.pressed {
                info!("Releasing keys in combo: {:?} {:?}", event, key_action);

                let mut combo_output = None;
                let mut releasing_triggered_combo = false;

                for combo in self
                    .keymap
                    .borrow_mut()
                    .behavior
                    .combo
                    .combos
                    .iter_mut()
                    .filter_map(|c| c.as_mut())
                {
                    if combo.config.actions.contains(key_action) {
                        // Releasing a combo key in triggered combo
                        releasing_triggered_combo |= combo.is_triggered();
                        info!("[Combo] releasing: {:?}", combo);

                        // Release the combo key, check whether the combo is fully released
                        if combo.update_released(key_action) {
                            // If the combo is fully released, update the combo output
                            debug!("[Combo] {:?} is released", combo.config.output);
                            combo_output = combo_output.or(Some(combo.config.output));
                        }
                    }
                }

                // Releasing a triggered combo
                // - Return the output of the triggered combo when the combo is fully released
                // - Return None when the combo is not fully released yet
                if releasing_triggered_combo {
                    return (combo_output, true);
                }
            }

            // When no key is updated(the combo is interruptted), or a key is released,
            self.dispatch_combos(key_action, event).await;
            (Some(*key_action), false)
        }
    }

    // Dispatch combo keys buffered in the held buffer when the combo isn't being triggered.
    async fn dispatch_combos(&mut self, key_action: &KeyAction, event: KeyboardEvent) {
        self.trigger_delayed_combo(key_action, event).await;
        // Dispatch all keys with state `WaitingCombo` in the held buffer
        let mut i = 0;
        while i < self.held_buffer.keys.len() {
            if self.held_buffer.keys[i].state == KeyState::WaitingCombo {
                let key = self.held_buffer.keys.swap_remove(i);
                debug!("[Combo] Dispatching combo: {:?}", key);
                self.process_key_action(&key.action, key.event, false).await;
            } else {
                i += 1;
            }
        }

        // Reset triggered combo states
        self.keymap
            .borrow_mut()
            .behavior
            .combo
            .combos
            .iter_mut()
            .filter_map(|combo| combo.as_mut())
            .filter(|combo| !combo.is_triggered())
            .for_each(Combo::reset);
    }

    async fn process_key_action_normal(&mut self, action: Action, event: KeyboardEvent) {
        match action {
            Action::No | Action::Transparent => {}
            Action::Key(key) => self.process_action_key(key, event).await,
            Action::LayerOn(layer_num) => self.process_action_layer_switch(layer_num, event),
            Action::LayerOff(layer_num) => {
                // Turn off a layer temporarily when the key is pressed
                // Reactivate the layer after the key is released
                if event.pressed {
                    self.keymap.borrow_mut().deactivate_layer(layer_num);
                }
            }
            Action::LayerToggle(layer_num) => {
                // Toggle a layer when the key is release
                if !event.pressed {
                    self.keymap.borrow_mut().toggle_layer(layer_num);
                }
            }
            Action::LayerToggleOnly(layer_num) => {
                // Activate a layer and deactivate all other layers(except default layer)
                if event.pressed {
                    // Disable all layers except the default layer
                    let default_layer = self.keymap.borrow().get_default_layer();
                    for i in 0..NUM_LAYER as u8 {
                        if i != default_layer {
                            self.keymap.borrow_mut().deactivate_layer(i);
                        }
                    }
                    // Activate the target layer
                    self.keymap.borrow_mut().activate_layer(layer_num);
                }
            }
            Action::DefaultLayer(layer_num) => {
                // Set the default layer
                self.keymap.borrow_mut().set_default_layer(layer_num);
            }
            Action::Modifier(modifiers) => {
                if event.pressed {
                    self.register_modifiers(modifiers);
                } else {
                    self.unregister_modifiers(modifiers);
                }
                //report the modifier press/release in its own hid report
                self.send_keyboard_report_with_resolved_modifiers(event.pressed).await;
                self.update_osl(event);
            }
            Action::TriggerMacro(macro_idx) => self.execute_macro(macro_idx, event).await,
            Action::KeyWithModifier(key_code, modifiers) => {
                if event.pressed {
                    // These modifiers will be combined into the hid report, so
                    // they will be "pressed" the same time as the key (in same hid report)
                    self.with_modifiers |= modifiers;
                } else {
                    // The modifiers will not be part of the hid report, so
                    // they will be "released" the same time as the key (in same hid report)
                    self.with_modifiers &= !(modifiers);
                }
                self.process_action_key(key_code, event).await
            }
            Action::LayerOnWithModifier(layer_num, modifiers) => {
                if event.pressed {
                    // These modifiers will be combined into the hid report, so
                    // they will be "pressed" the same time as the key (in same hid report)
                    self.held_modifiers |= modifiers;
                } else {
                    // The modifiers will not be part of the hid report, so
                    // they will be "released" the same time as the key (in same hid report)
                    self.held_modifiers &= !(modifiers);
                }
                self.process_action_layer_switch(layer_num, event);
                self.send_keyboard_report_with_resolved_modifiers(event.pressed).await
            }
            Action::OneShotLayer(l) => {
                self.process_action_osl(l, event).await;
                // Process OSM to avoid the OSL state stuck when an OSL is followed by an OSM
                self.update_osm(event);
            }
            Action::OneShotModifier(m) => {
                self.process_action_osm(m, event).await;
                // Process OSL to avoid the OSM state stuck when an OSM is followed by an OSL
                self.update_osl(event);
            }
            Action::OneShotKey(_k) => warn!("One-shot key is not supported: {:?}", action),
        }
    }

    /// Tap action, send a key when the key is pressed, then release the key.
    async fn process_key_action_tap(&mut self, action: Action, mut event: KeyboardEvent) {
        debug!("TAP action: {:?}, {:?}", action, event);

        if event.pressed {
            self.process_key_action_normal(action, event).await;

            // Wait 10ms, then send release
            Timer::after_millis(10).await;

            event.pressed = false;
            self.process_key_action_normal(action, event).await;
        }
    }

    pub fn print_buffer(&self) {
        self.held_buffer
            .keys
            .iter()
            .enumerate()
            .for_each(|(i, k)| info!("\n✅Held buffer {}: {:?}, state: {:?}", i, k.event, k.state));
    }

    async fn process_action_osm(&mut self, modifiers: ModifierCombination, event: KeyboardEvent) {
        // Update one shot state
        if event.pressed {
            // Add new modifier combination to existing one shot or init if none
            self.osm_state = match self.osm_state {
                OneShotState::None => OneShotState::Initial(modifiers),
                OneShotState::Initial(m) => OneShotState::Initial(m | modifiers),
                OneShotState::Single(m) => OneShotState::Single(m | modifiers),
                OneShotState::Held(m) => OneShotState::Held(m | modifiers),
            };

            self.update_osl(event);
        } else {
            match self.osm_state {
                OneShotState::Initial(m) | OneShotState::Single(m) => {
                    self.osm_state = OneShotState::Single(m);
                    let timeout = Timer::after(self.keymap.borrow().behavior.one_shot.timeout);
                    match select(timeout, KEY_EVENT_CHANNEL.receive()).await {
                        Either::First(_) => {
                            // Timeout, release modifiers
                            self.update_osl(event);
                            self.osm_state = OneShotState::None;
                        }
                        Either::Second(e) => {
                            // New event, send it to queue
                            if self.unprocessed_events.push(e).is_err() {
                                warn!("Unprocessed event queue is full, dropping event");
                            }
                        }
                    }
                }
                OneShotState::Held(_) => {
                    // Release modifier
                    self.update_osl(event);
                    self.osm_state = OneShotState::None;

                    // This sends a separate hid report with the
                    // currently registered modifiers except the
                    // one shoot modifiers -> this way "releasing" them.
                    self.send_keyboard_report_with_resolved_modifiers(event.pressed).await;
                }
                _ => (),
            };
        }
    }

    async fn process_action_osl(&mut self, layer_num: u8, event: KeyboardEvent) {
        // Update one shot state
        if event.pressed {
            // Deactivate old layer if any
            if let Some(&l) = self.osl_state.value() {
                self.keymap.borrow_mut().deactivate_layer(l);
            }

            // Update layer of one shot
            self.osl_state = match self.osl_state {
                OneShotState::None => OneShotState::Initial(layer_num),
                OneShotState::Initial(_) => OneShotState::Initial(layer_num),
                OneShotState::Single(_) => OneShotState::Single(layer_num),
                OneShotState::Held(_) => OneShotState::Held(layer_num),
            };

            // Activate new layer
            self.keymap.borrow_mut().activate_layer(layer_num);
        } else {
            match self.osl_state {
                OneShotState::Initial(l) | OneShotState::Single(l) => {
                    self.osl_state = OneShotState::Single(l);

                    let timeout = embassy_time::Timer::after(self.keymap.borrow().behavior.one_shot.timeout);
                    match select(timeout, KEY_EVENT_CHANNEL.receive()).await {
                        Either::First(_) => {
                            // Timeout, deactivate layer
                            self.keymap.borrow_mut().deactivate_layer(layer_num);
                            self.osl_state = OneShotState::None;
                        }
                        Either::Second(e) => {
                            // New event, send it to queue
                            if self.unprocessed_events.push(e).is_err() {
                                warn!("Unprocessed event queue is full, dropping event");
                            }
                        }
                    }
                }
                OneShotState::Held(layer_num) => {
                    self.osl_state = OneShotState::None;
                    self.keymap.borrow_mut().deactivate_layer(layer_num);
                }
                _ => (),
            };
        }
    }

    /// Calculates the combined effect of "explicit modifiers":
    /// - registered modifiers
    /// - one-shot modifiers
    pub fn resolve_explicit_modifiers(&self, pressed: bool) -> ModifierCombination {
        // if a one-shot modifier is active, decorate the hid report of keypress with those modifiers
        let mut result = self.held_modifiers;

        // OneShotState::Held keeps the temporary modifiers active until the key is released
        if pressed {
            if let Some(osm) = self.osm_state.value() {
                result |= *osm;
            }
        } else if let OneShotState::Held(osm) = self.osm_state {
            // One shot modifiers usually "released" together with the key release,
            // except when one-shoot is in "held mode" (to allow Alt+Tab like use cases)
            // In this later case Held -> None state change will report
            // the "modifier released" change in a separate hid report
            result |= osm;
        };

        result
    }

    /// Calculates the combined effect of all modifiers:
    /// - text macro related modifier suppressions + capitalization
    /// - registered (held) modifiers keys
    /// - one-shot modifiers
    /// - effect of Action::KeyWithModifiers (while they are pressed)
    /// - possible fork related modifier suppressions
    pub fn resolve_modifiers(&mut self, pressed: bool) -> ModifierCombination {
        // Text typing macro should not be affected by any modifiers,
        // only its own capitalization
        if self.macro_texting {
            if self.macro_caps {
                return ModifierCombination::new().with_left_shift(true);
            } else {
                return ModifierCombination::new();
            }
        }

        // "explicit" modifiers: one-shot modifier, registered held modifiers:
        let mut result = self.resolve_explicit_modifiers(pressed);

        // The triggered forks suppress the 'match_any' modifiers automatically
        // unless they are configured as the 'kept_modifiers'
        let mut fork_suppress = ModifierCombination::default();
        for fork_state in self.fork_states.iter().flatten() {
            fork_suppress |= fork_state.suppress;
        }

        // Some of these suppressions could have been canceled after the fork activation
        // by "explicit" modifier key presses - fork_keep_mask collects these:
        fork_suppress &= !self.fork_keep_mask;

        // Execute the remaining suppressions
        result &= !fork_suppress;

        // Apply the modifiers from Action::KeyWithModifiers
        // the suppression effect of forks should not apply on these
        result |= self.with_modifiers;

        // Apply Caps Word shift
        if self.caps_word.is_active() && pressed && self.caps_word.is_shift_current() {
            result |= ModifierCombination::new().with_left_shift(true);
        }

        result
    }

    // Process a basic keypress/release and also take care of applying one shot modifiers
    async fn process_basic(&mut self, key: KeyCode, event: KeyboardEvent) {
        if event.pressed {
            self.register_key(key, event);
        } else {
            self.unregister_key(key, event);
        }

        self.send_keyboard_report_with_resolved_modifiers(event.pressed).await;
    }

    // Process action key
    async fn process_action_key(&mut self, key: KeyCode, event: KeyboardEvent) {
        if matches!(key, KeyCode::TriLayerLower | KeyCode::TriLayerUpper) {
            // Check tri-layer first
            let layer = if key == KeyCode::TriLayerLower { 1 } else { 2 };
            self.process_action_layer_switch(layer, event);
            self.keymap.borrow_mut().update_fn_layer_state();
            return;
        }
        let key = match key {
            KeyCode::GraveEscape => {
                if self.held_modifiers.into_bits() == 0 {
                    KeyCode::Escape
                } else {
                    KeyCode::Grave
                }
            }
            KeyCode::CapsWordToggle => {
                // Handle Caps Word keycode
                if event.pressed {
                    self.caps_word.toggle();
                };
                return;
            }
            KeyCode::Again => {
                debug!("Repeat last key code: {:?} , {:?}", self.last_key_code, event);
                self.last_key_code
            }
            _ => key,
        };

        if event.pressed {
            // Record last press time
            if key.is_simple_key() {
                // Records only the simple key
                self.last_press_time = Instant::now();
            }
            // Check repeat key
            if key != KeyCode::Again {
                debug!(
                    "Last key code changed from {:?} to {:?}(pressed: {:?})",
                    self.last_key_code, key, event.pressed
                );
                self.last_key_code = key;
            }
            // Check Caps Word
            self.caps_word.check(key);
        }

        // Consumer, system and mouse keys should be processed before basic keycodes, since basic keycodes contain them all
        if key.is_consumer() {
            self.process_action_consumer_control(key, event).await;
        } else if key.is_system() {
            self.process_action_system_control(key, event).await;
        } else if key.is_mouse_key() {
            self.process_action_mouse(key, event).await;
        } else if key.is_basic() {
            self.process_basic(key, event).await;
        } else if key.is_user() {
            self.process_user(key, event).await;
        } else if key.is_macro() {
            // Process macro
            self.process_action_macro(key, event).await;
        } else if key.is_combo() {
            self.process_action_combo(key, event).await;
        } else if key.is_boot() {
            self.process_boot(key, event);
        } else {
            warn!("Unsupported key: {:?}", key);
        }
        self.update_osm(event);
        self.update_osl(event);
    }

    /// Process layer switch action.
    fn process_action_layer_switch(&mut self, layer_num: u8, event: KeyboardEvent) {
        // Change layer state only when the key's state is changed
        if event.pressed {
            self.keymap.borrow_mut().activate_layer(layer_num);
        } else {
            self.keymap.borrow_mut().deactivate_layer(layer_num);
        }
    }

    /// Process combo action.
    async fn process_action_combo(&mut self, key: KeyCode, event: KeyboardEvent) {
        if event.pressed {
            match key {
                KeyCode::ComboOn => self.combo_on = true,
                KeyCode::ComboOff => self.combo_on = false,
                KeyCode::ComboToggle => self.combo_on = !self.combo_on,
                _ => (),
            }
        }
    }

    /// Process consumer control action. Consumer control keys are keys in hid consumer page, such as media keys.
    async fn process_action_consumer_control(&mut self, key: KeyCode, event: KeyboardEvent) {
        if key.is_consumer() {
            self.media_report.usage_id = if event.pressed {
                key.as_consumer_control_usage_id() as u16
            } else {
                0
            };

            self.send_media_report().await;
        }
    }

    /// Process system control action. System control keys are keys in system page, such as power key.
    async fn process_action_system_control(&mut self, key: KeyCode, event: KeyboardEvent) {
        if key.is_system() {
            if event.pressed {
                if let Some(system_key) = key.as_system_control_usage_id() {
                    self.system_control_report.usage_id = system_key as u8;
                    self.send_system_control_report().await;
                }
            } else {
                self.system_control_report.usage_id = 0;
                self.send_system_control_report().await;
            }
        }
    }

    /// Process mouse key action with acceleration support.
    async fn process_action_mouse(&mut self, key: KeyCode, event: KeyboardEvent) {
        if key.is_mouse_key() {
            if event.pressed {
                match key {
                    KeyCode::MouseUp => {
                        // Reset repeat counter for direction change
                        if self.mouse_report.y > 0 {
                            self.mouse_repeat = 0;
                        }
                        let unit = self.calculate_mouse_move_unit();
                        self.mouse_report.y = -unit;
                    }
                    KeyCode::MouseDown => {
                        if self.mouse_report.y < 0 {
                            self.mouse_repeat = 0;
                        }
                        let unit = self.calculate_mouse_move_unit();
                        self.mouse_report.y = unit;
                    }
                    KeyCode::MouseLeft => {
                        if self.mouse_report.x > 0 {
                            self.mouse_repeat = 0;
                        }
                        let unit = self.calculate_mouse_move_unit();
                        self.mouse_report.x = -unit;
                    }
                    KeyCode::MouseRight => {
                        if self.mouse_report.x < 0 {
                            self.mouse_repeat = 0;
                        }
                        let unit = self.calculate_mouse_move_unit();
                        self.mouse_report.x = unit;
                    }
                    KeyCode::MouseWheelUp => {
                        if self.mouse_report.wheel < 0 {
                            self.mouse_wheel_repeat = 0;
                        }
                        let unit = self.calculate_mouse_wheel_unit();
                        self.mouse_report.wheel = unit;
                    }
                    KeyCode::MouseWheelDown => {
                        if self.mouse_report.wheel > 0 {
                            self.mouse_wheel_repeat = 0;
                        }
                        let unit = self.calculate_mouse_wheel_unit();
                        self.mouse_report.wheel = -unit;
                    }
                    KeyCode::MouseWheelLeft => {
                        if self.mouse_report.pan > 0 {
                            self.mouse_wheel_repeat = 0;
                        }
                        let unit = self.calculate_mouse_wheel_unit();
                        self.mouse_report.pan = -unit;
                    }
                    KeyCode::MouseWheelRight => {
                        if self.mouse_report.pan < 0 {
                            self.mouse_wheel_repeat = 0;
                        }
                        let unit = self.calculate_mouse_wheel_unit();
                        self.mouse_report.pan = unit;
                    }
                    KeyCode::MouseBtn1 => self.mouse_report.buttons |= 1 << 0,
                    KeyCode::MouseBtn2 => self.mouse_report.buttons |= 1 << 1,
                    KeyCode::MouseBtn3 => self.mouse_report.buttons |= 1 << 2,
                    KeyCode::MouseBtn4 => self.mouse_report.buttons |= 1 << 3,
                    KeyCode::MouseBtn5 => self.mouse_report.buttons |= 1 << 4,
                    KeyCode::MouseBtn6 => self.mouse_report.buttons |= 1 << 5,
                    KeyCode::MouseBtn7 => self.mouse_report.buttons |= 1 << 6,
                    KeyCode::MouseBtn8 => self.mouse_report.buttons |= 1 << 7,
                    KeyCode::MouseAccel0 => {
                        self.mouse_accel |= 1 << 0;
                    }
                    KeyCode::MouseAccel1 => {
                        self.mouse_accel |= 1 << 1;
                    }
                    KeyCode::MouseAccel2 => {
                        self.mouse_accel |= 1 << 2;
                    }
                    _ => {}
                }
            } else {
                match key {
                    KeyCode::MouseUp => {
                        if self.mouse_report.y < 0 {
                            self.mouse_report.y = 0;
                        }
                    }
                    KeyCode::MouseDown => {
                        if self.mouse_report.y > 0 {
                            self.mouse_report.y = 0;
                        }
                    }
                    KeyCode::MouseLeft => {
                        if self.mouse_report.x < 0 {
                            self.mouse_report.x = 0;
                        }
                    }
                    KeyCode::MouseRight => {
                        if self.mouse_report.x > 0 {
                            self.mouse_report.x = 0;
                        }
                    }
                    KeyCode::MouseWheelUp => {
                        if self.mouse_report.wheel > 0 {
                            self.mouse_report.wheel = 0;
                        }
                    }
                    KeyCode::MouseWheelDown => {
                        if self.mouse_report.wheel < 0 {
                            self.mouse_report.wheel = 0;
                        }
                    }
                    KeyCode::MouseWheelLeft => {
                        if self.mouse_report.pan < 0 {
                            self.mouse_report.pan = 0;
                        }
                    }
                    KeyCode::MouseWheelRight => {
                        if self.mouse_report.pan > 0 {
                            self.mouse_report.pan = 0;
                        }
                    }
                    KeyCode::MouseBtn1 => self.mouse_report.buttons &= !(1 << 0),
                    KeyCode::MouseBtn2 => self.mouse_report.buttons &= !(1 << 1),
                    KeyCode::MouseBtn3 => self.mouse_report.buttons &= !(1 << 2),
                    KeyCode::MouseBtn4 => self.mouse_report.buttons &= !(1 << 3),
                    KeyCode::MouseBtn5 => self.mouse_report.buttons &= !(1 << 4),
                    KeyCode::MouseBtn6 => self.mouse_report.buttons &= !(1 << 5),
                    KeyCode::MouseBtn7 => self.mouse_report.buttons &= !(1 << 6),
                    KeyCode::MouseBtn8 => self.mouse_report.buttons &= !(1 << 7),
                    KeyCode::MouseAccel0 => {
                        self.mouse_accel &= !(1 << 0);
                    }
                    KeyCode::MouseAccel1 => {
                        self.mouse_accel &= !(1 << 1);
                    }
                    KeyCode::MouseAccel2 => {
                        self.mouse_accel &= !(1 << 2);
                    }
                    _ => {}
                }

                // Reset repeat counters when movement stops
                if self.mouse_report.x == 0 && self.mouse_report.y == 0 {
                    self.mouse_repeat = 0;
                }
                if self.mouse_report.wheel == 0 && self.mouse_report.pan == 0 {
                    self.mouse_wheel_repeat = 0;
                }
            }

            // Apply diagonal compensation for movement
            if self.mouse_report.x != 0 && self.mouse_report.y != 0 {
                let (x, y) = self.apply_diagonal_compensation(self.mouse_report.x, self.mouse_report.y);
                self.mouse_report.x = x;
                self.mouse_report.y = y;
            }

            // Apply diagonal compensation for wheel
            if self.mouse_report.wheel != 0 && self.mouse_report.pan != 0 {
                let (wheel, pan) = self.apply_diagonal_compensation(self.mouse_report.wheel, self.mouse_report.pan);
                self.mouse_report.wheel = wheel;
                self.mouse_report.pan = pan;
            }

            if !matches!(key, KeyCode::MouseAccel0 | KeyCode::MouseAccel1 | KeyCode::MouseAccel2) {
                // Send mouse report only for movement and wheel keys
                self.send_mouse_report().await;
            }

            // Continue processing ONLY for movement and wheel keys
            if event.pressed {
                let is_movement_key = matches!(
                    key,
                    KeyCode::MouseUp | KeyCode::MouseDown | KeyCode::MouseLeft | KeyCode::MouseRight
                );
                let is_wheel_key = matches!(
                    key,
                    KeyCode::MouseWheelUp
                        | KeyCode::MouseWheelDown
                        | KeyCode::MouseWheelLeft
                        | KeyCode::MouseWheelRight
                );

                // Only continue processing for movement and wheel keys
                if is_movement_key || is_wheel_key {
                    // Determine the delay for the next repeat using convenience methods
                    let delay = {
                        let config = &self.keymap.borrow().behavior.mouse_key;
                        if is_movement_key {
                            config.get_movement_delay(self.mouse_repeat)
                        } else {
                            config.get_wheel_delay(self.mouse_wheel_repeat)
                        }
                    };

                    // Increment the appropriate repeat counter
                    if is_movement_key && self.mouse_repeat < u8::MAX {
                        self.mouse_repeat += 1;
                    }
                    if is_wheel_key && self.mouse_wheel_repeat < u8::MAX {
                        self.mouse_wheel_repeat += 1;
                    }

                    // Schedule next movement after the delay
                    embassy_time::Timer::after_millis(delay as u64).await;
                    // Check if there's a release event in the channel, if there's no release event, re-send the event
                    let len = KEY_EVENT_CHANNEL.len();
                    let mut released = false;
                    for _ in 0..len {
                        let queued_event = KEY_EVENT_CHANNEL.receive().await;
                        if queued_event.pos != event.pos || !queued_event.pressed {
                            KEY_EVENT_CHANNEL.send(queued_event).await;
                        }
                        // If there's a release event in the channel
                        if queued_event.pos == event.pos && !queued_event.pressed {
                            released = true;
                        }
                    }
                    if !released {
                        KEY_EVENT_CHANNEL.send(event).await;
                    }
                }
            }
        }
    }

    async fn process_user(&mut self, key: KeyCode, event: KeyboardEvent) {
        debug!("Processing user key: {:?}, event: {:?}", key, event);
        #[cfg(feature = "_ble")]
        {
            use crate::NUM_BLE_PROFILE;
            use crate::ble::profile::BleProfileAction;
            use crate::channel::BLE_PROFILE_CHANNEL;
            // Get user key id
            let id = (key as u16 - KeyCode::User0 as u16) as u8;
            if event.pressed {
                // Clear Peer is processed when pressed
                if id == NUM_BLE_PROFILE as u8 + 4 {
                    #[cfg(all(feature = "split", feature = "_ble"))]
                    if event.pressed {
                        // Wait for 5s, if the key is still pressed, clear split peer info
                        // If there's any other key event received during this period, skip
                        match select(embassy_time::Timer::after_millis(5000), KEY_EVENT_CHANNEL.receive()).await {
                            Either::First(_) => {
                                // Timeout reached, send clear peer message
                                #[cfg(feature = "controller")]
                                send_controller_event(&mut self.controller_pub, ControllerEvent::ClearPeer);
                                info!("Clear peer");
                            }
                            Either::Second(e) => {
                                // Received a new key event before timeout, add to unprocessed list
                                if self.unprocessed_events.push(e).is_err() {
                                    warn!("Unprocessed event queue is full, dropping event");
                                }
                            }
                        }
                    }
                }
            } else {
                // Other user keys are processed when released
                if id < NUM_BLE_PROFILE as u8 {
                    info!("Switch to profile: {}", id);
                    // User0~7: Swtich to the specific profile
                    BLE_PROFILE_CHANNEL.send(BleProfileAction::SwitchProfile(id)).await;
                } else if id == NUM_BLE_PROFILE as u8 {
                    // User8: Next profile
                    BLE_PROFILE_CHANNEL.send(BleProfileAction::NextProfile).await;
                } else if id == NUM_BLE_PROFILE as u8 + 1 {
                    // User9: Previous profile
                    BLE_PROFILE_CHANNEL.send(BleProfileAction::PreviousProfile).await;
                } else if id == NUM_BLE_PROFILE as u8 + 2 {
                    // User10: Clear profile
                    BLE_PROFILE_CHANNEL.send(BleProfileAction::ClearProfile).await;
                } else if id == NUM_BLE_PROFILE as u8 + 3 {
                    // User11:
                    BLE_PROFILE_CHANNEL.send(BleProfileAction::ToggleConnection).await;
                }
            }
        }
    }

    fn process_boot(&mut self, key: KeyCode, event: KeyboardEvent) {
        // When releasing the key, process the boot action
        if !event.pressed {
            match key {
                KeyCode::Bootloader => {
                    boot::jump_to_bootloader();
                }
                KeyCode::Reboot => {
                    boot::reboot_keyboard();
                }
                _ => (), // unreachable, do nothing
            };
        }
    }

    async fn process_action_macro(&mut self, key: KeyCode, event: KeyboardEvent) {
        // Get macro index
        if let Some(macro_idx) = key.as_macro_index() {
            self.execute_macro(macro_idx, event).await;
        }
    }

    async fn execute_macro(&mut self, macro_idx: u8, event: KeyboardEvent) {
        // Execute the macro only when releasing the key
        if event.pressed {
            return;
        }

        // Read macro operations until the end of the macro
        let macro_idx = self.keymap.borrow().get_macro_sequence_start(macro_idx);
        if let Some(macro_start_idx) = macro_idx {
            let mut offset = 0;
            loop {
                // First, get the next macro operation
                let (operation, new_offset) = self.keymap.borrow().get_next_macro_operation(macro_start_idx, offset);
                // Execute the operation
                match operation {
                    MacroOperation::Press(k) => {
                        self.macro_texting = false;
                        self.register_key(k, event);
                        self.send_keyboard_report_with_resolved_modifiers(true).await;
                    }
                    MacroOperation::Release(k) => {
                        self.macro_texting = false;
                        self.unregister_key(k, event);
                        self.send_keyboard_report_with_resolved_modifiers(false).await;
                    }
                    MacroOperation::Tap(k) => {
                        self.macro_texting = false;
                        self.register_key(k, event);
                        self.send_keyboard_report_with_resolved_modifiers(true).await;
                        embassy_time::Timer::after_millis(2).await;
                        self.unregister_key(k, event);
                        self.send_keyboard_report_with_resolved_modifiers(false).await;
                    }
                    MacroOperation::Text(k, is_cap) => {
                        self.macro_texting = true;
                        self.macro_caps = is_cap;
                        if is_cap {
                            self.send_keyboard_report_with_resolved_modifiers(true).await;
                            embassy_time::Timer::after_millis(12).await;
                        }
                        self.register_keycode(k, event);
                        self.send_keyboard_report_with_resolved_modifiers(true).await;
                        embassy_time::Timer::after_millis(12).await;
                        self.unregister_keycode(k, event);
                        self.send_keyboard_report_with_resolved_modifiers(false).await;
                        if is_cap {
                            self.macro_caps = false;
                            embassy_time::Timer::after_millis(12).await;
                            self.send_keyboard_report_with_resolved_modifiers(false).await;
                        }
                    }
                    MacroOperation::Delay(t) => {
                        embassy_time::Timer::after_millis(t as u64).await;
                    }
                    MacroOperation::End => {
                        if self.macro_texting {
                            //restore the state of the keyboard (held modifiers, etc.) after text typing
                            self.send_keyboard_report_with_resolved_modifiers(false).await;
                            self.macro_texting = false;
                        }
                        break;
                    }
                };

                offset = new_offset;
                if offset > self.keymap.borrow().behavior.keyboard_macros.macro_sequences.len() {
                    break;
                }
                embassy_time::Timer::after_millis(1).await;
            }
        } else {
            error!("Macro not found");
        }
    }

    pub(crate) async fn send_keyboard_report_with_resolved_modifiers(&mut self, pressed: bool) {
        // all modifier related effects are combined here to be sent with the hid report:
        let modifiers = self.resolve_modifiers(pressed);
        info!("Sending keyboard report, pressed: {}", pressed);
        self.send_report(Report::KeyboardReport(KeyboardReport {
            modifier: modifiers.into_bits(),
            reserved: 0,
            leds: LOCK_LED_STATES.load(core::sync::atomic::Ordering::Relaxed),
            keycodes: self.held_keycodes.map(|k| k as u8),
        }))
        .await;

        // Yield once after sending the report to channel
        yield_now().await;
    }

    /// Send system control report if needed
    pub(crate) async fn send_system_control_report(&mut self) {
        self.send_report(Report::SystemControlReport(self.system_control_report))
            .await;
        self.system_control_report.usage_id = 0;
        yield_now().await;
    }

    /// Send media report if needed
    pub(crate) async fn send_media_report(&mut self) {
        self.send_report(Report::MediaKeyboardReport(self.media_report)).await;
        self.media_report.usage_id = 0;
        yield_now().await;
    }

    /// Send mouse report if needed
    pub(crate) async fn send_mouse_report(&mut self) {
        // Prevent mouse report flooding, set maximum mouse report rate to 50 HZ
        self.send_report(Report::MouseReport(self.mouse_report)).await;
        yield_now().await;
    }

    fn update_osm(&mut self, event: KeyboardEvent) {
        match self.osm_state {
            OneShotState::Initial(m) => self.osm_state = OneShotState::Held(m),
            OneShotState::Single(_) => {
                if !event.pressed {
                    self.osm_state = OneShotState::None;
                }
            }
            _ => (),
        }
    }

    fn update_osl(&mut self, event: KeyboardEvent) {
        match self.osl_state {
            OneShotState::Initial(l) => self.osl_state = OneShotState::Held(l),
            OneShotState::Single(layer_num) => {
                if !event.pressed {
                    self.keymap.borrow_mut().deactivate_layer(layer_num);
                    self.osl_state = OneShotState::None;
                }
            }
            _ => (),
        }
    }

    /// Register a key, the key can be a basic keycode or a modifier.
    fn register_key(&mut self, key: KeyCode, event: KeyboardEvent) {
        if key.is_modifier() {
            self.register_modifier_key(key);
        } else if key.is_basic() {
            self.register_keycode(key, event);
        }
    }

    /// Unregister a key, the key can be a basic keycode or a modifier.
    fn unregister_key(&mut self, key: KeyCode, event: KeyboardEvent) {
        if key.is_modifier() {
            self.unregister_modifier_key(key);
        } else if key.is_basic() {
            self.unregister_keycode(key, event);
        }
    }

    /// Set the timer value for a key event
    fn set_timer_value(&mut self, event: KeyboardEvent, value: Option<Instant>) {
        match event.pos {
            KeyboardEventPos::Key(pos) => {
                self.timer[pos.col as usize][pos.row as usize] = value;
            }
            KeyboardEventPos::RotaryEncoder(encoder_pos) => {
                // Check if the rotary encoder id is valid
                if let Some(encoder) = self.rotary_encoder_timer.get_mut(encoder_pos.id as usize)
                    && encoder_pos.direction != Direction::None
                {
                    encoder[encoder_pos.direction as usize] = value;
                }
            }
        }
    }

    /// Get the timer value for a key event, if the key event is not in the timer, return the current time
    fn get_timer_value(&self, event: KeyboardEvent) -> Option<Instant> {
        match event.pos {
            KeyboardEventPos::Key(pos) => self.timer[pos.col as usize][pos.row as usize],
            KeyboardEventPos::RotaryEncoder(encoder_pos) => {
                // Check if the rotary encoder id is valid
                if let Some(encoder) = self.rotary_encoder_timer.get(encoder_pos.id as usize)
                    && encoder_pos.direction != Direction::None
                {
                    return encoder[encoder_pos.direction as usize];
                }
                None
            }
        }
    }

    /// Register a key to be sent in hid report.
    fn register_keycode(&mut self, key: KeyCode, event: KeyboardEvent) {
        // First, find the key event slot according to the position
        let slot = self.registered_keys.iter().enumerate().find_map(|(i, k)| {
            if let Some(e) = k
                && event.pos == e.pos
            {
                return Some(i);
            }
            None
        });

        // If the slot is found, update the key in the slot
        if let Some(index) = slot {
            self.held_keycodes[index] = key;
            self.registered_keys[index] = Some(event);
        } else {
            // Otherwise, find the first free slot
            if let Some(index) = self.held_keycodes.iter().position(|&k| k == KeyCode::No) {
                self.held_keycodes[index] = key;
                self.registered_keys[index] = Some(event);
            }
        }
    }

    /// Unregister a key from hid report.
    fn unregister_keycode(&mut self, key: KeyCode, event: KeyboardEvent) {
        // First, find the key event slot according to the position
        let slot = self.registered_keys.iter().enumerate().find_map(|(i, k)| {
            if let Some(e) = k
                && event.pos == e.pos
            {
                return Some(i);
            }
            None
        });

        // If the slot is found, update the key in the slot
        if let Some(index) = slot {
            self.held_keycodes[index] = KeyCode::No;
            self.registered_keys[index] = None;
        } else {
            // Otherwise, release the first same key
            if let Some(index) = self.held_keycodes.iter().position(|&k| k == key) {
                self.held_keycodes[index] = KeyCode::No;
                self.registered_keys[index] = None;
            }
        }
    }

    /// Register a modifier to be sent in hid report.
    fn register_modifier_key(&mut self, key: KeyCode) {
        self.held_modifiers |= key.to_hid_modifiers();

        #[cfg(feature = "controller")]
        send_controller_event(&mut self.controller_pub, ControllerEvent::Modifier(self.held_modifiers));

        // if a modifier key arrives after fork activation, it should be kept
        self.fork_keep_mask |= key.to_hid_modifiers();
    }

    /// Unregister a modifier from hid report.
    fn unregister_modifier_key(&mut self, key: KeyCode) {
        self.held_modifiers &= !key.to_hid_modifiers();

        #[cfg(feature = "controller")]
        send_controller_event(&mut self.controller_pub, ControllerEvent::Modifier(self.held_modifiers));
    }

    /// Register a modifier combination to be sent in hid report.
    fn register_modifiers(&mut self, modifiers: ModifierCombination) {
        self.held_modifiers |= modifiers;

        #[cfg(feature = "controller")]
        send_controller_event(&mut self.controller_pub, ControllerEvent::Modifier(self.held_modifiers));

        // if a modifier key arrives after fork activation, it should be kept
        self.fork_keep_mask |= modifiers;
    }

    /// Unregister a modifier combination from hid report.
    fn unregister_modifiers(&mut self, modifiers: ModifierCombination) {
        self.held_modifiers &= !modifiers;

        #[cfg(feature = "controller")]
        send_controller_event(&mut self.controller_pub, ControllerEvent::Modifier(self.held_modifiers));
    }

    /// Calculate mouse movement distance based on current repeat count and acceleration settings
    fn calculate_mouse_move_unit(&self) -> i8 {
        let config = &self.keymap.borrow().behavior.mouse_key;

        let unit = if self.mouse_accel & (1 << 2) != 0 {
            20
        } else if self.mouse_accel & (1 << 1) != 0 {
            12
        } else if self.mouse_accel & (1 << 0) != 0 {
            4
        } else if self.mouse_repeat == 0 {
            config.move_delta as u16
        } else if self.mouse_repeat >= config.time_to_max {
            (config.move_delta as u16).saturating_mul(config.max_speed as u16)
        } else {
            // Natural acceleration with smooth unit progression.
            // Calculate smooth progress using asymptotic curve: f(x) = 2x - x².
            // Where x = repeat_count / time_to_max, giving smooth progression from 0 to 1
            let repeat_count = self.mouse_repeat as u16;
            let time_to_max = config.time_to_max as u16;
            let min_unit = config.move_delta as u16;
            let max_unit = (config.move_delta as u16).saturating_mul(config.max_speed as u16);
            let unit_range = max_unit - min_unit;

            // Use saturating operations to handle overflow cases
            let linear_term = 2u16.saturating_mul(repeat_count).saturating_mul(time_to_max);
            let quadratic_term = repeat_count.saturating_mul(repeat_count);
            let progress_numerator = linear_term.saturating_sub(quadratic_term);
            let progress_denominator = time_to_max.saturating_mul(time_to_max);
            min_unit + (unit_range.saturating_mul(progress_numerator) / progress_denominator.max(1))
        };

        let final_unit = if unit > config.move_max as u16 {
            config.move_max as u16
        } else if unit == 0 {
            1
        } else {
            unit
        };

        final_unit.min(i8::MAX as u16) as i8
    }

    /// Calculate mouse wheel movement distance based on current repeat count and acceleration settings
    fn calculate_mouse_wheel_unit(&self) -> i8 {
        let config = &self.keymap.borrow().behavior.mouse_key;

        let unit = if self.mouse_accel & (1 << 2) != 0 {
            4
        } else if self.mouse_accel & (1 << 1) != 0 {
            2
        } else if self.mouse_accel & (1 << 0) != 0 {
            1
        } else if self.mouse_wheel_repeat == 0 {
            config.wheel_delta as u16
        } else if self.mouse_wheel_repeat >= config.wheel_time_to_max {
            (config.wheel_delta as u16).saturating_mul(config.wheel_max_speed_multiplier as u16)
        } else {
            // Natural acceleration with smooth unit progression.
            let repeat_count = self.mouse_wheel_repeat as u16;
            let time_to_max = config.wheel_time_to_max as u16;
            let min_unit = config.wheel_delta as u16;
            let max_unit = (config.wheel_delta as u16).saturating_mul(config.wheel_max_speed_multiplier as u16);
            let unit_range = max_unit - min_unit;

            // Calculate smooth progress using asymptotic curve: f(x) = 2x - x².
            // Use saturating operations to handle overflow cases.
            let linear_term = 2u16.saturating_mul(repeat_count).saturating_mul(time_to_max);
            let quadratic_term = repeat_count.saturating_mul(repeat_count);
            let progress_numerator = linear_term.saturating_sub(quadratic_term);
            let progress_denominator = time_to_max.saturating_mul(time_to_max);

            min_unit + (unit_range.saturating_mul(progress_numerator) / progress_denominator.max(1))
        };

        let final_unit = if unit > config.wheel_max as u16 {
            config.wheel_max as u16
        } else if unit == 0 {
            1
        } else {
            unit
        };

        final_unit.min(i8::MAX as u16) as i8
    }

    /// Apply diagonal movement compensation (approximation of 1/sqrt(2))
    fn apply_diagonal_compensation(&self, mut x: i8, mut y: i8) -> (i8, i8) {
        if x != 0 && y != 0 {
            // Apply 1/sqrt(2) approximation using 181/256 (0.70703125)
            let x_compensated = (x as i16 * 181 + 128) / 256;
            let y_compensated = (y as i16 * 181 + 128) / 256;

            x = if x_compensated == 0 && x != 0 {
                if x > 0 { 1 } else { -1 }
            } else {
                x_compensated as i8
            };

            y = if y_compensated == 0 && y != 0 {
                if y > 0 { 1 } else { -1 }
            } else {
                y_compensated as i8
            };
        }
        (x, y)
    }
}

#[derive(Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum KeyBehaviorDecision {
    // Clean holding buffer due to permissive hold is triggered
    CleanBuffer,
    // Skip key action processing and buffer key event
    Buffer,
    // Continue processing as normal key event
    Ignore,
    // Release current key
    Release,
    // Flow tap of current key is triggered
    FlowTap,
}

#[derive(Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum HeldKeyDecision {
    // Ignore it
    Ignore,
    // Unilateral tap triggered
    UnilateralTap,
    // Flow tap triggered, all held morse keys should be triggered as tapping
    FlowTap,
    // Permissive hold triggered
    PermissiveHold,
    // Hold on other key press triggered
    HoldOnOtherKeyPress,
    // Used for the buffered key which is releasing now
    Release,
    // Releasing a key that is pressed before any keys in the buffer
    NotInBuffer,
    // The held key is a normal key,
    // It will always be added to the decision list, and the decision will be made later
    Normal,
}

#[cfg(test)]
mod test {

    use embassy_futures::block_on;
    use embassy_time::{Duration, Timer};
    use rmk_types::action::{KeyAction, MorseMode, MorseProfile};
    use rmk_types::modifier::ModifierCombination;
    use rusty_fork::rusty_fork_test;

    use super::*;
    use crate::combo::{Combo, ComboConfig};
    use crate::config::{BehaviorConfig, CombosConfig, ForksConfig, PositionalConfig};
    use crate::event::{KeyPos, KeyboardEvent, KeyboardEventPos};
    use crate::fork::Fork;
    use crate::{a, k, layer, mo, th, thp};

    // Init logger for tests
    #[ctor::ctor]
    fn init_log() {
        let _ = env_logger::builder()
            .filter_level(log::LevelFilter::Debug)
            .is_test(true)
            .try_init();
    }

    #[rustfmt::skip]
    pub const fn get_keymap() -> [[[KeyAction; 14]; 5]; 2] {
        [
            layer!([
                [k!(Grave), k!(Kc1), k!(Kc2), k!(Kc3), k!(Kc4), k!(Kc5), k!(Kc6), k!(Kc7), k!(Kc8), k!(Kc9), k!(Kc0), k!(Minus), k!(Equal), k!(Backspace)],
                [k!(Tab), k!(Q), k!(W), k!(E), k!(R), k!(T), k!(Y), k!(U), k!(I), k!(O), k!(P), k!(LeftBracket), k!(RightBracket), k!(Backslash)],
                [k!(Escape), thp!(A, LShift, MorseProfile::new(Some(true), Some(MorseMode::PermissiveHold),None,None)), th!(S, LGui), k!(D), k!(F), k!(G), k!(H), k!(J), k!(K), k!(L), k!(Semicolon), k!(Quote), a!(No), k!(Enter)],
                [k!(LShift), k!(Z), k!(X), k!(C), k!(V), k!(B), k!(N), k!(M), k!(Comma), k!(Dot), k!(Slash), a!(No), a!(No), k!(RShift)],
                [k!(LCtrl), k!(LGui), k!(LAlt), a!(No), a!(No), k!(Space), a!(No), a!(No), a!(No), mo!(1), k!(RAlt), a!(No), k!(RGui), k!(RCtrl)]
            ]),
            layer!([
                [k!(Grave), k!(F1), k!(F2), k!(F3), k!(F4), k!(F5), k!(F6), k!(F7), k!(F8), k!(F9), k!(F10), k!(F11), k!(F12), k!(Delete)],
                [a!(No), a!(Transparent), a!(No), a!(No), a!(No), a!(No), a!(No), a!(No), a!(No), a!(No), a!(No), a!(No), a!(No), a!(No)],
                [k!(CapsLock), a!(No), a!(No), a!(No), a!(No), a!(No), a!(No), a!(No), a!(No), a!(No), a!(No), a!(No), a!(No), a!(No)],
                [a!(No), a!(No), a!(No), a!(No), a!(No), a!(No), a!(No), a!(No), a!(No), a!(No), a!(No), a!(No), a!(No), k!(Up)],
                [a!(No), a!(No), a!(No), a!(No), a!(No), a!(No), a!(No), a!(No), a!(No), a!(No), k!(Left), a!(No), k!(Down), k!(Right)]
            ]),
        ]
    }

    #[rustfmt::skip]
    fn get_combos_config() -> CombosConfig {
        // Define the function to return the appropriate combo configuration
        CombosConfig {
            combos: [
                Some(Combo::new(ComboConfig {
                    actions: [
                        k!(V), //3,4
                        k!(B), //3,5
                        k!(No), k!(No),
                    ],
                    output: k!(LShift),
                    layer: Some(0),
                })),
                Some(Combo::new(ComboConfig {
                    actions: [
                        k!(R), //1,4
                        k!(T), //1,5
                        k!(No), k!(No),
                    ],
                    output: k!(LAlt),
                    layer: Some(0),
                })),
                None, None, None, None, None, None
            ],
            timeout: Duration::from_millis(100),
        }
    }

    fn create_test_keyboard_with_config(config: BehaviorConfig) -> Keyboard<'static, 5, 14, 2> {
        static BEHAVIOR_CONFIG: static_cell::StaticCell<BehaviorConfig> = static_cell::StaticCell::new();
        let behavior_config = BEHAVIOR_CONFIG.init(config);

        // Box::leak is acceptable in tests
        let keymap = Box::new(get_keymap());
        let leaked_keymap = Box::leak(keymap);

        static KEY_CONFIG: static_cell::StaticCell<PositionalConfig<5, 14>> = static_cell::StaticCell::new();
        let per_key_config = KEY_CONFIG.init(PositionalConfig::default());
        let keymap = block_on(KeyMap::new(leaked_keymap, None, behavior_config, per_key_config));
        let keymap_cell = RefCell::new(keymap);
        let keymap_ref = Box::leak(Box::new(keymap_cell));

        Keyboard::new(keymap_ref)
    }

    fn create_test_keyboard() -> Keyboard<'static, 5, 14, 2> {
        create_test_keyboard_with_config(BehaviorConfig::default())
    }

    async fn force_timeout_first_hold(keyboard: &mut Keyboard<'static, 5, 14, 2>) {
        let key = keyboard.next_buffered_key().unwrap();
        keyboard.process_buffered_key(key).await;
    }

    fn create_test_keyboard_with_forks(fork1: Fork, fork2: Fork) -> Keyboard<'static, 5, 14, 2> {
        let mut cfg = ForksConfig::default();
        let _ = cfg.forks.push(fork1);
        let _ = cfg.forks.push(fork2);
        create_test_keyboard_with_config(BehaviorConfig {
            fork: cfg,
            ..BehaviorConfig::default()
        })
    }

    fn event(row: u8, col: u8, pressed: bool) -> KeyboardEvent {
        KeyboardEvent::key(row, col, pressed)
    }

    rusty_fork_test! {
        #[test]
        fn test_register_key() {
            let main = async {
                let mut keyboard = create_test_keyboard();
                keyboard.register_key(KeyCode::A, KeyboardEvent::key(2, 1, true));
                assert_eq!(keyboard.held_keycodes[0], KeyCode::A);
            };
            block_on(main);
        }

        #[test]
        fn test_basic_key_press_release() {
            let main = async {
                let mut keyboard = create_test_keyboard();

                // Press A key
                keyboard.process_inner(KeyboardEvent::key(0, 0, true)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::Grave); // A key's HID code is 0x04

                // Release A key
                keyboard.process_inner(KeyboardEvent::key(0, 0, false)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::No);
            };
            block_on(main);
        }

        #[test]
        fn test_modifier_key() {
            let main = async {
                let mut keyboard = create_test_keyboard();

                // Press Shift key
                keyboard.register_key(KeyCode::LShift, KeyboardEvent::key(3, 0, true));
                assert_eq!(keyboard.held_modifiers, ModifierCombination::new().with_left_shift(true)); // Left Shift's modifier bit is 0x02

                // Release Shift key
                keyboard.unregister_key(KeyCode::LShift, KeyboardEvent::key(3, 0, false));
                assert_eq!(keyboard.held_modifiers, ModifierCombination::new());
            };
            block_on(main);
        }

        #[test]
        fn test_multiple_keys() {
            let main = async {
                let mut keyboard = create_test_keyboard();

                keyboard.process_inner(KeyboardEvent::key(0, 0, true)).await;
                assert!(keyboard.held_keycodes.contains(&KeyCode::Grave));

                keyboard.process_inner(KeyboardEvent::key(1, 0, true)).await;
                assert!(keyboard.held_keycodes.contains(&KeyCode::Grave) && keyboard.held_keycodes.contains(&KeyCode::Tab));

                keyboard.process_inner(KeyboardEvent::key(1, 0, false)).await;
                assert!(keyboard.held_keycodes.contains(&KeyCode::Grave) && !keyboard.held_keycodes.contains(&KeyCode::Tab));

                keyboard.process_inner(KeyboardEvent::key(0, 0, false)).await;
                assert!(!keyboard.held_keycodes.contains(&KeyCode::Grave));
                assert!(keyboard.held_keycodes.iter().all(|&k| k == KeyCode::No));
            };

            block_on(main);
        }

        #[test]
        fn test_repeat_key_single() {
            let main = async {
                let mut keyboard = create_test_keyboard();
                keyboard.keymap.borrow_mut().set_action_at(
                    KeyboardEventPos::Key(KeyPos { row: 0, col: 0 }),
                    0,
                    KeyAction::Single(Action::Key(KeyCode::Again)),
                );

                // first press ever of the Again issues KeyCode:No
                keyboard.process_inner(KeyboardEvent::key(0, 0, true)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::No); // A key's HID code is 0x04

                // Press A key
                keyboard.process_inner(KeyboardEvent::key(2, 0, true)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::Escape); // A key's HID code is 0x04

                // Release A key
                keyboard.process_inner(KeyboardEvent::key(2, 0, false)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::No);

                // after another key is pressed, that key is repeated
                keyboard.process_inner(KeyboardEvent::key(0, 0, true)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::Escape); // A key's HID code is 0x04

                // releasing the repeat key
                keyboard.process_inner(KeyboardEvent::key(0, 0, false)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::No); // A key's HID code is 0x04

                // Press S key
                keyboard.process_inner(KeyboardEvent::key(1, 2, true)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::W); // A key's HID code is 0x04

                // after another key is pressed, that key is repeated
                keyboard.process_inner(KeyboardEvent::key(0, 0, true)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::W); // A key's HID code is 0x04
            };
            block_on(main);
        }


        #[test]
        fn test_repeat_key_th() {
            let main = async {
                let mut keyboard = create_test_keyboard();
                keyboard.keymap.borrow_mut().set_action_at(
                    KeyboardEventPos::Key(KeyPos { row: 0, col: 0 }),
                    0,
                    KeyAction::TapHold(Action::Key(KeyCode::F), Action::Key(KeyCode::Again), Default::default()),
                );
                keyboard.keymap.borrow_mut().set_action_at(
                    KeyboardEventPos::Key(KeyPos { row: 2, col: 1 }),
                    0,
                    KeyAction::Single(Action::Key(KeyCode::A)),
                );
                keyboard.keymap.borrow_mut().set_action_at(
                    KeyboardEventPos::Key(KeyPos { row: 2, col: 2 }),
                    0,
                    KeyAction::Single(Action::Key(KeyCode::S)),
                );

                // Press down F
                // first press ever of the Again issues KeyCode:No
                keyboard.process_inner(KeyboardEvent::key(0, 0, true)).await;
                keyboard
                    .send_keyboard_report_with_resolved_modifiers(true)
                    .await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::No);
                // Release F
                keyboard.process_inner(KeyboardEvent::key(0, 0, false)).await;

                // Press A key
                keyboard.process_inner(KeyboardEvent::key(2, 1, true)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::A);

                // Release A key
                keyboard.process_inner(KeyboardEvent::key(2, 1, false)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::No);

                // Release F
                keyboard.process_inner(KeyboardEvent::key(0, 0, false)).await;

                // Here release event should make again into hold

                embassy_time::Timer::after_millis(200 as u64).await;
                // after another key is pressed, that key is repeated
                keyboard.process_inner(KeyboardEvent::key(0, 0, true)).await;
                force_timeout_first_hold(&mut keyboard).await;

                assert_eq!(keyboard.held_keycodes[0], KeyCode::A);

                // releasing the repeat key
                keyboard.process_inner(KeyboardEvent::key(0, 0, false)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::No);

                // Press S key
                keyboard.process_inner(KeyboardEvent::key(2, 2, true)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::S);

                // after another key is pressed, that key is repeated
                keyboard.process_inner(KeyboardEvent::key(0, 0, true)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::S);
            };
            block_on(main);
        }

        #[test]
        fn test_key_action_transparent() {
            let main = async {
                let mut keyboard = create_test_keyboard();

                // Activate layer 1
                keyboard.process_action_layer_switch(1, KeyboardEvent::key(0, 0, true));

                // Press Transparent key (Q on lower layer)
                keyboard.process_inner(KeyboardEvent::key(1, 1, true)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::Q); // Q key's HID code is 0x14

                // Release Transparent key
                keyboard.process_inner(KeyboardEvent::key(1, 1, false)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::No);
            };
            block_on(main);
        }

        #[test]
        fn test_key_action_no() {
            let main = async {
                let mut keyboard = create_test_keyboard();

                // Press No key
                keyboard.process_inner(KeyboardEvent::key(4, 3, true)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::No);

                // Release No key
                keyboard.process_inner(KeyboardEvent::key(4, 3, false)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::No);
            };
            block_on(main);
        }


        #[test]
        fn test_fork_with_held_modifier() {
            let main = async {
                //{ trigger = "Dot", negative_output = "Dot", positive_output = "WM(Semicolon, LShift)", match_any = "LShift|RShift" },
                let fork1 = Fork {
                    trigger: KeyAction::Single(Action::Key(KeyCode::Dot)),
                    negative_output: KeyAction::Single(Action::Key(KeyCode::Dot)),
                    positive_output: KeyAction::Single(
                        Action::KeyWithModifier(KeyCode::Semicolon,
                        ModifierCombination::default().with_left_shift(true),)
                    ),
                    match_any: StateBits {
                        modifiers: ModifierCombination::default().with_left_shift(true).with_right_shift(true),
                        leds: LedIndicator::default(),
                        mouse: MouseButtons::default(),
                    },
                    match_none: StateBits::default(),
                    kept_modifiers: ModifierCombination::default(),
                    bindable: false,
                };

                //{ trigger = "Comma", negative_output = "Comma", positive_output = "Semicolon", match_any = "LShift|RShift" },
                let fork2 = Fork {
                    trigger: KeyAction::Single(Action::Key(KeyCode::Comma)),
                    negative_output: KeyAction::Single(Action::Key(KeyCode::Comma)),
                    positive_output: KeyAction::Single(Action::Key(KeyCode::Semicolon)),
                    match_any: StateBits {
                        modifiers: ModifierCombination::default().with_left_shift(true).with_right_shift(true),
                        leds: LedIndicator::default(),
                        mouse: MouseButtons::default(),
                    },
                    match_none: StateBits::default(),
                    kept_modifiers: ModifierCombination::default(),
                    bindable: false,
                };

                let mut keyboard = create_test_keyboard_with_forks(fork1, fork2);

                // Press Dot key, by itself it should emit '.'
                keyboard.process_inner(KeyboardEvent::key(3, 9, true)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::Dot);

                // Release Dot key
                keyboard.process_inner(KeyboardEvent::key(3, 9, false)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::No);

                // Press LShift key
                keyboard.process_inner(KeyboardEvent::key(3, 0, true)).await;

                // Press Dot key, with shift it should emit ':'
                keyboard.process_inner(KeyboardEvent::key(3, 9, true)).await;
                assert_eq!(
                    keyboard.resolve_modifiers(true),
                    ModifierCombination::new().with_left_shift(true)
                );
                assert_eq!(keyboard.held_keycodes[0], KeyCode::Semicolon);

                //Release Dot key
                keyboard.process_inner(KeyboardEvent::key(3, 9, false)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::No);
                assert_eq!(
                    keyboard.resolve_modifiers(false),
                    ModifierCombination::new().with_left_shift(true)
                );

                // Release LShift key
                keyboard.process_inner(KeyboardEvent::key(3, 0, false)).await;
                assert_eq!(keyboard.held_modifiers, ModifierCombination::new());
                assert_eq!(keyboard.resolve_modifiers(false), ModifierCombination::new());

                // Press Comma key, by itself it should emit ','
                keyboard.process_inner(KeyboardEvent::key(3, 8, true)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::Comma);

                // Release Dot key
                keyboard.process_inner(KeyboardEvent::key(3, 8, false)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::No);

                // Press LShift key
                keyboard.process_inner(KeyboardEvent::key(3, 0, true)).await;

                // Press Comma key, with shift it should emit ';' (shift is suppressed)
                keyboard.process_inner(KeyboardEvent::key(3, 8, true)).await;
                assert_eq!(keyboard.resolve_modifiers(true), ModifierCombination::new());
                assert_eq!(keyboard.held_keycodes[0], KeyCode::Semicolon);

                // Release Comma key, shift is still held
                keyboard.process_inner(KeyboardEvent::key(3, 8, false)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::No);
                assert_eq!(
                    keyboard.resolve_modifiers(false),
                    ModifierCombination::new().with_left_shift(true)
                );

                // Release LShift key
                keyboard.process_inner(KeyboardEvent::key(3, 0, false)).await;
                assert_eq!(keyboard.held_modifiers, ModifierCombination::new());
                assert_eq!(keyboard.resolve_modifiers(false), ModifierCombination::new());
            };

            block_on(main);
        }
        #[test]
        fn test_fork_with_held_mouse_button() {
            let main = async {
                //{ trigger = "Z", negative_output = "MouseBtn5", positive_output = "C", match_any = "LCtrl|RCtrl|LShift|RShift", kept_modifiers="LShift|RShift" },
                let fork1 = Fork {
                    trigger: KeyAction::Single(Action::Key(KeyCode::Z)),
                    negative_output: KeyAction::Single(Action::Key(KeyCode::MouseBtn5)),
                    positive_output: KeyAction::Single(Action::Key(KeyCode::C)),
                    match_any: StateBits {
                        modifiers: ModifierCombination::default()
                            .with_left_ctrl(true)
                            .with_right_ctrl(true)
                            .with_left_shift(true)
                            .with_right_shift(true),
                        leds: LedIndicator::default(),
                        mouse: MouseButtons::default(),
                    },
                    match_none: StateBits::default(),
                    kept_modifiers: ModifierCombination::default().with_left_shift(true).with_right_shift(true),
                    bindable: false,
                };

                //{ trigger = "A", negative_output = "S", positive_output = "D", match_any = "MouseBtn5" },
                let fork2 = Fork {
                    trigger: KeyAction::Single(Action::Key(KeyCode::A)),
                    negative_output: KeyAction::Single(Action::Key(KeyCode::S)),
                    positive_output: KeyAction::Single(Action::Key(KeyCode::D)),
                    match_any: StateBits {
                        modifiers: ModifierCombination::default(),
                        leds: LedIndicator::default(),
                        mouse: MouseButtons::default().with_button5(true),
                    },
                    match_none: StateBits::default(),
                    kept_modifiers: ModifierCombination::default(),
                    bindable: false,
                };

                let mut keyboard = create_test_keyboard_with_forks(fork1, fork2);

                // disable th on a
                keyboard.keymap.borrow_mut().set_action_at(
                    KeyboardEventPos::Key(KeyPos { row: 2, col: 1 }),
                    0,
                    KeyAction::Single(Action::Key(KeyCode::A)),
                );


                // Press Z key, by itself it should emit 'MouseBtn5'
                keyboard.process_inner(KeyboardEvent::key(3, 1, true)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::No);
                assert_eq!(keyboard.mouse_report.buttons, 1u8 << 4); // MouseBtn5

                // Release Z key
                keyboard.process_inner(KeyboardEvent::key(3, 1, false)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::No);
                assert_eq!(keyboard.mouse_report.buttons, 0);

                // Press LCtrl key
                keyboard.process_inner(KeyboardEvent::key(4, 0, true)).await;
                // Press LShift key
                keyboard.process_inner(KeyboardEvent::key(3, 0, true)).await;
                assert_eq!(
                    keyboard.resolve_modifiers(true),
                    ModifierCombination::new().with_left_ctrl(true).with_left_shift(true)
                );

                // Press 'Z' key, with Ctrl it should emit 'C', with suppressed ctrl, but kept shift
                keyboard.process_inner(KeyboardEvent::key(3, 1, true)).await;
                assert_eq!(
                    keyboard.resolve_modifiers(true),
                    ModifierCombination::new().with_left_shift(true)
                );
                assert_eq!(keyboard.held_keycodes[0], KeyCode::C);
                assert_eq!(keyboard.mouse_report.buttons, 0);

                // Release 'Z' key, suppression of ctrl is removed
                keyboard.process_inner(KeyboardEvent::key(3, 1, false)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::No);
                assert_eq!(
                    keyboard.resolve_modifiers(false),
                    ModifierCombination::new().with_left_ctrl(true).with_left_shift(true)
                );

                // Release LCtrl key
                keyboard.process_inner(KeyboardEvent::key(4, 0, false)).await;
                assert_eq!(
                    keyboard.resolve_modifiers(false),
                    ModifierCombination::new().with_left_shift(true)
                );

                // Release LShift key
                keyboard.process_inner(KeyboardEvent::key(3, 0, false)).await;
                assert_eq!(keyboard.resolve_modifiers(false), ModifierCombination::new());

                // Press 'A' key, by itself it should emit 'S'
                keyboard.process_inner(KeyboardEvent::key(2, 1, true)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::S);

                // Release 'A' key
                keyboard.process_inner(KeyboardEvent::key(2, 1, false)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::No);
                assert_eq!(keyboard.resolve_modifiers(false), ModifierCombination::new());
                assert_eq!(keyboard.mouse_report.buttons, 0);

                Timer::after(Duration::from_millis(200)).await; // wait a bit

                // Press Z key, by itself it should emit 'MouseBtn5'
                keyboard.process_inner(KeyboardEvent::key(3, 1, true)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::No);
                assert_eq!(keyboard.mouse_report.buttons, 1u8 << 4); // MouseBtn5 //this fails, but ok in debug - why?

                // Press 'A' key, with 'MouseBtn5' it should emit 'D'
                keyboard.process_inner(KeyboardEvent::key(2, 1, true)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::D);

                // Release Z (MouseBtn1) key, 'D' is still held
                keyboard.process_inner(KeyboardEvent::key(3, 1, false)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::D);

                // Release 'A' key -> releases 'D'
                keyboard.process_inner(KeyboardEvent::key(2, 1, false)).await;
                assert_eq!(keyboard.held_keycodes[0], KeyCode::No);
            };

            block_on(main);
        }
    }
}