1use hidapi::{HidDevice, HidError};
2
3pub trait HidAdapter {
4 fn write(&self, data: &[u8]) -> Result<usize, HidError>;
5}
6
7impl HidAdapter for HidDevice {
8 fn write(&self, data: &[u8]) -> Result<usize, HidError> {
9 self.write(data)
10 }
11}
12
13pub const PAYLOAD_SIZE: usize = 32;
15
16pub(crate) struct DataPacket {
17 index: u8,
18 payload: [u8; PAYLOAD_SIZE - 2],
19}
20
21impl DataPacket {
22 pub fn to_bytes(&self) -> Vec<u8> {
23 let mut bytes = vec![1, self.index];
24 bytes.extend_from_slice(&self.payload);
25 bytes
26 }
27
28 pub fn send(&self, device: &dyn HidAdapter) -> Result<(), HidError> {
29 let bytes = self.to_bytes();
30
31 device.write(&bytes)?;
32
33 Ok(())
34 }
35
36 pub fn new(starting_index: u8, payload: [u8; PAYLOAD_SIZE - 2]) -> Self {
37 Self {
38 index: starting_index,
39 payload,
40 }
41 }
42}