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 tokio::time::sleep;
use rppal::gpio::{Gpio, OutputPin};
use std::time;
use tokio::time::sleep;
/// Emits a sound on a passive buzzer.
pub async fn modulated_tone(pin_num: u8, carrier_hz: u32, sound_hz: u32, duration_ms: u64) {
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();
pub struct GPIOBuzzer {
pin: OutputPin,
}
let carrier_period =
time::Duration::from_micros((1_000_000.0 / carrier_hz as f64 / 2.0) as u64);
let mod_period = 1_000.0 / sound_hz as f64; // in ms
let total_cycles = duration_ms as f64 / mod_period;
impl GPIOBuzzer {
pub fn new(pin_num: u8) -> Result<Self, Box<dyn std::error::Error>> {
let gpio = Gpio::new()?;
let pin = gpio.get(pin_num)?.into_output();
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 {
pin.set_high();
sleep(carrier_period).await;
pin.set_low();
sleep(carrier_period).await;
Ok(GPIOBuzzer { pin })
}
/// Emits a sound on a passive buzzer.
async fn modulated_tone(&mut self, carrier_hz: u32, sound_hz: u32, duration_ms: u64) {
let carrier_period =
time::Duration::from_micros((1_000_000.0 / carrier_hz as f64 / 2.0) as u64);
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
let pause = time::Duration::from_millis((mod_period / 2.0) as u64);
sleep(pause).await;
pub async fn beep_nak(&mut self) {
// carrier = 2300 Hz, sound = 440 Hz, duration = 1 sec
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 mut gpio_buzzer = buzzer::GPIOBuzzer::new(4)?;
let channel_store = store.clone();
tokio::spawn(async move {
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))
{
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
gpio_buzzer.beep_ack().await;
if let Err(e) = channel_store.lock().await.export_json(STORE_PATH).await {
error!("Failed to save id store to file: {}", e);