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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
//! # Control System
//!
//! [`Control`] is the central control system for Blinksy: connecting a layout,
//! pattern, and driver together to form a complete LED control pipeline.
//!
//! As [`Control`] has a complex generic type signature, [`ControlBuilder`] is a
//! builder to help you create [`Control`] instances.
//!
//! # Example (Blocking)
//!
//! ```rust,ignore
//! // Define a 1d layout of 60 LEDs
//! layout1d!(Layout, 60);
//!
//! // Create a control system
//! let mut control = ControlBuilder::new_1d()
//!     .with_layout::<Layout, { Layout::PIXEL_COUNT }>()
//!     // Choose an animation pattern
//!     .with_pattern::</* Pattern type */>(/* Pattern params */)
//!     // Choose an LED driver
//!     .with_driver(/* LED driver */)
//!     // Set frame buffer size for your driver and LEDs
//!     .with_frame_buffer_size::</* Length of frame buffer */>()
//!     .build();
//!
//! // Use the control system
//! control.set_brightness(0.5);
//!
//! // Main control loop
//! loop {
//!     control.tick(/* current time in milliseconds */).unwrap();
//! }
//! ```
//!
//! # Example (Async)
//!
//! ```rust,ignore
//! // Define a 1d layout of 60 LEDs
//! layout1d!(Layout, 60);
//!
//! // Create a control system
//! let mut control = ControlBuilder::new_1d_async()
//!     .with_layout::<Layout, { Layout::PIXEL_COUNT }>()
//!     // Choose an animation pattern
//!     .with_pattern::</* Pattern type */>(/* Pattern params */)
//!     // Choose a driver type
//!     .with_driver(/* LED driver */)
//!     // Set frame buffer size for your driver and LEDs
//!     .with_frame_buffer_size::</* Length of frame buffer */>()
//!     .build();
//!
//! // Use the control system
//! control.set_brightness(0.5);
//!
//! // Main control loop
//! loop {
//!     control.tick(/* current time in milliseconds */).await.unwrap();
//! }
//! ```
use core::marker::PhantomData;

use crate::{
    color::{ColorCorrection, FromColor},
    driver::Driver as DriverTrait,
    layout::LayoutForDim,
    markers::{Blocking, Dim1d, Dim2d, Dim3d, Set, Unset},
    pattern::Pattern as PatternTrait,
};
#[cfg(feature = "async")]
use crate::{driver::DriverAsync as DriverAsyncTrait, markers::Async};

/// Central LED control system.
///
/// A [`Control`] is made up of:
///
/// - A [`layout`](crate::layout)
/// - A [`pattern`](crate::pattern)
/// - A [`driver`](crate::driver)
///
/// You can use [`Control`] to
///
/// - Set a global brightness
/// - Set a global color correction.
/// - Send a frame of colors from the pattern to the driver.
///
/// Tip: Use [`ControlBuilder`] to build your [`Control`] struct.
///
// # Type Parameters
//
// * `PIXEL_COUNT` - The number of LEDs in the layout
// * `FRAME_BUFFER_SIZE` - The per-call frame buffer size used by the driver
// * `Dim` - The dimension marker ([`Dim1d`] or [`Dim2d`] or [`Dim3d`])
// * `Exec` - The execution mode marker ([`Blocking`] or `Async`)
// * `Layout` - The [`layout`](crate::layout) type
// * `Pattern` - The [`pattern`](crate::pattern) type
// * `Driver` - The LED [`driver`](crate::driver) type
pub struct Control<
    const PIXEL_COUNT: usize,
    const FRAME_BUFFER_SIZE: usize,
    Dim,
    Exec,
    Layout,
    Pattern,
    Driver,
> where
    Layout: LayoutForDim<Dim>,
    Pattern: PatternTrait<Dim, Layout>,
{
    dim: PhantomData<Dim>,
    exec: PhantomData<Exec>,
    layout: PhantomData<Layout>,
    pattern: Pattern,
    driver: Driver,
    brightness: f32,
    correction: ColorCorrection,
}

impl<
        const PIXEL_COUNT: usize,
        const FRAME_BUFFER_SIZE: usize,
        Dim,
        Exec,
        Layout,
        Pattern,
        Driver,
    > Control<PIXEL_COUNT, FRAME_BUFFER_SIZE, Dim, Exec, Layout, Pattern, Driver>
