Skip to content

Commit

Permalink
Share extracting serial number from response
Browse files Browse the repository at this point in the history
Sensor data extraction is already shared and the serial number
extraction code should be shared as well.
  • Loading branch information
sirhcel committed Mar 1, 2025
1 parent 3538866 commit ca4af1c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 14 deletions.
12 changes: 5 additions & 7 deletions src/sht4x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,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(u32::from_be_bytes([
response[0],
response[1],
response[3],
response[4],
]))
Ok(serial_number_from_response(&response))
}

/// Performs a soft reset of the sensor.
Expand Down Expand Up @@ -184,3 +178,7 @@ pub(crate) fn sensor_data_from_response(response: &[u8; RESPONSE_LEN]) -> Sensor
humidity: u16::from_be_bytes([response[3], response[4]]),
}
}

pub(crate) fn serial_number_from_response(response: &[u8; RESPONSE_LEN]) -> u32 {
u32::from_be_bytes([response[0], response[1], response[3], response[4]])
}
8 changes: 1 addition & 7 deletions src/sht4x_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,7 @@ where
self.write_command_and_delay_for_execution(Command::SerialNumber, delay)
.await?;
let response = self.read_response().await?;

Ok(u32::from_be_bytes([
response[0],
response[1],
response[3],
response[4],
]))
Ok(crate::sht4x::serial_number_from_response(&response))
}

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

0 comments on commit ca4af1c

Please sign in to comment.