Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ jobs:
strategy:
fail-fast: false
matrix:
msrv: ["1.83"] # We're relying on namespaced-features, which
msrv: ["1.84"] # We're relying on namespaced-features, which
# was released in 1.60
#
# We also depend on `fixed' which requires rust
Expand All @@ -175,6 +175,8 @@ jobs:
# collapse_debuginfo
#
# embassy upstream switched to rust 1.83
#
# f32::abs moved to core from std

name: ubuntu / ${{ matrix.msrv }}
steps:
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/rolling.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
strategy:
fail-fast: false
matrix:
msrv: ["1.83"] # We're relying on namespaced-features, which
msrv: ["1.84"] # We're relying on namespaced-features, which
# was released in 1.60
#
# We also depend on `fixed' which requires rust
Expand All @@ -48,6 +48,8 @@ jobs:
# collapse_debuginfo
#
# embassy upstream switched to rust 1.83
#
# f32::abs moved to core from std
name: ubuntu / ${{ matrix.msrv }}
steps:
- uses: actions/checkout@v4
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tmp108"
version = "0.2.0"
version = "0.3.0"
authors = ["Felipe Balbi <febalbi@microsoft.com>"]
repository = "https://github.com/OpenDevicePartnership/tmp108"
license = "MIT"
Expand All @@ -17,14 +17,14 @@ include = [
"/LICENSE",
]
edition = "2021"
rust-version = "1.83.0"
rust-version = "1.84.0"

[dependencies]
bilge = "0.2.0"
embedded-hal = "1.0.0"
embedded-hal-async = { version = "1.0.0", optional = true }
embedded-sensors-hal = { version = "0.1.0", optional = true }
embedded-sensors-hal-async = { version = "0.2.0", optional = true }
embedded-sensors-hal-async = { version = "0.3.0", optional = true }

[features]
full = [ "async", "embedded-sensors-hal", "embedded-sensors-hal-async" ]
Expand Down
75 changes: 71 additions & 4 deletions src/asynchronous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ use core::future::Future;
#[cfg(feature = "embedded-sensors-hal-async")]
use embedded_sensors_hal_async::sensor;
#[cfg(feature = "embedded-sensors-hal-async")]
use embedded_sensors_hal_async::temperature::{DegreesCelsius, TemperatureSensor, TemperatureThresholdWait};
use embedded_sensors_hal_async::temperature::{
DegreesCelsius, TemperatureHysteresis, TemperatureSensor, TemperatureThresholdSet, TemperatureThresholdWait,
};

use super::{Configuration, ConversionMode, ConversionRate, Polarity, Register, ThermostatMode, A0};
use super::{Configuration, ConversionMode, ConversionRate, Hysteresis, Polarity, Register, ThermostatMode, A0};

/// TMP108 asynchronous device driver
pub struct Tmp108<I2C: embedded_hal_async::i2c::I2c, DELAY: embedded_hal_async::delay::DelayNs> {
Expand Down Expand Up @@ -244,14 +246,20 @@ impl<I2C: embedded_hal_async::i2c::I2c, DELAY: embedded_hal_async::delay::DelayN
pub enum Error<E: embedded_hal_async::i2c::Error> {
/// I2C Bus Error
Bus(E),
/// Invalid Input Error
InvalidInput,
/// Other Error
Other,
}

#[cfg(feature = "embedded-sensors-hal-async")]
impl<E: embedded_hal_async::i2c::Error> sensor::Error for Error<E> {
fn kind(&self) -> sensor::ErrorKind {
sensor::ErrorKind::Other
match *self {
Self::Bus(_) => sensor::ErrorKind::Peripheral,
Self::InvalidInput => sensor::ErrorKind::InvalidInput,
Self::Other => sensor::ErrorKind::Other,
}
}
}

Expand All @@ -271,6 +279,45 @@ impl<I2C: embedded_hal_async::i2c::I2c, DELAY: embedded_hal_async::delay::DelayN
}
}

