Skip to content

Commit

Permalink
Migrate to embedded-hal 1.0.0-alpha9
Browse files Browse the repository at this point in the history
  • Loading branch information
peckpeck committed Nov 10, 2022
1 parent 1ac9d6f commit cb2f66c
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 75 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ spi = ["spidev"]
default = [ "gpio_cdev", "gpio_sysfs", "i2c", "spi" ]

[dependencies]
embedded-hal = "=1.0.0-alpha.8"
embedded-hal = "=1.0.0-alpha.9"
embedded-hal-nb = "=1.0.0-alpha.1"
gpio-cdev = { version = "0.5.1", optional = true }
sysfs_gpio = { version = "0.6.1", optional = true }
i2cdev = { version = "0.5.1", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion examples/transactional-i2c.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use embedded_hal::i2c::blocking::{I2c, Operation as I2cOperation};
use embedded_hal::i2c::{I2c, Operation as I2cOperation};
use linux_embedded_hal::I2cdev;

const ADDR: u8 = 0x12;
Expand Down
88 changes: 43 additions & 45 deletions src/cdev_pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,47 @@ impl CdevPin {
}
return flags;
}

/// Set this pin to input mode
pub fn into_input_pin(self) -> Result<CdevPin, gpio_cdev::errors::Error> {
if self.1.direction() == gpio_cdev::LineDirection::In {
return Ok(self);
}
let line = self.0.line().clone();
let input_flags = self.get_input_flags();
let consumer = self.1.consumer().unwrap_or("").to_owned();

// Drop self to free the line before re-requesting it in a new mode.
std::mem::drop(self);

CdevPin::new(line.request(input_flags, 0, &consumer)?)
}

/// Set this pin to output mode
pub fn into_output_pin(
self,
state: embedded_hal::digital::PinState,
) -> Result<CdevPin, gpio_cdev::errors::Error> {
if self.1.direction() == gpio_cdev::LineDirection::Out {
return Ok(self);
}

let line = self.0.line().clone();
let output_flags = self.get_output_flags();
let consumer = self.1.consumer().unwrap_or("").to_owned();

// Drop self to free the line before re-requesting it in a new mode.
std::mem::drop(self);

CdevPin::new(line.request(
output_flags,
state_to_value(
state,
output_flags.intersects(gpio_cdev::LineRequestFlags::ACTIVE_LOW),
),
&consumer,
)?)
}
}

/// Converts a pin state to the gpio_cdev compatible numeric value, accounting
Expand All @@ -57,7 +98,7 @@ impl embedded_hal::digital::ErrorType for CdevPin {
type Error = gpio_cdev::errors::Error;
}

