This repository has been archived on 2026-09-13. You can view files and clone it, but cannot push or open issues or pull requests.
macropad-rs/src/main.rs

230 lines
7.6 KiB
Rust

#![no_std]
#![no_main]
mod dispatch;
mod keyboard;
mod keypress;
mod keysend;
mod layout;
use core::sync::atomic::{AtomicBool, Ordering};
use defmt::*;
use embassy_executor::Spawner;
use embassy_futures::join::{join, join3, join5, join_array};
use embassy_rp::bind_interrupts;
use embassy_rp::peripherals::USB;
use embassy_rp::usb::{Driver, InterruptHandler};
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_sync::channel::Channel;
use embassy_usb::class::hid::{HidReaderWriter, ReportId, RequestHandler, State};
use embassy_usb::control::OutResponse;
use embassy_usb::{Builder, Config, Handler};
use usbd_hid::descriptor::{KeyboardReport, SerializedDescriptor};
use crate::keypress::await_key;
use {defmt_rtt as _, panic_probe as _};
bind_interrupts!(struct Irqs {
USBCTRL_IRQ => InterruptHandler<USB>;
});
pub(crate) const CHANNEL_SIZE: usize = 12;
pub(crate) type MutexType = CriticalSectionRawMutex;
pub(crate) type ChannelType = Channel<MutexType, KeyboardReport, CHANNEL_SIZE>;
// pub(crate) type WriterType = HidWriter<'static, Driver<'static, USB>, 8>;
// static LOCKED_WRITER: Mutex<CriticalSectionRawMutex, RefCell<Option<WriterType>>> =
// Mutex::new(RefCell::new(None));
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_rp::init(Default::default());
// Create the driver, from the HAL.
let driver = Driver::new(p.USB, Irqs);
// Create embassy-usb Config
let mut config = Config::new(0x27db, 0x16c0);
config.manufacturer = Some("Dan");
config.product = Some("Dandaboard SplitSimple");
config.serial_number = Some("DDB-SSK-0001a");
config.max_power = 100;
config.max_packet_size_0 = 64;
// Create embassy-usb DeviceBuilder using the driver and config.
// It needs some buffers for building the descriptors.
let mut device_descriptor = [0; 256];
let mut config_descriptor = [0; 256];
let mut bos_descriptor = [0; 256];
// You can also add a Microsoft OS descriptor.
let mut msos_descriptor = [0; 256];
let mut control_buf = [0; 64];
let request_handler = MyRequestHandler {};
let mut device_handler = MyDeviceHandler::new();
let mut state = State::new();
let mut builder = Builder::new(
driver,
config,
&mut device_descriptor,
&mut config_descriptor,
&mut bos_descriptor,
&mut msos_descriptor,
&mut control_buf,
);
builder.handler(&mut device_handler);
// Create classes on the builder.
let config = embassy_usb::class::hid::Config {
report_descriptor: KeyboardReport::desc(),
request_handler: Some(&request_handler),
poll_ms: 10,
max_packet_size: 64,
};
let hid = HidReaderWriter::<_, 1, 8>::new(&mut builder, &mut state, config);
// Build the builder.
let mut usb = builder.build();
// Run the USB device.
let usb_fut = usb.run();
let (reader, mut writer) = hid.split();
// let mut channel = Channel::<MutexType, KeyboardReport, CHANNEL_SIZE>::new();
let channel = ChannelType::new();
let senderloop = async {
loop {
info!("Awayting new messages");
let report = channel.receive().await;
// Send the report.
match writer.write_serialize(&report).await {
Ok(()) => {}
Err(e) => warn!("Failed to send report: {:?}", e),
};
}
};
// let list_keys = [
// await_key(p.PIN_4, layout::KeypadKey::A4, &channel).await,
// await_key(p.PIN_1, layout::KeypadKey::A1, &channel).await,
// await_key(p.PIN_2, layout::KeypadKey::A2, &channel).await,
// await_key(p.PIN_3, layout::KeypadKey::A3, &channel).await,
// await_key(p.PIN_5, layout::KeypadKey::B1, &channel).await,
// await_key(p.PIN_6, layout::KeypadKey::B2, &channel).await,
// await_key(p.PIN_7, layout::KeypadKey::B3, &channel).await,
// await_key(p.PIN_8, layout::KeypadKey::B4, &channel).await,
// await_key(p.PIN_9, layout::KeypadKey::C1, &channel).await,
// ];
// let in_fut = async {
// await_key(p.PIN_4, layout::KeypadKey::A4, &channel).await;
// await_key(p.PIN_1, layout::KeypadKey::A1, &channel).await;
// await_key(p.PIN_2, layout::KeypadKey::A2, &channel).await;
// await_key(p.PIN_3, layout::KeypadKey::A3, &channel).await;
// await_key(p.PIN_5, layout::KeypadKey::B1, &channel).await;
// await_key(p.PIN_6, layout::KeypadKey::B2, &channel).await;
// await_key(p.PIN_7, layout::KeypadKey::B3, &channel).await;
// await_key(p.PIN_8, layout::KeypadKey::B4, &channel).await;
// await_key(p.PIN_9, layout::KeypadKey::C1, &channel).await;
// await_key(p.PIN_10, layout::KeypadKey::C2, &channel).await;
// await_key(p.PIN_11, layout::KeypadKey::C3, &channel).await;
// await_key(p.PIN_12, layout::KeypadKey::C4, &channel).await;
// // info!("Waiting for HIGH on pin 1");
// // pin.wait_for_high().await;
// // info!("HIGH DETECTED");
// // pin.wait_for_low().await;
// // info!("LOW DETECTED");
// // }
// };
let out_fut = async {
reader.run(false, &request_handler).await;
};
// Run everything concurrently.
// If we had made everything `'static` above instead, we could do this using separate tasks instead.
// join(usb_fut, join3(senderloop, join_array(list_keys), out_fut)).await;
join(
usb_fut,
join3(
senderloop,
// join5(
await_key(p.PIN_2, layout::KeypadKey::A2, &channel),
// await_key(p.PIN_3, layout::KeypadKey::A3, &channel),
// await_key(p.PIN_4, layout::KeypadKey::A4, &channel),
// await_key(p.PIN_6, layout::KeypadKey::B2, &channel),
// await_key(p.PIN_7, layout::KeypadKey::B3, &channel),
// ),
out_fut,
),
)
.await;
}
struct MyRequestHandler {}
impl RequestHandler for MyRequestHandler {
fn get_report(&self, id: ReportId, _buf: &mut [u8]) -> Option<usize> {
info!("Get report for {:?}", id);
None
}
fn set_report(&self, id: ReportId, data: &[u8]) -> OutResponse {
info!("Set report for {:?}: {=[u8]}", id, data);
OutResponse::Accepted
}
fn set_idle_ms(&self, id: Option<ReportId>, dur: u32) {
info!("Set idle rate for {:?} to {:?}", id, dur);
}
fn get_idle_ms(&self, id: Option<ReportId>) -> Option<u32> {
info!("Get idle rate for {:?}", id);
None
}
}
struct MyDeviceHandler {
configured: AtomicBool,
}
impl MyDeviceHandler {
fn new() -> Self {
MyDeviceHandler {
configured: AtomicBool::new(false),
}
}
}
impl Handler for MyDeviceHandler {
fn enabled(&mut self, enabled: bool) {
self.configured.store(false, Ordering::Relaxed);
if enabled {
info!("Device enabled");
} else {
info!("Device disabled");
}
}
fn reset(&mut self) {
self.configured.store(false, Ordering::Relaxed);
info!("Bus reset, the Vbus current limit is 100mA");
}
fn addressed(&mut self, addr: u8) {
self.configured.store(false, Ordering::Relaxed);
info!("USB address set to: {}", addr);
}
fn configured(&mut self, configured: bool) {
self.configured.store(configured, Ordering::Relaxed);
if configured {
info!(
"Device configured, it may now draw up to the configured current limit from Vbus."
)
} else {
info!("Device is no longer configured, the Vbus current limit is 100mA.");
}
}
}