improved gpio buzzer

This commit is contained in:
Djeeberjr 2025-05-05 13:36:53 +02:00
parent 17a66e26cb
commit b416b41040
2 changed files with 51 additions and 43 deletions

View File

@ -1,50 +1,56 @@
use rppal::gpio::Gpio; use rppal::gpio::{Gpio, OutputPin};
use tokio::time::sleep;
use std::time; use std::time;
use tokio::time::sleep;
/// Emits a sound on a passive buzzer. pub struct GPIOBuzzer {
pub async fn modulated_tone(pin_num: u8, carrier_hz: u32, sound_hz: u32, duration_ms: u64) { pin: OutputPin,
let gpio = Gpio::new().expect("GPIO konnte nicht initialisiert werden"); }
let mut pin = gpio
.get(pin_num)
.expect("Pin konnte nicht geöffnet werden")
.into_output();
let carrier_period = impl GPIOBuzzer {
time::Duration::from_micros((1_000_000.0 / carrier_hz as f64 / 2.0) as u64); pub fn new(pin_num: u8) -> Result<Self, Box<dyn std::error::Error>> {
let mod_period = 1_000.0 / sound_hz as f64; // in ms let gpio = Gpio::new()?;
let total_cycles = duration_ms as f64 / mod_period; let pin = gpio.get(pin_num)?.into_output();
for _ in 0..total_cycles as u64 { Ok(GPIOBuzzer { pin })
// Modulation on: Carrier on for mod_period / 2 }
let cycles_on = (carrier_hz as f64 * (mod_period / 2.0) / 1000.0) as u64;
for _ in 0..cycles_on { /// Emits a sound on a passive buzzer.
pin.set_high(); async fn modulated_tone(&mut self, carrier_hz: u32, sound_hz: u32, duration_ms: u64) {
sleep(carrier_period).await; let carrier_period =
pin.set_low(); time::Duration::from_micros((1_000_000.0 / carrier_hz as f64 / 2.0) as u64);
sleep(carrier_period).await; let mod_period = 1_000.0 / sound_hz as f64; // in ms
let total_cycles = duration_ms as f64 / mod_period;
for _ in 0..total_cycles as u64 {
// Modulation on: Carrier on for mod_period / 2
let cycles_on = (carrier_hz as f64 * (mod_period / 2.0) / 1000.0) as u64;
for _ in 0..cycles_on {
self.pin.set_high();
sleep(carrier_period).await;
self.pin.set_low();
sleep(carrier_period).await;
}
// Modulation off: Carrier on for mod_period / 2
let pause = time::Duration::from_millis((mod_period / 2.0) as u64);
sleep(pause).await;
} }
}
pub async fn beep_ack(&mut self) {
// carrier = 2300 Hz, sound = 440 Hz, duration = 1 sec
self.modulated_tone(2300, 500, 500).await;
self.modulated_tone(2300, 700, 500).await;
}
// Modulation off: Carrier on for mod_period / 2 pub async fn beep_nak(&mut self) {
let pause = time::Duration::from_millis((mod_period / 2.0) as u64); // carrier = 2300 Hz, sound = 440 Hz, duration = 1 sec
sleep(pause).await; self.modulated_tone(2300, 700, 500).await;
self.modulated_tone(2300, 500, 500).await;
}
pub async fn beep_unnkown(&mut self) {
self.modulated_tone(2300, 500, 500).await;
self.modulated_tone(2300, 500, 500).await;
self.modulated_tone(2300, 500, 500).await;
} }
} }
pub async fn beep_ack() {
// GPIO 17, carrier = 2300 Hz, sound = 440 Hz, Dauer = 1 sec
modulated_tone(4, 2300, 500, 500).await;
modulated_tone(4, 2300, 700, 500).await;
}
pub async fn beep_nak() {
// GPIO 17, carrier = 2300 Hz, sound = 440 Hz, duration = 1 sec
modulated_tone(4, 2300, 700, 500).await;
modulated_tone(4, 2300, 500, 500).await;
}
pub async fn beep_unnkown() {
modulated_tone(4, 2300, 500, 500).await;
modulated_tone(4, 2300, 500, 500).await;
modulated_tone(4, 2300, 500, 500).await;
}

View File

@ -70,6 +70,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
let store: Arc<Mutex<IDStore>> = Arc::new(Mutex::new(raw_store)); let store: Arc<Mutex<IDStore>> = Arc::new(Mutex::new(raw_store));
let mut gpio_buzzer = buzzer::GPIOBuzzer::new(4)?;
let channel_store = store.clone(); let channel_store = store.clone();
tokio::spawn(async move { tokio::spawn(async move {
while let Some(tally_id_string) = rx.recv().await { while let Some(tally_id_string) = rx.recv().await {
@ -79,8 +81,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
.add_id(id_store::TallyID(tally_id_string)) .add_id(id_store::TallyID(tally_id_string))
{ {
info!("Added new id to current day"); info!("Added new id to current day");
buzzer::beep_ack().await;
// led.set_named_color_time(NamedColor::Green, 1); //led is green for 1 sec // led.set_named_color_time(NamedColor::Green, 1); //led is green for 1 sec
gpio_buzzer.beep_ack().await;
if let Err(e) = channel_store.lock().await.export_json(STORE_PATH).await { if let Err(e) = channel_store.lock().await.export_json(STORE_PATH).await {
error!("Failed to save id store to file: {}", e); error!("Failed to save id store to file: {}", e);