From e1b898884379b4ebecbcacbbec79697f4b5f34a4 Mon Sep 17 00:00:00 2001 From: rrr-marble Date: Sun, 27 Jun 2021 21:17:37 +0300 Subject: [PATCH] add: mpsc framework for output events --- src/main.rs | 11 +++++++++-- src/pedals.rs | 6 +++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2526390..1dd2e63 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,15 +23,22 @@ fn main() -> () { fn run(input_device: &'static evdev::Device, pedals: impl Iterator) { let mut handles: Vec<_> = Vec::new(); + let (tx, rx) = std::sync::mpsc::channel(); for pedal in pedals { + let tx = tx.clone(); let handle = std::thread::spawn(move || loop { if input_device.get_key_state().unwrap().contains(pedal.key) { - (*pedal.action)(); + (*pedal.action)(&tx); } std::thread::sleep(std::time::Duration::from_millis(100)); }); handles.push(handle); } + + for recieved in rx { + println!("{}", recieved); + } + for handle in handles { handle.join().expect("Couldn't join"); } @@ -74,5 +81,5 @@ fn create_device() -> Result () + Send + 'static>, + action: Box) -> () + Send + 'static>, } diff --git a/src/pedals.rs b/src/pedals.rs index fcece47..206ad91 100644 --- a/src/pedals.rs +++ b/src/pedals.rs @@ -5,15 +5,15 @@ pub fn pedals() -> Vec { vec![ Pedal { key: Key::BTN_SIDE, - action: Box::new(|| println!("BTN_SIDE")), + action: Box::new(|tx| tx.send(String::from("BTN_SIDE")).unwrap()), }, Pedal { key: Key::BTN_EXTRA, - action: Box::new(|| println!("BTN_EXTRA")), + action: Box::new(|tx| tx.send(String::from("BTN_EXTRA")).unwrap()), }, Pedal { key: Key::BTN_RIGHT, - action: Box::new(|| println!("BTN_RIGHT")), + action: Box::new(|tx| tx.send(String::from("BTN_RIGHT")).unwrap()), }, ] }