Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgan Roff authored and MorganR committed Oct 2, 2021
2 parents 3e76395 + ee0e6af commit b78acf4
Show file tree
Hide file tree
Showing 14 changed files with 421 additions and 339 deletions.
2 changes: 1 addition & 1 deletion .github/bors.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ required_approvals = 1
status = [
"CI (stable, x86_64-unknown-linux-gnu)",
"CI (stable, armv7-unknown-linux-gnueabihf)",
"CI (1.36.0, x86_64-unknown-linux-gnu)",
"CI (1.46.0, x86_64-unknown-linux-gnu)",
]
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:

include:
# Test MSRV
- rust: 1.36.0
- rust: 1.46.0
TARGET: x86_64-unknown-linux-gnu

# Test nightly but don't fail
Expand Down
24 changes: 18 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,23 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added

- Implement `embedded_hal::digital::blocking::IoPin` for `CdevPin` and `SysfsPin`

## [v0.4.0-alpha.1] - 2021-09-27

### Changed

- Implement `embedded_hal::digital::IoPin` for `CdevPin` and `SysfsPin`
- Modified `OutputPin` behavior for active-low pins to match `InputPin` behavior.
- Set default features to build both sysfs and cdev pin types.
- Removed `Pin` export, use `CdevPin` or `SysfsPin`.
- Increased the Minimum Supported Rust Version to `1.36.0` due to an update of `gpio_cdev`.
- Adapted to `embedded-hal` `1.0.0-alpha.3` release.
- Adapted to `embedded-hal` `1.0.0-alpha.5` release.
- Increased the Minimum Supported Rust Version to `1.46.0` due to an update of `bitflags`.
- Updated `spidev` to version `0.5`.
- Updated `i2cdev` to version `0.5`.
- Updated `gpio-cdev` to version `0.5`.
- Updated `sysfs_gpio` to version `0.6`.
- Updated `nb` to version `1`.

## [v0.3.0] - 2019-11-25
Expand Down Expand Up @@ -66,8 +76,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

Initial release

[Unreleased]: https://github.com/japaric/linux-embedded-hal/compare/v0.2.1...HEAD
[v0.2.2]: https://github.com/japaric/linux-embedded-hal/compare/v0.2.1...v0.2.2
[Unreleased]: https://github.com/rust-embedded/linux-embedded-hal/compare/v0.4.0-alpha.1...HEAD
[v0.4.0-alpha.1]: https://github.com/rust-embedded/linux-embedded-hal/compare/v0.3.0...v0.4.0-alpha.1
[v0.3.0]: https://github.com/rust-embedded/linux-embedded-hal/compare/v0.2.2...v0.3.0
[v0.2.2]: https://github.com/rust-embedded/linux-embedded-hal/compare/v0.2.1...v0.2.2
[v0.2.1]: https://github.com/rust-embedded/linux-embedded-hal/compare/v0.2.0...v0.2.1
[v0.2.0]: https://github.com/rust-embedded/linux-embedded-hal/compare/v0.1.1...v0.2.0
[v0.1.1]: https://github.com/japaric/linux-embedded-hal/compare/v0.1.0...v0.1.1
[v0.1.1]: https://github.com/rust-embedded/linux-embedded-hal/compare/v0.1.0...v0.1.1
14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ keywords = ["Linux", "hal"]
license = "MIT OR Apache-2.0"
name = "linux-embedded-hal"
repository = "https://github.com/rust-embedded/linux-embedded-hal"
version = "0.4.0-alpha.0"
version = "0.4.0-alpha.1"
edition = "2018"

[features]
Expand All @@ -20,20 +20,20 @@ async-tokio = ["gpio-cdev/async-tokio"]
default = [ "gpio_cdev", "gpio_sysfs" ]

[dependencies]
embedded-hal = { git = "https://github.com/rust-embedded/embedded-hal" }
gpio-cdev = { version = "0.4", optional = true }
sysfs_gpio = { version = "0.5", optional = true }
embedded-hal = "=1.0.0-alpha.5"
gpio-cdev = { version = "0.5", optional = true }
sysfs_gpio = { version = "0.6", optional = true }

