From 3e079c905f93399e9f8743ad046691cabb23d578 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Wed, 25 Jun 2025 20:09:00 +0200 Subject: [PATCH] improved readability --- src/feedback.rs | 70 ++++++++++++++++++++++++++++++++++++++----------- src/hotspot.rs | 8 ++---- src/main.rs | 62 +++++++++++++++++++++++++------------------ src/spi_led.rs | 19 ++------------ 4 files changed, 94 insertions(+), 65 deletions(-) diff --git a/src/feedback.rs b/src/feedback.rs index b510876..86fdad5 100644 --- a/src/feedback.rs +++ b/src/feedback.rs @@ -5,7 +5,7 @@ use smart_leds::colors::{GREEN, RED}; use std::time::Duration; use tokio::{join, time::sleep}; -use crate::{hardware::{Buzzer, StatusLed}, spi_led}; +use crate::hardware::{Buzzer, StatusLed}; #[cfg(not(feature = "mock_pi"))] use crate::{gpio_buzzer::GPIOBuzzer, spi_led::SpiLed}; @@ -15,9 +15,23 @@ use crate::mock::{MockBuzzer, MockLed}; const LED_BLINK_DURATION: Duration = Duration::from_secs(1); -pub static CURRENTSTATUS: spi_led::CurrentStatus = spi_led::CurrentStatus::Ready; +pub enum DeviceStatus { + NotReady, + Ready, + HotspotEnabled, +} +impl DeviceStatus { + pub fn color(&self) -> RGB8 { + match self { + Self::NotReady => RGB8::new(0, 0, 0), + Self::Ready => RGB8::new(0, 50, 0), + Self::HotspotEnabled => RGB8::new(0, 0, 50), + } + } +} pub struct Feedback { + device_status: DeviceStatus, buzzer: B, led: L, } @@ -31,6 +45,8 @@ impl Feedback { buzzer_result.unwrap_or_else(|err| { error!("Failed to buzz: {err}"); }); + + let _ = self.led_to_status(); } pub async fn failure(&mut self) { @@ -42,6 +58,8 @@ impl Feedback { buzzer_result.unwrap_or_else(|err| { error!("Failed to buzz: {err}"); }); + + let _ = self.led_to_status(); } pub async fn activate_error_state(&mut self) -> Result<()> { @@ -50,24 +68,44 @@ impl Feedback { Ok(()) } - // ------------------ LED ------------------------- + pub async fn startup(&mut self){ + self.device_status = DeviceStatus::Ready; - /// flash led for amount of time - /// # Arguments - /// * `led`- led or mockled - /// * `color` - enum color - /// * `duration` - duration in ms - pub async fn flash_led_for_duration(led: &mut L, color: RGB8, duration: Duration) -> Result<()> { + let led_handle = Self::flash_led_for_duration(&mut self.led, GREEN, Duration::from_secs(1)); + let buzzer_handle = Self::beep_startup(&mut self.buzzer); + + let (buzzer_result, led_result) = join!(buzzer_handle, led_handle); + + buzzer_result.unwrap_or_else(|err| { + error!("Failed to buzz: {err}"); + }); + + led_result.unwrap_or_else(|err| { + error!("Failed to blink led: {err}"); + }); + + let _ = self.led_to_status(); + } + + pub fn set_device_status(&mut self, status: DeviceStatus){ + self.device_status = status; + let _ = self.led_to_status(); + } + + fn led_to_status(&mut self) -> Result<()> { + self.led.turn_on(self.device_status.color()) + } + + async fn flash_led_for_duration(led: &mut L, color: RGB8, duration: Duration) -> Result<()> { led.turn_on(color)?; + sleep(duration).await; + led.turn_off()?; + Ok(()) } - - // ----------------- BUZZER ------------------------ - - /// acknowledge beep tone async fn beep_ack(buzzer: &mut B) -> Result<()> { buzzer .modulated_tone(1200.0, Duration::from_millis(100)) @@ -79,7 +117,6 @@ impl Feedback { Ok(()) } - /// Not acknowledge beep tone async fn beep_nak(buzzer: &mut B) -> Result<()> { buzzer .modulated_tone(600.0, Duration::from_millis(150)) @@ -91,8 +128,7 @@ impl Feedback { Ok(()) } - /// beep tone for starting the device - pub async fn beep_startup(buzzer: &mut B) -> Result<()> { + async fn beep_startup(buzzer: &mut B) -> Result<()> { buzzer .modulated_tone(523.0, Duration::from_millis(150)) .await?; @@ -128,6 +164,7 @@ impl FeedbackImpl { #[cfg(feature = "mock_pi")] { Ok(Feedback { + device_status: DeviceStatus::NotReady, buzzer: MockBuzzer {}, led: MockLed {}, }) @@ -135,6 +172,7 @@ impl FeedbackImpl { #[cfg(not(feature = "mock_pi"))] { Ok(Feedback { + device_status: DeviceStatus::NotReady, buzzer: GPIOBuzzer::new_default()?, led: SpiLed::new()?, }) diff --git a/src/hotspot.rs b/src/hotspot.rs index 15f974a..ec9cfc3 100644 --- a/src/hotspot.rs +++ b/src/hotspot.rs @@ -1,10 +1,9 @@ use anyhow::{Result, anyhow}; use log::{trace, warn}; -use smart_leds::colors::GREEN; -use std::{env, time::Duration}; +use std::env; use tokio::process::Command; -use crate::{feedback::{self, FeedbackImpl}, hardware::Hotspot, spi_led}; +use crate::hardware::Hotspot; const SSID: &str = "fwa"; const CON_NAME: &str = "fwa-hotspot"; @@ -127,9 +126,6 @@ impl Hotspot for NMHotspot { return Err(anyhow!("nmcli command had non-zero exit code")); } - feedback::CURRENTSTATUS = Ready; - FeedbackImpl::flash_led_for_duration(led, GREEN, Duration::from_secs(1)); - Ok(()) } } diff --git a/src/main.rs b/src/main.rs index f8388fd..2453d42 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,6 @@ use hardware::{Hotspot, create_hotspot}; use id_store::IDStore; use log::{error, info, warn}; use pm3::run_pm3; -use smart_leds::colors::BLUE; use std::{ env::{self, args}, sync::Arc, @@ -46,6 +45,7 @@ async fn run_webserver( store: Arc>, id_channel: Sender, hotspot: Arc>, + user_feedback: Arc>, ) -> Result<()> where H: Hotspot + Send + Sync + 'static, @@ -53,8 +53,13 @@ where let activity_channel = spawn_idle_watcher(Duration::from_secs(60 * 30), move || { info!("No activity on webserver. Disabling hotspot"); let cloned_hotspot = hotspot.clone(); + let cloned_user_feedback = user_feedback.clone(); tokio::spawn(async move { let _ = cloned_hotspot.lock().await.disable_hotspot().await; + cloned_user_feedback + .lock() + .await + .set_device_status(feedback::DeviceStatus::Ready); }); }); @@ -64,9 +69,6 @@ where start_webserver(store, id_channel, notifier).await?; - feedback::CURRENTSTATUS = Hotspot; - FeedbackImpl::flash_led_for_duration(led, BLUE, 1000); - Ok(()) } @@ -99,32 +101,38 @@ async fn handle_ids_loop( hotspot_enable_ids: Vec, id_store: Arc>, hotspot: Arc>, - mut user_feedback: FeedbackImpl, + user_feedback: Arc>, ) -> Result<()> { while let Ok(tally_id_string) = id_channel.recv().await { let tally_id = TallyID(tally_id_string); if hotspot_enable_ids.contains(&tally_id) { info!("Enableing hotspot"); - hotspot - .lock() - .await - .enable_hotspot() - .await - .unwrap_or_else(|err| { - error!("Hotspot: {err}"); - }); + let hotspot_enable_result = hotspot.lock().await.enable_hotspot().await; + + match hotspot_enable_result { + Ok(_) => { + user_feedback + .lock() + .await + .set_device_status(feedback::DeviceStatus::HotspotEnabled); + } + Err(e) => { + error!("Hotspot: {e}"); + } + } + // TODO: Should the ID be added anyway or ignored ? } if id_store.lock().await.add_id(tally_id) { info!("Added new id to current day"); - user_feedback.success().await; + user_feedback.lock().await.success().await; if let Err(e) = id_store.lock().await.export_json(STORE_PATH).await { error!("Failed to save id store to file: {e}"); - user_feedback.failure().await; + user_feedback.lock().await.failure().await; // TODO: How to handle a failure to save ? } } @@ -133,8 +141,8 @@ async fn handle_ids_loop( Ok(()) } -async fn enter_error_state(mut feedback: FeedbackImpl, hotspot: Arc>) { - let _ = feedback.activate_error_state().await; +async fn enter_error_state(feedback: Arc>, hotspot: Arc>) { + let _ = feedback.lock().await.activate_error_state().await; let _ = hotspot.lock().await.enable_hotspot().await; let mut sigterm = signal(SignalKind::terminate()).unwrap(); @@ -147,13 +155,13 @@ async fn main() -> Result<()> { info!("Starting application"); - let user_feedback = Feedback::new()?; + let user_feedback = Arc::new(Mutex::new(Feedback::new()?)); let hotspot = Arc::new(Mutex::new(create_hotspot()?)); let error_flag_set = args().any(|e| e == "--error" || e == "-e"); if error_flag_set { error!("Error flag set. Entering error state"); - enter_error_state(user_feedback, hotspot).await; + enter_error_state(user_feedback.clone(), hotspot).await; return Ok(()); } @@ -165,22 +173,24 @@ async fn main() -> Result<()> { let pm3_handle = run_pm3(tx); + user_feedback.lock().await.startup().await; + let loop_handle = handle_ids_loop( rx, hotspot_enable_ids, store.clone(), hotspot.clone(), - user_feedback, + user_feedback.clone(), ); - let webserver_handle = run_webserver(store.clone(), sse_tx, hotspot.clone()); - - feedback::CURRENTSTATUS = Ready; - FeedbackImpl::beep_startup(buzzer); - FeedbackImpl::flash_led_for_duration(led, GREEN, Duration::from_secs(1)); + let webserver_handle = run_webserver( + store.clone(), + sse_tx, + hotspot.clone(), + user_feedback.clone(), + ); let run_result = try_join!(pm3_handle, loop_handle, webserver_handle); - if let Err(e) = run_result { error!("Failed to run application: {e}"); diff --git a/src/spi_led.rs b/src/spi_led.rs index 03e3453..4094690 100644 --- a/src/spi_led.rs +++ b/src/spi_led.rs @@ -1,27 +1,12 @@ use anyhow::Result; -use rgb::RGB8; use rppal::spi::{Bus, Mode, SlaveSelect, Spi}; use smart_leds::SmartLedsWrite; use ws2812_spi::Ws2812; -use crate::{feedback, hardware::StatusLed}; +use crate::hardware::StatusLed; const SPI_CLOCK_SPEED: u32 = 3_800_000; -pub enum CurrentStatus { - Ready, - Hotspot, -} - -impl CurrentStatus { - pub fn color(&self) -> RGB8 { - match self { - CurrentStatus::Ready => RGB8::new(0, 50, 0), - CurrentStatus::Hotspot => RGB8::new(0, 0, 50), - } - } -} - pub struct SpiLed { controller: Ws2812, } @@ -37,7 +22,7 @@ impl SpiLed { impl StatusLed for SpiLed { fn turn_off(&mut self) -> Result<()> { self.controller - .write(vec![feedback::CURRENTSTATUS.color()].into_iter())?; + .write(vec![rgb::RGB8::new(0, 0, 0)].into_iter())?; Ok(()) }