Skip to content

Commit

Permalink
Allow using stdio as a 'Device'
Browse files Browse the repository at this point in the history
  • Loading branch information
usbalbin committed Oct 21, 2023
1 parent 88e7f31 commit dbb7316
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 31 deletions.
9 changes: 3 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod data;
mod gui;
mod io;
mod serial;
mod stdio;
mod toggle;

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

fn split(payload: &str) -> Vec<f32> {
let mut split_data: Vec<&str> = vec![];
for s in payload.split(':') {
split_data.extend(s.split(','));
}
split_data
.iter()
payload
.split(&[':', ',', '=', ' ', '\t'])
.map(|x| x.trim())
.flat_map(|x| x.parse::<f32>())
.collect()
Expand Down
60 changes: 35 additions & 25 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize};
use serialport::{DataBits, FlowControl, Parity, SerialPort, StopBits};

use crate::data::{get_epoch_ms, SerialDirection};
use crate::{print_to_console, Packet, Print, APP_INFO, PREFS_KEY_SERIAL};
use crate::{print_to_console, stdio, Packet, Print, APP_INFO, PREFS_KEY_SERIAL};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SerialDevices {
Expand Down Expand Up @@ -112,32 +112,41 @@ pub fn serial_thread(

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

let mut port = match serialport::new(&device.name, device.baud_rate)
.timeout(Duration::from_millis(100))
.open()
{
Ok(p) => {
if let Ok(mut connected) = connected_lock.write() {
*connected = true;
}
print_to_console(
&print_lock,
Print::Ok(format!(
"Connected to serial port: {} @ baud = {}",
device.name, device.baud_rate
)),
);
BufReader::new(p)
let mut port = if device.name == "stdio" {
if let Ok(mut connected) = connected_lock.write() {
*connected = true;
}
Err(err) => {
if let Ok(mut write_guard) = device_lock.write() {
write_guard.name.clear();
print_to_console(&print_lock, Print::Ok(format!("Connected to stdio")));

BufReader::new(Box::new(stdio::Stdio) as _)
} else {
match serialport::new(&device.name, device.baud_rate)
.timeout(Duration::from_millis(100))
.open()
{
Ok(p) => {
if let Ok(mut connected) = connected_lock.write() {
*connected = true;
}
print_to_console(
&print_lock,
Print::Ok(format!(
"Connected to serial port: {} @ baud = {}",
device.name, device.baud_rate
)),
);
BufReader::new(p)
}
Err(err) => {
if let Ok(mut write_guard) = device_lock.write() {
write_guard.name.clear();
}
print_to_console(
&print_lock,
Print::Error(format!("Error connecting: {}", err)),
);
continue;
}
print_to_console(
&print_lock,
Print::Error(format!("Error connecting: {}", err)),
);
continue;
}
};

Expand Down Expand Up @@ -176,6 +185,7 @@ fn available_devices() -> Vec<String> {
.unwrap()
.iter()
.map(|p| p.port_name.clone())
.chain(std::iter::once("stdio".into()))
.collect()
}

Expand Down
124 changes: 124 additions & 0 deletions src/stdio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
use std::{io, time};

pub struct Stdio;

impl serialport::SerialPort for Stdio {
fn name(&self) -> Option<String> {
todo!()
}

fn baud_rate(&self) -> serialport::Result<u32> {
todo!()
}

fn data_bits(&self) -> serialport::Result<serialport::DataBits> {
todo!()
}

fn flow_control(&self) -> serialport::Result<serialport::FlowControl> {
todo!()
}

fn parity(&self) -> serialport::Result<serialport::Parity> {
todo!()
}

fn stop_bits(&self) -> serialport::Result<serialport::StopBits> {
todo!()
}

fn timeout(&self) -> time::Duration {
todo!()
}

fn set_baud_rate(&mut self, _baud_rate: u32) -> serialport::Result<()> {
todo!()
}

fn set_data_bits(&mut self, _data_bits: serialport::DataBits) -> serialport::Result<()> {
todo!()
}

fn set_flow_control(
&mut self,
_flow_control: serialport::FlowControl,
) -> serialport::Result<()> {
todo!()
}

fn set_parity(&mut self, _parity: serialport::Parity) -> serialport::Result<()> {
todo!()
}

fn set_stop_bits(&mut self, _stop_bits: serialport::StopBits) -> serialport::Result<()> {
todo!()
}

fn set_timeout(&mut self, _timeout: time::Duration) -> serialport::Result<()> {
todo!()
}

fn write_request_to_send(&mut self, _level: bool) -> serialport::Result<()> {
todo!()
}

fn write_data_terminal_ready(&mut self, _level: bool) -> serialport::Result<()> {
todo!()
}

fn read_clear_to_send(&mut self) -> serialport::Result<bool> {
todo!()
}

fn read_data_set_ready(&mut self) -> serialport::Result<bool> {
todo!()
}

fn read_ring_indicator(&mut self) -> serialport::Result<bool> {
todo!()
}

fn read_carrier_detect(&mut self) -> serialport::Result<bool> {
todo!()
}

fn bytes_to_read(&self) -> serialport::Result<u32> {
todo!()
}

fn bytes_to_write(&self) -> serialport::Result<u32> {
todo!()
}

fn clear(&self, _buffer_to_clear: serialport::ClearBuffer) -> serialport::Result<()> {
todo!()
}

fn try_clone(&self) -> serialport::Result<Box<dyn serialport::SerialPort>> {
todo!()
}

fn set_break(&self) -> serialport::Result<()> {
todo!()
}

fn clear_break(&self) -> serialport::Result<()> {
todo!()
}
}

impl io::Write for Stdio {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
io::stdout().write(buf)
}

fn flush(&mut self) -> io::Result<()> {
io::stdout().flush()
}
}

impl io::Read for Stdio {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
io::stdin().read(buf)
}
}

0 comments on commit dbb7316

Please sign in to comment.