diff --git a/src/sht4x.rs b/src/sht4x.rs index 2615f4e..6e82d12 100644 --- a/src/sht4x.rs +++ b/src/sht4x.rs @@ -7,6 +7,7 @@ use core::marker::PhantomData; use embedded_hal::{delay::DelayNs, i2c::I2c}; use sensirion_i2c::i2c; +const PAYLOAD_LEN: usize = 4; pub(crate) const RESPONSE_LEN: usize = 6; /// Driver for STH4x sensors. @@ -108,7 +109,7 @@ where self.write_command_and_delay_for_execution(command, delay)?; let response = self.read_response()?; - let raw = sensor_data_from_response(&response); + let raw = sensor_data_from_response(response); Ok(raw) } @@ -133,7 +134,7 @@ where self.write_command_and_delay_for_execution(command, delay)?; let response = self.read_response()?; - let raw = sensor_data_from_response(&response); + let raw = sensor_data_from_response(response); Ok(raw) } @@ -142,7 +143,7 @@ where pub fn serial_number(&mut self, delay: &mut D) -> Result> { self.write_command_and_delay_for_execution(Command::SerialNumber, delay)?; let response = self.read_response()?; - Ok(serial_number_from_response(&response)) + Ok(serial_number_from_response(response)) } /// Performs a soft reset of the sensor. @@ -172,13 +173,21 @@ where } } -pub(crate) fn sensor_data_from_response(response: &[u8; RESPONSE_LEN]) -> SensorData { +fn response_payload(response: [u8; RESPONSE_LEN]) -> [u8; PAYLOAD_LEN] { + // Response data comes in chunks of three bytes: [MSB, LSB, CRC]. The CRCs got already checked + // by sensirion_i2c::read_words_with_crc. So we just have to extract the payload data here. + [response[0], response[1], response[3], response[4]] +} + +pub(crate) fn sensor_data_from_response(response: [u8; RESPONSE_LEN]) -> SensorData { + let payload = response_payload(response); SensorData { - temperature: u16::from_be_bytes([response[0], response[1]]), - humidity: u16::from_be_bytes([response[3], response[4]]), + temperature: u16::from_be_bytes([payload[0], payload[1]]), + humidity: u16::from_be_bytes([payload[2], payload[3]]), } } -pub(crate) fn serial_number_from_response(response: &[u8; RESPONSE_LEN]) -> u32 { - u32::from_be_bytes([response[0], response[1], response[3], response[4]]) +pub(crate) fn serial_number_from_response(response: [u8; RESPONSE_LEN]) -> u32 { + let payload = response_payload(response); + u32::from_be_bytes(payload) } diff --git a/src/sht4x_async.rs b/src/sht4x_async.rs index f5e04b6..d3b85a7 100644 --- a/src/sht4x_async.rs +++ b/src/sht4x_async.rs @@ -89,7 +89,7 @@ where self.write_command_and_delay_for_execution(command, delay) .await?; let response = self.read_response().await?; - let raw = crate::sht4x::sensor_data_from_response(&response); + let raw = crate::sht4x::sensor_data_from_response(response); Ok(raw) } @@ -115,7 +115,7 @@ where self.write_command_and_delay_for_execution(command, delay) .await?; let response = self.read_response().await?; - let raw = crate::sht4x::sensor_data_from_response(&response); + let raw = crate::sht4x::sensor_data_from_response(response); Ok(raw) } @@ -125,7 +125,7 @@ where self.write_command_and_delay_for_execution(Command::SerialNumber, delay) .await?; let response = self.read_response().await?; - Ok(crate::sht4x::serial_number_from_response(&response)) + Ok(crate::sht4x::serial_number_from_response(response)) } /// Performs a soft reset of the sensor.