Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to Embedded hal 1.0.0 #16

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ version = "0.4.1"
edition = "2021"

[dependencies]
embedded-hal = { version = "0.2.4", features = ["unproven"] }
embedded-hal = { version = "1.0.0" }
shared-bus = "0.3.0"

[dev-dependencies]
embedded-hal-mock = "0.9.0"
embedded-hal-mock = "0.10.0"
12 changes: 5 additions & 7 deletions src/bus.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use embedded_hal::blocking::i2c as hal_i2c;
use embedded_hal::i2c as hal_i2c;

/// Blanket trait for types implementing `i2c::WriteRead + i2c::Write + i2c::Read`
pub trait I2cBus: hal_i2c::WriteRead + hal_i2c::Write + hal_i2c::Read {
type BusError: From<<Self as hal_i2c::WriteRead>::Error>
+ From<<Self as hal_i2c::Write>::Error>
+ From<<Self as hal_i2c::Read>::Error>;
/// Blanket trait for types implementing `i2c::I2c
pub trait I2cBus: hal_i2c::I2c {
type BusError: From<<Self as hal_i2c::ErrorType>::Error>;
}

impl<T, E> I2cBus for T
where
T: hal_i2c::WriteRead<Error = E> + hal_i2c::Write<Error = E> + hal_i2c::Read<Error = E>,
T: hal_i2c::I2c<Error = E>,
{
type BusError = E;
}
Expand Down
4 changes: 2 additions & 2 deletions src/dev/max7321.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ where
)))
}

