Skip to content

Commit

Permalink
Update to embedded-hal 1.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Jan 11, 2024
1 parent b002be9 commit 3feff90
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 169 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ keywords = ["raspberry", "pi", "embedded-hal", "embedded-hal-impl", "hal"]
libc = "0.2"
nb = { version = "0.1.1", 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 Down
91 changes: 32 additions & 59 deletions src/gpio/hal.rs
Original file line number Diff line number Diff line change
@@ -1,78 +1,64 @@
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 {
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> {
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 {
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))
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 {
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))
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 {
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> {
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 {
impl embedded_hal::digital::OutputPin for OutputPin {
fn set_low(&mut self) -> Result<(), Self::Error> {
OutputPin::set_low(self);

Expand All @@ -86,41 +72,35 @@ impl OutputPinHal for OutputPin {
}
}

/// `OutputPin` trait implementation for `embedded-hal` v0.2.7.
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> {
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 {
impl embedded_hal::digital::OutputPin for IoPin {
fn set_low(&mut self) -> Result<(), Self::Error> {
IoPin::set_low(self);

Expand All @@ -134,40 +114,34 @@ impl OutputPinHal for IoPin {
}
}

/// `OutputPin` trait implementation for `embedded-hal` v0.2.7.
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> {
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.
impl embedded_hal_0::PwmPin for OutputPin {
type Duty = f64;

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

/// `PwmPin` trait implementation for `embedded-hal` v0.2.7.
impl embedded_hal_0::PwmPin for IoPin {
type Duty = f64;

Expand Down
47 changes: 15 additions & 32 deletions src/gpio/hal_unproven.rs
Original file line number Diff line number Diff line change
@@ -1,109 +1,94 @@
use core::convert::Infallible;
use std::time::Duration;

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

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

const NANOS_PER_SEC: f64 = 1_000_000_000.0;

/// Unproven `InputPin` trait implementation for `embedded-hal` v0.2.7.
impl embedded_hal_0::digital::v2::InputPin for Pin {
type Error = Infallible;

fn is_high(&self) -> Result<bool, Self::Error> {
InputPinHal::is_high(self)
Ok((*self).read() == Level::High)
}

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

/// Unproven `InputPin` trait implementation for `embedded-hal` v0.2.7.
impl embedded_hal_0::digital::v2::InputPin for InputPin {
type Error = Infallible;

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

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

/// Unproven `InputPin` trait implementation for `embedded-hal` v0.2.7.
impl embedded_hal_0::digital::v2::InputPin for IoPin {
type Error = Infallible;

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

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

/// Unproven `InputPin` trait implementation for `embedded-hal` v0.2.7.
impl embedded_hal_0::digital::v2::InputPin for OutputPin {
type Error = Infallible;

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

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

/// Unproven `StatefulOutputPin` trait implementation for `embedded-hal` v0.2.7.
impl embedded_hal_0::digital::v2::StatefulOutputPin for IoPin {
fn is_set_high(&self) -> Result<bool, Self::Error> {
StatefulOutputPinHal::is_set_high(self)
Ok((*self).is_high())
}

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

/// Unproven `StatefulOutputPin` trait implementation for `embedded-hal` v0.2.7.
impl embedded_hal_0::digital::v2::StatefulOutputPin for OutputPin {
fn is_set_high(&self) -> Result<bool, Self::Error> {
StatefulOutputPinHal::is_set_high(self)
Ok((*self).is_set_high())
}

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

/// Unproven `ToggleableOutputPin` trait implementation for `embedded-hal` v0.2.7.
impl embedded_hal_0::digital::v2::ToggleableOutputPin for IoPin {
type Error = Infallible;

fn toggle(&mut self) -> Result<(), Self::Error> {
ToggleableOutputPinHal::toggle(self)
embedded_hal::digital::StatefulOutputPin::toggle(self)
}
}

/// Unproven `ToggleableOutputPin` trait implementation for `embedded-hal` v0.2.7.
impl embedded_hal_0::digital::v2::ToggleableOutputPin for OutputPin {
type Error = Infallible;

fn toggle(&mut self) -> Result<(), Self::Error> {
ToggleableOutputPinHal::toggle(self)
embedded_hal::digital::StatefulOutputPin::toggle(self)
}
}

/// Unproven `Pwm` trait implementation for `embedded-hal` v0.2.7.
impl embedded_hal_0::Pwm for OutputPin {
type Duty = f64;
type Channel = ();
Expand Down Expand Up @@ -162,7 +147,6 @@ impl embedded_hal_0::Pwm for OutputPin {
}
}

/// Unproven `Pwm` trait implementation for `embedded-hal` v0.2.7.
impl embedded_hal_0::Pwm for IoPin {
type Duty = f64;
type Channel = ();
Expand Down Expand Up @@ -221,7 +205,6 @@ impl embedded_hal_0::Pwm for IoPin {
}
}

/// Unproven `IoPin` trait implementation for `embedded-hal` v0.2.7.
impl embedded_hal_0::digital::v2::IoPin<IoPin, IoPin> for IoPin {
type Error = Infallible;

Expand Down

0 comments on commit 3feff90

Please sign in to comment.