dynamixel2/instructions/
write.rs

1use super::{instruction_id, read_response_if_not_broadcast};
2use crate::endian::{write_u16_le, write_u32_le};
3use crate::{Bus, Response, TransferError};
4
5impl<ReadBuffer, WriteBuffer> Bus<ReadBuffer, WriteBuffer>
6where
7	ReadBuffer: AsRef<[u8]> + AsMut<[u8]>,
8	WriteBuffer: AsRef<[u8]> + AsMut<[u8]>,
9{
10	/// Write an arbitrary number of bytes to a specific motor.
11	///
12	/// You may specify [`crate::instructions::packet_id::BROADCAST`] as motor ID.
13	/// If you do, none of the devices will reply with a response, and this function will not wait for any.
14	pub fn write(&mut self, motor_id: u8, address: u16, data: &[u8]) -> Result<Response<()>, TransferError> {
15		self.write_instruction(motor_id, instruction_id::WRITE, 2 + data.len(), |buffer| {
16			write_u16_le(&mut buffer[0..], address);
17			buffer[2..].copy_from_slice(data)
18		})?;
19		Ok(read_response_if_not_broadcast(self, motor_id)?)
20	}
21
22	/// Write an 8 bit value to a specific motor.
23	///
24	/// You may specify [`crate::instructions::packet_id::BROADCAST`] as motor ID.
25	/// If you do, none of the devices will reply with a response, and this function will not wait for any.
26	pub fn write_u8(&mut self, motor_id: u8, address: u16, value: u8) -> Result<Response<()>, TransferError> {
27		self.write_instruction(motor_id, instruction_id::WRITE, 2 + 1, |buffer| {
28			write_u16_le(&mut buffer[0..], address);
29			buffer[2] = value;
30		})?;
31		Ok(read_response_if_not_broadcast(self, motor_id)?)
32	}
33
34	/// Write an 16 bit value to a specific motor.
35	///
36	/// You may specify [`crate::instructions::packet_id::BROADCAST`] as motor ID.
37	/// If you do, none of the devices will reply with a response, and this function will not wait for any.
38	pub fn write_u16(&mut self, motor_id: u8, address: u16, value: u16) -> Result<Response<()>, TransferError> {
39		self.write_instruction(motor_id, instruction_id::WRITE, 2 + 2, |buffer| {
40			write_u16_le(&mut buffer[0..], address);
41			write_u16_le(&mut buffer[2..], value);
42		})?;
43		Ok(read_response_if_not_broadcast(self, motor_id)?)
44	}
45
46	/// Write an 32 bit value to a specific motor.
47	///
48	/// You may specify [`crate::instructions::packet_id::BROADCAST`] as motor ID.
49	/// If you do, none of the devices will reply with a response, and this function will not wait for any.
50	pub fn write_u32(&mut self, motor_id: u8, address: u16, value: u32) -> Result<Response<()>, TransferError> {
51		self.write_instruction(motor_id, instruction_id::WRITE, 2 + 4, |buffer| {
52			write_u16_le(&mut buffer[0..], address);
53			write_u32_le(&mut buffer[2..], value);
54		})?;
55		Ok(read_response_if_not_broadcast(self, motor_id)?)
56	}
57}