Skip to content

Commit

Permalink
Use suggested name 'Interface'
Browse files Browse the repository at this point in the history
  • Loading branch information
usbalbin committed Oct 26, 2023
1 parent 979803b commit 75bc670
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
31 changes: 31 additions & 0 deletions src/interface.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::io;

pub enum Interface {
SerialPort(Box<dyn serialport::SerialPort>),
Stdio,
}

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

fn flush(&mut self) -> io::Result<()> {
match self {
Interface::SerialPort(s) => s.flush(),
Interface::Stdio => io::stdout().flush(),
}
}
}

impl io::Read for Interface {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match self {
Interface::SerialPort(s) => s.read(buf),
Interface::Stdio => io::stdin().read(buf),
}
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use crate::io::{save_to_csv, FileOptions};
use crate::serial::{load_serial_settings, serial_thread, Device};

mod data;
mod data_source;
mod gui;
mod interface;
mod io;
mod serial;
mod toggle;
Expand Down
14 changes: 7 additions & 7 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, StopBits};

use crate::data::{get_epoch_ms, SerialDirection};
use crate::data_source::DataSource;
use crate::interface::Interface;
use crate::{print_to_console, Packet, Print, APP_INFO, PREFS_KEY_SERIAL};

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -75,13 +75,13 @@ impl Default for Device {
}
}

fn serial_write(port: &mut BufReader<DataSource>, cmd: &[u8]) -> Result<usize, std::io::Error> {
fn serial_write(port: &mut BufReader<Interface>, cmd: &[u8]) -> Result<usize, std::io::Error> {
let write_port = port.get_mut();
write_port.write(cmd)
}

fn serial_read(
port: &mut BufReader<DataSource>,
port: &mut BufReader<Interface>,
serial_buf: &mut String,
) -> Result<usize, std::io::Error> {
port.read_line(serial_buf)
Expand Down Expand Up @@ -116,7 +116,7 @@ pub fn serial_thread(
}
print_to_console(&print_lock, Print::Ok(format!("Connected to stdio")));

BufReader::new(DataSource::Stdio)
BufReader::new(Interface::Stdio)
} else {
match serialport::new(&device.name, device.baud_rate)
.timeout(Duration::from_millis(100))
Expand All @@ -133,7 +133,7 @@ pub fn serial_thread(
device.name, device.baud_rate
)),
);
BufReader::new(DataSource::SerialPort(p))
BufReader::new(Interface::SerialPort(p))
}
Err(err) => {
if let Ok(mut write_guard) = device_lock.write() {
Expand Down Expand Up @@ -236,7 +236,7 @@ fn disconnected(
}

fn perform_writes(
port: &mut BufReader<DataSource>,
port: &mut BufReader<Interface>,
send_rx: &Receiver<String>,
raw_data_tx: &Sender<Packet>,
t_zero: Instant,
Expand All @@ -259,7 +259,7 @@ fn perform_writes(
}
}

fn perform_reads(port: &mut BufReader<DataSource>, raw_data_tx: &Sender<Packet>, t_zero: Instant) {
fn perform_reads(port: &mut BufReader<Interface>, raw_data_tx: &Sender<Packet>, t_zero: Instant) {
let mut buf = "".to_string();
match serial_read(port, &mut buf) {
Ok(_) => {
Expand Down

0 comments on commit 75bc670

Please sign in to comment.