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
use super::{instruction_id, Instruction}; use crate::endian::{write_u16_le, write_u32_le}; #[derive(Debug, Clone)] pub struct Write<'a> { pub motor_id: u8, pub address: u16, pub data: &'a [u8], } #[derive(Debug, Clone)] pub struct WriteU8 { pub motor_id: u8, pub address: u16, pub data: u8, } #[derive(Debug, Clone)] pub struct WriteU16 { pub motor_id: u8, pub address: u16, pub data: u16, } #[derive(Debug, Clone)] pub struct WriteU32 { pub motor_id: u8, pub address: u16, pub data: u32, } impl<'a> Write<'a> { pub fn new(motor_id: u8, address: u16, data: &'a [u8]) -> Self { Self { motor_id, address, data } } } impl WriteU8 { pub fn new(motor_id: u8, address: u16, data: u8) -> Self { Self { motor_id, address, data } } } impl WriteU16 { pub fn new(motor_id: u8, address: u16, data: u16) -> Self { Self { motor_id, address, data } } } impl WriteU32 { pub fn new(motor_id: u8, address: u16, data: u32) -> Self { Self { motor_id, address, data } } } impl Instruction for Write<'_> { type Response = (); fn request_packet_id(&self) -> u8 { self.motor_id } fn request_instruction_id(&self) -> u8 { instruction_id::WRITE } fn request_parameters_len(&self) -> u16 { 2 + self.data.len() as u16 } fn encode_request_parameters(&self, buffer: &mut [u8]) { write_u16_le(&mut buffer[0..], self.address); buffer[2..].copy_from_slice(&self.data); } 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(()) } } impl Instruction for WriteU8 { type Response = (); fn request_packet_id(&self) -> u8 { self.motor_id } fn request_instruction_id(&self) -> u8 { instruction_id::WRITE } fn request_parameters_len(&self) -> u16 { 3 } fn encode_request_parameters(&self, buffer: &mut [u8]) { write_u16_le(&mut buffer[0..2], self.address); buffer[2] = self.data; } 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(()) } } impl Instruction for WriteU16 { type Response = (); fn request_packet_id(&self) -> u8 { self.motor_id } fn request_instruction_id(&self) -> u8 { instruction_id::WRITE } fn request_parameters_len(&self) -> u16 { 4 } fn encode_request_parameters(&self, buffer: &mut [u8]) { write_u16_le(&mut buffer[0..2], self.address); write_u16_le(&mut buffer[2..4], self.data); } 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(()) } } impl Instruction for WriteU32 { type Response = (); fn request_packet_id(&self) -> u8 { self.motor_id } fn request_instruction_id(&self) -> u8 { instruction_id::WRITE } fn request_parameters_len(&self) -> u16 { 6 } fn encode_request_parameters(&self, buffer: &mut [u8]) { write_u16_le(&mut buffer[0..2], self.address); write_u32_le(&mut buffer[2..6], self.data); } 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(()) } }