Add build features

This commit is contained in:
hodasemi 2023-03-23 06:37:37 +01:00
parent 3494ee5bd8
commit 2de4c0638e
4 changed files with 58 additions and 24 deletions

25
.vscode/tasks.json vendored Normal file
View file

@ -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)"
}
]
}

View file

@ -18,3 +18,6 @@ embedded-hal = "0.2.7"
fugit = "0.3.6" fugit = "0.3.6"
numtoa = "0.2.4" numtoa = "0.2.4"
usbd-serial = "0.1.1" usbd-serial = "0.1.1"
[features]
serial = []

View file

@ -1,15 +1,19 @@
#![no_std] #![no_std]
#![no_main] #![no_main]
extern crate alloc; #[cfg(features = "serial")]
mod custom_hid; mod custom_hid;
#[cfg(not(features = "serial"))]
mod mouse_hid; mod mouse_hid;
mod mouse_sensor; mod mouse_sensor;
mod serial; mod serial;
use alloc::boxed::Box; #[cfg(features = "serial")]
use custom_hid::CustomHID; use custom_hid::CustomHID;
#[cfg(not(features = "serial"))]
use mouse_hid::MouseHID; use mouse_hid::MouseHID;
// Ensure we halt the program on panic (if we don't mention this crate it won't // 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; use crate::mouse_sensor::MouseSensor;
const SERIAL: bool = true;
pub trait MouseInfoSender { pub trait MouseInfoSender {
fn send(&mut self, x: i8, y: i8, buttons: u8, wheel: i8, pan: i8); 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 peripheral_clock = clocks.peripheral_clock.freq();
let mut hid: Box<dyn MouseInfoSender> = if SERIAL { #[cfg(features = "serial")]
Box::new(CustomHID::new( let mut hid = CustomHID::new(
pac.USBCTRL_REGS, pac.USBCTRL_REGS,
pac.USBCTRL_DPRAM, pac.USBCTRL_DPRAM,
&mut pac.RESETS, &mut pac.RESETS,
clocks, clocks,
10, 10,
)) );
} else {
Box::new(MouseHID::new( #[cfg(not(features = "serial"))]
pac.USBCTRL_REGS, let mut hid = MouseHID::new(
pac.USBCTRL_DPRAM, pac.USBCTRL_REGS,
&mut pac.RESETS, pac.USBCTRL_DPRAM,
core, &mut pac.RESETS,
clocks, core,
)) clocks,
}; );
// The single-cycle I/O block controls our GPIO pins // The single-cycle I/O block controls our GPIO pins
let sio = hal::Sio::new(pac.SIO); let sio = hal::Sio::new(pac.SIO);

View file

@ -27,15 +27,19 @@ impl<'a> Serial<'a> {
pub fn send_number<T>(&mut self, i: impl NumToA<T>, base: T) { pub fn send_number<T>(&mut self, i: impl NumToA<T>, base: T) {
let s = i.numtoa_str(base, &mut self.buf); 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) { 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 // Send back to the host
let mut wr_ptr = s.as_bytes(); let mut wr_ptr = s.as_bytes();
while !wr_ptr.is_empty() { while !wr_ptr.is_empty() {
match self.serial.write(wr_ptr) { match serial.write(wr_ptr) {
Ok(len) => wr_ptr = &wr_ptr[len..], Ok(len) => wr_ptr = &wr_ptr[len..],
// On error, just drop unwritten data. // On error, just drop unwritten data.
// One possible error is Err(WouldBlock), meaning the USB // One possible error is Err(WouldBlock), meaning the USB