improved error handling in buzzer and led

This commit is contained in:
Djeeberjr 2025-05-12 22:12:26 +02:00
parent 47f19f3c54
commit 7225643296
2 changed files with 14 additions and 14 deletions

View File

@ -1,6 +1,6 @@
use rppal::pwm::{Pwm, Channel, Polarity}; use rppal::pwm::{Channel, Error, Polarity, Pwm};
use tokio::time::sleep; use tokio::time::sleep;
use std::{error::Error, time::Duration}; use std::time::Duration;
pub struct GPIOBuzzer { pub struct GPIOBuzzer {
pwm: Pwm, pwm: Pwm,
@ -11,7 +11,7 @@ impl GPIOBuzzer {
/// 0.5 duty cyle /// 0.5 duty cyle
/// # Arguments /// # Arguments
/// * "channel" - PWM channel for buzzer PWM0 = GPIO 12 / PWM1 = GPIO 13 /// * "channel" - PWM channel for buzzer PWM0 = GPIO 12 / PWM1 = GPIO 13
pub fn new(channel: Channel) -> Result<Self, Box<dyn Error>> { pub fn new(channel: Channel) -> Result<Self, Error> {
// Enable with dummy values; we'll set frequency/duty in the tone method // Enable with dummy values; we'll set frequency/duty in the tone method
let duty_cycle:f64 = 0.5; let duty_cycle:f64 = 0.5;
let pwm = Pwm::with_frequency(channel, 1000.0, duty_cycle, Polarity::Normal, true)?; let pwm = Pwm::with_frequency(channel, 1000.0, duty_cycle, Polarity::Normal, true)?;
@ -25,7 +25,7 @@ impl GPIOBuzzer {
/// # Arguments /// # Arguments
/// * `frequency` - Frequency in Hz. /// * `frequency` - Frequency in Hz.
/// * `duration_ms` - Duration in milliseconds. /// * `duration_ms` - Duration in milliseconds.
async fn modulated_tone(&mut self, frequency: f64, duration_ms: u64) -> Result<(), Box<dyn Error>> { async fn modulated_tone(&mut self, frequency: f64, duration_ms: u64) -> Result<(), Error> {
self.pwm.set_frequency(frequency, 0.5)?; // 50% duty cycle (square wave) self.pwm.set_frequency(frequency, 0.5)?; // 50% duty cycle (square wave)
self.pwm.enable()?; self.pwm.enable()?;
sleep(Duration::from_millis(duration_ms)).await; sleep(Duration::from_millis(duration_ms)).await;
@ -33,7 +33,7 @@ impl GPIOBuzzer {
Ok(()) Ok(())
} }
pub async fn beep_ack(&mut self) -> Result<(), Box<dyn Error>>{ pub async fn beep_ack(&mut self) -> Result<(), Error>{
let sleep_ms: u64 = 10; let sleep_ms: u64 = 10;
self.modulated_tone(1200.0, 100).await?; self.modulated_tone(1200.0, 100).await?;
@ -42,7 +42,7 @@ impl GPIOBuzzer {
Ok(()) Ok(())
} }
pub async fn beep_nak(&mut self) -> Result<(), Box<dyn Error>>{ pub async fn beep_nak(&mut self) -> Result<(), Error>{
let sleep_ms: u64 = 100; let sleep_ms: u64 = 100;
self.modulated_tone(600.0, 150).await?; self.modulated_tone(600.0, 150).await?;
@ -51,7 +51,7 @@ impl GPIOBuzzer {
Ok(()) Ok(())
} }
pub async fn beep_unnkown(&mut self) -> Result<(), Box<dyn Error>>{ pub async fn beep_unnkown(&mut self) -> Result<(), Error>{
let sleep_ms: u64 = 100; let sleep_ms: u64 = 100;
self.modulated_tone(750.0, 100).await?; self.modulated_tone(750.0, 100).await?;

View File

@ -1,6 +1,6 @@
use std::time::Duration; use std::time::Duration;
use rppal::spi::{Bus, Mode, SlaveSelect, Spi}; use rppal::spi::{Bus, Error, Mode, SlaveSelect, Spi};
use smart_leds::SmartLedsWrite; use smart_leds::SmartLedsWrite;
use tokio::time::sleep; use tokio::time::sleep;
use ws2812_spi::Ws2812; use ws2812_spi::Ws2812;
@ -14,27 +14,27 @@ pub struct Led {
const STATUS_DURATION: Duration = Duration::from_secs(1); // 1s sleep for all status led signals const STATUS_DURATION: Duration = Duration::from_secs(1); // 1s sleep for all status led signals
impl Led { impl Led {
pub fn new() -> Result<Self, Box<dyn std::error::Error>> { pub fn new() -> Result<Self, Error> {
let spi = Spi::new(Bus::Spi0, SlaveSelect::Ss0, 3_800_000, Mode::Mode0)?; let spi = Spi::new(Bus::Spi0, SlaveSelect::Ss0, 3_800_000, Mode::Mode0)?;
let controller = Ws2812::new(spi); let controller = Ws2812::new(spi);
Ok(Led { controller }) Ok(Led { controller })
} }
pub async fn turn_green_on_1s(&mut self) -> Result<(), Box<dyn std::error::Error>> { pub async fn turn_green_on_1s(&mut self) -> Result<(), Error> {
self.controller.write(NamedColor::Green.into_iter())?; self.controller.write(NamedColor::Green.into_iter())?;
sleep(STATUS_DURATION).await; sleep(STATUS_DURATION).await;
self.controller.write(NamedColor::Off.into_iter())?; self.turn_off()?;
Ok(()) Ok(())
} }
pub async fn turn_red_on_1s(&mut self) -> Result<(), Box<dyn std::error::Error>> { pub async fn turn_red_on_1s(&mut self) -> Result<(), Error> {
self.controller.write(NamedColor::Red.into_iter())?; self.controller.write(NamedColor::Red.into_iter())?;
sleep(STATUS_DURATION).await; sleep(STATUS_DURATION).await;
self.controller.write(NamedColor::Off.into_iter())?; self.turn_off()?;
Ok(()) Ok(())
} }
pub fn turn_off(&mut self) -> Result<(), Box<dyn std::error::Error>> { pub fn turn_off(&mut self) -> Result<(), Error> {
self.controller.write(NamedColor::Off.into_iter())?; self.controller.write(NamedColor::Off.into_iter())?;
Ok(()) Ok(())
} }