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
use super::{instruction_id, packet_id, Instruction};

#[derive(Debug, Clone)]
pub struct FactoryReset {
	pub motor_id: u8,
	pub kind: FactoryResetKind,
}

#[repr(u8)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum FactoryResetKind {
	ResetAll = 0xFF,
	KeepId = 0x01,
	KeepIdAndBaudRate = 0x02,
}

impl FactoryReset {
	pub fn unicast(motor_id: u8, kind: FactoryResetKind) -> Self {
		Self { motor_id, kind }
	}

	pub fn broadcast(kind: FactoryResetKind) -> Self {
		Self {
			motor_id: packet_id::BROADCAST,
			kind,
		}
	}
}

impl Instruction for FactoryReset {
	type Response = ();

	fn request_packet_id(&self) -> u8 {
		self.motor_id
	}

	fn request_instruction_id(&self) -> u8 {
		instruction_id::FACTORY_RESET
	}

	fn request_parameters_len(&self) -> u16 {
		1
	}

	fn encode_request_parameters(&self, buffer: &mut [u8]) {
		buffer[0] = self.kind as u8;
	}

	fn decode_response_parameters(&mut self, packet_id: u8, parameters: &[u8]) -> Result<Self::Response, crate::InvalidMessage> {
		crate::InvalidPacketId::check_ignore_broadcast(packet_id, self.motor_id)?;
		crate::InvalidParameterCount::check(parameters.len(), 0)?;

		Ok(())
	}
}