use crate::device::bus::UsbBus;
use crate::device::class::hid::{HidDevice, HidSpec, hid_protocol, hid_subclass};
pub const KEYBOARD_REPORT_DESCRIPTOR: &[u8] = &[
0x05, 0x01, 0x09, 0x06, 0xA1, 0x01, 0x05, 0x07, 0x19, 0xE0, 0x29, 0xE7, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95, 0x08, 0x81, 0x02, 0x75, 0x08, 0x95, 0x01, 0x81, 0x01, 0x05, 0x08, 0x19, 0x01, 0x29, 0x05, 0x75, 0x01, 0x95, 0x05, 0x91, 0x02, 0x75, 0x03, 0x95, 0x01, 0x91, 0x01, 0x05, 0x07, 0x19, 0x00, 0x29, 0xFF, 0x15, 0x00, 0x26, 0xFF, 0x00, 0x75, 0x08, 0x95, 0x06, 0x81, 0x00, 0xC0, ];
pub mod modifier {
pub const LEFT_CTRL: u8 = 1 << 0;
pub const LEFT_SHIFT: u8 = 1 << 1;
pub const LEFT_ALT: u8 = 1 << 2;
pub const LEFT_GUI: u8 = 1 << 3;
pub const RIGHT_CTRL: u8 = 1 << 4;
pub const RIGHT_SHIFT: u8 = 1 << 5;
pub const RIGHT_ALT: u8 = 1 << 6;
pub const RIGHT_GUI: u8 = 1 << 7;
}
pub mod led {
pub const NUM_LOCK: u8 = 1 << 0;
pub const CAPS_LOCK: u8 = 1 << 1;
pub const SCROLL_LOCK: u8 = 1 << 2;
pub const COMPOSE: u8 = 1 << 3;
pub const KANA: u8 = 1 << 4;
}
#[derive(Clone, Copy, PartialEq, Eq, Default, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct KeyboardReport {
pub modifiers: u8,
pub keys: [u8; 6],
}
impl KeyboardReport {
pub fn to_bytes(self) -> [u8; 8] {
[
self.modifiers,
0,
self.keys[0],
self.keys[1],
self.keys[2],
self.keys[3],
self.keys[4],
self.keys[5],
]
}
}
pub struct Keyboard {
leds: u8,
}
impl Keyboard {
pub const fn new() -> Self {
Self { leds: 0 }
}
pub fn leds(&self) -> u8 {
self.leds
}
}
impl Default for Keyboard {
fn default() -> Self {
Self::new()
}
}
impl<B: UsbBus> HidDevice<B> for Keyboard {
fn spec(&self) -> HidSpec {
HidSpec::new(KEYBOARD_REPORT_DESCRIPTOR, 8)
.subclass(hid_subclass::BOOT)
.protocol(hid_protocol::KEYBOARD)
.out_report_size(1) .poll_interval_ms(10)
}
fn output_report(&mut self, _report_id: u8, data: &[u8]) {
self.leds = data.first().copied().unwrap_or(0);
}
fn reset(&mut self) {
self.leds = 0;
}
}