added buzzer

This commit is contained in:
Philipp 2025-04-16 16:10:42 +02:00
parent a9bbc61300
commit ad009f987d
9 changed files with 243 additions and 156 deletions

2
.gitignore vendored
View File

@ -1 +1 @@
/target /target

33
Cargo.lock generated
View File

@ -79,9 +79,18 @@ name = "fw-anwesenheit"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"chrono", "chrono",
"gpio",
"regex", "regex",
"serde",
"serde_json",
] ]
[[package]]
name = "gpio"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fe6783270536547ac473c9d2ae5a7e0e715ea43f29004ced47fbd1c1372d2c7"
[[package]] [[package]]
name = "iana-time-zone" name = "iana-time-zone"
version = "0.1.63" version = "0.1.63"
@ -106,6 +115,12 @@ dependencies = [
"cc", "cc",
] ]
[[package]]
name = "itoa"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"
[[package]] [[package]]
name = "js-sys" name = "js-sys"
version = "0.3.77" version = "0.3.77"
@ -202,6 +217,12 @@ version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eded382c5f5f786b989652c49544c4877d9f015cc22e145a5ea8ea66c2921cd2" checksum = "eded382c5f5f786b989652c49544c4877d9f015cc22e145a5ea8ea66c2921cd2"
[[package]]
name = "ryu"
version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.219" version = "1.0.219"
@ -222,6 +243,18 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "serde_json"
version = "1.0.140"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373"
dependencies = [
"itoa",
"memchr",
"ryu",
"serde",
]
[[package]] [[package]]
name = "shlex" name = "shlex"
version = "1.3.0" version = "1.3.0"

View File

@ -5,4 +5,7 @@ edition = "2024"
[dependencies] [dependencies]
chrono = { version = "0.4.40", features = ["serde"] } chrono = { version = "0.4.40", features = ["serde"] }
gpio = "0.4.1"
regex = "1.11.1" regex = "1.11.1"
serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.140"

View File

@ -1,34 +1,34 @@
@startuml @startuml
actor user actor user
main -> pm3 :start main -> pm3 :start
loop loop
pm3 -> pm3 : look for HITAG pm3 -> pm3 : look for HITAG
end end
user -> pm3 :show HITAG user -> pm3 :show HITAG
pm3 -> parser : parse ID pm3 -> parser : parse ID
parser -> pm3 : return ID parser -> pm3 : return ID
pm3 --> main : send ID pm3 --> main : send ID
loop loop
main -> main : look for IDs main -> main : look for IDs
end end
main -> idstore : send ID main -> idstore : send ID
idstore -> System : ask for day idstore -> System : ask for day
alt alt
System -> idstore : return attendence_day System -> idstore : return attendence_day
else else
create collections attendence_day create collections attendence_day
idstore -> attendence_day : create new attendence_day idstore -> attendence_day : create new attendence_day
end end
idstore -> attendence_day : add ID idstore -> attendence_day : add ID
attendence_day -> system : save json attendence_day -> system : save json
@enduml @enduml

40
src/buzzer.rs Normal file
View File

@ -0,0 +1,40 @@
use rppal::gpio::Gpio;
use std::{thread, time};
/// Gibt einen Ton auf einem passiven Buzzer aus.
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;
for _ in 0..total_cycles as u64 {
// Modulations-Ein: Träger an für 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);
}
// Modulations-Aus: Träger aus für mod_period / 2
let pause = time::Duration::from_millis((mod_period / 2.0) as u64);
thread::sleep(pause);
}
}
pub fn beep_ack() {
// GPIO 17, Träger = 2300 Hz, Ton = 440 Hz, Dauer = 1 Sekunde
modulated_tone(4, 2300, 500, 500);
modulated_tone(4, 2300, 700, 500);
}
pub fn beep_nak() {
// GPIO 17, Träger = 2300 Hz, Ton = 440 Hz, Dauer = 1 Sekunde
modulated_tone(4, 2300, 700, 500);
modulated_tone(4, 2300, 500, 500);
}

View File

