Switch to a library and move main to an example

This commit is contained in:
Robert Sammelson 2023-05-14 23:07:54 -04:00
parent 559fbbaa12
commit 2a43bf94bd
No known key found for this signature in database
GPG key ID: 92F1F04EDB06B9E9
9 changed files with 21 additions and 389 deletions

42
src/device/mod.rs Normal file
View file

@ -0,0 +1,42 @@
mod elm327;
pub use elm327::Elm327;
type Result<T> = std::result::Result<T, Error>;
pub trait Obd2BaseDevice: Obd2Reader {
fn reset(&mut self) -> Result<()>;
fn flush(&mut self) -> Result<()>;
fn send_serial_cmd(&mut self, data: &str) -> Result<()>;
fn cmd(&mut self, cmd: &str) -> Result<Option<String>> {
self.send_serial_cmd(cmd)?;
self.get_until_prompt()
.map(|o| o.and_then(|resp| String::from_utf8(resp).ok()))
}
}
pub trait Obd2Reader {
fn get_line(&mut self) -> Result<Option<Vec<u8>>>;
fn get_until_prompt(&mut self) -> Result<Option<Vec<u8>>>;
}
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("FTDI error: `{0:?}`")]
Ftdi(ftdi::Error),
#[error("IO error: `{0:?}`")]
IO(std::io::Error),
#[error("Communication error: `{0}`")]
Communication(String),
}
impl From<ftdi::Error> for Error {
fn from(e: ftdi::Error) -> Self {
Error::Ftdi(e)
}
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Error::IO(e)
}
}