added serial

This commit is contained in:
Jacob Weaver
2024-12-19 09:52:40 -06:00
parent f59b164095
commit 27559a8d31
4 changed files with 59 additions and 1 deletions
+1
View File
@@ -24,3 +24,4 @@ bincode = { version = "2.0.0-rc.3", default-features = false, features = [
simple_logger = "5.0.0" simple_logger = "5.0.0"
rp2040-hal = "0.10.2" rp2040-hal = "0.10.2"
rp2040-pac = "0.6.0" rp2040-pac = "0.6.0"
mio-serial = "=5.0.5"
+2 -1
View File
@@ -5,7 +5,7 @@ edition = "2021"
[features] [features]
default = [] default = []
std = ["dep:mio"] std = ["dep:mio", "dep:mio-serial"]
rp2040 = ["dep:rp2040-hal", "dep:rp2040-pac"] rp2040 = ["dep:rp2040-hal", "dep:rp2040-pac"]
reflect = [] reflect = []
@@ -18,3 +18,4 @@ mio = { version = "1.0.2", features = ["net", "os-poll"], optional = true }
rp2040-hal = { workspace = true, optional = true } rp2040-hal = { workspace = true, optional = true }
rp2040-pac = { workspace = true, optional = true } rp2040-pac = { workspace = true, optional = true }
macros.path = "macros" macros.path = "macros"
mio-serial = { workspace = true, optional = true }
+1
View File
@@ -14,6 +14,7 @@ pub use rfe::*;
pub mod reflect; pub mod reflect;
pub use macros; pub use macros;
pub mod serial;
pub mod time; pub mod time;
pub mod utils; pub mod utils;
+55
View File
@@ -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::*;