mirror of
https://github.com/Djeeberjr/fw-anwesenheit.git
synced 2026-04-30 18:49:09 +00:00
improved readability
This commit is contained in:
@@ -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<B: Buzzer, L: StatusLed> {
|
||||
device_status: DeviceStatus,
|
||||
buzzer: B,
|
||||
led: L,
|
||||
}
|
||||
@@ -31,6 +45,8 @@ impl<B: Buzzer, L: StatusLed> Feedback<B, L> {
|
||||
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<B: Buzzer, L: StatusLed> Feedback<B, L> {
|
||||
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<B: Buzzer, L: StatusLed> Feedback<B, L> {
|
||||
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<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))
|
||||
@@ -91,8 +128,7 @@ impl<B: Buzzer, L: StatusLed> Feedback<B, L> {
|
||||
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()?,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user