where
    Layout: LayoutForDim<Dim>,
    Pattern: PatternTrait<Dim, Layout>,
{
    /// Creates a new control system.
    ///
    /// # Arguments
    ///
    /// - `pattern` - The pattern to use
    /// - `driver` - The LED driver to use
    ///
    /// # Returns
    ///
    /// A new Control instance with default brightness
    pub fn new(pattern: Pattern, driver: Driver) -> Self {
        Self {
            dim: PhantomData,
            exec: PhantomData,
            layout: PhantomData,
            pattern,
            driver,
            brightness: 1.0,
            correction: ColorCorrection::default(),
        }
    }

    /// Sets the overall brightness level.
    ///
    /// # Arguments
    ///
    /// - `brightness` - Brightness level from 0.0 (off) to 1.0 (full)
    pub fn set_brightness(&mut self, brightness: f32) {
        self.brightness = brightness;
    }

    /// Sets a color correction.
    ///
    /// # Arguments
    ///
    /// - `correction` - Color correction factors
    pub fn set_color_correction(&mut self, correction: ColorCorrection) {
        self.correction = correction;
    }
}

impl<const PIXEL_COUNT: usize, const FRAME_BUFFER_SIZE: usize, Dim, Layout, Pattern, Driver>
    Control<PIXEL_COUNT, FRAME_BUFFER_SIZE, Dim, Blocking, Layout, Pattern, Driver>
where
    Layout: LayoutForDim<Dim>,
    Pattern: PatternTrait<Dim, Layout>,
    Driver: DriverTrait,
    Driver::Color: FromColor<Pattern::Color>,
{
    /// Updates the LED state based on the current time.
    ///
    /// This method:
    /// 1. Calls the pattern to generate colors
    /// 2. Passes the colors and brightness to the driver
    ///
    /// # Arguments
    ///
    /// - `time_in_ms` - Current time in milliseconds
    ///
    /// # Returns
    ///
    /// Result indicating success or an error from the driver
    pub fn tick(&mut self, time_in_ms: u64) -> Result<(), Driver::Error> {
        let pixels = self.pattern.tick(time_in_ms);
        self.driver.show::<PIXEL_COUNT, FRAME_BUFFER_SIZE, _, _>(
            pixels,
            self.brightness,
            self.correction,
        )
    }
}

#[cfg(feature = "async")]
impl<const PIXEL_COUNT: usize, const FRAME_BUFFER_SIZE: usize, Dim, Layout, Pattern, Driver>
    Control<PIXEL_COUNT, FRAME_BUFFER_SIZE, Dim, Async, Layout, Pattern, Driver>
where
    Layout: LayoutForDim<Dim>,
    Pattern: PatternTrait<Dim, Layout>,
    Driver: DriverAsyncTrait,
    Driver::Color: FromColor<Pattern::Color>,
{
    /// Updates the LED state based on the current time, asynchronously.
    ///
    /// This method:
    /// 1. Calls the pattern to generate colors
    /// 2. Passes the colors and brightness to the driver
    ///
    /// # Arguments
    ///
    /// - `time_in_ms` - Current time in milliseconds
    ///
    /// # Returns
    ///
    /// Result indicating success or an error from the driver
    pub async fn tick(&mut self, time_in_ms: u64) -> Result<(), Driver::Error> {
        let pixels = self.pattern.tick(time_in_ms);
        self.driver
            .show::<PIXEL_COUNT, FRAME_BUFFER_SIZE, _, _>(pixels, self.brightness, self.correction)
            .await
    }
}

/// The builder allows your to build up your [`Control`] system one-by-one
/// and handles the combination of generic types and constraints that
/// [`Control`] expects.
pub struct ControlBuilder<
    const PIXEL_COUNT: usize,
    const FRAME_BUFFER_SIZE: usize,
    Dim,
    Exec,
    Layout,
    Pattern,
    Driver,
    IsFrameBufferSet,
> {
    dim: PhantomData<Dim>,
    exec: PhantomData<Exec>,
    layout: PhantomData<Layout>,
    pattern: Pattern,
    driver: Driver,
    is_frame_buffer_set: PhantomData<IsFrameBufferSet>,
}

