Skip to content

Commit cb2f66c

Browse files
committed
Migrate to embedded-hal 1.0.0-alpha9
1 parent 1ac9d6f commit cb2f66c

File tree

8 files changed

+73
-75
lines changed

8 files changed

+73
-75
lines changed

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ spi = ["spidev"]
2222
default = [ "gpio_cdev", "gpio_sysfs", "i2c", "spi" ]
2323

2424
[dependencies]
25-
embedded-hal = "=1.0.0-alpha.8"
25+
embedded-hal = "=1.0.0-alpha.9"
26+
embedded-hal-nb = "=1.0.0-alpha.1"
2627
gpio-cdev = { version = "0.5.1", optional = true }
2728
sysfs_gpio = { version = "0.6.1", optional = true }
2829
i2cdev = { version = "0.5.1", optional = true }

examples/transactional-i2c.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use embedded_hal::i2c::blocking::{I2c, Operation as I2cOperation};
1+
use embedded_hal::i2c::{I2c, Operation as I2cOperation};
22
use linux_embedded_hal::I2cdev;
33

44
const ADDR: u8 = 0x12;

src/cdev_pin.rs

+43-45
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,47 @@ impl CdevPin {
3535
}
3636
return flags;
3737
}
38+
39+
/// Set this pin to input mode
40+
pub fn into_input_pin(self) -> Result<CdevPin, gpio_cdev::errors::Error> {
41+
if self.1.direction() == gpio_cdev::LineDirection::In {
42+
return Ok(self);
43+
}
44+
let line = self.0.line().clone();
45+
let input_flags = self.get_input_flags();
46+
let consumer = self.1.consumer().unwrap_or("").to_owned();
47+
48+
// Drop self to free the line before re-requesting it in a new mode.
49+
std::mem::drop(self);
50+
51+
CdevPin::new(line.request(input_flags, 0, &consumer)?)
52+
}
53+
54+
/// Set this pin to output mode
55+
pub fn into_output_pin(
56+
self,
57+
state: embedded_hal::digital::PinState,
58+
) -> Result<CdevPin, gpio_cdev::errors::Error> {
59+
if self.1.direction() == gpio_cdev::LineDirection::Out {
60+
return Ok(self);
61+
}
62+
63+
let line = self.0.line().clone();
64+
let output_flags = self.get_output_flags();
65+
let consumer = self.1.consumer().unwrap_or("").to_owned();
66+
67+
// Drop self to free the line before re-requesting it in a new mode.
68+
std::mem::drop(self);
69+
70+
CdevPin::new(line.request(
71+
output_flags,
72+
state_to_value(
73+
state,
74+
output_flags.intersects(gpio_cdev::LineRequestFlags::ACTIVE_LOW),
75+
),
76+
&consumer,
77+
)?)
78+
}
3879
}
3980

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

