From 7185b911ef9e9dfb8112e094cbc1b3469778bdae Mon Sep 17 00:00:00 2001 From: Ayush Date: Thu, 16 Nov 2023 00:53:27 +0000 Subject: [PATCH 1/6] Remove alloc for no_std compatibility --- .gitignore | 3 ++- Cargo.toml | 1 + src/expander.rs | 26 ++++++++++++++------------ src/lib.rs | 5 ++++- src/tests.rs | 2 +- 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index f32812d..3f1525b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea/* target/* -Cargo.lock \ No newline at end of file +Cargo.lock +.DS_Store \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index a7fe8ba..13eb1f4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ embedded-hal = { version = "0.2.7", features = ["unproven"] } bitmaps = { version = "3.1.0", default-features = false } cortex-m = { version = "0.7.4", optional = true } spin = { version = "0.9.8", optional = true } +heapless = "0.8.0" [dev-dependencies] mockall = "0.11.0" diff --git a/src/expander.rs b/src/expander.rs index c4531dd..fdbb2a0 100644 --- a/src/expander.rs +++ b/src/expander.rs @@ -83,14 +83,16 @@ //! expander.reverse_polarity(Bank0, Pin3, true).unwrap(); //! ``` + #[cfg(feature = "cortex-m")] use crate::guard::CsMutexGuard; use crate::guard::LockFreeGuard; #[cfg(feature = "spin")] use crate::guard::SpinGuard; use crate::pins::Pins; -use alloc::borrow::ToOwned; -use alloc::string::{String, ToString}; +// use alloc::borrow::ToOwned; +// use alloc::string::{String, ToString}; +use heapless::String; use bitmaps::Bitmap; use core::cell::RefCell; use core::fmt::{Debug, Formatter}; @@ -337,11 +339,11 @@ where match bank { Bank::Bank0 => self.bus.write( self.address, - &[COMMAND_CONF_0, self.configuration_0.as_value().to_owned()], + &[COMMAND_CONF_0, self.configuration_0.as_value().clone()], ), Bank::Bank1 => self.bus.write( self.address, - &[COMMAND_CONF_1, self.configuration_1.as_value().to_owned()], + &[COMMAND_CONF_1, self.configuration_1.as_value().clone()], ), } } @@ -351,10 +353,10 @@ where match bank { Bank::Bank0 => self .bus - .write(self.address, &[COMMAND_OUTPUT_0, self.output_0.as_value().to_owned()]), + .write(self.address, &[COMMAND_OUTPUT_0, self.output_0.as_value().clone()]), Bank::Bank1 => self .bus - .write(self.address, &[COMMAND_OUTPUT_1, self.output_1.as_value().to_owned()]), + .write(self.address, &[COMMAND_OUTPUT_1, self.output_1.as_value().clone()]), } } @@ -363,11 +365,11 @@ where match bank { Bank::Bank0 => self.bus.write( self.address, - &[COMMAND_POLARITY_0, self.polarity_0.as_value().to_owned()], + &[COMMAND_POLARITY_0, self.polarity_0.as_value().clone()], ), Bank::Bank1 => self.bus.write( self.address, - &[COMMAND_POLARITY_1, self.polarity_1.as_value().to_owned()], + &[COMMAND_POLARITY_1, self.polarity_1.as_value().clone()], ), } } @@ -391,11 +393,11 @@ impl + Write> Debug for RefreshInputError { } } -impl + Write> ToString for RefreshInputError { - fn to_string(&self) -> String { +impl + Write> RefreshInputError { + pub fn to_string(&self) -> String<9> { match self { - RefreshInputError::WriteError(_) => "WriteError".to_string(), - RefreshInputError::ReadError(_) => "ReadError".to_string(), + RefreshInputError::WriteError(_) => String::try_from("WriteError").unwrap(), + RefreshInputError::ReadError(_) => String::try_from("ReadError").unwrap(), } } } diff --git a/src/lib.rs b/src/lib.rs index 674e976..519632a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,7 +25,10 @@ #![cfg_attr(not(test), no_std)] #![cfg_attr(feature = "strict", deny(warnings))] -extern crate alloc; +#[cfg(test)] +compile_error!("Test included"); + +// extern crate alloc; extern crate embedded_hal; #[cfg(feature = "example")] diff --git a/src/tests.rs b/src/tests.rs index 95e48d2..733e67f 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -9,7 +9,7 @@ use crate::guard::SpinGuard; use crate::mocks::{BusMockBuilder, MockI2CBus, WriteError}; use crate::pin_refreshable::{RefreshableInputPin, RefreshableOutputPin}; use crate::pins::Pins; -use alloc::string::ToString; +// use alloc::string::ToString; use embedded_hal::digital::v2::{InputPin, IoPin, OutputPin, PinState, StatefulOutputPin, ToggleableOutputPin}; #[test] From 4bdcad6c1fb4943ca3c7e599a0cd0fa7dd349aa5 Mon Sep 17 00:00:00 2001 From: Ayush Date: Thu, 16 Nov 2023 02:15:14 +0000 Subject: [PATCH 2/6] Bugfix --- src/expander.rs | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/src/expander.rs b/src/expander.rs index fdbb2a0..ac21358 100644 --- a/src/expander.rs +++ b/src/expander.rs @@ -83,7 +83,6 @@ //! expander.reverse_polarity(Bank0, Pin3, true).unwrap(); //! ``` - #[cfg(feature = "cortex-m")] use crate::guard::CsMutexGuard; use crate::guard::LockFreeGuard; @@ -92,13 +91,13 @@ use crate::guard::SpinGuard; use crate::pins::Pins; // use alloc::borrow::ToOwned; // use alloc::string::{String, ToString}; -use heapless::String; use bitmaps::Bitmap; 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 heapless::String; #[cfg(feature = "spin")] use spin::Mutex as SpinMutex; @@ -337,14 +336,12 @@ where /// Writes the configuration register of the given bank fn write_conf(&mut self, bank: Bank) -> Result<(), ::Error> { match bank { - Bank::Bank0 => self.bus.write( - self.address, - &[COMMAND_CONF_0, self.configuration_0.as_value().clone()], - ), - Bank::Bank1 => self.bus.write( - self.address, - &[COMMAND_CONF_1, self.configuration_1.as_value().clone()], - ), + Bank::Bank0 => self + .bus + .write(self.address, &[COMMAND_CONF_0, self.configuration_0.as_value().clone()]), + Bank::Bank1 => self + .bus + .write(self.address, &[COMMAND_CONF_1, self.configuration_1.as_value().clone()]), } } @@ -363,14 +360,12 @@ where /// Writes the polarity register of the given bank fn write_polarity(&mut self, bank: Bank) -> Result<(), ::Error> { match bank { - Bank::Bank0 => self.bus.write( - self.address, - &[COMMAND_POLARITY_0, self.polarity_0.as_value().clone()], - ), - Bank::Bank1 => self.bus.write( - self.address, - &[COMMAND_POLARITY_1, self.polarity_1.as_value().clone()], - ), + Bank::Bank0 => self + .bus + .write(self.address, &[COMMAND_POLARITY_0, self.polarity_0.as_value().clone()]), + Bank::Bank1 => self + .bus + .write(self.address, &[COMMAND_POLARITY_1, self.polarity_1.as_value().clone()]), } } } @@ -394,7 +389,7 @@ impl + Write> Debug for RefreshInputError { } impl + Write> RefreshInputError { - pub fn to_string(&self) -> String<9> { + pub fn to_string(&self) -> String<10> { match self { RefreshInputError::WriteError(_) => String::try_from("WriteError").unwrap(), RefreshInputError::ReadError(_) => String::try_from("ReadError").unwrap(), From baf53013648c342c4df571535bf675a890d9eb99 Mon Sep 17 00:00:00 2001 From: Ayush Date: Thu, 16 Nov 2023 02:16:22 +0000 Subject: [PATCH 3/6] cargo fmt --- src/expander.rs | 31 ++++++++++++++++++------------- src/lib.rs | 3 --- src/tests.rs | 2 +- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/expander.rs b/src/expander.rs index ac21358..5e0f141 100644 --- a/src/expander.rs +++ b/src/expander.rs @@ -83,6 +83,7 @@ //! expander.reverse_polarity(Bank0, Pin3, true).unwrap(); //! ``` + #[cfg(feature = "cortex-m")] use crate::guard::CsMutexGuard; use crate::guard::LockFreeGuard; @@ -91,13 +92,13 @@ use crate::guard::SpinGuard; use crate::pins::Pins; // use alloc::borrow::ToOwned; // use alloc::string::{String, ToString}; +use heapless::String; use bitmaps::Bitmap; 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 heapless::String; #[cfg(feature = "spin")] use spin::Mutex as SpinMutex; @@ -336,12 +337,14 @@ where /// Writes the configuration register of the given bank fn write_conf(&mut self, bank: Bank) -> Result<(), ::Error> { match bank { - Bank::Bank0 => self - .bus - .write(self.address, &[COMMAND_CONF_0, self.configuration_0.as_value().clone()]), - Bank::Bank1 => self - .bus - .write(self.address, &[COMMAND_CONF_1, self.configuration_1.as_value().clone()]), + Bank::Bank0 => self.bus.write( + self.address, + &[COMMAND_CONF_0, self.configuration_0.as_value().clone()], + ), + Bank::Bank1 => self.bus.write( + self.address, + &[COMMAND_CONF_1, self.configuration_1.as_value().clone()], + ), } } @@ -360,12 +363,14 @@ where /// Writes the polarity register of the given bank fn write_polarity(&mut self, bank: Bank) -> Result<(), ::Error> { match bank { - Bank::Bank0 => self - .bus - .write(self.address, &[COMMAND_POLARITY_0, self.polarity_0.as_value().clone()]), - Bank::Bank1 => self - .bus - .write(self.address, &[COMMAND_POLARITY_1, self.polarity_1.as_value().clone()]), + Bank::Bank0 => self.bus.write( + self.address, + &[COMMAND_POLARITY_0, self.polarity_0.as_value().clone()], + ), + Bank::Bank1 => self.bus.write( + self.address, + &[COMMAND_POLARITY_1, self.polarity_1.as_value().clone()], + ), } } } diff --git a/src/lib.rs b/src/lib.rs index 519632a..b42c358 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,9 +25,6 @@ #![cfg_attr(not(test), no_std)] #![cfg_attr(feature = "strict", deny(warnings))] -#[cfg(test)] -compile_error!("Test included"); - // extern crate alloc; extern crate embedded_hal; diff --git a/src/tests.rs b/src/tests.rs index 733e67f..b8354e4 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -247,7 +247,7 @@ fn test_refresh_input_state_write_error() { let mut expander = PCA9539::new(i2c_bus, 0x74); let result = expander.refresh_input_state(Bank0); - assert_eq!("WriteError", result.unwrap_err().to_string()); + assert_eq!("WriteError", result.unwrap_err().to_string().as_str()); } #[test] From b51151b81952e600bd03fbb756976da47aa12bb0 Mon Sep 17 00:00:00 2001 From: Ayush Date: Mon, 11 Dec 2023 15:01:51 +0000 Subject: [PATCH 4/6] Clean up code --- src/expander.rs | 33 +++++++++++++-------------------- src/lib.rs | 1 - src/tests.rs | 1 - 3 files changed, 13 insertions(+), 22 deletions(-) diff --git a/src/expander.rs b/src/expander.rs index 5e0f141..e253462 100644 --- a/src/expander.rs +++ b/src/expander.rs @@ -83,22 +83,19 @@ //! expander.reverse_polarity(Bank0, Pin3, true).unwrap(); //! ``` - #[cfg(feature = "cortex-m")] use crate::guard::CsMutexGuard; use crate::guard::LockFreeGuard; #[cfg(feature = "spin")] use crate::guard::SpinGuard; use crate::pins::Pins; -// use alloc::borrow::ToOwned; -// use alloc::string::{String, ToString}; -use heapless::String; use bitmaps::Bitmap; 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 heapless::String; #[cfg(feature = "spin")] use spin::Mutex as SpinMutex; @@ -337,14 +334,12 @@ where /// Writes the configuration register of the given bank fn write_conf(&mut self, bank: Bank) -> Result<(), ::Error> { match bank { - Bank::Bank0 => self.bus.write( - self.address, - &[COMMAND_CONF_0, self.configuration_0.as_value().clone()], - ), - Bank::Bank1 => self.bus.write( - self.address, - &[COMMAND_CONF_1, self.configuration_1.as_value().clone()], - ), + Bank::Bank0 => self + .bus + .write(self.address, &[COMMAND_CONF_0, self.configuration_0.as_value().clone()]), + Bank::Bank1 => self + .bus + .write(self.address, &[COMMAND_CONF_1, self.configuration_1.as_value().clone()]), } } @@ -363,14 +358,12 @@ where /// Writes the polarity register of the given bank fn write_polarity(&mut self, bank: Bank) -> Result<(), ::Error> { match bank { - Bank::Bank0 => self.bus.write( - self.address, - &[COMMAND_POLARITY_0, self.polarity_0.as_value().clone()], - ), - Bank::Bank1 => self.bus.write( - self.address, - &[COMMAND_POLARITY_1, self.polarity_1.as_value().clone()], - ), + Bank::Bank0 => self + .bus + .write(self.address, &[COMMAND_POLARITY_0, self.polarity_0.as_value().clone()]), + Bank::Bank1 => self + .bus + .write(self.address, &[COMMAND_POLARITY_1, self.polarity_1.as_value().clone()]), } } } diff --git a/src/lib.rs b/src/lib.rs index b42c358..0e56239 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,7 +25,6 @@ #![cfg_attr(not(test), no_std)] #![cfg_attr(feature = "strict", deny(warnings))] -// extern crate alloc; extern crate embedded_hal; #[cfg(feature = "example")] diff --git a/src/tests.rs b/src/tests.rs index b8354e4..e65bb15 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -9,7 +9,6 @@ use crate::guard::SpinGuard; use crate::mocks::{BusMockBuilder, MockI2CBus, WriteError}; use crate::pin_refreshable::{RefreshableInputPin, RefreshableOutputPin}; use crate::pins::Pins; -// use alloc::string::ToString; use embedded_hal::digital::v2::{InputPin, IoPin, OutputPin, PinState, StatefulOutputPin, ToggleableOutputPin}; #[test] From 6c7ee93e1cb016f2cb417cc1c6f1831981411a0b Mon Sep 17 00:00:00 2001 From: Ayush Date: Tue, 12 Dec 2023 17:55:47 +0000 Subject: [PATCH 5/6] Trigger QA action pipeline on pull request --- .github/workflows/qa.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/qa.yaml b/.github/workflows/qa.yaml index 14acba4..bc10bf2 100644 --- a/.github/workflows/qa.yaml +++ b/.github/workflows/qa.yaml @@ -2,6 +2,7 @@ name: QA on: push: + pull_request: env: CARGO_TERM_COLOR: always From 0c85ce393b60c5929e7ef4d4d5990f74ca24e2fe Mon Sep 17 00:00:00 2001 From: Ayush Date: Tue, 12 Dec 2023 18:15:20 +0000 Subject: [PATCH 6/6] Replace clones with dereferences --- src/expander.rs | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/expander.rs b/src/expander.rs index e253462..6e3f5ec 100644 --- a/src/expander.rs +++ b/src/expander.rs @@ -336,34 +336,26 @@ where match bank { Bank::Bank0 => self .bus - .write(self.address, &[COMMAND_CONF_0, self.configuration_0.as_value().clone()]), + .write(self.address, &[COMMAND_CONF_0, *self.configuration_0.as_value()]), Bank::Bank1 => self .bus - .write(self.address, &[COMMAND_CONF_1, self.configuration_1.as_value().clone()]), + .write(self.address, &[COMMAND_CONF_1, *self.configuration_1.as_value()]), } } /// Writes the output register of the given bank pub fn write_output_state(&mut self, bank: Bank) -> Result<(), ::Error> { match bank { - Bank::Bank0 => self - .bus - .write(self.address, &[COMMAND_OUTPUT_0, self.output_0.as_value().clone()]), - Bank::Bank1 => self - .bus - .write(self.address, &[COMMAND_OUTPUT_1, self.output_1.as_value().clone()]), + 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()]), } } /// Writes the polarity register of the given bank fn write_polarity(&mut self, bank: Bank) -> Result<(), ::Error> { match bank { - Bank::Bank0 => self - .bus - .write(self.address, &[COMMAND_POLARITY_0, self.polarity_0.as_value().clone()]), - Bank::Bank1 => self - .bus - .write(self.address, &[COMMAND_POLARITY_1, self.polarity_1.as_value().clone()]), + 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()]), } } }