#[cfg(not(all(feature = "async", feature = "embedded-sensors-hal-async")))]
fn main() {
eprintln!("examples/alert_comparator.rs requires --features async,embedded-sensors-hal-async");
}
#[cfg(all(feature = "async", feature = "embedded-sensors-hal-async"))]
#[tokio::main]
async fn main() -> anyhow::Result<()> {
use anyhow::anyhow;
use embedded_sensors_hal_async::temperature::{TemperatureThresholdSet, TemperatureThresholdWait};
use pico_de_gallo_hal::Hal;
use pico_de_gallo_lib::{GpioDirection, GpioPull};
use tmp108::{AlertTmp108, Config, Hysteresis, Polarity, Thermostat};
let hal = Hal::new();
let i2c = hal.i2c();
let mut alert = hal.gpio(0);
alert
.set_config(GpioDirection::Input, GpioPull::None)
.map_err(|_| anyhow!("Failed to configure GPIO0 as input"))?;
let mut tmp = AlertTmp108::new_with_a0_gnd(i2c, alert);
tmp.tmp108
.configure(Config {
thermostat_mode: Thermostat::Comparator,
alert_polarity: Polarity::ActiveLow,
hysteresis: Hysteresis::_2C,
..Default::default()
})
.await
.map_err(|_| anyhow!("Failed to configure TMP108"))?;
tmp.set_temperature_threshold_low(15.0)
.await
.map_err(|_| anyhow!("Failed to set low threshold"))?;
tmp.set_temperature_threshold_high(30.0)
.await
.map_err(|_| anyhow!("Failed to set high threshold"))?;
println!("Waiting for first ALERT (warm the sensor above 30 C)...");
let t1 = tmp
.wait_for_temperature_threshold()
.await
.map_err(|_| anyhow!("First wait failed"))?;
println!("First trigger: {t1:.2} C (pin is latched)");
let t2 = tmp
.wait_for_temperature_threshold()
.await
.map_err(|_| anyhow!("Second wait failed"))?;
println!("Second trigger (immediate): {t2:.2} C");
println!("Let the sensor cool below 28 C; the next ALERT will reassert when temperature rises again...");
let t3 = tmp
.wait_for_temperature_threshold()
.await
.map_err(|_| anyhow!("Third wait failed"))?;
println!("Third trigger: {t3:.2} C");
Ok(())
}