Skip to content

Commit

Permalink
Define our own KeyboardReport struct instead of using usbd-hid's
Browse files Browse the repository at this point in the history
  • Loading branch information
bschwind committed Jun 30, 2024
1 parent 39b9c56 commit fef8966
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
25 changes: 24 additions & 1 deletion firmware/src/key_scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use core::{convert::Infallible, ops::Deref};

use cortex_m::delay::Delay;
use embedded_hal::digital::InputPin;
use usbd_hid::descriptor::KeyboardReport;

use crate::{debounce::Debounce, key_codes::KeyCode};

Expand Down Expand Up @@ -49,6 +48,30 @@ impl<const NUM_ROWS: usize, const NUM_COLS: usize> KeyScan<NUM_ROWS, NUM_COLS> {
}
}

#[derive(Copy, Clone, PartialEq)]
pub struct KeyboardReport {
pub modifier: u8,
pub reserved: u8,
pub leds: u8,
pub keycodes: [u8; 6],
}

impl KeyboardReport {
pub fn as_raw_input(&self) -> [u8; 8] {
[
self.modifier,
0x0, // Reserved byte
// Keycodes
self.keycodes[0],
self.keycodes[1],
self.keycodes[2],
self.keycodes[3],
self.keycodes[4],
self.keycodes[5],
]
}
}

impl<const NUM_ROWS: usize, const NUM_COLS: usize> From<KeyScan<NUM_ROWS, NUM_COLS>>
for KeyboardReport
{
Expand Down
11 changes: 4 additions & 7 deletions firmware/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![no_main]
#![no_std]

use crate::key_scan::TRANSPOSED_NORMAL_LAYER_MAPPING;
use crate::key_scan::{KeyboardReport, TRANSPOSED_NORMAL_LAYER_MAPPING};
use cortex_m::prelude::_embedded_hal_timer_CountDown;
use usb_device::class::UsbClass;
mod debounce;
Expand All @@ -26,11 +26,8 @@ use rp2040_hal::{
Clock, Watchdog,
};
use usb_device::{bus::UsbBusAllocator, device::UsbDeviceBuilder, prelude::*};
use usbd_hid::{
descriptor::KeyboardReport,
hid_class::{
HIDClass, HidClassSettings, HidCountryCode, HidProtocol, HidSubClass, ProtocolModeConfig,
},
use usbd_hid::hid_class::{
HIDClass, HidClassSettings, HidCountryCode, HidProtocol, HidSubClass, ProtocolModeConfig,
};

use debounce::Debounce;
Expand Down Expand Up @@ -233,7 +230,7 @@ unsafe fn USBCTRL_IRQ() {
}

let report = critical_section::with(|cs| *KEYBOARD_REPORT.borrow_ref(cs));
if let Err(err) = usb_hid.push_input(&report) {
if let Err(err) = usb_hid.push_raw_input(&report.as_raw_input()) {
match err {
UsbError::WouldBlock => warn!("UsbError::WouldBlock"),
UsbError::ParseError => error!("UsbError::ParseError"),
Expand Down

0 comments on commit fef8966

Please sign in to comment.