diff --git a/src/lib.rs b/src/lib.rs index c4f178d..9d27622 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ mod commands; mod error; +mod responses; mod sht4x; mod types; diff --git a/src/responses.rs b/src/responses.rs new file mode 100644 index 0000000..40a9f0f --- /dev/null +++ b/src/responses.rs @@ -0,0 +1,23 @@ +use crate::types::SensorData; + +const PAYLOAD_LEN: usize = 4; +pub(crate) const RESPONSE_LEN: usize = 6; + +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([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 { + let payload = response_payload(response); + u32::from_be_bytes(payload) +} diff --git a/src/sht4x.rs b/src/sht4x.rs index 6e82d12..279f9e6 100644 --- a/src/sht4x.rs +++ b/src/sht4x.rs @@ -1,15 +1,13 @@ use crate::{ commands::Command, error::Error, + responses::{sensor_data_from_response, serial_number_from_response, RESPONSE_LEN}, types::{Address, HeatingDuration, HeatingPower, Measurement, Precision, SensorData}, }; 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. #[cfg_attr(feature = "defmt", derive(defmt::Format))] #[derive(Debug, Eq, Hash, PartialEq)] @@ -172,22 +170,3 @@ where Ok(()) } } - -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([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 { - let payload = response_payload(response); - u32::from_be_bytes(payload) -} diff --git a/src/sht4x_async.rs b/src/sht4x_async.rs index d3b85a7..f82824a 100644 --- a/src/sht4x_async.rs +++ b/src/sht4x_async.rs @@ -1,7 +1,7 @@ use crate::{ commands::Command, error::Error, - sht4x::RESPONSE_LEN, + responses::{sensor_data_from_response, serial_number_from_response, RESPONSE_LEN}, types::{Address, HeatingDuration, HeatingPower, Measurement, Precision, SensorData}, }; use core::marker::PhantomData; @@ -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 = 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 = 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(serial_number_from_response(response)) } /// Performs a soft reset of the sensor.