Skip to content

Commit

Permalink
types/command: add writer
Browse files Browse the repository at this point in the history
  • Loading branch information
johnlettman committed Aug 16, 2024
1 parent 94a1afb commit 5ed58ed
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/types/command.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -50,3 +51,30 @@ impl BinRead for Command {
Self::read(reader)
}
}

impl BinWrite for Command {
type Args<'a> = ();

fn write<W: Write + Seek>(&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<W: Write + Seek>(
&self,
writer: &mut W,
_: Endian,
_: Self::Args<'_>,
) -> BinResult<()> {
self.write(writer)
}
}

0 comments on commit 5ed58ed

Please sign in to comment.