From cfc0bc84f4e8abadf66fd0898f0fec8679c7b6ec Mon Sep 17 00:00:00 2001 From: Marius Meissner Date: Wed, 18 Dec 2024 12:08:36 +0100 Subject: [PATCH 1/4] #5: Fixed Clippy warnings --- src/guard.rs | 2 +- src/pin_refreshable.rs | 14 +++++++------- src/pin_regular.rs | 8 ++++---- src/pins.rs | 14 +++++++------- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/guard.rs b/src/guard.rs index 88f2754..597ab50 100644 --- a/src/guard.rs +++ b/src/guard.rs @@ -31,7 +31,7 @@ impl<'a, B: Write + Read> LockFreeGuard<'a, B> { } } -impl<'a, B> RefGuard for LockFreeGuard<'a, B> +impl RefGuard for LockFreeGuard<'_, B> where B: Write + Read, { diff --git a/src/pin_refreshable.rs b/src/pin_refreshable.rs index 8f18ecf..4617609 100644 --- a/src/pin_refreshable.rs +++ b/src/pin_refreshable.rs @@ -56,7 +56,7 @@ where } } -impl<'a, B, R> RefreshableInputPin for Pin<'a, B, R, Input, RefreshMode> +impl RefreshableInputPin for Pin<'_, B, R, Input, RefreshMode> where B: Write + Read, R: RefGuard, @@ -75,7 +75,7 @@ where } } -impl<'a, B, R> RefreshableOutputPin for Pin<'a, B, R, Output, RefreshMode> +impl RefreshableOutputPin for Pin<'_, B, R, Output, RefreshMode> where B: Write + Read, R: RefGuard, @@ -94,7 +94,7 @@ where } } -impl<'a, B, R> Pin<'a, B, R, Output, RefreshMode> +impl Pin<'_, B, R, Output, RefreshMode> where B: Write + Read, R: RefGuard, @@ -111,7 +111,7 @@ where } } -impl<'a, B, R> InputPin for Pin<'a, B, R, Input, RefreshMode> +impl InputPin for Pin<'_, B, R, Input, RefreshMode> where B: Write + Read, R: RefGuard, @@ -133,7 +133,7 @@ where } } -impl<'a, B, R> OutputPin for Pin<'a, B, R, Output, RefreshMode> +impl OutputPin for Pin<'_, B, R, Output, RefreshMode> where B: Read + Write, R: RefGuard, @@ -157,7 +157,7 @@ where } } -impl<'a, B, R> StatefulOutputPin for Pin<'a, B, R, Output, RefreshMode> +impl StatefulOutputPin for Pin<'_, B, R, Output, RefreshMode> where B: Write + Read, R: RefGuard, @@ -171,7 +171,7 @@ where } } -impl<'a, B, R> toggleable::Default for Pin<'a, B, R, Output, RefreshMode> +impl toggleable::Default for Pin<'_, B, R, Output, RefreshMode> where B: Write + Read, R: RefGuard, diff --git a/src/pin_regular.rs b/src/pin_regular.rs index 35db652..cc5b19f 100644 --- a/src/pin_regular.rs +++ b/src/pin_regular.rs @@ -22,7 +22,7 @@ where } } -impl<'a, B, R> InputPin for Pin<'a, B, R, Input, RegularAccessMode> +impl InputPin for Pin<'_, B, R, Input, RegularAccessMode> where B: Write + Read, R: RefGuard, @@ -47,7 +47,7 @@ where } } -impl<'a, B, R> OutputPin for Pin<'a, B, R, Output, RegularAccessMode> +impl OutputPin for Pin<'_, B, R, Output, RegularAccessMode> where B: Read + Write, R: RefGuard, @@ -74,7 +74,7 @@ where } } -impl<'a, B, R> StatefulOutputPin for Pin<'a, B, R, Output, RegularAccessMode> +impl StatefulOutputPin for Pin<'_, B, R, Output, RegularAccessMode> where B: Write + Read, R: RefGuard, @@ -90,7 +90,7 @@ where } } -impl<'a, B, R> toggleable::Default for Pin<'a, B, R, Output, RegularAccessMode> +impl toggleable::Default for Pin<'_, B, R, Output, RegularAccessMode> where B: Write + Read, R: RefGuard, diff --git a/src/pins.rs b/src/pins.rs index fd6affb..3170e8e 100644 --- a/src/pins.rs +++ b/src/pins.rs @@ -4,10 +4,10 @@ //! //! Due to the I2C overhead, this module offers two options for state management: //! * [Regular access mode](RegularAccessMode): The state is synchronously updated when calling -//! state functions like `is_high()`, causing 1:1 I2C operations for each individual call. +//! state functions like `is_high()`, causing 1:1 I2C operations for each individual call. //! * [Refresh access mode](RefreshMode): Register states are internally cached. Functions like -//! `is_high()` are just using the cached state. The state is updated explicitly, but for all pins at once. -//! In the best case, the I2C overhead is reduced to one eighth. See [below examples](#refreshable-access-mode) for more details. +//! `is_high()` are just using the cached state. The state is updated explicitly, but for all pins at once. +//! In the best case, the I2C overhead is reduced to one eighth. See [below examples](#refreshable-access-mode) for more details. //! //! ## Setup //! Individual pins can be fetched using [PCA9539](crate::expander::PCA9539) instance. @@ -199,7 +199,7 @@ impl> Pins { /// Currently there are two modes supported: /// * Regular: State of the pin is synchronously fetched from I2C bus when calling functions like `is_high()` /// * Refreshable: State of all pins is refreshed explicitly and functions like `is_high()` are working on a cached state. -/// This reducing the I2C overhead +/// This reducing the I2C overhead pub trait AccessMode {} /// State of the pin is synchronously fetched from I2C bus @@ -238,7 +238,7 @@ where pub(crate) access_mode: PhantomData, } -impl<'a, B, R, A> Pin<'a, B, R, Input, A> +impl Pin<'_, B, R, Input, A> where B: Write + Read, R: RefGuard, @@ -256,7 +256,7 @@ where } } -impl<'a, B, R, A> Pin<'a, B, R, Output, A> +impl Pin<'_, B, R, Output, A> where B: Write + Read, R: RefGuard, @@ -273,7 +273,7 @@ where } } -impl<'a, B, M, R, A> Pin<'a, B, R, M, A> +impl Pin<'_, B, R, M, A> where B: Write + Read, R: RefGuard, From 8ea856302d66fc691f44c354659d7b351ed3cdb2 Mon Sep 17 00:00:00 2001 From: Marius Meissner Date: Wed, 18 Dec 2024 12:12:00 +0100 Subject: [PATCH 2/4] #5: Fixed Clippy warnings for optional features --- src/guard.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/guard.rs b/src/guard.rs index 597ab50..2b56df6 100644 --- a/src/guard.rs +++ b/src/guard.rs @@ -63,7 +63,7 @@ impl<'a, B: Write + Read> CsMutexGuard<'a, B> { } #[cfg(feature = "cortex-m")] -impl<'a, B> RefGuard for CsMutexGuard<'a, B> +impl RefGuard for CsMutexGuard<'_, B> where B: Write + Read, { @@ -96,7 +96,7 @@ impl<'a, B: Write + Read> SpinGuard<'a, B> { } #[cfg(feature = "spin")] -impl<'a, B> RefGuard for SpinGuard<'a, B> +impl RefGuard for SpinGuard<'_, B> where B: Write + Read, { From f96a49dc3ec9054d930dda4c0126a9b6d719305a Mon Sep 17 00:00:00 2001 From: Marius Meissner Date: Wed, 18 Dec 2024 12:12:54 +0100 Subject: [PATCH 3/4] #5: Added RP2350 targets to QA pipeline --- .github/workflows/qa.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/qa.yaml b/.github/workflows/qa.yaml index bc10bf2..580569c 100644 --- a/.github/workflows/qa.yaml +++ b/.github/workflows/qa.yaml @@ -57,6 +57,8 @@ jobs: matrix: target: - thumbv7m-none-eabi + - thumbv8m.main-none-eabihf + - riscv32imac-unknown-none-elf rust: - stable - beta From 679ee25760575850aa2ec98dd28cafb024937ecc Mon Sep 17 00:00:00 2001 From: Marius Meissner Date: Wed, 18 Dec 2024 13:43:57 +0100 Subject: [PATCH 4/4] #4: Migrated to embedded-hal v1 --- Cargo.toml | 2 +- src/example.rs | 38 +++++++----- src/expander.rs | 37 ++++++----- src/guard.rs | 22 +++---- src/lib.rs | 4 +- src/mocks.rs | 101 +++++++++++++++++++----------- src/pin_refreshable.rs | 64 +++++++++---------- src/pin_regular.rs | 63 ++++++++++--------- src/pins.rs | 35 ++++++----- src/tests.rs | 136 ++++++++++++++++++++++------------------- 10 files changed, 282 insertions(+), 220 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7cb1040..fdc2bed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ readme = "README.md" documentation = "https://docs.rs/pca9539" [dependencies] -embedded-hal = { version = "0.2.7", features = ["unproven"] } +embedded-hal = { version = "1.0.0" } bitmaps = { version = "3.1.0", default-features = false } cortex-m = { version = "0.7.4", optional = true } spin = { version = "0.9.8", optional = true } diff --git a/src/example.rs b/src/example.rs index 5308eb8..4d728b2 100644 --- a/src/example.rs +++ b/src/example.rs @@ -1,6 +1,6 @@ //! Dummy I2C bus for examples use core::convert::Infallible; -use embedded_hal::blocking::i2c::{Read, SevenBitAddress, Write}; +use embedded_hal::i2c::{ErrorType, I2c, Operation, SevenBitAddress}; #[derive(Default)] pub struct DummyI2CBus { @@ -8,24 +8,30 @@ pub struct DummyI2CBus { previous_register: u8, } -impl Write for DummyI2CBus { +impl ErrorType for DummyI2CBus { type Error = Infallible; - - fn write(&mut self, _address: SevenBitAddress, _bytes: &[u8]) -> Result<(), Self::Error> { - self.previous_register = _bytes[0]; - Ok(()) - } } -impl Read for DummyI2CBus { - type Error = Infallible; - - fn read(&mut self, _address: SevenBitAddress, buffer: &mut [u8]) -> Result<(), Self::Error> { - match self.previous_register { - 0x00 => buffer[0] = 0b0010_0110, - 0x01 => buffer[0] = 0b1110_0101, - _ => {} - }; +impl I2c for DummyI2CBus { + fn transaction( + &mut self, + _address: SevenBitAddress, + operations: &mut [Operation<'_>], + ) -> Result<(), Self::Error> { + for operation in operations { + match operation { + Operation::Read(data) => { + match self.previous_register { + 0x00 => data[0] = 0b0010_0110, + 0x01 => data[0] = 0b1110_0101, + _ => {} + }; + } + Operation::Write(data) => { + self.previous_register = data[0]; + } + } + } Ok(()) } diff --git a/src/expander.rs b/src/expander.rs index 6e3f5ec..cbdb5aa 100644 --- a/src/expander.rs +++ b/src/expander.rs @@ -94,7 +94,8 @@ use core::cell::RefCell; use core::fmt::{Debug, Formatter}; #[cfg(feature = "cortex-m")] use cortex_m::interrupt::Mutex as CsMutex; -use embedded_hal::blocking::i2c::{Read, SevenBitAddress, Write}; +use embedded_hal::digital::ErrorKind; +use embedded_hal::i2c::{I2c, SevenBitAddress}; use heapless::String; #[cfg(feature = "spin")] use spin::Mutex as SpinMutex; @@ -129,7 +130,7 @@ pub enum Mode { /// Abstraction of [PCA9539]() I/O expander pub struct PCA9539 where - B: Write + Read, + B: I2c, { bus: B, @@ -164,9 +165,9 @@ where /// Wrapped I2C error when refreshing input state /// Reading input state consists of one write, followed by a read operation -pub enum RefreshInputError> { - WriteError(::Error), - ReadError(::Error), +pub enum RefreshInputError> { + WriteError(B::Error), + ReadError(B::Error), } const COMMAND_INPUT_0: u8 = 0x00; @@ -183,7 +184,7 @@ const COMMAND_CONF_1: u8 = 0x07; impl PCA9539 where - B: Write + Read, + B: I2c, { pub fn new(bus: B, address: u8) -> Self { let mut expander = Self { @@ -232,7 +233,7 @@ where } /// Switches the given pin to the input/output mode by adjusting the configuration register - pub fn set_mode(&mut self, bank: Bank, id: PinID, mode: Mode) -> Result<(), ::Error> { + pub fn set_mode(&mut self, bank: Bank, id: PinID, mode: Mode) -> Result<(), B::Error> { match bank { Bank::Bank0 => self.configuration_0.set(id as usize, mode.into()), Bank::Bank1 => self.configuration_1.set(id as usize, mode.into()), @@ -241,7 +242,7 @@ where } /// Switches all pins of the given bank to output/input mode1 - pub fn set_mode_all(&mut self, bank: Bank, mode: Mode) -> Result<(), ::Error> { + pub fn set_mode_all(&mut self, bank: Bank, mode: Mode) -> Result<(), B::Error> { let mut bitset = Bitmap::<8>::new(); if mode == Mode::Input { @@ -267,7 +268,7 @@ where } /// Sets output state for all pins of a bank - pub fn set_state_all(&mut self, bank: Bank, is_high: bool) -> Result<(), ::Error> { + pub fn set_state_all(&mut self, bank: Bank, is_high: bool) -> Result<(), B::Error> { let mut bitset = Bitmap::<8>::new(); if is_high { @@ -282,7 +283,7 @@ where } /// Reveres/Resets the input polarity of the given pin - pub fn reverse_polarity(&mut self, bank: Bank, id: PinID, reversed: bool) -> Result<(), ::Error> { + pub fn reverse_polarity(&mut self, bank: Bank, id: PinID, reversed: bool) -> Result<(), B::Error> { match bank { Bank::Bank0 => self.polarity_0.set(id as usize, reversed), Bank::Bank1 => self.polarity_1.set(id as usize, reversed), @@ -332,7 +333,7 @@ where } /// Writes the configuration register of the given bank - fn write_conf(&mut self, bank: Bank) -> Result<(), ::Error> { + fn write_conf(&mut self, bank: Bank) -> Result<(), B::Error> { match bank { Bank::Bank0 => self .bus @@ -344,7 +345,7 @@ where } /// Writes the output register of the given bank - pub fn write_output_state(&mut self, bank: Bank) -> Result<(), ::Error> { + pub fn write_output_state(&mut self, bank: Bank) -> Result<(), B::Error> { match bank { Bank::Bank0 => self.bus.write(self.address, &[COMMAND_OUTPUT_0, *self.output_0.as_value()]), Bank::Bank1 => self.bus.write(self.address, &[COMMAND_OUTPUT_1, *self.output_1.as_value()]), @@ -352,7 +353,7 @@ where } /// Writes the polarity register of the given bank - fn write_polarity(&mut self, bank: Bank) -> Result<(), ::Error> { + fn write_polarity(&mut self, bank: Bank) -> Result<(), B::Error> { match bank { Bank::Bank0 => self.bus.write(self.address, &[COMMAND_POLARITY_0, *self.polarity_0.as_value()]), Bank::Bank1 => self.bus.write(self.address, &[COMMAND_POLARITY_1, *self.polarity_1.as_value()]), @@ -369,7 +370,7 @@ impl From for bool { } } -impl + Write> Debug for RefreshInputError { +impl Debug for RefreshInputError { fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { match self { RefreshInputError::WriteError(_) => f.write_str("RefreshInputError::WriteError"), @@ -378,7 +379,7 @@ impl + Write> Debug for RefreshInputError { } } -impl + Write> RefreshInputError { +impl RefreshInputError { pub fn to_string(&self) -> String<10> { match self { RefreshInputError::WriteError(_) => String::try_from("WriteError").unwrap(), @@ -386,3 +387,9 @@ impl + Write> RefreshInputError { } } } + +impl embedded_hal::digital::Error for RefreshInputError { + fn kind(&self) -> ErrorKind { + ErrorKind::Other + } +} diff --git a/src/guard.rs b/src/guard.rs index 2b56df6..9dc9f6e 100644 --- a/src/guard.rs +++ b/src/guard.rs @@ -5,12 +5,11 @@ use crate::expander::PCA9539; use core::cell::RefCell; use core::ops::DerefMut; -use embedded_hal::blocking::i2c::{Read, Write}; /// Manages the access of pins to expander reference pub trait RefGuard where - B: Write + Read, + B: I2c, { fn access(&self, f: F) where @@ -20,12 +19,12 @@ where /// Guard which is neither Send or Sync, but is the most efficient pub struct LockFreeGuard<'a, B> where - B: Write + Read, + B: I2c, { expander: RefCell<&'a mut PCA9539>, } -impl<'a, B: Write + Read> LockFreeGuard<'a, B> { +impl<'a, B: I2c> LockFreeGuard<'a, B> { pub fn new(expander: RefCell<&'a mut PCA9539>) -> Self { LockFreeGuard { expander } } @@ -33,7 +32,7 @@ impl<'a, B: Write + Read> LockFreeGuard<'a, B> { impl RefGuard for LockFreeGuard<'_, B> where - B: Write + Read, + B: I2c, { fn access(&self, mut f: F) where @@ -45,18 +44,19 @@ where #[cfg(feature = "cortex-m")] use cortex_m::interrupt::Mutex as CsMutex; +use embedded_hal::i2c::{I2c, SevenBitAddress}; /// Guard bases on Cortex-M mutex, which is using critical sections internally #[cfg(feature = "cortex-m")] pub struct CsMutexGuard<'a, B> where - B: Write + Read, + B: I2c, { expander: CsMutex>>, } #[cfg(feature = "cortex-m")] -impl<'a, B: Write + Read> CsMutexGuard<'a, B> { +impl<'a, B: I2c> CsMutexGuard<'a, B> { pub fn new(expander: CsMutex>>) -> Self { CsMutexGuard { expander } } @@ -65,7 +65,7 @@ impl<'a, B: Write + Read> CsMutexGuard<'a, B> { #[cfg(feature = "cortex-m")] impl RefGuard for CsMutexGuard<'_, B> where - B: Write + Read, + B: I2c, { fn access(&self, mut f: F) where @@ -83,13 +83,13 @@ use spin::Mutex as SpinMutex; #[cfg(feature = "spin")] pub struct SpinGuard<'a, B> where - B: Write + Read, + B: I2c, { expander: SpinMutex>>, } #[cfg(feature = "spin")] -impl<'a, B: Write + Read> SpinGuard<'a, B> { +impl<'a, B: I2c> SpinGuard<'a, B> { pub fn new(expander: SpinMutex>>) -> Self { SpinGuard { expander } } @@ -98,7 +98,7 @@ impl<'a, B: Write + Read> SpinGuard<'a, B> { #[cfg(feature = "spin")] impl RefGuard for SpinGuard<'_, B> where - B: Write + Read, + B: I2c, { fn access(&self, mut f: F) where diff --git a/src/lib.rs b/src/lib.rs index 0e56239..759e9be 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,13 +14,13 @@ //! use pca9539::expander::Bank::Bank0; //! use pca9539::expander::PCA9539; //! use pca9539::expander::PinID::Pin1; -//! use embedded_hal::digital::v2::InputPin; +//! use embedded_hal::digital::InputPin; //! //! let i2c_bus = DummyI2CBus::default(); //! let mut expander = PCA9539::new(i2c_bus, 0x74); //! let pins = expander.pins(); //! -//! let pin01 = pins.get_pin(Bank0, Pin1); +//! let mut pin01 = pins.get_pin(Bank0, Pin1); //! assert!(pin01.is_high().unwrap()); #![cfg_attr(not(test), no_std)] #![cfg_attr(feature = "strict", deny(warnings))] diff --git a/src/mocks.rs b/src/mocks.rs index 003caeb..51fd2a2 100644 --- a/src/mocks.rs +++ b/src/mocks.rs @@ -1,30 +1,29 @@ -use embedded_hal::blocking::i2c::{Read, SevenBitAddress, Write}; +use embedded_hal::i2c::{Error, ErrorKind, ErrorType, I2c, Operation, SevenBitAddress}; use mockall::mock; #[derive(Debug, PartialEq)] #[allow(unused)] -pub enum WriteError { - Error1, -} - -#[derive(Debug, PartialEq)] -#[allow(unused)] -pub enum ReadError { - Error1, +pub enum DummyError { + ReadError, + WriteError, } mock! { #[derive(Debug)] pub I2CBus{} - impl Write for I2CBus { - type Error = WriteError; - fn write(&mut self, address: SevenBitAddress, bytes: &[u8]) -> Result<(), WriteError>; + impl I2c for I2CBus { + fn transaction<'a>(&mut self, address: SevenBitAddress, operations: &mut [Operation<'a>]) -> Result<(), DummyError>; } +} - impl Read for I2CBus { - type Error = ReadError; - fn read(&mut self, address: SevenBitAddress, buffer: &mut [u8]) -> Result<(), ReadError>; +impl ErrorType for MockI2CBus { + type Error = DummyError; +} + +impl Error for DummyError { + fn kind(&self) -> ErrorKind { + ErrorKind::Other } } @@ -38,50 +37,82 @@ impl BusMockBuilder { } /// Expect the given number of write calls without any assertions - pub fn mock_write(mut self, times: usize) -> Self { - self.bus.expect_write().times(times).returning(move |_, _| Ok(())); + pub fn mock_transaction(mut self, times: usize) -> Self { + self.bus.expect_transaction().times(times).returning(move |_, _| Ok(())); self } pub fn expect_write(mut self, times: usize, data: &[u8]) -> Self { let data_vec = data.to_vec(); - self.bus.expect_write().times(times).returning(move |address, buffer| { - assert_eq!(0x74, address); - assert_eq!(data_vec.len(), buffer.len()); - assert_eq!(data_vec.as_slice(), buffer); - Ok(()) - }); + self.bus + .expect_transaction() + .times(times) + .returning(move |address, operations| { + assert_eq!(1, operations.len()); + assert_eq!(0x74, address); + + match operations[0] { + Operation::Read(_) => panic!("Expected write operation"), + Operation::Write(buffer) => { + assert_eq!(data_vec.len(), buffer.len()); + assert_eq!(data_vec.as_slice(), buffer); + } + } + + Ok(()) + }); self } pub fn expect_read(mut self, times: usize, data: u8) -> Self { - self.bus.expect_read().times(times).returning(move |address, buffer| { - assert_eq!(0x74, address); - assert_eq!(1, buffer.len()); - buffer[0] = data; - - Ok(()) - }); + self.bus + .expect_transaction() + .times(times) + .returning(move |address, operations| { + assert_eq!(1, operations.len()); + assert_eq!(0x74, address); + + match &mut operations[0] { + Operation::Read(buffer) => { + assert_eq!(1, buffer.len()); + buffer[0] = data; + } + Operation::Write(_) => panic!("Expected read operation"), + } + + Ok(()) + }); self } pub fn write_error(mut self, command: u8) -> Self { - self.bus.expect_write().times(1).returning(move |address, buffer| { + self.bus.expect_transaction().times(1).returning(move |address, operations| { assert_eq!(0x74, address); - assert_eq!(command, buffer[0]); - Err(WriteError::Error1) + + match operations[0] { + Operation::Read(_) => panic!("Expected write operation"), + Operation::Write(buffer) => { + assert_eq!(command, buffer[0]); + } + } + + Err(DummyError::WriteError) }); self } pub fn read_error(mut self) -> Self { - self.bus.expect_read().times(1).returning(move |address, _| { + self.bus.expect_transaction().times(1).returning(move |address, operations| { assert_eq!(0x74, address); - Err(ReadError::Error1) + if let Operation::Write(_) = operations[0] { + panic!("Expected read operation"); + } + + Err(DummyError::ReadError) }); self diff --git a/src/pin_refreshable.rs b/src/pin_refreshable.rs index 4617609..07fe62c 100644 --- a/src/pin_refreshable.rs +++ b/src/pin_refreshable.rs @@ -3,8 +3,8 @@ use crate::guard::RefGuard; use crate::pins::{Input, Output, Pin, PinMode, RefreshMode}; use core::convert::Infallible; use core::marker::PhantomData; -use embedded_hal::blocking::i2c::{Read, Write}; -use embedded_hal::digital::v2::{toggleable, InputPin, IoPin, OutputPin, PinState, StatefulOutputPin}; +use embedded_hal::digital::{ErrorType, InputPin, OutputPin, PinState, StatefulOutputPin}; +use embedded_hal::i2c::{I2c, SevenBitAddress}; /// Trait for refreshable pins in output mode pub trait RefreshableOutputPin { @@ -30,7 +30,7 @@ pub trait RefreshableInputPin { impl<'a, B, R> Pin<'a, B, R, Input, RefreshMode> where - B: Write + Read, + B: I2c, R: RefGuard, { pub fn refreshable(expander: &'a R, bank: Bank, id: PinID) -> Self { @@ -58,7 +58,7 @@ where impl RefreshableInputPin for Pin<'_, B, R, Input, RefreshMode> where - B: Write + Read, + B: I2c, R: RefGuard, { type Error = RefreshInputError; @@ -77,10 +77,10 @@ where impl RefreshableOutputPin for Pin<'_, B, R, Output, RefreshMode> where - B: Write + Read, + B: I2c, R: RefGuard, { - type Error = ::Error; + type Error = B::Error; /// Updates the output state of all pins of the same bank fn update_bank(&self) -> Result<(), Self::Error> { @@ -96,11 +96,11 @@ where impl Pin<'_, B, R, Output, RefreshMode> where - B: Write + Read, + B: I2c, R: RefGuard, { /// Writes the output state of the given bank - fn update(&self, bank: Bank) -> Result<(), ::Error> { + fn update(&self, bank: Bank) -> Result<(), B::Error> { let mut result = Ok(()); self.expander.access(|expander| { @@ -111,14 +111,20 @@ where } } -impl InputPin for Pin<'_, B, R, Input, RefreshMode> +impl ErrorType for Pin<'_, B, R, Input, RefreshMode> where - B: Write + Read, + B: I2c, R: RefGuard, { type Error = Infallible; +} - fn is_high(&self) -> Result { +impl InputPin for Pin<'_, B, R, Input, RefreshMode> +where + B: I2c, + R: RefGuard, +{ + fn is_high(&mut self) -> Result { let mut state = false; self.expander.access(|expander| { @@ -128,18 +134,24 @@ where Ok(state) } - fn is_low(&self) -> Result { + fn is_low(&mut self) -> Result { Ok(!self.is_high()?) } } -impl OutputPin for Pin<'_, B, R, Output, RefreshMode> +impl ErrorType for Pin<'_, B, R, Output, RefreshMode> where - B: Read + Write, + B: I2c, R: RefGuard, { type Error = Infallible; +} +impl OutputPin for Pin<'_, B, R, Output, RefreshMode> +where + B: I2c, + R: RefGuard, +{ fn set_low(&mut self) -> Result<(), Self::Error> { self.set_state(PinState::Low) } @@ -159,35 +171,25 @@ where impl StatefulOutputPin for Pin<'_, B, R, Output, RefreshMode> where - B: Write + Read, + B: I2c, R: RefGuard, { - fn is_set_high(&self) -> Result { + fn is_set_high(&mut self) -> Result { Ok(self.is_pin_output_high()) } - fn is_set_low(&self) -> Result { + fn is_set_low(&mut self) -> Result { Ok(!self.is_pin_output_high()) } } -impl toggleable::Default for Pin<'_, B, R, Output, RefreshMode> +impl<'a, B, M, R> Pin<'a, B, R, M, RefreshMode> where - B: Write + Read, - R: RefGuard, -{ -} - -impl<'a, B, M, R> IoPin, Pin<'a, B, R, Output, RefreshMode>> - for Pin<'a, B, R, M, RefreshMode> -where - B: Write + Read, + B: I2c, R: RefGuard, M: PinMode, { - type Error = ::Error; - - fn into_input_pin(self) -> Result, Self::Error> { + pub fn into_input_pin(self) -> Result, B::Error> { self.change_mode(Mode::Input)?; Ok(Pin { @@ -200,7 +202,7 @@ where }) } - fn into_output_pin(self, state: PinState) -> Result, Self::Error> { + pub fn into_output_pin(self, state: PinState) -> Result, B::Error> { self.change_mode(Mode::Output)?; let mut pin = Pin { diff --git a/src/pin_regular.rs b/src/pin_regular.rs index cc5b19f..11c5647 100644 --- a/src/pin_regular.rs +++ b/src/pin_regular.rs @@ -2,12 +2,12 @@ use crate::expander::{Bank, Mode, PinID, RefreshInputError}; use crate::guard::RefGuard; use crate::pins::{Input, Output, Pin, PinMode, RegularAccessMode}; use core::marker::PhantomData; -use embedded_hal::blocking::i2c::{Read, Write}; -use embedded_hal::digital::v2::{toggleable, InputPin, IoPin, OutputPin, PinState, StatefulOutputPin}; +use embedded_hal::digital::{ErrorType, InputPin, OutputPin, PinState, StatefulOutputPin}; +use embedded_hal::i2c::{I2c, SevenBitAddress}; impl<'a, B, R> Pin<'a, B, R, Input, RegularAccessMode> where - B: Write + Read, + B: I2c, R: RefGuard, { pub fn regular(expander: &'a R, bank: Bank, id: PinID) -> Self { @@ -22,14 +22,20 @@ where } } -impl InputPin for Pin<'_, B, R, Input, RegularAccessMode> +impl ErrorType for Pin<'_, B, R, Input, RegularAccessMode> where - B: Write + Read, + B: I2c, R: RefGuard, { type Error = RefreshInputError; +} - fn is_high(&self) -> Result { +impl InputPin for Pin<'_, B, R, Input, RegularAccessMode> +where + B: I2c, + R: RefGuard, +{ + fn is_high(&mut self) -> Result { let mut result = Ok(false); self.expander.access(|expander| { @@ -42,18 +48,24 @@ where result } - fn is_low(&self) -> Result { + fn is_low(&mut self) -> Result { Ok(!self.is_high()?) } } -impl OutputPin for Pin<'_, B, R, Output, RegularAccessMode> +impl ErrorType for Pin<'_, B, R, Output, RegularAccessMode> where - B: Read + Write, + B: I2c, R: RefGuard, { - type Error = ::Error; + type Error = RefreshInputError; +} +impl OutputPin for Pin<'_, B, R, Output, RegularAccessMode> +where + B: I2c, + R: RefGuard, +{ fn set_low(&mut self) -> Result<(), Self::Error> { self.set_state(PinState::Low) } @@ -67,7 +79,7 @@ where self.expander.access(|expander| { expander.set_state(self.bank, self.id, state == PinState::High); - result = expander.write_output_state(self.bank); + result = expander.write_output_state(self.bank).map_err(RefreshInputError::WriteError); }); result @@ -76,37 +88,27 @@ where impl StatefulOutputPin for Pin<'_, B, R, Output, RegularAccessMode> where - B: Write + Read, + B: I2c, R: RefGuard, { /// As this is just acting on cached register data, its in fact Infallible - fn is_set_high(&self) -> Result { + fn is_set_high(&mut self) -> Result { Ok(self.is_pin_output_high()) } /// As this is just acting on cached register data, its in fact Infallible - fn is_set_low(&self) -> Result { + fn is_set_low(&mut self) -> Result { Ok(!self.is_pin_output_high()) } } -impl toggleable::Default for Pin<'_, B, R, Output, RegularAccessMode> +impl<'a, B, M, R> Pin<'a, B, R, M, RegularAccessMode> where - B: Write + Read, - R: RefGuard, -{ -} - -impl<'a, B, M, R> IoPin, Pin<'a, B, R, Output, RegularAccessMode>> - for Pin<'a, B, R, M, RegularAccessMode> -where - B: Write + Read, + B: I2c, R: RefGuard, M: PinMode, { - type Error = ::Error; - - fn into_input_pin(self) -> Result, Self::Error> { + pub fn into_input_pin(self) -> Result, B::Error> { self.change_mode(Mode::Input)?; Ok(Pin { @@ -119,8 +121,11 @@ where }) } - fn into_output_pin(self, state: PinState) -> Result, Self::Error> { - self.change_mode(Mode::Output)?; + pub fn into_output_pin( + self, + state: PinState, + ) -> Result, RefreshInputError> { + self.change_mode(Mode::Output).map_err(RefreshInputError::WriteError)?; let mut pin = Pin { expander: self.expander, diff --git a/src/pins.rs b/src/pins.rs index 3170e8e..7f3ad07 100644 --- a/src/pins.rs +++ b/src/pins.rs @@ -31,12 +31,12 @@ //!# use pca9539::expander::Bank::{Bank0, Bank1}; //!# use pca9539::expander::PCA9539; //!# use pca9539::expander::PinID::{Pin1, Pin2, Pin4}; -//!# use embedded_hal::digital::v2::{InputPin, IoPin, PinState, OutputPin}; +//!# use embedded_hal::digital::{InputPin, PinState, OutputPin}; //!# //!# let i2c_bus = DummyI2CBus::default(); //!# let mut expander = PCA9539::new(i2c_bus, 0x74); //! let pins = expander.pins(); -//! let pin12 = pins.get_pin(Bank1, Pin2); +//! let mut pin12 = pins.get_pin(Bank1, Pin2); //! let mut pin04 = pins.get_pin(Bank0, Pin4).into_output_pin(PinState::Low).unwrap(); //! //! // Fetching input state of Pin12 @@ -60,15 +60,15 @@ //!# use pca9539::expander::Bank::{Bank0, Bank1}; //!# use pca9539::expander::PCA9539; //!# use pca9539::expander::PinID::{Pin0, Pin1, Pin2, Pin3, Pin4}; -//!# use embedded_hal::digital::v2::{InputPin, IoPin, PinState, OutputPin}; +//!# use embedded_hal::digital::{InputPin, PinState, OutputPin}; //!# use pca9539::pins::RefreshableInputPin; //!# //!# let i2c_bus = DummyI2CBus::default(); //!# let mut expander = PCA9539::new(i2c_bus, 0x74); //! let pins = expander.pins(); -//! let pin00 = pins.get_refreshable_pin(Bank0, Pin0); -//! let pin10 = pins.get_refreshable_pin(Bank1, Pin0); -//! let pin11 = pins.get_refreshable_pin(Bank1, Pin1); +//! let mut pin00 = pins.get_refreshable_pin(Bank0, Pin0); +//! let mut pin10 = pins.get_refreshable_pin(Bank1, Pin0); +//! let mut pin11 = pins.get_refreshable_pin(Bank1, Pin1); //! //! // Updates the input state of just Bank1. So input state of Pin10 and Pin11 is now up2date //! pin10.refresh_bank().unwrap(); @@ -85,7 +85,7 @@ //!# use pca9539::expander::Bank::{Bank0, Bank1}; //!# use pca9539::expander::PCA9539; //!# use pca9539::expander::PinID::{Pin0, Pin1, Pin2, Pin3, Pin4}; -//!# use embedded_hal::digital::v2::{InputPin, IoPin, PinState, OutputPin}; +//!# use embedded_hal::digital::{InputPin, PinState, OutputPin}; //!# use pca9539::pins::RefreshableOutputPin; //!# //!# let i2c_bus = DummyI2CBus::default(); @@ -160,18 +160,17 @@ //! ``` use crate::expander::{Bank, Mode, PinID}; use crate::guard::RefGuard; -use core::marker::PhantomData; -use embedded_hal::blocking::i2c::{Read, Write}; - pub use crate::pin_refreshable::{RefreshableInputPin, RefreshableOutputPin}; +use core::marker::PhantomData; +use embedded_hal::i2c::{I2c, SevenBitAddress}; /// Container for fetching individual pins -pub struct Pins> { +pub struct Pins, R: RefGuard> { guard: R, bus: PhantomData B>, } -impl> Pins { +impl, R: RefGuard> Pins { pub fn new(guard: R) -> Self { Self { guard, @@ -224,7 +223,7 @@ impl PinMode for Output {} /// Individual GPIO pin pub struct Pin<'a, B, R, M, A> where - B: Write + Read, + B: I2c, R: RefGuard, M: PinMode, A: AccessMode, @@ -240,12 +239,12 @@ where impl Pin<'_, B, R, Input, A> where - B: Write + Read, + B: I2c, R: RefGuard, A: AccessMode, { /// Reverses/Resets the input polarity - pub fn invert_polarity(&self, invert: bool) -> Result<(), ::Error> { + pub fn invert_polarity(&self, invert: bool) -> Result<(), B::Error> { let mut result = Ok(()); self.expander.access(|expander| { @@ -258,7 +257,7 @@ where impl Pin<'_, B, R, Output, A> where - B: Write + Read, + B: I2c, R: RefGuard, A: AccessMode, { @@ -275,13 +274,13 @@ where impl Pin<'_, B, R, M, A> where - B: Write + Read, + B: I2c, R: RefGuard, M: PinMode, A: AccessMode, { /// Switches the pin to the given mode - pub(crate) fn change_mode(&self, mode: Mode) -> Result<(), ::Error> { + pub(crate) fn change_mode(&self, mode: Mode) -> Result<(), B::Error> { let mut result = Ok(()); self.expander.access(|expander| { diff --git a/src/tests.rs b/src/tests.rs index e65bb15..1650a80 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -6,10 +6,10 @@ use crate::expander::PCA9539; use crate::guard::LockFreeGuard; #[cfg(feature = "spin")] use crate::guard::SpinGuard; -use crate::mocks::{BusMockBuilder, MockI2CBus, WriteError}; +use crate::mocks::{BusMockBuilder, DummyError, MockI2CBus}; use crate::pin_refreshable::{RefreshableInputPin, RefreshableOutputPin}; use crate::pins::Pins; -use embedded_hal::digital::v2::{InputPin, IoPin, OutputPin, PinState, StatefulOutputPin, ToggleableOutputPin}; +use embedded_hal::digital::{InputPin, OutputPin, PinState, StatefulOutputPin}; #[test] fn test_expander_output_mode_bank0() { @@ -38,7 +38,7 @@ fn test_expander_output_mode_bank1() { #[test] fn test_expander_input_mode_bank0() { let i2c_bus = BusMockBuilder::new() - .mock_write(1) + .mock_transaction(1) .expect_write(1, &[0x06, 0b0000_0100]) .expect_write(1, &[0x06, 0b1000_0100]) .into_mock(); @@ -52,7 +52,7 @@ fn test_expander_input_mode_bank0() { #[test] fn test_expander_input_mode_bank1() { let i2c_bus = BusMockBuilder::new() - .mock_write(1) + .mock_transaction(1) .expect_write(1, &[0x07, 0b0000_0001]) .expect_write(1, &[0x07, 0b0000_1001]) .into_mock(); @@ -94,7 +94,7 @@ fn test_expander_state_low_bank1() { #[test] fn test_expander_state_high_bank0() { let i2c_bus = BusMockBuilder::new() - .mock_write(1) + .mock_transaction(1) .expect_write(1, &[0x02, 0b0010_0000]) .expect_write(1, &[0x02, 0b0010_0001]) .into_mock(); @@ -110,7 +110,7 @@ fn test_expander_state_high_bank0() { #[test] fn test_expander_state_high_bank1() { let i2c_bus = BusMockBuilder::new() - .mock_write(1) + .mock_transaction(1) .expect_write(1, &[0x03, 0b0100_0000]) .expect_write(1, &[0x03, 0b0101_0000]) .into_mock(); @@ -126,7 +126,7 @@ fn test_expander_state_high_bank1() { #[test] fn test_set_mode_all_input_bank0() { let i2c_bus = BusMockBuilder::new() - .mock_write(1) + .mock_transaction(1) .expect_write(1, &[0x06, 0b1111_1111]) .into_mock(); @@ -146,7 +146,7 @@ fn test_set_mode_all_output_bank0() { #[test] fn test_set_mode_all_input_bank1() { let i2c_bus = BusMockBuilder::new() - .mock_write(1) + .mock_transaction(1) .expect_write(1, &[0x07, 0b1111_1111]) .into_mock(); @@ -166,7 +166,7 @@ fn test_set_mode_all_output_bank1() { #[test] fn test_set_state_all_low_bank0() { let i2c_bus = BusMockBuilder::new() - .mock_write(1) + .mock_transaction(1) .expect_write(1, &[0x02, 0b0000_0000]) .into_mock(); @@ -178,7 +178,7 @@ fn test_set_state_all_low_bank0() { #[test] fn test_set_state_all_low_bank1() { let i2c_bus = BusMockBuilder::new() - .mock_write(1) + .mock_transaction(1) .expect_write(1, &[0x03, 0b0000_0000]) .into_mock(); @@ -304,14 +304,19 @@ fn test_is_pin_high_bank1() { #[test] fn test_regular_pin_input_bank0() { let i2c_bus = BusMockBuilder::new() - .expect_write(4, &[0x00]) - .expect_read(2, 0b0000_0100) - .expect_read(2, 0b0100_0000) + .expect_write(1, &[0x00]) + .expect_read(1, 0b0000_0100) + .expect_write(1, &[0x00]) + .expect_read(1, 0b0000_0100) + .expect_write(1, &[0x00]) + .expect_read(1, 0b0100_0000) + .expect_write(1, &[0x00]) + .expect_read(1, 0b0100_0000) .into_mock(); let mut expander = PCA9539::new(i2c_bus, 0x74); let pins = get_pins(&mut expander); - let pin = pins.get_pin(Bank0, Pin2); + let mut pin = pins.get_pin(Bank0, Pin2); assert!(pin.is_high().unwrap()); assert!(!pin.is_low().unwrap()); @@ -322,14 +327,19 @@ fn test_regular_pin_input_bank0() { #[test] fn test_regular_pin_input_bank1() { let i2c_bus = BusMockBuilder::new() - .expect_write(4, &[0x01]) - .expect_read(2, 0b0100_0100) - .expect_read(2, 0b0000_0000) + .expect_write(1, &[0x01]) + .expect_read(1, 0b0100_0100) + .expect_write(1, &[0x01]) + .expect_read(1, 0b0100_0100) + .expect_write(1, &[0x01]) + .expect_read(1, 0b0000_0000) + .expect_write(1, &[0x01]) + .expect_read(1, 0b0000_0000) .into_mock(); let mut expander = PCA9539::new(i2c_bus, 0x74); let pins = get_pins(&mut expander); - let pin = pins.get_pin(Bank1, Pin6); + let mut pin = pins.get_pin(Bank1, Pin6); assert!(pin.is_high().unwrap()); assert!(!pin.is_low().unwrap()); @@ -343,18 +353,18 @@ fn test_regular_pin_input_write_error() { let mut expander = PCA9539::new(i2c_bus, 0x74); let pins = get_pins(&mut expander); - let pin = pins.get_pin(Bank1, Pin6); + let mut pin = pins.get_pin(Bank1, Pin6); assert_eq!("WriteError", pin.is_high().unwrap_err().to_string()) } #[test] fn test_regular_pin_input_read_error() { - let i2c_bus = BusMockBuilder::new().mock_write(1).read_error().into_mock(); + let i2c_bus = BusMockBuilder::new().mock_transaction(1).read_error().into_mock(); let mut expander = PCA9539::new(i2c_bus, 0x74); let pins = get_pins(&mut expander); - let pin = pins.get_pin(Bank1, Pin6); + let mut pin = pins.get_pin(Bank1, Pin6); assert_eq!("ReadError", pin.is_high().unwrap_err().to_string()) } @@ -362,16 +372,17 @@ fn test_regular_pin_input_read_error() { #[test] fn test_refreshable_pin_input_bank0() { let i2c_bus = BusMockBuilder::new() - .expect_write(2, &[0x00]) + .expect_write(1, &[0x00]) .expect_read(1, 0b0000_0100) + .expect_write(1, &[0x00]) .expect_read(1, 0b0100_1000) .into_mock(); let mut expander = PCA9539::new(i2c_bus, 0x74); let pins = get_pins(&mut expander); - let pin02 = pins.get_refreshable_pin(Bank0, Pin2); - let pin03 = pins.get_refreshable_pin(Bank0, Pin3); + let mut pin02 = pins.get_refreshable_pin(Bank0, Pin2); + let mut pin03 = pins.get_refreshable_pin(Bank0, Pin3); pin02.refresh_bank().unwrap(); assert!(pin02.is_high().unwrap()); @@ -389,16 +400,17 @@ fn test_refreshable_pin_input_bank0() { #[test] fn test_refreshable_pin_input_bank1() { let i2c_bus = BusMockBuilder::new() - .expect_write(2, &[0x01]) + .expect_write(1, &[0x01]) .expect_read(1, 0b0010_0100) + .expect_write(1, &[0x01]) .expect_read(1, 0b0000_0000) .into_mock(); let mut expander = PCA9539::new(i2c_bus, 0x74); let pins = get_pins(&mut expander); - let pin12 = pins.get_refreshable_pin(Bank1, Pin2); - let pin15 = pins.get_refreshable_pin(Bank1, Pin5); + let mut pin12 = pins.get_refreshable_pin(Bank1, Pin2); + let mut pin15 = pins.get_refreshable_pin(Bank1, Pin5); pin12.refresh_bank().unwrap(); assert!(pin12.is_high().unwrap()); @@ -417,20 +429,20 @@ fn test_refreshable_pin_input_bank1() { fn test_refreshable_pin_input_mixed_banks() { let i2c_bus = BusMockBuilder::new() .expect_write(1, &[0x00]) - .expect_write(1, &[0x01]) - .expect_write(1, &[0x00]) - .expect_write(1, &[0x01]) .expect_read(1, 0b0001_0001) + .expect_write(1, &[0x01]) .expect_read(1, 0b1000_0000) + .expect_write(1, &[0x00]) .expect_read(1, 0b0000_0001) + .expect_write(1, &[0x01]) .expect_read(1, 0b0000_0000) .into_mock(); let mut expander = PCA9539::new(i2c_bus, 0x74); let pins = get_pins(&mut expander); - let pin00 = pins.get_refreshable_pin(Bank0, Pin0); - let pin17 = pins.get_refreshable_pin(Bank1, Pin7); + let mut pin00 = pins.get_refreshable_pin(Bank0, Pin0); + let mut pin17 = pins.get_refreshable_pin(Bank1, Pin7); pin00.refresh_all().unwrap(); assert!(pin00.is_high().unwrap()); @@ -452,7 +464,7 @@ fn test_refreshable_pin_refresh_bank_write_error() { let mut expander = PCA9539::new(i2c_bus, 0x74); let pins = get_pins(&mut expander); - let pin = pins.get_refreshable_pin(Bank0, Pin0); + let mut pin = pins.get_refreshable_pin(Bank0, Pin0); let error = pin.refresh_bank().unwrap_err(); assert_eq!("WriteError", error.to_string()); @@ -466,7 +478,7 @@ fn test_refreshable_pin_refresh_bank_read_error() { let mut expander = PCA9539::new(i2c_bus, 0x74); let pins = get_pins(&mut expander); - let pin = pins.get_refreshable_pin(Bank0, Pin0); + let mut pin = pins.get_refreshable_pin(Bank0, Pin0); let error = pin.refresh_bank().unwrap_err(); assert_eq!("ReadError", error.to_string()); @@ -484,7 +496,7 @@ fn test_refreshable_pin_refresh_all_write_error() { let mut expander = PCA9539::new(i2c_bus, 0x74); let pins = get_pins(&mut expander); - let pin = pins.get_refreshable_pin(Bank0, Pin0); + let mut pin = pins.get_refreshable_pin(Bank0, Pin0); let error = pin.refresh_all().unwrap_err(); assert_eq!("WriteError", error.to_string()); @@ -503,7 +515,7 @@ fn test_refreshable_pin_refresh_all_read_error() { let mut expander = PCA9539::new(i2c_bus, 0x74); let pins = get_pins(&mut expander); - let pin = pins.get_refreshable_pin(Bank0, Pin0); + let mut pin = pins.get_refreshable_pin(Bank0, Pin0); let error = pin.refresh_all().unwrap_err(); assert_eq!("ReadError", error.to_string()); @@ -513,7 +525,7 @@ fn test_refreshable_pin_refresh_all_read_error() { #[test] fn test_regular_pin_set_output_state() { let i2c_bus = BusMockBuilder::new() - .mock_write(6) // Mode switch + .mock_transaction(6) // Mode switch .expect_write(1, &[0x03, 0b1111_1011]) .expect_write(1, &[0x02, 0b1110_1111]) .expect_write(1, &[0x02, 0b1110_1110]) @@ -560,45 +572,45 @@ fn test_regular_pin_set_output_state() { #[test] fn test_regular_pin_set_low_write_error() { - let i2c_bus = BusMockBuilder::new().mock_write(2).write_error(0x2).into_mock(); + let i2c_bus = BusMockBuilder::new().mock_transaction(2).write_error(0x2).into_mock(); let mut expander = PCA9539::new(i2c_bus, 0x74); let pins = get_pins(&mut expander); let mut pin = pins.get_pin(Bank0, Pin0).into_output_pin(PinState::Low).unwrap(); let result = pin.set_low(); - assert_eq!(WriteError::Error1, result.unwrap_err()); + assert_eq!("WriteError", result.unwrap_err().to_string()); } #[test] fn test_regular_pin_set_high_write_error() { - let i2c_bus = BusMockBuilder::new().mock_write(2).write_error(0x2).into_mock(); + let i2c_bus = BusMockBuilder::new().mock_transaction(2).write_error(0x2).into_mock(); let mut expander = PCA9539::new(i2c_bus, 0x74); let pins = get_pins(&mut expander); let mut pin = pins.get_pin(Bank0, Pin0).into_output_pin(PinState::Low).unwrap(); let result = pin.set_high(); - assert_eq!(WriteError::Error1, result.unwrap_err()); + assert_eq!("WriteError", result.unwrap_err().to_string()); } #[test] fn test_regular_pin_set_state_write_error() { - let i2c_bus = BusMockBuilder::new().mock_write(2).write_error(0x2).into_mock(); + let i2c_bus = BusMockBuilder::new().mock_transaction(2).write_error(0x2).into_mock(); let mut expander = PCA9539::new(i2c_bus, 0x74); let pins = get_pins(&mut expander); let mut pin = pins.get_pin(Bank0, Pin0).into_output_pin(PinState::Low).unwrap(); let result = pin.set_state(PinState::High); - assert_eq!(WriteError::Error1, result.unwrap_err()); + assert_eq!("WriteError", result.unwrap_err().to_string()); } #[test] fn test_refreshable_pin_set_output_state() { let i2c_bus = BusMockBuilder::new() - .mock_write(2) // setting all low - .mock_write(16) // mode switch + .mock_transaction(2) // setting all low + .mock_transaction(16) // mode switch .expect_write(1, &[0x02, 0b0000_0110]) // Update Bank 0 .expect_write(1, &[0x03, 0b1110_0000]) // Update Bank 1 .expect_write(1, &[0x02, 0b0000_0110]) // Update all @@ -659,20 +671,20 @@ fn test_refreshable_pin_set_output_state() { #[test] fn test_refreshable_pin_update_bank_write_error() { - let i2c_bus = BusMockBuilder::new().mock_write(2).write_error(0x2).into_mock(); + let i2c_bus = BusMockBuilder::new().mock_transaction(2).write_error(0x2).into_mock(); let mut expander = PCA9539::new(i2c_bus, 0x74); let pins = get_pins(&mut expander); let pin = pins.get_refreshable_pin(Bank0, Pin0).into_output_pin(PinState::Low).unwrap(); let result = pin.update_bank(); - assert_eq!(WriteError::Error1, result.unwrap_err()); + assert_eq!(DummyError::WriteError, result.unwrap_err()); } #[test] fn test_refreshable_pin_update_all_write_error() { let i2c_bus = BusMockBuilder::new() - .mock_write(2) + .mock_transaction(2) .expect_write(1, &[0x2, 0b1111_1111]) // Update Bank 0 .write_error(0x3) .into_mock(); @@ -682,13 +694,13 @@ fn test_refreshable_pin_update_all_write_error() { let pin = pins.get_refreshable_pin(Bank1, Pin0).into_output_pin(PinState::Low).unwrap(); let result = pin.update_all(); - assert_eq!(WriteError::Error1, result.unwrap_err()); + assert_eq!(DummyError::WriteError, result.unwrap_err()); } #[test] fn test_regular_pin_into_output_pin() { let i2c_bus = BusMockBuilder::new() - .mock_write(1) + .mock_transaction(1) .expect_write(1, &[0x06, 0b1111_1110]) .expect_write(1, &[0x02, 0b0000_0001]) .into_mock(); @@ -702,7 +714,7 @@ fn test_regular_pin_into_output_pin() { #[test] fn test_regular_pin_into_input_pin() { let i2c_bus = BusMockBuilder::new() - .mock_write(2) + .mock_transaction(2) .expect_write(1, &[0x06, 0b1111_1111]) .into_mock(); @@ -730,7 +742,7 @@ fn test_regular_pin_into_output_pin_mode_switch_error() { #[test] fn test_regular_pin_into_output_pin_state_set_error() { - let i2c_bus = BusMockBuilder::new().mock_write(1).write_error(0x2).into_mock(); + let i2c_bus = BusMockBuilder::new().mock_transaction(1).write_error(0x2).into_mock(); let mut expander = PCA9539::new(i2c_bus, 0x74); let pins = get_pins(&mut expander); @@ -753,7 +765,7 @@ fn test_regular_pin_into_input_pin_mode_error() { #[test] fn test_refreshable_pin_into_output_pin() { let i2c_bus = BusMockBuilder::new() - .mock_write(1) + .mock_transaction(1) .expect_write(1, &[0x06, 0b1111_1110]) .expect_write(1, &[0x02, 0b0000_0001]) .into_mock(); @@ -767,7 +779,7 @@ fn test_refreshable_pin_into_output_pin() { #[test] fn test_refreshable_pin_into_input_pin() { let i2c_bus = BusMockBuilder::new() - .mock_write(2) + .mock_transaction(2) .expect_write(1, &[0x06, 0b1111_1111]) .into_mock(); @@ -795,7 +807,7 @@ fn test_refreshable_pin_into_output_pin_mode_switch_error() { #[test] fn test_refreshable_pin_into_output_pin_state_set_error() { - let i2c_bus = BusMockBuilder::new().mock_write(1).write_error(0x2).into_mock(); + let i2c_bus = BusMockBuilder::new().mock_transaction(1).write_error(0x2).into_mock(); let mut expander = PCA9539::new(i2c_bus, 0x74); let pins = get_pins(&mut expander); @@ -818,7 +830,7 @@ fn test_refreshable_pin_into_input_pin_mode_error() { #[test] fn test_regular_pin_toggle() { let i2c_bus = BusMockBuilder::new() - .mock_write(2) // Mode switch + .mock_transaction(2) // Mode switch .expect_write(1, &[0x02, 0b1111_1011]) .into_mock(); @@ -832,7 +844,7 @@ fn test_regular_pin_toggle() { #[test] fn test_regular_pin_toggle_error() { let i2c_bus = BusMockBuilder::new() - .mock_write(2) // Mode switch + .mock_transaction(2) // Mode switch .write_error(0x2) .into_mock(); @@ -841,13 +853,13 @@ fn test_regular_pin_toggle_error() { let mut pin = pins.get_pin(Bank0, Pin2).into_output_pin(PinState::High).unwrap(); let result = pin.toggle(); - assert_eq!(WriteError::Error1, result.unwrap_err()); + assert_eq!("WriteError", result.unwrap_err().to_string()); } #[test] fn test_refreshable_pin_toggle() { let i2c_bus = BusMockBuilder::new() - .mock_write(2) // Mode switch + .mock_transaction(2) // Mode switch .expect_write(1, &[0x02, 0b1111_0111]) .into_mock(); @@ -862,7 +874,7 @@ fn test_refreshable_pin_toggle() { #[test] fn test_refreshable_pin_toggle_no_update() { let i2c_bus = BusMockBuilder::new() - .mock_write(2) // Mode switch + .mock_transaction(2) // Mode switch .into_mock(); let mut expander = PCA9539::new(i2c_bus, 0x74); @@ -898,7 +910,7 @@ fn test_regular_pin_invert_polarity_error() { let pin = pins.get_pin(Bank0, Pin4); let result = pin.invert_polarity(true); - assert_eq!(WriteError::Error1, result.unwrap_err()); + assert_eq!(DummyError::WriteError, result.unwrap_err()); } #[test] @@ -927,7 +939,7 @@ fn test_refreshable_pin_invert_polarity_error() { let pin = pins.get_refreshable_pin(Bank1, Pin4); let result = pin.invert_polarity(true); - assert_eq!(WriteError::Error1, result.unwrap_err()); + assert_eq!(DummyError::WriteError, result.unwrap_err()); } /// Testing spin based RefGuard