respeaker 0.3.0

USB control library for Seeed ReSpeaker Mic Array v2.0 and XVF3800 (LEDs + DSP tuning), with reSpeaker Lite identification
Documentation
//! XVF3800 LED ring helpers (`LED_EFFECT` / `LED_COLOR` / …).

use crate::color::Rgb;
use crate::error::Result;
use crate::xvf3800::protocol::Xvf3800;
use rusb::UsbContext;

/// LED effect modes for [`Xvf3800Led::set_effect`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum LedEffect {
    Off = 0,
    Breath = 1,
    Rainbow = 2,
    Mono = 3,
    Doa = 4,
    Ring = 5,
}

/// Borrowed LED helper on top of the XVF3800 control protocol.
pub struct Xvf3800Led<'a, T: UsbContext> {
    ctrl: Xvf3800<'a, T>,
}

impl<'a, T: UsbContext> Xvf3800Led<'a, T> {
    pub(crate) fn new(ctrl: Xvf3800<'a, T>) -> Self {
        Self { ctrl }
    }

    pub fn set_effect(&self, effect: LedEffect) -> Result<()> {
        self.ctrl.write_cmd("LED_EFFECT", &[f64::from(effect as u8)])
    }

    pub fn off(&self) -> Result<()> {
        self.set_effect(LedEffect::Off)
    }

    pub fn breath(&self) -> Result<()> {
        self.set_effect(LedEffect::Breath)
    }

    pub fn rainbow(&self) -> Result<()> {
        self.set_effect(LedEffect::Rainbow)
    }

    pub fn doa(&self) -> Result<()> {
        self.set_effect(LedEffect::Doa)
    }

    pub fn mono(&self, color: Rgb) -> Result<()> {
        self.ctrl
            .write_cmd("LED_COLOR", &[f64::from(color.to_u32())])?;
        self.set_effect(LedEffect::Mono)
    }

    pub fn set_brightness(&self, brightness: u8) -> Result<()> {
        self.ctrl
            .write_cmd("LED_BRIGHTNESS", &[f64::from(brightness)])
    }

    pub fn set_speed(&self, speed: u8) -> Result<()> {
        self.ctrl.write_cmd("LED_SPEED", &[f64::from(speed)])
    }

    pub fn set_gammify(&self, enabled: bool) -> Result<()> {
        self.ctrl
            .write_cmd("LED_GAMMIFY", &[if enabled { 1.0 } else { 0.0 }])
    }

    pub fn set_doa_colors(&self, base: Rgb, highlight: Rgb) -> Result<()> {
        self.ctrl.write_cmd(
            "LED_DOA_COLOR",
            &[f64::from(base.to_u32()), f64::from(highlight.to_u32())],
        )
    }

    pub fn set_ring(&self, colors: &[Rgb; 12]) -> Result<()> {
        let values: Vec<f64> = colors.iter().map(|c| f64::from(c.to_u32())).collect();
        self.ctrl.write_cmd("LED_RING_COLOR", &values)?;
        self.set_effect(LedEffect::Ring)
    }
}