improved readability

This commit is contained in:
Djeeberjr 2025-06-25 20:09:00 +02:00
parent eaca9d8cec
commit 3e079c905f
4 changed files with 94 additions and 65 deletions

View File

@ -5,7 +5,7 @@ use smart_leds::colors::{GREEN, RED};
use std::time::Duration; use std::time::Duration;
use tokio::{join, time::sleep}; use tokio::{join, time::sleep};
use crate::{hardware::{Buzzer, StatusLed}, spi_led}; use crate::hardware::{Buzzer, StatusLed};
#[cfg(not(feature = "mock_pi"))] #[cfg(not(feature = "mock_pi"))]
use crate::{gpio_buzzer::GPIOBuzzer, spi_led::SpiLed}; 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); 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<B: Buzzer, L: StatusLed> { pub struct Feedback<B: Buzzer, L: StatusLed> {
device_status: DeviceStatus,
buzzer: B, buzzer: B,
led: L, led: L,
} }
@ -31,6 +45,8 @@ impl<B: Buzzer, L: StatusLed> Feedback<B, L> {
buzzer_result.unwrap_or_else(|err| { buzzer_result.unwrap_or_else(|err| {
error!("Failed to buzz: {err}"); error!("Failed to buzz: {err}");
}); });
let _ = self.led_to_status();
} }
pub async fn failure(&mut self) { pub async fn failure(&mut self) {
@ -42,6 +58,8 @@ impl<B: Buzzer, L: StatusLed> Feedback<B, L> {
buzzer_result.unwrap_or_else(|err| { buzzer_result.unwrap_or_else(|err| {
error!("Failed to buzz: {err}"); error!("Failed to buzz: {err}");
}); });
let _ = self.led_to_status();
} }
pub async fn activate_error_state(&mut self) -> Result<()> { pub async fn activate_error_state(&mut self) -> Result<()> {
@ -50,24 +68,44 @@ impl<B: Buzzer, L: StatusLed> Feedback<B, L> {
Ok(()) Ok(())
} }
// ------------------ LED ------------------------- pub async fn startup(&mut self){
self.device_status = DeviceStatus::Ready;
/// flash led for amount of time let led_handle = Self::flash_led_for_duration(&mut self.led, GREEN, Duration::from_secs(1));
/// # Arguments let buzzer_handle = Self::beep_startup(&mut self.buzzer);
/// * `led`- led or mockled
/// * `color` - enum color let (buzzer_result, led_result) = join!(buzzer_handle, led_handle);
/// * `duration` - duration in ms
pub async fn flash_led_for_duration(led: &mut L, color: RGB8, duration: Duration) -> Result<()> { 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)?; led.turn_on(color)?;
sleep(duration).await; sleep(duration).await;
led.turn_off()?; led.turn_off()?;
Ok(()) Ok(())
} }
// ----------------- BUZZER ------------------------
/// acknowledge beep tone
async fn beep_ack(buzzer: &mut B) -> Result<()> { async fn beep_ack(buzzer: &mut B) -> Result<()> {
buzzer buzzer
.modulated_tone(1200.0, Duration::from_millis(100)) .modulated_tone(1200.0, Duration::from_millis(100))
@ -79,7 +117,6 @@ impl<B: Buzzer, L: StatusLed> Feedback<B, L> {
Ok(()) Ok(())
} }
/// Not acknowledge beep tone
async fn beep_nak(buzzer: &mut B) -> Result<()> { async fn beep_nak(buzzer: &mut B) -> Result<()> {
buzzer buzzer
.modulated_tone(600.0, Duration::from_millis(150)) .modulated_tone(600.0, Duration::from_millis(150))
@ -91,8 +128,7 @@ impl<B: Buzzer, L: StatusLed> Feedback<B, L> {
Ok(()) Ok(())
} }
/// beep tone for starting the device async fn beep_startup(buzzer: &mut B) -> Result<()> {
pub async fn beep_startup(buzzer: &mut B) -> Result<()> {
buzzer buzzer
.modulated_tone(523.0, Duration::from_millis(150)) .modulated_tone(523.0, Duration::from_millis(150))
.await?; .await?;
@ -128,6 +164,7 @@ impl FeedbackImpl {
#[cfg(feature = "mock_pi")] #[cfg(feature = "mock_pi")]
{ {
Ok(Feedback { Ok(Feedback {
device_status: DeviceStatus::NotReady,
buzzer: MockBuzzer {}, buzzer: MockBuzzer {},
led: MockLed {}, led: MockLed {},
}) })
@ -135,6 +172,7 @@ impl FeedbackImpl {
#[cfg(not(feature = "mock_pi"))] #[cfg(not(feature = "mock_pi"))]
{ {
Ok(Feedback { Ok(Feedback {
device_status: DeviceStatus::NotReady,
buzzer: GPIOBuzzer::new_default()?, buzzer: GPIOBuzzer::new_default()?,
led: SpiLed::new()?, led: SpiLed::new()?,
}) })

View File

@ -1,10 +1,9 @@
use anyhow::{Result, anyhow}; use anyhow::{Result, anyhow};
use log::{trace, warn}; use log::{trace, warn};
use smart_leds::colors::GREEN; use std::env;
use std::{env, time::Duration};
use tokio::process::Command; use tokio::process::Command;
use crate::{feedback::{self, FeedbackImpl}, hardware::Hotspot, spi_led}; use crate::hardware::Hotspot;
const SSID: &str = "fwa"; const SSID: &str = "fwa";
const CON_NAME: &str = "fwa-hotspot"; const CON_NAME: &str = "fwa-hotspot";
@ -127,9 +126,6 @@ impl Hotspot for NMHotspot {
return Err(anyhow!("nmcli command had non-zero exit code")); 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(()) Ok(())
} }
} }

View File

@ -7,7 +7,6 @@ use hardware::{Hotspot, create_hotspot};
use id_store::IDStore; use id_store::IDStore;
use log::{error, info, warn}; use log::{error, info, warn};
use pm3::run_pm3; use pm3::run_pm3;
use smart_leds::colors::BLUE;
use std::{ use std::{
env::{self, args}, env::{self, args},
sync::Arc, sync::Arc,
@ -46,6 +45,7 @@ async fn run_webserver<H>(
store: Arc<Mutex<IDStore>>, store: Arc<Mutex<IDStore>>,
id_channel: Sender<String>, id_channel: Sender<String>,
hotspot: Arc<Mutex<H>>, hotspot: Arc<Mutex<H>>,
user_feedback: Arc<Mutex<FeedbackImpl>>,
) -> Result<()> ) -> Result<()>
where where
H: Hotspot + Send + Sync + 'static, H: Hotspot + Send + Sync + 'static,
@ -53,8 +53,13 @@ where
let activity_channel = spawn_idle_watcher(Duration::from_secs(60 * 30), move || { let activity_channel = spawn_idle_watcher(Duration::from_secs(60 * 30), move || {
info!("No activity on webserver. Disabling hotspot"); info!("No activity on webserver. Disabling hotspot");
let cloned_hotspot = hotspot.clone(); let cloned_hotspot = hotspot.clone();
let cloned_user_feedback = user_feedback.clone();
tokio::spawn(async move { tokio::spawn(async move {
let _ = cloned_hotspot.lock().await.disable_hotspot().await; 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?; start_webserver(store, id_channel, notifier).await?;
feedback::CURRENTSTATUS = Hotspot;
FeedbackImpl::flash_led_for_duration(led, BLUE, 1000);
Ok(()) Ok(())
} }
@ -99,32 +101,38 @@ async fn handle_ids_loop(
hotspot_enable_ids: Vec<TallyID>, hotspot_enable_ids: Vec<TallyID>,
id_store: Arc<Mutex<IDStore>>, id_store: Arc<Mutex<IDStore>>,
hotspot: Arc<Mutex<impl Hotspot>>, hotspot: Arc<Mutex<impl Hotspot>>,
mut user_feedback: FeedbackImpl, user_feedback: Arc<Mutex<FeedbackImpl>>,
) -> Result<()> { ) -> Result<()> {
while let Ok(tally_id_string) = id_channel.recv().await { while let Ok(tally_id_string) = id_channel.recv().await {
let tally_id = TallyID(tally_id_string); let tally_id = TallyID(tally_id_string);
if hotspot_enable_ids.contains(&tally_id) { if hotspot_enable_ids.contains(&tally_id) {
info!("Enableing hotspot"); info!("Enableing hotspot");
hotspot let hotspot_enable_result = hotspot.lock().await.enable_hotspot().await;
.lock()
.await match hotspot_enable_result {
.enable_hotspot() Ok(_) => {
.await user_feedback
.unwrap_or_else(|err| { .lock()
error!("Hotspot: {err}"); .await
}); .set_device_status(feedback::DeviceStatus::HotspotEnabled);
}
Err(e) => {
error!("Hotspot: {e}");
}
}
// TODO: Should the ID be added anyway or ignored ? // TODO: Should the ID be added anyway or ignored ?
} }
if id_store.lock().await.add_id(tally_id) { if id_store.lock().await.add_id(tally_id) {
info!("Added new id to current day"); 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 { if let Err(e) = id_store.lock().await.export_json(STORE_PATH).await {
error!("Failed to save id store to file: {e}"); 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 ? // TODO: How to handle a failure to save ?
} }
} }
@ -133,8 +141,8 @@ async fn handle_ids_loop(
Ok(()) Ok(())
} }
async fn enter_error_state(mut feedback: FeedbackImpl, hotspot: Arc<Mutex<impl Hotspot>>) { async fn enter_error_state(feedback: Arc<Mutex<FeedbackImpl>>, hotspot: Arc<Mutex<impl Hotspot>>) {
let _ = feedback.activate_error_state().await; let _ = feedback.lock().await.activate_error_state().await;
let _ = hotspot.lock().await.enable_hotspot().await; let _ = hotspot.lock().await.enable_hotspot().await;
let mut sigterm = signal(SignalKind::terminate()).unwrap(); let mut sigterm = signal(SignalKind::terminate()).unwrap();
@ -147,13 +155,13 @@ async fn main() -> Result<()> {
info!("Starting application"); 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 hotspot = Arc::new(Mutex::new(create_hotspot()?));
let error_flag_set = args().any(|e| e == "--error" || e == "-e"); let error_flag_set = args().any(|e| e == "--error" || e == "-e");
if error_flag_set { if error_flag_set {
error!("Error flag set. Entering error state"); error!("Error flag set. Entering error state");
enter_error_state(user_feedback, hotspot).await; enter_error_state(user_feedback.clone(), hotspot).await;
return Ok(()); return Ok(());
} }
@ -165,22 +173,24 @@ async fn main() -> Result<()> {
let pm3_handle = run_pm3(tx); let pm3_handle = run_pm3(tx);
user_feedback.lock().await.startup().await;
let loop_handle = handle_ids_loop( let loop_handle = handle_ids_loop(
rx, rx,
hotspot_enable_ids, hotspot_enable_ids,
store.clone(), store.clone(),
hotspot.clone(), hotspot.clone(),
user_feedback, user_feedback.clone(),
); );
let webserver_handle = run_webserver(store.clone(), sse_tx, hotspot.clone()); let webserver_handle = run_webserver(
store.clone(),
feedback::CURRENTSTATUS = Ready; sse_tx,
FeedbackImpl::beep_startup(buzzer); hotspot.clone(),
FeedbackImpl::flash_led_for_duration(led, GREEN, Duration::from_secs(1)); user_feedback.clone(),
);
let run_result = try_join!(pm3_handle, loop_handle, webserver_handle); let run_result = try_join!(pm3_handle, loop_handle, webserver_handle);
if let Err(e) = run_result { if let Err(e) = run_result {
error!("Failed to run application: {e}"); error!("Failed to run application: {e}");

View File

@ -1,27 +1,12 @@
use anyhow::Result; use anyhow::Result;
use rgb::RGB8;
use rppal::spi::{Bus, Mode, SlaveSelect, Spi}; use rppal::spi::{Bus, Mode, SlaveSelect, Spi};
use smart_leds::SmartLedsWrite; use smart_leds::SmartLedsWrite;
use ws2812_spi::Ws2812; use ws2812_spi::Ws2812;
use crate::{feedback, hardware::StatusLed}; use crate::hardware::StatusLed;
const SPI_CLOCK_SPEED: u32 = 3_800_000; 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 { pub struct SpiLed {
controller: Ws2812<Spi>, controller: Ws2812<Spi>,
} }
@ -37,7 +22,7 @@ impl SpiLed {
impl StatusLed for SpiLed { impl StatusLed for SpiLed {
fn turn_off(&mut self) -> Result<()> { fn turn_off(&mut self) -> Result<()> {
self.controller self.controller
.write(vec![feedback::CURRENTSTATUS.color()].into_iter())?; .write(vec![rgb::RGB8::new(0, 0, 0)].into_iter())?;
Ok(()) Ok(())
} }