From c97894f84c4967c5c45e183a1914f37b536c77f0 Mon Sep 17 00:00:00 2001 From: huntc Date: Thu, 29 Apr 2021 14:43:11 +1000 Subject: [PATCH] Cater for non-Linux environments Doctests can be run using `cargo test --doc`. The examples still require Linux. --- Cargo.toml | 4 +++- src/lib.rs | 38 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3495125..7e962c3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,8 @@ env_logger = "0.7" futures = { version = "0.3" } i2cdev = "0.4" influx_db_client = { version = "0.4", default-features= false, features = ["rustls-tls"] } -linux-embedded-hal = "0.3" tokio = {version = "0.2", features = ["full"] } url = "2.1" + +[target.'cfg(target_os = "linux")'.dev-dependencies] +linux-embedded-hal = "0.3" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 8df1b24..25b80cf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,8 @@ //! extern crate bme680; //! extern crate embedded_hal; -//! extern crate linux_embedded_hal as hal; +//! // Note that you'll have to import your board crates types corresponding to +//! // Delay and I2cdev. //! //! use bme680::*; //! use embedded_hal::blocking::i2c; @@ -15,10 +16,43 @@ //! use std::result; //! use std::time::Duration; //! +//! # mod hal { +//! # use super::*; +//! # use embedded_hal::blocking::delay; +//! # +//! # #[derive(Debug)] +//! # pub struct Delay {} +//! # +//! # impl delay::DelayMs for Delay { +//! # fn delay_ms(&mut self, _ms: u8) {} +//! # } +//! # +//! # #[derive(Debug)] +//! # pub enum I2CError {} +//! # +//! # pub struct I2cdev {} +//! # +//! # impl i2c::Write for I2cdev { +//! # type Error = I2CError; +//! # +//! # fn write<'w>(&mut self, addr: u8, bytes: &'w [u8]) -> result::Result<(), Self::Error> { +//! # Ok(()) +//! # } +//! # } +//! # +//! # impl i2c::Read for I2cdev { +//! # type Error = I2CError; +//! # +//! # fn read<'w>(&mut self, addr: u8, bytes: &'w mut [u8]) -> result::Result<(), Self::Error> { +//! # Ok(()) +//! # } +//! # } +//! # } +//! //! fn main() -> result::Result<(), Error<::Error, ::Error>> //! { //! // Initialize device -//! let i2c = I2cdev::new("/dev/i2c-1").unwrap(); +//! let i2c = I2cdev {}; // Your I2C device construction will look different, perhaps using I2cdev::new(..) //! let mut dev = Bme680::init(i2c, Delay {}, I2CAddress::Primary)?; //! let settings = SettingsBuilder::new() //! .with_humidity_oversampling(OversamplingSetting::OS2x)