blinksy 0.11.0

no-std, no-alloc LED control library designed for 1D, 2D, and 3D layouts
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use core::{array::IntoIter, iter::Iterator, ops::Index};

use crate::util::component::Component;

use super::{ColorCorrection, LinearSrgb};

/// Color data ready for output to LED hardware
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum LedColor<C> {
    /// Output RGB color data
    Rgb(LedRgb<C>),
    /// Output RGBW color data
    Rgbw(LedRgbw<C>),
}

impl<C: Component> LedColor<C> {
    /// Creates an output-ready LED color from a linear sRGB color
    ///
    /// # Arguments
    ///
    /// - `linear_srgb` - Linear RGB color to convert
    /// - `channels` - The LED channel format specification
    /// - `brightness` - Global brightness scaling factor (0.0 to 1.0)
    /// - `correction` - Color correction factors for the LEDs
    ///
    /// # Returns
    ///
    /// A `LedColor` ready for output to hardware
    pub fn from_linear_srgb(
        linear_srgb: LinearSrgb,
        channels: LedChannels,
        brightness: f32,
        correction: ColorCorrection,
    ) -> LedColor<C> {
        match channels {
            LedChannels::Rgb(rgb_channels) => {
                let rgb = LedRgb::from_linear_srgb(linear_srgb, brightness, correction);
                LedColor::Rgb(rgb.reorder(rgb_channels))
            }
            LedChannels::Rgbw(rgbw_channels) => {
                let rgbw = LedRgbw::from_linear_srgb(linear_srgb, brightness, correction);
                LedColor::Rgbw(rgbw.reorder(rgbw_channels))
            }
        }
    }
}

impl<C> AsRef<[C]> for LedColor<C> {
    #[inline]
    fn as_ref(&self) -> &[C] {
        use LedColor::*;
        match self {
            Rgb(rgb) => rgb.as_ref(),
            Rgbw(rgbw) => rgbw.as_ref(),
        }
    }
}

pub enum LedColorIntoIter<C> {
    Rgb(IntoIter<C, 3>),
    Rgbw(IntoIter<C, 4>),
}

impl<C> Iterator for LedColorIntoIter<C> {
    type Item = C;

    fn next(&mut self) -> Option<Self::Item> {
        use LedColorIntoIter::*;
        match self {
            Rgb(rgb_iter) => rgb_iter.next(),
            Rgbw(rgbw_iter) => rgbw_iter.next(),
        }
    }
}

impl<C> IntoIterator for LedColor<C> {
    type Item = C;
    type IntoIter = LedColorIntoIter<C>;

    fn into_iter(self) -> Self::IntoIter {
        use LedColor::*;
        match self {
            Rgb(rgb) => LedColorIntoIter::Rgb(rgb.into_iter()),
            Rgbw(rgbw) => LedColorIntoIter::Rgbw(rgbw.into_iter()),
        }
    }
}

/// RGB color values ready for output to LED hardware
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct LedRgb<C>([C; 3]);

impl<C: Component> LedRgb<C> {
    /// Creates RGB LED output values from a linear sRGB color
    ///
    /// # Arguments
    ///
    /// - `linear_srgb` - Linear RGB color to convert
    /// - `brightness` - Global brightness scaling factor (0.0 to 1.0)
    /// - `correction` - Color correction factors for the LEDs
    ///
    /// # Returns
    ///
    /// A `LedRgb` with component values ready for output
    pub fn from_linear_srgb(
        linear_srgb: LinearSrgb,
        brightness: f32,
        correction: ColorCorrection,
    ) -> Self {
        let LinearSrgb { red, green, blue } = linear_srgb;

        // Apply color correction
        let red = red * correction.red;
        let green = green * correction.green;
        let blue = blue * correction.blue;

        // Apply brightness
        let red = red * brightness;
        let green = green * brightness;
        let blue = blue * brightness;

        // Clamp values
        let red = red.clamp(0., 1.);
        let green = green.clamp(0., 1.);
        let blue = blue.clamp(0., 1.);

        Self([
            C::from_normalized_f32(red),
            C::from_normalized_f32(green),
            C::from_normalized_f32(blue),
        ])
    }

