st7305-lcd 0.2.0

Platform-independent no_std driver for the Sitronix ST7305 reflective LCD controller (embedded-hal 1.0, blocking and async)
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
use crate::{command::WriteCommand, instr::Instr};

macro_rules! millivolt_value {
    (
        $(#[$meta:meta])*
        $name:ident: $min:literal ..= $max:literal,
        zero = $zero:literal,
        step = $step:literal
    ) => {
        $(#[$meta])*
        #[derive(Clone, Copy, Debug, PartialEq, Eq)]
        pub struct $name(u8);

        impl $name {
            #[doc = concat!(
                "Voltage in millivolts, ", stringify!($min), " to ", stringify!($max),
                " in steps of ", stringify!($step), " mV.\n\n# Panics\n\n",
                "If `mv` is out of range or not on a step (at compile time in a `const`)."
            )]
            pub const fn from_mv(mv: i16) -> Self {
                assert!(
                    mv >= $min && mv <= $max && (mv - $zero) % $step == 0,
                    concat!(stringify!($name), " out of range")
                );
                Self(((mv - $zero).unsigned_abs() / $step) as u8)
            }
        }
    };
}

/// `GCTRL` (§8.2.10): gate voltages VGH and VGL.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct GateVoltageControl {
    vgha: Vgh,
    vgla: Vgl,
}

impl GateVoltageControl {
    /// Register default: VGH 15 V, VGL -10 V.
    pub const DEFAULT: Self = Self {
        vgha: Vgh::from_mv(15_000),
        vgla: Vgl::from_mv(-10_000),
    };

    /// `VGHA[4:0]`.
    #[must_use]
    pub const fn with_vgh(mut self, v: Vgh) -> Self {
        self.vgha = v;
        self
    }

    /// `VGLA[4:0]`.
    #[must_use]
    pub const fn with_vgl(mut self, v: Vgl) -> Self {
        self.vgla = v;
        self
    }

    const fn bytes(&self) -> [u8; 2] {
        [self.vgha.0, self.vgla.0]
    }
}

impl Default for GateVoltageControl {
    fn default() -> Self {
        Self::DEFAULT
    }
}

impl WriteCommand for GateVoltageControl {
    const OPCODE: Instr = Instr::GCTRL;
    type Params = [u8; 2];
    fn params(&self) -> Self::Params {
        self.bytes()
    }
}

millivolt_value!(
    /// Gate high voltage VGH.
    Vgh: 8_000..=17_000, zero = 8_000, step = 500
);
millivolt_value!(
    /// Gate low voltage VGL.
    Vgl: -15_000..=-6_000, zero = -5_000, step = 500
);

const _: () = assert!(matches!(GateVoltageControl::DEFAULT.bytes(), [0x0E, 0x0A]));
const _: () = assert!(matches!(
    GateVoltageControl::DEFAULT
        .with_vgh(Vgh::from_mv(8_000))
        .with_vgl(Vgl::from_mv(-6_000))
        .bytes(),
    [0x00, 0x02]
));
const _: () = assert!(matches!(
    GateVoltageControl::DEFAULT
        .with_vgh(Vgh::from_mv(17_000))
        .with_vgl(Vgl::from_mv(-15_000))
        .bytes(),
    [0x12, 0x14]
));

/// `VSHPCTRL` (§8.2.11): source high positive voltage of the four source
/// voltage sets (see [`SourceVoltageSelect`]).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SourceHighPositiveVoltageControl {
    vshpa: [SourceHighPositiveVoltage; 4],
}

impl SourceHighPositiveVoltageControl {
    /// Register default: 5.0 V in all four sets.
    pub const DEFAULT: Self = Self {
        vshpa: [
            SourceHighPositiveVoltage::from_mv(5_000),
            SourceHighPositiveVoltage::from_mv(5_000),
            SourceHighPositiveVoltage::from_mv(5_000),
            SourceHighPositiveVoltage::from_mv(5_000),
        ],
    };

    /// `VSHPA1..=4`.
    #[must_use]
    pub const fn with_vshpa(mut self, v: [SourceHighPositiveVoltage; 4]) -> Self {
        self.vshpa = v;
        self
    }

    const fn bytes(&self) -> [u8; 4] {
        [
            self.vshpa[0].0,
            self.vshpa[1].0,
            self.vshpa[2].0,
            self.vshpa[3].0,
        ]
    }
}

impl Default for SourceHighPositiveVoltageControl {
    fn default() -> Self {
        Self::DEFAULT
    }
}

impl WriteCommand for SourceHighPositiveVoltageControl {
    const OPCODE: Instr = Instr::VSHPCTRL;
    type Params = [u8; 4];
    fn params(&self) -> Self::Params {
        self.bytes()
    }
}

millivolt_value!(
    /// Source high positive voltage VSHPAx.
    SourceHighPositiveVoltage: 3_700..=6_200, zero = 3_700, step = 20
);

const _: () = assert!(matches!(
    SourceHighPositiveVoltageControl::DEFAULT.bytes(),
    [0x41, 0x41, 0x41, 0x41]
));
const _: () = assert!(matches!(
    SourceHighPositiveVoltageControl {
        vshpa: [
            SourceHighPositiveVoltage::from_mv(3_700),
            SourceHighPositiveVoltage::from_mv(6_200),
            SourceHighPositiveVoltage::from_mv(5_800),
            SourceHighPositiveVoltage::from_mv(5_820)
        ]
    }
    .bytes(),
    [0x00, 0x7D, 0x69, 0x6A]
));

/// `VSLPCTRL` (§8.2.12): source low positive voltage of the four source
/// voltage sets (see [`SourceVoltageSelect`]).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SourceLowPositiveVoltageControl {
    vslpa: [SourceLowPositiveVoltage; 4],
}

impl SourceLowPositiveVoltageControl {
    /// Register default: 1.0 V in all four sets.
    pub const DEFAULT: Self = Self {
        vslpa: [
            SourceLowPositiveVoltage::from_mv(1_000),
            SourceLowPositiveVoltage::from_mv(1_000),
            SourceLowPositiveVoltage::from_mv(1_000),
            SourceLowPositiveVoltage::from_mv(1_000),
        ],
    };

    /// `VSLPA1..=4`.
    #[must_use]
    pub const fn with_vslpa(mut self, v: [SourceLowPositiveVoltage; 4]) -> Self {
        self.vslpa = v;
        self
    }

    const fn bytes(&self) -> [u8; 4] {
        [
            self.vslpa[0].0,
            self.vslpa[1].0,
            self.vslpa[2].0,
            self.vslpa[3].0,
        ]
    }
}

impl Default for SourceLowPositiveVoltageControl {
    fn default() -> Self {
        Self::DEFAULT
    }
}

impl WriteCommand for SourceLowPositiveVoltageControl {
    const OPCODE: Instr = Instr::VSLPCTRL;
    type Params = [u8; 4];
    fn params(&self) -> Self::Params {
        self.bytes()
    }
}

millivolt_value!(
    /// Source low positive voltage VSLPAx.
    SourceLowPositiveVoltage: 0..=2_000, zero = 0, step = 20
);

const _: () = assert!(matches!(
    SourceLowPositiveVoltageControl::DEFAULT.bytes(),
    [0x32, 0x32, 0x32, 0x32]
));
const _: () = assert!(matches!(
    SourceLowPositiveVoltageControl {
        vslpa: [
            SourceLowPositiveVoltage::from_mv(0),
            SourceLowPositiveVoltage::from_mv(2_000),
            SourceLowPositiveVoltage::from_mv(500),
            SourceLowPositiveVoltage::from_mv(520)
        ]
    }
    .bytes(),
    [0x00, 0x64, 0x19, 0x1A]
));

/// `VSHNCTRL` (§8.2.13): source high negative voltage of the four source
/// voltage sets (see [`SourceVoltageSelect`]).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SourceHighNegativeVoltageControl {
    vshna: [SourceHighNegativeVoltage; 4],
}

impl SourceHighNegativeVoltageControl {
    /// Register default: -3.9 V in all four sets.
    pub const DEFAULT: Self = Self {
        vshna: [
            SourceHighNegativeVoltage::from_mv(-3_900),
            SourceHighNegativeVoltage::from_mv(-3_900),
            SourceHighNegativeVoltage::from_mv(-3_900),
            SourceHighNegativeVoltage::from_mv(-3_900),
        ],
    };

    /// `VSHNA1..=4`.
    #[must_use]
    pub const fn with_vshna(mut self, v: [SourceHighNegativeVoltage; 4]) -> Self {
        self.vshna = v;
        self
    }

    const fn bytes(&self) -> [u8; 4] {
        [
            self.vshna[0].0,
            self.vshna[1].0,
            self.vshna[2].0,
            self.vshna[3].0,
        ]
    }
}

