ran rustfmt
This commit is contained in:
parent
e0b5bab35a
commit
fde9f2f408
|
@ -4,11 +4,8 @@ use std::time;
|
|||
|
||||
fn main() -> Result<(), obd2::Error> {
|
||||
env_logger::init();
|
||||
let mut device: obd2::Obd2<obd2::device::Elm327<obd2::device::FTDIDevice>> = obd2::Obd2::new(
|
||||
obd2::device::Elm327::new(
|
||||
obd2::device::FTDIDevice::new()?
|
||||
)?
|
||||
)?;
|
||||
let mut device: obd2::Obd2<obd2::device::Elm327<obd2::device::FTDIDevice>> =
|
||||
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() {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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<T: SerialComm> {
|
||||
device: T,
|
||||
device: T,
|
||||
buffer: VecDeque<u8>,
|
||||
baud_rate: u32,
|
||||
}
|
||||
|
@ -58,7 +55,7 @@ impl<T: SerialComm> Obd2Reader for Elm327<T> {
|
|||
}
|
||||
|
||||
impl<T: SerialComm> Elm327<T> {
|
||||
/// 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<Self> {
|
||||
let mut device = Elm327 {
|
||||
|
@ -111,9 +108,7 @@ impl<T: SerialComm> Elm327<T> {
|
|||
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<T: SerialComm> Elm327<T> {
|
|||
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(())
|
||||
|
|
|
@ -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<T> = std::result::Result<T, Error>;
|
||||
|
||||
|
@ -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),
|
||||
|
|
|
@ -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<dyn serialport::SerialPort>
|
||||
device: Box<dyn serialport::SerialPort>,
|
||||
}
|
||||
|
||||
impl SerialPort {
|
||||
/// Creates a new instance of a SerialPort
|
||||
pub fn new(path: &str) -> Result<Self> {
|
||||
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<usize> {
|
||||
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()?)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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<T> = std::result::Result<T, Error>;
|
||||
|
@ -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);
|
||||
|
||||
|
|
|
@ -42,9 +42,7 @@ impl<T: Obd2BaseDevice> Obd2Device for Obd2<T> {
|
|||
impl<T: Obd2BaseDevice> Obd2<T> {
|
||||
/// Creates a new instance of an Obd device
|
||||
pub fn new(dev: T) -> Result<Self> {
|
||||
let device = Obd2 {
|
||||
device: dev
|
||||
};
|
||||
let device = Obd2 { device: dev };
|
||||
|
||||
Ok(device)
|
||||
}
|
||||
|
|
|
@ -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::<Elm327::<SerialPort>>::new(Elm327::new(SerialPort::new("/dev/ttyUSB0")?)?)?;
|
||||
|
|
Loading…
Reference in a new issue