diff --git a/src/main.rs b/src/main.rs index 68a04cb..941d19a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,16 @@ +use enigo::{Direction, Enigo, Key, Keyboard, Settings}; use once_cell::sync::Lazy; use regex::Regex; use serialport::{SerialPort, SerialPortInfo, SerialPortType}; use std::io::{BufRead, BufReader, ErrorKind as IOErrorKind, Result as IOResult}; use std::time::Duration; -use enigo::{Enigo, Direction, Settings, Keyboard, Key}; // My dev board - final might be different const VID_PID: (u16, u16) = (0x303a, 0x1001); -static KEY_RE: Lazy = Lazy::new(|| Regex::new(r"key '(?P[A-Z])' pressed").unwrap()); +static KEY_RE: Lazy = + Lazy::new(|| Regex::new(r"'Button (?P\d+)': Sending state ON").unwrap()); + +const KEY_COUNT: u8 = 24; fn get_board_port() -> Option { let ports = serialport::available_ports().expect("No ports found!"); @@ -22,37 +25,38 @@ fn get_board_port() -> Option { None } -fn key_from_line(line: String) -> Option { +fn key_from_line(line: String) -> Option { KEY_RE .captures(&line) .and_then(|captures| captures.name("key")) - .map(|k| String::from(k.as_str())) + .and_then(|k| k.as_str().parse::().ok()) + .filter(|k| *k <= KEY_COUNT) } -fn get_keys(conn: Box) -> impl Iterator> { +fn get_keys(conn: Box) -> impl Iterator> { let reader = BufReader::new(conn); reader.lines().filter_map(|l| match l { // Discard timeouts Err(e) if e.kind() == IOErrorKind::TimedOut => None, - Err(_) => Some(l), + Err(e) => Some(Err(e)), Ok(s) => key_from_line(s).map(Ok), }) } -fn handle_key(key: String) { +fn handle_key(key: u8) { let mut enigo = Enigo::new(&Settings::default()).unwrap(); println!("Key pressed: {}", key); - match key.as_str() { - "X" => { + match key { + 24 => { enigo.key(Key::VolumeUp, Direction::Click).unwrap(); - }, - "R" => { + } + 18 => { enigo.key(Key::VolumeDown, Direction::Click).unwrap(); } - _ => println!("Unhandled key: {} - doing nothing", key) + _ => println!("Unhandled key: {} - doing nothing", key), } }