libfp 0.10.5

Faderpunk shared libraries
Documentation
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
#![no_std]

use core::ops::Add;

use embassy_time::Duration;
use heapless::Vec;
use max11300::config::{ADCRANGE, DACRANGE};
use midly::num::{u4, u7};
use minicbor::{Decode, Encode};
use postcard_bindgen::PostcardBindings;
use serde::{Deserialize, Serialize};

pub mod colors;
pub mod constants;
pub mod ext;
pub mod fp_grids_lib;
pub mod i2c_proto;
pub mod latch;
pub mod quantizer;
pub mod sysex;
pub mod types;
pub mod utils;

// Re-export commonly used latch types
pub use latch::{AnalogLatch, LatchLayer, TakeoverMode};

use constants::{
    CURVE_EXP, CURVE_LOG, WAVEFORM_SAW, WAVEFORM_SAW_INV, WAVEFORM_SINE, WAVEFORM_SQUARE,
    WAVEFORM_TRIANGLE,
};
use smart_leds::RGB8;

use crate::ext::FromValue;
use colors::{
    BLUE, CYAN, GREEN, LIGHT_BLUE, LIME, ORANGE, PALE_GREEN, PINK, RED, ROSE, SALMON, SAND,
    SKY_BLUE, VIOLET, WHITE, YELLOW,
};

/// Total channel size of this device
pub const GLOBAL_CHANNELS: usize = 16;

/// The devices I2C address (as a follower)
pub const I2C_ADDRESS: u16 = 0x56;
pub const I2C_ADDRESS_CALIBRATION: u16 = 0x57;

/// Maximum number of params per app
pub const APP_MAX_PARAMS: usize = 16;

/// Length of the startup animation
pub const STARTUP_ANIMATION_DURATION: Duration = Duration::from_secs(2);

/// Range in which the LED brightness is scaled
pub const LED_BRIGHTNESS_RANGE: core::ops::Range<u8> = 100..255;

pub const CALIBRATION_SCALE_FACTOR: i64 = 1 << 16;
pub const CALIBRATION_VERSION_LATEST: u8 = 2;
pub const CALIB_FILE_MAGIC: [u8; 4] = *b"FPBC";

pub type ConfigMeta<'a> = (usize, &'a str, &'a str, Color, AppIcon, &'a [Param]);

/// The config layout is a layout with all the apps in the appropriate spots
// (app_id, channels, layout_id)
pub type InnerLayout = [Option<(u8, usize, u8)>; GLOBAL_CHANNELS];

#[derive(Clone, Serialize, Deserialize, PostcardBindings, Encode, Decode)]
#[cbor(transparent)]
pub struct Layout(#[n(0)] pub InnerLayout);

impl Layout {
    pub fn validate(&mut self, get_channels: fn(u8) -> Option<usize>) -> bool {
        let mut validated: InnerLayout = [None; GLOBAL_CHANNELS];
        let mut occupied = [false; GLOBAL_CHANNELS];
        let mut used_ids: Vec<u8, { GLOBAL_CHANNELS }> = Vec::new();

        for (app_id, start_channel, _channels, layout_id) in self.iter() {
            let validated_layout_id =
                if used_ids.contains(&layout_id) || layout_id >= GLOBAL_CHANNELS as u8 {
                    // ID is a duplicate or out of bounds, find the next available one
                    (0..GLOBAL_CHANNELS as u8)
                        .find(|id| !used_ids.contains(id))
                        // This is safe because a free slot is guaranteed
                        .unwrap()
                } else {
                    layout_id
                };

            let _ = used_ids.push(validated_layout_id);

            // Re-verify the channel count for the app_id. Skip if it's not a valid app_id
            let Some(channels) = get_channels(app_id) else {
                continue;
            };

            let end_channel = start_channel + channels;

            // Check if the app fits within the channel count and doesn't overlap
            if end_channel <= GLOBAL_CHANNELS
                && !occupied[start_channel..end_channel].iter().any(|&o| o)
            {
                // Mark channels as occupied
                for occ in occupied.iter_mut().take(end_channel).skip(start_channel) {
                    *occ = true;
                }
                // Add the app to the validated layout
                validated[start_channel] = Some((app_id, channels, validated_layout_id));
            }
        }

        let changed = self.0 != validated;
        self.0 = validated;

        changed
    }

    pub fn iter(&self) -> LayoutIter<'_> {
        self.into_iter()
    }

    pub fn count(&self) -> usize {
        self.iter().count()
    }

    pub fn get_layout_ids(&self) -> Vec<u8, { GLOBAL_CHANNELS }> {
        self.iter().map(|(_, _, _, layout_id)| layout_id).collect()
    }
}

impl Default for Layout {
    fn default() -> Self {
        let inner_layout: InnerLayout = core::array::from_fn(|idx| Some((1, 1, idx as u8)));
        Self(inner_layout)
    }
}

pub struct LayoutIter<'a> {
    slice: &'a [Option<(u8, usize, u8)>],
    index: usize,
}

impl Iterator for LayoutIter<'_> {
    // (app_id, start_channel, channels, layout_id)
    type Item = (u8, usize, usize, u8);

    fn next(&mut self) -> Option<Self::Item> {
        // Skip None values
        while self.index < self.slice.len() {
            if let Some(value) = self.slice[self.index] {
                let idx = self.index;
                self.index += 1;
                return Some((value.0, idx, value.1, value.2));
            }
            self.index += 1;
        }
        None
    }
}

impl<'a> IntoIterator for &'a Layout {
    type Item = (u8, usize, usize, u8);
    type IntoIter = LayoutIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        LayoutIter {
            slice: &self.0,
            index: 0,
        }
    }
}

/// Persisted in `GlobalConfig` via CBOR. New variants may be appended with the
/// next free `#[n(N)]` tag without a migration. **Removing** a variant
/// requires a one-shot FRAM migration (see `storage::migrate_fram`) — old
/// stored data containing the removed tag would otherwise fail to decode.
#[derive(
    Clone, Copy, Default, PartialEq, Serialize, Deserialize, PostcardBindings, Encode, Decode,
)]
#[cbor(index_only)]
#[repr(u8)]
pub enum ClockSrc {
    #[n(0)]
    None,
    #[n(1)]
    Atom,
    #[n(2)]
    Meteor,
    #[n(3)]
    Cube,
    #[default]
    #[n(4)]
    Internal,
    #[n(5)]
    MidiIn,
    #[n(6)]
    MidiUsb,
}

impl From<ResetSrc> for ClockSrc {
    fn from(value: ResetSrc) -> Self {
        match value {
            ResetSrc::None => ClockSrc::None,
            ResetSrc::Atom => ClockSrc::Atom,
            ResetSrc::Meteor => ClockSrc::Meteor,
            ResetSrc::Cube => ClockSrc::Cube,
        }
    }
}