impl ControlBuilder<0, 0, (), (), (), (), (), Unset> {
    /// Starts building a one-dimensional blocking control system.
    ///
    /// # Returns
    ///
    /// A builder initialized for 1D, blocking
    pub fn new_1d() -> ControlBuilder<0, 0, Dim1d, Blocking, (), (), (), Unset> {
        ControlBuilder {
            dim: PhantomData,
            exec: PhantomData,
            layout: PhantomData,
            pattern: (),
            driver: (),
            is_frame_buffer_set: PhantomData,
        }
    }
}

#[cfg(feature = "async")]
impl ControlBuilder<0, 0, (), (), (), (), (), Unset> {
    /// Starts building a one-dimensional asynchronous control system.
    ///
    /// # Returns
    ///
    /// A builder initialized for 1D, async
    pub fn new_1d_async() -> ControlBuilder<0, 0, Dim1d, Async, (), (), (), Unset> {
        ControlBuilder {
            dim: PhantomData,
            exec: PhantomData,
            layout: PhantomData,
            pattern: (),
            driver: (),
            is_frame_buffer_set: PhantomData,
        }
    }
}

impl ControlBuilder<0, 0, (), (), (), (), (), Unset> {
    /// Starts building a two-dimensional blocking control system.
    ///
    /// # Returns
    ///
    /// A builder initialized for 2D, blocking
    pub fn new_2d() -> ControlBuilder<0, 0, Dim2d, Blocking, (), (), (), Unset> {
        ControlBuilder {
            dim: PhantomData,
            exec: PhantomData,
            layout: PhantomData,
            pattern: (),
            driver: (),
            is_frame_buffer_set: PhantomData,
        }
    }
}

#[cfg(feature = "async")]
impl ControlBuilder<0, 0, (), (), (), (), (), Unset> {
    /// Starts building a two-dimensional asynchronous control system.
    ///
    /// # Returns
    ///
    /// A builder initialized for 2D, async
    pub fn new_2d_async() -> ControlBuilder<0, 0, Dim2d, Async, (), (), (), Unset> {
        ControlBuilder {
            dim: PhantomData,
            exec: PhantomData,
            layout: PhantomData,
            pattern: (),
            driver: (),
            is_frame_buffer_set: PhantomData,
        }
    }
}

impl ControlBuilder<0, 0, (), (), (), (), (), Unset> {
    /// Starts building a three-dimensional blocking control system.
    ///
    /// # Returns
    ///
    /// A builder initialized for 3D, blocking
    pub fn new_3d() -> ControlBuilder<0, 0, Dim3d, Blocking, (), (), (), Unset> {
        ControlBuilder {
            dim: PhantomData,
            exec: PhantomData,
            layout: PhantomData,
            pattern: (),
            driver: (),
            is_frame_buffer_set: PhantomData,
        }
    }
}

#[cfg(feature = "async")]
impl ControlBuilder<0, 0, (), (), (), (), (), Unset> {
    /// Starts building a three-dimensional asynchronous control system.
    ///
    /// # Returns
    ///
    /// A builder initialized for 3D, async
    pub fn new_3d_async() -> ControlBuilder<0, 0, Dim3d, Async, (), (), (), Unset> {
        ControlBuilder {
            dim: PhantomData,
            exec: PhantomData,
            layout: PhantomData,
            pattern: (),
            driver: (),
            is_frame_buffer_set: PhantomData,
        }
    }
}

impl<const FRAME_BUFFER_SIZE: usize, Dim, Exec, Pattern, Driver, IsFrameBufferSet>
    ControlBuilder<0, FRAME_BUFFER_SIZE, Dim, Exec, (), Pattern, Driver, IsFrameBufferSet>
{
    /// Specifies the layout type for the control system.
    ///
    /// # Type Parameters
    ///
    /// - `Layout` - The layout type implementing Layout that corresponds to Dim
    /// - `PIXEL_COUNT` - A constant for the number of pixels (`Layout::PIXEL_COUNT`)
    ///
    /// Until  [the `generic_const_exprs` feature](https://doc.rust-lang.org/beta/unstable-book/language-features/generic-const-exprs.html) is stable,
    /// the user must explicitly provide `PIXEL_COUNT` as `Layout::PIXEL_COUNT`.
    ///
    /// # Returns
    ///
    /// Builder with layout type specified
    pub fn with_layout<Layout, const PIXEL_COUNT: usize>(
        self,
    ) -> ControlBuilder<
        PIXEL_COUNT,
        FRAME_BUFFER_SIZE,
        Dim,
        Exec,
        Layout,
        Pattern,
        Driver,
        IsFrameBufferSet,
    >
    where
        Layout: LayoutForDim<Dim>,
    {
        ControlBuilder {
            dim: self.dim,
            exec: self.exec,
            layout: PhantomData,
            pattern: self.pattern,
            driver: self.driver,
            is_frame_buffer_set: self.is_frame_buffer_set,
        }
    }
}

