Add get_rpm and get_speed

This commit is contained in:
Robert Sammelson 2023-05-14 22:16:28 -04:00
parent 4e8bfd38d1
commit 559fbbaa12
No known key found for this signature in database
GPG key ID: 92F1F04EDB06B9E9
3 changed files with 54 additions and 5 deletions

4
Cargo.lock generated
View file

@ -230,9 +230,9 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.15" version = "2.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a34fcf3e8b60f57e6a14301a2e916d323af98b0ea63c599441eec8558660c822" checksum = "a6f671d4b5ffdb8eadec19c0ae67fe2639df8684bd7bc4b83d986b8db549cf01"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",

View file

@ -3,6 +3,8 @@
mod obd2; mod obd2;
use obd2::Obd2Device; use obd2::Obd2Device;
use std::time;
fn main() { fn main() {
env_logger::init(); env_logger::init();
let mut device: obd2::Obd2<obd2::Elm327> = obd2::Obd2::default(); let mut device: obd2::Obd2<obd2::Elm327> = obd2::Obd2::default();
@ -20,4 +22,10 @@ fn main() {
} }
} }
} }
let state = time::Instant::now();
while state.elapsed() < time::Duration::from_secs(5) {
println!("RPM: {:?}", device.get_rpm());
println!("Speed: {:?}", device.get_speed());
}
} }

View file

@ -6,6 +6,33 @@ pub trait Obd2Device {
fn obd_command(&mut self, mode: u8, pid: u8) -> Result<Vec<Vec<u8>>>; fn obd_command(&mut self, mode: u8, pid: u8) -> Result<Vec<Vec<u8>>>;
fn obd_mode_command(&mut self, mode: u8) -> Result<Vec<Vec<u8>>>; fn obd_mode_command(&mut self, mode: u8) -> Result<Vec<Vec<u8>>>;
fn obd_command_len<const RESPONSE_LENGTH: usize>(
&mut self,
mode: u8,
pid: u8,
) -> Result<Vec<[u8; RESPONSE_LENGTH]>> {
self.obd_command(mode, pid)?
.into_iter()
.map(|v| {
let l = v.len();
v.try_into()
.map_err(|_| Error::IncorrectResponseLength("length", RESPONSE_LENGTH, l))
})
.collect()
}
fn obd_command_cnt_len<const RESPONSE_COUNT: usize, const RESPONSE_LENGTH: usize>(
&mut self,
mode: u8,
pid: u8,
) -> Result<[[u8; RESPONSE_LENGTH]; RESPONSE_COUNT]> {
let result = self.obd_command_len::<RESPONSE_LENGTH>(mode, pid)?;
let count = result.len();
result
.try_into()
.map_err(|_| Error::IncorrectResponseLength("count", RESPONSE_COUNT, count))
}
fn get_vin(&mut self) -> Result<String> { fn get_vin(&mut self) -> Result<String> {
let mut result = self.obd_command(0x09, 0x02)?.pop().unwrap(); let mut result = self.obd_command(0x09, 0x02)?.pop().unwrap();
result.remove(0); // do not know what this byte is result.remove(0); // do not know what this byte is
@ -74,6 +101,15 @@ pub trait Obd2Device {
}) })
.collect::<Result<Vec<Vec<Dtc>>>>() .collect::<Result<Vec<Vec<Dtc>>>>()
} }
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)
}
fn get_speed(&mut self) -> Result<u8> {
Ok(self.obd_command_cnt_len::<1, 1>(0x01, 0x0C)?[0][0])
}
} }
#[allow(dead_code)] #[allow(dead_code)]
@ -108,15 +144,20 @@ impl fmt::Display for Dtc {
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]
pub enum Error { pub enum Error {
#[error("Communication error: `{0:?}`")] #[error("Device error: `{0:?}`")]
Communication(super::device::Error), Device(DeviceError),
#[error("Other OBD2 error: `{0}`")] #[error("Other OBD2 error: `{0}`")]
Other(String), Other(String),
#[error("Incorrect length (`{0}`): expected `{1}`, got `{2}`")]
IncorrectResponseLength(&'static str, usize, usize),
} }
#[derive(Debug)]
pub struct DeviceError(super::device::Error);
impl From<super::device::Error> for Error { impl From<super::device::Error> for Error {
fn from(e: super::device::Error) -> Self { fn from(e: super::device::Error) -> Self {
Error::Communication(e) Error::Device(DeviceError(e))
} }
} }