/// Persisted in `GlobalConfig` via CBOR. New variants may be appended with the
/// next free `#[n(N)]` tag without a migration. **Removing** a variant
/// requires a one-shot FRAM migration (see `storage::migrate_fram`).
#[derive(
    Clone, Copy, Default, PartialEq, Serialize, Deserialize, PostcardBindings, Encode, Decode,
)]
#[cbor(index_only)]
#[repr(u8)]
pub enum ResetSrc {
    #[default]
    #[n(0)]
    None,
    #[n(1)]
    Atom,
    #[n(2)]
    Meteor,
    #[n(3)]
    Cube,
}

/// Persisted in `GlobalConfig` via CBOR. New variants may be appended with the
/// next free `#[n(N)]` tag without a migration. **Removing** a variant
/// requires a one-shot FRAM migration (see `storage::migrate_fram`).
#[derive(Clone, Default, Serialize, Deserialize, PostcardBindings, Encode, Decode)]
#[cbor(index_only)]
#[repr(u8)]
pub enum I2cMode {
    #[n(0)]
    Calibration,
    #[default]
    #[n(1)]
    Leader,
    #[n(2)]
    Follower,
}

/// Persisted in `GlobalConfig` via CBOR (inside `QuantizerConfig`). New
/// variants may be appended with the next free `#[n(N)]` tag without a
/// migration. **Removing** a variant requires a one-shot FRAM migration (see
/// `storage::migrate_fram`).
#[derive(
    Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize, PostcardBindings, Encode, Decode,
)]
#[cbor(index_only)]
#[repr(u8)]
pub enum Note {
    #[default]
    #[n(0)]
    C = 0,
    #[n(1)]
    CSharp = 1,
    #[n(2)]
    D = 2,
    #[n(3)]
    DSharp = 3,
    #[n(4)]
    E = 4,
    #[n(5)]
    F = 5,
    #[n(6)]
    FSharp = 6,
    #[n(7)]
    G = 7,
    #[n(8)]
    GSharp = 8,
    #[n(9)]
    A = 9,
    #[n(10)]
    ASharp = 10,
    #[n(11)]
    B = 11,
}

impl From<u8> for Note {
    fn from(value: u8) -> Self {
        match value.min(11) {
            0 => Note::C,
            1 => Note::CSharp,
            2 => Note::D,
            3 => Note::DSharp,
            4 => Note::E,
            5 => Note::F,
            6 => Note::FSharp,
            7 => Note::G,
            8 => Note::GSharp,
            9 => Note::A,
            10 => Note::ASharp,
            11 => Note::B,
            _ => Note::C,
        }
    }
}

impl FromValue for Note {
    fn from_value(value: Value) -> Self {
        match value {
            Value::Note(n) => n,
            _ => Self::default(),
        }
    }
}

/// Persisted in `GlobalConfig` via CBOR (inside `QuantizerConfig`). New
/// variants may be appended with the next free `#[n(N)]` tag without a
/// migration. **Removing** a variant requires a one-shot FRAM migration (see
/// `storage::migrate_fram`).
#[derive(
    Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize, PostcardBindings, Encode, Decode,
)]
#[cbor(index_only)]
#[repr(u8)]
pub enum Key {
    #[default]
    #[n(0)]
    Chromatic,
    #[n(1)]
    Ionian,
    #[n(2)]
    Dorian,
    #[n(3)]
    Phrygian,
    #[n(4)]
    Lydian,
    #[n(5)]
    Mixolydian,
    #[n(6)]
    Aeolian,
    #[n(7)]
    Locrian,
    #[n(8)]
    BluesMaj,
    #[n(9)]
    BluesMin,
    #[n(10)]
    PentatonicMaj,
    #[n(11)]
    PentatonicMin,
    #[n(12)]
    Folk,
    #[n(13)]
    Japanese,
    #[n(14)]
    Gamelan,
    #[n(15)]
    HungarianMin,
    /// CV passes through unmodified; MIDI output still uses nearest chromatic semitone.
    #[n(16)]
    Off,
}

impl Key {
    /// Get the u16 bitmask
    pub fn as_u16_key(&self) -> u16 {
        match self {
            Key::Chromatic => 0b111111111111,
            Key::Ionian => 0b101011010101,
            Key::Dorian => 0b101101010110,
            Key::Phrygian => 0b110101011010,
            Key::Lydian => 0b101010110101,
            Key::Mixolydian => 0b101011010110,
            Key::Aeolian => 0b101101011010,
            Key::Locrian => 0b110101101010,
            Key::BluesMaj => 0b101110010100,
            Key::BluesMin => 0b100101110010,
            Key::PentatonicMaj => 0b101010010100,
            Key::PentatonicMin => 0b100101010010,
            Key::Folk => 0b110111011010,
            Key::Japanese => 0b110001011000,
            Key::Gamelan => 0b110100011000,
            Key::HungarianMin => 0b101100111001,
            // Off: internally treated as Chromatic for MIDI output
            Key::Off => 0b111111111111,
        }
    }
}

/// Volts-per-octave standard. Selects pitch calibration for quantizer and pitch apps.
/// `Standard` = 1V/Oct (Eurorack), `Buchla` = 1.2V/Oct.
#[derive(Clone, Copy, Default, Debug, PartialEq, Serialize, Deserialize, PostcardBindings)]
pub enum VoltPerOct {
    #[default]
    Standard,
    Buchla,
}

impl VoltPerOct {
    pub fn counts_per_oct(self) -> i16 {
        match self {
            VoltPerOct::Standard => 410,
            VoltPerOct::Buchla => 492,
        }
    }

    pub fn semitones_per_volt(self) -> f32 {
        match self {
            VoltPerOct::Standard => 12.0,
            VoltPerOct::Buchla => 10.0,
        }
    }

    pub fn voltage_scale(self) -> f32 {
        match self {
            VoltPerOct::Standard => 1.0,
            VoltPerOct::Buchla => 1.2,
        }
    }
}

impl From<VoltPerOct> for Value {
    fn from(value: VoltPerOct) -> Self {
        Value::VoltPerOct(value)
    }
}

impl FromValue for VoltPerOct {
    fn from_value(value: Value) -> Self {
        match value {
            Value::VoltPerOct(v) => v,
            _ => Self::default(),
        }
    }
}

/// Persisted in `GlobalConfig` via CBOR (inside `MidiOutConfig`). New variants
/// may be appended with the next free `#[n(N)]` tag without a migration.
/// **Removing** a variant requires a one-shot FRAM migration (see
/// `storage::migrate_fram`).
#[derive(
    Clone, Copy, Default, Serialize, Deserialize, PostcardBindings, PartialEq, Encode, Decode,
)]
pub enum MidiOutMode {
    #[n(0)]
    None,
    #[default]
    #[n(1)]
    Local,
    #[n(2)]
    MidiThru {
        #[n(0)]
        sources: MidiIn,
    },
    #[n(3)]
    MidiMerge {
        #[n(0)]
        sources: MidiIn,
    },
}

