mirror of
https://github.com/Djeeberjr/fw-anwesenheit.git
synced 2026-04-30 18:49:09 +00:00
moved related files into own module
This commit is contained in:
37
src/hardware/gpio_buzzer.rs
Normal file
37
src/hardware/gpio_buzzer.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use anyhow::Result;
|
||||
use rppal::pwm::{Channel, Polarity, Pwm};
|
||||
use std::time::Duration;
|
||||
use tokio::time::sleep;
|
||||
|
||||
use crate::hardware::Buzzer;
|
||||
|
||||
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<Self, rppal::pwm::Error> {
|
||||
// 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)?;
|
||||
pwm.disable()?;
|
||||
|
||||
Ok(GPIOBuzzer { pwm })
|
||||
}
|
||||
|
||||
pub fn new_default() -> Result<Self, rppal::pwm::Error> {
|
||||
Self::new_from_channel(DEFAULT_PWM_CHANNEL_BUZZER)
|
||||
}
|
||||
}
|
||||
|
||||
impl Buzzer for GPIOBuzzer {
|
||||
async fn modulated_tone(&mut self, frequency_hz: f64, duration: Duration) -> Result<()> {
|
||||
self.pwm.set_frequency(frequency_hz, 0.5)?; // 50% duty cycle (square wave)
|
||||
self.pwm.enable()?;
|
||||
sleep(duration).await;
|
||||
self.pwm.disable()?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
131
src/hardware/hotspot.rs
Normal file
131
src/hardware/hotspot.rs
Normal file
@@ -0,0 +1,131 @@
|
||||
use anyhow::{Result, anyhow};
|
||||
use log::{trace, warn};
|
||||
use std::env;
|
||||
use tokio::process::Command;
|
||||
|
||||
use crate::hardware::Hotspot;
|
||||
|
||||
const SSID: &str = "fwa";
|
||||
const CON_NAME: &str = "fwa-hotspot";
|
||||
const PASSWORD: &str = "a9LG2kUVrsRRVUo1";
|
||||
const IPV4_ADDRES: &str = "192.168.4.1/24";
|
||||
|
||||
/// NetworkManager Hotspot
|
||||
pub struct NMHotspot {
|
||||
ssid: String,
|
||||
con_name: String,
|
||||
password: String,
|
||||
ipv4: String,
|
||||
}
|
||||
|
||||
impl NMHotspot {
|
||||
pub fn new_from_env() -> Result<Self> {
|
||||
let ssid = env::var("HOTSPOT_SSID").unwrap_or(SSID.to_owned());
|
||||
let password = env::var("HOTSPOT_PW").unwrap_or_else(|_| {
|
||||
warn!("HOTSPOT_PW not set. Using default password");
|
||||
PASSWORD.to_owned()
|
||||
});
|
||||
|
||||
if password.len() < 8 {
|
||||
return Err(anyhow!("Hotspot password to short"));
|
||||
}
|
||||
|
||||
Ok(NMHotspot {
|
||||
ssid,
|
||||
con_name: CON_NAME.to_owned(),
|
||||
password,
|
||||
ipv4: IPV4_ADDRES.to_owned(),
|
||||
})
|
||||
}
|
||||
|
||||
async fn create_hotspot(&self) -> Result<()> {
|
||||
let cmd = Command::new("nmcli")
|
||||
.args(["device", "wifi", "hotspot"])
|
||||
.arg("con-name")
|
||||
.arg(&self.con_name)
|
||||
.arg("ssid")
|
||||
.arg(&self.ssid)
|
||||
.arg("password")
|
||||
.arg(&self.password)
|
||||
.output()
|
||||
.await?;
|
||||
|
||||
trace!("nmcli (std): {}", String::from_utf8_lossy(&cmd.stdout));
|
||||
trace!("nmcli (err): {}", String::from_utf8_lossy(&cmd.stderr));
|
||||
|
||||
if !cmd.status.success() {
|
||||
return Err(anyhow!("nmcli command had non-zero exit code"));
|
||||
}
|
||||
|
||||
let cmd = Command::new("nmcli")
|
||||
.arg("connection")
|
||||
.arg("modify")
|
||||
.arg(&self.con_name)
|
||||
.arg("ipv4.method")
|
||||
.arg("shared")
|
||||
.arg("ipv4.addresses")
|
||||
.arg(&self.ipv4)
|
||||
.output()
|
||||
.await?;
|
||||
|
||||
if !cmd.status.success() {
|
||||
return Err(anyhow!("nmcli command had non-zero exit code"));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Checks if the connection already exists
|
||||
async fn exists(&self) -> Result<bool> {
|
||||
let cmd = Command::new("nmcli")
|
||||
.args(["connection", "show"])
|
||||
.arg(&self.con_name)
|
||||
.output()
|
||||
.await?;
|
||||
|
||||
trace!("nmcli (std): {}", String::from_utf8_lossy(&cmd.stdout));
|
||||
trace!("nmcli (err): {}", String::from_utf8_lossy(&cmd.stderr));
|
||||
|
||||
Ok(cmd.status.success())
|
||||
}
|
||||
}
|
||||
|
||||
impl Hotspot for NMHotspot {
|
||||
async fn enable_hotspot(&self) -> Result<()> {
|
||||
if !self.exists().await? {
|
||||
self.create_hotspot().await?;
|
||||
}
|
||||
|
||||
let cmd = Command::new("nmcli")
|
||||
.args(["connection", "up"])
|
||||
.arg(&self.con_name)
|
||||
.output()
|
||||
.await?;
|
||||
|
||||
trace!("nmcli (std): {}", String::from_utf8_lossy(&cmd.stdout));
|
||||
trace!("nmcli (err): {}", String::from_utf8_lossy(&cmd.stderr));
|
||||
|
||||
if !cmd.status.success() {
|
||||
return Err(anyhow!("nmcli command had non-zero exit code"));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn disable_hotspot(&self) -> Result<()> {
|
||||
let cmd = Command::new("nmcli")
|
||||
.args(["connection", "down"])
|
||||
.arg(&self.con_name)
|
||||
.output()
|
||||
.await?;
|
||||
|
||||
trace!("nmcli (std): {}", String::from_utf8_lossy(&cmd.stdout));
|
||||
trace!("nmcli (err): {}", String::from_utf8_lossy(&cmd.stderr));
|
||||
|
||||
if !cmd.status.success() {
|
||||
return Err(anyhow!("nmcli command had non-zero exit code"));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
44
src/hardware/mock.rs
Normal file
44
src/hardware/mock.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
use anyhow::Result;
|
||||
use log::debug;
|
||||
use std::time::Duration;
|
||||
use tokio::time::sleep;
|
||||
|
||||
use crate::hardware::{Buzzer, Hotspot, StatusLed};
|
||||
|
||||
pub struct MockBuzzer {}
|
||||
|
||||
impl Buzzer for MockBuzzer {
|
||||
async fn modulated_tone(&mut self, frequency_hz: f64, duration: Duration) -> Result<()> {
|
||||
debug!("MockBuzzer: modulte tone: {frequency_hz} Hz");
|
||||
sleep(duration).await;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MockLed {}
|
||||
|
||||
impl StatusLed for MockLed {
|
||||
fn turn_off(&mut self) -> Result<()> {
|
||||
debug!("Turn mock LED off");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn turn_on(&mut self, color: rgb::RGB8) -> Result<()> {
|
||||
debug!("Turn mock LED on to: {color}");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MockHotspot {}
|
||||
|
||||
impl Hotspot for MockHotspot {
|
||||
async fn enable_hotspot(&self) -> Result<()> {
|
||||
debug!("Mockhotspot: Enable hotspot");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn disable_hotspot(&self) -> Result<()> {
|
||||
debug!("Mockhotspot: Disable hotspot");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
45
src/hardware/mod.rs
Normal file
45
src/hardware/mod.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use anyhow::Result;
|
||||
use std::time::Duration;
|
||||
|
||||
mod gpio_buzzer;
|
||||
mod hotspot;
|
||||
mod mock;
|
||||
mod spi_led;
|
||||
|
||||
pub use gpio_buzzer::GPIOBuzzer;
|
||||
pub use mock::{MockBuzzer, MockHotspot, MockLed};
|
||||
pub use spi_led::SpiLed;
|
||||
|
||||
pub trait StatusLed {
|
||||
fn turn_off(&mut self) -> Result<()>;
|
||||
|
||||
fn turn_on(&mut self, color: rgb::RGB8) -> Result<()>;
|
||||
}
|
||||
|
||||
pub trait Buzzer {
|
||||
fn modulated_tone(
|
||||
&mut self,
|
||||
frequency_hz: f64,
|
||||
duration: Duration,
|
||||
) -> impl Future<Output = Result<()>> + std::marker::Send;
|
||||
}
|
||||
|
||||
pub trait Hotspot {
|
||||
fn enable_hotspot(&self) -> impl std::future::Future<Output = Result<()>> + std::marker::Send;
|
||||
|
||||
fn disable_hotspot(&self) -> impl std::future::Future<Output = Result<()>> + std::marker::Send;
|
||||
}
|
||||
|
||||
/// Create a struct to manage the hotspot
|
||||
/// Respects the `mock_pi` flag.
|
||||
pub fn create_hotspot() -> Result<impl Hotspot> {
|
||||
#[cfg(feature = "mock_pi")]
|
||||
{
|
||||
Ok(mock::MockHotspot {})
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "mock_pi"))]
|
||||
{
|
||||
hotspot::NMHotspot::new_from_env()
|
||||
}
|
||||
}
|
||||
33
src/hardware/spi_led.rs
Normal file
33
src/hardware/spi_led.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
use anyhow::Result;
|
||||
use rppal::spi::{Bus, Mode, SlaveSelect, Spi};
|
||||
use smart_leds::SmartLedsWrite;
|
||||
use ws2812_spi::Ws2812;
|
||||
|
||||
use crate::hardware::StatusLed;
|
||||
|
||||
const SPI_CLOCK_SPEED: u32 = 3_800_000;
|
||||
|
||||
pub struct SpiLed {
|
||||
controller: Ws2812<Spi>,
|
||||
}
|
||||
|
||||
impl SpiLed {
|
||||
pub fn new() -> Result<Self, rppal::spi::Error> {
|
||||
let spi = Spi::new(Bus::Spi0, SlaveSelect::Ss0, SPI_CLOCK_SPEED, Mode::Mode0)?;
|
||||
let controller = Ws2812::new(spi);
|
||||
Ok(SpiLed { controller })
|
||||
}
|
||||
}
|
||||
|
||||
impl StatusLed for SpiLed {
|
||||
fn turn_off(&mut self) -> Result<()> {
|
||||
self.controller
|
||||
.write(vec![rgb::RGB8::new(0, 0, 0)].into_iter())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn turn_on(&mut self, color: rgb::RGB8) -> Result<()> {
|
||||
self.controller.write(vec![color].into_iter())?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user