Switch to a library and move main to an example

This commit is contained in:
Robert Sammelson 2023-05-14 23:07:54 -04:00
parent 559fbbaa12
commit 2a43bf94bd
No known key found for this signature in database
GPG key ID: 92F1F04EDB06B9E9
9 changed files with 21 additions and 389 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
/target /target
Cargo.lock

378
Cargo.lock generated
View file

@ -1,378 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aho-corasick"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67fc08ce920c31afb70f013dcce1bfc3a3195de6a228474e45e1f145b36f8d04"
dependencies = [
"memchr",
]
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "cc"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "env_logger"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85cdab6a89accf66733ad5a1693a4dcced6aeff64602b634530dd73c1f3ee9f0"
dependencies = [
"humantime",
"is-terminal",
"log",
"regex",
"termcolor",
]
[[package]]
name = "errno"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bcfec3a70f97c962c307b2d2c56e358cf1d00b558d74262b5f929ee8cc7e73a"
dependencies = [
"errno-dragonfly",
"libc",
"windows-sys",
]
[[package]]
name = "errno-dragonfly"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf"
dependencies = [
"cc",
"libc",
]
[[package]]
name = "ftdi"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f9c8c625c6b8634ce70cd8cbb2ab8a103e4c2b4185c86d9954d2e16b1ef7c4a"
dependencies = [
"ftdi-mpsse",
"libftdi1-sys",
"thiserror",
]
[[package]]
name = "ftdi-mpsse"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa7cfcda69930a8d2fdcdd7ffb9234fe4c79a8c73934ed4904327d77bfb5078a"
dependencies = [
"static_assertions",
]
[[package]]
name = "hermit-abi"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286"
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]]
name = "io-lifetimes"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c66c74d2ae7e79a5a8f7ac924adbe38ee42a859c6539ad869eb51f0b52dc220"
dependencies = [
"hermit-abi",
"libc",
"windows-sys",
]
[[package]]
name = "is-terminal"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adcf93614601c8129ddf72e2d5633df827ba6551541c6d8c59520a371475be1f"
dependencies = [
"hermit-abi",
"io-lifetimes",
"rustix",
"windows-sys",
]
[[package]]
name = "libc"
version = "0.2.144"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1"
[[package]]
name = "libftdi1-sys"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ff6928872c7d13bec3c8a60c4c92f41f6252f3369b7552a5b4f9c90c8ba2338"
dependencies = [
"cfg-if",
"libc",
"pkg-config",
"vcpkg",
]
[[package]]
name = "linux-raw-sys"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ece97ea872ece730aed82664c424eb4c8291e1ff2480247ccf7409044bc6479f"
[[package]]
name = "log"
version = "0.4.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
dependencies = [
"cfg-if",
]
[[package]]
name = "memchr"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "obd"
version = "0.0.0"
dependencies = [
"env_logger",
"ftdi",
"log",
"thiserror",
]
[[package]]
name = "pkg-config"
version = "0.3.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964"
[[package]]
name = "proc-macro2"
version = "1.0.56"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f4f29d145265ec1c483c7c654450edde0bfe043d3938d6972630663356d9500"
dependencies = [
"proc-macro2",
]
[[package]]
name = "regex"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af83e617f331cc6ae2da5443c602dfa5af81e517212d9d611a5b3ba1777b5370"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5996294f19bd3aae0453a862ad728f60e6600695733dd5df01da90c54363a3c"
[[package]]
name = "rustix"
version = "0.37.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acf8729d8542766f1b2cf77eb034d52f40d375bb8b615d0b147089946e16613d"
dependencies = [
"bitflags",
"errno",
"io-lifetimes",
"libc",
"linux-raw-sys",
"windows-sys",
]
[[package]]
name = "static_assertions"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "syn"
version = "2.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6f671d4b5ffdb8eadec19c0ae67fe2639df8684bd7bc4b83d986b8db549cf01"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "termcolor"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6"
dependencies = [
"winapi-util",
]
[[package]]
name = "thiserror"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "978c9a314bd8dc99be594bc3c175faaa9794be04a5a5e153caba6915336cebac"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "unicode-ident"
version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4"
[[package]]
name = "vcpkg"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b1eb6f0cd7c80c79759c929114ef071b87354ce476d9d94271031c0497adfd5"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3"
[[package]]
name = "windows_i686_gnu"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241"
[[package]]
name = "windows_i686_msvc"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a"

View file

@ -1,6 +1,9 @@
[package] [package]
name = "obd" name = "obd2"
version = "0.0.0" description = "Utility for reading data from a vehicle over OBD-II"
license = "MIT OR Apache-2.0"
repository = "https://github.com/rsammelson/obd2"
version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]

View file

