feat: use async interupts for gpio monitoring

This commit is contained in:
Jet Pham 2025-06-07 23:53:19 -07:00
parent bcf986ff1f
commit 19862ecf70
No known key found for this signature in database
4 changed files with 77 additions and 117 deletions

View file

@ -9,7 +9,6 @@ use anyhow::Result;
use tracing::{error, info};
const DEFAULT_GPIO_PIN: u8 = 17;
const DEFAULT_POLL_INTERVAL_MS: u64 = 100;
const DEFAULT_DEBOUNCE_DELAY_SECS: u64 = 5;
#[tokio::main]
@ -20,12 +19,11 @@ async fn main() -> Result<()> {
let discord_client = discord::DiscordClient::new().await?;
let discord_client = Arc::new(discord_client);
discord_client.send_startup_message().await?;
discord_client.handle_event(discord::SpaceEvent::Initializing).await?;
info!("initializing gpio monitor");
let mut gpio_monitor = gpio::GpioMonitor::new(
DEFAULT_GPIO_PIN,
Duration::from_millis(DEFAULT_POLL_INTERVAL_MS),
Duration::from_secs(DEFAULT_DEBOUNCE_DELAY_SECS)
)?;
@ -34,17 +32,25 @@ async fn main() -> Result<()> {
info!("Circuit state changed to: {:?}", event);
let discord_client = discord_client.clone();
tokio::spawn(async move {
if let Err(e) = discord_client.send_circuit_event(&event).await {
let space_event = match event {
gpio::CircuitEvent::Open => discord::SpaceEvent::Open,
gpio::CircuitEvent::Closed => discord::SpaceEvent::Closed,
};
if let Err(e) = discord_client.handle_event(space_event).await {
error!("Failed to send Discord message: {}", e);
}
});
};
// Start monitoring - this will block until an error occurs
if let Err(e) = gpio_monitor.monitor(callback).await {
if let Err(e) = gpio_monitor.monitor(callback) {
error!("GPIO monitoring error: {}", e);
return Err(anyhow::anyhow!("GPIO monitoring failed"));
}
info!("GPIO monitoring started. Press Ctrl+C to exit.");
tokio::signal::ctrl_c().await?;
info!("Shutting down...");
Ok(())
}