m5unified 0.3.2

Safe Rust wrapper for M5Unified.
Documentation
//! RGB LED helpers.
//!
//! The LED wrapper exposes the count, type, brightness, auto-display behavior,
//! and per-pixel or bulk color setters for boards with controllable LEDs.

#[derive(Debug)]
pub struct Led;

impl Led {
    pub fn begin(&mut self) -> bool {
        unsafe { m5unified_sys::m5u_led_begin() }
    }

    pub fn display(&mut self) {
        unsafe { m5unified_sys::m5u_led_display() }
    }

    pub fn set_auto_display(&mut self, enable: bool) {
        unsafe { m5unified_sys::m5u_led_set_auto_display(enable) }
    }

    pub fn count(&self) -> usize {
        unsafe { m5unified_sys::m5u_led_count() }
    }

    pub fn set_brightness(&mut self, brightness: u8) {
        unsafe { m5unified_sys::m5u_led_set_brightness(brightness) }
    }

    pub fn set_color(&mut self, index: usize, color: LedColor) {
        unsafe { m5unified_sys::m5u_led_set_color_rgb(index, color.r, color.g, color.b) }
    }

    pub fn set_all_color(&mut self, color: LedColor) {
        unsafe { m5unified_sys::m5u_led_set_all_color_rgb(color.r, color.g, color.b) }
    }

    pub fn set_colors(&mut self, index: usize, colors: &[LedColor]) {
        let raw: Vec<m5unified_sys::m5u_led_color_t> = colors
            .iter()
            .map(|color| m5unified_sys::m5u_led_color_t {
                r: color.r,
                g: color.g,
                b: color.b,
            })
            .collect();
        unsafe { m5unified_sys::m5u_led_set_colors_rgb(raw.as_ptr(), index, raw.len()) }
    }

    pub fn led_type(&self, index: usize) -> LedType {
        LedType::from_raw(unsafe { m5unified_sys::m5u_led_get_type(index) })
    }

    pub fn is_enabled(&self) -> bool {
        unsafe { m5unified_sys::m5u_led_is_enabled() }
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum LedType {
    Unknown,
    FullColor,
    Single,
    Raw(i32),
}

impl LedType {
    pub const fn from_raw(raw: i32) -> Self {
        match raw {
            0 => Self::Unknown,
            1 => Self::FullColor,
            2 => Self::Single,
            other => Self::Raw(other),
        }
    }

    pub const fn raw(self) -> i32 {
        match self {
            Self::Unknown => 0,
            Self::FullColor => 1,
            Self::Single => 2,
            Self::Raw(raw) => raw,
        }
    }
}

#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
pub struct LedColor {
    pub r: u8,
    pub g: u8,
    pub b: u8,
}

impl LedColor {
    pub const BLACK: Self = Self::new(0, 0, 0);
    pub const BLUE: Self = Self::new(0, 0, 255);
    pub const GREEN: Self = Self::new(0, 255, 0);
    pub const RED: Self = Self::new(255, 0, 0);
    pub const WHITE: Self = Self::new(255, 255, 255);

    pub const fn new(r: u8, g: u8, b: u8) -> Self {
        Self { r, g, b }
    }
}