diff --git a/src/feedback.rs b/src/feedback.rs index 796f120..b510876 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}; +use crate::{hardware::{Buzzer, StatusLed}, spi_led}; #[cfg(not(feature = "mock_pi"))] use crate::{gpio_buzzer::GPIOBuzzer, spi_led::SpiLed}; @@ -15,6 +15,8 @@ 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 struct Feedback { buzzer: B, led: L, @@ -23,7 +25,7 @@ pub struct Feedback { impl Feedback { pub async fn success(&mut self) { let buzzer_handle = Self::beep_ack(&mut self.buzzer); - let led_handle = Self::blink_led_for_duration(&mut self.led, GREEN, LED_BLINK_DURATION); + let led_handle = Self::flash_led_for_duration(&mut self.led, GREEN, LED_BLINK_DURATION); let (buzzer_result, _) = join!(buzzer_handle, led_handle); buzzer_result.unwrap_or_else(|err| { @@ -33,7 +35,7 @@ impl Feedback { pub async fn failure(&mut self) { let buzzer_handle = Self::beep_nak(&mut self.buzzer); - let led_handle = Self::blink_led_for_duration(&mut self.led, RED, LED_BLINK_DURATION); + let led_handle = Self::flash_led_for_duration(&mut self.led, RED, LED_BLINK_DURATION); let (buzzer_result, _) = join!(buzzer_handle, led_handle); @@ -48,13 +50,24 @@ impl Feedback { Ok(()) } - async fn blink_led_for_duration(led: &mut L, color: RGB8, duration: Duration) -> Result<()> { + // ------------------ LED ------------------------- + + /// 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<()> { 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)) @@ -66,6 +79,7 @@ impl Feedback { Ok(()) } + /// Not acknowledge beep tone async fn beep_nak(buzzer: &mut B) -> Result<()> { buzzer .modulated_tone(600.0, Duration::from_millis(150)) @@ -76,6 +90,32 @@ impl Feedback { .await?; Ok(()) } + + /// beep tone for starting the device + pub async fn beep_startup(buzzer: &mut B) -> Result<()> { + buzzer + .modulated_tone(523.0, Duration::from_millis(150)) + .await?; + buzzer + .modulated_tone(659.0, Duration::from_millis(150)) + .await?; + buzzer + .modulated_tone(784.0, Duration::from_millis(150)) + .await?; + buzzer + .modulated_tone(1046.0, Duration::from_millis(200)) + .await?; + + sleep(Duration::from_millis(100)).await; + + buzzer + .modulated_tone(784.0, Duration::from_millis(100)) + .await?; + buzzer + .modulated_tone(880.0, Duration::from_millis(200)) + .await?; + Ok(()) + } } #[cfg(feature = "mock_pi")] diff --git a/src/hotspot.rs b/src/hotspot.rs index ec9cfc3..a929ae8 100644 --- a/src/hotspot.rs +++ b/src/hotspot.rs @@ -1,9 +1,10 @@ use anyhow::{Result, anyhow}; use log::{trace, warn}; +use smart_leds::colors::GREEN; use std::env; use tokio::process::Command; -use crate::hardware::Hotspot; +use crate::{feedback::{self, FeedbackImpl}, hardware::Hotspot, spi_led}; const SSID: &str = "fwa"; const CON_NAME: &str = "fwa-hotspot"; @@ -126,6 +127,9 @@ impl Hotspot for NMHotspot { return Err(anyhow!("nmcli command had non-zero exit code")); } + feedback::CURRENTSTATUS = Ready; + FeedbackImpl::flash_led_for_duration(led, GREEN, 1000); + Ok(()) } } diff --git a/src/main.rs b/src/main.rs index c855968..7c80244 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ 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, @@ -62,6 +63,10 @@ where }; start_webserver(store, id_channel, notifier).await?; + + feedback::CURRENTSTATUS = Hotspot; + FeedbackImpl::flash_led_for_duration(led, BLUE, 1000); + Ok(()) } @@ -171,11 +176,15 @@ async fn main() -> Result<()> { let webserver_handle = run_webserver(store.clone(), sse_tx, hotspot.clone()); let run_result = try_join!(pm3_handle, loop_handle, webserver_handle); + if let Err(e) = run_result { error!("Failed to run application: {e}"); return Err(e); } + feedback::CURRENTSTATUS = Ready; + FeedbackImpl::beep_startup(buzzer); + Ok(()) } diff --git a/src/spi_led.rs b/src/spi_led.rs index 4094690..03e3453 100644 --- a/src/spi_led.rs +++ b/src/spi_led.rs @@ -1,12 +1,27 @@ use anyhow::Result; +use rgb::RGB8; use rppal::spi::{Bus, Mode, SlaveSelect, Spi}; use smart_leds::SmartLedsWrite; use ws2812_spi::Ws2812; -use crate::hardware::StatusLed; +use crate::{feedback, 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, } @@ -22,7 +37,7 @@ impl SpiLed { impl StatusLed for SpiLed { fn turn_off(&mut self) -> Result<()> { self.controller - .write(vec![rgb::RGB8::new(0, 0, 0)].into_iter())?; + .write(vec![feedback::CURRENTSTATUS.color()].into_iter())?; Ok(()) }