Expose errors
This commit is contained in:
parent
6098ff699e
commit
2e10a7de1b
3 changed files with 32 additions and 14 deletions
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -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"
|
||||||
|
|
|
@ -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"
|
||||||
|
|
36
src/main.rs
36
src/main.rs
|
@ -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)
|
||||||
|
|
||||||
reader.lines().filter_map(move |l| {
|
|
||||||
if let Ok(msg) = l {
|
|
||||||
return key_re
|
|
||||||
.captures(&msg)
|
|
||||||
.and_then(|captures| captures.name("key"))
|
.and_then(|captures| captures.name("key"))
|
||||||
.map(|k| String::from(k.as_str()));
|
.map(|k| String::from(k.as_str()))
|
||||||
}
|
}
|
||||||
None
|
|
||||||
|
fn get_keys(conn: Box<dyn SerialPort>) -> impl Iterator<Item = IOResult<String>> {
|
||||||
|
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),
|
||||||
|
|
||||||
|
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),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue