From 1e1a82830bf49e281608a64ba1e60bb88f7fb1ab Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Thu, 27 Mar 2025 19:39:42 -0400 Subject: [PATCH] show auto status on gui --- interface/src/auto.rs | 10 +++++++--- interface/src/gui.rs | 15 +++++++++++---- interface/src/main.rs | 31 +++++++++++++++++-------------- 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/interface/src/auto.rs b/interface/src/auto.rs index 0d6d9e5..3c075d7 100644 --- a/interface/src/auto.rs +++ b/interface/src/auto.rs @@ -17,18 +17,22 @@ pub struct AutoInterface { } impl AutoInterface { - pub fn new(data: watch::Receiver, config: StorageManager, enable_permission: Arc) -> (Self, watch::Receiver) { + /// new + /// + /// returns command channel from auto, enable status + pub fn new(data: watch::Receiver, config: StorageManager, enable_permission: Arc) -> (Self, watch::Receiver, Arc) { let (commands, receiver) = watch::channel(ControlPacket::Fire); + let enabled = Arc::new(AtomicBool::new(false)); let interface = Self { command_sender: commands, data_receiver: data, config, - enabled: Arc::new(AtomicBool::new(false)), + enabled: enabled.clone(), enable_permission, }; - (interface, receiver) + (interface, receiver, enabled) } pub fn subscribe(&self) -> watch::Receiver { diff --git a/interface/src/gui.rs b/interface/src/gui.rs index 5d19bb7..28a7e26 100644 --- a/interface/src/gui.rs +++ b/interface/src/gui.rs @@ -1,4 +1,4 @@ -use std::{cell::OnceCell, collections::HashMap, path::PathBuf, sync::{atomic::Ordering, Arc}, time::Duration}; +use std::{cell::OnceCell, collections::HashMap, path::PathBuf, sync::{atomic::{AtomicBool, Ordering}, Arc}, time::Duration}; use common::{ControlPacket, TelemetryPacket}; use eframe::{egui::{self, containers, Align2, Checkbox, Context, IconData, Id, ImageSource, Label, Ui}, Storage}; @@ -10,7 +10,7 @@ use crate::{auto::Configurable, storage::StorageManager, POWER_THRESHOLD}; pub const GUI: OnceCell = OnceCell::new(); -pub fn gui(data: Receiver, toasts: mpsc::Receiver, executor: Runtime, autoconf: watch::Receiver<&'static [Configurable]>, storage: StorageManager) -> eframe::Result { +pub fn gui(data: Receiver, toasts: mpsc::Receiver, executor: Runtime, autoconf: watch::Receiver<&'static [Configurable]>, storage: StorageManager, auto_allowed: Arc, auto_enabled: Arc) -> eframe::Result { let icon = egui::include_image!("../assets/lizard.png"); let icon = image::load_from_memory_with_format(include_bytes!("../assets/lizard.png"), ImageFormat::Png).unwrap(); @@ -35,7 +35,7 @@ pub fn gui(data: Receiver, toasts: mpsc::Receiver, executor: Run // This gives us image support: egui_extras::install_image_loaders(&cc.egui_ctx); - Ok(Box::new(GUI::with_receivers(data, toasts, executor, storage, autoconf))) + Ok(Box::new(GUI::with_receivers(data, toasts, executor, storage, autoconf, auto_allowed, auto_enabled))) }), ) } @@ -63,10 +63,12 @@ struct GUI { selected_auto: usize, autoconf: watch::Receiver<&'static [Configurable]>, storage: StorageManager, + auto_allowed: Arc, + auto_enabled: Arc, } impl GUI { - fn with_receivers(data: Receiver, toasts: mpsc::Receiver, executor: Runtime, storage: StorageManager, autoconf: watch::Receiver<&'static [Configurable]>) -> Self { + fn with_receivers(data: Receiver, toasts: mpsc::Receiver, executor: Runtime, storage: StorageManager, autoconf: watch::Receiver<&'static [Configurable]>, auto_allowed: Arc, auto_enabled: Arc) -> Self { Self { data, toasts, @@ -74,6 +76,8 @@ impl GUI { selected_auto: 0, autoconf, storage, + auto_allowed, + auto_enabled, } } } @@ -115,6 +119,9 @@ impl eframe::App for GUI { if let Some(ref command) = self.data.borrow().last_command { ui.label(format!("sending {command:?}")); } + ui.label(format!("auto authorized: {}", if self.auto_allowed.load(Ordering::Acquire) {"✅"} else {"❌"})); + ui.label(format!("auto running: {}", if self.auto_enabled.load(Ordering::Acquire) {"✅ zoom vroom"} else {"❌"})); + if let Some(ref telem) = self.data.borrow().telemetry { ui.label(format!("Left tof: {}", if let Some(tof) = telem.sensors.tof_l {format!("✅ {tof}mm")} else {"❌".into()})); ui.label(format!("Right tof: {}", if let Some(tof) = telem.sensors.tof_r {format!("✅ {tof}mm")} else {"❌".into()})); diff --git a/interface/src/main.rs b/interface/src/main.rs index bf78209..ee8fa5d 100644 --- a/interface/src/main.rs +++ b/interface/src/main.rs @@ -22,7 +22,7 @@ fn main() -> Result<()> { let auto_allowed = Arc::new(AtomicBool::new(false)); let (auto_telem_sender, auto_telem) = watch::channel(TelemetryPacket::default()); - let (interface, auto_command) = AutoInterface::new(auto_telem.clone(), storage.clone(), auto_allowed.clone()); + let (interface, auto_command, auto_enabled) = AutoInterface::new(auto_telem.clone(), storage.clone(), auto_allowed.clone()); println!("name: {}", auto.name()); @@ -99,6 +99,7 @@ fn main() -> Result<()> { let spawner = executor.handle().clone(); + let auto_allowed_ = auto_allowed.clone(); thread::spawn(move || { spawner.block_on(async { let log_toasts = toast_sender.clone(); @@ -109,7 +110,7 @@ fn main() -> Result<()> { }); loop { - if let Err(e) = connect(toast_sender.clone(), notes.resubscribe(), data_sender.clone(), logging_sender.clone(), auto_telem_sender.clone(), auto_allowed.clone(), interface.clone()).await { + if let Err(e) = connect(toast_sender.clone(), notes.resubscribe(), data_sender.clone(), logging_sender.clone(), auto_telem_sender.clone(), auto_allowed_.clone(), interface.clone()).await { if let Err(_) = toast_sender.send(Toast::new().text(format!("{e:?}")).kind(ToastKind::Error)).await { break; }; @@ -121,7 +122,7 @@ fn main() -> Result<()> { println!("launching gui"); let (_conf_sender, conf) = watch::channel(CONFIGS); - gui(gui_data, toasts, executor, conf, storage.clone()).unwrap(); + gui(gui_data, toasts, executor, conf, storage.clone(), auto_allowed, auto_enabled).unwrap(); drop(stream); @@ -177,6 +178,7 @@ async fn controller(mut notes: broadcast::Receiver, controller: Owned //auto = Some(spawn(seek)) + let mut auto_data = interface.subscribe(); loop { let mut control = ControlPacket::Stop; @@ -190,12 +192,16 @@ async fn controller(mut notes: broadcast::Receiver, controller: Owned let (note,vol) = note.context("channel closed")?; if let Some(note) = note { dbg!(note); - if let ControlFlow::Break(_) = sax_control(&mut control, vol, note) { - if let ControlFlow::Break(_) = recorder_control(&mut control, vol, note) { + if let ControlFlow::Break(_) = sax_control(&mut control, vol, note, enable.clone()) { + if let ControlFlow::Break(_) = recorder_control(&mut control, vol, note, enable.clone()) { continue; } } + if enable.load(Ordering::Acquire) && interface.enabled() { + control = auto_data.borrow_and_update().clone(); + } + send_packet(&mut controller, control.clone()).await?; let _ = logging_sender.send(CombatData::Control(control.clone())).await; gui.send_modify(|gui| gui.last_command = Some(control)); @@ -210,7 +216,7 @@ async fn controller(mut notes: broadcast::Receiver, controller: Owned /// Weapon enabled const ARMED: bool = true; -fn sax_control(control: &mut ControlPacket, vol: f32, frequency: f32) -> ControlFlow<()> { +fn sax_control(control: &mut ControlPacket, vol: f32, frequency: f32, enable: Arc) -> ControlFlow<()> { if frequency < 150. { println!("too low"); return ControlFlow::Break(()); @@ -263,7 +269,7 @@ fn sax_control(control: &mut ControlPacket, vol: f32, frequency: f32) -> Control } -fn recorder_control(control: &mut ControlPacket, vol: f32, frequency: f32) -> ControlFlow<()> { +fn recorder_control(control: &mut ControlPacket, vol: f32, frequency: f32, enable: Arc) -> ControlFlow<()> { if frequency < 300. { println!("too low"); return ControlFlow::Break(()); @@ -303,15 +309,12 @@ fn recorder_control(control: &mut ControlPacket, vol: f32, frequency: f32) -> Co *control = ControlPacket::Stop; } Pitch { letter: NoteLetter::F, accidental: 0} => { - println!("stop"); - *control = ControlPacket::Stop; + println!("auto en"); + enable.store(true, Ordering::Release); } Pitch { letter: NoteLetter::E, accidental: 0} => { - println!("stop"); - *control = ControlPacket::Stop; - //if let result::Result::Ok(command) = auto_rx.try_recv() { - // control = command; - //} + println!("auto disable"); + enable.store(false, Ordering::Release); } pitch => { if vol > 3000. {