From efd096a14958ef61355e1176534690d8b4280bb1 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Mon, 2 Jun 2025 14:46:24 +0200 Subject: [PATCH] refactored hardware components detach trait from mock and hardware implementation --- src/feedback.rs | 8 +++--- src/{buzzer.rs => gpio_buzzer.rs} | 24 ++++++++-------- src/hardware.rs | 47 +++++++++++++++++++++++++++++++ src/hotspot.rs | 12 ++------ src/main.rs | 26 ++++------------- src/mock.rs | 6 ++-- src/{led.rs => spi_led.rs} | 8 ++---- 7 files changed, 75 insertions(+), 56 deletions(-) rename src/{buzzer.rs => gpio_buzzer.rs} (65%) create mode 100644 src/hardware.rs rename src/{led.rs => spi_led.rs} (83%) diff --git a/src/feedback.rs b/src/feedback.rs index 6701584..bb87bf3 100644 --- a/src/feedback.rs +++ b/src/feedback.rs @@ -4,10 +4,10 @@ use smart_leds::colors::{GREEN, RED}; use std::{error::Error, time::Duration}; use tokio::{join, time::sleep}; -use crate::{ - buzzer::{Buzzer, GPIOBuzzer}, - led::{SpiLed, StatusLed}, -}; +use crate::hardware::{Buzzer, StatusLed}; + +#[cfg(not(feature = "mock_pi"))] +use crate::{gpio_buzzer::GPIOBuzzer, spi_led::SpiLed}; #[cfg(feature = "mock_pi")] use crate::mock::{MockBuzzer, MockLed}; diff --git a/src/buzzer.rs b/src/gpio_buzzer.rs similarity index 65% rename from src/buzzer.rs rename to src/gpio_buzzer.rs index 0ac517e..1c82bb2 100644 --- a/src/buzzer.rs +++ b/src/gpio_buzzer.rs @@ -1,23 +1,17 @@ -use rppal::pwm::{Channel, Error, Polarity, Pwm}; -use std::{future::Future, time::Duration}; +use rppal::pwm::{Channel, Polarity, Pwm}; +use std::{error::Error, time::Duration}; use tokio::time::sleep; -const DEFAULT_PWM_CHANNEL_BUZZER: Channel = Channel::Pwm0; //PWM0 = GPIO18/Physical pin 12 +use crate::hardware::Buzzer; -pub trait Buzzer { - fn modulated_tone( - &mut self, - frequency_hz: f64, - duration: Duration, - ) -> impl Future> + std::marker::Send; -} +const DEFAULT_PWM_CHANNEL_BUZZER: Channel = Channel::Pwm0; //PWM0 = GPIO18/Physical pin 12 pub struct GPIOBuzzer { pwm: Pwm, } impl GPIOBuzzer { - pub fn new_from_channel(channel: Channel) -> Result { + pub fn new_from_channel(channel: Channel) -> Result { // Enable with dummy values; we'll set frequency/duty in the tone method let duty_cycle: f64 = 0.5; let pwm = Pwm::with_frequency(channel, 1000.0, duty_cycle, Polarity::Normal, true)?; @@ -26,13 +20,17 @@ impl GPIOBuzzer { Ok(GPIOBuzzer { pwm }) } - pub fn new_default() -> Result { + pub fn new_default() -> Result { Self::new_from_channel(DEFAULT_PWM_CHANNEL_BUZZER) } } impl Buzzer for GPIOBuzzer { - async fn modulated_tone(&mut self, frequency_hz: f64, duration: Duration) -> Result<(), Error> { + async fn modulated_tone( + &mut self, + frequency_hz: f64, + duration: Duration, + ) -> Result<(), Box> { self.pwm.set_frequency(frequency_hz, 0.5)?; // 50% duty cycle (square wave) self.pwm.enable()?; sleep(duration).await; diff --git a/src/hardware.rs b/src/hardware.rs new file mode 100644 index 0000000..d96fde1 --- /dev/null +++ b/src/hardware.rs @@ -0,0 +1,47 @@ +use std::{error::Error, time::Duration}; + +use crate::hotspot::{HotspotError}; + +#[cfg(feature = "mock_pi")] +use crate::mock::MockHotspot; + +#[cfg(not(feature = "mock_pi"))] +use crate::hotspot::NMHotspot; + +pub trait StatusLed { + fn turn_off(&mut self) -> Result<(), Box>; + + fn turn_on(&mut self, color: rgb::RGB8) -> Result<(), Box>; +} + +pub trait Buzzer { + fn modulated_tone( + &mut self, + frequency_hz: f64, + duration: Duration, + ) -> impl Future>> + std::marker::Send; +} + +pub trait Hotspot { + fn enable_hotspot( + &self, + ) -> impl std::future::Future> + std::marker::Send; + + fn disable_hotspot( + &self, + ) -> impl std::future::Future> + std::marker::Send; +} + +/// Create a struct to manage the hotspot +/// Respects the `mock_pi` flag. +pub fn create_hotspot() -> Result { + #[cfg(feature = "mock_pi")] + { + Ok(MockHotspot {}) + } + + #[cfg(not(feature = "mock_pi"))] + { + NMHotspot::new_from_env() + } +} diff --git a/src/hotspot.rs b/src/hotspot.rs index cc7dd89..4a1847f 100644 --- a/src/hotspot.rs +++ b/src/hotspot.rs @@ -6,6 +6,8 @@ use std::{ }; use tokio::process::Command; +use crate::hardware::Hotspot; + const SSID: &str = "fwa"; const CON_NAME: &str = "fwa-hotspot"; const PASSWORD: &str = "a9LG2kUVrsRRVUo1"; @@ -41,16 +43,6 @@ impl fmt::Display for HotspotError { impl std::error::Error for HotspotError {} -pub trait Hotspot { - fn enable_hotspot( - &self, - ) -> impl std::future::Future> + std::marker::Send; - - fn disable_hotspot( - &self, - ) -> impl std::future::Future> + std::marker::Send; -} - /// NetworkManager Hotspot pub struct NMHotspot { ssid: String, diff --git a/src/main.rs b/src/main.rs index f62cbf0..a96fe23 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ +#![allow(dead_code)] + use activity_fairing::{ActivityNotifier, spawn_idle_watcher}; use feedback::{Feedback, FeedbackImpl}; -use hotspot::{Hotspot, HotspotError, NMHotspot}; +use hardware::{Hotspot, create_hotspot}; use id_store::IDStore; use log::{error, info, warn}; use pm3::run_pm3; @@ -16,39 +18,23 @@ use tokio::{ }; use webserver::start_webserver; -#[cfg(feature = "mock_pi")] -use mock::MockHotspot; - mod activity_fairing; -mod buzzer; mod feedback; +mod gpio_buzzer; +mod hardware; mod hotspot; mod id_mapping; mod id_store; -mod led; mod logger; mod mock; mod parser; mod pm3; +mod spi_led; mod tally_id; mod webserver; const STORE_PATH: &str = "./data.json"; -/// Create a struct to manage the hotspot -/// Respects the `mock_pi` flag. -fn create_hotspot() -> Result { - #[cfg(feature = "mock_pi")] - { - Ok(MockHotspot {}) - } - - #[cfg(not(feature = "mock_pi"))] - { - NMHotspot::new_from_env() - } -} - async fn run_webserver( store: Arc>, id_channel: Sender, diff --git a/src/mock.rs b/src/mock.rs index 552e9f6..b2ed20b 100644 --- a/src/mock.rs +++ b/src/mock.rs @@ -1,9 +1,9 @@ -use std::time::Duration; +use std::{error::Error, time::Duration}; use log::debug; use tokio::time::sleep; -use crate::{buzzer::Buzzer, hotspot::Hotspot, led::StatusLed}; +use crate::hardware::{Buzzer, Hotspot, StatusLed}; pub struct MockBuzzer {} @@ -12,7 +12,7 @@ impl Buzzer for MockBuzzer { &mut self, frequency_hz: f64, duration: Duration, - ) -> Result<(), rppal::pwm::Error> { + ) -> Result<(), Box> { debug!("MockBuzzer: modulte tone: {frequency_hz} Hz"); sleep(duration).await; Ok(()) diff --git a/src/led.rs b/src/spi_led.rs similarity index 83% rename from src/led.rs rename to src/spi_led.rs index 0602fea..936ddfd 100644 --- a/src/led.rs +++ b/src/spi_led.rs @@ -3,14 +3,10 @@ use smart_leds::SmartLedsWrite; use std::error::Error; use ws2812_spi::Ws2812; +use crate::hardware::StatusLed; + const SPI_CLOCK_SPEED: u32 = 3_800_000; -pub trait StatusLed { - fn turn_off(&mut self) -> Result<(), Box>; - - fn turn_on(&mut self, color: rgb::RGB8) -> Result<(), Box>; -} - pub struct SpiLed { controller: Ws2812, }