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 7625e09 commit 756a681
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 67 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
67 changes: 28 additions & 39 deletions src/gpio/hal.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
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 {
/// `ErrorType` trait implementation for `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 {
/// `InputPin` trait implementation for `embedded-hal`.
impl embedded_hal::digital::InputPin for Pin {
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(Self::read(self) == Level::High)
}
Expand All @@ -23,13 +18,13 @@ impl InputPinHal for Pin {
}
}

/// `ErrorType` trait implementation for `embedded-hal` v1.0.0.
impl ErrorType for InputPin {
/// `ErrorType` trait implementation for `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 {
/// `InputPin` trait implementation for `embedded-hal`.
impl embedded_hal::digital::InputPin for InputPin {
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(Self::is_high(self))
}
Expand All @@ -39,13 +34,13 @@ impl InputPinHal for InputPin {
}
}

/// `ErrorType` trait implementation for `embedded-hal` v1.0.0.
impl ErrorType for IoPin {
/// `ErrorType` trait implementation for `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 {
/// `InputPin` trait implementation for `embedded-hal`.
impl embedded_hal::digital::InputPin for IoPin {
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(Self::is_high(self))
}
Expand All @@ -55,13 +50,13 @@ impl InputPinHal for IoPin {
}
}

/// `ErrorType` trait implementation for `embedded-hal` v1.0.0.
impl ErrorType for OutputPin {
/// `ErrorType` trait implementation for `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 {
/// `InputPin` trait implementation for `embedded-hal`.
impl embedded_hal::digital::InputPin for OutputPin {
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(Self::is_set_high(self))
}
Expand All @@ -71,8 +66,8 @@ impl InputPinHal for OutputPin {
}
}

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

Expand All @@ -91,36 +86,33 @@ impl embedded_hal_0::digital::v2::OutputPin for OutputPin {
type Error = Infallible;

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

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

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

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

Expand All @@ -139,27 +131,24 @@ impl embedded_hal_0::digital::v2::OutputPin for IoPin {
type Error = Infallible;

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

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

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

fn is_set_low(&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);

Expand Down
33 changes: 14 additions & 19 deletions src/gpio/hal_unproven.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
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;

Expand All @@ -16,11 +11,11 @@ impl embedded_hal_0::digital::v2::InputPin for Pin {
type Error = Infallible;

fn is_high(&self) -> Result<bool, Self::Error> {
InputPinHal::is_high(self)
embedded_hal::digital::InputPin::is_high(self)
}

fn is_low(&self) -> Result<bool, Self::Error> {
InputPinHal::is_low(self)
embedded_hal::digital::InputPin::is_low(self)
}
}

Expand All @@ -29,11 +24,11 @@ impl embedded_hal_0::digital::v2::InputPin for InputPin {
type Error = Infallible;

fn is_high(&self) -> Result<bool, Self::Error> {
InputPinHal::is_high(self)
embedded_hal::digital::InputPin::is_high(self)
}

fn is_low(&self) -> Result<bool, Self::Error> {
InputPinHal::is_low(self)
embedded_hal::digital::InputPin::is_low(self)
}
}

Expand All @@ -42,11 +37,11 @@ impl embedded_hal_0::digital::v2::InputPin for IoPin {
type Error = Infallible;

fn is_high(&self) -> Result<bool, Self::Error> {
InputPinHal::is_high(self)
embedded_hal::digital::InputPin::is_high(self)
}

fn is_low(&self) -> Result<bool, Self::Error> {
InputPinHal::is_low(self)
embedded_hal::digital::InputPin::is_low(self)
}
}

Expand All @@ -55,33 +50,33 @@ impl embedded_hal_0::digital::v2::InputPin for OutputPin {
type Error = Infallible;

fn is_high(&self) -> Result<bool, Self::Error> {
InputPinHal::is_high(self)
embedded_hal::digital::InputPin::is_high(self)
}

fn is_low(&self) -> Result<bool, Self::Error> {
InputPinHal::is_low(self)
embedded_hal::digital::InputPin::is_low(self)
}
}

/// 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)
StatefulOutputPin::is_set_high(self)
}

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

/// 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)
embedded_hal::digital::StatefulOutputPin::is_set_high(self)
}

fn is_set_low(&self) -> Result<bool, Self::Error> {
StatefulOutputPinHal::is_set_low(self)
embedded_hal::digital::StatefulOutputPin::is_set_low(self)
}
}

Expand All @@ -90,7 +85,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)
}
}

Expand All @@ -99,7 +94,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)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/hal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use void::Void;
#[derive(Debug, Default)]
pub struct Delay;

/// `Delay` trait implementation for `embedded-hal` v1.0.0.
/// `Delay` trait implementation for `embedded-hal`.
impl Delay {
/// Constructs a new `Delay`.
pub fn new() -> Delay {
Expand Down Expand Up @@ -71,7 +71,7 @@ impl embedded_hal_0::blocking::delay::DelayUs<u16> for Delay {
}
}

/// `DelayNs` trait implementation for `embedded-hal` v1.0.0.
/// `DelayNs` trait implementation for `embedded-hal`.
impl DelayNs for Delay {
fn delay_ns(&mut self, ns: u32) {
sleep(Duration::from_nanos(ns.into()));
Expand Down
2 changes: 1 addition & 1 deletion src/i2c/hal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl i2c::Error for Error {
}
}

/// `I2c` trait implementation for `embedded-hal` v1.0.0.
/// `I2c` trait implementation for `embedded-hal`.
impl I2cHal for I2c {
fn transaction(
&mut self,
Expand Down
4 changes: 2 additions & 2 deletions src/spi/hal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl spi::Error for Error {
}
}

/// `SpiBus<u8>` trait implementation for `embedded-hal` v1.0.0.
/// `SpiBus<u8>` trait implementation for `embedded-hal`.
impl SpiBus<u8> for Spi {
fn read(&mut self, words: &mut [u8]) -> Result<(), Self::Error> {
Spi::read(self, words)?;
Expand Down Expand Up @@ -64,7 +64,7 @@ impl embedded_hal_0::blocking::spi::Write<u8> for Spi {
}
}

/// `FullDuplex<u8>` trait implementation for `embedded-hal` v1.0.0
/// `FullDuplex<u8>` trait implementation for `embedded-hal`
impl FullDuplex<u8> for Spi {
fn read(&mut self) -> nb::Result<u8, Self::Error> {
if let Some(last_read) = self.last_read.take() {
Expand Down
4 changes: 2 additions & 2 deletions src/uart/hal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl serial::Error for Error {
}
}

/// `Read<u8>` trait implementation for `embedded-hal` v1.0.0.
/// `Read<u8>` trait implementation for `embedded-hal`.
impl Read<u8> for Uart {
fn read(&mut self) -> nb::Result<u8, Self::Error> {
let mut buffer = [0u8; 1];
Expand All @@ -33,7 +33,7 @@ impl embedded_hal_0::serial::Read<u8> for Uart {
}
}

/// `Write<u8>` trait implementation for `embedded-hal` v1.0.0.
/// `Write<u8>` trait implementation for `embedded-hal`.
impl Write<u8> for Uart {
fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> {
if Uart::write(self, &[word])? == 0 {
Expand Down

0 comments on commit 756a681

Please sign in to comment.