i2cdev = "0.4.3"
i2cdev = "0.5"
nb = "1"
serial-core = "0.4.0"
serial-unix = "0.4.0"
spidev = "0.4"
spidev = "0.5"

[dev-dependencies]
openpty = "0.1.0"

[dependencies.cast]
# we don't need the `Error` implementation
default-features = false
version = "0.2.2"
version = "0.3"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ linux-embedded-hal = { version = "0.3", features = ["gpio_cdev"] }

## Minimum Supported Rust Version (MSRV)

This crate is guaranteed to compile on stable Rust 1.36.0 and up. It *might*
This crate is guaranteed to compile on stable Rust 1.46.0 and up. It *might*
compile with older versions but that may change in any new patch release.

## License
Expand Down
4 changes: 1 addition & 3 deletions examples/transactional-i2c.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
extern crate embedded_hal;
extern crate linux_embedded_hal;
use embedded_hal::blocking::i2c::{Operation as I2cOperation, Transactional};
use embedded_hal::i2c::blocking::{Operation as I2cOperation, Transactional};
use linux_embedded_hal::I2cdev;

const ADDR: u8 = 0x12;
Expand Down
67 changes: 47 additions & 20 deletions src/cdev_pin.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
//! Linux CDev pin type
//! Implementation of [`embedded-hal`] digital input/output traits using a Linux CDev pin
//!
//! [`embedded-hal`]: https://docs.rs/embedded-hal
/// Newtype around [`gpio_cdev::LineHandle`] that implements the `embedded-hal` traits
///
/// [`gpio_cdev::LineHandle`]: https://docs.rs/gpio-cdev/0.2.0/gpio_cdev/struct.LineHandle.html
/// [`gpio_cdev::LineHandle`]: https://docs.rs/gpio-cdev/0.5.0/gpio_cdev/struct.LineHandle.html
pub struct CdevPin(pub gpio_cdev::LineHandle, gpio_cdev::LineInfo);

