Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to embedded-hal 1.0.0 #34

Merged
merged 5 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
rust: [stable, 1.62.0]
rust: [stable, 1.73.0]
features: ["", "--all-features"]
TARGET:
- x86_64-unknown-linux-gnu
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ features = [ "unproven" ]

[dependencies.eh1_0]
package = "embedded-hal"
version = "=1.0.0-rc.1"
version = "1.0.0"


[package.metadata.docs.rs]
# To build locally:
# RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc --all-features --no-deps --open
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
rustdoc-args = ["--cfg", "docsrs"]
14 changes: 9 additions & 5 deletions src/forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ mod digital {
E: core::fmt::Debug,
{
/// Is the input pin high?
fn is_high(&self) -> Result<bool, Self::Error> {
fn is_high(&mut self) -> Result<bool, Self::Error> {
self.inner.is_high().map_err(ForwardError)
}

/// Is the input pin low?
fn is_low(&self) -> Result<bool, Self::Error> {
fn is_low(&mut self) -> Result<bool, Self::Error> {
self.inner.is_low().map_err(ForwardError)
}
}
Expand Down Expand Up @@ -136,12 +136,12 @@ mod digital {
E: core::fmt::Debug,
{
/// Is the input pin high?
fn is_high(&self) -> Result<bool, Self::Error> {
fn is_high(&mut self) -> Result<bool, Self::Error> {
self.inner.is_high().map_err(ForwardError)
}

/// Is the input pin low?
fn is_low(&self) -> Result<bool, Self::Error> {
fn is_low(&mut self) -> Result<bool, Self::Error> {
self.inner.is_low().map_err(ForwardError)
}
}
Expand All @@ -167,10 +167,14 @@ mod digital {
mod delay {
use super::Forward;

impl<T> eh1_0::delay::DelayUs for Forward<T>
impl<T> eh1_0::delay::DelayNs for Forward<T>
where
T: eh0_2::blocking::delay::DelayUs<u32>,
{
fn delay_ns(&mut self, ns: u32) {
self.inner.delay_us(ns.div_ceil(1000))
}

fn delay_us(&mut self, us: u32) {
self.inner.delay_us(us)
}
Expand Down
36 changes: 35 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
//! use with `v0.2.x` consumers, so you can drop these wrapped types into drivers expecting
//! `v0.2.x` types.
//!
//! Note that input and output pins require `.reverse_cell()` as a workaround for mutability changes.
//!
//!```
//! # use core::convert::Infallible;
//! # pub struct OutputPin1_0;
Expand Down Expand Up @@ -94,11 +96,43 @@
//! let _ = eh1_0::digital::OutputPin::set_high(&mut new);
//!
//! // Apply backwards compatibility wrapper
//! let mut old = new.reverse();
//! let mut old = new.reverse_cell();
//! // Access via e-h v0.2.x methods
//! let _ = eh0_2::digital::v2::OutputPin::set_high(&mut old);
//!```
//!
//! ```
//! # use core::convert::Infallible;
//! # pub struct InputPin1_0;
//! #
//! # impl eh1_0::digital::ErrorType for InputPin1_0 {
//! # type Error = Infallible;
//! # }
//! #
//! # impl eh1_0::digital::InputPin for InputPin1_0 {
//! # /// Set the output as high
//! # fn is_high(&mut self) -> Result<bool, Self::Error> {
//! # Ok(true)
//! # }
//! #
//! # /// Set the output as low
//! # fn is_low(&mut self) -> Result<bool, Self::Error> {
//! # Ok(false)
//! # }
//! # }
//! use embedded_hal_compat::ReverseCompat;
//!
//! // Create e-h v1.x.x based type (mock)
//! let mut new = InputPin1_0;
//! // Access via e-h v1.x.x methods
//! let _ = eh1_0::digital::InputPin::is_high(&mut new);
//!
//! // Apply backwards compatibility wrapper
//! let mut old = new.reverse_cell();
//! // Access via e-h v0.2.x methods
//! let _ = eh0_2::digital::v2::InputPin::is_high(&mut old);
//!```
//!
//! ## Optional features
//! ### `alloc`
//! The `alloc` feature enables an implementation of the I2C and SPI `Transactional`
Expand Down
35 changes: 24 additions & 11 deletions src/reverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! A compatibility layer to alleviate (some) of the issues resolving from changes to embedded-hal
// Copyright 2021 Ryan Kurte

use core::cell::RefCell;
use core::fmt::Debug;

/// Reverse compatibility container object.
Expand All @@ -11,10 +12,15 @@ pub struct Reverse<T> {
inner: T,
}

/// Convert a type into a forward compatibility wrapper object
/// Convert a type into a reverse compatibility wrapper object
/// call `.reverse()` on `e-h@1.0.x` types to create an `e-h@0.2.x` compatible wrapper object
pub trait ReverseCompat<T> {
/// Create an e-h-c reverse compatibility wrapper from an existing type
fn reverse(self) -> Reverse<T>;

/// Create an e-h-c reverse compatibility wrapper using a ref-cell for interior mutability
/// (required for InputPin types)
fn reverse_cell(self) -> Reverse<RefCell<T>>;
}

impl<T> ReverseCompat<T> for T {
Expand All @@ -23,6 +29,12 @@ impl<T> ReverseCompat<T> for T {
fn reverse(self) -> Reverse<T> {
Reverse::new(self)
}

/// Create an e-h-c reverse compatibility wrapper using a ref-cell for interior mutability
/// (required for InputPin types)
fn reverse_cell(self) -> Reverse<RefCell<T>> {
Reverse::new(RefCell::new(self))
}
}

impl<T> Reverse<T> {
Expand Down Expand Up @@ -53,8 +65,9 @@ impl<T> Reverse<T> {
// Digital / GPIOs
mod digital {
use super::{Debug, Reverse};
use core::cell::RefCell;

impl<T, E> eh0_2::digital::v2::InputPin for Reverse<T>
impl<T, E> eh0_2::digital::v2::InputPin for Reverse<RefCell<T>>
where
T: eh1_0::digital::InputPin<Error = E>,
E: Debug,
Expand All @@ -63,16 +76,16 @@ mod digital {

/// Is the input pin high?
fn is_high(&self) -> Result<bool, Self::Error> {
self.inner.is_high()
self.inner.borrow_mut().is_high()
}

/// Is the input pin low?
fn is_low(&self) -> Result<bool, Self::Error> {
self.inner.is_low()
self.inner.borrow_mut().is_low()
}
}

impl<T, E> eh0_2::digital::v2::OutputPin for Reverse<T>
impl<T, E> eh0_2::digital::v2::OutputPin for Reverse<RefCell<T>>
where
T: eh1_0::digital::OutputPin<Error = E>,
E: Debug,
Expand All @@ -81,12 +94,12 @@ mod digital {

/// Set the output as high
fn set_high(&mut self) -> Result<(), Self::Error> {
self.inner.set_high()
self.inner.borrow_mut().set_high()
}

/// Set the output as low
fn set_low(&mut self) -> Result<(), Self::Error> {
self.inner.set_low()
self.inner.borrow_mut().set_low()
}
}
}
Expand All @@ -97,7 +110,7 @@ mod delay {

impl<T> eh0_2::blocking::delay::DelayMs<u32> for Reverse<T>
where
T: eh1_0::delay::DelayUs,
T: eh1_0::delay::DelayNs,
{
fn delay_ms(&mut self, ms: u32) {
self.inner.delay_us(ms * 1000)
Expand All @@ -106,7 +119,7 @@ mod delay {

impl<T> eh0_2::blocking::delay::DelayMs<u16> for Reverse<T>
where
T: eh1_0::delay::DelayUs,
T: eh1_0::delay::DelayNs,
{
fn delay_ms(&mut self, ms: u16) {
self.inner.delay_us(ms as u32 * 1000)
Expand All @@ -115,7 +128,7 @@ mod delay {

impl<T> eh0_2::blocking::delay::DelayUs<u32> for Reverse<T>
where
T: eh1_0::delay::DelayUs,
T: eh1_0::delay::DelayNs,
{
fn delay_us(&mut self, us: u32) {
self.inner.delay_us(us)
Expand All @@ -124,7 +137,7 @@ mod delay {

impl<T> eh0_2::blocking::delay::DelayUs<u16> for Reverse<T>
where
T: eh1_0::delay::DelayUs,
T: eh1_0::delay::DelayNs,
{
fn delay_us(&mut self, us: u16) {
self.inner.delay_us(us as u32)
Expand Down
4 changes: 2 additions & 2 deletions tests/delay_forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ impl eh0_2::blocking::delay::DelayUs<u32> for Peripheral {
fn can_forward() {
let periph_0_2 = Peripheral;
let mut periph_1_0 = periph_0_2.forward();
eh1_0::delay::DelayUs::delay_ms(&mut periph_1_0, 0);
eh1_0::delay::DelayUs::delay_ms(&mut periph_1_0, 0);
eh1_0::delay::DelayNs::delay_ms(&mut periph_1_0, 0);
eh1_0::delay::DelayNs::delay_ms(&mut periph_1_0, 0);
}
5 changes: 2 additions & 3 deletions tests/delay_reverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use embedded_hal_compat::ReverseCompat;

struct Peripheral;

impl eh1_0::delay::DelayUs for Peripheral {
fn delay_us(&mut self, _us: u32) {}
fn delay_ms(&mut self, _ms: u32) {}
impl eh1_0::delay::DelayNs for Peripheral {
fn delay_ns(&mut self, _ns: u32) {}
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions tests/digital_forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ fn io_pin_forward() {
let periph_0_2 = IoPin;
let mut periph_1_0: Forward<_, ForwardIoPin> = periph_0_2.forward();
assert!(eh1_0::digital::OutputPin::set_high(&mut periph_1_0).is_ok());
assert!(eh1_0::digital::InputPin::is_high(&periph_1_0).unwrap());
assert!(eh1_0::digital::InputPin::is_high(&mut periph_1_0).unwrap());
}

#[test]
fn input_pin_forward() {
let periph_0_2 = InputPin;
let periph_1_0 = periph_0_2.forward();
assert!(eh1_0::digital::InputPin::is_high(&periph_1_0).unwrap());
let mut periph_1_0 = periph_0_2.forward();
assert!(eh1_0::digital::InputPin::is_high(&mut periph_1_0).unwrap());
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions tests/digital_reverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ impl eh1_0::digital::OutputPin for Peripheral {
}

impl eh1_0::digital::InputPin for Peripheral {
fn is_high(&self) -> Result<bool, Self::Error> {
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok(true)
}
fn is_low(&self) -> Result<bool, Self::Error> {
fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok(false)
}
}

#[test]
fn can_reverse() {
let periph_1_0 = Peripheral;
let mut periph_0_2 = periph_1_0.reverse();
let mut periph_0_2 = periph_1_0.reverse_cell();
assert!(eh0_2::digital::v2::OutputPin::set_high(&mut periph_0_2).is_ok());
assert!(eh0_2::digital::v2::InputPin::is_high(&periph_0_2).unwrap());
}