added serial
This commit is contained in:
@@ -24,3 +24,4 @@ bincode = { version = "2.0.0-rc.3", default-features = false, features = [
|
||||
simple_logger = "5.0.0"
|
||||
rp2040-hal = "0.10.2"
|
||||
rp2040-pac = "0.6.0"
|
||||
mio-serial = "=5.0.5"
|
||||
|
||||
+2
-1
@@ -5,7 +5,7 @@ edition = "2021"
|
||||
|
||||
[features]
|
||||
default = []
|
||||
std = ["dep:mio"]
|
||||
std = ["dep:mio", "dep:mio-serial"]
|
||||
rp2040 = ["dep:rp2040-hal", "dep:rp2040-pac"]
|
||||
reflect = []
|
||||
|
||||
@@ -18,3 +18,4 @@ mio = { version = "1.0.2", features = ["net", "os-poll"], optional = true }
|
||||
rp2040-hal = { workspace = true, optional = true }
|
||||
rp2040-pac = { workspace = true, optional = true }
|
||||
macros.path = "macros"
|
||||
mio-serial = { workspace = true, optional = true }
|
||||
|
||||
@@ -14,6 +14,7 @@ pub use rfe::*;
|
||||
pub mod reflect;
|
||||
|
||||
pub use macros;
|
||||
pub mod serial;
|
||||
pub mod time;
|
||||
pub mod utils;
|
||||
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
use anyhow::Result;
|
||||
|
||||
pub trait SerialPort {
|
||||
fn open(&mut self) -> Result<()>;
|
||||
fn write(&mut self, bytes: &[u8]) -> Result<usize>;
|
||||
fn read(&mut self, bytes: &mut [u8]) -> Result<usize>;
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
mod serial_std {
|
||||
use std::io::{Read, Write};
|
||||
|
||||
use anyhow::anyhow;
|
||||
use mio_serial::{SerialPortBuilder, SerialPortBuilderExt, SerialStream};
|
||||
pub struct StdSerialPort {
|
||||
config: SerialPortBuilder,
|
||||
serial: Option<SerialStream>,
|
||||
}
|
||||
|
||||
impl StdSerialPort {
|
||||
pub fn new(config: SerialPortBuilder) -> Self {
|
||||
Self {
|
||||
config,
|
||||
serial: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl super::SerialPort for StdSerialPort {
|
||||
fn open(&mut self) -> anyhow::Result<()> {
|
||||
self.serial = None;
|
||||
self.serial = Some(self.config.clone().open_native_async()?);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
fn write(&mut self, bytes: &[u8]) -> anyhow::Result<usize> {
|
||||
Ok(self
|
||||
.serial
|
||||
.as_mut()
|
||||
.ok_or(anyhow!("port not open"))?
|
||||
.write(bytes)?)
|
||||
}
|
||||
|
||||
fn read(&mut self, bytes: &mut [u8]) -> anyhow::Result<usize> {
|
||||
Ok(self
|
||||
.serial
|
||||
.as_mut()
|
||||
.ok_or(anyhow!("port not open"))?
|
||||
.read(bytes)?)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
pub use serial_std::*;
|
||||
Reference in New Issue
Block a user