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
use std::mem::size_of;

use crate::{
    error::AUTDInternalError,
    firmware::operation::{write_to_tx, Operation, TypeTag},
    geometry::Device,
};

#[repr(C, align(2))]
struct DebugSetting {
    tag: TypeTag,
    __pad: u8,
    ty: [u8; 4],
    value: [u16; 4],
}

pub struct DebugSettingOp {
    is_done: bool,
    ty: [u8; 4],
    value: [u16; 4],
}

impl DebugSettingOp {
    pub const fn new(ty: [u8; 4], value: [u16; 4]) -> Self {
        Self {
            is_done: false,
            ty,
            value,
        }
    }
}

impl Operation for DebugSettingOp {
    fn pack(&mut self, _: &Device, tx: &mut [u8]) -> Result<usize, AUTDInternalError> {
        write_to_tx(
            DebugSetting {
                tag: TypeTag::Debug,
                __pad: 0,
                ty: self.ty,
                value: self.value,
            },
            tx,
        );

        self.is_done = true;
        Ok(size_of::<DebugSetting>())
    }

    fn required_size(&self, _: &Device) -> usize {
        size_of::<DebugSetting>()
    }

    fn is_done(&self) -> bool {
        self.is_done
    }
}

#[cfg(test)]
mod tests {
    use crate::geometry::tests::create_device;

    use super::*;

    const NUM_TRANS_IN_UNIT: usize = 249;

    #[test]
    fn debug_op() {
        const FRAME_SIZE: usize = size_of::<DebugSetting>();

        let device = create_device(0, NUM_TRANS_IN_UNIT);
        let mut tx = vec![0x00u8; FRAME_SIZE];

        let mut op =
            DebugSettingOp::new([0x01, 0x02, 0x03, 0x04], [0x0605, 0x0807, 0x0a09, 0x0c0b]);

        assert_eq!(size_of::<DebugSetting>(), op.required_size(&device));
        assert_eq!(Ok(size_of::<DebugSetting>()), op.pack(&device, &mut tx));
        assert!(op.is_done());
        assert_eq!(TypeTag::Debug as u8, tx[0]);
        assert_eq!(0x01, tx[2]);
        assert_eq!(0x02, tx[3]);
        assert_eq!(0x03, tx[4]);
        assert_eq!(0x04, tx[5]);
        assert_eq!(0x05, tx[6]);
        assert_eq!(0x06, tx[7]);
        assert_eq!(0x07, tx[8]);
        assert_eq!(0x08, tx[9]);
        assert_eq!(0x09, tx[10]);
        assert_eq!(0x0a, tx[11]);
        assert_eq!(0x0b, tx[12]);
        assert_eq!(0x0c, tx[13]);
    }
}