From 01454a018de654e68ea75a2fe257aee9b4edadc1 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Mon, 5 May 2025 14:39:13 +0200 Subject: [PATCH] fixed buzzer use in tokio thread --- src/buzzer.rs | 4 ++-- src/id_store.rs | 3 +-- src/main.rs | 7 ++++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/buzzer.rs b/src/buzzer.rs index 98f5da0..2e46b0c 100644 --- a/src/buzzer.rs +++ b/src/buzzer.rs @@ -1,5 +1,5 @@ use rppal::gpio::{Gpio, OutputPin}; -use std::time; +use std::{error::Error, time}; use tokio::time::sleep; pub struct GPIOBuzzer { @@ -7,7 +7,7 @@ pub struct GPIOBuzzer { } impl GPIOBuzzer { - pub fn new(pin_num: u8) -> Result> { + pub fn new(pin_num: u8) -> Result> { let gpio = Gpio::new()?; let pin = gpio.get(pin_num)?.into_output(); diff --git a/src/id_store.rs b/src/id_store.rs index 7332156..33c875b 100644 --- a/src/id_store.rs +++ b/src/id_store.rs @@ -4,7 +4,6 @@ use std::{ error::Error, fmt::Display, }; - use tokio::fs; /// Represents the ID that is stored on the Tally @@ -67,7 +66,7 @@ impl IDStore { } /// Writes the store to a json file - pub async fn export_json(&self, filepath: &str) -> Result<(), Box> { + pub async fn export_json(&self, filepath: &str) -> Result<(), Box> { fs::write(filepath, serde_json::to_string(&self)?).await?; Ok(()) } diff --git a/src/main.rs b/src/main.rs index e583392..7714bb4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use buzzer::GPIOBuzzer; use color::NamedColor; use id_store::IDStore; use log::{LevelFilter, error, info, warn}; @@ -69,8 +70,7 @@ async fn main() -> Result<(), Box> { }; let store: Arc> = Arc::new(Mutex::new(raw_store)); - - let mut gpio_buzzer = buzzer::GPIOBuzzer::new(4)?; + let gpio_buzzer: Arc> = Arc::new(Mutex::new(buzzer::GPIOBuzzer::new(4)?)); let channel_store = store.clone(); tokio::spawn(async move { @@ -82,11 +82,12 @@ async fn main() -> Result<(), Box> { { info!("Added new id to current day"); // led.set_named_color_time(NamedColor::Green, 1); //led is green for 1 sec - gpio_buzzer.beep_ack().await; + gpio_buzzer.lock().await.beep_ack().await; if let Err(e) = channel_store.lock().await.export_json(STORE_PATH).await { error!("Failed to save id store to file: {}", e); // TODO: How to handle a failure to save ? + gpio_buzzer.lock().await.beep_nak().await; } } }