2023-03-19 15:19:56 +00:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
|
2023-03-22 15:29:13 +00:00
|
|
|
extern crate alloc;
|
|
|
|
|
2023-03-22 10:24:32 +00:00
|
|
|
mod custom_hid;
|
|
|
|
mod mouse_hid;
|
2023-03-20 20:50:34 +00:00
|
|
|
mod mouse_sensor;
|
2023-03-22 10:24:32 +00:00
|
|
|
mod serial;
|
2023-03-20 20:50:34 +00:00
|
|
|
|
2023-03-22 15:29:13 +00:00
|
|
|
use alloc::boxed::Box;
|
|
|
|
use custom_hid::CustomHID;
|
2023-03-22 10:24:32 +00:00
|
|
|
use mouse_hid::MouseHID;
|
2023-03-22 15:29:13 +00:00
|
|
|
|
2023-03-19 15:19:56 +00:00
|
|
|
// Ensure we halt the program on panic (if we don't mention this crate it won't
|
|
|
|
// be linked)
|
|
|
|
use panic_halt as _;
|
|
|
|
|
|
|
|
use rp_pico as bsp;
|
|
|
|
|
|
|
|
use bsp::{
|
|
|
|
entry,
|
2023-03-22 15:29:13 +00:00
|
|
|
hal::{self, pac, prelude::*},
|
2023-03-19 15:19:56 +00:00
|
|
|
XOSC_CRYSTAL_FREQ,
|
|
|
|
};
|
|
|
|
|
2023-03-22 15:29:13 +00:00
|
|
|
use crate::mouse_sensor::MouseSensor;
|
2023-03-19 15:19:56 +00:00
|
|
|
|
2023-03-22 15:29:13 +00:00
|
|
|
const SERIAL: bool = true;
|
2023-03-19 15:19:56 +00:00
|
|
|
|
2023-03-22 15:29:13 +00:00
|
|
|
pub trait MouseInfoSender {
|
|
|
|
fn send(&mut self, x: i8, y: i8, buttons: u8, wheel: i8, pan: i8);
|
|
|
|
}
|
2023-03-20 20:50:34 +00:00
|
|
|
|
2023-03-19 15:19:56 +00:00
|
|
|
#[entry]
|
|
|
|
fn main() -> ! {
|
|
|
|
// Grab our singleton objects
|
|
|
|
let mut pac = pac::Peripherals::take().unwrap();
|
|
|
|
let core = pac::CorePeripherals::take().unwrap();
|
|
|
|
|
|
|
|
// Set up the watchdog driver - needed by the clock setup code
|
|
|
|
let mut watchdog = hal::Watchdog::new(pac.WATCHDOG);
|
|
|
|
|
|
|
|
// Configure the clocks
|
|
|
|
let clocks = hal::clocks::init_clocks_and_plls(
|
|
|
|
XOSC_CRYSTAL_FREQ,
|
|
|
|
pac.XOSC,
|
|
|
|
pac.CLOCKS,
|
|
|
|
pac.PLL_SYS,
|
|
|
|
pac.PLL_USB,
|
|
|
|
&mut pac.RESETS,
|
|
|
|
&mut watchdog,
|
|
|
|
)
|
|
|
|
.ok()
|
|
|
|
.unwrap();
|
|
|
|
|
2023-03-22 10:24:32 +00:00
|
|
|
let peripheral_clock = clocks.peripheral_clock.freq();
|
|
|
|
|
2023-03-22 15:29:13 +00:00
|
|
|
let mut hid: Box<dyn MouseInfoSender> = if SERIAL {
|
2023-03-22 20:03:39 +00:00
|
|
|
Box::new(CustomHID::new(
|
2023-03-22 15:29:13 +00:00
|
|
|
pac.USBCTRL_REGS,
|
|
|
|
pac.USBCTRL_DPRAM,
|
|
|
|
&mut pac.RESETS,
|
|
|
|
clocks,
|
2023-03-22 20:03:39 +00:00
|
|
|
10,
|
2023-03-22 15:29:13 +00:00
|
|
|
))
|
|
|
|
} else {
|
2023-03-22 20:03:39 +00:00
|
|
|
Box::new(MouseHID::new(
|
2023-03-22 15:29:13 +00:00
|
|
|
pac.USBCTRL_REGS,
|
|
|
|
pac.USBCTRL_DPRAM,
|
|
|
|
&mut pac.RESETS,
|
2023-03-22 20:03:39 +00:00
|
|
|
core,
|
2023-03-22 15:29:13 +00:00
|
|
|
clocks,
|
|
|
|
))
|
|
|
|
};
|
2023-03-22 10:24:32 +00:00
|
|
|
|
2023-03-20 20:50:34 +00:00
|
|
|
// The single-cycle I/O block controls our GPIO pins
|
|
|
|
let sio = hal::Sio::new(pac.SIO);
|
|
|
|
|
|
|
|
// Set the pins up according to their function on this particular board
|
|
|
|
let pins = rp_pico::Pins::new(
|
|
|
|
pac.IO_BANK0,
|
|
|
|
pac.PADS_BANK0,
|
|
|
|
sio.gpio_bank0,
|
|
|
|
&mut pac.RESETS,
|
|
|
|
);
|
|
|
|
|
2023-03-22 15:29:13 +00:00
|
|
|
let mut mouse_sensor = MouseSensor::new(pins, &mut pac.RESETS, pac.SPI0, peripheral_clock);
|
2023-03-19 15:19:56 +00:00
|
|
|
|
|
|
|
loop {
|
2023-03-22 15:29:13 +00:00
|
|
|
if let Some((x, y)) = mouse_sensor.read_movement() {
|
|
|
|
hid.send(x, y, 0, 0, 0);
|
|
|
|
}
|
2023-03-19 15:19:56 +00:00
|
|
|
}
|
|
|
|
}
|