impl Default for SourceHighNegativeVoltageControl {
    fn default() -> Self {
        Self::DEFAULT
    }
}

impl WriteCommand for SourceHighNegativeVoltageControl {
    const OPCODE: Instr = Instr::VSHNCTRL;
    type Params = [u8; 4];
    fn params(&self) -> Self::Params {
        self.bytes()
    }
}

millivolt_value!(
    /// Source high negative voltage VSHNAx.
    SourceHighNegativeVoltage: -5_200..=-2_500, zero = -2_500, step = 20
);

const _: () = assert!(matches!(
    SourceHighNegativeVoltageControl::DEFAULT.bytes(),
    [0x46, 0x46, 0x46, 0x46]
));
const _: () = assert!(matches!(
    SourceHighNegativeVoltageControl {
        vshna: [
            SourceHighNegativeVoltage::from_mv(-2_500),
            SourceHighNegativeVoltage::from_mv(-5_200),
            SourceHighNegativeVoltage::from_mv(-4_000),
            SourceHighNegativeVoltage::from_mv(-4_020),
        ]
    }
    .bytes(),
    [0x00, 0x87, 0x4B, 0x4C]
));

/// `VSLNCTRL` (§8.2.14): source low negative voltage of the four source
/// voltage sets (see [`SourceVoltageSelect`]).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SourceLowNegativeVoltageControl {
    vslna: [SourceLowNegativeVoltage; 4],
}

impl SourceLowNegativeVoltageControl {
    /// Register default: -0.4 V in all four sets.
    pub const DEFAULT: Self = Self {
        vslna: [
            SourceLowNegativeVoltage::from_mv(-400),
            SourceLowNegativeVoltage::from_mv(-400),
            SourceLowNegativeVoltage::from_mv(-400),
            SourceLowNegativeVoltage::from_mv(-400),
        ],
    };

    /// `VSLNA1..=4`.
    #[must_use]
    pub const fn with_vslna(mut self, v: [SourceLowNegativeVoltage; 4]) -> Self {
        self.vslna = v;
        self
    }

    const fn bytes(&self) -> [u8; 4] {
        [
            self.vslna[0].0,
            self.vslna[1].0,
            self.vslna[2].0,
            self.vslna[3].0,
        ]
    }
}

impl Default for SourceLowNegativeVoltageControl {
    fn default() -> Self {
        Self::DEFAULT
    }
}

impl WriteCommand for SourceLowNegativeVoltageControl {
    const OPCODE: Instr = Instr::VSLNCTRL;
    type Params = [u8; 4];
    fn params(&self) -> Self::Params {
        self.bytes()
    }
}

millivolt_value!(
    /// Source low negative voltage VSLNAx.
    SourceLowNegativeVoltage: -1_800..=1_000, zero = 1_000, step = 20
);

const _: () = assert!(matches!(
    SourceLowNegativeVoltageControl::DEFAULT.bytes(),
    [0x46, 0x46, 0x46, 0x46]
));
const _: () = assert!(matches!(
    SourceLowNegativeVoltageControl {
        vslna: [
            SourceLowNegativeVoltage::from_mv(1_000),
            SourceLowNegativeVoltage::from_mv(-1_800),
            SourceLowNegativeVoltage::from_mv(500),
            SourceLowNegativeVoltage::from_mv(480),
        ]
    }
    .bytes(),
    [0x00, 0x8C, 0x19, 0x1A]
));

/// `VSIKCTRL` (§8.2.15): source gamma voltages.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SourceGammaVoltageControl {
    vsip: VsipVoltage,
    vskp: VskpVoltage,
    vsin: VsinVoltage,
    vskn: VsknVoltage,
}

impl SourceGammaVoltageControl {
    /// Register default: VSIP 3.6 V, VSKP 2.6 V, VSIN -2.4 V, VSKN -1.4 V.
    pub const DEFAULT: Self = Self {
        vsip: VsipVoltage::from_mv(3_600),
        vskp: VskpVoltage::from_mv(2_600),
        vsin: VsinVoltage::from_mv(-2_400),
        vskn: VsknVoltage::from_mv(-1_400),
    };

    /// `VSIP[7:0]`: positive gamma voltage 1.
    #[must_use]
    pub const fn with_vsip(mut self, v: VsipVoltage) -> Self {
        self.vsip = v;
        self
    }