    /// Reorders the RGB components according to the specified channel order
    pub fn reorder(self, channels: RgbChannels) -> Self {
        Self(channels.reorder(self.0))
    }
}

impl<C> AsRef<[C]> for LedRgb<C> {
    #[inline]
    fn as_ref(&self) -> &[C] {
        &self.0
    }
}

impl<C> IntoIterator for LedRgb<C> {
    type Item = C;
    type IntoIter = IntoIter<C, 3>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl<C> Index<usize> for LedRgb<C> {
    type Output = C;

    #[inline]
    fn index(&self, index: usize) -> &Self::Output {
        &self.0[index]
    }
}

/// RGBW color values ready for output to LED hardware
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct LedRgbw<C>([C; 4]);

impl<C: Component> LedRgbw<C> {
    /// Creates RGBW LED output values from a linear sRGB color
    ///
    /// This performs white channel extraction using the common minimum method,
    /// where the white component is the minimum of R,G,B, and those values are
    /// then subtracted from the RGB components.
    ///
    /// # Arguments
    ///
    /// - `linear_srgb` - Linear RGB color to convert
    /// - `brightness` - Global brightness scaling factor (0.0 to 1.0)
    /// - `correction` - Color correction factors for the LEDs
    ///
    /// # Returns
    ///
    /// A `LedRgbw` with component values ready for output
    pub fn from_linear_srgb(
        linear_srgb: LinearSrgb,
        brightness: f32,
        correction: ColorCorrection,
    ) -> Self {
        let LinearSrgb { red, green, blue } = linear_srgb;

        // Extract white component (minimum of RGB)
        let white = red.min(green).min(blue);

        // Subtract white from RGB to get true RGB components
        let red = red - white;
        let green = green - white;
        let blue = blue - white;

        // Apply color correction
        let red = red * correction.red;
        let green = green * correction.green;
        let blue = blue * correction.blue;

        // Apply brightness
        let red = red * brightness;
        let green = green * brightness;
        let blue = blue * brightness;
        let white = white * brightness;

        // Clamp values
        let red = red.clamp(0., 1.);
        let green = green.clamp(0., 1.);
        let blue = blue.clamp(0., 1.);
        let white = white.clamp(0., 1.);

        Self([
            C::from_normalized_f32(red),
            C::from_normalized_f32(green),
            C::from_normalized_f32(blue),
            C::from_normalized_f32(white),
        ])
    }