#[derive(Clone, Copy, Serialize, Deserialize, PostcardBindings, PartialEq, Encode, Decode)]
pub struct MidiOutConfig {
    #[n(0)]
    #[cbor(default)]
    pub send_clock: bool,
    #[n(1)]
    #[cbor(default)]
    pub send_transport: bool,
    #[n(2)]
    #[cbor(default)]
    pub mode: MidiOutMode,
}

impl Default for MidiOutConfig {
    fn default() -> Self {
        Self::new()
    }
}

#[allow(clippy::new_without_default)]
impl MidiOutConfig {
    pub const fn new() -> Self {
        Self {
            send_clock: true,
            send_transport: true,
            mode: MidiOutMode::Local,
        }
    }
}

#[derive(Clone, Serialize, Deserialize, PostcardBindings, PartialEq, Encode, Decode)]
pub struct MidiConfig {
    // [usb, out1, out2]
    #[n(0)]
    #[cbor(default)]
    pub outs: [MidiOutConfig; 3],
}

impl Default for MidiConfig {
    fn default() -> Self {
        Self::new()
    }
}

#[allow(clippy::new_without_default)]
impl MidiConfig {
    pub const fn new() -> Self {
        Self {
            outs: [MidiOutConfig::new(); 3],
        }
    }
}

#[derive(Clone, Serialize, Deserialize, PostcardBindings, PartialEq, Encode, Decode)]
pub struct ClockConfig {
    #[n(0)]
    #[cbor(default)]
    pub clock_src: ClockSrc,
    #[n(1)]
    #[cbor(default)]
    pub ext_ppqn: u8,
    #[n(2)]
    #[cbor(default)]
    pub reset_src: ResetSrc,
    #[n(3)]
    #[cbor(default)]
    pub internal_bpm: f32,
    /// Deluge-style swing amount in `[-35, 35]`. `0` = straight.
    #[n(4)]
    #[cbor(default)]
    pub swing_amount: i8,
}

impl Default for ClockConfig {
    fn default() -> Self {
        Self::new()
    }
}

#[allow(clippy::new_without_default)]
impl ClockConfig {
    pub const fn new() -> Self {
        Self {
            ext_ppqn: 24,
            clock_src: ClockSrc::Internal,
            reset_src: ResetSrc::None,
            internal_bpm: 120.0,
            swing_amount: 0,
        }
    }
}

#[derive(Clone, Default, Serialize, Deserialize, PostcardBindings, PartialEq, Encode, Decode)]
pub struct QuantizerConfig {
    #[n(0)]
    #[cbor(default)]
    pub key: Key,
    #[n(1)]
    #[cbor(default)]
    pub tonic: Note,
}

#[allow(clippy::new_without_default)]
impl QuantizerConfig {
    pub const fn new() -> Self {
        Self {
            key: Key::Chromatic,
            tonic: Note::C,
        }
    }
}

/// Persisted in `GlobalConfig` via CBOR (inside `AuxJackMode::ClockOut`). New
/// variants may be appended with the next free `#[n(N)]` tag without a
/// migration. **Removing** a variant requires a one-shot FRAM migration (see
/// `storage::migrate_fram`).
#[derive(
    Copy, Clone, Default, Serialize, PartialEq, Deserialize, PostcardBindings, Encode, Decode,
)]
#[cbor(index_only)]
#[repr(u16)]
pub enum ClockDivision {
    #[default]
    #[n(1)]
    _1 = 1,
    #[n(2)]
    _2 = 2,
    #[n(4)]
    _4 = 4,
    #[n(6)]
    _6 = 6,
    #[n(8)]
    _8 = 8,
    #[n(12)]
    _12 = 12,
    // 1 quarter note at 24 ppqn
    #[n(24)]
    _24 = 24,
    // 1 bar at 24 ppqn
    #[n(96)]
    _96 = 96,
    // 2 bars
    #[n(192)]
    _192 = 192,
    // 4 bars
    #[n(384)]
    _384 = 384,
}

/// Persisted in `GlobalConfig` via CBOR (inside `aux: [AuxJackMode; 3]`). New
/// variants may be appended with the next free `#[n(N)]` tag without a
/// migration. **Removing** a variant requires a one-shot FRAM migration (see
/// `storage::migrate_fram`).
#[derive(Clone, Default, Serialize, PartialEq, Deserialize, PostcardBindings, Encode, Decode)]
#[repr(u8)]
pub enum AuxJackMode {
    #[default]
    #[n(0)]
    None,
    #[n(1)]
    ClockOut(#[n(0)] ClockDivision),
    #[n(2)]
    ResetOut,
}

/// `GlobalConfig` is persisted to FRAM as CBOR. To keep the on-FRAM format
/// forward/backward compatible without writing a migration:
///
/// - **Every field has `#[cbor(default)]`.** Missing tags decode as
///   `Default::default()` instead of erroring. Removing a field is just
///   deleting the field; old stored data with that tag is silently skipped.
///   Adding a field is just declaring it with the next free tag — old data
///   without the tag falls back to its `Default`.
/// - **Tags are append-only.** Never reuse an `#[n(N)]` for a different
///   purpose; pick the next unused integer. Reusing a tag would silently
///   reinterpret old stored data.
/// - **Field types must implement `Default`** with a value that's safe if it
///   ever shows up on a device that's missing the field in FRAM.
///
/// This convention applies recursively to every type reachable from
/// `GlobalConfig` through fields tagged `#[cbor(default)]`. Things that *do*
/// require a one-shot migration (handled in `storage::migrate_fram`):
///
/// - Changing the type of an existing field (e.g. `u8 → u16`).
/// - Resizing fixed-size arrays (`[T; N]`) or tuples.
/// - Removing an enum variant while old data may still contain it.
#[derive(Clone, Serialize, Deserialize, PostcardBindings, Encode, Decode)]
pub struct GlobalConfig {
    #[n(0)]
    #[cbor(default)]
    pub aux: [AuxJackMode; 3],
    #[n(1)]
    #[cbor(default)]
    pub clock: ClockConfig,
    #[n(2)]
    #[cbor(default)]
    pub i2c_mode: I2cMode,
    #[n(3)]
    #[cbor(default)]
    pub led_brightness: u8,
    #[n(4)]
    #[cbor(default)]
    pub midi: MidiConfig,
    #[n(5)]
    #[cbor(default)]
    pub quantizer: QuantizerConfig,
    #[n(6)]
    #[cbor(default)]
    pub takeover_mode: TakeoverMode,
}

impl Default for GlobalConfig {
    fn default() -> Self {
        Self::new()
    }
}

#[allow(clippy::new_without_default)]
impl GlobalConfig {
    pub const fn new() -> Self {
        Self {
            aux: [
                AuxJackMode::ClockOut(ClockDivision::_1),
                AuxJackMode::None,
                AuxJackMode::None,
            ],
            clock: ClockConfig::new(),
            i2c_mode: I2cMode::Leader,
            led_brightness: 150,
            midi: MidiConfig::new(),
            quantizer: QuantizerConfig::new(),
            takeover_mode: TakeoverMode::Pickup,
        }
    }

