From e6df9f08a5ea657963a41035f66b01555dbe263e Mon Sep 17 00:00:00 2001 From: Moritz Bitsch Date: Mon, 19 Feb 2024 22:26:21 +0100 Subject: [PATCH 1/7] Update for embedded-hal 1.0 --- Cargo.toml | 6 +-- src/device_impl.rs | 111 +++++++++++++++++++++++++------------------ tests/common/mod.rs | 2 +- tests/integration.rs | 2 +- tests/one_shot.rs | 2 +- 5 files changed, 70 insertions(+), 53 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f98a972..8435d28 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,12 +21,12 @@ include = [ edition = "2018" [dependencies] -embedded-hal = "0.2.6" +embedded-hal = "1.0.0" nb = "1" [dev-dependencies] -linux-embedded-hal = "0.3" -embedded-hal-mock = "0.8" +linux-embedded-hal = "0.4.0" +embedded-hal-mock = "0.10.0" [profile.release] lto = true diff --git a/src/device_impl.rs b/src/device_impl.rs index 1fc0fc3..49c5519 100644 --- a/src/device_impl.rs +++ b/src/device_impl.rs @@ -3,7 +3,7 @@ use crate::{ InterruptPinPolarity, LuxRange, Measurement, ModeChangeError, Opt300x, PhantomData, SlaveAddr, Status, }; -use embedded_hal::blocking::i2c; +use embedded_hal::i2c; struct Register; impl Register { @@ -85,9 +85,9 @@ impl Opt300x { } } -impl Opt300x +impl Opt300x where - I2C: i2c::Write, + I2C: i2c::I2c, { /// Change into continuous measurement mode /// @@ -95,7 +95,7 @@ where /// after calling this method. pub fn into_continuous( mut self, - ) -> Result, ModeChangeError> { + ) -> Result, ModeChangeError> { if let Err(Error::I2C(e)) = self.set_config( self.config .with_high(BitFlags::MODE0) @@ -115,16 +115,16 @@ where } } -impl Opt300x +impl Opt300x where - I2C: i2c::Write, + I2C: i2c::I2c, { /// Change into one-shot mode /// /// This will actually shut down the device until a measurement is requested. pub fn into_one_shot( mut self, - ) -> Result, ModeChangeError> { + ) -> Result, ModeChangeError> { if let Err(Error::I2C(e)) = self.set_config( self.config .with_low(BitFlags::MODE0) @@ -144,19 +144,19 @@ where } } -impl Opt300x +impl Opt300x where - I2C: i2c::WriteRead, + I2C: i2c::I2c, { /// Read the result of the most recent light to digital conversion in lux - pub fn read_lux(&mut self) -> Result> { + pub fn read_lux(&mut self) -> Result> { let result = self.read_raw()?; Ok(raw_to_lux(result)) } /// Read the result of the most recent light to digital conversion in /// raw format: (exponent, mantissa) - pub fn read_raw(&mut self) -> Result<(u8, u16), Error> { + pub fn read_raw(&mut self) -> Result<(u8, u16), Error> { let result = self.read_register(Register::RESULT)?; Ok(((result >> 12) as u8, result & 0xFFF)) } @@ -166,12 +166,12 @@ fn raw_to_lux(result: (u8, u16)) -> f32 { (f64::from(1 << result.0) * 0.01 * f64::from(result.1)) as f32 } -impl Opt300x +impl Opt300x where - I2C: i2c::WriteRead + i2c::Write, + I2C: i2c::I2c, { /// Read the result of the most recent light to digital conversion in lux - pub fn read_lux(&mut self) -> nb::Result, Error> { + pub fn read_lux(&mut self) -> nb::Result, Error> { let measurement = self.read_raw()?; Ok(Measurement { result: raw_to_lux(measurement.result), @@ -181,7 +181,7 @@ where /// Read the result of the most recent light to digital conversion in /// raw format: (exponent, mantissa) - pub fn read_raw(&mut self) -> nb::Result, Error> { + pub fn read_raw(&mut self) -> nb::Result, Error> { if self.was_conversion_started { let status = self.read_status().map_err(nb::Error::Other)?; if status.conversion_ready { @@ -206,15 +206,15 @@ where } } -impl Opt300x +impl Opt300x where - I2C: i2c::WriteRead, + I2C: i2c::I2c, { /// Read the status of the conversion. /// /// Note that the conversion ready flag is cleared automatically /// after calling this method. - pub fn read_status(&mut self) -> Result> { + pub fn read_status(&mut self) -> Result> { let config = self.read_register(Register::CONFIG)?; Ok(Status { has_overflown: (config & BitFlags::OVF) != 0, @@ -225,15 +225,15 @@ where } } -impl Opt300x +impl Opt300x where - I2C: i2c::Write, + I2C: i2c::I2c, { /// Set the fault count /// /// Note that the conversion ready flag is cleared automatically /// after calling this method. - pub fn set_fault_count(&mut self, count: FaultCount) -> Result<(), Error> { + pub fn set_fault_count(&mut self, count: FaultCount) -> Result<(), Error> { let config = self.config.bits & !0b11; let config = match count { FaultCount::One => config, @@ -251,7 +251,7 @@ where /// /// Note that the conversion ready flag is cleared automatically /// after calling this method. - pub fn set_lux_range(&mut self, range: LuxRange) -> Result<(), Error> { + pub fn set_lux_range(&mut self, range: LuxRange) -> Result<(), Error> { let value = match range { LuxRange::Auto => Ok(0b1100), LuxRange::Manual(rn) if rn >= 0b1100 => Err(Error::InvalidInputData), @@ -267,7 +267,7 @@ where /// /// Note that the conversion ready flag is cleared automatically /// after calling this method. - pub fn set_integration_time(&mut self, time: IntegrationTime) -> Result<(), Error> { + pub fn set_integration_time(&mut self, time: IntegrationTime) -> Result<(), Error> { let config = match time { IntegrationTime::Ms100 => self.config.with_low(BitFlags::CT), IntegrationTime::Ms800 => self.config.with_high(BitFlags::CT), @@ -282,7 +282,7 @@ where pub fn set_interrupt_pin_polarity( &mut self, polarity: InterruptPinPolarity, - ) -> Result<(), Error> { + ) -> Result<(), Error> { let config = match polarity { InterruptPinPolarity::Low => self.config.with_low(BitFlags::POL), InterruptPinPolarity::High => self.config.with_high(BitFlags::POL), @@ -294,7 +294,7 @@ where /// /// Note that the conversion ready flag is cleared automatically /// after calling this method. - pub fn enable_exponent_masking(&mut self) -> Result<(), Error> { + pub fn enable_exponent_masking(&mut self) -> Result<(), Error> { self.set_config(self.config.with_high(BitFlags::ME)) } @@ -302,7 +302,7 @@ where /// /// Note that the conversion ready flag is cleared automatically /// after calling this method. - pub fn disable_exponent_masking(&mut self) -> Result<(), Error> { + pub fn disable_exponent_masking(&mut self) -> Result<(), Error> { self.set_config(self.config.with_low(BitFlags::ME)) } @@ -310,7 +310,7 @@ where /// /// Note that the conversion ready flag is cleared automatically /// after calling this method. - pub fn set_comparison_mode(&mut self, mode: ComparisonMode) -> Result<(), Error> { + pub fn set_comparison_mode(&mut self, mode: ComparisonMode) -> Result<(), Error> { let config = match mode { ComparisonMode::LatchedWindow => self.config.with_high(BitFlags::L), ComparisonMode::TransparentHysteresis => self.config.with_low(BitFlags::L), @@ -324,7 +324,11 @@ where /// 11 or a mantissa value greater than 4095. /// /// Note that this disables the end-of-conversion mode. - pub fn set_low_limit_raw(&mut self, exponent: u8, mantissa: u16) -> Result<(), Error> { + pub fn set_low_limit_raw( + &mut self, + exponent: u8, + mantissa: u16, + ) -> Result<(), Error> { if exponent > 0b1011 || mantissa > 0xFFF { return Err(Error::InvalidInputData); } @@ -338,7 +342,11 @@ where /// /// Returns `Error::InvalidInputData` for an exponent value greater than /// 11 or a mantissa value greater than 4095. - pub fn set_high_limit_raw(&mut self, exponent: u8, mantissa: u16) -> Result<(), Error> { + pub fn set_high_limit_raw( + &mut self, + exponent: u8, + mantissa: u16, + ) -> Result<(), Error> { if exponent > 0b1011 || mantissa > 0xFFF { return Err(Error::InvalidInputData); } @@ -350,7 +358,7 @@ where /// /// Note that this changes the two highest bits of the lux low limit exponent. /// Please see the device datasheet for further details. - pub fn enable_end_of_conversion_mode(&mut self) -> Result<(), Error> { + pub fn enable_end_of_conversion_mode(&mut self) -> Result<(), Error> { let limit = self.low_limit | 0b1100 << 12; self.write_register(Register::LOW_LIMIT, limit) } @@ -360,28 +368,28 @@ where /// Note that this restores the two highest bits of the lux low limit /// exponent to the last value set before enabling the end-of-conversion /// mode (0b00 by default). - pub fn disable_end_of_conversion_mode(&mut self) -> Result<(), Error> { + pub fn disable_end_of_conversion_mode(&mut self) -> Result<(), Error> { self.write_register(Register::LOW_LIMIT, self.low_limit) } } -impl Opt300x +impl Opt300x where - I2C: i2c::WriteRead, + I2C: i2c::I2c, { /// Read the manifacturer ID - pub fn get_manufacturer_id(&mut self) -> Result> { + pub fn get_manufacturer_id(&mut self) -> Result> { self.read_register(Register::MANUFACTURER_ID) } } -impl Opt300x +impl Opt300x where - I2C: i2c::WriteRead, + I2C: i2c::I2c, IC: marker::WithDeviceId, { /// Read the device ID - pub fn get_device_id(&mut self) -> Result> { + pub fn get_device_id(&mut self) -> Result> { self.read_register(Register::DEVICE_ID) } } @@ -405,11 +413,11 @@ impl Opt300x { } } -impl Opt300x +impl Opt300x where - I2C: i2c::WriteRead, + I2C: i2c::I2c, { - fn read_register(&mut self, register: u8) -> Result> { + fn read_register(&mut self, register: u8) -> Result> { let mut data = [0, 0]; self.i2c .write_read(self.address, &[register], &mut data) @@ -418,17 +426,17 @@ where } } -impl Opt300x +impl Opt300x where - I2C: i2c::Write, + I2C: i2c::I2c, { - fn set_config(&mut self, config: Config) -> Result<(), Error> { + fn set_config(&mut self, config: Config) -> Result<(), Error> { self.write_register(Register::CONFIG, config.bits)?; self.config = config; Ok(()) } - fn write_register(&mut self, register: u8, value: u16) -> Result<(), Error> { + fn write_register(&mut self, register: u8, value: u16) -> Result<(), Error> { let data = [register, (value >> 8) as u8, value as u8]; self.i2c.write(self.address, &data).map_err(Error::I2C) } @@ -436,12 +444,21 @@ where #[cfg(test)] mod tests { + use core::convert::Infallible; + use super::*; + impl embedded_hal::i2c::ErrorType for I2cMock { + type Error = Infallible; + } + struct I2cMock; - impl i2c::Write for I2cMock { - type Error = (); - fn write(&mut self, _addr: u8, _bytes: &[u8]) -> Result<(), Self::Error> { + impl i2c::I2c for I2cMock { + fn transaction( + &mut self, + _address: u8, + _operations: &mut [i2c::Operation<'_>], + ) -> Result<(), Self::Error> { Ok(()) } } diff --git a/tests/common/mod.rs b/tests/common/mod.rs index c412d6c..f9762fd 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -1,4 +1,4 @@ -use hal::i2c::{Mock as I2cMock, Transaction as I2cTrans}; +use hal::eh1::i2c::{Mock as I2cMock, Transaction as I2cTrans}; use opt300x::{ic, mode, Opt300x, SlaveAddr}; pub const DEV_ADDR: u8 = 0b100_0100; diff --git a/tests/integration.rs b/tests/integration.rs index bcc2498..5a46a6a 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -1,6 +1,6 @@ extern crate embedded_hal_mock as hal; extern crate opt300x; -use hal::i2c::{Mock as I2cMock, Transaction as I2cTrans}; +use hal::eh1::i2c::{Mock as I2cMock, Transaction as I2cTrans}; use opt300x::{ ComparisonMode, Error, FaultCount, IntegrationTime, InterruptPinPolarity, LuxRange, Opt300x, SlaveAddr, Status, diff --git a/tests/one_shot.rs b/tests/one_shot.rs index dd3ae19..2e0433c 100644 --- a/tests/one_shot.rs +++ b/tests/one_shot.rs @@ -2,7 +2,7 @@ extern crate embedded_hal_mock as hal; #[macro_use] extern crate nb; extern crate opt300x; -use hal::i2c::Transaction as I2cTrans; +use hal::eh1::i2c::Transaction as I2cTrans; use opt300x::Status; mod common; From 01dd86dd8c379cdb783240af9e6ee073169dd6a7 Mon Sep 17 00:00:00 2001 From: Moritz Bitsch Date: Fri, 23 Feb 2024 11:20:16 +0100 Subject: [PATCH 2/7] Update workflow with new MSRV version --- .github/workflows/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 856fc07..e99f6e3 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -13,7 +13,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - rust: [stable, 1.31.0] + rust: [stable, 1.63.0] TARGET: - x86_64-unknown-linux-gnu - x86_64-unknown-linux-musl @@ -85,7 +85,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - rust: [1.54.0] + rust: [1.63.0] TARGET: - x86_64-unknown-linux-gnu From 353b2d3b8f762e4df51b931850a17410f67ad447 Mon Sep 17 00:00:00 2001 From: Moritz Bitsch Date: Fri, 23 Feb 2024 15:43:46 +0100 Subject: [PATCH 3/7] Fix clippy warnings --- src/lib.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a41104b..2e43ac7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -286,7 +286,7 @@ pub enum SlaveAddr { /// Fault count /// /// Number of consecutive fault events necessary to trigger interrupt. -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum FaultCount { /// One (default) One, @@ -299,7 +299,7 @@ pub enum FaultCount { } /// Interrupt pin polarity (active state) -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum InterruptPinPolarity { /// Active low (default) Low, @@ -308,7 +308,7 @@ pub enum InterruptPinPolarity { } /// Lux range -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum LuxRange { /// Manual [0-11] Manual(u8), @@ -317,7 +317,7 @@ pub enum LuxRange { } /// Integration time -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum IntegrationTime { /// 100 ms Ms100, @@ -326,7 +326,7 @@ pub enum IntegrationTime { } /// Result comparison mode for interrupt reporting -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum ComparisonMode { /// Latched window-style LatchedWindow, @@ -335,7 +335,7 @@ pub enum ComparisonMode { } /// Conversion status -#[derive(Debug, Clone, Copy, PartialEq, Default)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] pub struct Status { /// Whether an overflow condition during the conversion has occurred. pub has_overflown: bool, @@ -348,7 +348,7 @@ pub struct Status { } /// One-shot measurement -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct Measurement { /// Result pub result: T, From cea08c438e2219e16dc3b5002e613ebfacd2193f Mon Sep 17 00:00:00 2001 From: Moritz Bitsch Date: Fri, 23 Feb 2024 12:04:19 +0100 Subject: [PATCH 4/7] Update CHANGELOG.md --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e8b973..be8e47d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] -... +### Changed +- Update to embedded-hal 1.0 (Bumped MSRV to 1.62.0) ## [0.1.1] - 2021-09-10 From c43d44a9461b43cdfe1d33237cdeb52fb8932356 Mon Sep 17 00:00:00 2001 From: Moritz Bitsch Date: Fri, 23 Feb 2024 16:01:41 +0100 Subject: [PATCH 5/7] Use cargo-llvm-cov for coverage cargo-llvm-cov is more acurate than tarpaulin --- .github/workflows/build.yml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e99f6e3..4301d13 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -139,10 +139,11 @@ jobs: toolchain: stable override: true - - name: Run cargo-tarpaulin - uses: actions-rs/tarpaulin@v0.1 - with: - args: '--out Lcov -- --test-threads 1' + - name: Install cargo-llvm-cov + uses: taiki-e/install-action@cargo-llvm-cov + + - name: Generate code coverage + run: cargo llvm-cov --all-features --workspace --lcov --output-path lcov.info - name: upload to Coveralls uses: coverallsapp/github-action@master From 32bc3c4894accc9fc073da2be1a176e872ad00f0 Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Sat, 24 Feb 2024 20:32:43 +0100 Subject: [PATCH 6/7] Avoid embedded-hal 0.2 dependency --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 8435d28..68e362f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ nb = "1" [dev-dependencies] linux-embedded-hal = "0.4.0" -embedded-hal-mock = "0.10.0" +embedded-hal-mock = {version="0.10.0", default-features=false, features=["eh1"]} [profile.release] lto = true From eebf956fc87506d1bd08a2a66a5b8b3f2e9fe1ba Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Sat, 24 Feb 2024 21:50:42 +0100 Subject: [PATCH 7/7] Use 1.62.0 MSRV --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 4301d13..cceec16 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -13,7 +13,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - rust: [stable, 1.63.0] + rust: [stable, 1.62.0] TARGET: - x86_64-unknown-linux-gnu - x86_64-unknown-linux-musl