This commit is contained in:
2025-10-18 14:01:18 +02:00
parent f0f8e556b8
commit 2477be392e
2 changed files with 35 additions and 99 deletions

View File

@ -7,6 +7,7 @@ mod filtering;
mod iq;
mod math;
mod nco;
mod squelch;
mod ted;
mod units;
mod windows;
@ -35,7 +36,7 @@ use crate::{
ted::elg::ELGate,
units::frequency::hz_to_rad_per_sample,
};
use eframe::egui::{self, CentralPanel, Color32};
use eframe::egui::{self, CentralPanel, Color32, debug_text::print};
use tokio::sync::RwLock;
use tokio::sync::mpsc::{Receiver, Sender, channel};
@ -121,67 +122,14 @@ impl Transceiver {
mut sample_stream: Receiver<f32>,
mut sample_sender: Sender<SampleSenderCommand>,
) -> Self {
let mut resend: Option<Vec<u8>> = None;
let (mut eyes_tx, eyes_rx) = channel::<Vec<f32>>(1024);
let (rx_stream_sender, rx_stream_receiver) = channel::<Vec<u8>>(1024);
let (tx_stream_sender, mut tx_stream_receiver) = channel::<Vec<u8>>(1024);
tokio::spawn(async move {
loop {
select! {
_ = Self::squelch_detector(&mut sample_stream) =>
{
println!("Squelch up");
select!
{
x = Self::receive(&mut sample_stream, &mut eyes_tx) =>
{
match x
{
Err(()) => {continue;},
Ok(Frame::Ack) =>
{
resend = None;
}
Ok(Frame::Data(data)) =>
{
println!("Got data frame, send data");
let _ = rx_stream_sender.send(data).await;
tokio::time::sleep(Duration::from_secs(1)).await;
println!("Got data frame, sending ack");
Self::transmit(Frame::Ack, &mut sample_sender).await;
println!("Sent ack");
}
}
},
_ = tokio::time::sleep(Duration::from_secs(100)) => {continue;}, //TODO: 65
//sec
//timeout
}
}, // End squelch
data_opt = async
{
tokio::time::sleep(Duration::from_secs(2)).await;
if let Some(resend_data) = resend.clone()
{
Some(resend_data)
}
else
{
tx_stream_receiver.recv().await
}
}
=>
{
if let Some(data) = data_opt
{
Self::transmit(Frame::Data(data.clone()), &mut sample_sender).await;
resend = Some(data);
}
}
}
}
let squelch_sum = 0.;
loop {}
});
Self {
@ -305,15 +253,9 @@ impl Transceiver {
//print!("{}", last_byte as char);
print!(".{:x}.", last_byte);
let _ = std::io::stdout().flush();
if let Ok(Some(Frame::Ack)) = frame_opt {
println!("Got ack");
return Ok(Frame::Ack);
}
if let Ok(Some(Frame::Data(ref frame_data))) = frame_opt {
if let Ok(Some(Frame(ref frame_data))) = frame_opt {
println!("Got data");
return Ok(Frame::Data(frame_data.to_vec()));
return Ok(Frame(frame_data.to_vec()));
}
if let Err(()) = frame_opt {
@ -328,10 +270,7 @@ impl Transceiver {
}
}
enum Frame {
Data(Vec<u8>),
Ack,
}
struct Frame(Vec<u8>);
type FrameConstructionError = ();
pub struct FrameConstructor {
@ -352,17 +291,12 @@ impl FrameConstructor {
}
pub fn add_byte(&mut self, byte: u8) -> Result<Option<Frame>, FrameConstructionError> {
if self.frame.is_empty() && byte != 0xC4 && byte != 0x4C && !self.started {
if self.frame.is_empty() && byte != 0x4C && !self.started {
println!("Wrong type {:x}", byte);
self.started = true;
return Err(());
}
if self.frame.is_empty() && byte == 0xC4 && !self.started {
self.started = true;
return Ok(Some(Frame::Ack));
}
if self.frame.is_empty() && byte == 0x4C && !self.started {
self.started = true;
return Ok(None);
@ -383,9 +317,7 @@ impl FrameConstructor {
if self.frame_countdown.unwrap() == 0 {
// All data has been received
if self.checksum == byte {
return Ok(Some(Frame::Data(
self.frame.iter().skip(2).copied().collect(),
)));
return Ok(Some(Frame(self.frame.iter().skip(2).copied().collect())));
}
println!("Checksum failed");
@ -410,27 +342,20 @@ impl Frame {
// Preamble byte
output_bytes.push(0xD8);
// Command
match self {
Frame::Data(x) => {
assert!(x.len() < 65536, "Data size over MTU");
let x = &self.0;
assert!(x.len() < 65536, "Data size over MTU");
output_bytes.push(0x4C); // DATA FRAME
output_bytes.push(0x4C); // DATA FRAME
let len_u16 = x.len() as u16;
output_bytes.push((len_u16 & 0xFF).try_into().unwrap());
output_bytes.push(((len_u16 >> 8) & 0xFF).try_into().unwrap());
let len_u16 = x.len() as u16;
output_bytes.push((len_u16 & 0xFF).try_into().unwrap());
output_bytes.push(((len_u16 >> 8) & 0xFF).try_into().unwrap());
let mut checksum = 0u8;
x.iter().for_each(|x| checksum ^= x);
output_bytes.extend(x.iter());
let mut checksum = 0u8;
x.iter().for_each(|x| checksum ^= x);
output_bytes.extend(x.iter());
output_bytes.push(checksum);
}
Frame::Ack => {
output_bytes.push(0xC4); // ACK FRAME
}
}
output_bytes.push(checksum);
// SEND EOT
output_bytes.extend(std::iter::repeat_n(4, 32));
@ -494,9 +419,11 @@ impl EguiApp {
match up_a_receiver.try_recv() {
Ok(SampleSenderCommand::Open) => {
sending = true;
println!("open");
}
Ok(SampleSenderCommand::Close) => {
sending = false;
println!("close");
}
Ok(SampleSenderCommand::Sample(x)) => {
sample = x;
@ -509,9 +436,9 @@ impl EguiApp {
while let Ok(_) = b2a_rx.try_recv() {}
// Send to other
a2b_tx.send(sample + noise).await.unwrap();
a2b_tx.send(sample + noise).await;
} else if let Ok(down_sample) = b2a_rx.try_recv() {
down_a_sender.send(down_sample).await.unwrap();
down_a_sender.send(down_sample).await;
}
}
});
@ -541,9 +468,9 @@ impl EguiApp {
while let Ok(_) = a2b_rx.try_recv() {}
// Send to other
b2a_tx.send(sample + noise).await.unwrap();
b2a_tx.send(sample + noise).await;
} else if let Ok(down_sample) = a2b_rx.try_recv() {
down_b_sender.send(down_sample).await.unwrap();
down_b_sender.send(down_sample).await;
}
}
});
@ -599,7 +526,7 @@ impl eframe::App for EguiApp {
let snd = self.a_transceiver.get_sender();
tokio::spawn(async move {
let _ = snd
.send("Skibditoilet".repeat(100).as_bytes().to_vec())
.send("Skibditoilet".repeat(10).as_bytes().to_vec())
.await;
});
}
@ -620,6 +547,15 @@ impl eframe::App for EguiApp {
plot_ui.line(line);
}
});
if uis[1].button("Start").clicked() {
let snd = self.b_transceiver.get_sender();
tokio::spawn(async move {
let _ = snd
.send("Skibditoilet".repeat(10).as_bytes().to_vec())
.await;
});
}
});
}); // Central panel

0
src/squelch.rs Normal file
View File