pub fn split<'a>(&'a mut self) -> Parts<'a, I2C, M> {
pub fn split(&mut self) -> Parts<'_, I2C, M> {
Parts {
p0: crate::Pin::new(0, &self.0),
p1: crate::Pin::new(1, &self.0),
Expand Down Expand Up @@ -92,7 +92,7 @@ impl<I2C: crate::I2cBus> crate::PortDriver for Driver<I2C> {

#[cfg(test)]
mod tests {
use embedded_hal_mock::i2c as mock_i2c;
use embedded_hal_mock::eh1::i2c as mock_i2c;

#[test]
fn max7321() {
Expand Down
4 changes: 2 additions & 2 deletions src/dev/pca9536.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ where
Self(shared_bus::BusMutex::create(Driver::new(i2c)))
}

pub fn split<'a>(&'a mut self) -> Parts<'a, I2C, M> {
pub fn split(&mut self) -> Parts<'_, I2C, M> {
Parts {
io0: crate::Pin::new(0, &self.0),
io1: crate::Pin::new(1, &self.0),
Expand Down Expand Up @@ -136,7 +136,7 @@ impl<I2C: crate::I2cBus> crate::PortDriverPolarity for Driver<I2C> {

#[cfg(test)]
mod tests {
use embedded_hal_mock::i2c as mock_i2c;
use embedded_hal_mock::eh1::i2c as mock_i2c;

#[test]
fn pca9536() {
Expand Down
4 changes: 2 additions & 2 deletions src/dev/pca9538.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ where
Self(shared_bus::BusMutex::create(Driver::new(i2c, a0, a1)))
}

pub fn split<'a>(&'a mut self) -> Parts<'a, I2C, M> {
pub fn split(&mut self) -> Parts<'_, I2C, M> {
Parts {
io0: crate::Pin::new(0, &self.0),
io1: crate::Pin::new(1, &self.0),
Expand Down Expand Up @@ -148,7 +148,7 @@ impl<I2C: crate::I2cBus> crate::PortDriverPolarity for Driver<I2C> {

#[cfg(test)]
mod tests {
use embedded_hal_mock::i2c as mock_i2c;
use embedded_hal_mock::eh1::i2c as mock_i2c;

#[test]
fn pca9538() {
Expand Down
4 changes: 2 additions & 2 deletions src/dev/pca9555.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ where
Self(shared_bus::BusMutex::create(Driver::new(i2c, a0, a1, a2)))
}

pub fn split<'a>(&'a mut self) -> Parts<'a, I2C, M> {
pub fn split(&mut self) -> Parts<'_, I2C, M> {
Parts {
io0_0: crate::Pin::new(0, &self.0),
io0_1: crate::Pin::new(1, &self.0),
Expand Down Expand Up @@ -210,7 +210,7 @@ impl<I2C: crate::I2cBus> crate::PortDriverPolarity for Driver<I2C> {

#[cfg(test)]
mod tests {
use embedded_hal_mock::i2c as mock_i2c;
use embedded_hal_mock::eh1::i2c as mock_i2c;

#[test]
fn pca9555() {
Expand Down
7 changes: 3 additions & 4 deletions src/dev/pcal6408a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ where
Self(shared_bus::BusMutex::create(Driver::new(i2c, addr)))
}

pub fn split<'a>(&'a mut self) -> Parts<'a, I2C, M> {
pub fn split(&mut self) -> Parts<'_, I2C, M> {
Parts {
io0: crate::Pin::new(0, &self.0),
io1: crate::Pin::new(1, &self.0),
Expand Down Expand Up @@ -115,8 +115,7 @@ impl<I2C: crate::I2cBus> crate::PortDriver for Driver<I2C> {
out &= !mask_low as u8;
self.out = Some(out);
if (mask_high | mask_low) & 0x00FF != 0 {
self.i2c
.write_reg(self.addr, Regs::OutputPort, (out & 0xFF) as u8)?;
self.i2c.write_reg(self.addr, Regs::OutputPort, out)?;
}
Ok(())
}
Expand Down Expand Up @@ -185,7 +184,7 @@ impl<I2C: crate::I2cBus> crate::PortDriverPolarity for Driver<I2C> {

#[cfg(test)]
mod tests {
use embedded_hal_mock::i2c as mock_i2c;
use embedded_hal_mock::eh1::i2c as mock_i2c;

#[test]
fn pca6408a() {
Expand Down
4 changes: 2 additions & 2 deletions src/dev/pcal6416a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ where
Self(shared_bus::BusMutex::create(Driver::new(i2c, addr)))
}

pub fn split<'a>(&'a mut self) -> Parts<'a, I2C, M> {
pub fn split(&mut self) -> Parts<'_, I2C, M> {
Parts {
io0_0: crate::Pin::new(0, &self.0),
io0_1: crate::Pin::new(1, &self.0),
Expand Down Expand Up @@ -245,7 +245,7 @@ impl<I2C: crate::I2cBus> crate::PortDriverPolarity for Driver<I2C> {

#[cfg(test)]
mod tests {
use embedded_hal_mock::i2c as mock_i2c;
use embedded_hal_mock::eh1::i2c as mock_i2c;

#[test]
fn pca6416a() {
Expand Down
6 changes: 3 additions & 3 deletions src/dev/pcf8574.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ where
)))
}

pub fn split<'a>(&'a mut self) -> Parts<'a, I2C, M> {
pub fn split(&mut self) -> Parts<'_, I2C, M> {
Parts {
p0: crate::Pin::new(0, &self.0),
p1: crate::Pin::new(1, &self.0),
Expand All @@ -59,7 +59,7 @@ where
)))
}

pub fn split<'a>(&'a mut self) -> Parts<'a, I2C, M> {
pub fn split(&mut self) -> Parts<'_, I2C, M> {
Parts {
p0: crate::Pin::new(0, &self.0),
p1: crate::Pin::new(1, &self.0),
Expand Down Expand Up @@ -133,7 +133,7 @@ impl<I2C: crate::I2cBus> crate::PortDriver for Driver<I2C> {

#[cfg(test)]
mod tests {
use embedded_hal_mock::i2c as mock_i2c;
use embedded_hal_mock::eh1::i2c as mock_i2c;

#[test]
fn pcf8574() {
Expand Down
4 changes: 2 additions & 2 deletions src/dev/pcf8575.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ where
Self(shared_bus::BusMutex::create(Driver::new(i2c, a0, a1, a2)))
}

pub fn split<'a>(&'a mut self) -> Parts<'a, I2C, M> {
pub fn split(&mut self) -> Parts<'_, I2C, M> {
Parts {
p00: crate::Pin::new(0, &self.0),
p01: crate::Pin::new(1, &self.0),
Expand Down Expand Up @@ -113,7 +113,7 @@ impl<I2C: crate::I2cBus> crate::PortDriver for Driver<I2C> {

#[cfg(test)]
mod tests {
use embedded_hal_mock::i2c as mock_i2c;
use embedded_hal_mock::eh1::i2c as mock_i2c;

#[test]
fn pcf8575() {
Expand Down
4 changes: 2 additions & 2 deletions src/dev/tca6408a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ where
Self(shared_bus::BusMutex::create(Driver::new(i2c, a0)))
}

pub fn split<'a>(&'a mut self) -> Parts<'a, I2C, M> {
pub fn split(&mut self) -> Parts<'_, I2C, M> {
Parts {
io0: crate::Pin::new(0, &self.0),
io1: crate::Pin::new(1, &self.0),
Expand Down Expand Up @@ -148,7 +148,7 @@ impl<I2C: crate::I2cBus> crate::PortDriverPolarity for Driver<I2C> {

#[cfg(test)]
mod tests {
use embedded_hal_mock::i2c as mock_i2c;
use embedded_hal_mock::eh1::i2c as mock_i2c;

#[test]
fn tca6408a() {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! ```no_run
//! // Initialize I2C peripheral from HAL
//! let i2c = todo!();
//! # let i2c = embedded_hal_mock::i2c::Mock::new(&[]);
//! # let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
//!
//! // A0: HIGH, A1: LOW, A2: LOW
//! let mut pca9555 = port_expander::Pca9555::new(i2c, true, false, false);
Expand Down
10 changes: 5 additions & 5 deletions src/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// The usual method of setting multiple pins
///
/// ```no_run
/// # let i2c = embedded_hal_mock::i2c::Mock::new(&[]);
/// # let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
/// # let mut pcf = port_expander::Pcf8574::new(i2c, false, false, false);
/// # let p = pcf.split();
/// # let mut io0 = p.p0;
Expand All @@ -18,7 +18,7 @@
///
/// ## Example
/// ```no_run
/// # let i2c = embedded_hal_mock::i2c::Mock::new(&[]);
/// # let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
/// # let mut pcf = port_expander::Pcf8574::new(i2c, false, false, false);
/// # let p = pcf.split();
/// # let mut io0 = p.p0;
Expand Down Expand Up @@ -62,7 +62,7 @@ where
/// inputs at once. The naive approach of checking the pins in order
///
/// ```no_run
/// # let i2c = embedded_hal_mock::i2c::Mock::new(&[]);
/// # let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
/// # let mut pcf = port_expander::Pcf8574::new(i2c, false, false, false);
/// # let p = pcf.split();
/// # let io0 = p.p0;
Expand All @@ -81,7 +81,7 @@ where
///
/// ## Example
/// ```no_run
/// # let i2c = embedded_hal_mock::i2c::Mock::new(&[]);
/// # let i2c = embedded_hal_mock::eh1::i2c::Mock::new(&[]);
/// # let mut pcf = port_expander::Pcf8574::new(i2c, false, false, false);
/// # let p = pcf.split();
/// # let io0 = p.p0;
Expand Down Expand Up @@ -115,7 +115,7 @@ where

#[cfg(test)]
mod tests {
use embedded_hal_mock::i2c as mock_i2c;
use embedded_hal_mock::eh1::i2c as mock_i2c;

#[test]
fn pcf8574_write_multiple() {
Expand Down
45 changes: 21 additions & 24 deletions src/pin.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::marker::PhantomData;
use embedded_hal::digital::v2 as hal_digital;
use embedded_hal::digital::{self as hal_digital, ErrorType};

/// Representation of a port-expander pin.
///
Expand Down Expand Up @@ -29,11 +29,18 @@ where
self.pin_mask
}

pub(crate) fn port_driver<'b>(&'b self) -> &'b MUTEX {
&self.port_driver
pub(crate) fn port_driver(&self) -> &MUTEX {
self.port_driver
}
}

impl<'a, MODE, MUTEX, PD> ErrorType for Pin<'a, MODE, MUTEX>
where
PD: crate::PortDriver + crate::PortDriverTotemPole,
PD::Error: embedded_hal::digital::Error,
MUTEX: shared_bus::BusMutex<Bus = PD>,
{
type Error = PD::Error;
}
impl<'a, MODE, MUTEX, PD> Pin<'a, MODE, MUTEX>
where
PD: crate::PortDriver + crate::PortDriverTotemPole,
Expand Down Expand Up @@ -121,16 +128,15 @@ where

impl<'a, MODE: crate::mode::HasInput, MUTEX, PD> hal_digital::InputPin for Pin<'a, MODE, MUTEX>
where
PD: crate::PortDriver,
PD: crate::PortDriver + crate::PortDriverTotemPole,
<PD as crate::PortDriver>::Error: embedded_hal::digital::Error,
MUTEX: shared_bus::BusMutex<Bus = PD>,
{
type Error = PD::Error;

fn is_high(&self) -> Result<bool, Self::Error> {
fn is_high(&mut self) -> Result<bool, <PD as crate::PortDriver>::Error> {
Pin::is_high(self)
}

fn is_low(&self) -> Result<bool, Self::Error> {
fn is_low(&mut self) -> Result<bool, <PD as crate::PortDriver>::Error> {
Pin::is_low(self)
}
}
Expand Down Expand Up @@ -178,11 +184,10 @@ where

impl<'a, MODE: crate::mode::HasOutput, MUTEX, PD> hal_digital::OutputPin for Pin<'a, MODE, MUTEX>
where
PD: crate::PortDriver,
PD: crate::PortDriver + crate::PortDriverTotemPole,
<PD as crate::PortDriver>::Error: embedded_hal::digital::Error,
MUTEX: shared_bus::BusMutex<Bus = PD>,
{
type Error = PD::Error;

fn set_low(&mut self) -> Result<(), Self::Error> {
Pin::set_low(self)
}
Expand All @@ -195,25 +200,17 @@ where
impl<'a, MODE: crate::mode::HasOutput, MUTEX, PD> hal_digital::StatefulOutputPin
for Pin<'a, MODE, MUTEX>
where
PD: crate::PortDriver,
PD: crate::PortDriver + crate::PortDriverTotemPole,
<PD as crate::PortDriver>::Error: embedded_hal::digital::Error,
MUTEX: shared_bus::BusMutex<Bus = PD>,
{
fn is_set_high(&self) -> Result<bool, Self::Error> {
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
Pin::is_set_high(self)
}

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

impl<'a, MODE: crate::mode::HasOutput, MUTEX, PD> hal_digital::ToggleableOutputPin
for Pin<'a, MODE, MUTEX>
where
PD: crate::PortDriver,
MUTEX: shared_bus::BusMutex<Bus = PD>,
{
type Error = PD::Error;

fn toggle(&mut self) -> Result<(), Self::Error> {
Pin::toggle(self)
Expand Down
Loading