diff --git a/.vscode/tasks.json b/.vscode/tasks.json index ffef723..215ef3d 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -8,6 +8,14 @@ "$rustc" ], "label": "rust: cargo run" + }, + { + "type": "cargo", + "command": "build", + "problemMatcher": [ + "$rustc" + ], + "label": "rust: cargo build" } ] } \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 9a84e3c..1f0888b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,4 +19,8 @@ defmt-rtt = "0.4" cortex-m = { version = "0.7.6" } cortex-m-rt = "0.7.0" -panic-probe = { version = "0.3", features = ["print-defmt"] } \ No newline at end of file +panic-probe = { version = "0.3", features = ["print-defmt"] } + +[features] +serial = [] +# default = ["serial"] \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 8e18822..fe657e9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 + '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 + '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; } diff --git a/src/usb_serial/mod.rs b/src/usb_serial/mod.rs index 71e84a5..2edbaad 100644 --- a/src/usb_serial/mod.rs +++ b/src/usb_serial/mod.rs @@ -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 { + 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(()); + } }