add: mpsc framework for output events

v0.1.1
rrr-marble 5 years ago
parent 6f2caab6dd
commit e1b8988843

@ -23,15 +23,22 @@ fn main() -> () {
fn run(input_device: &'static evdev::Device, pedals: impl Iterator<Item = Pedal>) { fn run(input_device: &'static evdev::Device, pedals: impl Iterator<Item = Pedal>) {
let mut handles: Vec<_> = Vec::new(); let mut handles: Vec<_> = Vec::new();
let (tx, rx) = std::sync::mpsc::channel();
for pedal in pedals { for pedal in pedals {
let tx = tx.clone();
let handle = std::thread::spawn(move || loop { let handle = std::thread::spawn(move || loop {
if input_device.get_key_state().unwrap().contains(pedal.key) { if input_device.get_key_state().unwrap().contains(pedal.key) {
(*pedal.action)(); (*pedal.action)(&tx);
} }
std::thread::sleep(std::time::Duration::from_millis(100)); std::thread::sleep(std::time::Duration::from_millis(100));
}); });
handles.push(handle); handles.push(handle);
} }
for recieved in rx {
println!("{}", recieved);
}
for handle in handles { for handle in handles {
handle.join().expect("Couldn't join"); handle.join().expect("Couldn't join");
} }
@ -74,5 +81,5 @@ fn create_device() -> Result<evdev::uinput::VirtualDevice, Box<dyn std::error::E
pub struct Pedal { pub struct Pedal {
key: evdev::Key, key: evdev::Key,
action: Box<dyn Fn() -> () + Send + 'static>, action: Box<dyn Fn(&std::sync::mpsc::Sender<String>) -> () + Send + 'static>,
} }

@ -5,15 +5,15 @@ pub fn pedals() -> Vec<Pedal> {
vec![ vec![
Pedal { Pedal {
key: Key::BTN_SIDE, key: Key::BTN_SIDE,
action: Box::new(|| println!("BTN_SIDE")), action: Box::new(|tx| tx.send(String::from("BTN_SIDE")).unwrap()),
}, },
Pedal { Pedal {
key: Key::BTN_EXTRA, key: Key::BTN_EXTRA,
action: Box::new(|| println!("BTN_EXTRA")), action: Box::new(|tx| tx.send(String::from("BTN_EXTRA")).unwrap()),
}, },
Pedal { Pedal {
key: Key::BTN_RIGHT, key: Key::BTN_RIGHT,
action: Box::new(|| println!("BTN_RIGHT")), action: Box::new(|tx| tx.send(String::from("BTN_RIGHT")).unwrap()),
}, },
] ]
} }

Loading…
Cancel
Save