impl embedded_hal::digital::blocking::OutputPin for CdevPin {
impl embedded_hal::digital::OutputPin for CdevPin {
fn set_low(&mut self) -> Result<(), Self::Error> {
self.0.set_value(state_to_value(
embedded_hal::digital::PinState::Low,
Expand All @@ -73,7 +114,7 @@ impl embedded_hal::digital::blocking::OutputPin for CdevPin {
}
}

impl embedded_hal::digital::blocking::InputPin for CdevPin {
impl embedded_hal::digital::InputPin for CdevPin {
fn is_high(&self) -> Result<bool, Self::Error> {
self.0.get_value().map(|val| {
val == state_to_value(
Expand All @@ -88,49 +129,6 @@ impl embedded_hal::digital::blocking::InputPin for CdevPin {
}
}

impl embedded_hal::digital::blocking::IoPin<CdevPin, CdevPin> for CdevPin {
type Error = gpio_cdev::errors::Error;

fn into_input_pin(self) -> Result<CdevPin, Self::Error> {
if self.1.direction() == gpio_cdev::LineDirection::In {
return Ok(self);
}
let line = self.0.line().clone();
let input_flags = self.get_input_flags();
let consumer = self.1.consumer().unwrap_or("").to_owned();

// Drop self to free the line before re-requesting it in a new mode.
std::mem::drop(self);

CdevPin::new(line.request(input_flags, 0, &consumer)?)
}

fn into_output_pin(
self,
state: embedded_hal::digital::PinState,
) -> Result<CdevPin, Self::Error> {
if self.1.direction() == gpio_cdev::LineDirection::Out {
return Ok(self);
}

let line = self.0.line().clone();
let output_flags = self.get_output_flags();
let consumer = self.1.consumer().unwrap_or("").to_owned();

// Drop self to free the line before re-requesting it in a new mode.
std::mem::drop(self);

CdevPin::new(line.request(
output_flags,
state_to_value(
state,
output_flags.intersects(gpio_cdev::LineRequestFlags::ACTIVE_LOW),
),
&consumer,
)?)
}
}

impl core::ops::Deref for CdevPin {
type Target = gpio_cdev::LineHandle;

Expand Down
2 changes: 1 addition & 1 deletion src/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use cast::u64;
use core::convert::Infallible;
use embedded_hal::delay::blocking::DelayUs;
use embedded_hal::delay::DelayUs;
use std::thread;
use std::time::Duration;

Expand Down
2 changes: 1 addition & 1 deletion src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl ops::DerefMut for I2cdev {

mod embedded_hal_impl {
use super::*;
use embedded_hal::i2c::blocking::{I2c, Operation as I2cOperation};
use embedded_hal::i2c::{I2c, Operation as I2cOperation};
use embedded_hal::i2c::ErrorType;
use i2cdev::core::{I2CDevice, I2CMessage, I2CTransfer};
use i2cdev::linux::LinuxI2CMessage;
Expand Down
7 changes: 4 additions & 3 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ impl embedded_hal::serial::ErrorType for Serial {
type Error = SerialError;
}

impl embedded_hal::serial::nb::Read<u8> for Serial {

impl embedded_hal_nb::serial::Read<u8> for Serial {
fn read(&mut self) -> nb::Result<u8, Self::Error> {
let mut buffer = [0; 1];
let bytes_read = self.0.read(&mut buffer).map_err(translate_io_errors)?;
Expand All @@ -45,7 +46,7 @@ impl embedded_hal::serial::nb::Read<u8> for Serial {
}
}

impl embedded_hal::serial::nb::Write<u8> for Serial {
impl embedded_hal_nb::serial::Write<u8> for Serial {
fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> {
self.0.write(&[word]).map_err(translate_io_errors)?;
Ok(())
Expand Down Expand Up @@ -83,7 +84,7 @@ impl embedded_hal::serial::Error for SerialError {
mod test {
use std::path::Path;

use embedded_hal::serial::nb::{Read, Write};
use embedded_hal_nb::serial::{Read, Write};
use std::io::{Read as IoRead, Write as IoWrite};

use super::*;
Expand Down
2 changes: 1 addition & 1 deletion src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl ops::DerefMut for Spidev {

mod embedded_hal_impl {
use super::*;
use embedded_hal::spi::blocking::{SpiBus, SpiBusFlush, SpiBusRead, SpiBusWrite, SpiDevice};
use embedded_hal::spi::{SpiBus, SpiBusFlush, SpiBusRead, SpiBusWrite, SpiDevice};
use embedded_hal::spi::ErrorType;
use spidev::SpidevTransfer;
use std::io::{Read, Write};
Expand Down
42 changes: 20 additions & 22 deletions src/sysfs_pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,31 @@ impl SysfsPin {
{
sysfs_gpio::Pin::from_path(path).map(SysfsPin)
}

/// Convert this pin to an input pin
pub fn into_input_pin(self) -> Result<SysfsPin, sysfs_gpio::Error> {
self.set_direction(sysfs_gpio::Direction::In)?;
Ok(self)
}

/// Convert this pin to an output pin
pub fn into_output_pin(
self,
state: embedded_hal::digital::PinState,
) -> Result<SysfsPin, sysfs_gpio::Error> {
self.set_direction(match state {
embedded_hal::digital::PinState::High => sysfs_gpio::Direction::High,
embedded_hal::digital::PinState::Low => sysfs_gpio::Direction::Low,
})?;
Ok(self)
}
}

impl embedded_hal::digital::ErrorType for SysfsPin {
type Error = sysfs_gpio::Error;
}

impl embedded_hal::digital::blocking::OutputPin for SysfsPin {
impl embedded_hal::digital::OutputPin for SysfsPin {
fn set_low(&mut self) -> Result<(), Self::Error> {
if self.0.get_active_low()? {
self.0.set_value(1)
Expand All @@ -50,7 +68,7 @@ impl embedded_hal::digital::blocking::OutputPin for SysfsPin {
}
}

impl embedded_hal::digital::blocking::InputPin for SysfsPin {
impl embedded_hal::digital::InputPin for SysfsPin {
fn is_high(&self) -> Result<bool, Self::Error> {
if !self.0.get_active_low()? {
self.0.get_value().map(|val| val != 0)
Expand All @@ -64,26 +82,6 @@ impl embedded_hal::digital::blocking::InputPin for SysfsPin {
}
}

impl embedded_hal::digital::blocking::IoPin<SysfsPin, SysfsPin> for SysfsPin {
type Error = sysfs_gpio::Error;

fn into_input_pin(self) -> Result<SysfsPin, Self::Error> {
self.set_direction(sysfs_gpio::Direction::In)?;
Ok(self)
}

fn into_output_pin(
self,
state: embedded_hal::digital::PinState,
) -> Result<SysfsPin, Self::Error> {
self.set_direction(match state {
embedded_hal::digital::PinState::High => sysfs_gpio::Direction::High,
embedded_hal::digital::PinState::Low => sysfs_gpio::Direction::Low,
})?;
Ok(self)
}
}

impl core::ops::Deref for SysfsPin {
type Target = sysfs_gpio::Pin;

Expand Down

0 comments on commit cb2f66c

Please sign in to comment.