    /// `VSKP[7:0]`: positive gamma voltage 2.
    #[must_use]
    pub const fn with_vskp(mut self, v: VskpVoltage) -> Self {
        self.vskp = v;
        self
    }

    /// `VSIN[7:0]`: negative gamma voltage 1.
    #[must_use]
    pub const fn with_vsin(mut self, v: VsinVoltage) -> Self {
        self.vsin = v;
        self
    }

    /// `VSKN[7:0]`: negative gamma voltage 2.
    #[must_use]
    pub const fn with_vskn(mut self, v: VsknVoltage) -> Self {
        self.vskn = v;
        self
    }

    const fn bytes(&self) -> [u8; 4] {
        [self.vsip.0, self.vskp.0, self.vsin.0, self.vskn.0]
    }
}

impl Default for SourceGammaVoltageControl {
    fn default() -> Self {
        Self::DEFAULT
    }
}

impl WriteCommand for SourceGammaVoltageControl {
    const OPCODE: Instr = Instr::VSIKCTRL;
    type Params = [u8; 4];
    fn params(&self) -> Self::Params {
        self.bytes()
    }
}

millivolt_value!(
    /// Positive gamma voltage 1 (VSIP).
    VsipVoltage: 2_200..=5_000, zero = 2_200, step = 20
);
millivolt_value!(
    /// Positive gamma voltage 2 (VSKP).
    VskpVoltage: 1_200..=4_000, zero = 1_200, step = 20
);
millivolt_value!(
    /// Negative gamma voltage 1 (VSIN).
    VsinVoltage: -3_800..=-1_000, zero = -1_000, step = 20
);
millivolt_value!(
    /// Negative gamma voltage 2 (VSKN).
    VsknVoltage: -2_800..=0, zero = 0, step = 20
);

const _: () = assert!(matches!(
    SourceGammaVoltageControl::DEFAULT.bytes(),
    [0x46, 0x46, 0x46, 0x46]
));
const _: () = assert!(matches!(
    SourceGammaVoltageControl {
        vsip: VsipVoltage::from_mv(2_200),
        vskp: VskpVoltage::from_mv(1_200),
        vsin: VsinVoltage::from_mv(-1_000),
        vskn: VsknVoltage::from_mv(0),
    }
    .bytes(),
    [0x00, 0x00, 0x00, 0x00]
));
const _: () = assert!(matches!(
    SourceGammaVoltageControl::DEFAULT
        .with_vsip(VsipVoltage::from_mv(5_000))
        .with_vskp(VskpVoltage::from_mv(4_000))
        .with_vsin(VsinVoltage::from_mv(-3_800))
        .with_vskn(VsknVoltage::from_mv(-2_800))
        .bytes(),
    [0x8C, 0x8C, 0x8C, 0x8C]
));

/// `VSHLSEL` (§8.2.16): which of the four source voltage sets is used.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SourceVoltageSelect {
    sdhlsel: SourceVoltage,
}

impl SourceVoltageSelect {
    /// Register default: set 1.
    pub const DEFAULT: Self = Self {
        sdhlsel: SourceVoltage::Source1,
    };

    /// Uses the set `sdhlsel`.
    pub const fn new(sdhlsel: SourceVoltage) -> Self {
        Self { sdhlsel }
    }

    const fn bytes(&self) -> [u8; 1] {
        [self.sdhlsel as u8]
    }
}

impl Default for SourceVoltageSelect {
    fn default() -> Self {
        Self::DEFAULT
    }
}

impl WriteCommand for SourceVoltageSelect {
    const OPCODE: Instr = Instr::VSHLSEL;
    type Params = [u8; 1];
    fn params(&self) -> Self::Params {
        self.bytes()
    }
}

/// `VSHLSEL[1:0]`: a source voltage set.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SourceVoltage {
    /// VSHPA1/VSLPA1/VSHNA1/VSLNA1
    Source1 = 0,
    /// VSHPA2/VSLPA2/VSHNA2/VSLNA2
    Source2 = 1,
    /// VSHPA3/VSLPA3/VSHNA3/VSLNA3
    Source3 = 2,
    /// VSHPA4/VSLPA4/VSHNA4/VSLNA4
    Source4 = 3,
}

const _: () = assert!(matches!(SourceVoltageSelect::DEFAULT.bytes(), [0x00]));

/// `AUTOPWRCTRL` (§8.2.20): automatic power down on or off.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct AutoPowerDownControl {
    autopwr: AutoPowerDown,
}