impl<
        const PIXEL_COUNT: usize,
        const FRAME_BUFFER_SIZE: usize,
        Dim,
        Exec,
        Layout,
        Driver,
        IsFrameBufferSet,
    >
    ControlBuilder<PIXEL_COUNT, FRAME_BUFFER_SIZE, Dim, Exec, Layout, (), Driver, IsFrameBufferSet>
where
    Layout: LayoutForDim<Dim>,
{
    /// Specifies the pattern and its parameters.
    ///
    /// # Type Parameters
    ///
    /// - `Pattern` - The pattern type implementing Pattern<Dim, Layout>
    ///
    /// # Arguments
    ///
    /// - `params` - The pattern parameters
    ///
    /// # Returns
    ///
    /// Builder with pattern specified
    pub fn with_pattern<Pattern>(
        self,
        params: Pattern::Params,
    ) -> ControlBuilder<
        PIXEL_COUNT,
        FRAME_BUFFER_SIZE,
        Dim,
        Exec,
        Layout,
        Pattern,
        Driver,
        IsFrameBufferSet,
    >
    where
        Pattern: PatternTrait<Dim, Layout>,
    {
        let pattern = Pattern::new(params);
        ControlBuilder {
            dim: self.dim,
            exec: self.exec,
            layout: self.layout,
            pattern,
            driver: self.driver,
            is_frame_buffer_set: self.is_frame_buffer_set,
        }
    }
}

impl<const PIXEL_COUNT: usize, const FRAME_BUFFER_SIZE: usize, Dim, Layout, Pattern>
    ControlBuilder<PIXEL_COUNT, FRAME_BUFFER_SIZE, Dim, Blocking, Layout, Pattern, (), Unset>
{
    /// Specifies the LED driver for the control system (blocking).
    ///
    /// # Type Parameters
    ///
    /// - `Driver` - The blocking driver type
    ///
    /// # Arguments
    ///
    /// - `driver` - The LED driver instance (blocking)
    ///
    /// # Returns
    ///
    /// Builder with driver and frame buffer size specified
    pub fn with_driver<Driver>(
        self,
        driver: Driver,
    ) -> ControlBuilder<PIXEL_COUNT, FRAME_BUFFER_SIZE, Dim, Blocking, Layout, Pattern, Driver, Unset>
    where
        Driver: DriverTrait,
    {
        ControlBuilder {
            dim: self.dim,
            exec: self.exec,
            layout: self.layout,
            pattern: self.pattern,
            driver,
            is_frame_buffer_set: self.is_frame_buffer_set,
        }
    }
}

#[cfg(feature = "async")]
impl<const PIXEL_COUNT: usize, const FRAME_BUFFER_SIZE: usize, Dim, Layout, Pattern>
    ControlBuilder<PIXEL_COUNT, FRAME_BUFFER_SIZE, Dim, Async, Layout, Pattern, (), Unset>
{
    /// Specifies the LED driver for the control system (blocking).
    ///
    /// # Type Parameters
    ///
    /// - `Driver` - The blocking driver type
    ///
    /// # Arguments
    ///
    /// - `driver` - The LED driver instance (blocking)
    ///
    /// # Returns
    ///
    /// Builder with driver and frame buffer size specified
    pub fn with_driver<Driver>(
        self,
        driver: Driver,
    ) -> ControlBuilder<PIXEL_COUNT, FRAME_BUFFER_SIZE, Dim, Async, Layout, Pattern, Driver, Unset>
    where
        Driver: DriverAsyncTrait,
    {
        ControlBuilder {
            dim: self.dim,
            exec: self.exec,
            layout: self.layout,
            pattern: self.pattern,
            driver,
            is_frame_buffer_set: self.is_frame_buffer_set,
        }
    }
}

