added led ws 2812 and buzzer stuff

This commit is contained in:
PC_WSL
2025-05-05 12:34:41 +02:00
parent f3c4e9884e
commit e14f703a76
8 changed files with 226 additions and 40 deletions

View File

@@ -1,46 +1,49 @@
use rppal::gpio::Gpio;
use std::{thread, time};
pub mod buzzer {
use rppal::gpio::Gpio;
use std::{thread, time};
/// Emits a sound on a passive buzzer.
pub 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();
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;
/// Emits a sound on a passive buzzer.
pub 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();
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();
thread::sleep(carrier_period);
pin.set_low();
thread::sleep(carrier_period);
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 {
pin.set_high();
thread::sleep(carrier_period);
pin.set_low();
thread::sleep(carrier_period);
}
// Modulation off: Carrier on for mod_period / 2
let pause = time::Duration::from_millis((mod_period / 2.0) as u64);
thread::sleep(pause);
}
}
// Modulation off: Carrier on for mod_period / 2
let pause = time::Duration::from_millis((mod_period / 2.0) as u64);
thread::sleep(pause);
pub 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);
}
pub 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);
}
pub fn beep_unnkown(){
modulated_tone(4, 2300, 500, 500);
modulated_tone(4, 2300, 500, 500);
modulated_tone(4, 2300, 500, 500);
}
}
pub 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);
}
pub 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);
}
pub fn beep_unnkown(){
modulated_tone(4, 2300, 500, 500);
modulated_tone(4, 2300, 500, 500);
modulated_tone(4, 2300, 500, 500);
}

39
src/color.rs Normal file
View File

@@ -0,0 +1,39 @@
#[derive(Clone, Copy)]
pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8,
}
impl Color {
pub fn to_grb(self) -> [u8; 3] {
[self.g, self.r, self.b]
}
}
#[derive(Debug, Clone, Copy)]
pub enum NamedColor {
Red,
Green,
Blue,
White,
Off,
Yellow,
Cyan,
Magenta,
}
impl From<NamedColor> for Color {
fn from(named: NamedColor) -> Self {
match named {
NamedColor::Red => Color { r: 255, g: 0, b: 0 },
NamedColor::Green => Color { r: 0, g: 255, b: 0 },
NamedColor::Blue => Color { r: 0, g: 0, b: 255 },
NamedColor::White => Color { r: 255, g: 255, b: 255 },
NamedColor::Off => Color { r: 0, g: 0, b: 0 },
NamedColor::Yellow => Color { r: 255, g: 255, b: 0 },
NamedColor::Cyan => Color { r: 0, g: 255, b: 255 },
NamedColor::Magenta => Color { r: 255, g: 0, b: 255 },
}
}
}

View File

@@ -4,6 +4,11 @@ use std::{
error::Error,
fmt::Display,
};
use crate::led::Led;
use tokio::fs;
use crate::led::Led;
use tokio::fs;
/// Represents the ID that is stored on the Tally
@@ -130,7 +135,12 @@ impl AttendanceDay {
return false;
}
self.ids.push(id);
true
buzzer::beep_ack();
led.set_named_color_time(NamedColor::Green, 1); //led is green for 1 sec
return true;
}
}

28
src/led.rs Normal file
View File

@@ -0,0 +1,28 @@
use rppal::spi::{Bus, Mode, SlaveSelect, Spi};
use smart_leds::{SmartLedsWrite, RGB8};
use ws2812_spi::Ws2812;
pub struct Led {
controller: Ws2812<Spi>,
}
impl Led {
pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
let spi = Spi::new(Bus::Spi0, SlaveSelect::Ss0, 3_000_000, Mode::Mode0)?;
let controller = Ws2812::new(spi);
Ok(Led { controller })
}
pub fn turn_green_on(&mut self) -> Result<(), Box<dyn std::error::Error>> {
let green = [RGB8 { r: 0, g: 255, b: 0 }];
self.controller.write(green.iter().cloned())?;
Ok(())
}
pub fn turn_off(&mut self) -> Result<(), Box<dyn std::error::Error>> {
let off = [RGB8 { r: 0, g: 0, b: 0 }];
self.controller.write(off.iter().cloned())?;
Ok(())
}
}

View File

@@ -13,6 +13,10 @@ mod id_store;
mod parser;
mod pm3;
mod webserver;
mod color;
mod led;
mod buzzer;
const STORE_PATH: &str = "./data.json";
@@ -85,6 +89,10 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
});
tokio::spawn(async move {
})
match start_webserver(store.clone()).await {
Ok(()) => {}
Err(e) => {