autd3_driver/firmware/operation/
debug.rs

1use std::{convert::Infallible, mem::size_of};
2
3use crate::{
4    firmware::{
5        fpga::DebugValue,
6        operation::{Operation, TypeTag},
7    },
8    geometry::Device,
9};
10
11use zerocopy::{Immutable, IntoBytes};
12
13#[repr(C, align(2))]
14#[derive(IntoBytes, Immutable)]
15struct DebugSetting {
16    tag: TypeTag,
17    __: [u8; 7],
18    value: [DebugValue; 4],
19}
20
21pub struct DebugSettingOp {
22    is_done: bool,
23    value: [DebugValue; 4],
24}
25
26impl DebugSettingOp {
27    pub(crate) const fn new(value: [DebugValue; 4]) -> Self {
28        Self {
29            is_done: false,
30            value,
31        }
32    }
33}
34
35impl Operation for DebugSettingOp {
36    type Error = Infallible;
37
38    fn pack(&mut self, _: &Device, tx: &mut [u8]) -> Result<usize, Self::Error> {
39        super::write_to_tx(
40            tx,
41            DebugSetting {
42                tag: TypeTag::Debug,
43                __: [0; 7],
44                value: self.value,
45            },
46        );
47
48        self.is_done = true;
49        Ok(size_of::<DebugSetting>())
50    }
51
52    fn required_size(&self, _: &Device) -> usize {
53        size_of::<DebugSetting>()
54    }
55
56    fn is_done(&self) -> bool {
57        self.is_done
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use crate::firmware::operation::tests::create_device;
64
65    use super::*;
66
67    const NUM_TRANS_IN_UNIT: u8 = 249;
68
69    #[test]
70    fn debug_op() {
71        const FRAME_SIZE: usize = size_of::<DebugSetting>();
72
73        let device = create_device(NUM_TRANS_IN_UNIT);
74        let mut tx = vec![0x00u8; FRAME_SIZE];
75
76        let mut op = DebugSettingOp::new([
77            DebugValue::new()
78                .with_tag(0x01)
79                .with_value(0x02030405060708),
80            DebugValue::new()
81                .with_tag(0x11)
82                .with_value(0x12131415161718),
83            DebugValue::new()
84                .with_tag(0x10)
85                .with_value(0x20304050607080),
86            DebugValue::new()
87                .with_tag(0x11)
88                .with_value(0x21314151617181),
89        ]);
90
91        assert_eq!(size_of::<DebugSetting>(), op.required_size(&device));
92        assert_eq!(Ok(size_of::<DebugSetting>()), op.pack(&device, &mut tx));
93        assert!(op.is_done());
94        assert_eq!(TypeTag::Debug as u8, tx[0]);
95        assert_eq!(0x08, tx[8]);
96        assert_eq!(0x07, tx[9]);
97        assert_eq!(0x06, tx[10]);
98        assert_eq!(0x05, tx[11]);
99        assert_eq!(0x04, tx[12]);
100        assert_eq!(0x03, tx[13]);
101        assert_eq!(0x02, tx[14]);
102        assert_eq!(0x01, tx[15]);
103        assert_eq!(0x18, tx[16]);
104        assert_eq!(0x17, tx[17]);
105        assert_eq!(0x16, tx[18]);
106        assert_eq!(0x15, tx[19]);
107        assert_eq!(0x14, tx[20]);
108        assert_eq!(0x13, tx[21]);
109        assert_eq!(0x12, tx[22]);
110        assert_eq!(0x11, tx[23]);
111        assert_eq!(0x80, tx[24]);
112        assert_eq!(0x70, tx[25]);
113        assert_eq!(0x60, tx[26]);
114        assert_eq!(0x50, tx[27]);
115        assert_eq!(0x40, tx[28]);
116        assert_eq!(0x30, tx[29]);
117        assert_eq!(0x20, tx[30]);
118        assert_eq!(0x10, tx[31]);
119        assert_eq!(0x81, tx[32]);
120        assert_eq!(0x71, tx[33]);
121        assert_eq!(0x61, tx[34]);
122        assert_eq!(0x51, tx[35]);
123        assert_eq!(0x41, tx[36]);
124        assert_eq!(0x31, tx[37]);
125        assert_eq!(0x21, tx[38]);
126        assert_eq!(0x11, tx[39]);
127    }
128}