huesmith 0.1.0

Hue-compatible Zigbee light library for ESP32-C6/H2 (ESP-IDF)
pub mod cct;
pub mod simple;
pub mod ws2812;

/// Map a ZCL level (0-254) onto an LEDC duty range, shared by every PWM
/// backend so they all sit on the same integer curve (a CCT and a dimmable
/// light at the same level must land on the same duty cycle, and any rounding
/// change happens in exactly one place).
pub(crate) fn level_to_duty(level: u8, max_duty: u32) -> u32 {
    (u32::from(level.min(254)) * max_duty) / 254
}

/// Resolve the electrical duty for a channel, honoring the wiring polarity.
///
/// `inverted = false` (default): active-high — duty grows with level, for a
/// low-side switch (N-MOSFET/transistor) or an LED wired GPIO-high = on.
/// `inverted = true`: sink drive — the GPIO sinks current directly from the
/// LED's cathode, so electrical duty is reversed (`max_duty` = dark, 0 = full
/// on) and "off" must park the pin high.
pub(crate) fn electrical_duty(level: u8, max_duty: u32, inverted: bool) -> u32 {
    let duty = level_to_duty(level, max_duty);
    if inverted {
        max_duty - duty
    } else {
        duty
    }
}