Implement generic sender
This commit is contained in:
parent
e94f2eb8d7
commit
2f8406da9b
5 changed files with 82 additions and 51 deletions
|
@ -1,4 +1,4 @@
|
|||
use crate::serial::Serial;
|
||||
use crate::{serial::Serial, MouseInfoSender};
|
||||
|
||||
use rp_pico as bsp;
|
||||
|
||||
|
@ -8,18 +8,12 @@ use bsp::{
|
|||
pac,
|
||||
pac::{interrupt, Interrupt},
|
||||
usb::UsbBus,
|
||||
Clock,
|
||||
},
|
||||
pac::{RESETS, USBCTRL_DPRAM, USBCTRL_REGS},
|
||||
};
|
||||
|
||||
use cortex_m::{delay::Delay, Peripherals as CortexPeripherals};
|
||||
|
||||
use usb_device::{class_prelude::*, prelude::*};
|
||||
use usbd_hid::{
|
||||
descriptor::{MouseReport, SerializedDescriptor},
|
||||
hid_class::HIDClass,
|
||||
};
|
||||
use usbd_hid::descriptor::MouseReport;
|
||||
|
||||
static mut USB_BUS: Option<UsbBusAllocator<UsbBus>> = None;
|
||||
static mut USB_SERIAL: Option<Serial> = None;
|
||||
|
@ -35,7 +29,6 @@ impl<'a> CustomHID<'a> {
|
|||
usbctrl_reg: USBCTRL_REGS,
|
||||
usbctrl_dpram: USBCTRL_DPRAM,
|
||||
resets: &mut RESETS,
|
||||
core: CortexPeripherals,
|
||||
clocks: ClocksManager,
|
||||
poll_ms: u8,
|
||||
) -> Self {
|
||||
|
@ -76,8 +69,31 @@ impl<'a> CustomHID<'a> {
|
|||
|
||||
Self { in_ep, out_ep }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run(&mut self) {}
|
||||
impl<'a> MouseInfoSender for CustomHID<'a> {
|
||||
fn send(&mut self, x: i8, y: i8, buttons: u8, wheel: i8, pan: i8) {
|
||||
push_mouse_movement(MouseReport {
|
||||
x,
|
||||
y,
|
||||
buttons,
|
||||
wheel,
|
||||
pan,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// with interrupts disabled, to avoid a race hazard with the USB IRQ.
|
||||
fn push_mouse_movement(report: MouseReport) {
|
||||
critical_section::with(|_| unsafe {
|
||||
let serial = USB_SERIAL.as_mut().unwrap();
|
||||
|
||||
serial.send_text("x");
|
||||
serial.send_number(report.x, 10);
|
||||
|
||||
serial.send_text("y");
|
||||
serial.send_number(report.y, 10);
|
||||
});
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
|
|
56
src/main.rs
56
src/main.rs
|
@ -1,12 +1,17 @@
|
|||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
mod custom_hid;
|
||||
mod mouse_hid;
|
||||
mod mouse_sensor;
|
||||
mod serial;
|
||||
|
||||
use alloc::boxed::Box;
|
||||
use custom_hid::CustomHID;
|
||||
use mouse_hid::MouseHID;
|
||||
|
||||
// Ensure we halt the program on panic (if we don't mention this crate it won't
|
||||
// be linked)
|
||||
use panic_halt as _;
|
||||
|
@ -15,25 +20,18 @@ use rp_pico as bsp;
|
|||
|
||||
use bsp::{
|
||||
entry,
|
||||
hal::{
|
||||
self, pac,
|
||||
pac::{interrupt, Interrupt},
|
||||
prelude::*,
|
||||
usb::UsbBus,
|
||||
},
|
||||
hal::{self, pac, prelude::*},
|
||||
XOSC_CRYSTAL_FREQ,
|
||||
};
|
||||
|
||||
use cortex_m::delay::Delay;
|
||||
|
||||
use usb_device::{class_prelude::*, prelude::*};
|
||||
use usbd_hid::{
|
||||
descriptor::{MouseReport, SerializedDescriptor},
|
||||
hid_class::HIDClass,
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
// Grab our singleton objects
|
||||
|
@ -58,13 +56,23 @@ fn main() -> ! {
|
|||
|
||||
let peripheral_clock = clocks.peripheral_clock.freq();
|
||||
|
||||
let mut mouse_hid = MouseHID::new(
|
||||
pac.USBCTRL_REGS,
|
||||
pac.USBCTRL_DPRAM,
|
||||
&mut pac.RESETS,
|
||||
core,
|
||||
clocks,
|
||||
);
|
||||
let mut hid: Box<dyn MouseInfoSender> = if SERIAL {
|
||||
Box::new(MouseHID::new(
|
||||
pac.USBCTRL_REGS,
|
||||
pac.USBCTRL_DPRAM,
|
||||
&mut pac.RESETS,
|
||||
core,
|
||||
clocks,
|
||||
))
|
||||
} else {
|
||||
Box::new(CustomHID::new(
|
||||
pac.USBCTRL_REGS,
|
||||
pac.USBCTRL_DPRAM,
|
||||
&mut pac.RESETS,
|
||||
clocks,
|
||||
10,
|
||||
))
|
||||
};
|
||||
|
||||
// The single-cycle I/O block controls our GPIO pins
|
||||
let sio = hal::Sio::new(pac.SIO);
|
||||
|
@ -77,9 +85,11 @@ fn main() -> ! {
|
|||
&mut pac.RESETS,
|
||||
);
|
||||
|
||||
let mouse_sensor = MouseSensor::new(pins, &mut pac.RESETS, pac.SPI0, peripheral_clock);
|
||||
let mut mouse_sensor = MouseSensor::new(pins, &mut pac.RESETS, pac.SPI0, peripheral_clock);
|
||||
|
||||
loop {
|
||||
mouse_hid.run();
|
||||
if let Some((x, y)) = mouse_sensor.read_movement() {
|
||||
hid.send(x, y, 0, 0, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,8 @@ use usbd_hid::{
|
|||
hid_class::HIDClass,
|
||||
};
|
||||
|
||||
use crate::MouseInfoSender;
|
||||
|
||||
/// The USB Device Driver (shared with the interrupt).
|
||||
static mut USB_DEVICE: Option<UsbDevice<UsbBus>> = None;
|
||||
|
||||
|
@ -80,23 +82,18 @@ impl MouseHID {
|
|||
|
||||
Self { delay }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run(&mut self) {
|
||||
self.mouse_move(Self::WAIT_TIME, Self::PIXEL);
|
||||
self.mouse_move(50, -Self::PIXEL);
|
||||
}
|
||||
|
||||
fn mouse_move(&mut self, wait_time: u32, v: i8) {
|
||||
self.delay.delay_ms(wait_time);
|
||||
|
||||
let rep = MouseReport {
|
||||
x: 0,
|
||||
y: v,
|
||||
buttons: 0,
|
||||
wheel: 0,
|
||||
pan: 0,
|
||||
};
|
||||
push_mouse_movement(rep).unwrap_or(0);
|
||||
impl MouseInfoSender for MouseHID {
|
||||
fn send(&mut self, x: i8, y: i8, buttons: u8, wheel: i8, pan: i8) {
|
||||
push_mouse_movement(MouseReport {
|
||||
x,
|
||||
y,
|
||||
buttons,
|
||||
wheel,
|
||||
pan,
|
||||
})
|
||||
.unwrap_or(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -88,4 +88,8 @@ impl MouseSensor {
|
|||
me.spi.send(value).unwrap();
|
||||
});
|
||||
}
|
||||
|
||||
pub fn read_movement(&mut self) -> Option<(i8, i8)> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,9 +24,13 @@ impl<'a> Serial<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn send(&mut self, i: impl NumToA<u16>) {
|
||||
let s = i.numtoa_str(10, &mut self.buf);
|
||||
pub fn send_number<T>(&mut self, i: impl NumToA<T>, base: T) {
|
||||
let s = i.numtoa_str(base, &mut self.buf);
|
||||
|
||||
self.send_text(s);
|
||||
}
|
||||
|
||||
pub fn send_text(&mut self, s: &str) {
|
||||
// Send back to the host
|
||||
let mut wr_ptr = s.as_bytes();
|
||||
|
||||
|
|
Loading…
Reference in a new issue