impl<const PIXEL_COUNT: usize, Dim, Layout, Pattern, Driver>
    ControlBuilder<PIXEL_COUNT, 0, Dim, Blocking, Layout, Pattern, Driver, Unset>
{
    /// Specifies the frame buffer size for the control system (blocking).
    ///
    /// # Type Parameters
    ///
    /// - `FRAME_BUFFER_SIZE` - The per-call frame buffer size
    ///
    /// Until  [the `generic_const_exprs` feature](https://doc.rust-lang.org/beta/unstable-book/language-features/generic-const-exprs.html) is stable,
    /// the user must explicitly provide the correct `FRAME_BUFFER_SIZE`. Typically this should be
    /// calculated using the [LED](crate::leds) `frame_buffer_size` constant function, e.g. (`{ Ws2812::frame_buffer_size(Layout::PIXEL_COUNT) }`).
    ///
    /// # Returns
    ///
    /// Builder with frame buffer size specified
    pub fn with_frame_buffer_size<const FRAME_BUFFER_SIZE: usize>(
        self,
    ) -> ControlBuilder<PIXEL_COUNT, FRAME_BUFFER_SIZE, Dim, Blocking, Layout, Pattern, Driver, Set>
    where
        Driver: DriverTrait,
    {
        ControlBuilder {
            dim: self.dim,
            exec: self.exec,
            layout: self.layout,
            pattern: self.pattern,
            driver: self.driver,
            is_frame_buffer_set: PhantomData,
        }
    }
}

#[cfg(feature = "async")]
impl<const PIXEL_COUNT: usize, Dim, Layout, Pattern, Driver>
    ControlBuilder<PIXEL_COUNT, 0, Dim, Async, Layout, Pattern, Driver, Unset>
{
    /// Specifies the frame buffer size for the control system (async).
    ///
    /// # Type Parameters
    ///
    /// - `FRAME_BUFFER_SIZE` - The per-call frame buffer size
    ///
    /// # Returns
    ///
    /// Builder with frame buffer size specified
    pub fn with_frame_buffer_size<const FRAME_BUFFER_SIZE: usize>(
        self,
    ) -> ControlBuilder<PIXEL_COUNT, FRAME_BUFFER_SIZE, Dim, Async, Layout, Pattern, Driver, Set>
    where
        Driver: DriverAsyncTrait,
    {
        ControlBuilder {
            dim: self.dim,
            exec: self.exec,
            layout: self.layout,
            pattern: self.pattern,
            driver: self.driver,
            is_frame_buffer_set: PhantomData,
        }
    }
}

impl<const PIXEL_COUNT: usize, const FRAME_BUFFER_SIZE: usize, Dim, Layout, Pattern, Driver>
    ControlBuilder<PIXEL_COUNT, FRAME_BUFFER_SIZE, Dim, Blocking, Layout, Pattern, Driver, Set>
where
    Layout: LayoutForDim<Dim>,
    Pattern: PatternTrait<Dim, Layout>,
    Driver: DriverTrait,
    Driver::Color: FromColor<Pattern::Color>,
{
    /// Builds the final [`Control`] struct.
    ///
    /// # Returns
    ///
    /// A fully configured Control instance
    pub fn build(
        self,
    ) -> Control<PIXEL_COUNT, FRAME_BUFFER_SIZE, Dim, Blocking, Layout, Pattern, Driver> {
        Control::new(self.pattern, self.driver)
    }
}

#[cfg(feature = "async")]
impl<const PIXEL_COUNT: usize, const FRAME_BUFFER_SIZE: usize, Dim, Layout, Pattern, Driver>
    ControlBuilder<PIXEL_COUNT, FRAME_BUFFER_SIZE, Dim, Async, Layout, Pattern, Driver, Set>
where
    Layout: LayoutForDim<Dim>,
    Pattern: PatternTrait<Dim, Layout>,
    Driver: DriverAsyncTrait,
    Driver::Color: FromColor<Pattern::Color>,
{
    /// Builds the final [`Control`] struct.
    ///
    /// # Returns
    ///
    /// A fully configured Control instance
    pub fn build(
        self,
    ) -> Control<PIXEL_COUNT, FRAME_BUFFER_SIZE, Dim, Async, Layout, Pattern, Driver> {
        Control::new(self.pattern, self.driver)
    }
}