use std::collections::VecDeque;
pub mod descriptor;
pub mod kbd;
pub mod mouse;
pub mod usage;
pub type ReportDescriptor = descriptor::Items;
pub trait Dev {
fn reset(&mut self);
fn report_descriptor(&self, report_id: u8) -> ReportDescriptor;
}
pub trait InputDev: Dev {
const IN_LEN: u8;
fn poll(&mut self) -> bool;
fn input(&self) -> &[u8];
}
pub trait OutputDev: Dev {
const OUT_LEN: u8;
fn set_output(&mut self, v: &[u8]);
fn output(&self) -> &[u8];
}
#[derive(Debug)]
struct InputBuf<const N: usize> {
v: [u8; N],
b: VecDeque<[u8; N]>,
}
impl<const N: usize> InputBuf<N> {
fn reset(&mut self) {
self.v.fill(0);
self.b.clear();
}
fn poll(&mut self) -> bool {
if let Some(v) = self.b.pop_front() {
self.v = v;
return true;
}
if self.v.into_iter().any(|v| v != 0) {
self.v.fill(0);
return true;
}
false
}
const fn input(&self) -> &[u8] {
&self.v
}
fn add_input(&mut self, v: [u8; N]) {
self.b.push_back(v);
}
fn last_input(&self) -> [u8; N] {
*self.b.back().unwrap_or(&self.v)
}
}
impl<const N: usize> Default for InputBuf<N> {
fn default() -> Self {
Self {
v: [0; N],
b: VecDeque::new(),
}
}
}