Skip to content

Commit

Permalink
wip: composite hid report
Browse files Browse the repository at this point in the history
Signed-off-by: HaoboGu <[email protected]>
  • Loading branch information
HaoboGu committed Jan 31, 2024
1 parent c2020ec commit c2a3852
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 43 deletions.
9 changes: 6 additions & 3 deletions rmk/src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
keycode::{KeyCode, ModifierCombination},
keymap::KeyMap,
matrix::{KeyState, Matrix},
usb::descriptor::ViaReport,
usb::descriptor::{MyKeyboardReport, ViaReport},
};
use core::{cell::RefCell, convert::Infallible};
use defmt::{error, warn};
Expand Down Expand Up @@ -75,7 +75,7 @@ pub(crate) struct Keyboard<
report: KeyboardReport,

/// Media internal report
media_report: MediaKeyboardReport,
media_report: MyKeyboardReport,

/// System control internal report
system_control_report: SystemControlReport,
Expand Down Expand Up @@ -119,7 +119,7 @@ impl<
leds: 0,
keycodes: [0; 6],
},
media_report: MediaKeyboardReport { usage_id: 0 },
media_report: MyKeyboardReport::default(),
system_control_report: SystemControlReport { usage_id: 0 },
via_report: ViaReport {
input_data: [0; 32],
Expand Down Expand Up @@ -198,6 +198,9 @@ impl<
hid_interface: &mut HidReaderWriter<'d, D, 1, 8>,
) {
if self.need_send_consumer_control_report {
let mut buf: [u8; 9] = [0; 9];
// Report id for media(consumer)
buf[0] = 0x02;
match hid_interface.write_serialize(&self.media_report).await {
Ok(()) => {}
Err(e) => error!("Send media(consumer control) report error: {}", e),
Expand Down
8 changes: 4 additions & 4 deletions rmk/src/usb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use embassy_usb::{
Builder, Handler, UsbDevice,
};
use static_cell::StaticCell;
use usbd_hid::descriptor::{KeyboardReport, MediaKeyboardReport, SerializedDescriptor};
use usbd_hid::descriptor::{KeyboardReport, SerializedDescriptor};

pub(crate) mod descriptor;

use crate::{keyboard::KeyboardUsbConfig, usb::descriptor::ViaReport};
use crate::{keyboard::KeyboardUsbConfig, usb::descriptor::{MyKeyboardReport, ViaReport}};

static SUSPENDED: AtomicBool = AtomicBool::new(false);

Expand All @@ -35,7 +35,7 @@ impl<D: Driver<'static>> KeyboardUsbDevice<'static, D> {
usb_config.manufacturer = keyboard_config.manufacturer;
usb_config.product = keyboard_config.product_name;
usb_config.serial_number = keyboard_config.serial_number;
usb_config.max_power = 499;
usb_config.max_power = 450;

// Create embassy-usb DeviceBuilder using the driver and config.
static DEVICE_DESC: StaticCell<[u8; 256]> = StaticCell::new();
Expand Down Expand Up @@ -76,7 +76,7 @@ impl<D: Driver<'static>> KeyboardUsbDevice<'static, D> {
);

let other_hid_config = Config {
report_descriptor: MediaKeyboardReport::desc(),
report_descriptor: MyKeyboardReport::desc(),
request_handler: Some(&request_handler),
poll_ms: 60,
max_packet_size: 64,
Expand Down
86 changes: 50 additions & 36 deletions rmk/src/usb/descriptor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use serde::ser::SerializeStruct;
use usbd_hid::descriptor::generator_prelude::*;

#[gen_hid_descriptor(
Expand All @@ -19,41 +20,54 @@ pub(crate) struct ViaReport {
// KeyboardReport describes a report and its companion descriptor that can be
// used to send keyboard button presses to a host and receive the status of the
// keyboard LEDs.
// #[gen_hid_descriptor(
// (report_id = 0x01, collection = APPLICATION, usage_page = GENERIC_DESKTOP, usage = KEYBOARD) = {
// (usage_page = KEYBOARD, usage_min = 0xE0, usage_max = 0xE7) = {
// #[packed_bits 8] #[item_settings data,variable,absolute] modifier=input;
// };
// (usage_min = 0x00, usage_max = 0xFF) = {
// #[item_settings constant,variable,absolute] reserved=input;
// };
// (usage_page = LEDS, usage_min = 0x01, usage_max = 0x05) = {
// #[packed_bits 5] #[item_settings data,variable,absolute] leds=output;
// };
// (usage_page = KEYBOARD, usage_min = 0x00, usage_max = 0xDD) = {
// #[item_settings data,array,absolute] keycodes=input;
// };
// },
// (report_id = 0x02, collection = APPLICATION, usage_page = CONSUMER, usage = CONSUMER_CONTROL) = {
// (usage_page = CONSUMER, usage_min = 0x00, usage_max = 0x514) = {
// #[item_settings data,array,absolute,not_null] usage_id=input;
// };
// },
// (report_id = 0x03, collection = APPLICATION, usage_page = GENERIC_DESKTOP, usage = SYSTEM_CONTROL) = {
// (usage_min = 0x81, usage_max = 0xB7, logical_min = 1) = {
// #[item_settings data,array,absolute,not_null] usage_id=input;
// };
// }
// )]
// #[allow(dead_code)]
// pub struct MyKeyboardReport {
// pub(crate) modifier: u8,
// pub(crate) reserved: u8,
// pub(crate) leds: u8,
// pub(crate) keycodes: [u8; 6],
// pub(crate) usage_id: u16,
// }
#[gen_hid_descriptor(
(report_id = 0x01, collection = APPLICATION, usage_page = GENERIC_DESKTOP, usage = KEYBOARD) = {
(usage_page = KEYBOARD, usage_min = 0xE0, usage_max = 0xE7) = {
#[packed_bits 8] #[item_settings data,variable,absolute] modifier=input;
};
(usage_min = 0x00, usage_max = 0xFF) = {
#[item_settings constant,variable,absolute] reserved=input;
};
(usage_page = LEDS, usage_min = 0x01, usage_max = 0x05) = {
#[packed_bits 5] #[item_settings data,variable,absolute] leds=output;
};
(usage_page = KEYBOARD, usage_min = 0x00, usage_max = 0xDD) = {
#[item_settings data,array,absolute] keycodes=input;
};
},
(report_id = 0x02, collection = APPLICATION, usage_page = CONSUMER, usage = CONSUMER_CONTROL) = {
(usage_page = CONSUMER, usage_min = 0x00, usage_max = 0x514) = {
#[item_settings data,array,absolute,not_null] usage_id=input;
};
},
(report_id = 0x03, collection = APPLICATION, usage_page = GENERIC_DESKTOP, usage = SYSTEM_CONTROL) = {
(usage_min = 0x81, usage_max = 0xB7, logical_min = 1) = {
#[item_settings data,array,absolute,not_null] usage_id=input;
};
}
)]
#[allow(dead_code)]
#[derive(Default)]
pub struct MyKeyboardReport {
pub(crate) modifier: u8,
pub(crate) reserved: u8,
pub(crate) leds: u8,
pub(crate) keycodes: [u8; 6],
pub(crate) usage_id: u16,
}

// impl AsInputReport for MyKeyboardReport {
impl MyKeyboardReport {
fn serialize_to_media<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut s = serializer.serialize_struct("OtherKeyboardReport", 2)?;
s.serialize_field("name", &self.leds)?;
// s.serialize_field("age", &self.age)?;
// s.serialize_field("phones", &self.phones)?;
s.end()
// serializer.serialize_bool(true)
}
}

// }
// impl AsInputReport for MyKeyboardReport {}

0 comments on commit c2a3852

Please sign in to comment.