Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
FloppyDisck committed Oct 27, 2023
1 parent 2b0dcbb commit 5aa9dee
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
3 changes: 1 addition & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
pub type Result<T> = core::result::Result<T, SHTError>;

#[cfg_attr(feature = "thiserror", derive(thiserror::Error))]
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
pub enum SHTError {
#[cfg_attr(feature = "thiserror", error("Write Read I2C Error"))]
WriteReadI2CError,
#[cfg_attr(feature = "thiserror", error("Write I2C Error")) ]
#[cfg_attr(feature = "thiserror", error("Write I2C Error"))]
WriteI2CError,
#[cfg_attr(feature = "thiserror", error("Humidity bytes [{bytes_start:#x}, {bytes_end:#x}] expected {expected_checksum:#x} but got the checksum {calculated_checksum:#x}"))]
InvalidHumidityChecksumError {
Expand Down
18 changes: 6 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#![cfg_attr(not(feature="thiserror"), no_std)]
#![cfg_attr(not(feature = "thiserror"), no_std)]
use core::prelude::v1::*;

pub mod error;
pub mod mode;

use crate::mode::SimpleSingleShot;
use crc::{Algorithm, Crc};
use embedded_hal::blocking::{i2c, delay::DelayMs};
use embedded_hal::blocking::{delay::DelayMs, i2c};

pub use crate::error::{Result, SHTError};
pub mod prelude {
Expand Down Expand Up @@ -169,7 +169,7 @@ impl<Mode, I2C> SHT31<Mode, I2C> {
impl<I2C, D> SHT31<SimpleSingleShot<D>, I2C>
where
I2C: i2c::WriteRead + i2c::Write,
D: DelayMs<u32>
D: DelayMs<u32>,
{
/// Create a new sensor
/// I2C clock frequency must must be between 0 and 1000 kHz
Expand Down Expand Up @@ -321,12 +321,8 @@ where
let raw_temp = Self::merge_bytes(buffer[0], buffer[1]) as f32;

let (sub, mul) = match self.unit {
TemperatureUnit::Celsius => {
CELSIUS_PAIR
}
TemperatureUnit::Fahrenheit => {
FAHRENHEIT_PAIR
}
TemperatureUnit::Celsius => CELSIUS_PAIR,
TemperatureUnit::Fahrenheit => FAHRENHEIT_PAIR,
};

let pre_sub = mul * (raw_temp / CONVERSION_DENOM);
Expand Down Expand Up @@ -363,9 +359,7 @@ mod test {
let corrupt_temperature = [98, 153, 180, 98, 32, 139];

assert_eq!(
verify_reading(corrupt_temperature)
.err()
.unwrap(),
verify_reading(corrupt_temperature).err().unwrap(),
SHTError::InvalidTemperatureChecksumError {
bytes_start: 98,
bytes_end: 153,
Expand Down
15 changes: 9 additions & 6 deletions src/mode/simple_single_shot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@ use crate::{
mode::{single_shot::single_shot_read, Sht31Reader},
Accuracy, Reading, SHT31,
};
use embedded_hal::blocking::{i2c, delay::DelayMs};
use embedded_hal::blocking::{delay::DelayMs, i2c};

/// A simple reading that blocks until the measurement is obtained
#[derive(Copy, Clone, Debug)]
pub struct SimpleSingleShot <D: DelayMs<u32>>{
pub struct SimpleSingleShot<D: DelayMs<u32>> {
max_retries: u8,
ms_delay: u32,
delay: D
delay: D,
}

impl <D> SimpleSingleShot<D> where D: DelayMs<u32> {
impl<D> SimpleSingleShot<D>
where
D: DelayMs<u32>,
{
#[allow(dead_code)]
pub fn new(delay: D) -> Self {
Self {
max_retries: 8,
ms_delay: 100,
delay
delay,
}
}
/// Sets the max number of retries to read a sensor before giving up
Expand All @@ -45,7 +48,7 @@ impl <D> SimpleSingleShot<D> where D: DelayMs<u32> {
impl<I2C, D> Sht31Reader for SHT31<SimpleSingleShot<D>, I2C>
where
I2C: i2c::WriteRead + i2c::Write,
D: DelayMs<u32>
D: DelayMs<u32>,
{
/// It will initiate a read and wont stop until its either exhausted its retries or a reading is found
fn read(&mut self) -> Result<Reading> {
Expand Down

0 comments on commit 5aa9dee

Please sign in to comment.