From 65f0d1c6b2e584f41ea8ec226fa6fb169ea20c84 Mon Sep 17 00:00:00 2001 From: Sean Bruton Date: Tue, 24 Dec 2019 13:34:42 -0600 Subject: [PATCH] adds optional serde support, upgrades deps Signed-off-by: Sean Bruton --- Cargo.toml | 8 +++++--- README.md | 43 +++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 11 +++++------ 3 files changed, 53 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 94e5e05..462b63e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,15 +1,17 @@ [package] name = "bme280" -version = "0.1.2" +version = "0.2.0" authors = ["Sean Bruton "] description = "A rust device driver for the Bosch BME280 temperature, humidity, and atmospheric pressure sensor and the Bosch BMP280 temperature, and atmospheric pressure sensor" repository = "https://github.com/uber-foo/bme280-rs" license = "MIT OR Apache-2.0" keywords = ["bme280", "bmp280", "temperature", "pressure", "humidity"] categories = ["embedded", "no-std", "hardware-support"] +edition = "2018" [dependencies] -embedded-hal = "0.2.1" +embedded-hal = "0.2.3" +serde = { version = "1.0.104", optional = true, features = ["derive"] } [dev-dependencies] -linux-embedded-hal = "0.2.0" +linux-embedded-hal = "0.3.0" diff --git a/README.md b/README.md index 266f2e6..179c6be 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,49 @@ A rust device driver for the Bosch BME280 temperature, humidity, and atmospheric pressure sensor and the Bosch BMP280 temperature and atmospheric pressure sensor. +## Usage + +[View Complete Documentation Here](https://docs.rs/bme280) + +```rust +use linux_embedded_hal as hal; + +use linux_embedded_hal::{Delay, I2cdev}; +use bme280::BME280; + +// using Linux I2C Bus #1 in this example +let i2c_bus = I2cdev::new("/dev/i2c-1").unwrap(); + +// initialize the BME280 using the primary I2C address 0x76 +let mut bme280 = BME280::new_primary(i2c_bus, Delay); + +// or, initialize the BME280 using the secondary I2C address 0x77 +// let mut bme280 = BME280::new_secondary(i2c_bus, Delay); + +// or, initialize the BME280 using a custom I2C address +// let bme280_i2c_addr = 0x88; +// let mut bme280 = BME280::new(i2c_bus, bme280_i2c_addr, Delay); + +// initialize the sensor +bme280.init().unwrap(); + +// measure temperature, pressure, and humidity +let measurements = bme280.measure().unwrap(); + +println!("Relative Humidity = {}%", measurements.humidity); +println!("Temperature = {} deg C", measurements.temperature); +println!("Pressure = {} pascals", measurements.pressure); +``` + +## Serde Support + +To enable optional serde serialization support for the [measurements struct](https://docs.rs/bme280/0.1.2/bme280/struct.Measurements.html), simply enable the `serde` feature, like so in `Cargo.toml`: + +```toml +[dependencies] +bme280 = { version = "0.2", features = ["serde"] } +``` + ## License Licensed under either of: diff --git a/src/lib.rs b/src/lib.rs index 18d61d0..945ac9a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,10 +35,7 @@ //! ## Usage //! //! ```no_run -//! extern crate linux_embedded_hal as hal; -//! extern crate bme280; -//! -//! use hal::{Delay, I2cdev}; +//! use linux_embedded_hal::{Delay, I2cdev}; //! use bme280::BME280; //! //! // using Linux I2C Bus #1 in this example @@ -65,12 +62,13 @@ //! println!("Pressure = {} pascals", measurements.pressure); //! ``` -extern crate embedded_hal; - use core::marker::PhantomData; use embedded_hal::blocking::delay::DelayMs; use embedded_hal::blocking::i2c::{Read, Write, WriteRead}; +#[cfg(feature = "serde")] +use serde::Serialize; + const BME280_I2C_ADDR_PRIMARY: u8 = 0x76; const BME280_I2C_ADDR_SECONDARY: u8 = 0x77; @@ -188,6 +186,7 @@ struct CalibrationData { } /// Measurement data +#[cfg_attr(feature = "serde", derive(Serialize))] #[derive(Debug)] pub struct Measurements { /// temperature in degrees celsius