    pub const fn validate(&mut self) {
        match self.clock.clock_src {
            ClockSrc::Atom => {
                self.aux[0] = AuxJackMode::None;
            }
            ClockSrc::Meteor => {
                self.aux[1] = AuxJackMode::None;
            }
            ClockSrc::Cube => {
                self.aux[2] = AuxJackMode::None;
            }
            _ => {}
        }
        match self.clock.reset_src {
            ResetSrc::Atom => {
                self.aux[0] = AuxJackMode::None;
            }
            ResetSrc::Meteor => {
                self.aux[1] = AuxJackMode::None;
            }
            ResetSrc::Cube => {
                self.aux[2] = AuxJackMode::None;
            }
            _ => {}
        }
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize, PostcardBindings)]
pub enum Curve {
    #[default]
    Linear,
    Logarithmic,
    Exponential,
}

impl Curve {
    pub fn at(&self, value: u16) -> u16 {
        let value = value.min(4095);
        match self {
            Curve::Linear => value,
            Curve::Exponential => CURVE_EXP[value as usize],
            Curve::Logarithmic => CURVE_LOG[value as usize],
        }
    }

    pub fn cycle(&self) -> Curve {
        match self {
            Curve::Linear => Curve::Exponential,
            Curve::Exponential => Curve::Logarithmic,
            Curve::Logarithmic => Curve::Linear,
        }
    }
}

impl FromValue for Curve {
    fn from_value(value: Value) -> Self {
        match value {
            Value::Curve(c) => c,
            _ => Self::default(),
        }
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize, PostcardBindings)]
pub enum Waveform {
    #[default]
    Triangle,
    Saw,
    SawInv,
    Square,
    Sine,
}

impl Waveform {
    pub fn at(&self, index: usize) -> u16 {
        let i = index % 4096;
        match self {
            Waveform::Sine => WAVEFORM_SINE[i],
            Waveform::Triangle => WAVEFORM_TRIANGLE[i],
            Waveform::Saw => WAVEFORM_SAW[i],
            Waveform::SawInv => WAVEFORM_SAW_INV[i],
            Waveform::Square => WAVEFORM_SQUARE[i],
        }
    }

    pub fn cycle(&self) -> Waveform {
        match self {
            Waveform::Sine => Waveform::Triangle,
            Waveform::Triangle => Waveform::Saw,
            Waveform::Saw => Waveform::SawInv,
            Waveform::SawInv => Waveform::Square,
            Waveform::Square => Waveform::Sine,
        }
    }
}

impl FromValue for Waveform {
    fn from_value(value: Value) -> Self {
        match value {
            Value::Waveform(w) => w,
            _ => Self::default(),
        }
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize, PostcardBindings)]
pub enum Color {
    #[default]
    White,
    Yellow,
    Orange,
    Red,
    Lime,
    Green,
    Cyan,
    SkyBlue,
    Blue,
    Violet,
    Pink,
    PaleGreen,
    Sand,
    Rose,
    Salmon,
    LightBlue,
    Custom(u8, u8, u8),
}

const PALETTE: [Color; 16] = [
    Color::White,
    Color::Pink,
    Color::Yellow,
    Color::Cyan,
    Color::Salmon,
    Color::Lime,
    Color::Orange,
    Color::Green,
    Color::SkyBlue,
    Color::Red,
    Color::PaleGreen,
    Color::Blue,
    Color::Sand,
    Color::Violet,
    Color::LightBlue,
    Color::Rose,
];

impl From<usize> for Color {
    fn from(value: usize) -> Self {
        PALETTE[value]
    }
}

impl From<Color> for RGB8 {
    fn from(value: Color) -> Self {
        match value {
            Color::White => WHITE,
            Color::Pink => PINK,
            Color::Yellow => YELLOW,
            Color::Cyan => CYAN,
            Color::Salmon => SALMON,
            Color::Lime => LIME,
            Color::Orange => ORANGE,
            Color::Green => GREEN,
            Color::SkyBlue => SKY_BLUE,
            Color::Red => RED,
            Color::PaleGreen => PALE_GREEN,
            Color::Blue => BLUE,
            Color::Sand => SAND,
            Color::Violet => VIOLET,
            Color::LightBlue => LIGHT_BLUE,
            Color::Rose => ROSE,
            Color::Custom(r, g, b) => RGB8 { r, g, b },
        }
    }
}

impl FromValue for Color {
    fn from_value(value: Value) -> Self {
        match value {
            Value::Color(c) => c,
            _ => Self::default(),
        }
    }
}

#[derive(Clone, Copy)]
pub enum Brightness {
    Off,
    Low,
    Mid,
    High,
    Custom(u8),
}

impl From<Brightness> for u8 {
    fn from(value: Brightness) -> Self {
        match value {
            Brightness::Off => 0,
            Brightness::Low => 110,
            Brightness::Mid => 180,
            Brightness::High => 255,
            Brightness::Custom(value) => value,
        }
    }
}

#[derive(Clone, Copy, Default, Serialize, Deserialize, PostcardBindings)]
pub enum AppIcon {
    #[default]
    Fader,
    AdEnv,
    Random,
    Euclid,
    Attenuate,
    Die,
    Quantize,
    Sequence,
    Note,
    EnvFollower,
    SoftRandom,
    Sine,
    NoteBox,
    SequenceSquare,
    NoteGrid,
    KnobRound,
    Stereo,
}

#[allow(non_camel_case_types)]
#[derive(Clone, Copy, Serialize, PostcardBindings)]
pub enum Param {
    None,
    i32 {
        name: &'static str,
        min: i32,
        max: i32,
    },
    f32 {
        name: &'static str,
        min: f32,
        max: f32,
    },
    bool {
        name: &'static str,
    },
    Enum {
        name: &'static str,
        variants: &'static [&'static str],
    },
    Curve {
        name: &'static str,
        variants: &'static [Curve],
    },
    Waveform {
        name: &'static str,
        variants: &'static [Waveform],
    },
    Color {
        name: &'static str,
        variants: &'static [Color],
    },
    Range {
        name: &'static str,
        variants: &'static [Range],
    },
    Note {
        name: &'static str,
        variants: &'static [Note],
    },
    MidiCc {
        name: &'static str,
    },
    MidiChannel {
        name: &'static str,
    },
    MidiIn,
    MidiMode,
    MidiNote {
        name: &'static str,
    },
    MidiOut,
    MidiNrpn,
    VoltPerOct,
}

