1use crate::{
2 impl_aux_ops, impl_message_ops, len::SOFT_RESET, std::fmt, AuxCommand, AuxCommandOps,
3 MessageOps, MessageType,
4};
5
6pub mod index {
7 pub const DATA: usize = 3;
8 pub const DATA_END: usize = DATA + 2;
9}
10
11#[repr(C)]
26#[derive(Clone, Copy, Debug, Default, PartialEq)]
27pub struct SoftReset {
28 buf: [u8; SOFT_RESET],
29}
30
31impl SoftReset {
32 pub fn new() -> Self {
34 let mut message = Self {
35 buf: [0u8; SOFT_RESET],
36 };
37
38 message.init();
39 message.set_message_type(MessageType::AuxCommand);
40 message.set_aux_command(AuxCommand::SoftReset);
41 message.set_reset_data();
42
43 message
44 }
45
46 fn set_reset_data(&mut self) {
47 self.buf[index::DATA..index::DATA_END].copy_from_slice([0x7f, 0x7f].as_ref());
48 }
49}
50
51impl_message_ops!(SoftReset);
52impl_aux_ops!(SoftReset);
53
54impl fmt::Display for SoftReset {
55 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56 write!(f, "{{")?;
57 write!(f, r#""acknak": {}, "#, self.acknak())?;
58 write!(f, r#""device_type": {}, "#, self.device_type())?;
59 write!(f, r#""message_type": {}, "#, self.message_type())?;
60 write!(f, r#""aux_command": {}"#, self.aux_command())?;
61 write!(f, "}}")
62 }
63}
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68 use crate::Result;
69
70 #[test]
71 #[rustfmt::skip]
72 fn test_soft_reset_from_bytes() -> Result<()> {
73 let msg_bytes = [
74 0x02, 0x08, 0x60,
76 0x7f, 0x7f,
78 0x7f,
80 0x03, 0x17,
82 ];
83
84 let mut msg = SoftReset::new();
85 msg.from_buf(msg_bytes.as_ref())?;
86
87 assert_eq!(msg.message_type(), MessageType::AuxCommand);
88 assert_eq!(msg.aux_command(), AuxCommand::SoftReset);
89
90 Ok(())
91 }
92}