Skip to content

Commit

Permalink
Merge pull request #136 from reitermarkus/embedded-hal-1
Browse files Browse the repository at this point in the history
Update to `embedded-hal` 1.0.
  • Loading branch information
golemparts committed Jan 12, 2024
2 parents 7625e09 + dfb095f commit 82cbe06
Show file tree
Hide file tree
Showing 17 changed files with 278 additions and 226 deletions.
16 changes: 8 additions & 8 deletions Cargo.toml
Expand Up @@ -15,10 +15,10 @@ keywords = ["raspberry", "pi", "embedded-hal", "embedded-hal-impl", "hal"]

[dependencies]
libc = "0.2"
nb = { version = "0.1.1", optional = true }
nb = { version = "0.1.3", optional = true }
embedded-hal-0 = { version = "0.2.7", optional = true, package = "embedded-hal" }
embedded-hal = { version = "=1.0.0-rc.2", optional = true }
embedded-hal-nb = { version = "=1.0.0-rc.2", optional = true }
embedded-hal = { version = "1", optional = true }
embedded-hal-nb = { version = "1", optional = true }
void = { version = "1.0.2", optional = true }
spin_sleep = { version = "1.0.0", optional = true }

Expand All @@ -27,12 +27,12 @@ simple-signal = "1.1.1"

[features]
default = []
embedded-hal-0 = ["dep:embedded-hal-0", "dep:void", "embedded-hal-nb", "nb"]
embedded-hal = ["dep:embedded-hal", "dep:spin_sleep"]
embedded-hal-nb = ["dep:embedded-hal-nb", "embedded-hal"]
hal = [
"nb",
"embedded-hal-0",
"embedded-hal",
"embedded-hal-nb",
"embedded-hal-0",
"spin_sleep",
"void",
]
hal-unproven = ["nb", "embedded-hal-0/unproven", "hal"]
hal-unproven = ["embedded-hal-0/unproven", "hal"]
6 changes: 5 additions & 1 deletion src/gpio.rs
Expand Up @@ -125,7 +125,11 @@ use std::time::Duration;

mod epoll;
mod gpiomem;
#[cfg(feature = "hal")]
#[cfg(any(
feature = "embedded-hal-0",
feature = "embedded-hal",
feature = "embedded-hal-nb"
))]
mod hal;
#[cfg(feature = "hal-unproven")]
mod hal_unproven;
Expand Down
2 changes: 1 addition & 1 deletion src/gpio/epoll.rs
Expand Up @@ -9,7 +9,7 @@ use libc::{
self, c_int, c_void, EFD_NONBLOCK, EFD_SEMAPHORE, EPOLL_CTL_ADD, EPOLL_CTL_DEL, EPOLL_CTL_MOD,
};

pub use libc::{epoll_event, EPOLLERR, EPOLLET, EPOLLIN, EPOLLONESHOT, EPOLLOUT, EPOLLPRI};
pub use libc::{epoll_event, EPOLLERR, EPOLLET, EPOLLIN, EPOLLPRI};

pub type Result<T> = result::Result<T, io::Error>;

Expand Down
107 changes: 48 additions & 59 deletions src/gpio/hal.rs
@@ -1,78 +1,73 @@
use core::convert::Infallible;

use embedded_hal::digital::{
ErrorType, InputPin as InputPinHal, OutputPin as OutputPinHal,
StatefulOutputPin as StatefulOutputPinHal, ToggleableOutputPin as ToggleableOutputPinHal,
};

use super::{InputPin, IoPin, Level, OutputPin, Pin};

/// `ErrorType` trait implementation for `embedded-hal` v1.0.0.
impl ErrorType for Pin {
#[cfg(feature = "embedded-hal")]
impl embedded_hal::digital::ErrorType for Pin {
type Error = Infallible;
}

/// `InputPin` trait implementation for `embedded-hal` v1.0.0.
impl InputPinHal for Pin {
fn is_high(&self) -> Result<bool, Self::Error> {
#[cfg(feature = "embedded-hal")]
impl embedded_hal::digital::InputPin for Pin {
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok(Self::read(self) == Level::High)
}

fn is_low(&self) -> Result<bool, Self::Error> {
fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok(Self::read(self) == Level::Low)
}
}

/// `ErrorType` trait implementation for `embedded-hal` v1.0.0.
impl ErrorType for InputPin {
#[cfg(feature = "embedded-hal")]
impl embedded_hal::digital::ErrorType for InputPin {
type Error = Infallible;
}

/// `InputPin` trait implementation for `embedded-hal` v1.0.0.
impl InputPinHal for InputPin {
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(Self::is_high(self))
#[cfg(feature = "embedded-hal")]
impl embedded_hal::digital::InputPin for InputPin {
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_high())
}

fn is_low(&self) -> Result<bool, Self::Error> {
Ok(Self::is_low(self))
fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_high())
}
}

/// `ErrorType` trait implementation for `embedded-hal` v1.0.0.
impl ErrorType for IoPin {
#[cfg(feature = "embedded-hal")]
impl embedded_hal::digital::ErrorType for IoPin {
type Error = Infallible;
}

/// `InputPin` trait implementation for `embedded-hal` v1.0.0.
impl InputPinHal for IoPin {
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(Self::is_high(self))
#[cfg(feature = "embedded-hal")]
impl embedded_hal::digital::InputPin for IoPin {
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_high())
}

fn is_low(&self) -> Result<bool, Self::Error> {
Ok(Self::is_low(self))
fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_low())
}
}