impl AutoPowerDownControl {
    /// Register default `0xFF`: enabled.
    pub const DEFAULT: Self = Self {
        autopwr: AutoPowerDown::Enable,
    };

    /// `AUTOPWR` (D7) = `autopwr`.
    pub const fn new(autopwr: AutoPowerDown) -> Self {
        Self { autopwr }
    }

    #[allow(clippy::identity_op, reason = "readability")]
    const fn bytes(&self) -> [u8; 1] {
        [0xFF & !(1 << 7) | (self.autopwr as u8) << 7]
    }
}

impl Default for AutoPowerDownControl {
    fn default() -> Self {
        Self::DEFAULT
    }
}

impl WriteCommand for AutoPowerDownControl {
    const OPCODE: Instr = Instr::AUTOPWRCTRL;
    type Params = [u8; 1];
    fn params(&self) -> Self::Params {
        self.bytes()
    }
}

/// `AUTOPWR`: automatic power down.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AutoPowerDown {
    /// Off
    Disable = 0,
    /// On
    Enable = 1,
}

const _: () = assert!(matches!(AutoPowerDownControl::DEFAULT.bytes(), [0xFF]));

/// `BSTEN` (§8.2.21): booster on or off.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct BoosterControl {
    bsten: Booster,
}

impl BoosterControl {
    /// Register default `0x00`: booster off.
    pub const DEFAULT: Self = Self {
        bsten: Booster::Disable,
    };

    /// `BSTEN` (D0) = `bsten`.
    pub const fn new(bsten: Booster) -> Self {
        Self { bsten }
    }

    const fn bytes(&self) -> [u8; 1] {
        [self.bsten as u8]
    }
}

impl Default for BoosterControl {
    fn default() -> Self {
        Self::DEFAULT
    }
}

impl WriteCommand for BoosterControl {
    const OPCODE: Instr = Instr::BSTEN;
    type Params = [u8; 1];
    fn params(&self) -> Self::Params {
        self.bytes()
    }
}

/// `BSTEN`: booster.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Booster {
    /// Off
    Disable = 0,
    /// On
    Enable = 1,
}

const _: () = assert!(matches!(BoosterControl::DEFAULT.bytes(), [0x00]));

/// `OSCSET` (§8.2.23): oscillator on/off and the maximum HPM frame rate.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct OscSetting {
    oscen: OscEnable,
    oscsw: OscSwitch,
}

impl OscSetting {
    /// Register default `0x26, 0xE9`: oscillator off, 32 Hz maximum.
    pub const DEFAULT: Self = Self {
        oscen: OscEnable::Disable,
        oscsw: OscSwitch::HpmMax32Hz,
    };

    /// `OSCEN` (D7).
    #[must_use]
    pub const fn with_osc_en(mut self, v: OscEnable) -> Self {
        self.oscen = v;
        self
    }

    /// `OSCSW[2:0]` (D5, D2, D1).
    #[must_use]
    pub const fn with_osc_switch(mut self, v: OscSwitch) -> Self {
        self.oscsw = v;
        self
    }

    #[allow(clippy::identity_op, reason = "readability")]
    const fn bytes(&self) -> [u8; 2] {
        let sw = match self.oscsw {
            OscSwitch::HpmMax32Hz => 1 << 5 | 1 << 2 | 1 << 1,
            OscSwitch::HpmMax51Hz => 0,
        };
        [
            0x26 & !(1 << 7 | 1 << 5 | 0b11 << 1) | (self.oscen as u8) << 7 | sw,
            0xE9,
        ]
    }
}

impl Default for OscSetting {
    fn default() -> Self {
        Self::DEFAULT
    }
}

impl WriteCommand for OscSetting {
    const OPCODE: Instr = Instr::OSCSET;
    type Params = [u8; 2];
    fn params(&self) -> Self::Params {
        self.bytes()
    }
}

/// `OSCEN`: oscillator.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum OscEnable {
    /// Off
    Disable = 0,
    /// On
    Enable = 1,
}

/// OSCSW\[2:0\]: selects the maximum HPM frame rate.
/// The actual rate is this maximum at HFRA=1 and half of it at HFRA=0 (see FRCTRL).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum OscSwitch {
    /// OSCSW = 000
    HpmMax51Hz = 0b000,
    /// OSCSW = 111
    HpmMax32Hz = 0b111,
}

const _: () = assert!(matches!(OscSetting::DEFAULT.bytes(), [0x26, 0xE9]));