From 6411543345785479975e27d151c1ea3483ce6df4 Mon Sep 17 00:00:00 2001 From: JadKHaddad Date: Sat, 20 Apr 2024 12:28:30 +0200 Subject: [PATCH 1/9] Changed compute_b5 to return Option Signed-off-by: JadKHaddad --- bmp180/src/device.rs | 35 ++++++++++++++++++++++------------- bmp180/src/lib.rs | 13 +++++++++++++ 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/bmp180/src/device.rs b/bmp180/src/device.rs index 4809b00..6f24343 100644 --- a/bmp180/src/device.rs +++ b/bmp180/src/device.rs @@ -28,7 +28,7 @@ pub mod module { use crate::{ address::Address, calibration::Calibration, error::BMP180Error, id::Id, mode::Mode, - register::Register, tri, + register::Register, tri, tri_opt, }; /// Builder for an uninitialized `BMP180` device. @@ -227,7 +227,7 @@ pub mod module { } /// Compute B5 value. - fn compute_b5(&self, raw_temperature: i16) -> i32 { + fn compute_b5(&self, raw_temperature: i16) -> Option { let calibration = self.calibration(); let x1 = ((raw_temperature as i32 - calibration.ac6 as i32) @@ -235,12 +235,12 @@ pub mod module { >> 15; let x2 = ((calibration.mc as i32) << 11) / (x1 + calibration.md as i32); - x1 + x2 + Some(x1 + x2) } /// Compute true temprature in `0.1 C`. - fn compute_temperature(&self, raw_temperature: i16) -> i32 { - let b5 = self.compute_b5(raw_temperature); + fn compute_temperature(&self, raw_temperature: i16) -> Option { + let b5 = tri_opt!(self.compute_b5(raw_temperature)); #[cfg(feature = "defmt")] { @@ -256,11 +256,11 @@ pub mod module { log::debug!("B5: {}", b5); } - (b5 + 8) >> 4 + Some((b5 + 8) >> 4) } /// Compute true pressure in `Pa`. - fn compute_pressure(&self, raw_temperature: i16, raw_pressure: i32) -> i32 { + fn compute_pressure(&self, raw_temperature: i16, raw_pressure: i32) -> Option { let calibration = self.calibration(); let mode = self.mode(); @@ -278,7 +278,7 @@ pub mod module { log::debug!("Raw pressure: {}", raw_pressure); } - let b5 = self.compute_b5(raw_temperature); + let b5 = tri_opt!(self.compute_b5(raw_temperature)); let b6 = b5 - 4000; let x1 = (calibration.b2 as i32 * ((b6 * b6) >> 12)) >> 11; @@ -358,7 +358,7 @@ pub mod module { log::debug!("P: {}", p); } - p + ((x1 + x2 + 3791_i32) >> 4) + Some(p + ((x1 + x2 + 3791_i32) >> 4)) } /// Pressure in `Pa` at sea level. @@ -448,7 +448,9 @@ pub mod module { pub async fn update_temperature(&mut self) -> Result<(), BMP180Error> { let raw_temperature = tri!(self.read_raw_temperature().await); - self.temperature = self.compute_temperature(raw_temperature); + self.temperature = tri!(self + .compute_temperature(raw_temperature) + .ok_or(BMP180Error::Arithmetic)); Ok(()) } @@ -458,7 +460,9 @@ pub mod module { let raw_temperature = tri!(self.read_raw_temperature().await); let raw_pressure = tri!(self.read_raw_pressure().await); - self.pressure = self.compute_pressure(raw_temperature, raw_pressure); + self.pressure = tri!(self + .compute_pressure(raw_temperature, raw_pressure) + .ok_or(BMP180Error::Arithmetic)); Ok(()) } @@ -468,8 +472,13 @@ pub mod module { let raw_temperature = tri!(self.read_raw_temperature().await); let raw_pressure = tri!(self.read_raw_pressure().await); - self.temperature = self.compute_temperature(raw_temperature); - self.pressure = self.compute_pressure(raw_temperature, raw_pressure); + self.temperature = tri!(self + .compute_temperature(raw_temperature) + .ok_or(BMP180Error::Arithmetic)); + + self.pressure = tri!(self + .compute_pressure(raw_temperature, raw_pressure) + .ok_or(BMP180Error::Arithmetic)); Ok(()) } diff --git a/bmp180/src/lib.rs b/bmp180/src/lib.rs index d69b2a5..1396814 100644 --- a/bmp180/src/lib.rs +++ b/bmp180/src/lib.rs @@ -53,4 +53,17 @@ macro_rules! tri { }; } +/// Just like [`tri!`] but for [`core::option::Option`]. +macro_rules! tri_opt { + ($e:expr $(,)?) => { + match $e { + core::option::Option::Some(value) => value, + core::option::Option::None => { + return core::option::Option::None; + } + } + }; +} + use tri; +use tri_opt; From 135bb23d0edaa1e5fbe78523f65539122cca5bc9 Mon Sep 17 00:00:00 2001 From: JadKHaddad Date: Sat, 20 Apr 2024 12:33:41 +0200 Subject: [PATCH 2/9] Added feature: disable-arithmetic-checks Signed-off-by: JadKHaddad --- bmp180/Cargo.toml | 1 + bmp180/src/device.rs | 273 ++++++++++++++++++++++++------------------- 2 files changed, 152 insertions(+), 122 deletions(-) diff --git a/bmp180/Cargo.toml b/bmp180/Cargo.toml index a57e56e..3d3105f 100644 --- a/bmp180/Cargo.toml +++ b/bmp180/Cargo.toml @@ -13,6 +13,7 @@ readme = "../README.md" default = ["async", "impl-debug"] async = ["dep:embedded-hal-async"] blocking = ["dep:embedded-hal"] +disable-arithmetic-checks = [] log = ["dep:log"] defmt = ["dep:defmt"] i-know-what-i-am-doing = [] diff --git a/bmp180/src/device.rs b/bmp180/src/device.rs index 6f24343..89b7900 100644 --- a/bmp180/src/device.rs +++ b/bmp180/src/device.rs @@ -226,6 +226,157 @@ pub mod module { self.pressure } + /// Pressure in `Pa` at sea level. + pub fn sea_level_pressure(&self, altitude_meters: f32) -> i32 { + let pressure = self.pressure() as f32; + + (pressure / libm::powf(1.0 - altitude_meters / 44330.0, 5.255)) as i32 + } + + /// Altitude in meters. + /// + /// Standard pressure at sea level is `101325 Pa`. + pub fn altitude(&self, sea_level_pressure: f32) -> f32 { + let pressure = self.pressure(); + + 44330.0 * (1.0 - libm::powf(pressure as f32 / sea_level_pressure, 0.1903)) + } + + /// Read raw temperature. + async fn read_raw_temperature(&mut self) -> Result> { + tri!(self + .i2c + .write( + self.addr_u8(), + &[Register::Control as u8, Register::ReadTempCmd as u8] + ) + .await + .map_err(BMP180Error::I2C)); + + self.delay.delay_ms(5).await; + + let mut data = [0u8; 2]; + + tri!(self + .i2c + .write_read( + self.addr_u8(), + &[Register::TempPressureData as u8], + &mut data + ) + .await + .map_err(BMP180Error::I2C)); + + let raw_temperature = ((data[0] as i16) << 8) | data[1] as i16; + + Ok(raw_temperature) + } + + /// Read raw pressure. + async fn read_raw_pressure(&mut self) -> Result> { + let mode = self.mode(); + + tri!(self + .i2c + .write( + self.addr_u8(), + &[ + Register::Control as u8, + Register::ReadPressureCmd as u8 + ((mode as u8) << 6) + ], + ) + .await + .map_err(BMP180Error::I2C)); + + self.delay.delay_ms(mode.delay_ms()).await; + + let mut data = [0u8; 3]; + + tri!(self + .i2c + .write_read( + self.addr_u8(), + &[Register::TempPressureData as u8], + &mut data + ) + .await + .map_err(BMP180Error::I2C)); + + let raw_pressure = + (((data[0] as i32) << 16) + ((data[1] as i32) << 8) + data[2] as i32) + >> (8 - mode as u8); + + Ok(raw_pressure) + } + + /// Update temperature in `self`. + pub async fn update_temperature(&mut self) -> Result<(), BMP180Error> { + let raw_temperature = tri!(self.read_raw_temperature().await); + + self.temperature = tri!(self + .compute_temperature(raw_temperature) + .ok_or(BMP180Error::Arithmetic)); + + Ok(()) + } + + /// Update pressure in `self`. + pub async fn update_pressure(&mut self) -> Result<(), BMP180Error> { + let raw_temperature = tri!(self.read_raw_temperature().await); + let raw_pressure = tri!(self.read_raw_pressure().await); + + self.pressure = tri!(self + .compute_pressure(raw_temperature, raw_pressure) + .ok_or(BMP180Error::Arithmetic)); + + Ok(()) + } + + /// Update both temperature and pressure in `self`. + pub async fn update(&mut self) -> Result<(), BMP180Error> { + let raw_temperature = tri!(self.read_raw_temperature().await); + let raw_pressure = tri!(self.read_raw_pressure().await); + + self.temperature = tri!(self + .compute_temperature(raw_temperature) + .ok_or(BMP180Error::Arithmetic)); + + self.pressure = tri!(self + .compute_pressure(raw_temperature, raw_pressure) + .ok_or(BMP180Error::Arithmetic)); + + Ok(()) + } + } + + #[cfg(not(feature = "disable-arithmetic-checks"))] + impl BMP180 + where + I2C: i2c_trait, + DELAY: delay_trait, + { + /// Compute B5 value. + fn compute_b5(&self, raw_temperature: i16) -> Option { + todo!() + } + + /// Compute true temprature in `0.1 C`. + fn compute_temperature(&self, raw_temperature: i16) -> Option { + todo!() + } + + /// Compute true pressure in `Pa`. + fn compute_pressure(&self, raw_temperature: i16, raw_pressure: i32) -> Option { + todo!() + } + } + + #[cfg(feature = "disable-arithmetic-checks")] + impl BMP180 + where + I2C: i2c_trait, + DELAY: delay_trait, + { /// Compute B5 value. fn compute_b5(&self, raw_temperature: i16) -> Option { let calibration = self.calibration(); @@ -360,128 +511,6 @@ pub mod module { Some(p + ((x1 + x2 + 3791_i32) >> 4)) } - - /// Pressure in `Pa` at sea level. - pub fn sea_level_pressure(&self, altitude_meters: f32) -> i32 { - let pressure = self.pressure() as f32; - - (pressure / libm::powf(1.0 - altitude_meters / 44330.0, 5.255)) as i32 - } - - /// Altitude in meters. - /// - /// Standard pressure at sea level is `101325 Pa`. - pub fn altitude(&self, sea_level_pressure: f32) -> f32 { - let pressure = self.pressure(); - - 44330.0 * (1.0 - libm::powf(pressure as f32 / sea_level_pressure, 0.1903)) - } - - /// Read raw temperature. - async fn read_raw_temperature(&mut self) -> Result> { - tri!(self - .i2c - .write( - self.addr_u8(), - &[Register::Control as u8, Register::ReadTempCmd as u8] - ) - .await - .map_err(BMP180Error::I2C)); - - self.delay.delay_ms(5).await; - - let mut data = [0u8; 2]; - - tri!(self - .i2c - .write_read( - self.addr_u8(), - &[Register::TempPressureData as u8], - &mut data - ) - .await - .map_err(BMP180Error::I2C)); - - let raw_temperature = ((data[0] as i16) << 8) | data[1] as i16; - - Ok(raw_temperature) - } - - /// Read raw pressure. - async fn read_raw_pressure(&mut self) -> Result> { - let mode = self.mode(); - - tri!(self - .i2c - .write( - self.addr_u8(), - &[ - Register::Control as u8, - Register::ReadPressureCmd as u8 + ((mode as u8) << 6) - ], - ) - .await - .map_err(BMP180Error::I2C)); - - self.delay.delay_ms(mode.delay_ms()).await; - - let mut data = [0u8; 3]; - - tri!(self - .i2c - .write_read( - self.addr_u8(), - &[Register::TempPressureData as u8], - &mut data - ) - .await - .map_err(BMP180Error::I2C)); - - let raw_pressure = - (((data[0] as i32) << 16) + ((data[1] as i32) << 8) + data[2] as i32) - >> (8 - mode as u8); - - Ok(raw_pressure) - } - - /// Update temperature in `self`. - pub async fn update_temperature(&mut self) -> Result<(), BMP180Error> { - let raw_temperature = tri!(self.read_raw_temperature().await); - - self.temperature = tri!(self - .compute_temperature(raw_temperature) - .ok_or(BMP180Error::Arithmetic)); - - Ok(()) - } - - /// Update pressure in `self`. - pub async fn update_pressure(&mut self) -> Result<(), BMP180Error> { - let raw_temperature = tri!(self.read_raw_temperature().await); - let raw_pressure = tri!(self.read_raw_pressure().await); - - self.pressure = tri!(self - .compute_pressure(raw_temperature, raw_pressure) - .ok_or(BMP180Error::Arithmetic)); - - Ok(()) - } - - /// Update both temperature and pressure in `self`. - pub async fn update(&mut self) -> Result<(), BMP180Error> { - let raw_temperature = tri!(self.read_raw_temperature().await); - let raw_pressure = tri!(self.read_raw_pressure().await); - - self.temperature = tri!(self - .compute_temperature(raw_temperature) - .ok_or(BMP180Error::Arithmetic)); - - self.pressure = tri!(self - .compute_pressure(raw_temperature, raw_pressure) - .ok_or(BMP180Error::Arithmetic)); - - Ok(()) - } } #[cfg(feature = "i-know-what-i-am-doing")] From fc9f4246fa2be9e2c07279be9f9dbb8732ac1f22 Mon Sep 17 00:00:00 2001 From: JadKHaddad Date: Sun, 21 Apr 2024 10:12:41 +0200 Subject: [PATCH 3/9] impl checked_compute_b5 and checked_compute_temperature Signed-off-by: JadKHaddad --- bmp180/src/device.rs | 47 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/bmp180/src/device.rs b/bmp180/src/device.rs index 89b7900..fd90b46 100644 --- a/bmp180/src/device.rs +++ b/bmp180/src/device.rs @@ -357,12 +357,47 @@ pub mod module { { /// Compute B5 value. fn compute_b5(&self, raw_temperature: i16) -> Option { - todo!() + let calibration = self.calibration(); + + let rt = raw_temperature as i32; + let ac6 = calibration.ac6 as i32; + let ac5 = calibration.ac5 as i32; + let mc = calibration.mc as i32; + let md = calibration.md as i32; + + let x1 = tri_opt!(rt.checked_sub(ac6)); + let x1 = tri_opt!(x1.checked_mul(ac5)); + let x1 = tri_opt!(x1.checked_shr(15)); + + let x2 = tri_opt!(mc.checked_shl(11)); + let x = tri_opt!(x1.checked_add(md)); + let x2 = tri_opt!(x2.checked_div(x)); + + Some(x1 + x2) } /// Compute true temprature in `0.1 C`. fn compute_temperature(&self, raw_temperature: i16) -> Option { - todo!() + let b5 = tri_opt!(self.compute_b5(raw_temperature)); + + #[cfg(feature = "defmt")] + { + defmt::debug!("Computing temperature"); + defmt::debug!("Raw temperature: {}", raw_temperature); + defmt::debug!("B5: {}", b5); + } + + #[cfg(feature = "log")] + { + log::debug!("Computing temperature"); + log::debug!("Raw temperature: {}", raw_temperature); + log::debug!("B5: {}", b5); + } + + let temperature = tri_opt!(b5.checked_add(8)); + let temperature = tri_opt!(temperature.checked_shr(4)); + + Some(temperature) } /// Compute true pressure in `Pa`. @@ -491,10 +526,10 @@ pub mod module { let x1 = (x1 * 3038) >> 16; let x2 = (-7357 * p) >> 16; + let p = p + ((x1 + x2 + 3791_i32) >> 4); + #[cfg(feature = "defmt")] { - let p = p + ((x1 + x2 + 3791_i32) >> 4); - defmt::debug!("X1: {}", x1); defmt::debug!("X2: {}", x2); defmt::debug!("P: {}", p); @@ -502,14 +537,12 @@ pub mod module { #[cfg(feature = "log")] { - let p = p + ((x1 + x2 + 3791_i32) >> 4); - log::debug!("X1: {}", x1); log::debug!("X2: {}", x2); log::debug!("P: {}", p); } - Some(p + ((x1 + x2 + 3791_i32) >> 4)) + Some(p) } } From da1ee8901041e4ebe17795dbea12498dfbcc9e58 Mon Sep 17 00:00:00 2001 From: JadKHaddad Date: Fri, 26 Apr 2024 20:45:44 +0200 Subject: [PATCH 4/9] use ? instead of tri_opt! for options Signed-off-by: JadKHaddad --- bmp180/src/device.rs | 16 ++++++---------- bmp180/src/lib.rs | 13 ------------- 2 files changed, 6 insertions(+), 23 deletions(-) diff --git a/bmp180/src/device.rs b/bmp180/src/device.rs index fd90b46..46b1284 100644 --- a/bmp180/src/device.rs +++ b/bmp180/src/device.rs @@ -28,7 +28,7 @@ pub mod module { use crate::{ address::Address, calibration::Calibration, error::BMP180Error, id::Id, mode::Mode, - register::Register, tri, tri_opt, + register::Register, tri, }; /// Builder for an uninitialized `BMP180` device. @@ -365,20 +365,16 @@ pub mod module { let mc = calibration.mc as i32; let md = calibration.md as i32; - let x1 = tri_opt!(rt.checked_sub(ac6)); - let x1 = tri_opt!(x1.checked_mul(ac5)); - let x1 = tri_opt!(x1.checked_shr(15)); + let x1 = rt.checked_sub(ac6)?.checked_mul(ac5)?.checked_shr(15)?; - let x2 = tri_opt!(mc.checked_shl(11)); - let x = tri_opt!(x1.checked_add(md)); - let x2 = tri_opt!(x2.checked_div(x)); + let x2 = mc.checked_shl(11)?.checked_div(x1.checked_add(md)?)?; Some(x1 + x2) } /// Compute true temprature in `0.1 C`. fn compute_temperature(&self, raw_temperature: i16) -> Option { - let b5 = tri_opt!(self.compute_b5(raw_temperature)); + let b5 = self.compute_b5(raw_temperature)?; #[cfg(feature = "defmt")] { @@ -394,8 +390,8 @@ pub mod module { log::debug!("B5: {}", b5); } - let temperature = tri_opt!(b5.checked_add(8)); - let temperature = tri_opt!(temperature.checked_shr(4)); + let temperature = b5.checked_add(8)?; + let temperature = temperature.checked_shr(4)?; Some(temperature) } diff --git a/bmp180/src/lib.rs b/bmp180/src/lib.rs index 1396814..d69b2a5 100644 --- a/bmp180/src/lib.rs +++ b/bmp180/src/lib.rs @@ -53,17 +53,4 @@ macro_rules! tri { }; } -/// Just like [`tri!`] but for [`core::option::Option`]. -macro_rules! tri_opt { - ($e:expr $(,)?) => { - match $e { - core::option::Option::Some(value) => value, - core::option::Option::None => { - return core::option::Option::None; - } - } - }; -} - use tri; -use tri_opt; From 540103574559649bb436148a47a239cd3f2e50d3 Mon Sep 17 00:00:00 2001 From: JadKHaddad Date: Sun, 28 Apr 2024 18:45:56 +0200 Subject: [PATCH 5/9] use ? instead of tri_opt! for options in disable-arithmetic-checks feature Signed-off-by: JadKHaddad --- bmp180/src/device.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bmp180/src/device.rs b/bmp180/src/device.rs index 46b1284..9f8cc89 100644 --- a/bmp180/src/device.rs +++ b/bmp180/src/device.rs @@ -422,7 +422,7 @@ pub mod module { /// Compute true temprature in `0.1 C`. fn compute_temperature(&self, raw_temperature: i16) -> Option { - let b5 = tri_opt!(self.compute_b5(raw_temperature)); + let b5 = self.compute_b5(raw_temperature)?; #[cfg(feature = "defmt")] { @@ -460,7 +460,7 @@ pub mod module { log::debug!("Raw pressure: {}", raw_pressure); } - let b5 = tri_opt!(self.compute_b5(raw_temperature)); + let b5 = self.compute_b5(raw_temperature)?; let b6 = b5 - 4000; let x1 = (calibration.b2 as i32 * ((b6 * b6) >> 12)) >> 11; From 0bb066be8fc8fdc2d0693d86da6374f27de25773 Mon Sep 17 00:00:00 2001 From: JadKHaddad Date: Sun, 28 Apr 2024 19:05:16 +0200 Subject: [PATCH 6/9] compute_pressure is noew checked Signed-off-by: JadKHaddad --- bmp180/src/device.rs | 121 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 120 insertions(+), 1 deletion(-) diff --git a/bmp180/src/device.rs b/bmp180/src/device.rs index 9f8cc89..16abc61 100644 --- a/bmp180/src/device.rs +++ b/bmp180/src/device.rs @@ -398,7 +398,126 @@ pub mod module { /// Compute true pressure in `Pa`. fn compute_pressure(&self, raw_temperature: i16, raw_pressure: i32) -> Option { - todo!() + let calibration = self.calibration(); + let mode = self.mode(); + + #[cfg(feature = "defmt")] + { + defmt::debug!("Computing pressure"); + defmt::debug!("Raw temperature: {}", raw_temperature); + defmt::debug!("Raw pressure: {}", raw_pressure); + } + + #[cfg(feature = "log")] + { + log::debug!("Computing pressure"); + log::debug!("Raw temperature: {}", raw_temperature); + log::debug!("Raw pressure: {}", raw_pressure); + } + + let b2 = calibration.b2 as i32; + let ac2 = calibration.ac2 as i32; + let ac1 = calibration.ac1 as i32; + + let b5 = self.compute_b5(raw_temperature)?; + + let b6 = b5.checked_sub(4000)?; + let x1 = b2 + .checked_mul(b6.checked_mul(b6)?.checked_shr(12)?)? + .checked_shr(11)?; + let x2 = ac2.checked_mul(b6)?.checked_shr(11)?; + let x3 = x1.checked_add(x2)?; + let b3 = ac1 + .checked_mul(4)? + .checked_add(x3)? + // in "disable-arithmetic-checks" it is: mode as u8 + .checked_shl(mode as u32)? + .checked_add(2)? + .checked_div(4)?; + + #[cfg(feature = "defmt")] + { + defmt::debug!("B5: {}", b5); + defmt::debug!("B6: {}", b6); + defmt::debug!("X1: {}", x1); + defmt::debug!("X2: {}", x2); + defmt::debug!("X3: {}", x3); + defmt::debug!("B3: {}", b3); + } + + #[cfg(feature = "log")] + { + log::debug!("B5: {}", b5); + log::debug!("B6: {}", b6); + log::debug!("X1: {}", x1); + log::debug!("X2: {}", x2); + log::debug!("X3: {}", x3); + log::debug!("B3: {}", b3); + } + + let ac3 = calibration.ac3 as i32; + let b1 = calibration.b1 as i32; + let ac4 = calibration.ac4 as u32; + + let x1 = ac3.checked_mul(b6)?.checked_shr(13)?; + let x2 = b1 + .checked_mul(b6.checked_mul(b6)?.checked_shr(12)?)? + .checked_shr(16)?; + let x3 = x1.checked_add(x2)?.checked_add(2)?.checked_shr(2)?; + let b4 = ac4 + .checked_mul(x3.checked_add(32768)? as u32)? + .checked_shr(15)?; + let b7 = (raw_pressure as u32) + .checked_sub(b3 as u32)? + // in "disable-arithmetic-checks" it is: mode as u8 + .checked_mul(50000_u32.checked_shr(mode as u32)?)?; + + #[cfg(feature = "defmt")] + { + defmt::debug!("X1: {}", x1); + defmt::debug!("X2: {}", x2); + defmt::debug!("X3: {}", x3); + defmt::debug!("B4: {}", b4); + defmt::debug!("B7: {}", b7); + } + + #[cfg(feature = "log")] + { + log::debug!("X1: {}", x1); + log::debug!("X2: {}", x2); + log::debug!("X3: {}", x3); + log::debug!("B4: {}", b4); + log::debug!("B7: {}", b7); + } + + let p = if b7 < 0x80000000 { + b7.checked_mul(2)?.checked_div(b4)? + } else { + b7.checked_div(b4)?.checked_mul(2)? + } as i32; + + let x1 = p.checked_shr(8)?.checked_mul(p.checked_shr(8)?)?; + let x1 = x1.checked_mul(3038)?.checked_shr(16)?; + let x2 = -7357_i32.checked_mul(p)?.checked_shr(16)?; + + let p = + p.checked_add(x1.checked_add(x2)?.checked_add(3791_i32)?.checked_shr(4)?)?; + + #[cfg(feature = "defmt")] + { + defmt::debug!("X1: {}", x1); + defmt::debug!("X2: {}", x2); + defmt::debug!("P: {}", p); + } + + #[cfg(feature = "log")] + { + log::debug!("X1: {}", x1); + log::debug!("X2: {}", x2); + log::debug!("P: {}", p); + } + + Some(p) } } From 77b154fe496ca9aff110b23900ce80a1120251e3 Mon Sep 17 00:00:00 2001 From: JadKHaddad Date: Thu, 2 May 2024 21:01:45 +0200 Subject: [PATCH 7/9] added workspace Signed-off-by: JadKHaddad --- .gitignore | 1 + Cargo.lock | 191 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 5 ++ 3 files changed, 197 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..7248e17 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,191 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bmp180" +version = "0.1.0" +dependencies = [ + "defmt", + "duplicate", + "embedded-hal", + "embedded-hal-async", + "libm", + "log", +] + +[[package]] +name = "defmt" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3939552907426de152b3c2c6f51ed53f98f448babd26f28694c95f5906194595" +dependencies = [ + "bitflags", + "defmt-macros", +] + +[[package]] +name = "defmt-macros" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18bdc7a7b92ac413e19e95240e75d3a73a8d8e78aa24a594c22cbb4d44b4bbda" +dependencies = [ + "defmt-parser", + "proc-macro-error", + "proc-macro2", + "quote", + "syn 2.0.60", +] + +[[package]] +name = "defmt-parser" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff4a5fefe330e8d7f31b16a318f9ce81000d8e35e69b93eae154d16d2278f70f" +dependencies = [ + "thiserror", +] + +[[package]] +name = "duplicate" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de78e66ac9061e030587b2a2e75cc88f22304913c907b11307bca737141230cb" +dependencies = [ + "heck", + "proc-macro-error", +] + +[[package]] +name = "embedded-hal" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "361a90feb7004eca4019fb28352a9465666b24f840f5c3cddf0ff13920590b89" + +[[package]] +name = "embedded-hal-async" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c4c685bbef7fe13c3c6dd4da26841ed3980ef33e841cddfa15ce8a8fb3f1884" +dependencies = [ + "embedded-hal", +] + +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + +[[package]] +name = "libm" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" + +[[package]] +name = "log" +version = "0.4.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn 1.0.109", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d1597b0c024618f09a9c3b8655b7e430397a36d23fdafec26d6965e9eec3eba" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "909518bc7b1c9b779f1bbf07f2929d35af9f0f37e47c6e9ef7f9dddc1e1821f3" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "1.0.59" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0126ad08bff79f29fc3ae6a55cc72352056dfff61e3ff8bb7129476d44b23aa" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.59" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1cd413b5d558b4c5bf3680e324a6fa5014e7b7c067a51e69dbdf47eb7148b66" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.60", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..92c1313 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,5 @@ +[workspace] +members = ["bmp180"] +exclude = ["examples/esp32", "examples/blue-pill"] +resolver = "2" +default-members = ["bmp180"] From eca96aa992755915122b40bcc879855bd3689061 Mon Sep 17 00:00:00 2001 From: JadKHaddad Date: Fri, 3 May 2024 19:55:52 +0200 Subject: [PATCH 8/9] updated init_update fuzz target Signed-off-by: JadKHaddad --- bmp180/fuzz/fuzz_targets/init_update.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bmp180/fuzz/fuzz_targets/init_update.rs b/bmp180/fuzz/fuzz_targets/init_update.rs index 3a22025..bd7e9ab 100644 --- a/bmp180/fuzz/fuzz_targets/init_update.rs +++ b/bmp180/fuzz/fuzz_targets/init_update.rs @@ -13,7 +13,7 @@ fuzz_target!(|data: &[u8]| { let mut bmp180 = UninitBMP180::builder(fuzz_i2c, FuzzDelay {}) .build() .initialize() - .unwrap(); + .expect("Could not initialize BMP180"); - bmp180.update().unwrap(); + bmp180.update().ok(); }); From 39616f0d57f15273470f656d24e6558801242254 Mon Sep 17 00:00:00 2001 From: JadKHaddad Date: Fri, 3 May 2024 19:59:56 +0200 Subject: [PATCH 9/9] update docs Signed-off-by: JadKHaddad --- Readme.md | 1 + bmp180/src/lib.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/Readme.md b/Readme.md index 956a6e6..f2dbeea 100644 --- a/Readme.md +++ b/Readme.md @@ -13,6 +13,7 @@ The following features are available: - `impl-debug`: implements `core::fmt::Debug` for structs and enums. - `impl-defmt-format`: implements `defmt::Format` for structs and enums. - `fuzz`: enables the `fuzz` module for fuzz testing. +- `disable-arithmetic-checks`: disables arithmetic checks. - `i-know-what-i-am-doing`: allows you to split an initialized device into its parts and put it back together. Useful when you want to release the I2C bus and use it for something else. This is not recommended though, you can use [`embedded-hal-bus`](https://crates.io/crates/embedded-hal-bus) diff --git a/bmp180/src/lib.rs b/bmp180/src/lib.rs index d69b2a5..b604b4f 100644 --- a/bmp180/src/lib.rs +++ b/bmp180/src/lib.rs @@ -9,6 +9,7 @@ //! - `impl-debug`: implements `core::fmt::Debug` for structs and enums. //! - `impl-defmt-format`: implements `defmt::Format` for structs and enums. //! - `fuzz`: enables the `fuzz` module for fuzz testing. +//! - `disable-arithmetic-checks`: disables arithmetic checks. //! - `i-know-what-i-am-doing`: allows you to split an initialized device into its parts and put it back together. //! Useful when you want to release the I2C bus and use it for something else. //! This is not recommended though, you can use [`embedded-hal-bus`](https://crates.io/crates/embedded-hal-bus)