Skip to content

Commit dbb7316

Browse files
committed
Allow using stdio as a 'Device'
1 parent 88e7f31 commit dbb7316

File tree

3 files changed

+162
-31
lines changed

3 files changed

+162
-31
lines changed

src/main.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ mod data;
2424
mod gui;
2525
mod io;
2626
mod serial;
27+
mod stdio;
2728
mod toggle;
2829

2930
const APP_INFO: AppInfo = AppInfo {
@@ -34,12 +35,8 @@ const PREFS_KEY: &str = "config/gui";
3435
const PREFS_KEY_SERIAL: &str = "config/serial_devices";
3536

3637
fn split(payload: &str) -> Vec<f32> {
37-
let mut split_data: Vec<&str> = vec![];
38-
for s in payload.split(':') {
39-
split_data.extend(s.split(','));
40-
}
41-
split_data
42-
.iter()
38+
payload
39+
.split(&[':', ',', '=', ' ', '\t'])
4340
.map(|x| x.trim())
4441
.flat_map(|x| x.parse::<f32>())
4542
.collect()

src/serial.rs

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize};
88
use serialport::{DataBits, FlowControl, Parity, SerialPort, StopBits};
99

1010
use crate::data::{get_epoch_ms, SerialDirection};
11-
use crate::{print_to_console, Packet, Print, APP_INFO, PREFS_KEY_SERIAL};
11+
use crate::{print_to_console, stdio, Packet, Print, APP_INFO, PREFS_KEY_SERIAL};
1212

1313
#[derive(Debug, Clone, Serialize, Deserialize)]
1414
pub struct SerialDevices {
@@ -112,32 +112,41 @@ pub fn serial_thread(
112112

113113
let device = get_device(&devices_lock, &device_lock);
114114

115-
let mut port = match serialport::new(&device.name, device.baud_rate)
116-
.timeout(Duration::from_millis(100))
117-
.open()
118-
{
119-
Ok(p) => {
120-
if let Ok(mut connected) = connected_lock.write() {
121-
*connected = true;
122-
}
123-
print_to_console(
124-
&print_lock,
125-
Print::Ok(format!(
126-
"Connected to serial port: {} @ baud = {}",
127-
device.name, device.baud_rate
128-
)),
129-
);
130-
BufReader::new(p)
115+
let mut port = if device.name == "stdio" {
116+
if let Ok(mut connected) = connected_lock.write() {
117+
*connected = true;
131118
}
132-
Err(err) => {
133-
if let Ok(mut write_guard) = device_lock.write() {
134-
write_guard.name.clear();
119+
print_to_console(&print_lock, Print::Ok(format!("Connected to stdio")));
120+
121+
BufReader::new(Box::new(stdio::Stdio) as _)
122+
} else {
123+
match serialport::new(&device.name, device.baud_rate)
124+
.timeout(Duration::from_millis(100))
125+
.open()
126+
{
127+
Ok(p) => {
128+
if let Ok(mut connected) = connected_lock.write() {
129+
*connected = true;
130+
}
131+
print_to_console(
132+
&print_lock,
133+
Print::Ok(format!(
134+
"Connected to serial port: {} @ baud = {}",
135+
device.name, device.baud_rate
136+
)),
137+
);
138+
BufReader::new(p)
139+
}
140+
Err(err) => {
141+
if let Ok(mut write_guard) = device_lock.write() {
142+
write_guard.name.clear();
143+
}
144+
print_to_console(
145+
&print_lock,
146+
Print::Error(format!("Error connecting: {}", err)),
147+
);
148+
continue;
135149
}
136-
print_to_console(
137-
&print_lock,
138-
Print::Error(format!("Error connecting: {}", err)),
139-
);
140-
continue;
141150
}
142151
};
143152

@@ -176,6 +185,7 @@ fn available_devices() -> Vec<String> {
176185
.unwrap()
177186
.iter()
178187
.map(|p| p.port_name.clone())
188+
.chain(std::iter::once("stdio".into()))
179189
.collect()
180190
}
181191

src/stdio.rs

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
use std::{io, time};
2+
3+
pub struct Stdio;
4+
5+
impl serialport::SerialPort for Stdio {
6+
fn name(&self) -> Option<String> {
7+
todo!()
8+
}
9+
10+
fn baud_rate(&self) -> serialport::Result<u32> {
11+
todo!()
12+
}
13+
14+
fn data_bits(&self) -> serialport::Result<serialport::DataBits> {
15+
todo!()
16+
}
17+
18+
fn flow_control(&self) -> serialport::Result<serialport::FlowControl> {
19+
todo!()
20+
}
21+
22+
fn parity(&self) -> serialport::Result<serialport::Parity> {
23+
todo!()
24+
}
25+
26+
fn stop_bits(&self) -> serialport::Result<serialport::StopBits> {
27+
todo!()
28+
}
29+
30+
fn timeout(&self) -> time::Duration {
31+
todo!()
32+
}
33+
34+
fn set_baud_rate(&mut self, _baud_rate: u32) -> serialport::Result<()> {
35+
todo!()
36+
}
37+
38+
fn set_data_bits(&mut self, _data_bits: serialport::DataBits) -> serialport::Result<()> {
39+
todo!()
40+
}
41+
42+
fn set_flow_control(
43+
&mut self,
44+
_flow_control: serialport::FlowControl,
45+
) -> serialport::Result<()> {
46+
todo!()
47+
}
48+
49+
fn set_parity(&mut self, _parity: serialport::Parity) -> serialport::Result<()> {
50+
todo!()
51+
}
52+
53+
fn set_stop_bits(&mut self, _stop_bits: serialport::StopBits) -> serialport::Result<()> {
54+
todo!()
55+
}
56+
57+
fn set_timeout(&mut self, _timeout: time::Duration) -> serialport::Result<()> {
58+
todo!()
59+
}
60+
61+
fn write_request_to_send(&mut self, _level: bool) -> serialport::Result<()> {
62+
todo!()
63+
}
64+
65+
fn write_data_terminal_ready(&mut self, _level: bool) -> serialport::Result<()> {
66+
todo!()
67+
}
68+
69+
fn read_clear_to_send(&mut self) -> serialport::Result<bool> {
70+
todo!()
71+
}
72+
73+
fn read_data_set_ready(&mut self) -> serialport::Result<bool> {
74+
todo!()
75+
}
76+
77+
fn read_ring_indicator(&mut self) -> serialport::Result<bool> {
78+
todo!()
79+
}
80+
81+
fn read_carrier_detect(&mut self) -> serialport::Result<bool> {
82+
todo!()
83+
}
84+
85+
fn bytes_to_read(&self) -> serialport::Result<u32> {
86+
todo!()
87+
}
88+
89+
fn bytes_to_write(&self) -> serialport::Result<u32> {
90+
todo!()
91+
}
92+
93+
fn clear(&self, _buffer_to_clear: serialport::ClearBuffer) -> serialport::Result<()> {
94+
todo!()
95+
}
96+
97+
fn try_clone(&self) -> serialport::Result<Box<dyn serialport::SerialPort>> {
98+
todo!()
99+
}
100+
101+
fn set_break(&self) -> serialport::Result<()> {
102+
todo!()
103+
}
104+
105+
fn clear_break(&self) -> serialport::Result<()> {
106+
todo!()
107+
}
108+
}
109+
110+
impl io::Write for Stdio {
111+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
112+
io::stdout().write(buf)
113+
}
114+
115+
fn flush(&mut self) -> io::Result<()> {
116+
io::stdout().flush()
117+
}
118+
}
119+
120+
impl io::Read for Stdio {
121+
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
122+
io::stdin().read(buf)
123+
}
124+
}

0 commit comments

Comments
 (0)