From 8182f907839e77ec6e7aae06e976c1be1af98b79 Mon Sep 17 00:00:00 2001 From: bel Date: Sat, 25 Mar 2023 09:38:05 -0600 Subject: [PATCH] background thread sends ts as mvp --- src/gui.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 5 deletions(-) diff --git a/src/gui.rs b/src/gui.rs index bf169f3..d81c4f3 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -7,6 +7,7 @@ use iced_futures::backend::native::async_std::time::every; use handlebars::Handlebars; use serde_json::json; use std::time::{SystemTime, UNIX_EPOCH}; +use std::thread; use crate::stream::OutputStream; use crate::config::GUI; @@ -31,7 +32,9 @@ pub fn main(cfg: GUI, output_stream: Box) -> iced::Result { } struct Main { - ntfy: String, + c: std::sync::mpsc::Receiver, + ntfy1: String, + ntfy2: String, configuring: Option, inputs: Inputs, keys_newly_down: Vec, @@ -100,6 +103,18 @@ impl Main { } fn exchange(&mut self) { + self.exchange_send(); + self.exchange_recv(); + } + + fn exchange_recv(&mut self) { + match self.c.try_recv() { + Ok(msg) => self.ntfy2 = msg, + _ => {}, + }; + } + + fn exchange_send(&mut self) { let mut s = vec![]; for key_code in self.keys_newly_down.iter() { match self.key_code_to_string(key_code) { @@ -170,8 +185,16 @@ impl Application for Main { type Executor = executor::Default; fn new(flags: Self::Flags) -> (Self, Command) { + let (sender, receiver) = std::sync::mpsc::channel(); + thread::spawn(move || { + Feedback{ + c: sender, + }.listen() + }); return (Self { - ntfy: String::from(":wave:"), + c: receiver, + ntfy1: String::from(":wave:"), + ntfy2: String::from(""), configuring: None, inputs: Inputs{ stick: Stick { @@ -223,7 +246,7 @@ impl Application for Main { Message::R => { self.inputs.r = key_code }, _ => {}, }; - self.ntfy = format!("{:?} => {:?}", key_code.clone(), self.configuring.as_ref().unwrap()); + self.ntfy1 = format!("{:?} => {:?}", key_code.clone(), self.configuring.as_ref().unwrap()); self.configuring = None; }, _ => {}, @@ -268,7 +291,7 @@ impl Application for Main { }, _ => { self.configuring = Some(msg.clone()); - self.ntfy = format!("push a key to bind to {:?}", msg.clone()); + self.ntfy1 = format!("push a key to bind to {:?}", msg.clone()); }, } return Command::none(); @@ -296,10 +319,38 @@ impl Application for Main { button(text(controller_button_to_string(Message::Y, self.inputs.y))).on_press(Message::Y), button(text(controller_button_to_string(Message::L, self.inputs.l))).on_press(Message::L), button(text(controller_button_to_string(Message::R, self.inputs.r))).on_press(Message::R), - text(self.ntfy.clone()).size(50), + text(self.ntfy1.clone()).size(50), + text(self.ntfy2.clone()).size(50), ] .padding(20) .align_items(Alignment::Center) .into(); } } + +struct Feedback { + c: std::sync::mpsc::Sender, +} + +impl Feedback { + fn listen(&mut self) { + loop { + std::thread::sleep(std::time::Duration::from_secs(2)); + match self.read() { + Some(msg) if msg.len() > 0 => self.write(msg), + _ => {}, + }; + } + } + + fn read(&mut self) -> Option { + return Some(String::from(format!("feedback.read: {:?}", SystemTime::now()))); + } + + fn write(&mut self, msg: String) { + match self.c.send(msg.clone()) { + Ok(_) => {}, + Err(err) => eprintln!("feedback.listen() failed to display {}: {}", msg, err), + }; + } +}