# as7341

Platform-agnostic Rust driver for the **AS7341** 11-channel spectral color
sensor (AMS/OSRAM), built on [`embedded-hal`] v1 traits.

[`embedded-hal`]: https://docs.rs/embedded-hal/1

## Channels

| Channel | Wavelength | Color   |
|---------|-----------|---------|
| F1      | 415 nm    | Violet  |
| F2      | 445 nm    | Indigo  |
| F3      | 480 nm    | Blue    |
| F4      | 515 nm    | Cyan    |
| F5      | 555 nm    | Green   |
| F6      | 590 nm    | Yellow  |
| F7      | 630 nm    | Orange  |
| F8      | 680 nm    | Red     |
| Clear   | —         | Broadband |
| NIR     | —         | Near-IR |

## Usage

```rust,ignore
use as7341::{As7341, Gain};

// Any embedded-hal I2C + Delay implementation.
let mut sensor = As7341::new(i2c, delay);
sensor.init()?;                    // power on, verify chip ID, set defaults

sensor.set_gain(Gain::X16)?;      // override gain (default is 8x)

let data = sensor.read_all_channels()?;
// data.f1 .. data.f8, data.clear, data.nir

sensor.set_led(true, 10)?;        // LED on, 10 mA
sensor.set_led(false, 0)?;        // LED off
```

### Integration time

```text
T = (ATIME + 1) * (ASTEP + 1) * 2.78 us
```

Defaults after `init()`: ATIME = 29, ASTEP = 599 -> ~50 ms.

```rust,ignore
sensor.set_atime(29)?;
sensor.set_astep(599)?;
```

### Flicker detection

```rust,ignore
match sensor.detect_flicker()? {
    Some(freq) => println!("Mains frequency: {} Hz", freq),
    None       => println!("Not detected"),
}
```

## Example: ESP32-C3 demo

A full demo is included in [`examples/esp32c3`](examples/esp32c3). It
exercises every driver feature and ends with a continuous ASCII bar-chart
monitor.

### Wiring

| AS7341 | ESP32-C3 |
|--------|----------|
| SDA    | GPIO1    |
| SCL    | GPIO0    |
| VIN    | 3.3 V    |
| GND    | GND      |

### Build & flash

```bash
cd examples/esp32c3
cargo run --release          # builds, flashes via espflash, opens monitor
```

### Demo output

The example runs through six phases automatically:

**1 -- Device info**

```text
========================================
       AS7341 Spectral Sensor Demo
========================================

[device]  chip id = 0x24
[device]  defaults: ATIME=29, ASTEP=599, gain=8x
[device]  integration time ~50 ms
```

**2 -- Flicker detection** (100 / 120 Hz mains)

```text
--- Flicker Detection ---
  attempt 1: unknown
  attempt 2: 100 Hz
  attempt 3: 100 Hz
```

**3 -- LED demo** (ramp to 50 mA, compare ambient vs illuminated)

```text
--- LED Demo ---
[ambient]  F1=1 F3=3 F6=14 F8=7 Clear=30
  LED on @ 4 mA
  LED on @ 10 mA
  LED on @ 25 mA
  LED on @ 50 mA
[LED 50mA] F1=3 F3=7 F6=31 F8=13 Clear=33
  LED off
```

**4 -- Gain sweep** (0.5x -- 512x at fixed integration time)

```text
--- Gain Sweep (ATIME=29, ASTEP=599) ---
  Gain |     F1     F4     F6     F8  Clear
-------+--------------------------------------
  0.5x |      0      0      1      1      2
    1x |      1      0      1      0      3
    2x |      0      1      3      1      7
    4x |      0      3      7      4     15
    8x |      1      5     14      7     30
   16x |      2     11     29     14     59
   32x |      3     20     56     26    118
   64x |      6     40    111     53    234
  128x |     12     82    221    106    468
  256x |     24    163    438    210    922
  512x |     47    319    852    413   1789
```

**5 -- Integration time demo** (varying ATIME/ASTEP at fixed gain)

```text
--- Integration Time Demo (gain=8x) ---
      Time |     F1     F6  Clear
-----------+-----------------------
   ~2.8 ms |      0      1      2
    ~50 ms |      1     14     30
   ~278 ms |      4     81    169
```

**6 -- Continuous spectral monitor** (ASCII bar chart, gain 16x)

```text
--- Continuous Monitor (gain=16x, ~50 ms) ---

F1    415nm ........................     2
F2    445nm ##......................     5
F3    480nm ##......................     6
F4    515nm ####....................    11
F5    555nm ########................    20
F6    590nm ###########.............    29
F7    630nm ###########.............    28
F8    680nm #####...................    14
Clear       ########################    60
NIR         #.......................     4
```

Bars are normalized to the maximum value in each reading.

## Features

| Feature | Description |
|---------|-------------|
| `defmt` | Derive `defmt::Format` for all public types |

## License

Licensed under either of

- [Apache License, Version 2.0](LICENSE-APACHE)
- [MIT License](LICENSE-MIT)

at your option.
