Split serial and mouse hid

This commit is contained in:
hodasemi 2023-04-16 14:19:09 +02:00
parent f0f10ebdea
commit 66baaf38f0
4 changed files with 87 additions and 15 deletions

8
.vscode/tasks.json vendored
View file

@ -8,6 +8,14 @@
"$rustc"
],
"label": "rust: cargo run"
},
{
"type": "cargo",
"command": "build",
"problemMatcher": [
"$rustc"
],
"label": "rust: cargo build"
}
]
}

View file

@ -20,3 +20,7 @@ defmt-rtt = "0.4"
cortex-m = { version = "0.7.6" }
cortex-m-rt = "0.7.0"
panic-probe = { version = "0.3", features = ["print-defmt"] }
[features]
serial = []
# default = ["serial"]

View file

@ -3,20 +3,31 @@
#![feature(type_alias_impl_trait)]
#![feature(never_type)]
#[cfg(not(feature = "serial"))]
mod mouse_hid;
mod mouse_sensor;
#[cfg(feature = "serial")]
mod usb_serial;
mod mouse_sensor;
use embassy_executor::Spawner;
use embassy_futures::join::join;
use embassy_rp::{
peripherals::USB,
spi::{CsPin, Instance},
};
#[cfg(not(feature = "serial"))]
use usbd_hid::descriptor::MouseReport;
use crate::{
mouse_hid::mouse_config::MouseConfig,
mouse_hid::MouseHID,
mouse_sensor::{MouseSensor, MouseSensorPins},
};
#[cfg(not(feature = "serial"))]
use crate::{mouse_hid::mouse_config::MouseConfig, mouse_hid::MouseHID};
#[cfg(feature = "serial")]
use crate::usb_serial::Serial;
use crate::mouse_sensor::{MouseSensor, MouseSensorPins};
use {defmt_rtt as _, panic_probe as _};
@ -24,14 +35,29 @@ use {defmt_rtt as _, panic_probe as _};
async fn main(_spawner: Spawner) {
let p = embassy_rp::init(Default::default());
let mut mouse_hid = MouseHID::new(p.USB, MouseConfig::new());
let mut mouse_sensor = MouseSensor::new(
let mouse_sensor = MouseSensor::new(
p.PIN_25,
MouseSensorPins::new(p.SPI0, p.PIN_2, p.PIN_3, p.PIN_4, p.PIN_5),
|_| (),
)
.await;
#[cfg(not(feature = "serial"))]
mouse_hid(p.USB, mouse_sensor).await;
#[cfg(feature = "serial")]
serial_hid(p.USB, mouse_sensor).await;
}
#[cfg(not(feature = "serial"))]
async fn mouse_hid<'d, L, T, CSP>(usb: USB, mut mouse_sensor: MouseSensor<'d, L, T, CSP>)
where
L: FnMut(&str) + 'd,
T: Instance,
CSP: CsPin<T> + 'd,
{
let mut mouse_hid = MouseHID::new(usb, MouseConfig::new());
let usb_future = MouseHID::run_usb().await;
// Do stuff with the class!
@ -46,12 +72,42 @@ async fn main(_spawner: Spawner) {
pan: 0,
};
#[cfg(not(feature = "serial"))]
mouse_hid.mouse_report(report).await;
}
}
};
// Run everything concurrently.
// If we had made everything `'static` above instead, we could do this using separate tasks instead.
join(hid_future, usb_future).await;
}
#[cfg(feature = "serial")]
async fn serial_hid<'d, L, T, CSP>(usb: USB, mut mouse_sensor: MouseSensor<'d, L, T, CSP>)
where
L: FnMut(&str) + 'd,
T: Instance,
CSP: CsPin<T> + 'd,
{
let mut serial = Serial::new(usb);
let usb_future = Serial::run_usb().await;
// Do stuff with the class!
let hid_future = async {
loop {
if let Some((x, y)) = mouse_sensor.read_movement().await {
// let report = MouseReport {
// buttons: 0,
// x,
// y,
// wheel: 0,
// pan: 0,
// };
serial.send_msg("test").await;
}
}
};
join(hid_future, usb_future).await;
}

View file

@ -4,12 +4,8 @@ use embassy_rp::usb::Driver;
use embassy_rp::{interrupt, peripherals::USB};
use embassy_usb::class::cdc_acm::{CdcAcmClass, State};
use embassy_usb::class::hid::{HidWriter, ReportId, RequestHandler};
use embassy_usb::control::OutResponse;
use embassy_usb::{Builder, Config, UsbDevice};
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];
@ -67,4 +63,12 @@ impl Serial<'static> {
Self { class }
}
pub async fn run_usb() -> impl Future<Output = !> {
unsafe { USB.as_mut().unwrap().run() }
}
pub async fn send_msg(&mut self, msg: &str) {
self.class.write_packet(msg.as_bytes()).await.unwrap_or(());
}
}