Skip to main content

capsules_extra/
ws2812.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2025.
4
5//! WS2812-compatible single-wire RGB LED (WS2812, SK68XX).
6//!
7//! Tested with the SK68XXMINI on the ESP32-C3-DevKitM-1 and the WS2812 on the
8//! ESP32-C3-DevKit-RUST-2.
9//!
10//! Datasheet: <https://www.rose-lighting.com/wp-content/uploads/sites/53/2020/05/SK68XX-MINI-HS-REV.04-EN23535RGB-thick.pdf>
11
12use core::cell::Cell;
13
14use kernel::hil::gpio::Pin;
15use kernel::hil::led::Led;
16
17/// The single-wire, tri-color (RGB) LED.
18///
19/// `write_frame` is provided by the chip and sends one 24-bit GRB frame.
20pub struct Ws2812<'a, P: Pin> {
21    pin: &'a P,
22    write_frame: fn(&P, u32),
23    red: Cell<bool>,
24    green: Cell<bool>,
25    blue: Cell<bool>,
26}
27
28impl<'a, P: Pin> Ws2812<'a, P> {
29    pub fn new(pin: &'a P, write_frame: fn(&P, u32)) -> Self {
30        Self {
31            pin,
32            write_frame,
33            red: Cell::new(false),
34            green: Cell::new(false),
35            blue: Cell::new(false),
36        }
37    }
38
39    pub fn init(&self) {
40        self.pin.make_output();
41        self.pin.clear();
42    }
43
44    fn write(&self) {
45        let byte = |on: bool| if on { 0xFF } else { 0 };
46        let grb = byte(self.green.get()) << 16 | byte(self.red.get()) << 8 | byte(self.blue.get());
47        (self.write_frame)(self.pin, grb);
48    }
49
50    fn update_red(&self, on: bool) {
51        self.red.set(on);
52        self.write();
53    }
54
55    fn update_green(&self, on: bool) {
56        self.green.set(on);
57        self.write();
58    }
59
60    fn update_blue(&self, on: bool) {
61        self.blue.set(on);
62        self.write();
63    }
64
65    fn toggle_red(&self) {
66        self.red.set(!self.red.get());
67        self.write();
68    }
69
70    fn toggle_green(&self) {
71        self.green.set(!self.green.get());
72        self.write();
73    }
74
75    fn toggle_blue(&self) {
76        self.blue.set(!self.blue.get());
77        self.write();
78    }
79
80    fn get_red(&self) -> bool {
81        self.red.get()
82    }
83
84    fn get_green(&self) -> bool {
85        self.green.get()
86    }
87
88    fn get_blue(&self) -> bool {
89        self.blue.get()
90    }
91}
92
93// One of the LEDs on the tri-color LED.
94//
95// - Index 0: Red
96// - Index 1: Green
97// - Index 2: Blue
98pub struct Ws2812Led<'a, P: Pin> {
99    ws2812: &'a Ws2812<'a, P>,
100    index: usize,
101}
102
103impl<'a, P: Pin> Ws2812Led<'a, P> {
104    pub fn new(ws2812: &'a Ws2812<'a, P>, index: usize) -> Self {
105        Self { ws2812, index }
106    }
107}
108
109impl<P: Pin> Led for Ws2812Led<'_, P> {
110    fn init(&self) {}
111
112    fn on(&self) {
113        match self.index {
114            0 => self.ws2812.update_red(true),
115            1 => self.ws2812.update_green(true),
116            2 => self.ws2812.update_blue(true),
117            _ => {}
118        }
119    }
120
121    fn off(&self) {
122        match self.index {
123            0 => self.ws2812.update_red(false),
124            1 => self.ws2812.update_green(false),
125            2 => self.ws2812.update_blue(false),
126            _ => {}
127        }
128    }
129
130    fn toggle(&self) {
131        match self.index {
132            0 => self.ws2812.toggle_red(),
133            1 => self.ws2812.toggle_green(),
134            2 => self.ws2812.toggle_blue(),
135            _ => {}
136        }
137    }
138
139    fn read(&self) -> bool {
140        match self.index {
141            0 => self.ws2812.get_red(),
142            1 => self.ws2812.get_green(),
143            2 => self.ws2812.get_blue(),
144            _ => false,
145        }
146    }
147}