# as6221

`no_std` Rust driver for the [AS6221](https://ams-osram.com/products/sensors/temperature-sensors/ams-as6221-digital-temperature-sensor) digital temperature sensor.

Supports both synchronous and asynchronous I2C via feature flags, built on [`embedded-hal`](https://docs.rs/embedded-hal) v1.

## Features

| Feature | Description |
|---------|-------------|
| *(default)* | Synchronous API using `embedded-hal::i2c::I2c` |
| `async` | Asynchronous API using `embedded-hal-async::i2c::I2c` |
| `defmt` | `defmt::Format` derives and structured logging |
| `log` | `log` crate logging |

## Usage

```toml
# Synchronous (default)
[dependencies]
as6221 = "0.1"

# Async (e.g. Embassy on STM32)
[dependencies]
as6221 = { version = "0.1", default-features = false, features = ["async", "defmt"] }
```

```rust
use as6221::{As6221, ConversionRate, AlertMode};

let mut sensor = As6221::new(i2c);
sensor.init().await?; // drop .await for sync

// Read temperature (integer, no float)
let centi_c = sensor.read_temperature_centi_c().await?; // e.g. 2534 = 25.34 C

// Read temperature as f32
let temp = sensor.read_temperature_c().await?; // e.g. 25.34

// Set alert thresholds
sensor.set_tlow_c(10.0).await?;
sensor.set_thigh_c(40.0).await?;

// Configure conversion rate, alert behavior, then push to device
sensor.set_conversion_rate(ConversionRate::Sps8);
sensor.set_alert_mode(AlertMode::Interrupt);
sensor.set_consecutive_faults(4);
sensor.flush_config().await?;

// Single-shot mode (low power)
sensor.set_sleep_mode(true);
sensor.flush_config().await?;
sensor.trigger_single_shot().await?;
// ... wait for conversion ...
while !sensor.single_shot_ready().await? {}
let temp = sensor.read_temperature_c().await?;
```

## Register map

| Address | Register |
|---------|----------|
| `0x00` | TVAL - Temperature value (read-only) |
| `0x01` | CONFIG - Configuration |
| `0x02` | TLOW - Low temperature threshold (default 75 C) |
| `0x03` | THIGH - High temperature threshold (default 80 C) |

## Config register

| Bits | Field | Values |
|------|-------|--------|
| 5 | AL | Alert status (read-only) |
| 7:6 | CR | Conversion rate: 0.25, 1, 4, 8 sps |
| 8 | SM | Sleep mode |
| 9 | IM | 0 = comparator, 1 = interrupt |
| 10 | POL | Alert polarity: 0 = active-low, 1 = active-high |
| 12:11 | CF | Consecutive faults (1-4) |
| 15 | SS | Single-shot trigger |

Temperature resolution: 1/128 C per LSB (0.0078125 C). Range: -40 C to +125 C.

## I2C addresses

Selectable via ADD0/ADD1 pin strapping: `0x44`-`0x4B`. See `as6221::addr` constants.

## License

MIT
