Skip to content

Commit

Permalink
use Data trait with write methods
Browse files Browse the repository at this point in the history
  • Loading branch information
omelia-iliffe committed Jan 13, 2025
1 parent d4afa91 commit 66b76ed
Showing 1 changed file with 8 additions and 29 deletions.
37 changes: 8 additions & 29 deletions src/instructions/write.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::bus::endian::{write_u16_le, write_u32_le};
use crate::bus::endian::write_u16_le;
use crate::{Client, Response, TransferError};
use crate::bus::Data;
use super::{instruction_id, read_response_if_not_broadcast};

impl<SerialPort, Buffer> Client<SerialPort, Buffer>
Expand All @@ -11,46 +12,24 @@ where
///
/// You may specify [`crate::instructions::packet_id::BROADCAST`] as motor ID.
/// If you do, none of the devices will reply with a response, and this function will not wait for any.
pub fn write(&mut self, motor_id: u8, address: u16, data: &[u8]) -> Result<Response<()>, TransferError<SerialPort::Error>> {
pub fn write_bytes(&mut self, motor_id: u8, address: u16, data: &[u8]) -> Result<Response<()>, TransferError<SerialPort::Error>> {
self.write_instruction(motor_id, instruction_id::WRITE, 2 + data.len(), |buffer| {
write_u16_le(&mut buffer[0..], address);
buffer[2..].copy_from_slice(data)
})?;
Ok(read_response_if_not_broadcast(self, motor_id)?)
}

/// Write an 8 bit value to a specific motor.
/// Write value to a specific motor.
///
/// You may specify [`crate::instructions::packet_id::BROADCAST`] as motor ID.
/// If you do, none of the devices will reply with a response, and this function will not wait for any.
pub fn write_u8(&mut self, motor_id: u8, address: u16, value: u8) -> Result<Response<()>, TransferError<SerialPort::Error>> {
self.write_instruction(motor_id, instruction_id::WRITE, 2 + 1, |buffer| {
pub fn write<T: Data>(&mut self, motor_id: u8, address: u16, data: &T) -> Result<Response<()>, TransferError<SerialPort::Error>> {
self.write_instruction(motor_id, instruction_id::WRITE, 2 + T::ENCODED_SIZE as usize, |buffer| {
write_u16_le(&mut buffer[0..], address);
buffer[2] = value;
})?;
Ok(read_response_if_not_broadcast(self, motor_id)?)
}
// TODO: handle this unwrap
data.encode(&mut buffer[2..]).unwrap();

/// Write an 16 bit value to a specific motor.
///
/// You may specify [`crate::instructions::packet_id::BROADCAST`] as motor ID.
/// If you do, none of the devices will reply with a response, and this function will not wait for any.
pub fn write_u16(&mut self, motor_id: u8, address: u16, value: u16) -> Result<Response<()>, TransferError<SerialPort::Error>> {
self.write_instruction(motor_id, instruction_id::WRITE, 2 + 2, |buffer| {
write_u16_le(&mut buffer[0..], address);
write_u16_le(&mut buffer[2..], value);
})?;
Ok(read_response_if_not_broadcast(self, motor_id)?)
}

/// Write an 32 bit value to a specific motor.
///
/// You may specify [`crate::instructions::packet_id::BROADCAST`] as motor ID.
/// If you do, none of the devices will reply with a response, and this function will not wait for any.
pub fn write_u32(&mut self, motor_id: u8, address: u16, value: u32) -> Result<Response<()>, TransferError<SerialPort::Error>> {
self.write_instruction(motor_id, instruction_id::WRITE, 2 + 4, |buffer| {
write_u16_le(&mut buffer[0..], address);
write_u32_le(&mut buffer[2..], value);
})?;
Ok(read_response_if_not_broadcast(self, motor_id)?)
}
Expand Down

0 comments on commit 66b76ed

Please sign in to comment.