dynamixel2/instructions/
read.rs

1use super::instruction_id;
2use crate::endian::write_u16_le;
3use crate::{bus::StatusPacket, 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	/// Read an arbitrary number of bytes from multiple motors.
11	fn read_raw(&mut self, motor_id: u8, address: u16, count: u16) -> Result<StatusPacket<'_>, TransferError> {
12		let response = self.transfer_single(motor_id, instruction_id::READ, 4, count, |buffer| {
13			write_u16_le(&mut buffer[0..], address);
14			write_u16_le(&mut buffer[2..], count);
15		})?;
16		crate::error::InvalidParameterCount::check(response.parameters().len(), count.into()).map_err(crate::ReadError::from)?;
17		Ok(response)
18	}
19
20	/// Read an arbitrary number of bytes from a specific motor.
21	///
22	/// This function will not work correctly if the motor ID is set to [`packet_id::BROADCAST`][crate::instructions::packet_id::BROADCAST].
23	/// Use [`Self::sync_read`] to read from multiple motors with one command.
24	pub fn read(&mut self, motor_id: u8, address: u16, count: u16) -> Result<Response<Vec<u8>>, TransferError> {
25		let response = self.read_raw(motor_id, address, count)?;
26		Ok(response.into())
27	}
28
29	/// Read an 8 bit register from a specific motor.
30	///
31	/// This function will not work correctly if the motor ID is set to [`packet_id::BROADCAST`][crate::instructions::packet_id::BROADCAST].
32	/// Use [`Self::sync_read`] to read from multiple motors with one command.
33	pub fn read_u8(&mut self, motor_id: u8, address: u16) -> Result<Response<u8>, TransferError> {
34		let response = self.read_raw(motor_id, address, 1)?;
35		Ok(response.try_into()?)
36	}
37
38	/// Read 16 bit register from a specific motor.
39	///
40	/// This function will not work correctly if the motor ID is set to [`packet_id::BROADCAST`][crate::instructions::packet_id::BROADCAST].
41	/// Use [`Self::sync_read`] to read from multiple motors with one command.
42	pub fn read_u16(&mut self, motor_id: u8, address: u16) -> Result<Response<u16>, TransferError> {
43		let response = self.read_raw(motor_id, address, 2)?;
44		Ok(response.try_into()?)
45	}
46
47	/// Read 32 bit register from a specific motor.
48	///
49	/// This function will not work correctly if the motor ID is set to [`packet_id::BROADCAST`][crate::instructions::packet_id::BROADCAST].
50	/// Use [`Self::sync_read`] to read from multiple motors with one command.
51	pub fn read_u32(&mut self, motor_id: u8, address: u16) -> Result<Response<u32>, TransferError> {
52		let response = self.read_raw(motor_id, address, 4)?;
53		Ok(response.try_into()?)
54	}
55}