diff --git a/src/types/command.rs b/src/types/command.rs index 3269b31..235ee6e 100644 --- a/src/types/command.rs +++ b/src/types/command.rs @@ -1,8 +1,9 @@ -use crate::types::util::primitive::read_u8_bits; +use crate::types::util::primitive::{read_u8_bits, write_u8_bits}; use crate::types::{ProfilePointDetection, StepDirection}; +use binrw::__private::Required; use binrw::meta::{EndianKind, ReadEndian, WriteEndian}; -use binrw::{BinRead, BinResult, Endian}; -use std::io::{Read, Seek}; +use binrw::{BinRead, BinResult, BinWrite, Endian}; +use std::io::{Read, Seek, Write}; const MASK_STEP_DIRECTION: u8 = 0b0100_0000; const SHIFT_STEP_DIRECTION: usize = 6; @@ -50,3 +51,30 @@ impl BinRead for Command { Self::read(reader) } } + +impl BinWrite for Command { + type Args<'a> = (); + + fn write(&self, writer: &mut W) -> BinResult<()> + where + Self: WriteEndian, + { + let mut raw: u8 = 0; + let pos = writer.stream_position()?; + + raw |= write_u8_bits(self.profile_point_detection, MASK_PROFILE_POINT_DETECTION, 0, pos)?; + raw |= write_u8_bits(self.step_direction, MASK_STEP_DIRECTION, SHIFT_STEP_DIRECTION, pos)?; + + raw.write(writer) + } + + #[inline] + fn write_options( + &self, + writer: &mut W, + _: Endian, + _: Self::Args<'_>, + ) -> BinResult<()> { + self.write(writer) + } +}