@ -1,13 +1,10 @@
#![forbid(unsafe_code)]
mod obd2;
use obd2::Obd2Device; use obd2::Obd2Device;
use std::time; 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::device::Elm327> = obd2::Obd2::default();
println!("VIN: {:?}", device.get_vin()); println!("VIN: {:?}", device.get_vin());
println!("DTC Info: {:#?}", device.get_dtc_info()); println!("DTC Info: {:#?}", device.get_dtc_info());

View file

@ -3,7 +3,11 @@ use core::fmt;
pub type Result<T> = std::result::Result<T, Error>; pub type Result<T> = std::result::Result<T, Error>;
pub trait Obd2Device { pub trait Obd2Device {
/// Send an OBD command with mode and PID, and get a list of responses (one for each ECU that
/// responds)
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>>>;
/// Like [obd_command](Self::obd_command), but for commands that do not require a PID
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>( fn obd_command_len<const RESPONSE_LENGTH: usize>(
@ -33,12 +37,15 @@ pub trait Obd2Device {
.map_err(|_| Error::IncorrectResponseLength("count", RESPONSE_COUNT, count)) .map_err(|_| Error::IncorrectResponseLength("count", RESPONSE_COUNT, count))
} }
/// Retreive the VIN (vehicle identification number), this should match the one printed on the
/// vehicle
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
Ok(String::from_utf8(result)?) Ok(String::from_utf8(result)?)
} }
/// Get DTC (diagnostic trouble code) metadata for each ECU
fn get_dtc_info(&mut self) -> Result<Vec<DtcsInfo>> { fn get_dtc_info(&mut self) -> Result<Vec<DtcsInfo>> {
let result = self.obd_command(0x01, 0x01)?; let result = self.obd_command(0x01, 0x01)?;
@ -65,6 +72,7 @@ pub trait Obd2Device {
.collect() .collect()
} }
/// Get DTCs for each ECU
fn get_dtcs(&mut self) -> Result<Vec<Vec<Dtc>>> { fn get_dtcs(&mut self) -> Result<Vec<Vec<Dtc>>> {
let result = self.obd_mode_command(0x03)?; let result = self.obd_mode_command(0x03)?;
result result
@ -102,11 +110,13 @@ pub trait Obd2Device {
.collect::<Result<Vec<Vec<Dtc>>>>() .collect::<Result<Vec<Vec<Dtc>>>>()
} }
/// Get the RPM in increments of 0.25
fn get_rpm(&mut self) -> Result<f32> { fn get_rpm(&mut self) -> Result<f32> {
let result = self.obd_command_cnt_len::<1, 2>(0x01, 0x0C)?[0]; let result = self.obd_command_cnt_len::<1, 2>(0x01, 0x0C)?[0];
Ok(f32::from(u16::from_be_bytes(result)) / 4.0) Ok(f32::from(u16::from_be_bytes(result)) / 4.0)
} }
/// Get the speed in km/h
fn get_speed(&mut self) -> Result<u8> { fn get_speed(&mut self) -> Result<u8> {
Ok(self.obd_command_cnt_len::<1, 1>(0x01, 0x0C)?[0][0]) Ok(self.obd_command_cnt_len::<1, 1>(0x01, 0x0C)?[0][0])
} }

View file

@ -39,7 +39,6 @@ impl Obd2BaseDevice for Elm327 {
fn send_serial_cmd(&mut self, data: &str) -> Result<()> { fn send_serial_cmd(&mut self, data: &str) -> Result<()> {
self.device.write_all(data.as_bytes())?; self.device.write_all(data.as_bytes())?;
self.device.write_all(b"\r\n")?; self.device.write_all(b"\r\n")?;
// thread::sleep(time::Duration::from_millis(200));
let line = self.get_line()?; let line = self.get_line()?;
if line.as_ref().is_some_and(|v| v == data.as_bytes()) { if line.as_ref().is_some_and(|v| v == data.as_bytes()) {
Ok(()) Ok(())

View file

@ -1,6 +1,6 @@
use log::{debug, trace}; use log::{debug, trace};
use super::{Error, Obd2BaseDevice, Obd2Device, Result}; use super::{device::Obd2BaseDevice, Error, Obd2Device, Result};
#[derive(Default)] #[derive(Default)]
pub struct Obd2<T: Obd2BaseDevice> { pub struct Obd2<T: Obd2BaseDevice> {

View file

@ -1,10 +1,10 @@
#![forbid(unsafe_code)]
mod accessors; mod accessors;
use accessors::Result; use accessors::Result;
pub use accessors::{Error, Obd2Device}; pub use accessors::{Error, Obd2Device};
mod device; pub mod device;
pub use device::Elm327;
use device::Obd2BaseDevice;
mod interface; mod interface;
pub use interface::Obd2; pub use interface::Obd2;