ref: use mbe rust macro mash! to dry

v0.1.1
rrr-marble 5 years ago
parent eaad057411
commit dddc3322c0

@ -5,71 +5,43 @@
pub use crate::Pedal; pub use crate::Pedal;
use evdev::{EventType, InputEvent, Key}; use evdev::{EventType, InputEvent, Key};
macro_rules! mash {
( $key:ident, $dt:literal, $ut:literal, $tx:ident ) => {{
let code = Key::$key.code();
let down_event = InputEvent::new(EventType::KEY, code, 1);
let up_event = InputEvent::new(EventType::KEY, code, 0);
$tx.send(down_event).unwrap();
std::thread::sleep(std::time::Duration::from_millis($dt));
$tx.send(up_event).unwrap();
std::thread::sleep(std::time::Duration::from_millis($ut));
}};
}
/// Returns a vector of currently defined key-action pairs /// Returns a vector of currently defined key-action pairs
pub fn pedals() -> Vec<Pedal> { pub fn pedals() -> Vec<Pedal> {
vec![ vec![
Pedal { Pedal {
key: Key::KEY_KP0, key: Key::KEY_KP7,
action: Box::new(|tx| { action: Box::new(|tx| mash!(KEY_SPACE, 26, 31, tx)),
let code = Key::KEY_SPACE.code();
let down_event = InputEvent::new(EventType::KEY, code, 1);
let up_event = InputEvent::new(EventType::KEY, code, 0);
tx.send(down_event).unwrap();
std::thread::sleep(std::time::Duration::from_millis(26));
tx.send(up_event).unwrap();
std::thread::sleep(std::time::Duration::from_millis(31));
}),
}, },
Pedal { Pedal {
key: Key::BTN_SIDE, key: Key::BTN_SIDE,
action: Box::new(|tx| { action: Box::new(|tx| mash!(KEY_SPACE, 26, 31, tx)),
let code = Key::KEY_SPACE.code();
let down_event = InputEvent::new(EventType::KEY, code, 1);
let up_event = InputEvent::new(EventType::KEY, code, 0);
tx.send(down_event).unwrap();
std::thread::sleep(std::time::Duration::from_millis(26));
tx.send(up_event).unwrap();
std::thread::sleep(std::time::Duration::from_millis(31));
}),
}, },
Pedal { Pedal {
key: Key::BTN_EXTRA, key: Key::BTN_EXTRA,
action: Box::new(|tx| { action: Box::new(|tx| mash!(KEY_F, 24, 33, tx)),
let code = Key::KEY_F.code();
let down_event = InputEvent::new(EventType::KEY, code, 1);
let up_event = InputEvent::new(EventType::KEY, code, 0);
tx.send(down_event).unwrap();
std::thread::sleep(std::time::Duration::from_millis(24));
tx.send(up_event).unwrap();
std::thread::sleep(std::time::Duration::from_millis(33));
}),
}, },
Pedal { Pedal {
key: Key::KEY_KP6, key: Key::KEY_KP9,
action: Box::new(|tx| { action: Box::new(|tx| mash!(KEY_F, 24, 33, tx)),
let code = Key::KEY_F.code();
let down_event = InputEvent::new(EventType::KEY, code, 1);
let up_event = InputEvent::new(EventType::KEY, code, 0);
tx.send(down_event).unwrap();
std::thread::sleep(std::time::Duration::from_millis(24));
tx.send(up_event).unwrap();
std::thread::sleep(std::time::Duration::from_millis(33));
}),
}, },
Pedal { Pedal {
key: Key::BTN_RIGHT, key: Key::BTN_RIGHT,
action: Box::new(|tx| { action: Box::new(|tx| mash!(BTN_LEFT, 19, 26, tx)),
let code = Key::BTN_LEFT.code();
let down_event = InputEvent::new(EventType::KEY, code, 1);
let up_event = InputEvent::new(EventType::KEY, code, 0);
tx.send(down_event).unwrap();
std::thread::sleep(std::time::Duration::from_millis(19));
tx.send(up_event).unwrap();
std::thread::sleep(std::time::Duration::from_millis(26));
}),
}, },
Pedal { Pedal {
key: Key::KEY_KP4, key: Key::KEY_KP8,
action: Box::new(|tx| { action: Box::new(|tx| {
let code = Key::BTN_LEFT.code(); let code = Key::BTN_LEFT.code();
let down_event = InputEvent::new(EventType::KEY, code, 1); let down_event = InputEvent::new(EventType::KEY, code, 1);

Loading…
Cancel
Save