Skip to content

Commit a7d411e

Browse files
committed
refactor: move descriptor to hid mod
Signed-off-by: Haobo Gu <haobogu@outlook.com>
1 parent 48eb2aa commit a7d411e

13 files changed

Lines changed: 142 additions & 149 deletions

File tree

rmk/src/ble/ble_server.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ use super::device_info::DeviceConfigurationService;
66
#[cfg(feature = "host")]
77
use super::host_service::HostService;
88
use crate::channel::KEYBOARD_REPORT_CHANNEL;
9-
use crate::descriptor::{CompositeReport, CompositeReportType, KeyboardReport};
10-
use crate::hid::{HidError, HidWriterTrait, Report, RunnableHidWriter};
9+
use crate::hid::{
10+
CompositeReport, CompositeReportType, HidError, HidWriterTrait, KeyboardReport, Report, RunnableHidWriter,
11+
};
1112

1213
// Used for saving the CCCD table
1314
pub(crate) const CCCD_TABLE_SIZE: usize = _CCCD_TABLE_SIZE;

rmk/src/ble/host_service/vial.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ use usbd_hid::descriptor::{AsInputReport, SerializedDescriptor};
33

44
use crate::ble::Server;
55
use crate::ble::host_service::HOST_GUI_INPUT_CHANNEL;
6-
use crate::descriptor::ViaReport;
7-
use crate::hid::{HidError, HidReaderTrait, HidWriterTrait};
6+
use crate::hid::{HidError, HidReaderTrait, HidWriterTrait, ViaReport};
87