#[allow(non_camel_case_types)]
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize, PostcardBindings)]
pub enum Value {
    i32(i32),
    f32(f32),
    bool(bool),
    Enum(usize),
    Curve(Curve),
    Waveform(Waveform),
    Color(Color),
    Range(Range),
    Note(Note),
    MidiCc(MidiCc),
    MidiChannel(MidiChannel),
    MidiIn(MidiIn),
    MidiMode(MidiMode),
    MidiNote(MidiNote),
    MidiOut(MidiOut),
    MidiNrpn(bool),
    VoltPerOct(VoltPerOct),
}

impl From<Curve> for Value {
    fn from(value: Curve) -> Self {
        Value::Curve(value)
    }
}

impl From<Waveform> for Value {
    fn from(value: Waveform) -> Self {
        Value::Waveform(value)
    }
}

impl From<Color> for Value {
    fn from(value: Color) -> Self {
        Value::Color(value)
    }
}

impl From<Range> for Value {
    fn from(value: Range) -> Self {
        Value::Range(value)
    }
}

impl From<Note> for Value {
    fn from(value: Note) -> Self {
        Value::Note(value)
    }
}

impl From<MidiCc> for Value {
    fn from(value: MidiCc) -> Self {
        Value::MidiCc(value)
    }
}

impl From<MidiChannel> for Value {
    fn from(value: MidiChannel) -> Self {
        Value::MidiChannel(value)
    }
}

impl From<MidiIn> for Value {
    fn from(value: MidiIn) -> Self {
        Value::MidiIn(value)
    }
}

impl From<MidiMode> for Value {
    fn from(value: MidiMode) -> Self {
        Value::MidiMode(value)
    }
}

impl From<MidiNote> for Value {
    fn from(value: MidiNote) -> Self {
        Value::MidiNote(value)
    }
}

impl From<MidiOut> for Value {
    fn from(value: MidiOut) -> Self {
        Value::MidiOut(value)
    }
}

impl From<i32> for Value {
    fn from(value: i32) -> Self {
        Value::i32(value)
    }
}

impl From<f32> for Value {
    fn from(value: f32) -> Self {
        Value::f32(value)
    }
}

impl From<bool> for Value {
    fn from(value: bool) -> Self {
        Value::bool(value)
    }
}

impl From<usize> for Value {
    fn from(value: usize) -> Self {
        Value::Enum(value)
    }
}

#[derive(Deserialize, PostcardBindings)]
pub enum ConfigMsgIn {
    Ping,
    GetAllApps,
    GetGlobalConfig,
    SetGlobalConfig(GlobalConfig),
    GetLayout,
    SetLayout(Layout),
    GetAllAppParams,
    GetAppParams {
        layout_id: u8,
    },
    SetAppParams {
        layout_id: u8,
        values: [Option<Value>; APP_MAX_PARAMS],
    },
    FactoryReset,
    GetVersion,
}

#[derive(Clone, Serialize, PostcardBindings)]
#[allow(clippy::large_enum_variant)]
pub enum ConfigMsgOut<'a> {
    Pong,
    BatchMsgStart(usize),
    BatchMsgEnd,
    GlobalConfig(GlobalConfig),
    Layout(Layout),
    AppConfig(u8, usize, ConfigMeta<'a>),
    AppState(u8, &'a [Value]),
    Version { major: u8, minor: u8, patch: u8 },
}

pub struct Config<const N: usize> {
    len: usize,
    name: &'static str,
    description: &'static str,
    params: [Param; N],
    color: Color,
    icon: AppIcon,
}

impl<const N: usize> Config<N> {
    pub const fn new(
        name: &'static str,
        description: &'static str,
        color: Color,
        icon: AppIcon,
    ) -> Self {
        assert!(N <= APP_MAX_PARAMS, "Too many params");
        Config {
            color,
            description,
            icon,
            len: 0,
            name,
            params: [const { Param::None }; N],
        }
    }

    pub const fn add_param(mut self, param: Param) -> Self {
        self.params[self.len] = param;
        let new_len = self.len + 1;
        Config {
            color: self.color,
            description: self.description,
            icon: self.icon,
            len: new_len,
            name: self.name,
            params: self.params,
        }
    }

    pub fn get_meta(&self) -> ConfigMeta<'_> {
        (
            N,
            self.name,
            self.description,
            self.color,
            self.icon,
            &self.params,
        )
    }
}

/// Supported DAC ranges
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, PostcardBindings, PartialEq, Eq)]
#[repr(u8)]
pub enum Range {
    // 0 - 10V
    #[default]
    _0_10V,
    // 0 - 5V
    _0_5V,
    // -5 - 5V
    _Neg5_5V,
}

impl Range {
    // TODO: We might want to not need this in apps (handle it differently)
    pub fn is_bipolar(&self) -> bool {
        *self == Range::_Neg5_5V
    }
}

impl From<Range> for DACRANGE {
    fn from(value: Range) -> Self {
        match value {
            Range::_0_10V => DACRANGE::Rg0_10v,
            Range::_0_5V => DACRANGE::Rg0_10v,
            Range::_Neg5_5V => DACRANGE::RgNeg5_5v,
        }
    }
}
impl From<Range> for ADCRANGE {
    fn from(value: Range) -> Self {
        match value {
            Range::_0_10V => ADCRANGE::Rg0_10v,
            Range::_0_5V => ADCRANGE::Rg0_10v,
            Range::_Neg5_5V => ADCRANGE::RgNeg5_5v,
        }
    }
}

impl FromValue for Range {
    fn from_value(value: Value) -> Self {
        match value {
            Value::Range(r) => r,
            _ => Self::default(),
        }
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize, PostcardBindings)]
pub struct MidiCc(u16);

impl MidiCc {
    pub fn as_u16(&self) -> u16 {
        self.0
    }
}

impl FromValue for MidiCc {
    fn from_value(value: Value) -> Self {
        match value {
            Value::MidiCc(m) => m,
            _ => Self::default(),
        }
    }
}

impl From<u8> for MidiCc {
    fn from(value: u8) -> Self {
        Self(value as u16)
    }
}

impl From<u16> for MidiCc {
    fn from(value: u16) -> Self {
        Self(value.min(16383))
    }
}

impl From<i32> for MidiCc {
    fn from(value: i32) -> Self {
        Self((value.clamp(0, 16383)) as u16)
    }
}

impl From<MidiCc> for u7 {
    fn from(value: MidiCc) -> Self {
        u7::from_int_lossy(value.0 as u8)
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize, PostcardBindings)]
pub struct MidiChannel(u8);

impl FromValue for MidiChannel {
    fn from_value(value: Value) -> Self {
        match value {
            Value::MidiChannel(m) => m,
            _ => Self::default(),
        }
    }
}

impl Default for MidiChannel {
    fn default() -> Self {
        MidiChannel(1)
    }
}

impl From<u8> for MidiChannel {
    fn from(value: u8) -> Self {
        Self(value.min(16))
    }
}

impl From<MidiChannel> for u4 {
    fn from(value: MidiChannel) -> Self {
        u4::from_int_lossy(value.0.saturating_sub(1).min(15))
    }
}

