Add more getter functions to the commands module

This commit is contained in:
Robert Sammelson 2023-05-17 22:12:13 -04:00
parent 97f17af790
commit 8d521ff46d
No known key found for this signature in database
GPG key ID: 92F1F04EDB06B9E9
4 changed files with 42 additions and 6 deletions

View file

@ -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());
}

View file

@ -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<T: Obd2Device> Obd2DataRetrieval for T {
fn get_vin(&mut self) -> Result<String> {
@ -72,6 +72,22 @@ impl<T: Obd2Device> Obd2DataRetrieval for T {
.collect::<Result<Vec<Vec<Dtc>>>>()
}
fn get_engine_load(&mut self) -> Result<u8> {
Ok(self.obd_command_cnt_len::<1, 1>(0x01, 0x0C)?[0][0])
}
fn get_engine_coolant_temperature(&mut self) -> Result<i16> {
Ok((self.obd_command_cnt_len::<1, 1>(0x01, 0x0C)?[0][0] as i16) - 40)
}
fn get_fuel_pressure(&mut self) -> Result<i16> {
todo!()
}
fn get_engine_manifold_pressure(&mut self) -> Result<f32> {
todo!()
}
fn get_rpm(&mut self) -> Result<f32> {
let result = self.obd_command_cnt_len::<1, 2>(0x01, 0x0C)?[0];
Ok(f32::from(u16::from_be_bytes(result)) / 4.0)

View file

@ -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<String>;
/// 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<Vec<Vec<Dtc>>>;
/// Get the calculated engine
// fn get_engine_load(&mut self) -> Result<u8>;
/// Get the calculated engine load (out of 255)
fn get_engine_load(&mut self) -> Result<u8>;
/// Get the temperature of the engine's coolant in ºC
fn get_engine_coolant_temperature(&mut self) -> Result<i16>;
/// Get the fuel pressure in kPa
///
/// This measurement is gauge pressure (measured relative to the atmosphere)
fn get_fuel_pressure(&mut self) -> Result<i16>;
/// Get the intake manifold pressure in kPa
///
/// This measurement is absolute pressure.
fn get_engine_manifold_pressure(&mut self) -> Result<f32>;
/// Get the RPM in increments of 0.25
fn get_rpm(&mut self) -> Result<f32>;

View file

@ -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};