heartbeat: Code cleanup
Signed-off-by: Nicholas Mello <nick@nmello.dev>
This commit is contained in:
11
src/heartbeat.rs
Normal file
11
src/heartbeat.rs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
use defmt::*;
|
||||||
|
use embassy_rp::gpio::Output;
|
||||||
|
use embassy_time::{Duration, Timer};
|
||||||
|
|
||||||
|
pub async fn heartbeat(mut led: Output<'_>) {
|
||||||
|
loop {
|
||||||
|
info!("Alive!");
|
||||||
|
led.toggle();
|
||||||
|
Timer::after(Duration::from_millis(1000)).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
39
src/main.rs
39
src/main.rs
@@ -1,6 +1,8 @@
|
|||||||
#![no_std]
|
#![no_std]
|
||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
|
mod heartbeat;
|
||||||
|
|
||||||
use core::sync::atomic::{AtomicBool, AtomicU8, Ordering};
|
use core::sync::atomic::{AtomicBool, AtomicU8, Ordering};
|
||||||
|
|
||||||
use defmt::*;
|
use defmt::*;
|
||||||
@@ -10,7 +12,6 @@ use embassy_rp::bind_interrupts;
|
|||||||
use embassy_rp::gpio::{Input, Level, Output, Pull};
|
use embassy_rp::gpio::{Input, Level, Output, Pull};
|
||||||
use embassy_rp::peripherals::USB;
|
use embassy_rp::peripherals::USB;
|
||||||
use embassy_rp::usb::{Driver, InterruptHandler};
|
use embassy_rp::usb::{Driver, InterruptHandler};
|
||||||
use embassy_time::{Duration, Timer};
|
|
||||||
use embassy_usb::class::hid::{
|
use embassy_usb::class::hid::{
|
||||||
HidBootProtocol, HidProtocolMode, HidReaderWriter, HidSubclass, ReportId, RequestHandler, State,
|
HidBootProtocol, HidProtocolMode, HidReaderWriter, HidSubclass, ReportId, RequestHandler, State,
|
||||||
};
|
};
|
||||||
@@ -33,9 +34,9 @@ async fn main(_spawner: Spawner) {
|
|||||||
|
|
||||||
// Create embassy-usb Config
|
// Create embassy-usb Config
|
||||||
let mut config = Config::new(0xc0de, 0xcafe);
|
let mut config = Config::new(0xc0de, 0xcafe);
|
||||||
config.manufacturer = Some("Embassy");
|
config.manufacturer = Some("Nicholas Mello");
|
||||||
config.product = Some("HID keyboard example");
|
config.product = Some("Pi Pico Arcade Keyboard");
|
||||||
config.serial_number = Some("12345678");
|
config.serial_number = Some("00000001");
|
||||||
config.max_power = 100;
|
config.max_power = 100;
|
||||||
config.max_packet_size_0 = 64;
|
config.max_packet_size_0 = 64;
|
||||||
config.composite_with_iads = false;
|
config.composite_with_iads = false;
|
||||||
@@ -77,12 +78,6 @@ async fn main(_spawner: Spawner) {
|
|||||||
};
|
};
|
||||||
let hid = HidReaderWriter::<_, 1, 8>::new(&mut builder, &mut state, config);
|
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();
|
|
||||||
|
|
||||||
// Set up the signal pin that will be used to trigger the keyboard.
|
// Set up the signal pin that will be used to trigger the keyboard.
|
||||||
let mut signal_pin = Input::new(p.PIN_16, Pull::None);
|
let mut signal_pin = Input::new(p.PIN_16, Pull::None);
|
||||||
|
|
||||||
@@ -94,7 +89,6 @@ async fn main(_spawner: Spawner) {
|
|||||||
// Do stuff with the class!
|
// Do stuff with the class!
|
||||||
let in_fut = async {
|
let in_fut = async {
|
||||||
loop {
|
loop {
|
||||||
info!("Waiting for HIGH on pin 16");
|
|
||||||
signal_pin.wait_for_high().await;
|
signal_pin.wait_for_high().await;
|
||||||
info!("HIGH DETECTED");
|
info!("HIGH DETECTED");
|
||||||
|
|
||||||
@@ -144,23 +138,14 @@ async fn main(_spawner: Spawner) {
|
|||||||
reader.run(false, &mut request_handler).await;
|
reader.run(false, &mut request_handler).await;
|
||||||
};
|
};
|
||||||
|
|
||||||
let heartbeat = async {
|
let mut usb = builder.build();
|
||||||
let mut led = Output::new(p.PIN_25, Level::Low);
|
let led = Output::new(p.PIN_25, Level::Low);
|
||||||
|
|
||||||
loop {
|
join(
|
||||||
info!("led on!");
|
join(usb.run(), heartbeat::heartbeat(led)),
|
||||||
led.set_high();
|
join(in_fut, out_fut),
|
||||||
Timer::after(Duration::from_millis(1000)).await;
|
)
|
||||||
|
.await;
|
||||||
info!("led off!");
|
|
||||||
led.set_low();
|
|
||||||
Timer::after(Duration::from_millis(1000)).await;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Run everything concurrently.
|
|
||||||
// If we had made everything `'static` above instead, we could do this using separate tasks instead.
|
|
||||||
join(join(usb_fut, heartbeat), join(in_fut, out_fut)).await;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct MyRequestHandler {}
|
struct MyRequestHandler {}
|
||||||
|
|||||||
Reference in New Issue
Block a user