#[derive(
    Clone, Copy, Debug, PartialEq, Serialize, Deserialize, PostcardBindings, Encode, Decode,
)]
#[cbor(transparent)]
// [usb, din]
pub struct MidiIn(#[n(0)] pub [bool; 2]);

impl Default for MidiIn {
    fn default() -> Self {
        Self([true; 2])
    }
}

impl FromValue for MidiIn {
    fn from_value(value: Value) -> Self {
        match value {
            Value::MidiIn(m) => m,
            _ => Self::default(),
        }
    }
}

impl MidiIn {
    pub fn is_some(&self) -> bool {
        self.0.iter().any(|i| *i)
    }

    pub fn is_none(&self) -> bool {
        !self.is_some()
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize, PostcardBindings)]
pub struct MidiNote(u8);

impl FromValue for MidiNote {
    fn from_value(value: Value) -> Self {
        match value {
            Value::MidiNote(m) => m,
            _ => Self::default(),
        }
    }
}

impl From<u8> for MidiNote {
    fn from(value: u8) -> Self {
        Self(value.min(127))
    }
}

impl From<i32> for MidiNote {
    fn from(value: i32) -> Self {
        Self(value.clamp(0, 127) as u8)
    }
}

impl From<MidiNote> for u7 {
    fn from(value: MidiNote) -> Self {
        u7::from_int_lossy(value.0)
    }
}

impl Add<MidiNote> for MidiNote {
    type Output = Self;

    fn add(self, rhs: MidiNote) -> Self::Output {
        Self(self.0.saturating_add(rhs.0).min(127))
    }
}

impl MidiNote {
    /// Transpose a MidiNote by +/- semitones
    pub fn transpose(&mut self, semitones: i8) -> Self {
        Self((self.0 as i8 + semitones).clamp(0, 127) as u8)
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize, PostcardBindings)]
#[repr(u8)]
pub enum MidiMode {
    #[default]
    Note,
    Cc,
}

impl FromValue for MidiMode {
    fn from_value(value: Value) -> Self {
        match value {
            Value::MidiMode(m) => m,
            _ => Self::default(),
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize, PostcardBindings)]
// [usb, out1, out2]
pub struct MidiOut(pub [bool; 3]);

impl FromValue for MidiOut {
    fn from_value(value: Value) -> Self {
        match value {
            Value::MidiOut(m) => m,
            _ => Self::default(),
        }
    }
}

impl Default for MidiOut {
    fn default() -> Self {
        Self([true; 3])
    }
}

impl MidiOut {
    pub fn is_some(&self) -> bool {
        self.0.iter().any(|i| *i)
    }

    pub fn is_none(&self) -> bool {
        !self.is_some()
    }
}

#[cfg(test)]
mod tests {
    use super::{Layout, GLOBAL_CHANNELS};
    use heapless::Vec;

    fn mock_get_channels(app_id: u8) -> Option<usize> {
        match app_id {
            1 => Some(1), // App 1 takes 2 channels
            2 => Some(4), // App 2 takes 4 channels
            3 => Some(3), // App 3 takes 3 channels
            _ => None,    // Any other app_id is invalid
        }
    }

    #[test]
    fn validate_no_changes() {
        let mut layout = Layout([None; GLOBAL_CHANNELS]);
        layout.0[0] = Some((1, 1, 0));
        layout.0[4] = Some((2, 4, 1));
        let original_layout = layout.0;

        let changed = layout.validate(mock_get_channels);

        assert!(!changed);
        assert_eq!(layout.0, original_layout);
    }

    #[test]
    fn validate_removes_overlapping() {
        let mut layout = Layout([None; GLOBAL_CHANNELS]);
        // App 2 is valid (takes 4 channels: 0, 1, 2, 3)
        layout.0[0] = Some((2, 4, 0));
        // App 3 overlaps with App 2 (tries to start at channel 2 but channels 2, 3 overlap with App 2)
        layout.0[2] = Some((3, 3, 1));
        // App 1 is valid and doesn't overlap
        layout.0[5] = Some((1, 1, 2));

        let changed = layout.validate(mock_get_channels);

        assert!(changed);
        // App 2 should remain (processed first)
        assert_eq!(layout.0[0], Some((2, 4, 0)));
        // App 3 should be removed (overlaps)
        assert_eq!(layout.0[2], None);
        // App 1 should remain
        assert_eq!(layout.0[5], Some((1, 1, 2)));
    }

    #[test]
    fn validate_removes_out_of_bounds() {
        let mut layout = Layout([None; GLOBAL_CHANNELS]);
        // This app goes from channel 14 up to 18, which is beyond GLOBAL_CHANNELS (16)
        layout.0[14] = Some((2, 4, 0));

        let changed = layout.validate(mock_get_channels);

        assert!(changed);
        // The out-of-bounds app should be removed
        assert_eq!(layout.0[14], None);
        assert!(layout.0.iter().all(|&app| app.is_none()));
    }

    #[test]
    fn validate_removes_invalid_id() {
        let mut layout = Layout([None; GLOBAL_CHANNELS]);
        // App ID 99 is not valid according to mock_get_channels
        layout.0[0] = Some((99, 2, 0));

        let changed = layout.validate(mock_get_channels);

        assert!(changed);
        assert_eq!(layout.0[0], None);
    }

    #[test]
    fn validate_corrects_channel_size() {
        let mut layout = Layout([None; GLOBAL_CHANNELS]);
        // The stored channel size is 99, but mock_get_channels returns 1 for app_id 1
        layout.0[0] = Some((1, 99, 0));

        let changed = layout.validate(mock_get_channels);

        assert!(changed);
        // The channel size should be corrected to 1
        assert_eq!(layout.0[0], Some((1, 1, 0)));
    }

    #[test]
    fn validate_resolves_duplicate_and_oob_layout_ids() {
        let mut layout = Layout([None; GLOBAL_CHANNELS]);
        // Set up layout with duplicates and an out-of-bounds ID
        layout.0[0] = Some((1, 1, 5)); // Valid
        layout.0[2] = Some((1, 1, 2)); // Will be kept
        layout.0[4] = Some((1, 1, 2)); // Duplicate of ID 2
        layout.0[6] = Some((1, 1, 16)); // Out of bounds (>= GLOBAL_CHANNELS)
        layout.0[8] = Some((1, 1, 5)); // Duplicate of ID 5

        let changed = layout.validate(mock_get_channels);
        assert!(changed);

        // Expected layout_id assignments based on iteration order
        assert_eq!(layout.0[0].unwrap().2, 5);
        assert_eq!(layout.0[2].unwrap().2, 2);
        assert_eq!(layout.0[4].unwrap().2, 0); // First free ID
        assert_eq!(layout.0[6].unwrap().2, 1); // Second free ID
        assert_eq!(layout.0[8].unwrap().2, 3); // Third free ID

        // Verify all final layout_ids are unique
        let mut final_ids: Vec<u8, { GLOBAL_CHANNELS }> = Vec::new();
        for (_, _, _, layout_id) in layout.iter() {
            assert!(!final_ids.contains(&layout_id));
            final_ids.push(layout_id).unwrap();
        }
    }

    // ---- CBOR storage / migration tests ----
    //
    // These pin down the behaviour the rest of the storage layer relies on:
    //   1. CBOR round-trips current persisted types.
    //   2. Adding a `#[cbor(default)]` field is forward-compatible: data written
    //      by an older shape decodes into the newer shape with the new field
    //      defaulted.
    //   3. Removing a field is backward-compatible: data written by an older
    //      shape decodes into the newer (slimmer) shape with the dropped field
    //      silently skipped.
    //   4. v1.8.2 postcard `GlobalConfig` data fails to decode as the current
    //      postcard `GlobalConfig` (which is what makes the migration's
    //      "current first, V0 fallback" strategy safe).

    use super::*;
    use minicbor::{Decode, Encode};
    use serde::{Deserialize, Serialize};

    fn cbor_encode_to_vec<T: Encode<()>>(value: &T) -> heapless::Vec<u8, 256> {
        let mut buf = [0u8; 256];
        let initial = buf.len();
        let mut writer: &mut [u8] = &mut buf[..];
        minicbor::encode(value, &mut writer).unwrap();
        let written = initial - writer.len();
        heapless::Vec::from_slice(&buf[..written]).unwrap()
    }

    #[test]
    fn cbor_round_trip_default_global_config() {
        let original = GlobalConfig::new();
        let encoded = cbor_encode_to_vec(&original);
        let decoded: GlobalConfig = minicbor::decode(&encoded).unwrap();

        assert_eq!(decoded.led_brightness, original.led_brightness);
        assert_eq!(
            decoded.clock.internal_bpm.to_bits(),
            original.clock.internal_bpm.to_bits()
        );
        assert_eq!(decoded.clock.swing_amount, original.clock.swing_amount);
        assert_eq!(decoded.i2c_mode as u8, I2cMode::Leader as u8);
    }

    #[test]
    fn cbor_field_added_decodes_with_default() {
        // V1: a struct shaped like an "older" version.
        #[derive(Encode, Decode)]
        struct V1 {
            #[n(0)]
            x: u32,
            #[n(1)]
            y: u32,
        }

        // V2: same tags, plus a new field that uses cbor(default).
        #[derive(Encode, Decode, Debug, PartialEq)]
        struct V2 {
            #[n(0)]
            #[cbor(default)]
            x: u32,
            #[n(1)]
            #[cbor(default)]
            y: u32,
            #[n(2)]
            #[cbor(default)]
            z: u32,
        }

        let v1 = V1 { x: 42, y: 7 };
        let bytes = cbor_encode_to_vec(&v1);
        let v2: V2 = minicbor::decode(&bytes).unwrap();
        assert_eq!(v2, V2 { x: 42, y: 7, z: 0 });
    }

    #[test]
    fn cbor_field_removed_skips_unknown_tag() {
        // V2: an "older" version that wrote a y field.
        #[derive(Encode, Decode)]
        struct V2 {
            #[n(0)]
            x: u32,
            #[n(1)]
            y: u32,
        }

        // V3: the y field has been removed.
        #[derive(Encode, Decode, Debug, PartialEq)]
        struct V3 {
            #[n(0)]
            x: u32,
        }

        let v2 = V2 { x: 42, y: 999 };
        let bytes = cbor_encode_to_vec(&v2);
        let v3: V3 = minicbor::decode(&bytes).unwrap();
        assert_eq!(v3, V3 { x: 42 });
    }

    #[test]
    fn cbor_postcard_data_does_not_decode_as_cbor() {
        // After migration, FRAM holds CBOR. If the schema header somehow gets
        // lost and migrate_fram runs on already-migrated data, the postcard
        // legacy decoders must reject the CBOR bytes (so we don't clobber).
        let original = GlobalConfig::new();
        let cbor_bytes = cbor_encode_to_vec(&original);

        let postcard_result: Result<GlobalConfig, _> = postcard::from_bytes(&cbor_bytes);
        assert!(
            postcard_result.is_err(),
            "postcard must reject CBOR-encoded GlobalConfig bytes"
        );
    }

    /// Mirror of v1.8.2's `ClockConfig` shape, used only by the migration
    /// regression tests below.
    #[derive(Serialize, Deserialize)]
    struct ClockConfigPreSwing {
        clock_src: ClockSrc,
        ext_ppqn: u8,
        reset_src: ResetSrc,
        internal_bpm: f32,
    }

    /// Mirror of v1.8.2's `GlobalConfig` shape (no `swing_amount` in clock).
    #[derive(Serialize, Deserialize)]
    struct GlobalConfigPreSwing {
        aux: [AuxJackMode; 3],
        clock: ClockConfigPreSwing,
        i2c_mode: I2cMode,
        led_brightness: u8,
        midi: MidiConfig,
        quantizer: QuantizerConfig,
        takeover_mode: TakeoverMode,
    }

    fn make_v18_default() -> GlobalConfigPreSwing {
        GlobalConfigPreSwing {
            aux: [
                AuxJackMode::ClockOut(ClockDivision::_1),
                AuxJackMode::None,
                AuxJackMode::None,
            ],
            clock: ClockConfigPreSwing {
                clock_src: ClockSrc::Internal,
                ext_ppqn: 24,
                reset_src: ResetSrc::None,
                internal_bpm: 120.0,
            },
            i2c_mode: I2cMode::Leader,
            led_brightness: 150,
            midi: MidiConfig::new(),
            quantizer: QuantizerConfig::new(),
            takeover_mode: TakeoverMode::Pickup,
        }
    }

    #[test]
    fn v18_postcard_data_fails_current_postcard_decode() {
        // The migration's correctness rests on this invariant: v1.8.2 stored
        // bytes are always 1 byte short of what the current GlobalConfig
        // postcard decoder needs (the missing `swing_amount`). Without it,
        // the "try current first" path could mis-decode v1.8.2 data as
        // pre-fix v1.9 data with shifted fields.
        let v18 = make_v18_default();
        let mut buf = [0u8; 256];
        let bytes = postcard::to_slice(&v18, &mut buf).unwrap();

        let result: Result<GlobalConfig, _> = postcard::from_bytes(bytes);
        assert!(
            result.is_err(),
            "v1.8.2 postcard data must NOT decode as current postcard GlobalConfig"
        );
    }

    #[test]
    fn v18_postcard_data_decodes_as_pre_swing_shape() {
        // The fallback path: same legacy bytes, decoded with the v1.8.2-shaped
        // type, must succeed.
        let v18 = make_v18_default();
        let mut buf = [0u8; 256];
        let bytes = postcard::to_slice(&v18, &mut buf).unwrap();

        let decoded: GlobalConfigPreSwing = postcard::from_bytes(bytes).unwrap();
        assert_eq!(decoded.led_brightness, 150);
        assert_eq!(decoded.clock.ext_ppqn, 24);
        assert_eq!(decoded.clock.internal_bpm.to_bits(), 120.0_f32.to_bits());
    }

    #[test]
    fn pre_fix_v19_postcard_data_decodes_as_current() {
        // Pre-fix v1.9 betas wrote postcard `GlobalConfig` with the swing
        // field present. The "try current first" path must accept it as-is.
        let original = GlobalConfig::new();
        let mut buf = [0u8; 256];
        let bytes = postcard::to_slice(&original, &mut buf).unwrap();

        let decoded: GlobalConfig = postcard::from_bytes(bytes).unwrap();
        assert_eq!(decoded.led_brightness, 150);
        assert_eq!(decoded.clock.swing_amount, 0);
        assert_eq!(decoded.i2c_mode as u8, I2cMode::Leader as u8);
    }

    #[test]
    fn brightness_round_trips_through_v18_migration() {
        // Reproduction of a real hardware report: brightness 111 on v1.8.2
        // came back as 100 after upgrading to fixed v1.9. Pin down that the
        // postcard-V0 → CBOR migration preserves an arbitrary u8 brightness
        // exactly (no clamping, no off-by-one).
        let mut v18 = make_v18_default();
        v18.led_brightness = 111;
        let mut buf = [0u8; 256];
        let bytes = postcard::to_slice(&v18, &mut buf).unwrap();

        // Step 1: migration reads the legacy bytes via GlobalConfigPreSwing.
        let decoded_v0: GlobalConfigPreSwing = postcard::from_bytes(bytes).unwrap();
        assert_eq!(decoded_v0.led_brightness, 111);

        // Step 2: migration would convert to current GlobalConfig (we mirror
        // the From<GlobalConfigV0> conversion in storage.rs here).
        let migrated = GlobalConfig {
            aux: decoded_v0.aux,
            clock: ClockConfig {
                clock_src: decoded_v0.clock.clock_src,
                ext_ppqn: decoded_v0.clock.ext_ppqn,
                reset_src: decoded_v0.clock.reset_src,
                internal_bpm: decoded_v0.clock.internal_bpm,
                swing_amount: 0,
            },
            i2c_mode: decoded_v0.i2c_mode,
            led_brightness: decoded_v0.led_brightness,
            midi: decoded_v0.midi,
            quantizer: decoded_v0.quantizer,
            takeover_mode: decoded_v0.takeover_mode,
        };
        assert_eq!(migrated.led_brightness, 111);

        // Step 3: round-trip through CBOR (what migration actually writes,
        // and what every subsequent boot reads).
        let encoded = cbor_encode_to_vec(&migrated);
        let decoded: GlobalConfig = minicbor::decode(&encoded).unwrap();
        assert_eq!(decoded.led_brightness, 111);
    }

    /// Mirror of v1.7.0's `GlobalConfig` shape (no `takeover_mode`, no
    /// `swing_amount`).
    #[derive(Serialize, Deserialize)]
    struct GlobalConfigV170 {
        aux: [AuxJackMode; 3],
        clock: ClockConfigPreSwing,
        i2c_mode: I2cMode,
        led_brightness: u8,
        midi: MidiConfig,
        quantizer: QuantizerConfig,
    }

    fn make_v17_default() -> GlobalConfigV170 {
        GlobalConfigV170 {
            aux: [
                AuxJackMode::ClockOut(ClockDivision::_1),
                AuxJackMode::None,
                AuxJackMode::None,
            ],
            clock: ClockConfigPreSwing {
                clock_src: ClockSrc::Internal,
                ext_ppqn: 24,
                reset_src: ResetSrc::None,
                internal_bpm: 120.0,
            },
            i2c_mode: I2cMode::Leader,
            led_brightness: 150,
            midi: MidiConfig::new(),
            quantizer: QuantizerConfig::new(),
        }
    }

    #[test]
    fn v17_postcard_data_fails_current_and_v18_postcard_decode() {
        // v1.7.0 data is 2 bytes shorter than current and 1 byte shorter than
        // v1.8.x. Both larger shapes must fail so the V170 fallback is the one
        // that actually runs.
        let v17 = make_v17_default();
        let mut buf = [0u8; 256];
        let bytes = postcard::to_slice(&v17, &mut buf).unwrap();

        let current: Result<GlobalConfig, _> = postcard::from_bytes(bytes);
        assert!(current.is_err(), "v1.7.0 must NOT decode as current");
        let v18: Result<GlobalConfigPreSwing, _> = postcard::from_bytes(bytes);
        assert!(v18.is_err(), "v1.7.0 must NOT decode as v1.8.x");
    }

    #[test]
    fn v17_postcard_data_decodes_as_v170_shape() {
        let v17 = make_v17_default();
        let mut buf = [0u8; 256];
        let bytes = postcard::to_slice(&v17, &mut buf).unwrap();

        let decoded: GlobalConfigV170 = postcard::from_bytes(bytes).unwrap();
        assert_eq!(decoded.led_brightness, 150);
        assert_eq!(decoded.clock.internal_bpm.to_bits(), 120.0_f32.to_bits());
    }

    #[test]
    fn brightness_round_trips_through_v170_migration() {
        // Same hardware-bug regression as the v1.8.x test, one version older:
        // brightness 111 set on v1.7.0 must survive the postcard-V170 → CBOR
        // migration unchanged. `swing_amount` and `takeover_mode` come back as
        // their `Default::default()` values.
        let mut v17 = make_v17_default();
        v17.led_brightness = 111;
        let mut buf = [0u8; 256];
        let bytes = postcard::to_slice(&v17, &mut buf).unwrap();

        let decoded_v17: GlobalConfigV170 = postcard::from_bytes(bytes).unwrap();
        assert_eq!(decoded_v17.led_brightness, 111);

        // Mirror the From<GlobalConfigV170> conversion from storage.rs.
        let migrated = GlobalConfig {
            aux: decoded_v17.aux,
            clock: ClockConfig {
                clock_src: decoded_v17.clock.clock_src,
                ext_ppqn: decoded_v17.clock.ext_ppqn,
                reset_src: decoded_v17.clock.reset_src,
                internal_bpm: decoded_v17.clock.internal_bpm,
                swing_amount: 0,
            },
            i2c_mode: decoded_v17.i2c_mode,
            led_brightness: decoded_v17.led_brightness,
            midi: decoded_v17.midi,
            quantizer: decoded_v17.quantizer,
            takeover_mode: TakeoverMode::Pickup,
        };
        assert_eq!(migrated.led_brightness, 111);
        assert_eq!(migrated.clock.swing_amount, 0);

        let encoded = cbor_encode_to_vec(&migrated);
        let decoded: GlobalConfig = minicbor::decode(&encoded).unwrap();
        assert_eq!(decoded.led_brightness, 111);
        assert_eq!(decoded.clock.swing_amount, 0);
    }
}