ProjektMaus/src/main.rs

163 lines
3.9 KiB
Rust
Raw Normal View History

2023-03-19 15:19:56 +00:00
#![no_std]
#![no_main]
2023-03-25 13:29:35 +00:00
#[cfg(feature = "serial")]
mod custom_hid;
2023-03-23 05:37:37 +00:00
2023-03-25 13:29:35 +00:00
#[cfg(not(feature = "serial"))]
mod mouse_hid;
2023-03-23 05:37:37 +00:00
2023-03-20 20:50:34 +00:00
mod mouse_sensor;
mod serial;
2023-03-20 20:50:34 +00:00
2023-03-25 13:29:35 +00:00
use core::panic::PanicInfo;
use cortex_m::delay::Delay;
#[cfg(feature = "serial")]
2023-03-22 15:29:13 +00:00
use custom_hid::CustomHID;
2023-03-23 05:37:37 +00:00
2023-03-25 13:29:35 +00:00
#[cfg(not(feature = "serial"))]
use mouse_hid::MouseHID;
2023-03-22 15:29:13 +00:00
2023-03-25 13:29:35 +00:00
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}
2023-03-19 15:19:56 +00:00
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
pub trait MouseInfoSender {
fn send(&mut self, x: i8, y: i8, buttons: u8, wheel: i8, pan: i8);
2023-03-25 13:29:35 +00:00
fn heart_beat(&mut self);
2023-03-22 15:29:13 +00:00
}
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();
let peripheral_clock = clocks.peripheral_clock.freq();
2023-03-25 13:29:35 +00:00
let delay = Delay::new(core.SYST, clocks.system_clock.freq().to_Hz());
2023-03-25 13:29:35 +00:00
#[cfg(feature = "serial")]
2023-03-23 05:37:37 +00:00
let mut hid = CustomHID::new(
pac.USBCTRL_REGS,
pac.USBCTRL_DPRAM,
&mut pac.RESETS,
clocks,
10,
);
2023-03-25 13:29:35 +00:00
#[cfg(not(feature = "serial"))]
2023-03-23 05:37:37 +00:00
let mut hid = MouseHID::new(
pac.USBCTRL_REGS,
pac.USBCTRL_DPRAM,
&mut pac.RESETS,
core,
clocks,
);
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-25 13:29:35 +00:00
let mut mouse_sensor = MouseSensor::new(
pins,
&mut pac.RESETS,
pac.SPI0,
peripheral_clock,
delay,
|msg| {
#[cfg(feature = "serial")]
critical_section::with(|_| {
custom_hid::serial().send_text(msg);
});
},
);
// #[cfg(feature = "serial")]
// {
// hid.serial(|serial| {
// serial.send_number(mouse_sensor.product_id_1(), 10);
// serial.send_number(mouse_sensor.product_id_2(), 10);
// });
// }
let mut c = 0;
2023-03-19 15:19:56 +00:00
loop {
2023-03-25 13:29:35 +00:00
// if let Some((x, y)) = mouse_sensor.read_movement() {
// hid.send(x, y, 0, 0, 0);
// }
c += 1;
if c == 10_000_000 {
c = 0;
#[cfg(feature = "serial")]
critical_section::with(|_| {
let serial = custom_hid::serial();
2023-03-25 14:41:15 +00:00
// serial.send_text("---");
// serial.send_number(mouse_sensor.product_id_1(), 10);
// serial.send_text(" - ");
// serial.send_number(0x30, 10);
// serial.send_text("---");
// serial.send_number(mouse_sensor.product_id_2(), 10);
// serial.send_text(" - ");
// serial.send_number(0x02, 10);
2023-03-25 13:29:35 +00:00
2023-08-15 10:59:42 +00:00
// custom_hid::serial().send_number(mouse_sensor.spi_device.product_id_2(), 10);
custom_hid::serial().send_text("verify ids:");
custom_hid::serial().send_text(if mouse_sensor.verify_product_id_1() {
"PID1 true"
} else {
"PID1 false"
});
custom_hid::serial().send_text(if mouse_sensor.verify_product_id_2() {
"PID2 true"
} else {
"PID2 false"
});
2023-03-25 13:29:35 +00:00
});
2023-03-22 15:29:13 +00:00
}
2023-03-25 13:29:35 +00:00
// hid.heart_beat();
2023-03-19 15:19:56 +00:00
}
}