60-
impl embedded_hal::digital::blocking::OutputPin for CdevPin {
101+
impl embedded_hal::digital::OutputPin for CdevPin {
61102
fn set_low(&mut self) -> Result<(), Self::Error> {
62103
self.0.set_value(state_to_value(
63104
embedded_hal::digital::PinState::Low,
@@ -73,7 +114,7 @@ impl embedded_hal::digital::blocking::OutputPin for CdevPin {
73114
}
74115
}
75116

76-
impl embedded_hal::digital::blocking::InputPin for CdevPin {
117+
impl embedded_hal::digital::InputPin for CdevPin {
77118
fn is_high(&self) -> Result<bool, Self::Error> {
78119
self.0.get_value().map(|val| {
79120
val == state_to_value(
@@ -88,49 +129,6 @@ impl embedded_hal::digital::blocking::InputPin for CdevPin {
88129
}
89130
}
90131

91-
impl embedded_hal::digital::blocking::IoPin<CdevPin, CdevPin> for CdevPin {
92-
type Error = gpio_cdev::errors::Error;
93-
94-
fn into_input_pin(self) -> Result<CdevPin, Self::Error> {
95-
if self.1.direction() == gpio_cdev::LineDirection::In {
96-
return Ok(self);
97-
}
98-
let line = self.0.line().clone();
99-
let input_flags = self.get_input_flags();
100-
let consumer = self.1.consumer().unwrap_or("").to_owned();
101-
102-
// Drop self to free the line before re-requesting it in a new mode.
103-
std::mem::drop(self);
104-
105-
CdevPin::new(line.request(input_flags, 0, &consumer)?)
106-
}
107-
108-
fn into_output_pin(
109-
self,
110-
state: embedded_hal::digital::PinState,
111-
) -> Result<CdevPin, Self::Error> {
112-
if self.1.direction() == gpio_cdev::LineDirection::Out {
113-
return Ok(self);
114-
}
115-
116-
let line = self.0.line().clone();
117-
let output_flags = self.get_output_flags();
118-
let consumer = self.1.consumer().unwrap_or("").to_owned();
119-
120-
// Drop self to free the line before re-requesting it in a new mode.
121-
std::mem::drop(self);
122-
123-
CdevPin::new(line.request(
124-
output_flags,
125-
state_to_value(
126-
state,
127-
output_flags.intersects(gpio_cdev::LineRequestFlags::ACTIVE_LOW),
128-
),
129-
&consumer,
130-
)?)
131-
}
132-
}
133-
134132
impl core::ops::Deref for CdevPin {
135133
type Target = gpio_cdev::LineHandle;
136134

src/delay.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
use cast::u64;
66
use core::convert::Infallible;
7-
use embedded_hal::delay::blocking::DelayUs;
7+
use embedded_hal::delay::DelayUs;
88
use std::thread;
99
use std::time::Duration;
1010

src/i2c.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl ops::DerefMut for I2cdev {
5757

5858
mod embedded_hal_impl {
5959
use super::*;
60-
use embedded_hal::i2c::blocking::{I2c, Operation as I2cOperation};
60+
use embedded_hal::i2c::{I2c, Operation as I2cOperation};
6161
use embedded_hal::i2c::ErrorType;
6262
use i2cdev::core::{I2CDevice, I2CMessage, I2CTransfer};
6363
use i2cdev::linux::LinuxI2CMessage;

src/serial.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ impl embedded_hal::serial::ErrorType for Serial {
3333
type Error = SerialError;
3434
}
3535

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

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

86-
use embedded_hal::serial::nb::{Read, Write};
87+
use embedded_hal_nb::serial::{Read, Write};
8788
use std::io::{Read as IoRead, Write as IoWrite};
8889

8990
use super::*;

src/spi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl ops::DerefMut for Spidev {
4040

4141
mod embedded_hal_impl {
4242
use super::*;
43-
use embedded_hal::spi::blocking::{SpiBus, SpiBusFlush, SpiBusRead, SpiBusWrite, SpiDevice};
43+
use embedded_hal::spi::{SpiBus, SpiBusFlush, SpiBusRead, SpiBusWrite, SpiDevice};
4444
use embedded_hal::spi::ErrorType;
4545
use spidev::SpidevTransfer;
4646
use std::io::{Read, Write};

src/sysfs_pin.rs

+20-22
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,31 @@ impl SysfsPin {
2626
{
2727
sysfs_gpio::Pin::from_path(path).map(SysfsPin)
2828
}
29+
30+
/// Convert this pin to an input pin
31+
pub fn into_input_pin(self) -> Result<SysfsPin, sysfs_gpio::Error> {
32+
self.set_direction(sysfs_gpio::Direction::In)?;
33+
Ok(self)
34+
}
35+
36+
/// Convert this pin to an output pin
37+
pub fn into_output_pin(
38+
self,
39+
state: embedded_hal::digital::PinState,
40+
) -> Result<SysfsPin, sysfs_gpio::Error> {
41+
self.set_direction(match state {
42+
embedded_hal::digital::PinState::High => sysfs_gpio::Direction::High,
43+
embedded_hal::digital::PinState::Low => sysfs_gpio::Direction::Low,
44+
})?;
45+
Ok(self)
46+
}
2947
}
3048

3149
impl embedded_hal::digital::ErrorType for SysfsPin {
3250
type Error = sysfs_gpio::Error;
3351
}
3452

35-
impl embedded_hal::digital::blocking::OutputPin for SysfsPin {
53+
impl embedded_hal::digital::OutputPin for SysfsPin {
3654
fn set_low(&mut self) -> Result<(), Self::Error> {
3755
if self.0.get_active_low()? {
3856
self.0.set_value(1)
@@ -50,7 +68,7 @@ impl embedded_hal::digital::blocking::OutputPin for SysfsPin {
5068
}
5169
}
5270

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

67-
impl embedded_hal::digital::blocking::IoPin<SysfsPin, SysfsPin> for SysfsPin {
68-
type Error = sysfs_gpio::Error;
69-
70-
fn into_input_pin(self) -> Result<SysfsPin, Self::Error> {
71-
self.set_direction(sysfs_gpio::Direction::In)?;
72-
Ok(self)
73-
}
74-
75-
fn into_output_pin(
76-
self,
77-
state: embedded_hal::digital::PinState,
78-
) -> Result<SysfsPin, Self::Error> {
79-
self.set_direction(match state {
80-
embedded_hal::digital::PinState::High => sysfs_gpio::Direction::High,
81-
embedded_hal::digital::PinState::Low => sysfs_gpio::Direction::Low,
82-
})?;
83-
Ok(self)
84-
}
85-
}
86-
8785
impl core::ops::Deref for SysfsPin {
8886
type Target = sysfs_gpio::Pin;
8987

0 commit comments

Comments
 (0)