zephyr/device/led_strip.rs
1//! Device wrappers for Zephyr LED strip controllers.
2
3use super::{NoStatic, Unique};
4use crate::{error::to_result_void, raw, Result};
5
6/// Type alias for the Zephyr `led_rgb` struct used to describe a single pixel's RGB value.
7///
8/// Note that `led_strip_update_rgb` is documented to potentially overwrite the pixel buffer,
9/// hence methods that update the strip take `&mut [LedRgb]`.
10pub type LedRgb = raw::led_rgb;
11
12/// A Zephyr LED strip device (e.g. a WS2812 addressable RGB strip).
13///
14/// This wrapper maps to Zephyr's `led_strip_*` API.
15#[allow(dead_code)]
16pub struct LedStrip {
17 pub(crate) device: *const raw::device,
18}
19
20// SAFETY: `LedStrip` holds a raw pointer to a static Zephyr device structure with no
21// thread-affine Rust state.
22unsafe impl Send for LedStrip {}
23
24impl LedStrip {
25 /// Constructor, intended to be called by devicetree generated code.
26 #[allow(dead_code)]
27 pub(crate) unsafe fn new(
28 unique: &Unique,
29 _static: &NoStatic,
30 device: *const raw::device,
31 ) -> Option<LedStrip> {
32 if !unique.once() {
33 return None;
34 }
35
36 Some(LedStrip { device })
37 }
38
39 /// Verify that the underlying LED strip device is ready for use.
40 pub fn is_ready(&self) -> bool {
41 unsafe { raw::device_is_ready(self.device) }
42 }
43
44 /// Push an array of RGB pixel values to the strip.
45 ///
46 /// The driver may overwrite the contents of `pixels`.
47 pub fn update_rgb(&mut self, pixels: &mut [LedRgb]) -> Result<()> {
48 to_result_void(unsafe {
49 raw::led_strip_update_rgb(self.device, pixels.as_mut_ptr(), pixels.len())
50 })
51 }
52
53 /// Return the number of pixels (LEDs) in the strip.
54 pub fn length(&self) -> usize {
55 unsafe { raw::led_strip_length(self.device) }
56 }
57}