trinket-streamdeck/common/src/usb.rs

53 lines
1.5 KiB
Rust

use crate::device::Device;
static NAME: &[u8] = b"Streamdeck";
static AUTHOR: &[u8] = b"asonix";
static REPO: &[u8] = b"https://git.asonix.dog/asonix/trinket-streamdeck";
pub fn handle_input<D, const SN_LEN: usize, const EXTRAS_LEN: usize>(
device: &mut D,
input: &[u8; 32],
count: usize,
) -> Option<()>
where
D: Device<SN_LEN, EXTRAS_LEN>,
{
if matches(input, count, b"ident") {
let sn = device.serial_number();
write_length_delim(device, &sn)?;
} else if matches(input, count, b"name") {
write_length_delim(device, NAME)?;
} else if matches(input, count, b"repo") {
write_length_delim(device, REPO)?;
} else if matches(input, count, b"author") {
write_length_delim(device, AUTHOR)?;
} else if matches(input, count, b"reset") {
device.reset();
} else {
for (key, value) in D::extras() {
if matches(input, count, key.as_bytes()) {
write_length_delim(device, value.as_bytes())?;
return Some(());
}
}
write_length_delim(device, &input[..count])?;
}
Some(())
}
fn matches(input: &[u8], count: usize, bytes: &'static [u8]) -> bool {
count == bytes.len() && input.starts_with(bytes)
}
fn write_length_delim<D, const SN_LEN: usize, const EXTRAS_LEN: usize>(
device: &mut D,
bytes: &[u8],
) -> Option<()>
where
D: Device<SN_LEN, EXTRAS_LEN>,
{
device.write(&[bytes.len() as u8])?;
device.write(bytes)
}