use std::io;
use std::time::Duration;
use crate::error::Result;
pub trait SabertoothSerial: io::Write + io::Read {
fn set_timeout(&mut self, timeout: Duration) -> Result<()>;
fn timeout(&self) -> Duration;
fn set_baud_rate(&mut self, baud_rate: u32) -> Result<()>;
fn baud_rate(&self) -> Result<u32>;
fn clear_all(&self) -> Result<()>;
}
#[cfg(feature = "serialport")]
pub mod sabertoothport {
use std::cell::RefCell;
use std::io;
use std::rc::Rc;
use std::time::Duration;
use serialport;
use serialport::{ClearBuffer, DataBits, FlowControl, Parity, StopBits};
use serialport::{SerialPort, SerialPortSettings};
use crate::{Result, SabertoothSerial};
const DEFAULT_BAUDRATE: u32 = 9600;
const DEFAULT_TIMEOUT_MS: u64 = 100;
const DEFAULT_SETTINGS: SerialPortSettings = SerialPortSettings {
baud_rate: DEFAULT_BAUDRATE,
data_bits: DataBits::Eight,
flow_control: FlowControl::None,
parity: Parity::None,
stop_bits: StopBits::One,
timeout: Duration::from_millis(DEFAULT_TIMEOUT_MS),
};
pub struct SabertoothPort {
dev: Box<dyn SerialPort>,
}
impl SabertoothPort {
pub fn new(port: &str) -> Result<SabertoothPort> {
let ser = serialport::open_with_settings(port, &DEFAULT_SETTINGS)?;
Ok(SabertoothPort { dev: ser })
}
}
impl SabertoothSerial for SabertoothPort {
fn set_timeout(&mut self, timeout: Duration) -> Result<()> {
Ok(self.dev.set_timeout(timeout)?)
}
fn timeout(&self) -> Duration {
self.dev.timeout()
}
fn set_baud_rate(&mut self, baud_rate: u32) -> Result<()> {
Ok(self.dev.set_baud_rate(baud_rate)?)
}
fn baud_rate(&self) -> Result<u32> {
Ok(self.dev.baud_rate()?)
}
fn clear_all(&self) -> Result<()> {
Ok(self.dev.clear(ClearBuffer::All)?)
}
}
impl io::Read for SabertoothPort {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.dev.read(buf)
}
}
impl io::Write for SabertoothPort {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.dev.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.dev.flush()
}
}
impl std::fmt::Debug for SabertoothPort {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"SabertoothPort({:?})",
self.dev.name().unwrap_or_else(|| String::from("_"))
)
}
}
#[derive(Clone)]
pub struct SabertoothPortShared {
dev: Rc<RefCell<Box<dyn SerialPort>>>,
}
impl SabertoothPortShared {
pub fn new(port: &str) -> Result<SabertoothPortShared> {
let ser = serialport::open_with_settings(port, &DEFAULT_SETTINGS)?;
Ok(SabertoothPortShared {
dev: Rc::new(RefCell::new(ser)),
})
}
}
impl SabertoothSerial for SabertoothPortShared {
fn set_timeout(&mut self, timeout: Duration) -> Result<()> {
Ok(self.dev.borrow_mut().set_timeout(timeout)?)
}
fn timeout(&self) -> Duration {
self.dev.borrow_mut().timeout()
}
fn set_baud_rate(&mut self, baud_rate: u32) -> Result<()> {
Ok(self.dev.borrow_mut().set_baud_rate(baud_rate)?)
}
fn baud_rate(&self) -> Result<u32> {
Ok(self.dev.borrow_mut().baud_rate()?)
}
fn clear_all(&self) -> Result<()> {
Ok(self.dev.borrow_mut().clear(ClearBuffer::All)?)
}
}
impl io::Read for SabertoothPortShared {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.dev.borrow_mut().read(buf)
}
}
impl io::Write for SabertoothPortShared {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.dev.borrow_mut().write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.dev.borrow_mut().flush()
}
}
impl std::fmt::Debug for SabertoothPortShared {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"SabertoothPortShared({:?})",
self.dev
.borrow_mut()
.name()
.unwrap_or_else(|| String::from("_"))
)
}
}
}