diff options
author | Galen Guyer <galen@galenguyer.com> | 2022-11-28 14:07:18 -0500 |
---|---|---|
committer | Galen Guyer <galen@galenguyer.com> | 2022-11-28 14:07:18 -0500 |
commit | 8870639dd5bd0e64bcb27b10cb9174362811e1f2 (patch) | |
tree | 6927cbdae574ede80f7146deb2cfadac49c9ffbb | |
parent | 163c64e7f06f09ed34b30fdec9a6073d52d74185 (diff) |
Add rapid talkgroup switching
-rw-r--r-- | src/main.rs | 44 |
1 files changed, 36 insertions, 8 deletions
diff --git a/src/main.rs b/src/main.rs index 86e7c75..d8e0a5a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use serde::Deserialize; use std::{collections::HashMap, net::UdpSocket}; #[allow(dead_code)] -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Clone)] struct TalkGroup { #[serde(rename(deserialize = "DEC"))] id: u32, @@ -19,6 +19,18 @@ struct TalkGroup { #[serde(rename(deserialize = "Priority"))] priority: i32, } +impl TalkGroup { + fn unknown(id: u32) -> Self { + Self { + id, + name: String::from("Unknown"), + description: String::from("Unknown"), + tag: String::from("Unknown"), + group: String::from("Unknown"), + priority: 0, + } + } +} fn init_cpal() -> (cpal::Device, cpal::SupportedStreamConfig) { let host = cpal::default_host(); @@ -94,7 +106,7 @@ fn main() { let buffer = HeapRb::<i16>::new(8192); let (mut producer, mut consumer) = buffer.split(); - let mut curr_tg: Option<u32> = None; + let mut listening_tg: Option<TalkGroup> = None; let audio_stream = match stream_config.sample_format() { cpal::SampleFormat::I16 => device @@ -118,20 +130,36 @@ fn main() { .try_into() .expect("slice with incorrect length"), ); + let curr_tg = talkgroups + .get(&tg) + .map(|tg| tg.to_owned()) + .unwrap_or(TalkGroup::unknown(tg)); + let audio_bytes = &filled_buf[4..]; - match curr_tg { - Some(curr_tg) => { - if curr_tg != tg { + match &listening_tg { + Some(listen_tg) => { + if listen_tg.id != curr_tg.id { + if curr_tg.priority > 0 && curr_tg.priority < listen_tg.priority { + println!("Switching to TG with higher priority: {}", curr_tg.name); + listening_tg = Some(curr_tg.to_owned()); + } continue; } } None => { - curr_tg = Some(tg); + listening_tg = Some(curr_tg.to_owned()); } } - println!("TalkGroup: {tg}: {} bytes", filled_buf.len() - 4); - let audio_bytes = &filled_buf[4..]; + if audio_bytes.len() == 2 && i16::from_le_bytes(audio_bytes.try_into().unwrap()) == 0 { + if let Some(curr_tg) = listening_tg { + println!("End of transmission from TG {}", curr_tg.name); + } + listening_tg = None; + continue; + } + + println!("TalkGroup: {}: {} bytes", curr_tg.name, filled_buf.len() - 4); for sample in audio_bytes.chunks_exact(2) { let sample = i16::from_le_bytes(sample.try_into().expect("slice with incorrect length")); |