made buzzer async

This commit is contained in:
Djeeberjr 2025-05-05 13:12:52 +02:00
parent 1ba74be096
commit 17a66e26cb
2 changed files with 17 additions and 17 deletions

View File

@ -1,8 +1,9 @@
use rppal::gpio::Gpio;
use std::{thread, time};
use tokio::time::sleep;
use std::time;
/// Emits a sound on a passive buzzer.
pub fn modulated_tone(pin_num: u8, carrier_hz: u32, sound_hz: u32, duration_ms: u64) {
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)
@ -19,31 +20,31 @@ pub fn modulated_tone(pin_num: u8, carrier_hz: u32, sound_hz: u32, duration_ms:
let cycles_on = (carrier_hz as f64 * (mod_period / 2.0) / 1000.0) as u64;
for _ in 0..cycles_on {
pin.set_high();
thread::sleep(carrier_period);
sleep(carrier_period).await;
pin.set_low();
thread::sleep(carrier_period);
sleep(carrier_period).await;
}
// Modulation off: Carrier on for mod_period / 2
let pause = time::Duration::from_millis((mod_period / 2.0) as u64);
thread::sleep(pause);
sleep(pause).await;
}
}
pub fn beep_ack() {
pub async fn beep_ack() {
// GPIO 17, carrier = 2300 Hz, sound = 440 Hz, Dauer = 1 sec
modulated_tone(4, 2300, 500, 500);
modulated_tone(4, 2300, 700, 500);
modulated_tone(4, 2300, 500, 500).await;
modulated_tone(4, 2300, 700, 500).await;
}
pub fn beep_nak() {
pub async fn beep_nak() {
// GPIO 17, carrier = 2300 Hz, sound = 440 Hz, duration = 1 sec
modulated_tone(4, 2300, 700, 500);
modulated_tone(4, 2300, 500, 500);
modulated_tone(4, 2300, 700, 500).await;
modulated_tone(4, 2300, 500, 500).await;
}
pub fn beep_unnkown() {
modulated_tone(4, 2300, 500, 500);
modulated_tone(4, 2300, 500, 500);
modulated_tone(4, 2300, 500, 500);
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

@ -79,8 +79,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
.add_id(id_store::TallyID(tally_id_string))
{
info!("Added new id to current day");
// TODO: trigger the buzzer
buzzer::beep_ack();
buzzer::beep_ack().await;
// led.set_named_color_time(NamedColor::Green, 1); //led is green for 1 sec
if let Err(e) = channel_store.lock().await.export_json(STORE_PATH).await {