1
Fork 0

Use u8 as key rather than string

It's much smaller and faster and easier to work with
This commit is contained in:
Jake Howard 2024-10-19 18:03:39 +01:00
parent b2a5c906ad
commit 576468d309
Signed by: jake
GPG key ID: 57AFB45680EDD477

View file

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