1
Fork 0

Expose errors

This commit is contained in:
Jake Howard 2024-09-21 22:23:56 +01:00
parent 6098ff699e
commit 2e10a7de1b
Signed by: jake
GPG key ID: 57AFB45680EDD477
3 changed files with 32 additions and 14 deletions

7
Cargo.lock generated
View file

@ -75,6 +75,7 @@ dependencies = [
name = "localdeck-keyboard" name = "localdeck-keyboard"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"once_cell",
"regex", "regex",
"serialport", "serialport",
] ]
@ -105,6 +106,12 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "once_cell"
version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]] [[package]]
name = "pkg-config" name = "pkg-config"
version = "0.3.30" version = "0.3.30"

View file

@ -4,5 +4,6 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
once_cell = "1.19.0"
regex = "1.10.6" regex = "1.10.6"
serialport = "4.5.1" serialport = "4.5.1"

View file

@ -1,10 +1,12 @@
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}; use std::io::{BufRead, BufReader, ErrorKind as IOErrorKind, Result as IOResult};
use std::time::Duration; use std::time::Duration;
// 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());
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!");
@ -19,18 +21,23 @@ fn get_board_port() -> Option<SerialPortInfo> {
None None
} }
fn get_keys(conn: Box<dyn SerialPort>) -> impl Iterator<Item = String> { fn key_from_line(line: String) -> Option<String> {
let reader = BufReader::new(conn); KEY_RE
let key_re = Regex::new(r"key '(?P<key>[A-Z])' pressed").unwrap(); .captures(&line)
.and_then(|captures| captures.name("key"))
.map(|k| String::from(k.as_str()))
}
reader.lines().filter_map(move |l| { fn get_keys(conn: Box<dyn SerialPort>) -> impl Iterator<Item = IOResult<String>> {
if let Ok(msg) = l { let reader = BufReader::new(conn);
return key_re
.captures(&msg) reader.lines().filter_map(|l| match l {
.and_then(|captures| captures.name("key")) // Discard timeouts
.map(|k| String::from(k.as_str())); Err(e) if e.kind() == IOErrorKind::TimedOut => None,
}
None Err(_) => Some(l),
Ok(s) => key_from_line(s).map(Ok),
}) })
} }
@ -49,7 +56,10 @@ fn main() {
.open() .open()
.unwrap(); .unwrap();
for key in get_keys(conn) { for maybe_key in get_keys(conn) {
println!("Key pressed: {}", key); match maybe_key {
Ok(key) => println!("Key pressed: {}", key),
Err(e) => println!("Error: {:?}", e),
}
} }
} }