embassy/examples/nrf/src/bin/usb_hid_mouse.rs

130 lines
3.6 KiB
Rust
Raw Normal View History

2022-03-29 19:18:43 +00:00
#![no_std]
#![no_main]
#![feature(generic_associated_types)]
#![feature(type_alias_impl_trait)]
use core::mem;
use defmt::*;
use embassy::executor::Spawner;
use embassy::time::{Duration, Timer};
use embassy_nrf::interrupt;
use embassy_nrf::pac;
use embassy_nrf::usb::Driver;
use embassy_nrf::Peripherals;
2022-03-31 15:25:01 +00:00
use embassy_usb::control::OutResponse;
2022-03-29 19:18:43 +00:00
use embassy_usb::{Config, UsbDeviceBuilder};
use embassy_usb_hid::{HidClass, ReportId, RequestHandler, State};
use futures::future::join;
use usbd_hid::descriptor::{MouseReport, SerializedDescriptor};
use defmt_rtt as _; // global logger
use panic_probe as _;
2022-03-29 19:18:43 +00:00
#[embassy::main]
async fn main(_spawner: Spawner, p: Peripherals) {
let clock: pac::CLOCK = unsafe { mem::transmute(()) };
let power: pac::POWER = unsafe { mem::transmute(()) };
info!("Enabling ext hfosc...");
clock.tasks_hfclkstart.write(|w| unsafe { w.bits(1) });
while clock.events_hfclkstarted.read().bits() != 1 {}
info!("Waiting for vbus...");
while !power.usbregstatus.read().vbusdetect().is_vbus_present() {}
info!("vbus OK");
// Create the driver, from the HAL.
let irq = interrupt::take!(USBD);
let driver = Driver::new(p.USBD, irq);
// Create embassy-usb Config
let mut config = Config::new(0xc0de, 0xcafe);
config.manufacturer = Some("Tactile Engineering");
config.product = Some("Testy");
config.serial_number = Some("12345678");
config.max_power = 100;
// Create embassy-usb DeviceBuilder using the driver and config.
// It needs some buffers for building the descriptors.
let mut device_descriptor = [0; 256];
let mut config_descriptor = [0; 256];
let mut bos_descriptor = [0; 256];
let mut control_buf = [0; 16];
let request_handler = MyRequestHandler {};
2022-04-02 15:58:01 +00:00
let mut control = State::<5, 0>::new();
2022-03-29 19:18:43 +00:00
let mut builder = UsbDeviceBuilder::new(
driver,
config,
&mut device_descriptor,
&mut config_descriptor,
&mut bos_descriptor,
&mut control_buf,
);
// Create classes on the builder.
let mut hid = HidClass::new(
2022-03-29 19:18:43 +00:00
&mut builder,
2022-04-02 15:58:01 +00:00
&mut control,
2022-03-29 19:18:43 +00:00
MouseReport::desc(),
Some(&request_handler),
60,
8,
2022-03-29 19:18:43 +00:00
);
// Build the builder.
let mut usb = builder.build().await;
2022-03-29 19:18:43 +00:00
// Run the USB device.
let usb_fut = usb.run();
// Do stuff with the class!
let hid_fut = async {
let mut y: i8 = 5;
2022-03-29 19:18:43 +00:00
loop {
Timer::after(Duration::from_millis(500)).await;
y = -y;
let report = MouseReport {
buttons: 0,
x: 0,
y,
wheel: 0,
pan: 0,
};
match hid.input().serialize(&report).await {
Ok(()) => {}
Err(e) => warn!("Failed to send report: {:?}", e),
}
2022-03-29 19:18:43 +00:00
}
};
// Run everything concurrently.
// If we had made everything `'static` above instead, we could do this using separate tasks instead.
join(usb_fut, hid_fut).await;
}
struct MyRequestHandler {}
impl RequestHandler for MyRequestHandler {
fn get_report(&self, id: ReportId, _buf: &mut [u8]) -> Option<usize> {
info!("Get report for {:?}", id);
None
}
2022-03-31 15:25:01 +00:00
fn set_report(&self, id: ReportId, data: &[u8]) -> OutResponse {
info!("Set report for {:?}: {=[u8]}", id, data);
OutResponse::Accepted
}
2022-03-29 19:18:43 +00:00
fn set_idle(&self, id: Option<ReportId>, dur: Duration) {
info!("Set idle rate for {:?} to {:?}", id, dur);
}
fn get_idle(&self, id: Option<ReportId>) -> Option<Duration> {
info!("Get idle rate for {:?}", id);
None
}
}