#[cfg(feature = "embedded-sensors-hal-async")]
impl<I2C: embedded_hal_async::i2c::I2c, DELAY: embedded_hal_async::delay::DelayNs> TemperatureThresholdSet
for Tmp108<I2C, DELAY>
{
async fn set_temperature_threshold_low(&mut self, threshold: DegreesCelsius) -> Result<(), Self::Error> {
self.set_low_limit(threshold).await.map_err(Error::Bus)
}

async fn set_temperature_threshold_high(&mut self, threshold: DegreesCelsius) -> Result<(), Self::Error> {
self.set_high_limit(threshold).await.map_err(Error::Bus)
}
}

#[cfg(feature = "embedded-sensors-hal-async")]
impl<I2C: embedded_hal_async::i2c::I2c, DELAY: embedded_hal_async::delay::DelayNs> TemperatureHysteresis
for Tmp108<I2C, DELAY>
{
async fn set_temperature_threshold_hysteresis(&mut self, hysteresis: DegreesCelsius) -> Result<(), Self::Error> {
/* Trait method takes a continuous range of f32 values as argument,
* but internally driver only accepts 4 discrete values for hysteresis.
* We ensure only a correct value for hysteresis is passed in, and return error otherwise.
*/
let hysteresis = if (hysteresis - 0.0).abs() < f32::EPSILON {
Hysteresis::ZeroCelsius
} else if (hysteresis - 1.0).abs() < f32::EPSILON {
Hysteresis::OneCelsius
} else if (hysteresis - 2.0).abs() < f32::EPSILON {
Hysteresis::TwoCelsius
} else if (hysteresis - 4.0).abs() < f32::EPSILON {
Hysteresis::FourCelsius
} else {
return Err(Error::InvalidInput);
};

let config = self.config.with_hysteresis(hysteresis);
self.set_configuration(config).await.map_err(Error::Bus)
}
}

/// TMP108 asynchronous device driver (with alert pin)
#[cfg(feature = "embedded-sensors-hal-async")]
pub struct AlertTmp108<
Expand Down Expand Up @@ -353,7 +400,7 @@ impl<
I2C: embedded_hal_async::i2c::I2c,
DELAY: embedded_hal_async::delay::DelayNs,
ALERT: embedded_hal_async::digital::Wait + embedded_hal::digital::InputPin,
> TemperatureThresholdWait for AlertTmp108<I2C, DELAY, ALERT>
> TemperatureThresholdSet for AlertTmp108<I2C, DELAY, ALERT>
{
async fn set_temperature_threshold_low(&mut self, threshold: DegreesCelsius) -> Result<(), Self::Error> {
self.tmp108.set_low_limit(threshold).await.map_err(Error::Bus)
Expand All @@ -362,7 +409,15 @@ impl<
async fn set_temperature_threshold_high(&mut self, threshold: DegreesCelsius) -> Result<(), Self::Error> {
self.tmp108.set_high_limit(threshold).await.map_err(Error::Bus)
}
}

#[cfg(feature = "embedded-sensors-hal-async")]
impl<
I2C: embedded_hal_async::i2c::I2c,
DELAY: embedded_hal_async::delay::DelayNs,
ALERT: embedded_hal_async::digital::Wait + embedded_hal::digital::InputPin,
> TemperatureThresholdWait for AlertTmp108<I2C, DELAY, ALERT>
{
async fn wait_for_temperature_threshold(&mut self) -> Result<DegreesCelsius, Self::Error> {
match (self.tmp108.config.tm(), self.tmp108.config.polarity()) {
// In comparator mode, the ALERT pin remains active even after triggering.
Expand Down Expand Up @@ -393,6 +448,18 @@ impl<
}
}

#[cfg(feature = "embedded-sensors-hal-async")]
impl<
I2C: embedded_hal_async::i2c::I2c,
DELAY: embedded_hal_async::delay::DelayNs,
ALERT: embedded_hal_async::digital::Wait + embedded_hal::digital::InputPin,
> TemperatureHysteresis for AlertTmp108<I2C, DELAY, ALERT>
{
async fn set_temperature_threshold_hysteresis(&mut self, hysteresis: DegreesCelsius) -> Result<(), Self::Error> {
self.tmp108.set_temperature_threshold_hysteresis(hysteresis).await
}
}

#[cfg(test)]
mod tests {
use assert_approx_eq::assert_approx_eq;
Expand Down