mirror of
https://github.com/Djeeberjr/fw-anwesenheit.git
synced 2025-07-03 01:24:17 +00:00
improved gpio buzzer
This commit is contained in:
parent
17a66e26cb
commit
b416b41040
@ -1,15 +1,21 @@
|
|||||||
use rppal::gpio::Gpio;
|
use rppal::gpio::{Gpio, OutputPin};
|
||||||
use tokio::time::sleep;
|
|
||||||
use std::time;
|
use std::time;
|
||||||
|
use tokio::time::sleep;
|
||||||
|
|
||||||
|
pub struct GPIOBuzzer {
|
||||||
|
pin: OutputPin,
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
Ok(GPIOBuzzer { pin })
|
||||||
|
}
|
||||||
|
|
||||||
/// Emits a sound on a passive buzzer.
|
/// Emits a sound on a passive buzzer.
|
||||||
pub async fn modulated_tone(pin_num: u8, carrier_hz: u32, sound_hz: u32, duration_ms: u64) {
|
async fn modulated_tone(&mut self, 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();
|
|
||||||
|
|
||||||
let carrier_period =
|
let carrier_period =
|
||||||
time::Duration::from_micros((1_000_000.0 / carrier_hz as f64 / 2.0) as u64);
|
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 mod_period = 1_000.0 / sound_hz as f64; // in ms
|
||||||
@ -19,9 +25,9 @@ pub async fn modulated_tone(pin_num: u8, carrier_hz: u32, sound_hz: u32, duratio
|
|||||||
// Modulation on: Carrier on for mod_period / 2
|
// Modulation on: Carrier on for mod_period / 2
|
||||||
let cycles_on = (carrier_hz as f64 * (mod_period / 2.0) / 1000.0) as u64;
|
let cycles_on = (carrier_hz as f64 * (mod_period / 2.0) / 1000.0) as u64;
|
||||||
for _ in 0..cycles_on {
|
for _ in 0..cycles_on {
|
||||||
pin.set_high();
|
self.pin.set_high();
|
||||||
sleep(carrier_period).await;
|
sleep(carrier_period).await;
|
||||||
pin.set_low();
|
self.pin.set_low();
|
||||||
sleep(carrier_period).await;
|
sleep(carrier_period).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,21 +36,21 @@ pub async fn modulated_tone(pin_num: u8, carrier_hz: u32, sound_hz: u32, duratio
|
|||||||
sleep(pause).await;
|
sleep(pause).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pub async fn beep_ack(&mut self) {
|
||||||
pub async fn beep_ack() {
|
// carrier = 2300 Hz, sound = 440 Hz, duration = 1 sec
|
||||||
// GPIO 17, carrier = 2300 Hz, sound = 440 Hz, Dauer = 1 sec
|
self.modulated_tone(2300, 500, 500).await;
|
||||||
modulated_tone(4, 2300, 500, 500).await;
|
self.modulated_tone(2300, 700, 500).await;
|
||||||
modulated_tone(4, 2300, 700, 500).await;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn beep_nak() {
|
pub async fn beep_nak(&mut self) {
|
||||||
// GPIO 17, carrier = 2300 Hz, sound = 440 Hz, duration = 1 sec
|
// carrier = 2300 Hz, sound = 440 Hz, duration = 1 sec
|
||||||
modulated_tone(4, 2300, 700, 500).await;
|
self.modulated_tone(2300, 700, 500).await;
|
||||||
modulated_tone(4, 2300, 500, 500).await;
|
self.modulated_tone(2300, 500, 500).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn beep_unnkown() {
|
pub async fn beep_unnkown(&mut self) {
|
||||||
modulated_tone(4, 2300, 500, 500).await;
|
self.modulated_tone(2300, 500, 500).await;
|
||||||
modulated_tone(4, 2300, 500, 500).await;
|
self.modulated_tone(2300, 500, 500).await;
|
||||||
modulated_tone(4, 2300, 500, 500).await;
|
self.modulated_tone(2300, 500, 500).await;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user