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>) {
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<evdev::uinput::VirtualDevice, Box<dyn std::error::E
pub struct Pedal {
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![
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()),
},
]
}

Loading…
Cancel
Save