qmk-oled-api 0.1.0-alpha.2

A HID-wrapper API for QMK keyboards designed to make it significantly easier to draw to OLED screens remotely
Documentation
use hidapi::{HidDevice, HidError};

pub trait HidAdapter {
    fn write(&self, data: &[u8]) -> Result<usize, HidError>;
}

impl HidAdapter for HidDevice {
    fn write(&self, data: &[u8]) -> Result<usize, HidError> {
        self.write(data)
    }
}

/// The number of bytes in a payload. Typically this is 32.
pub const PAYLOAD_SIZE: usize = 32;

pub(crate) struct DataPacket {
    index: u8,
    payload: [u8; PAYLOAD_SIZE - 2],
}

impl DataPacket {
    pub fn to_bytes(&self) -> Vec<u8> {
        let mut bytes = vec![1, self.index];
        bytes.extend_from_slice(&self.payload);
        bytes
    }

    pub fn send(&self, device: &dyn HidAdapter) -> Result<(), HidError> {
        let bytes = self.to_bytes();

        device.write(&bytes)?;

        Ok(())
    }

    pub fn new(starting_index: u8, payload: [u8; PAYLOAD_SIZE - 2]) -> Self {
        Self {
            index: starting_index,
            payload,
        }
    }
}