smart_leds_trait/lib.rs
1//! # Smart Leds Trait
2//!
3//! Smart leds is a collection of crates to use smart leds on embedded devices with rust.
4//!
5//! Examples of smart leds include the popular WS2812 (also called Neopixel),
6//! APA102 (DotStar) and other leds, which can be individually adressed.
7//!
8//! This crate is used as a common base, so that breaking changes which would
9//! force all other crates to be updated, can be avoided.
10//!
11//! End users should use the [smart-leds](https://crates.io/crates/smart-leds)
12//! crate, which contains various convenience functions.
13#![no_std]
14
15pub use {rgb::RGB, rgb::RGB16, rgb::RGB8, rgb::RGBA};
16
17#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
18pub struct White<C>(pub C);
19
20#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
21pub struct CctWhite<C>{
22 pub cold: C,
23 pub warm: C,
24}
25
26/// The RGBW Pixel
27///
28/// This is used for leds, that in addition to RGB leds also contain a white led
29pub type RGBW<ComponentType, WhiteComponentType = ComponentType> =
30 RGBA<ComponentType, White<WhiteComponentType>>;
31
32
33/// The RGBCCT Pixel
34///
35/// This is used for leds that, in addition to RGB leds, also contain two white leds (cold white and warm white)
36pub type RGBCCT<ComponentType, CctWhiteComponentType = ComponentType> =
37 RGBA<ComponentType, CctWhite<CctWhiteComponentType>>;
38
39/// A trait that Smart Led Drivers implement
40///
41/// The amount of time each iteration of `iterator` might take is undefined.
42/// Drivers, where this might lead to issues, aren't expected to work in all cases.
43pub trait SmartLedsWrite {
44 type Error;
45 type Color;
46 fn write<T, I>(&mut self, iterator: T) -> Result<(), Self::Error>
47 where
48 T: IntoIterator<Item = I>,
49 I: Into<Self::Color>;
50}
51
52/// An async trait that Smart Led Drivers implement
53///
54/// The amount of time each iteration of `iterator` might take is undefined.
55/// Drivers, where this might lead to issues, aren't expected to work in all cases.
56pub trait SmartLedsWriteAsync {
57 type Error;
58 type Color;
59 // The async_fn_in_trait warning doesn't really matter for embedded cases because
60 // no_std async executors don't require futures to be Send. Also, embedded-hal-async
61 // does not have Send bounds in its traits, so the HAL functions called in
62 // implementations of this trait wouldn't return Send futures anyway. It's
63 // questionable if it would be desirable for embedded HALs to return a Send future
64 // for the write function for a peripheral because you probably don't want to
65 // write data to the same peripheral from multiple threads simultaneously and have
66 // the data get interleaved, nor have the embedded HAL implement a synchronization
67 // mechanism with a run time cost to avoid that.
68 // https://github.com/rust-embedded/embedded-hal/pull/515#issuecomment-1763525962
69 #[allow(async_fn_in_trait)]
70 async fn write<T, I>(&mut self, iterator: T) -> Result<(), Self::Error>
71 where
72 T: IntoIterator<Item = I>,
73 I: Into<Self::Color>;
74}