From 8d521ff46d1fb0e95bbd4556c18a9a6ab01ca47e Mon Sep 17 00:00:00 2001 From: Robert Sammelson Date: Wed, 17 May 2023 22:12:13 -0400 Subject: [PATCH] Add more getter functions to the commands module --- examples/basic/main.rs | 5 +++++ src/commands/implementation.rs | 20 ++++++++++++++++++-- src/commands/mod.rs | 21 +++++++++++++++++---- src/lib.rs | 2 ++ 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/examples/basic/main.rs b/examples/basic/main.rs index 9433b1f..291eb54 100644 --- a/examples/basic/main.rs +++ b/examples/basic/main.rs @@ -22,6 +22,11 @@ fn main() { let state = time::Instant::now(); while state.elapsed() < time::Duration::from_secs(5) { + println!(""); + println!( + "Coolant Temperature: {:?}", + device.get_engine_coolant_temperature() + ); println!("RPM: {:?}", device.get_rpm()); println!("Speed: {:?}", device.get_speed()); } diff --git a/src/commands/implementation.rs b/src/commands/implementation.rs index 2543ecf..0dcb3ba 100644 --- a/src/commands/implementation.rs +++ b/src/commands/implementation.rs @@ -1,6 +1,6 @@ -use crate::{commands::Obd2DataRetrieval, Error, Obd2Device, Result}; +use crate::{Error, Obd2Device, Result}; -use super::{Dtc, DtcsInfo}; +use super::{Dtc, DtcsInfo, Obd2DataRetrieval}; impl Obd2DataRetrieval for T { fn get_vin(&mut self) -> Result { @@ -72,6 +72,22 @@ impl Obd2DataRetrieval for T { .collect::>>>() } + fn get_engine_load(&mut self) -> Result { + Ok(self.obd_command_cnt_len::<1, 1>(0x01, 0x0C)?[0][0]) + } + + fn get_engine_coolant_temperature(&mut self) -> Result { + Ok((self.obd_command_cnt_len::<1, 1>(0x01, 0x0C)?[0][0] as i16) - 40) + } + + fn get_fuel_pressure(&mut self) -> Result { + todo!() + } + + fn get_engine_manifold_pressure(&mut self) -> Result { + todo!() + } + fn get_rpm(&mut self) -> Result { let result = self.obd_command_cnt_len::<1, 2>(0x01, 0x0C)?[0]; Ok(f32::from(u16::from_be_bytes(result)) / 4.0) diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 8de799c..64a10b0 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -16,8 +16,8 @@ pub trait Obd2DataRetrieval: private::Sealed { /// Retreive the VIN (vehicle identification number) /// - /// This should match the number printed on the vehicle, and is a good command for checking - /// that the OBD-II interface is working correctly. + /// Service 0x09, PID 0x01. This should match the number printed on the vehicle, and is a good + /// command for checking that the OBD-II interface is working correctly. fn get_vin(&mut self) -> Result; /// Get DTC (diagnostic trouble code) metadata for each ECU @@ -26,8 +26,21 @@ pub trait Obd2DataRetrieval: private::Sealed { /// Get DTCs for each ECU fn get_dtcs(&mut self) -> Result>>; - /// Get the calculated engine - // fn get_engine_load(&mut self) -> Result; + /// Get the calculated engine load (out of 255) + fn get_engine_load(&mut self) -> Result; + + /// Get the temperature of the engine's coolant in ÂșC + fn get_engine_coolant_temperature(&mut self) -> Result; + + /// Get the fuel pressure in kPa + /// + /// This measurement is gauge pressure (measured relative to the atmosphere) + fn get_fuel_pressure(&mut self) -> Result; + + /// Get the intake manifold pressure in kPa + /// + /// This measurement is absolute pressure. + fn get_engine_manifold_pressure(&mut self) -> Result; /// Get the RPM in increments of 0.25 fn get_rpm(&mut self) -> Result; diff --git a/src/lib.rs b/src/lib.rs index 11b59ba..b5b82be 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,7 @@ //! Crate for communicating with OBD-II (on-board diagnostics) interfaces on cars //! +//! The high-level data retrieval functions can be found in [commands::Obd2DataRetrieval]. +//! //! # Usage //! ``` //! use obd2::{commands::Obd2DataRetrieval, device::Elm327, Obd2};