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")]