Compilable version

This commit is contained in:
hodasemi 2023-04-12 13:08:29 +02:00
parent 510d910669
commit 4eac78708b
2 changed files with 32 additions and 46 deletions

View file

@ -42,9 +42,9 @@ async fn main(_spawner: Spawner) {
) )
.await; .await;
// let usb_fut = mouse_hid.run(); let usb_fut = MouseHID::run_usb().await;
const PIXEL: i8 = 2; const PIXEL: i8 = 100;
const WAIT_TIME: Duration = Duration::from_secs(1); const WAIT_TIME: Duration = Duration::from_secs(1);
// Do stuff with the class! // Do stuff with the class!
@ -56,14 +56,12 @@ async fn main(_spawner: Spawner) {
} }
}; };
// // Run everything concurrently. // Run everything concurrently.
// // If we had made everything `'static` above instead, we could do this using separate tasks instead. // If we had made everything `'static` above instead, we could do this using separate tasks instead.
// join(usb_fut, hid_fut).await; join(hid_fut, usb_fut).await;
hid_fut.await;
} }
async fn mouse_move(mouse_hid: &mut MouseHID<'_>, duration: Duration, v: i8) { async fn mouse_move(mouse_hid: &mut MouseHID<'static>, duration: Duration, v: i8) {
Timer::after(duration).await; Timer::after(duration).await;
let report = MouseReport { let report = MouseReport {

View file

@ -9,6 +9,14 @@ use embassy_usb::{Builder, Config, UsbDevice};
use usbd_hid::descriptor::{MouseReport, SerializedDescriptor}; use usbd_hid::descriptor::{MouseReport, SerializedDescriptor};
static mut DEVICE_DESCRIPTOR_BUFFER: [u8; 256] = [0; 256];
static mut CONFIG_DESCRIPTOR_BUFFER: [u8; 256] = [0; 256];
static mut BOS_DESCRIPTOR_BUFFER: [u8; 256] = [0; 256];
static mut CONTROL_BUFFER: [u8; 64] = [0; 64];
static mut STATE: Option<State<'static>> = None;
static mut USB: Option<UsbDevice<'static, Driver<'static, USB>>> = None;
static REQUEST_HANDLER: MyRequestHandler = MyRequestHandler {};
pub struct MouseConfig<'a> { pub struct MouseConfig<'a> {
pub vendor_id: u16, pub vendor_id: u16,
pub product_id: u16, pub product_id: u16,
@ -19,18 +27,10 @@ pub struct MouseConfig<'a> {
pub struct MouseHID<'d> { pub struct MouseHID<'d> {
writer: HidWriter<'d, Driver<'d, USB>, 5>, writer: HidWriter<'d, Driver<'d, USB>, 5>,
usb: UsbDevice<'d, Driver<'d, USB>>,
state: State<'d>,
device_descriptor: [u8; 256],
config_descriptor: [u8; 256],
bos_descriptor: [u8; 256],
control_buf: [u8; 64],
} }
impl<'d> MouseHID<'d> { impl MouseHID<'static> {
pub async fn new(usb: USB, mouse_config: MouseConfig<'_>) -> MouseHID<'d> { pub async fn new(usb: USB, mouse_config: MouseConfig<'static>) -> MouseHID<'static> {
// Create the driver, from the HAL. // Create the driver, from the HAL.
let irq = interrupt::take!(USBCTRL_IRQ); let irq = interrupt::take!(USBCTRL_IRQ);
let driver = Driver::new(usb, irq); let driver = Driver::new(usb, irq);
@ -42,52 +42,40 @@ impl<'d> MouseHID<'d> {
config.device_class = 0; config.device_class = 0;
config.device_protocol = 2; config.device_protocol = 2;
let mut device_descriptor = [0; 256]; unsafe {
let mut config_descriptor = [0; 256]; STATE = Some(State::new());
let mut bos_descriptor = [0; 256]; }
let mut control_buf = [0; 64];
let request_handler = MyRequestHandler {};
let mut state = State::new();
let mut builder = Builder::new( let mut builder = Builder::new(
driver, driver,
config, config,
&mut device_descriptor, unsafe { &mut DEVICE_DESCRIPTOR_BUFFER },
&mut config_descriptor, unsafe { &mut CONFIG_DESCRIPTOR_BUFFER },
&mut bos_descriptor, unsafe { &mut BOS_DESCRIPTOR_BUFFER },
&mut control_buf, unsafe { &mut CONTROL_BUFFER },
); );
// Create classes on the builder. // Create classes on the builder.
let config = embassy_usb::class::hid::Config { let config = embassy_usb::class::hid::Config {
report_descriptor: MouseReport::desc(), report_descriptor: MouseReport::desc(),
request_handler: Some(&request_handler), request_handler: Some(&REQUEST_HANDLER),
poll_ms: 60, poll_ms: 60,
max_packet_size: 8, max_packet_size: 8,
}; };
let writer = HidWriter::<_, 5>::new(&mut builder, &mut state, config); let writer =
HidWriter::<_, 5>::new(&mut builder, unsafe { STATE.as_mut().unwrap() }, config);
// Build the builder. // Build the builder.
let usb = builder.build(); unsafe {
USB = Some(builder.build());
};
MouseHID { MouseHID { writer }
writer,
usb,
state,
device_descriptor,
config_descriptor,
bos_descriptor,
control_buf,
}
} }
pub async fn run(&'d mut self) -> impl Future<Output = !> + 'd { pub async fn run_usb() -> impl Future<Output = !> {
self.usb.run() unsafe { USB.as_mut().unwrap().run() }
} }
pub async fn mouse_report(&mut self, report: MouseReport) { pub async fn mouse_report(&mut self, report: MouseReport) {