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;
|
use rp_pico as bsp;
|
||||||
|
|
||||||
|
@ -8,18 +8,12 @@ use bsp::{
|
||||||
pac,
|
pac,
|
||||||
pac::{interrupt, Interrupt},
|
pac::{interrupt, Interrupt},
|
||||||
usb::UsbBus,
|
usb::UsbBus,
|
||||||
Clock,
|
|
||||||
},
|
},
|
||||||
pac::{RESETS, USBCTRL_DPRAM, USBCTRL_REGS},
|
pac::{RESETS, USBCTRL_DPRAM, USBCTRL_REGS},
|
||||||
};
|
};
|
||||||
|
|
||||||
use cortex_m::{delay::Delay, Peripherals as CortexPeripherals};
|
|
||||||
|
|
||||||
use usb_device::{class_prelude::*, prelude::*};
|
use usb_device::{class_prelude::*, prelude::*};
|
||||||
use usbd_hid::{
|
use usbd_hid::descriptor::MouseReport;
|
||||||
descriptor::{MouseReport, SerializedDescriptor},
|
|
||||||
hid_class::HIDClass,
|
|
||||||
};
|
|
||||||
|
|
||||||
static mut USB_BUS: Option<UsbBusAllocator<UsbBus>> = None;
|
static mut USB_BUS: Option<UsbBusAllocator<UsbBus>> = None;
|
||||||
static mut USB_SERIAL: Option<Serial> = None;
|
static mut USB_SERIAL: Option<Serial> = None;
|
||||||
|
@ -35,7 +29,6 @@ impl<'a> CustomHID<'a> {
|
||||||
usbctrl_reg: USBCTRL_REGS,
|
usbctrl_reg: USBCTRL_REGS,
|
||||||
usbctrl_dpram: USBCTRL_DPRAM,
|
usbctrl_dpram: USBCTRL_DPRAM,
|
||||||
resets: &mut RESETS,
|
resets: &mut RESETS,
|
||||||
core: CortexPeripherals,
|
|
||||||
clocks: ClocksManager,
|
clocks: ClocksManager,
|
||||||
poll_ms: u8,
|
poll_ms: u8,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
|
@ -76,8 +69,31 @@ impl<'a> CustomHID<'a> {
|
||||||
|
|
||||||
Self { in_ep, out_ep }
|
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)]
|
#[allow(non_snake_case)]
|
||||||
|
|
46
src/main.rs
46
src/main.rs
|
@ -1,12 +1,17 @@
|
||||||
#![no_std]
|
#![no_std]
|
||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
|
extern crate alloc;
|
||||||
|
|
||||||
mod custom_hid;
|
mod custom_hid;
|
||||||
mod mouse_hid;
|
mod mouse_hid;
|
||||||
mod mouse_sensor;
|
mod mouse_sensor;
|
||||||
mod serial;
|
mod serial;
|
||||||
|
|
||||||
|
use alloc::boxed::Box;
|
||||||
|
use custom_hid::CustomHID;
|
||||||
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
|
||||||
// be linked)
|
// be linked)
|
||||||
use panic_halt as _;
|
use panic_halt as _;
|
||||||
|
@ -15,25 +20,18 @@ use rp_pico as bsp;
|
||||||
|
|
||||||
use bsp::{
|
use bsp::{
|
||||||
entry,
|
entry,
|
||||||
hal::{
|
hal::{self, pac, prelude::*},
|
||||||
self, pac,
|
|
||||||
pac::{interrupt, Interrupt},
|
|
||||||
prelude::*,
|
|
||||||
usb::UsbBus,
|
|
||||||
},
|
|
||||||
XOSC_CRYSTAL_FREQ,
|
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;
|
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]
|
#[entry]
|
||||||
fn main() -> ! {
|
fn main() -> ! {
|
||||||
// Grab our singleton objects
|
// Grab our singleton objects
|
||||||
|
@ -58,13 +56,23 @@ fn main() -> ! {
|
||||||
|
|
||||||
let peripheral_clock = clocks.peripheral_clock.freq();
|
let peripheral_clock = clocks.peripheral_clock.freq();
|
||||||
|
|
||||||
let mut mouse_hid = MouseHID::new(
|
let mut hid: Box<dyn MouseInfoSender> = if SERIAL {
|
||||||
|
Box::new(MouseHID::new(
|
||||||
pac.USBCTRL_REGS,
|
pac.USBCTRL_REGS,
|
||||||
pac.USBCTRL_DPRAM,
|
pac.USBCTRL_DPRAM,
|
||||||
&mut pac.RESETS,
|
&mut pac.RESETS,
|
||||||
core,
|
core,
|
||||||
clocks,
|
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
|
// The single-cycle I/O block controls our GPIO pins
|
||||||
let sio = hal::Sio::new(pac.SIO);
|
let sio = hal::Sio::new(pac.SIO);
|
||||||
|
@ -77,9 +85,11 @@ fn main() -> ! {
|
||||||
&mut pac.RESETS,
|
&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 {
|
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,
|
hid_class::HIDClass,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::MouseInfoSender;
|
||||||
|
|
||||||
/// The USB Device Driver (shared with the interrupt).
|
/// The USB Device Driver (shared with the interrupt).
|
||||||
static mut USB_DEVICE: Option<UsbDevice<UsbBus>> = None;
|
static mut USB_DEVICE: Option<UsbDevice<UsbBus>> = None;
|
||||||
|
|
||||||
|
@ -80,23 +82,18 @@ impl MouseHID {
|
||||||
|
|
||||||
Self { delay }
|
Self { delay }
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn run(&mut self) {
|
impl MouseInfoSender for MouseHID {
|
||||||
self.mouse_move(Self::WAIT_TIME, Self::PIXEL);
|
fn send(&mut self, x: i8, y: i8, buttons: u8, wheel: i8, pan: i8) {
|
||||||
self.mouse_move(50, -Self::PIXEL);
|
push_mouse_movement(MouseReport {
|
||||||
}
|
x,
|
||||||
|
y,
|
||||||
fn mouse_move(&mut self, wait_time: u32, v: i8) {
|
buttons,
|
||||||
self.delay.delay_ms(wait_time);
|
wheel,
|
||||||
|
pan,
|
||||||
let rep = MouseReport {
|
})
|
||||||
x: 0,
|
.unwrap_or(0);
|
||||||
y: v,
|
|
||||||
buttons: 0,
|
|
||||||
wheel: 0,
|
|
||||||
pan: 0,
|
|
||||||
};
|
|
||||||
push_mouse_movement(rep).unwrap_or(0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -88,4 +88,8 @@ impl MouseSensor {
|
||||||
me.spi.send(value).unwrap();
|
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>) {
|
pub fn send_number<T>(&mut self, i: impl NumToA<T>, base: T) {
|
||||||
let s = i.numtoa_str(10, &mut self.buf);
|
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
|
// Send back to the host
|
||||||
let mut wr_ptr = s.as_bytes();
|
let mut wr_ptr = s.as_bytes();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue