rsiot/components/cmp_esp_led/
fn_process.rs

1use esp_idf_svc::hal::{peripheral::Peripheral, rmt::RmtChannel};
2use ws2812_esp32_rmt_driver::Ws2812Esp32Rmt;
3
4use crate::{executor::CmpInOut, message::MsgDataBound};
5
6use super::Config;
7
8pub async fn fn_process<TMsg, TPeripheral, TRmt>(
9    config: Config<TMsg, TPeripheral, TRmt>,
10    mut msg_bus: CmpInOut<TMsg>,
11) -> super::Result<()>
12where
13    TMsg: MsgDataBound,
14    TPeripheral: RmtChannel,
15    TRmt: Peripheral<P = TPeripheral> + 'static,
16{
17    let mut ws2812 = Ws2812Esp32Rmt::new(config.rmt_channel, config.pin)?;
18
19    while let Ok(msg) = msg_bus.recv_input().await {
20        let config = (config.fn_input)(&msg);
21        let Some(config) = config else { continue };
22
23        let mut leds = vec![];
24        for (amount, color) in config {
25            for _ in 0..amount {
26                leds.push(color);
27            }
28        }
29
30        // ws2812.write_nocopy(leds)?;
31        ws2812.write_nocopy(leds)?;
32    }
33
34    Err(super::Error::FnProcessEnd)
35}