Skip to content

Commit

Permalink
Allow tests to run on non-linux platforms
Browse files Browse the repository at this point in the history
- Make linux-specific doc-tests ignored on non-linux platforms
- Make linux.rs example compile on non-linux platforms
  • Loading branch information
DusterTheFirst committed Dec 19, 2021
1 parent 65268cb commit 6697faf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ embedded-hal = "0.2.5"
nb = "1"

[dev-dependencies]
linux-embedded-hal = "0.3"
embedded-hal-mock = "0.8"

[target.'cfg(target_os = "linux")'.dev-dependencies]
linux-embedded-hal = "0.3"

[profile.release]
lto = true
13 changes: 11 additions & 2 deletions examples/linux.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
use linux_embedded_hal::I2cdev;
use lsm303agr::{AccelOutputDataRate, Lsm303agr};
#[cfg(not(target_os = "linux"))]
fn main() {
// This exists to let `cargo test` succeed on non-linux platforms
panic!("This example can only run on linux");
}

#[cfg(target_os = "linux")]
fn main() {
use linux_embedded_hal::I2cdev;
use lsm303agr::{AccelOutputDataRate, Lsm303agr};

let dev = I2cdev::new("/dev/i2c-1").unwrap();
let mut sensor = Lsm303agr::new_with_i2c(dev);

sensor.init().unwrap();
sensor.set_accel_odr(AccelOutputDataRate::Hz50).unwrap();

loop {
if sensor.accel_status().unwrap().xyz_new_data {
let data = sensor.accel_data().unwrap();
Expand Down
18 changes: 16 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,21 @@
//!
//! ### Connect through I2C, initialize and take some measurements
//!
//! ```no_run
#![cfg_attr(
not(target_os = "linux"),
doc = "**note**: the following example will only compile on linux"
)]
#![cfg_attr(not(target_os = "linux"), doc = "```ignore")]
#![cfg_attr(target_os = "linux", doc = "```no_run")]
//! use linux_embedded_hal::I2cdev;
//! use lsm303agr::{AccelOutputDataRate, Lsm303agr};
//!
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
//! let mut sensor = Lsm303agr::new_with_i2c(dev);
//!
//! sensor.init().unwrap();
//! sensor.set_accel_odr(AccelOutputDataRate::Hz10).unwrap();
//!
//! loop {
//! if sensor.accel_status().unwrap().xyz_new_data {
//! let data = sensor.accel_data().unwrap();
Expand All @@ -88,16 +95,23 @@
//!
//! ### Connect through SPI, initialize and take some measurements
//!
//! ```no_run
#![cfg_attr(
not(target_os = "linux"),
doc = "**note**: the following example will only compile on linux"
)]
#![cfg_attr(not(target_os = "linux"), doc = "```ignore")]
#![cfg_attr(target_os = "linux", doc = "```no_run")]
//! use linux_embedded_hal::{Spidev, Pin};
//! use lsm303agr::{AccelOutputDataRate, Lsm303agr};
//!
//! let dev = Spidev::open("/dev/spidev0.0").unwrap();
//! let accel_cs = Pin::new(17);
//! let mag_cs = Pin::new(27);
//! let mut sensor = Lsm303agr::new_with_spi(dev, accel_cs, mag_cs);
//!
//! sensor.init().unwrap();
//! sensor.set_accel_odr(AccelOutputDataRate::Hz10).unwrap();
//!
//! loop {
//! if sensor.accel_status().unwrap().xyz_new_data {
//! let data = sensor.accel_data().unwrap();
Expand Down

0 comments on commit 6697faf

Please sign in to comment.