98
#[gatt_service(uuid = service::HUMAN_INTERFACE_DEVICE)]
109
pub(crate) struct VialService {

rmk/src/ble/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ use trouble_host::prelude::*;
1919
#[cfg(feature = "host")]
2020
use {crate::ble::host_service::BleHostServer, crate::keymap::KeyMap};
2121
#[cfg(all(feature = "host", not(feature = "_no_usb")))]
22-
use {crate::descriptor::ViaReport, crate::host::UsbHostReaderWriter};
22+
use {crate::hid::ViaReport, crate::host::UsbHostReaderWriter};
2323
#[cfg(not(feature = "_no_usb"))]
2424
use {
25-
crate::descriptor::{CompositeReport, KeyboardReport},
25+
crate::hid::{CompositeReport, KeyboardReport},
2626
crate::light::UsbLedReader,
2727
crate::state::get_connection_type,
2828
crate::usb::{USB_REMOTE_WAKEUP, UsbKeyboardWriter, add_usb_reader_writer, add_usb_writer, new_usb_builder},

rmk/src/descriptor.rs

Lines changed: 0 additions & 126 deletions
This file was deleted.

rmk/src/hid.rs

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,140 @@ use core::{future::Future, sync::atomic::Ordering};
33

44
use embassy_usb::class::hid::ReadError;
55
use embassy_usb::driver::EndpointError;
6+
use serde::Serialize;
7+
use usbd_hid::descriptor::generator_prelude::*;
68
use usbd_hid::descriptor::{AsInputReport, MediaKeyboardReport, MouseReport, SystemControlReport};
79

810
use crate::CONNECTION_STATE;
911
use crate::channel::KEYBOARD_REPORT_CHANNEL;
10-
use crate::descriptor::KeyboardReport;
1112
use crate::state::ConnectionState;
1213
#[cfg(not(feature = "_no_usb"))]
1314
use crate::usb::USB_REMOTE_WAKEUP;
1415

16+
/// KeyboardReport describes a report and its companion descriptor that can be
17+
/// used to send keyboard button presses to a host and receive the status of the
18+
/// keyboard LEDs.
19+
#[gen_hid_descriptor(
20+
(collection = APPLICATION, usage_page = GENERIC_DESKTOP, usage = KEYBOARD) = {
21+
(usage_page = KEYBOARD, usage_min = 0xE0, usage_max = 0xE7) = {
22+
#[packed_bits = 8] #[item_settings(data,variable,absolute)] modifier=input;
23+
};
24+
(logical_min = 0,) = {
25+
#[item_settings(constant,variable,absolute)] reserved=input;
26+
};
27+
(usage_page = LEDS, usage_min = 0x01, usage_max = 0x05) = {
28+
#[packed_bits = 5] #[item_settings(data,variable,absolute)] leds=output;
29+
};
30+
(usage_page = KEYBOARD, usage_min = 0x00, usage_max = 0xDD) = {
31+
#[item_settings(data,array,absolute)] keycodes=input;
32+
};
33+
}
34+
)]
35+
#[allow(dead_code)]
36+
#[derive(Default)]
37+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
38+
pub struct KeyboardReport {
39+
pub modifier: u8, // ModifierCombination
40+
pub reserved: u8,
41+
pub leds: u8, // LedIndicator
42+
pub keycodes: [u8; 6],
43+
}
44+
45+
#[gen_hid_descriptor(
46+
(collection = APPLICATION, usage_page = 0xFF60, usage = 0x61) = {
47+
(usage = 0x62, logical_min = 0x0) = {
48+
#[item_settings(data,variable,absolute)] input_data=input;
49+
};
50+
(usage = 0x63, logical_min = 0x0) = {
51+
#[item_settings(data,variable,absolute)] output_data=output;
52+
};
53+
}
54+
)]
55+
#[derive(Default)]
56+
pub struct ViaReport {
57+
pub(crate) input_data: [u8; 32],
58+
pub(crate) output_data: [u8; 32],
59+
}
60+
61+
/// Predefined report ids for composite hid report.
62+
/// Should be same with `#[gen_hid_descriptor]`
63+
/// DO NOT EDIT
64+
#[repr(u8)]
65+
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Serialize)]
66+
67+
pub enum CompositeReportType {
68+
#[default]
69+
None = 0x00,
70+
Mouse = 0x01,
71+
Media = 0x02,
72+
System = 0x03,
73+
}
74+
75+
impl CompositeReportType {
76+
fn from_u8(report_id: u8) -> Self {
77+
match report_id {
78+
0x01 => Self::Mouse,
79+
0x02 => Self::Media,
80+
0x03 => Self::System,
81+
_ => Self::None,
82+
}
83+
}
84+
}
85+
86+
/// A composite hid report which contains mouse, consumer, system reports.
87+
/// Report id is used to distinguish from them.
88+
#[gen_hid_descriptor(
89+
(collection = APPLICATION, usage_page = GENERIC_DESKTOP, usage = MOUSE) = {
90+
(collection = PHYSICAL, usage = POINTER) = {
91+
(report_id = 0x01,) = {
92+
(usage_page = BUTTON, usage_min = BUTTON_1, usage_max = BUTTON_8) = {
93+
#[packed_bits = 8] #[item_settings(data,variable,absolute)] buttons=input;
94+
};
95+
(usage_page = GENERIC_DESKTOP,) = {
96+
(usage = X,) = {
97+
#[item_settings(data,variable,relative)] x=input;
98+
};
99+
(usage = Y,) = {
100+
#[item_settings(data,variable,relative)] y=input;
101+
};
102+
(usage = WHEEL,) = {
103+
#[item_settings(data,variable,relative)] wheel=input;
104+
};
105+
};
106+
(usage_page = CONSUMER,) = {
107+
(usage = AC_PAN,) = {
108+
#[item_settings(data,variable,relative)] pan=input;
109+
};
110+
};
111+
};
112+
};
113+
},
114+
(collection = APPLICATION, usage_page = CONSUMER, usage = CONSUMER_CONTROL) = {
115+
(report_id = 0x02,) = {
116+
(usage_page = CONSUMER, usage_min = 0x00, usage_max = 0x514) = {
117+
#[item_settings(data,array,absolute,not_null)] media_usage_id=input;
118+
}
119+
};
120+
},
121+
(collection = APPLICATION, usage_page = GENERIC_DESKTOP, usage = SYSTEM_CONTROL) = {
122+
(report_id = 0x03,) = {
123+
(usage_min = 0x81, usage_max = 0xB7, logical_min = 1) = {
124+
#[item_settings(data,array,absolute,not_null)] system_usage_id=input;
125+
};
126+
};
127+
}
128+
)]
129+
#[derive(Default, Serialize)]
130+
pub struct CompositeReport {
131+
pub(crate) buttons: u8, // MouseButtons
132+
pub(crate) x: i8,
133+
pub(crate) y: i8,
134+
pub(crate) wheel: i8, // Scroll down (negative) or up (positive) this many units
135+
pub(crate) pan: i8, // Scroll left (negative) or right (positive) this many units
136+
pub(crate) media_usage_id: u16,
137+
pub(crate) system_usage_id: u8,
138+
}
139+
15140
#[derive(Debug, Clone)]
16141
pub enum Report {
17142
/// Normal keyboard hid report

rmk/src/host/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ pub(crate) use via::VialService as HostService;
99

1010
#[cfg(feature = "vial")]
1111
use crate::config::VialConfig;
12-
use crate::descriptor::ViaReport;
13-
use crate::hid::{HidReaderTrait, HidWriterTrait};
12+
use crate::hid::{HidReaderTrait, HidWriterTrait, ViaReport};
1413
use crate::keymap::KeyMap;
1514

1615
#[cfg(feature = "vial")]

rmk/src/host/via/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ use usbd_hid::descriptor::AsInputReport as _;
77
use vial::process_vial;
88

99
use crate::config::VialConfig;
10-
use crate::descriptor::ViaReport;
1110
use crate::event::KeyboardEventPos;
12-
use crate::hid::{HidError, HidReaderTrait, HidWriterTrait};
11+
use crate::hid::{HidError, HidReaderTrait, HidWriterTrait, ViaReport};
1312
use crate::host::via::keycode_convert::{from_via_keycode, to_via_keycode};
1413
use crate::keymap::KeyMap;
1514
use crate::state::ConnectionState;

rmk/src/host/via/vial.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rmk_types::morse::{DOUBLE_TAP, HOLD, HOLD_AFTER_TAP, Morse, MorseMode, TAP};
66
use rmk_types::protocol::vial::{SettingKey, VIAL_EP_SIZE, VIAL_PROTOCOL_VERSION, VialCommand, VialDynamic};
77

88
use crate::config::VialConfig;
9-
use crate::descriptor::ViaReport;
9+
use crate::hid::ViaReport;
1010
use crate::host::via::keycode_convert::{from_via_keycode, to_via_keycode};
1111
use crate::keymap::KeyMap;
1212
#[cfg(feature = "storage")]

rmk/src/keyboard.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ use usbd_hid::descriptor::{MediaKeyboardReport, SystemControlReport};
1919
use crate::channel::KEYBOARD_REPORT_CHANNEL;
2020
use crate::config::Hand;
2121
use crate::core_traits::Runnable;
22-
use crate::descriptor::KeyboardReport;
2322
#[cfg(all(feature = "split", feature = "_ble"))]
2423
use crate::event::ClearPeerEvent;
2524
use crate::event::{
2625
ActionEvent, KeyboardEvent, KeyboardEventPos, ModifierEvent, SubscribableEvent, publish_event, publish_event_async,
2726
};
28-
use crate::hid::Report;
27+
use crate::hid::{KeyboardReport, Report};
2928
use crate::keyboard::combo::Combo;
3029
use crate::keyboard::fork::ActiveFork;
3130
use crate::keyboard::held_buffer::{HeldBuffer, HeldKey, KeyState};

rmk/src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ use bt_hci::{
4141
controller::{ControllerCmdAsync, ControllerCmdSync},
4242
};
4343
use config::RmkConfig;
44-
#[cfg(not(feature = "_ble"))]
45-
use descriptor::{CompositeReport, KeyboardReport};
4644
pub use embassy_futures;
4745
#[cfg(not(any(cortex_m)))]
4846
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex as RawMutex;
@@ -53,6 +51,8 @@ use embassy_usb::driver::Driver;
5351
pub use futures;
5452
use futures::FutureExt;
5553
pub use heapless;
54+
#[cfg(not(feature = "_ble"))]
55+
use hid::{CompositeReport, KeyboardReport};
5656
use hid::{HidReaderTrait, RunnableHidWriter};
5757
use keymap::KeyMap;
5858
pub use keymap::KeymapData;
@@ -67,7 +67,7 @@ use state::CONNECTION_STATE;
6767
#[cfg(feature = "_ble")]
6868
pub use trouble_host::prelude::*;
6969
#[cfg(feature = "host")]
70-
use {crate::descriptor::ViaReport, crate::hid::HidWriterTrait, crate::host::run_host_communicate_task};
70+
use {crate::hid::HidWriterTrait, crate::hid::ViaReport, crate::host::run_host_communicate_task};
7171
#[cfg(all(not(feature = "_no_usb"), not(feature = "_ble")))]
7272
use {
7373
crate::light::UsbLedReader,
@@ -90,7 +90,6 @@ pub mod channel;
9090
pub mod config;
9191
pub mod core_traits;
9292
pub mod debounce;
93-
pub mod descriptor;
9493
#[cfg(feature = "display")]
9594
pub mod display;
9695
pub mod driver;

0 commit comments

Comments
 (0)