impl CdevPin {
/// See [`gpio_cdev::Line::request`][0] for details.
///
/// [0]: https://docs.rs/gpio-cdev/0.2.0/gpio_cdev/struct.Line.html#method.request
/// [0]: https://docs.rs/gpio-cdev/0.5.0/gpio_cdev/struct.Line.html#method.request
pub fn new(handle: gpio_cdev::LineHandle) -> Result<Self, gpio_cdev::errors::Error> {
let info = handle.line().info()?;
Ok(CdevPin(handle, info))
}

fn get_input_flags(&self) -> gpio_cdev::LineRequestFlags {
let mut flags = gpio_cdev::LineRequestFlags::INPUT;
if self.1.is_active_low() {
flags.insert(gpio_cdev::LineRequestFlags::ACTIVE_LOW);
return gpio_cdev::LineRequestFlags::INPUT | gpio_cdev::LineRequestFlags::ACTIVE_LOW;
}
return flags;
gpio_cdev::LineRequestFlags::INPUT
}

fn get_output_flags(&self) -> gpio_cdev::LineRequestFlags {
let mut flags = gpio_cdev::LineRequestFlags::OUTPUT;
if self.1.is_active_low() {
flags.insert(gpio_cdev::LineRequestFlags::ACTIVE_LOW);
}
if self.1.is_open_drain() {
flags.insert(gpio_cdev::LineRequestFlags::OPEN_DRAIN);
} else if self.1.is_open_source() {
Expand All @@ -33,35 +37,58 @@ impl CdevPin {
}
}

impl embedded_hal::digital::OutputPin for CdevPin {
/// Converts a pin state to the gpio_cdev compatible numeric value, accounting
/// for the active_low condition.
fn state_to_value(state: embedded_hal::digital::PinState, is_active_low: bool) -> u8 {
if is_active_low {
match state {
embedded_hal::digital::PinState::High => 0,
embedded_hal::digital::PinState::Low => 1,
}
} else {
match state {
embedded_hal::digital::PinState::High => 1,
embedded_hal::digital::PinState::Low => 0,
}
}
}

impl embedded_hal::digital::blocking::OutputPin for CdevPin {
type Error = gpio_cdev::errors::Error;

fn set_low(&mut self) -> Result<(), Self::Error> {
self.0.set_value(0)
self.0.set_value(state_to_value(
embedded_hal::digital::PinState::Low,
self.1.is_active_low(),
))
}

fn set_high(&mut self) -> Result<(), Self::Error> {
self.0.set_value(1)
self.0.set_value(state_to_value(
embedded_hal::digital::PinState::High,
self.1.is_active_low(),
))
}
}

impl embedded_hal::digital::InputPin for CdevPin {
impl embedded_hal::digital::blocking::InputPin for CdevPin {
type Error = gpio_cdev::errors::Error;

fn is_high(&self) -> Result<bool, Self::Error> {
if !self.1.is_active_low() {
self.0.get_value().map(|val| val != 0)
} else {
self.0.get_value().map(|val| val == 0)
}
self.0.get_value().map(|val| {
val == state_to_value(
embedded_hal::digital::PinState::High,
self.1.is_active_low(),
)
})
}

fn is_low(&self) -> Result<bool, Self::Error> {
self.is_high().map(|val| !val)
}
}

impl embedded_hal::digital::IoPin<CdevPin, CdevPin> 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> {
Expand Down Expand Up @@ -95,10 +122,10 @@ impl embedded_hal::digital::IoPin<CdevPin, CdevPin> for CdevPin {

CdevPin::new(line.request(
output_flags,
match state {
embedded_hal::digital::PinState::High => 1,
embedded_hal::digital::PinState::Low => 0,
},
state_to_value(
state,
output_flags.intersects(gpio_cdev::LineRequestFlags::ACTIVE_LOW),
),
&consumer,
)?)
}
Expand Down
90 changes: 90 additions & 0 deletions src/delay.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//! Implementation of [`embedded-hal`] delay traits
//!
//! [`embedded-hal`]: https://docs.rs/embedded-hal
use cast::{u32, u64};
use core::convert::Infallible;
use embedded_hal::delay::blocking::{DelayMs, DelayUs};
use std::thread;
use std::time::Duration;

/// Empty struct that provides delay functionality on top of `thread::sleep`
pub struct Delay;

impl DelayUs<u8> for Delay {
type Error = Infallible;

fn delay_us(&mut self, n: u8) -> Result<(), Self::Error> {
thread::sleep(Duration::new(0, u32(n) * 1000));
Ok(())
}
}

impl DelayUs<u16> for Delay {
type Error = Infallible;

fn delay_us(&mut self, n: u16) -> Result<(), Self::Error> {
thread::sleep(Duration::new(0, u32(n) * 1000));
Ok(())
}
}

impl DelayUs<u32> for Delay {
type Error = Infallible;

fn delay_us(&mut self, n: u32) -> Result<(), Self::Error> {
let secs = n / 1_000_000;
let nsecs = (n % 1_000_000) * 1_000;

thread::sleep(Duration::new(u64(secs), nsecs));
Ok(())
}
}

impl DelayUs<u64> for Delay {
type Error = Infallible;

fn delay_us(&mut self, n: u64) -> Result<(), Self::Error> {
let secs = n / 1_000_000;
let nsecs = ((n % 1_000_000) * 1_000) as u32;

thread::sleep(Duration::new(secs, nsecs));
Ok(())
}
}

impl DelayMs<u8> for Delay {
type Error = Infallible;

fn delay_ms(&mut self, n: u8) -> Result<(), Self::Error> {
thread::sleep(Duration::from_millis(u64(n)));
Ok(())
}
}

impl DelayMs<u16> for Delay {
type Error = Infallible;

fn delay_ms(&mut self, n: u16) -> Result<(), Self::Error> {
thread::sleep(Duration::from_millis(u64(n)));
Ok(())
}
}

impl DelayMs<u32> for Delay {
type Error = Infallible;

fn delay_ms(&mut self, n: u32) -> Result<(), Self::Error> {
thread::sleep(Duration::from_millis(u64(n)));
Ok(())
}
}

impl DelayMs<u64> for Delay {
type Error = Infallible;

fn delay_ms(&mut self, n: u64) -> Result<(), Self::Error> {
thread::sleep(Duration::from_millis(n));
Ok(())
}
}
Loading

0 comments on commit b78acf4

Please sign in to comment.