diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..a9a1899 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,25 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "type": "cargo", + "command": "run", + "problemMatcher": [ + "$rustc" + ], + "label": "rust: Install to Pico (Mouse HID)" + }, + { + "type": "cargo", + "command": "run", + "args": [ + "--features", + "serial" + ], + "problemMatcher": [ + "$rustc" + ], + "label": "rust: Install to Pico (Serial HID)" + } + ] +} \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index be28cea..d694ba2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,3 +18,6 @@ embedded-hal = "0.2.7" fugit = "0.3.6" numtoa = "0.2.4" usbd-serial = "0.1.1" + +[features] +serial = [] diff --git a/src/main.rs b/src/main.rs index 2f7f255..a7a9521 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,19 @@ #![no_std] #![no_main] -extern crate alloc; - +#[cfg(features = "serial")] mod custom_hid; + +#[cfg(not(features = "serial"))] mod mouse_hid; + mod mouse_sensor; mod serial; -use alloc::boxed::Box; +#[cfg(features = "serial")] use custom_hid::CustomHID; + +#[cfg(not(features = "serial"))] use mouse_hid::MouseHID; // Ensure we halt the program on panic (if we don't mention this crate it won't @@ -26,8 +30,6 @@ use bsp::{ use crate::mouse_sensor::MouseSensor; -const SERIAL: bool = true; - pub trait MouseInfoSender { fn send(&mut self, x: i8, y: i8, buttons: u8, wheel: i8, pan: i8); } @@ -56,23 +58,23 @@ fn main() -> ! { let peripheral_clock = clocks.peripheral_clock.freq(); - let mut hid: Box = if SERIAL { - Box::new(CustomHID::new( - pac.USBCTRL_REGS, - pac.USBCTRL_DPRAM, - &mut pac.RESETS, - clocks, - 10, - )) - } else { - Box::new(MouseHID::new( - pac.USBCTRL_REGS, - pac.USBCTRL_DPRAM, - &mut pac.RESETS, - core, - clocks, - )) - }; + #[cfg(features = "serial")] + let mut hid = CustomHID::new( + pac.USBCTRL_REGS, + pac.USBCTRL_DPRAM, + &mut pac.RESETS, + clocks, + 10, + ); + + #[cfg(not(features = "serial"))] + let mut hid = MouseHID::new( + pac.USBCTRL_REGS, + pac.USBCTRL_DPRAM, + &mut pac.RESETS, + core, + clocks, + ); // The single-cycle I/O block controls our GPIO pins let sio = hal::Sio::new(pac.SIO); diff --git a/src/serial.rs b/src/serial.rs index 3a46cb7..8410eab 100644 --- a/src/serial.rs +++ b/src/serial.rs @@ -27,15 +27,19 @@ impl<'a> Serial<'a> { pub fn send_number(&mut self, i: impl NumToA, base: T) { let s = i.numtoa_str(base, &mut self.buf); - self.send_text(s); + Self::send(&mut self.serial, s); } pub fn send_text(&mut self, s: &str) { + Self::send(&mut self.serial, s); + } + + fn send(serial: &mut SerialPort<'a, UsbBus>, s: &str) { // Send back to the host let mut wr_ptr = s.as_bytes(); while !wr_ptr.is_empty() { - match self.serial.write(wr_ptr) { + match serial.write(wr_ptr) { Ok(len) => wr_ptr = &wr_ptr[len..], // On error, just drop unwritten data. // One possible error is Err(WouldBlock), meaning the USB