Skip to content

Commit

Permalink
Factor out response data processing to responses.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
sirhcel committed Mar 1, 2025
1 parent cd07518 commit 7ff8d0f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

mod commands;
mod error;
mod responses;
mod sht4x;
mod types;

Expand Down
23 changes: 23 additions & 0 deletions src/responses.rs
Original file line number Diff line number Diff line change
@@ -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)
}
23 changes: 1 addition & 22 deletions src/sht4x.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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)
}
8 changes: 4 additions & 4 deletions src/sht4x_async.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down 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 = 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 = 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(serial_number_from_response(response))
}

/// Performs a soft reset of the sensor.
Expand Down

0 comments on commit 7ff8d0f

Please sign in to comment.