same functionality now works with axum the same way it did with actix

This commit is contained in:
2026-03-02 20:39:08 +01:00
parent 05ba925d54
commit a86a28fd85
7 changed files with 96 additions and 57 deletions

View File

@@ -1,4 +1,7 @@
use std::{sync::{Arc, Mutex}, thread};
use std::{
sync::{Arc, Mutex},
thread,
};
use gpio_cdev::{Chip, LineRequestFlags};
@@ -12,43 +15,40 @@ pub struct BeepRinger {
}
impl BeepRinger {
fn beep(times: u32, chip: &mut Chip) -> Result<(), String> {
let beeper = chip.get_line(17);
fn beep(times: u32, chip: &mut Chip) -> Result<(), String> {
let beeper = chip.get_line(17);
let beeper = match beeper {
Ok(beeper) => beeper,
Err(e) => {
println!("Error opening line: {}", e);
return Err("Could not open Line to Beeper".to_string());
}
};
let beeper = match beeper {
Ok(beeper) => beeper,
Err(e) => {
println!("Error opening line: {}", e);
return Err("Could not open Line to Beeper".to_string());
}
};
let beeper = beeper.request(LineRequestFlags::OUTPUT, 0, "my-gpio");
let beeper = match beeper {
Ok(beeper) => beeper,
Err(e) => {
println!("Error requesting line: {}", e);
return Err("Could not request Line to Beeper".to_string());
}
};
let beeper = beeper
.request(LineRequestFlags::OUTPUT, 0, "my-gpio");
let beeper = match beeper {
Ok(beeper) => beeper,
Err(e) => {
println!("Error requesting line: {}", e);
return Err("Could not request Line to Beeper".to_string());
}
};
for _ in 0..times {
beeper.set_value(1).map_err(|e| e.to_string())?;
thread::sleep(std::time::Duration::from_secs(1));
beeper.set_value(0).map_err(|e| e.to_string())?;
thread::sleep(std::time::Duration::from_secs(1));
}
for _ in 0..times {
beeper.set_value(1).map_err(|e| e.to_string())?;
thread::sleep(std::time::Duration::from_secs(1));
beeper.set_value(0).map_err(|e| e.to_string())?;
thread::sleep(std::time::Duration::from_secs(1));
}
return Ok(());
}
}
pub fn new(chip: Arc<Mutex<Chip>>) -> Self {
Self { chip }
}
pub fn new(chip: Arc<Mutex<Chip>>) -> Self {
Self { chip }
}
}
impl Ringer for BeepRinger {
@@ -63,8 +63,25 @@ impl Ringer for BeepRinger {
}
};
BeepRinger::beep(5, &mut *chip)?;
BeepRinger::beep(5, &mut *chip)?;
Ok(())
Ok(())
}
}
/// Used for local testing without an actual beeper or similar. The only thing it does is print
/// that it's ringing.
pub struct SilentRinger;
impl SilentRinger {
pub fn new() -> Self {
Self {}
}
}
impl Ringer for SilentRinger {
fn ring(&self) -> Result<(), String> {
println!("Ringing");
Ok(())
}
}