Skip to content

Commit

Permalink
Make extracting payload from response more explicit
Browse files Browse the repository at this point in the history
It's written in the sensors data sheet but I did not remember this after
looking into the code after quite some time.
  • Loading branch information
sirhcel committed Mar 1, 2025
1 parent ca4af1c commit cd07518
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
25 changes: 17 additions & 8 deletions src/sht4x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -142,7 +143,7 @@ where
pub fn serial_number(&mut self, delay: &mut D) -> Result<u32, Error<I::Error>> {
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.
Expand Down Expand Up @@ -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)
}
6 changes: 3 additions & 3 deletions src/sht4x_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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.
Expand Down

0 comments on commit cd07518

Please sign in to comment.