added passive status led + startup sound

This commit is contained in:
Philipp 2025-06-17 18:06:23 +02:00
parent 1514409070
commit cd713d5849
4 changed files with 75 additions and 7 deletions

View File

@ -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<B: Buzzer, L: StatusLed> {
buzzer: B,
led: L,
@ -23,7 +25,7 @@ pub struct Feedback<B: Buzzer, L: StatusLed> {
impl<B: Buzzer, L: StatusLed> Feedback<B, L> {
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<B: Buzzer, L: StatusLed> Feedback<B, L> {
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<B: Buzzer, L: StatusLed> Feedback<B, L> {
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<B: Buzzer, L: StatusLed> Feedback<B, L> {
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<B: Buzzer, L: StatusLed> Feedback<B, L> {
.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")]

View File

@ -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(())
}
}

View File

@ -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(())
}
@ -172,10 +177,14 @@ async fn main() -> Result<()> {
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(())
}

View File

@ -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<Spi>,
}
@ -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(())
}