Skip to content

Commit 44816d1

Browse files
committed
Adapt to embedded-hal-1.0.0-alpha.1
1 parent 25bf2be commit 44816d1

File tree

6 files changed

+72
-49
lines changed

6 files changed

+72
-49
lines changed

Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ gpio_cdev = ["gpio-cdev"]
1515
default = [ "gpio_cdev", "gpio_sysfs" ]
1616

1717
[dependencies]
18-
embedded-hal = { version = "0.2.3", features = ["unproven"] }
18+
embedded-hal = "=1.0.0-alpha.1"
1919
gpio-cdev = { version = "0.3", optional = true }
2020
sysfs_gpio = { version = "0.5", optional = true }
2121

@@ -24,7 +24,6 @@ nb = "0.1.1"
2424
serial-core = "0.4.0"
2525
serial-unix = "0.4.0"
2626
spidev = "0.4"
27-
void = "1"
2827

2928
[dev-dependencies]
3029
openpty = "0.1.0"

src/cdev_pin.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,31 @@ impl CdevPin {
1515
}
1616
}
1717

18-
impl hal::digital::v2::OutputPin for CdevPin {
18+
impl hal::digital::OutputPin for CdevPin {
1919
type Error = gpio_cdev::errors::Error;
2020

21-
fn set_low(&mut self) -> Result<(), Self::Error> {
21+
fn try_set_low(&mut self) -> Result<(), Self::Error> {
2222
self.0.set_value(0)
2323
}
2424

25-
fn set_high(&mut self) -> Result<(), Self::Error> {
25+
fn try_set_high(&mut self) -> Result<(), Self::Error> {
2626
self.0.set_value(1)
2727
}
2828
}
2929

30-
impl hal::digital::v2::InputPin for CdevPin {
30+
impl hal::digital::InputPin for CdevPin {
3131
type Error = gpio_cdev::errors::Error;
3232

33-
fn is_high(&self) -> Result<bool, Self::Error> {
33+
fn try_is_high(&self) -> Result<bool, Self::Error> {
3434
if !self.1 {
3535
self.0.get_value().map(|val| val != 0)
3636
} else {
3737
self.0.get_value().map(|val| val == 0)
3838
}
3939
}
4040

41-
fn is_low(&self) -> Result<bool, Self::Error> {
42-
self.is_high().map(|val| !val)
41+
fn try_is_low(&self) -> Result<bool, Self::Error> {
42+
self.try_is_high().map(|val| !val)
4343
}
4444
}
4545

src/lib.rs

+49-28
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,18 @@ extern crate cast;
1616
extern crate core;
1717
extern crate embedded_hal as hal;
1818
pub extern crate i2cdev;
19-
pub extern crate spidev;
20-
pub extern crate serial_unix;
21-
pub extern crate serial_core;
2219
pub extern crate nb;
23-
20+
pub extern crate serial_core;
21+
pub extern crate serial_unix;
22+
pub extern crate spidev;
2423

2524
#[cfg(feature = "gpio_sysfs")]
2625
pub extern crate sysfs_gpio;
2726

2827
#[cfg(feature = "gpio_cdev")]
2928
pub extern crate gpio_cdev;
3029

31-
30+
use core::convert::Infallible;
3231
use std::io::{self, Write};
3332
use std::path::{Path, PathBuf};
3433
use std::time::Duration;
@@ -60,65 +59,87 @@ pub use cdev_pin::CdevPin;
6059
/// Sysfs pin re-export
6160
pub use sysfs_pin::SysfsPin;
6261

63-
6462
/// Empty struct that provides delay functionality on top of `thread::sleep`
6563
pub struct Delay;
6664

6765
impl hal::blocking::delay::DelayUs<u8> for Delay {
68-
fn delay_us(&mut self, n: u8) {
69-
thread::sleep(Duration::new(0, u32(n) * 1000))
66+
type Error = Infallible;
67+
68+
fn try_delay_us(&mut self, n: u8) -> Result<(), Self::Error> {
69+
thread::sleep(Duration::new(0, u32(n) * 1000));
70+
Ok(())
7071
}
7172
}
7273

7374
impl hal::blocking::delay::DelayUs<u16> for Delay {
74-
fn delay_us(&mut self, n: u16) {
75-
thread::sleep(Duration::new(0, u32(n) * 1000))
75+
type Error = Infallible;
76+
77+
fn try_delay_us(&mut self, n: u16) -> Result<(), Self::Error> {
78+
thread::sleep(Duration::new(0, u32(n) * 1000));
79+
Ok(())
7680
}
7781
}
7882

7983
impl hal::blocking::delay::DelayUs<u32> for Delay {
80-
fn delay_us(&mut self, n: u32) {
84+
type Error = Infallible;
85+
86+
fn try_delay_us(&mut self, n: u32) -> Result<(), Self::Error> {
8187
let secs = n / 1_000_000;
8288
let nsecs = (n % 1_000_000) * 1_000;
8389

84-
thread::sleep(Duration::new(u64(secs), nsecs))
90+
thread::sleep(Duration::new(u64(secs), nsecs));
91+
Ok(())
8592
}
8693
}
8794

8895
impl hal::blocking::delay::DelayUs<u64> for Delay {
89-
fn delay_us(&mut self, n: u64) {
96+
type Error = Infallible;
97+
98+
fn try_delay_us(&mut self, n: u64) -> Result<(), Self::Error> {
9099
let secs = n / 1_000_000;
91100
let nsecs = ((n % 1_000_000) * 1_000) as u32;
92101

93-
thread::sleep(Duration::new(secs, nsecs))
102+
thread::sleep(Duration::new(secs, nsecs));
103+
Ok(())
94104
}
95105
}
96106

97107
impl hal::blocking::delay::DelayMs<u8> for Delay {
98-
fn delay_ms(&mut self, n: u8) {
99-
thread::sleep(Duration::from_millis(u64(n)))
108+
type Error = Infallible;
109+
110+
fn try_delay_ms(&mut self, n: u8) -> Result<(), Self::Error> {
111+
thread::sleep(Duration::from_millis(u64(n)));
112+
Ok(())
100113
}
101114
}
102115

103116
impl hal::blocking::delay::DelayMs<u16> for Delay {
104-
fn delay_ms(&mut self, n: u16) {
105-
thread::sleep(Duration::from_millis(u64(n)))
117+
type Error = Infallible;
118+
119+
fn try_delay_ms(&mut self, n: u16) -> Result<(), Self::Error> {
120+
thread::sleep(Duration::from_millis(u64(n)));
121+
Ok(())
106122
}
107123
}
108124

109125
impl hal::blocking::delay::DelayMs<u32> for Delay {
110-
fn delay_ms(&mut self, n: u32) {
111-
thread::sleep(Duration::from_millis(u64(n)))
126+
type Error = Infallible;
127+
128+
fn try_delay_ms(&mut self, n: u32) -> Result<(), Self::Error> {
129+
thread::sleep(Duration::from_millis(u64(n)));
130+
Ok(())
112131
}
113132
}
114133

115134
impl hal::blocking::delay::DelayMs<u64> for Delay {
116-
fn delay_ms(&mut self, n: u64) {
117-
thread::sleep(Duration::from_millis(n))
135+
type Error = Infallible;
136+
137+
fn try_delay_ms(&mut self, n: u64) -> Result<(), Self::Error> {
138+
thread::sleep(Duration::from_millis(n));
139+
Ok(())
118140
}
119141
}
120142

121-
122143
/// Newtype around [`i2cdev::linux::LinuxI2CDevice`] that implements the `embedded-hal` traits
123144
///
124145
/// [`i2cdev::linux::LinuxI2CDevice`]: https://docs.rs/i2cdev/0.3.1/i2cdev/linux/struct.LinuxI2CDevice.html
@@ -156,7 +177,7 @@ impl I2cdev {
156177
impl hal::blocking::i2c::Read for I2cdev {
157178
type Error = i2cdev::linux::LinuxI2CError;
158179

159-
fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
180+
fn try_read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
160181
self.set_address(address)?;
161182
self.inner.read(buffer)
162183
}
@@ -165,7 +186,7 @@ impl hal::blocking::i2c::Read for I2cdev {
165186
impl hal::blocking::i2c::Write for I2cdev {
166187
type Error = i2cdev::linux::LinuxI2CError;
167188

168-
fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> {
189+
fn try_write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> {
169190
self.set_address(address)?;
170191
self.inner.write(bytes)
171192
}
@@ -174,7 +195,7 @@ impl hal::blocking::i2c::Write for I2cdev {
174195
impl hal::blocking::i2c::WriteRead for I2cdev {
175196
type Error = i2cdev::linux::LinuxI2CError;
176197

177-
fn write_read(
198+
fn try_write_read(
178199
&mut self,
179200
address: u8,
180201
bytes: &[u8],
@@ -220,7 +241,7 @@ impl Spidev {
220241
impl hal::blocking::spi::Transfer<u8> for Spidev {
221242
type Error = io::Error;
222243

223-
fn transfer<'b>(&mut self, buffer: &'b mut [u8]) -> io::Result<&'b [u8]> {
244+
fn try_transfer<'b>(&mut self, buffer: &'b mut [u8]) -> io::Result<&'b [u8]> {
224245
let tx = buffer.to_owned();
225246
self.0
226247
.transfer(&mut SpidevTransfer::read_write(&tx, buffer))?;
@@ -231,7 +252,7 @@ impl hal::blocking::spi::Transfer<u8> for Spidev {
231252
impl hal::blocking::spi::Write<u8> for Spidev {
232253
type Error = io::Error;
233254

234-
fn write(&mut self, buffer: &[u8]) -> io::Result<()> {
255+
fn try_write(&mut self, buffer: &[u8]) -> io::Result<()> {
235256
self.0.write_all(buffer)
236257
}
237258
}

src/serial.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn translate_io_errors(err: std::io::Error) -> nb::Error<IoErrorKind> {
3333
impl hal::serial::Read<u8> for Serial {
3434
type Error = IoErrorKind;
3535

36-
fn read(&mut self) -> nb::Result<u8, Self::Error> {
36+
fn try_read(&mut self) -> nb::Result<u8, Self::Error> {
3737
let mut buffer = [0; 1];
3838
let bytes_read = self.0.read(&mut buffer).map_err(translate_io_errors)?;
3939
if bytes_read == 1 {
@@ -47,12 +47,12 @@ impl hal::serial::Read<u8> for Serial {
4747
impl hal::serial::Write<u8> for Serial {
4848
type Error = IoErrorKind;
4949

50-
fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> {
50+
fn try_write(&mut self, word: u8) -> nb::Result<(), Self::Error> {
5151
self.0.write(&[word]).map_err(translate_io_errors)?;
5252
Ok(())
5353
}
5454

55-
fn flush(&mut self) -> nb::Result<(), Self::Error> {
55+
fn try_flush(&mut self) -> nb::Result<(), Self::Error> {
5656
self.0.flush().map_err(translate_io_errors)
5757
}
5858
}

src/sysfs_pin.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,31 @@ impl SysfsPin {
2626
}
2727
}
2828

29-
impl hal::digital::v2::OutputPin for SysfsPin {
29+
impl hal::digital::OutputPin for SysfsPin {
3030
type Error = sysfs_gpio::Error;
3131

32-
fn set_low(&mut self) -> Result<(), Self::Error> {
32+
fn try_set_low(&mut self) -> Result<(), Self::Error> {
3333
self.0.set_value(0)
3434
}
3535

36-
fn set_high(&mut self) -> Result<(), Self::Error> {
36+
fn try_set_high(&mut self) -> Result<(), Self::Error> {
3737
self.0.set_value(1)
3838
}
3939
}
4040

41-
impl hal::digital::v2::InputPin for SysfsPin {
41+
impl hal::digital::InputPin for SysfsPin {
4242
type Error = sysfs_gpio::Error;
4343

44-
fn is_high(&self) -> Result<bool, Self::Error> {
44+
fn try_is_high(&self) -> Result<bool, Self::Error> {
4545
if !self.0.get_active_low()? {
4646
self.0.get_value().map(|val| val != 0)
4747
} else {
4848
self.0.get_value().map(|val| val == 0)
4949
}
5050
}
5151

52-
fn is_low(&self) -> Result<bool, Self::Error> {
53-
self.is_high().map(|val| !val)
52+
fn try_is_low(&self) -> Result<bool, Self::Error> {
53+
self.try_is_high().map(|val| !val)
5454
}
5555
}
5656

src/timer.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Timers.
22
3+
use core::convert::Infallible;
34
use std::time::{Duration, Instant};
45

56
use hal::timer::{CountDown, Periodic};
@@ -27,17 +28,19 @@ impl SysTimer {
2728
}
2829

2930
impl CountDown for SysTimer {
31+
type Error = Infallible;
3032
type Time = Duration;
3133

32-
fn start<T>(&mut self, count: T)
34+
fn try_start<T>(&mut self, count: T) -> Result<(), Self::Error>
3335
where
3436
T: Into<Self::Time>,
3537
{
3638
self.start = Instant::now();
3739
self.duration = count.into();
40+
Ok(())
3841
}
3942

40-
fn wait(&mut self) -> nb::Result<(), void::Void> {
43+
fn try_wait(&mut self) -> nb::Result<(), Self::Error> {
4144
if (Instant::now() - self.start) >= self.duration {
4245
// Restart the timer to fulfill the contract by `Periodic`
4346
self.start = Instant::now();

0 commit comments

Comments
 (0)