    /// Reorders the RGBW components according to the specified channel order
    pub fn reorder(self, channels: RgbwChannels) -> Self {
        Self(channels.reorder(self.0))
    }
}

impl<C> AsRef<[C]> for LedRgbw<C> {
    #[inline]
    fn as_ref(&self) -> &[C] {
        &self.0
    }
}

impl<C> IntoIterator for LedRgbw<C> {
    type Item = C;
    type IntoIter = IntoIter<C, 4>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl<C> Index<usize> for LedRgbw<C> {
    type Output = C;

    #[inline]
    fn index(&self, index: usize) -> &Self::Output {
        &self.0[index]
    }
}

/// Enumeration of color channel formats.
///
/// Different LED chipsets have different ordering of color channels.
/// This enum represents the possible arrangements.
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum LedChannels {
    /// RGB with 3 channels
    Rgb(RgbChannels),
    /// RGBW with 4 channels
    Rgbw(RgbwChannels),
}

impl LedChannels {
    /// Returns the number of color channels.
    pub const fn channel_count(&self) -> usize {
        match self {
            LedChannels::Rgb(_) => 3,
            LedChannels::Rgbw(_) => 4,
        }
    }
}

/// Enumeration of RGB channel orders.
///
/// Different RGB LED chipsets may use different ordering of the R, G, and B channels.
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum RgbChannels {
    /// Red, Green, Blue
    RGB,
    /// Red, Blue, Green
    RBG,
    /// Green, Red, Blue
    GRB,
    /// Green, Blue, Red
    GBR,
    /// Blue, Red, Green
    BRG,
    /// Blue, Green, Red
    BGR,
}

/// Enumeration of RGBW channel orders.
///
/// Different RGBW LED chipsets may use different ordering of the R, G, B, and W channels.
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum RgbwChannels {
    // RGB
    WRGB,
    RWGB,
    RGWB,
    RGBW,

    // RBG
    WRBG,
    RWBG,
    RBWG,
    RBGW,

    // GRB
    WGRB,
    GWRB,
    GRWB,
    GRBW,

    // GBR
    WGBR,
    GWBR,
    GBWR,
    GBRW,

    // BRG
    WBRG,
    BWRG,
    BRWG,
    BRGW,

    // BGR
    WBGR,
    BWGR,
    BGWR,
    BGRW,
}

impl RgbChannels {
    /// Reorders RGB values according to the channel order.
    ///
    /// # Arguments
    ///
    /// - `rgb` - Array of [R, G, B] values in canonical order
    ///
    /// # Returns
    ///
    /// Array of values reordered according to the channel specification
    pub fn reorder<Word: Copy>(&self, rgb: [Word; 3]) -> [Word; 3] {
        use RgbChannels::*;
        match self {
            RGB => [rgb[0], rgb[1], rgb[2]],
            RBG => [rgb[0], rgb[2], rgb[1]],
            GRB => [rgb[1], rgb[0], rgb[2]],
            GBR => [rgb[1], rgb[2], rgb[0]],
            BRG => [rgb[2], rgb[0], rgb[1]],
            BGR => [rgb[2], rgb[1], rgb[0]],
        }
    }
}

impl RgbwChannels {
    /// Reorders RGBW values according to the channel order.
    ///
    /// # Arguments
    ///
    /// - `rgbw` - Array of [R, G, B, W] values in canonical order
    ///
    /// # Returns
    ///
    /// Array of values reordered according to the channel specification
    pub fn reorder<Word: Copy>(&self, rgbw: [Word; 4]) -> [Word; 4] {
        use RgbwChannels::*;
        match self {
            // RGB
            WRGB => [rgbw[3], rgbw[0], rgbw[1], rgbw[2]],
            RWGB => [rgbw[0], rgbw[3], rgbw[1], rgbw[2]],
            RGWB => [rgbw[0], rgbw[1], rgbw[3], rgbw[2]],
            RGBW => [rgbw[0], rgbw[1], rgbw[2], rgbw[3]],

            // RBG
            WRBG => [rgbw[3], rgbw[0], rgbw[2], rgbw[1]],
            RWBG => [rgbw[0], rgbw[3], rgbw[2], rgbw[1]],
            RBWG => [rgbw[0], rgbw[2], rgbw[3], rgbw[1]],
            RBGW => [rgbw[0], rgbw[2], rgbw[1], rgbw[3]],

            // GRB
            WGRB => [rgbw[3], rgbw[1], rgbw[0], rgbw[2]],
            GWRB => [rgbw[1], rgbw[3], rgbw[0], rgbw[2]],
            GRWB => [rgbw[1], rgbw[0], rgbw[3], rgbw[2]],
            GRBW => [rgbw[1], rgbw[0], rgbw[2], rgbw[3]],

            // GBR
            WGBR => [rgbw[3], rgbw[1], rgbw[2], rgbw[0]],
            GWBR => [rgbw[1], rgbw[3], rgbw[2], rgbw[0]],
            GBWR => [rgbw[1], rgbw[2], rgbw[3], rgbw[0]],
            GBRW => [rgbw[1], rgbw[2], rgbw[0], rgbw[3]],

            // BRG
            WBRG => [rgbw[3], rgbw[2], rgbw[0], rgbw[1]],
            BWRG => [rgbw[2], rgbw[3], rgbw[0], rgbw[1]],
            BRWG => [rgbw[2], rgbw[0], rgbw[3], rgbw[1]],
            BRGW => [rgbw[2], rgbw[0], rgbw[1], rgbw[3]],

            // BGR
            WBGR => [rgbw[3], rgbw[2], rgbw[1], rgbw[0]],
            BWGR => [rgbw[2], rgbw[3], rgbw[1], rgbw[0]],
            BGWR => [rgbw[2], rgbw[1], rgbw[3], rgbw[0]],
            BGRW => [rgbw[2], rgbw[1], rgbw[0], rgbw[3]],
        }
    }
}