Skip to content

Commit

Permalink
Merge pull request #75 from tomgilligan/1-alpha
Browse files Browse the repository at this point in the history
Support both embedded-hal 0.x and 1.x in the same codebase.
  • Loading branch information
dbrgn authored Jul 30, 2023
2 parents f952d6f + 1e9848b commit 566239c
Show file tree
Hide file tree
Showing 20 changed files with 2,624 additions and 93 deletions.
19 changes: 8 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ on:
name: CI

jobs:

build:
name: Build and Test
runs-on: ubuntu-latest
Expand All @@ -34,20 +33,18 @@ jobs:
rustc --version
cargo --version
# Check crate
- name: Check
run: cargo check
- name: Check (no default features)
run: cargo check --no-default-features
- name: Check (all features)
run: cargo check --all-features
# Build main crate
- name: Build
run: cargo build
- name: Build (all features)
if: ${{ matrix.toolchain == 'nightly' }}
run: cargo build --all-features

# Test crate
# Test main crate
- name: Test
run: cargo test
- name: Test (no default features)
run: cargo test --no-default-features
- name: Test (all features)
if: ${{ matrix.toolchain == 'nightly' }}
run: cargo test --all-features

# Check code formatting
Expand Down
21 changes: 16 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,23 @@ include = [
edition = "2021"
rust-version = "1.60"

[features]
eh0 = ["dep:eh0", "dep:nb"]
eh1 = ["dep:eh1", "dep:embedded-hal-nb"]

embedded-time = ["dep:embedded-time", "dep:void"]
embedded-hal-async = ["dep:embedded-hal-async"]

default = ["eh0", "embedded-time"]

[dependencies]
embedded-hal = { version = "0.2.7", features = ["unproven"] }
eh0 = { package = "embedded-hal", version = "0.2.7", features = ["unproven"], optional = true }
eh1 = { package = "embedded-hal", version = "=1.0.0-alpha.10", optional = true }
embedded-hal-nb = { version = "=1.0.0-alpha.2", optional = true }
embedded-hal-async = { version = "=0.2.0-alpha.1", optional = true }
embedded-time = { version = "0.12", optional = true }
nb = { version = "0.1.1", optional = true}
void = { version = "^1.0", optional = true }
nb = "0.1.1"

[features]
default = ["embedded-time"]
embedded-time = ["dep:embedded-time", "dep:void"]
[dev-dependencies]
tokio = { version = "1.21.1", features = ["rt", "macros"] }
48 changes: 25 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
![Minimum Rust Version][min-rust-badge]
[![Crates.io Version][version-badge]][crates-io]

_(Note: This create currently targets the latest stable version of embedded-hal.
If you're looking for a version that's compatible with the 1.0.0 alpha of
embedded-hal, check out the [`1-alpha`
branch](https://github.com/dbrgn/embedded-hal-mock/tree/1-alpha).)_

This is a collection of types that implement the embedded-hal traits.

The implementations never access real hardware. Instead, the hardware is mocked
Expand All @@ -19,6 +14,28 @@ to hardware.

[Docs](https://docs.rs/embedded-hal-mock/)

## Usage

See module-level docs for more information.

## embedded_hal version

This crate supports both version 0.x and version 1.x of embedded-hal. By default only support
for version 0.x is enabled. To enable support for version 1.x, use the `eh1` feature.

## Cargo Features

There are currently the following cargo features:

- `eh0`: Provide module [`eh0`] that mocks embedded-hal version 0.x (enabled by default)
- `eh1`: Provide module [`eh1`] that mocks embedded-hal version 1.x
- `embedded-time`: Enable the [`eh0::timer`] module (enabled by default)
- `embedded-hal-async`: Provide mocks for embedded-hal-async in [`eh1`]

## no\_std

Currently this crate is not `no_std`. If you think this is important, let
me know.

## Status

Expand All @@ -35,36 +52,22 @@ to hardware.

Pull requests for more mock implementations are welcome! :)


## no\_std

Currently this crate is not `no_std`. If you think this is important, let me
know.


## Usage

See [docs](https://docs.rs/embedded-hal-mock/).


## Minimum Supported Rust Version (MSRV)

This crate is guaranteed to compile on stable Rust 1.60 and up. It *might*
compile with older versions but that may change in any new patch release.


## Development Version of `embedded-hal`

If you would like to use the current development version of `embedded-hal` (or any other version),
so long as they are API compatible you can use a patch field in your `Cargo.toml` file to override
If you would like to use the current development version of `embedded-hal` (or any other version),
so long as they are API compatible you can use a patch field in your `Cargo.toml` file to override
the dependency version.

```yaml
[patch.crates-io]
embedded-hal = { git = "https://github.com/rust-embedded/embedded-hal" }
eh0 = { git = "https://github.com/rust-embedded/embedded-hal" }
```


## License

Licensed under either of
Expand All @@ -80,7 +83,6 @@ Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you, as defined in the Apache-2.0 license, shall
be dual licensed as above, without any additional terms or conditions.


<!-- Badges -->
[github-actions]: https://github.com/dbrgn/embedded-hal-mock/actions/workflows/ci.yml
[github-actions-badge]: https://github.com/dbrgn/embedded-hal-mock/actions/workflows/ci.yml/badge.svg
Expand Down
17 changes: 17 additions & 0 deletions src/eh0.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//! This is a collection of types that implement the embedded-hal version 0.x traits.
//!
//! ## Usage
//!
//! See module-level docs for more information.

mod error;
pub use error::MockError;

pub mod adc;
pub mod delay;
pub mod i2c;
pub mod pin;
pub mod serial;
pub mod spi;
#[cfg(feature = "embedded-time")]
pub mod timer;
25 changes: 15 additions & 10 deletions src/adc.rs → src/eh0/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
//! ## Usage
//!
//! ```
//! # use eh0 as embedded_hal;
//! use embedded_hal::adc::OneShot;
//! use embedded_hal_mock::adc::Mock;
//! use embedded_hal_mock::adc::Transaction;
//! use embedded_hal_mock::adc::{MockChan0, MockChan1};
//! use embedded_hal_mock::eh0::adc::Mock;
//! use embedded_hal_mock::eh0::adc::Transaction;
//! use embedded_hal_mock::eh0::adc::{MockChan0, MockChan1};
//!
//! // Configure expectations: expected input channel numbers and values returned by read operations
//! let expectations = [
Expand All @@ -28,11 +29,12 @@
//! Attach an error to test error handling. An error is returned when such a transaction is executed.
//!
//! ```
//! # use eh0 as embedded_hal;
//! use embedded_hal::adc::OneShot;
//! use embedded_hal_mock::adc::Mock;
//! use embedded_hal_mock::adc::Transaction;
//! use embedded_hal_mock::adc::MockChan1;
//! use embedded_hal_mock::MockError;
//! use embedded_hal_mock::eh0::adc::Mock;
//! use embedded_hal_mock::eh0::adc::Transaction;
//! use embedded_hal_mock::eh0::adc::MockChan1;
//! use embedded_hal_mock::eh0::MockError;
//! use std::io::ErrorKind;
//!
//!
Expand All @@ -49,13 +51,14 @@
//! adc.done();
//! ```

use eh0 as embedded_hal;
use embedded_hal::adc::Channel;
use embedded_hal::adc::OneShot;
use nb;
use std::fmt::Debug;

use super::error::MockError;
use crate::common::Generic;
use crate::error::MockError;

/// ADC transaction type
///
Expand Down Expand Up @@ -138,10 +141,12 @@ where

#[cfg(test)]
mod test {
use super::super::error::MockError;
use super::*;

use eh0 as embedded_hal;
use embedded_hal::adc::OneShot;

use crate::adc::{Mock, MockChan0, MockChan1, MockChan2, Transaction};
use crate::MockError;
use std::io::ErrorKind;

#[test]
Expand Down
1 change: 1 addition & 0 deletions src/delay.rs → src/eh0/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use std::thread;
use std::time::Duration;

use eh0 as embedded_hal;
use embedded_hal::blocking::delay;

/// A `Delay` implementation that does not actually block.
Expand Down
File renamed without changes.
18 changes: 9 additions & 9 deletions src/i2c.rs → src/eh0/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
//! ## Usage
//!
//! ```
//! extern crate embedded_hal;
//! extern crate embedded_hal_mock;
//!
//! # use eh0 as embedded_hal;
//! use embedded_hal::prelude::*;
//! use embedded_hal::blocking::i2c::{Read, Write, WriteRead};
//! use embedded_hal_mock::i2c::{Mock as I2cMock, Transaction as I2cTransaction};
//! use embedded_hal_mock::eh0::i2c::{Mock as I2cMock, Transaction as I2cTransaction};
//!
//! // Configure expectations
//! let expectations = [
Expand Down Expand Up @@ -44,13 +42,12 @@
//! a transaction. When the transaction is executed, an error is returned.
//!
//! ```
//! # extern crate embedded_hal;
//! # extern crate embedded_hal_mock;
//! # use eh0 as embedded_hal;
//! # use embedded_hal::prelude::*;
//! # use embedded_hal::blocking::i2c::{Read, Write, WriteRead};
//! # use embedded_hal_mock::i2c::{Mock as I2cMock, Transaction as I2cTransaction};
//! # use embedded_hal_mock::eh0::i2c::{Mock as I2cMock, Transaction as I2cTransaction};
//! use std::io::ErrorKind;
//! use embedded_hal_mock::MockError;
//! use embedded_hal_mock::eh0::MockError;
//!
//! // Configure expectations
//! let expectations = [
Expand All @@ -71,10 +68,11 @@
//! i2c.done();
//! ```

use eh0 as embedded_hal;
use embedded_hal::blocking::i2c;

use super::error::MockError;
use crate::common::Generic;
use crate::error::MockError;

/// I2C Transaction modes
#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -279,10 +277,12 @@ impl i2c::WriteIter for Mock {

#[cfg(test)]
mod test {
use super::super::error::MockError;
use super::*;

use std::{io::ErrorKind as IoErrorKind, time::SystemTime};

use eh0 as embedded_hal;
use embedded_hal::blocking::i2c::{Read, Write, WriteRead};

#[test]
Expand Down
17 changes: 10 additions & 7 deletions src/pin.rs → src/eh0/pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
//! [`OutputPin`]: https://docs.rs/embedded-hal/0.2/embedded_hal/digital/v2/trait.OutputPin.html
//!
//! ```
//! # use eh0 as embedded_hal;
//! use std::io::ErrorKind;
//!
//! use embedded_hal_mock::MockError;
//! use embedded_hal_mock::pin::{Transaction as PinTransaction, Mock as PinMock, State as PinState};
//! use embedded_hal_mock::eh0::MockError;
//! use embedded_hal_mock::eh0::pin::{Transaction as PinTransaction, Mock as PinMock, State as PinState};
//! use embedded_hal::digital::v2::{InputPin, OutputPin};
//!
//! let err = MockError::Io(ErrorKind::NotConnected);
Expand Down Expand Up @@ -39,9 +40,10 @@
//!
//! ```

use super::error::MockError;
use crate::common::Generic;
use crate::error::MockError;

use eh0 as embedded_hal;
use embedded_hal::digital::v2::{InputPin, OutputPin};
use embedded_hal::PwmPin;

Expand Down Expand Up @@ -298,15 +300,16 @@ impl PwmPin for Mock {

#[cfg(test)]
mod test {
use super::super::error::MockError;
use super::TransactionKind::*;
use super::*;

use std::io::ErrorKind;

use crate::error::MockError;
use eh0 as embedded_hal;
use embedded_hal::digital::v2::{InputPin, OutputPin};
use embedded_hal::PwmPin;

use crate::pin::TransactionKind::{Disable, Enable, Get, GetDuty, GetMaxDuty, Set, SetDuty};
use crate::pin::{Mock, State, Transaction};

#[test]
fn test_input_pin() {
let expectations = [
Expand Down
Loading

0 comments on commit 566239c

Please sign in to comment.