refactored main function

- moved logger into its own file
- moved code from main into its own function
This commit is contained in:
2025-06-01 18:03:16 +02:00
parent 3fe2f3f376
commit 7a438d1a9f
3 changed files with 184 additions and 162 deletions

70
src/feedback.rs Normal file
View File

@@ -0,0 +1,70 @@
use log::error;
use rppal::pwm::Channel;
use std::error::Error;
use tokio::join;
use crate::{
buzzer::{Buzzer, GPIOBuzzer},
led::{SpiLed, StatusLed},
};
#[cfg(feature = "mock_pi")]
use crate::mock::{MockBuzzer, MockLed};
const PWM_CHANNEL_BUZZER: Channel = Channel::Pwm0; //PWM0 = GPIO18/Physical pin 12
pub struct Feedback<B: Buzzer, L: StatusLed> {
buzzer: B,
led: L,
}
impl<B: Buzzer, L: StatusLed> Feedback<B, L> {
pub async fn success(&mut self) {
let (buzzer_result, led_result) =
join!(self.buzzer.beep_ack(), self.led.turn_green_on_1s());
buzzer_result.unwrap_or_else(|err| {
error!("Failed to buzz: {err}");
});
led_result.unwrap_or_else(|err| {
error!("Failed to set LED: {err}");
});
}
pub async fn failure(&mut self) {
let (buzzer_result, led_result) = join!(self.buzzer.beep_nak(), self.led.turn_red_on_1s());
buzzer_result.unwrap_or_else(|err| {
error!("Failed to buzz: {err}");
});
led_result.unwrap_or_else(|err| {
error!("Failed to set LED: {err}");
});
}
}
#[cfg(feature = "mock_pi")]
pub type FeedbackImpl = Feedback<MockBuzzer, MockLed>;
#[cfg(not(feature = "mock_pi"))]
pub type FeedbackImpl = Feedback<GPIOBuzzer, SpiLed>;
impl FeedbackImpl {
pub fn new() -> Result<Self, Box<dyn Error>> {
#[cfg(feature = "mock_pi")]
{
Ok(Feedback {
buzzer: MockBuzzer {},
led: MockLed {},
})
}
#[cfg(not(feature = "mock_pi"))]
{
Ok(Feedback {
buzzer: GPIOBuzzer::new(PWM_CHANNEL_BUZZER)?,
led: SpiLed::new()?,
})
}
}
}