@ -1,67 +1,77 @@
use std::collections::HashMap; use std::{collections::HashMap, error::Error, fs::{self, read_to_string}, result};
use serde::{Deserialize, Serialize};
#[derive(PartialEq, Eq)]
struct TellyID (String); #[derive(PartialEq, Eq)]
#[derive(Deserialize, Serialize)]
struct AttendanceDay { struct TellyID (String);
date: String,
ids: Vec<TellyID>, #[derive(Deserialize, Serialize)]
} struct AttendanceDay {
date: String,
struct IDStore { ids: Vec<TellyID>,
days: HashMap<String,AttendanceDay> }
}
#[derive(Deserialize, Serialize)]
impl IDStore { struct IDStore {
fn new() -> Self { days: HashMap<String,AttendanceDay>
IDStore{ }
days: HashMap::new(),
} impl IDStore {
}
fn new() -> Self {
fn add_id(&mut self, id: TellyID){ IDStore{
let day = self.get_current_day(); days: HashMap::new(),
}
day.add_id(id); }
self.clean_map(); fn new_from_json(filepath:&str) -> Result<Self, Box<dyn Error>>{
} let readed_string = fs::read_to_string(filepath)?;
Ok(serde_json::from_str(&readed_string)?)
fn get_current_day(&mut self) -> &mut AttendanceDay { }
let current_day = get_day_str();
fn add_id(&mut self, id: TellyID){
if self.days.contains_key(&current_day) { let day = self.get_current_day();
return self.days.get_mut(&current_day).unwrap();
} day.add_id(id);
self.days.insert(current_day.clone(), AttendanceDay::new(&current_day.clone())); }
self.days.get_mut(&current_day.clone()).unwrap() fn get_current_day(&mut self) -> &mut AttendanceDay {
} let current_day = get_day_str();
fn clean_map(&mut self){ if self.days.contains_key(&current_day) {
todo!() return self.days.get_mut(&current_day).unwrap();
} }
}
self.days.insert(current_day.clone(), AttendanceDay::new(&current_day.clone()));
impl AttendanceDay {
fn new(day: &str) -> Self{ self.days.get_mut(&current_day.clone()).unwrap()
Self{ }
date: day.to_owned(),
ids: Vec::new(), fn export_jason(&self, filepath:&str) -> Result <(), Box<dyn Error>> {
}
} // Serialize it to a JSON string and safe it in filepath file
Ok(fs::write("attendence_list.json", serde_json::to_string(&self)?)?)
fn add_id(&mut self, id: TellyID){ }
if self.ids.contains(&id) { }
return
} impl AttendanceDay {
fn new(day: &str) -> Self{
self.ids.push(id); Self{
} date: day.to_owned(),
} ids: Vec::new(),
}
fn get_day_str() -> String { }
let now = chrono::offset::Local::now();
now.format("%Y-%m-%d").to_string() fn add_id(&mut self, id: TellyID){
} if self.ids.contains(&id) {
return
}
self.ids.push(id);
}
}
fn get_day_str() -> String {
let now = chrono::offset::Local::now();
now.format("%Y-%m-%d").to_string()
}

View File

@ -1,9 +1,10 @@
use pm3::run_pm3; use pm3::run_pm3;
mod parser; mod parser;
mod pm3; mod pm3;
mod id_store; mod id_store;
mod buzzer;
fn main() {
run_pm3().unwrap(); fn main() {
} run_pm3().unwrap();
}

View File

@ -1,8 +1,8 @@
use regex::Regex; use regex::Regex;
pub fn parse_line(line: &str) -> Option<String> { pub fn parse_line(line: &str) -> Option<String> {
let regex = Regex::new(r"(?m)^\[\+\] UID.... (.*)$").unwrap(); let regex = Regex::new(r"(?m)^\[\+\] UID.... (.*)$").unwrap();
let result = regex.captures(line); let result = regex.captures(line);
result.map(|c| c.get(1).unwrap().as_str().to_owned()) result.map(|c| c.get(1).unwrap().as_str().to_owned())
} }

View File

@ -1,38 +1,38 @@
use std::error::Error; use std::error::Error;
use std::process::{Command, Stdio}; use std::process::{Command, Stdio};
use std::io::{self, BufRead}; use std::io::{self, BufRead};
pub fn run_pm3() -> Result<(), Box<dyn Error>> { pub fn run_pm3() -> Result<(), Box<dyn Error>> {
let mut cmd = Command::new("stdbuf") let mut cmd = Command::new("stdbuf")
.arg("-oL") .arg("-oL")
.arg("pm3") .arg("pm3")
.arg("-c") .arg("-c")
.arg("lf hitag reader -@") .arg("lf hitag reader -@")
.stdout(Stdio::piped()) .stdout(Stdio::piped())
.spawn()?; .spawn()?;
let stdout = cmd.stdout.take().ok_or("Failed to get stdout")?; let stdout = cmd.stdout.take().ok_or("Failed to get stdout")?;
let reader = io::BufReader::new(stdout); let reader = io::BufReader::new(stdout);
for line_result in reader.lines() { for line_result in reader.lines() {
match line_result { match line_result {
Ok(line) => { Ok(line) => {
let parse_result = super::parser::parse_line(&line); let parse_result = super::parser::parse_line(&line);
if let Some(uid) = parse_result { if let Some(uid) = parse_result {
println!("UID: {}",uid); println!("UID: {}",uid);
} }
} }
Err(e) => { Err(e) => {
eprintln!("{}",e); eprintln!("{}",e);
} }
} }
} }
let status = cmd.wait().expect("Failed to wait on child"); let status = cmd.wait().expect("Failed to wait on child");
if status.success() { if status.success() {
Ok(()) Ok(())
}else { }else {
Err("pm3 had non zero exit code".into()) Err("pm3 had non zero exit code".into())
} }
} }