/// `ErrorType` trait implementation for `embedded-hal` v1.0.0.
impl ErrorType for OutputPin {
#[cfg(feature = "embedded-hal")]
impl embedded_hal::digital::ErrorType for OutputPin {
type Error = Infallible;
}

/// `InputPin` trait implementation for `embedded-hal` v1.0.0.
impl InputPinHal for OutputPin {
fn is_high(&self) -> Result<bool, Self::Error> {
#[cfg(feature = "embedded-hal")]
impl embedded_hal::digital::InputPin for OutputPin {
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok(Self::is_set_high(self))
}

fn is_low(&self) -> Result<bool, Self::Error> {
fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok(Self::is_set_low(self))
}
}

/// `OutputPin` trait implementation for `embedded-hal` v1.0.0.
impl OutputPinHal for OutputPin {
#[cfg(feature = "embedded-hal")]
impl embedded_hal::digital::OutputPin for OutputPin {
fn set_low(&mut self) -> Result<(), Self::Error> {
OutputPin::set_low(self);

Expand All @@ -86,41 +81,38 @@ impl OutputPinHal for OutputPin {
}
}

/// `OutputPin` trait implementation for `embedded-hal` v0.2.7.
#[cfg(feature = "embedded-hal-0")]
impl embedded_hal_0::digital::v2::OutputPin for OutputPin {
type Error = Infallible;

fn set_low(&mut self) -> Result<(), Self::Error> {
OutputPinHal::set_low(self)
embedded_hal::digital::OutputPin::set_low(self)
}

fn set_high(&mut self) -> Result<(), Self::Error> {
OutputPinHal::set_high(self)
embedded_hal::digital::OutputPin::set_high(self)
}
}

/// `StatefulOutputPin` trait implementation for `embedded-hal` v1.0.0.
impl StatefulOutputPinHal for OutputPin {
fn is_set_high(&self) -> Result<bool, Self::Error> {
#[cfg(feature = "embedded-hal")]
impl embedded_hal::digital::StatefulOutputPin for OutputPin {
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
Ok(OutputPin::is_set_high(self))
}

fn is_set_low(&self) -> Result<bool, Self::Error> {
fn is_set_low(&mut self) -> Result<bool, Self::Error> {
Ok(OutputPin::is_set_low(self))
}
}

/// `ToggleableOutputPin` trait implementation for `embedded-hal` v1.0.0.
impl ToggleableOutputPinHal for OutputPin {
fn toggle(&mut self) -> Result<(), Self::Error> {
OutputPin::toggle(self);

Ok(())
}
}

/// `OutputPin` trait implementation for `embedded-hal` v1.0.0.
impl OutputPinHal for IoPin {
#[cfg(feature = "embedded-hal")]
impl embedded_hal::digital::OutputPin for IoPin {
fn set_low(&mut self) -> Result<(), Self::Error> {
IoPin::set_low(self);

Expand All @@ -134,40 +126,37 @@ impl OutputPinHal for IoPin {
}
}

/// `OutputPin` trait implementation for `embedded-hal` v0.2.7.
#[cfg(feature = "embedded-hal-0")]
impl embedded_hal_0::digital::v2::OutputPin for IoPin {
type Error = Infallible;

fn set_low(&mut self) -> Result<(), Self::Error> {
OutputPinHal::set_low(self)
embedded_hal::digital::OutputPin::set_low(self)
}

fn set_high(&mut self) -> Result<(), Self::Error> {
OutputPinHal::set_high(self)
embedded_hal::digital::OutputPin::set_high(self)
}
}

/// `StatefulOutputPin` trait implementation for `embedded-hal` v1.0.0.
impl StatefulOutputPinHal for IoPin {
fn is_set_high(&self) -> Result<bool, Self::Error> {
#[cfg(feature = "embedded-hal")]
impl embedded_hal::digital::StatefulOutputPin for IoPin {
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
Ok(IoPin::is_high(self))
}

fn is_set_low(&self) -> Result<bool, Self::Error> {
fn is_set_low(&mut self) -> Result<bool, Self::Error> {
Ok(IoPin::is_low(self))
}
}

/// `ToggleableOutputPin` trait implementation for `embedded-hal` v1.0.0.
impl ToggleableOutputPinHal for IoPin {
fn toggle(&mut self) -> Result<(), Self::Error> {
IoPin::toggle(self);

Ok(())
}
}

/// `PwmPin` trait implementation for `embedded-hal` v0.2.7.
#[cfg(feature = "embedded-hal-0")]
impl embedded_hal_0::PwmPin for OutputPin {
type Duty = f64;

Expand Down Expand Up @@ -196,7 +185,7 @@ impl embedded_hal_0::PwmPin for OutputPin {
}
}

/// `PwmPin` trait implementation for `embedded-hal` v0.2.7.
#[cfg(feature = "embedded-hal-0")]
impl embedded_hal_0::PwmPin for IoPin {
type Duty = f64;

Expand Down

0 comments on commit 82cbe06

Please sign in to comment.