diff --git a/examples/basic/main.rs b/examples/basic/main.rs index fb83144..7ecb3ad 100644 --- a/examples/basic/main.rs +++ b/examples/basic/main.rs @@ -4,11 +4,8 @@ use std::time; fn main() -> Result<(), obd2::Error> { env_logger::init(); - let mut device: obd2::Obd2> = obd2::Obd2::new( - obd2::device::Elm327::new( - obd2::device::FTDIDevice::new()? - )? - )?; + let mut device: obd2::Obd2> = + obd2::Obd2::new(obd2::device::Elm327::new(obd2::device::FTDIDevice::new()?)?)?; println!("VIN: {:?}", device.get_vin()); for s in device.get_service_1_pid_support_1()?.iter() { diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 443d8f0..6d60b1a 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -220,7 +220,7 @@ func! { /// Get service 1 PID support for $21 to $40 fn get_service_1_pid_support_2(0x01, 0x20) -> u32; - - // Get the fuel level (out of 255) + + // Get the fuel level (out of 255) fn get_fuel_level(0x01, 0x2F) -> u8; } diff --git a/src/device/elm327.rs b/src/device/elm327.rs index 8dd3513..735aab4 100644 --- a/src/device/elm327.rs +++ b/src/device/elm327.rs @@ -1,10 +1,7 @@ use log::{debug, info, trace}; -use std::{ - collections::VecDeque, - thread, time, -}; +use std::{collections::VecDeque, thread, time}; -use super::{Error, Obd2BaseDevice, Obd2Reader, Result, serial_comm::SerialComm}; +use super::{serial_comm::SerialComm, Error, Obd2BaseDevice, Obd2Reader, Result}; /// An ELM327 OBD-II adapter /// @@ -16,7 +13,7 @@ use super::{Error, Obd2BaseDevice, Obd2Reader, Result, serial_comm::SerialComm}; /// [Datasheet for v1.4b](https://github.com/rsammelson/obd2/blob/master/docs/ELM327DSH.pdf), and /// the [source](https://www.elmelectronics.com/products/dsheets/). pub struct Elm327 { - device: T, + device: T, buffer: VecDeque, baud_rate: u32, } @@ -58,7 +55,7 @@ impl Obd2Reader for Elm327 { } impl Elm327 { - /// Creates a new Elm327 adapter with the given + /// Creates a new Elm327 adapter with the given /// unserlying Serial Communication device pub fn new(serial_device: T) -> Result { let mut device = Elm327 { @@ -111,9 +108,7 @@ impl Elm327 { let response = self.get_response()?; debug!( "reset_ic: got response {:?}", - response - .as_ref() - .map(|l| std::str::from_utf8(l.as_slice())) + response.as_ref().map(|l| std::str::from_utf8(l.as_slice())) ); Ok(()) } @@ -121,16 +116,10 @@ impl Elm327 { fn reset_protocol(&mut self) -> Result<()> { info!("Performing protocol reset"); let elm_response = self.serial_cmd("ATSP0")?; - debug!( - "reset_protocol: got response {:?}", - elm_response - ); + debug!("reset_protocol: got response {:?}", elm_response); let obd_response = self.cmd(&[0x01, 0x00])?; - debug!( - "reset_protocol: got OBD response {:?}", - obd_response - ); + debug!("reset_protocol: got OBD response {:?}", obd_response); self.flush_buffers()?; Ok(()) diff --git a/src/device/mod.rs b/src/device/mod.rs index 96fca1c..31f0293 100644 --- a/src/device/mod.rs +++ b/src/device/mod.rs @@ -4,7 +4,7 @@ mod elm327; pub use elm327::Elm327; mod serial_comm; -pub use serial_comm::{SerialPort, FTDIDevice}; +pub use serial_comm::{FTDIDevice, SerialPort}; type Result = std::result::Result; @@ -56,7 +56,7 @@ pub enum Error { /// An error with the underlying [FTDI device](ftdi::Device) #[error("FTDI error: `{0:?}`")] Ftdi(ftdi::Error), - + /// An error with the underlying [serialport device](serialport::SerialPort) #[error("Serialport error: `{0:?}`")] Serialport(serialport::Error), diff --git a/src/device/serial_comm.rs b/src/device/serial_comm.rs index 0ee1dd1..0ab6467 100644 --- a/src/device/serial_comm.rs +++ b/src/device/serial_comm.rs @@ -1,6 +1,6 @@ -use std::time::Duration; use super::Result; use std::io::{Read, Write}; +use std::time::Duration; const DEFAULT_BAUD_RATE: u32 = 38_400; @@ -12,25 +12,25 @@ pub trait SerialComm { fn purge_buffers(&mut self) -> Result<()>; } -/// Communicate with a serial device using the -/// serialport library +/// Communicate with a serial device using the +/// serialport library /// /// /dev/tty* or similar on unix-like systems /// COM devices on Windows systems pub struct SerialPort { - device: Box + device: Box, } impl SerialPort { /// Creates a new instance of a SerialPort pub fn new(path: &str) -> Result { let device = serialport::new(path, DEFAULT_BAUD_RATE) - .timeout(Duration::from_millis(10)) - .parity(serialport::Parity::None) - .data_bits(serialport::DataBits::Eight) - .stop_bits(serialport::StopBits::One) - .path(path) - .open()?; + .timeout(Duration::from_millis(10)) + .parity(serialport::Parity::None) + .data_bits(serialport::DataBits::Eight) + .stop_bits(serialport::StopBits::One) + .path(path) + .open()?; Ok(Self { device }) } @@ -54,10 +54,10 @@ impl SerialComm for SerialPort { } } -/// Communicate with a USB to Serial FTDI device +/// Communicate with a USB to Serial FTDI device /// with the FTDI library pub struct FTDIDevice { - device: ftdi::Device + device: ftdi::Device, } impl FTDIDevice { @@ -83,7 +83,7 @@ impl SerialComm for FTDIDevice { fn read(&mut self, data: &mut [u8]) -> Result { Ok(self.device.read(data)?) } - + fn set_baud_rate(&mut self, baud_rate: u32) -> Result<()> { Ok(self.device.set_baud_rate(baud_rate)?) } @@ -92,4 +92,3 @@ impl SerialComm for FTDIDevice { Ok(self.device.usb_purge_buffers()?) } } - diff --git a/src/error.rs b/src/error.rs index 3498595..f6afd75 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,4 +1,4 @@ -//! Error types for OBD-II related errors +//! Error types for OBD-II related errors /// Result type defaulted with this library's error type pub type Result = std::result::Result; @@ -19,7 +19,7 @@ pub enum Error { Other(String), } -/// An error with the ELM327 device +/// An error with the ELM327 device #[derive(Debug)] pub struct DeviceError(pub crate::device::Error); diff --git a/src/interface.rs b/src/interface.rs index ae728fa..724ae15 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -42,9 +42,7 @@ impl Obd2Device for Obd2 { impl Obd2 { /// Creates a new instance of an Obd device pub fn new(dev: T) -> Result { - let device = Obd2 { - device: dev - }; + let device = Obd2 { device: dev }; Ok(device) } diff --git a/src/lib.rs b/src/lib.rs index 10bc301..2e04c5b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,7 +15,7 @@ //! } //! ``` //! -//! alternatively, you could use a serial port provided by your operating system such as +//! alternatively, you could use a serial port provided by your operating system such as //! /dev/ttyUSB0 on unix-like systems //! ``` //! let mut device = Obd2::>::new(Elm327::new(SerialPort::new("/dev/ttyUSB0")?)?)?;