diff --git a/rmk/src/keyboard.rs b/rmk/src/keyboard.rs index a3d95741..136b0e39 100644 --- a/rmk/src/keyboard.rs +++ b/rmk/src/keyboard.rs @@ -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}; @@ -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, @@ -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], @@ -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), diff --git a/rmk/src/usb.rs b/rmk/src/usb.rs index d9573d34..703bede9 100644 --- a/rmk/src/usb.rs +++ b/rmk/src/usb.rs @@ -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); @@ -35,7 +35,7 @@ impl> 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(); @@ -76,7 +76,7 @@ impl> 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, diff --git a/rmk/src/usb/descriptor.rs b/rmk/src/usb/descriptor.rs index 9187fa12..ea40ef58 100644 --- a/rmk/src/usb/descriptor.rs +++ b/rmk/src/usb/descriptor.rs @@ -1,3 +1,4 @@ +use serde::ser::SerializeStruct; use usbd_hid::descriptor::generator_prelude::*; #[gen_hid_descriptor( @@ -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(&self, serializer: S) -> Result + 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 {}