-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathled_controllers.rs
More file actions
194 lines (165 loc) · 4.83 KB
/
Copy pathled_controllers.rs
File metadata and controls
194 lines (165 loc) · 4.83 KB
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
use anyhow::Result;
use cichlid::ColorRGB;
#[cfg(feature = "controller_gpio")]
use rppal::gpio::{Gpio, OutputPin};
#[cfg(feature = "controller_ws2811")]
use rs_ws281x::{ChannelBuilder, ControllerBuilder, RawColor, StripType};
pub trait LedController {
fn is_addressable_individually() -> bool;
fn led_amount(&self) -> usize;
fn set_all(&mut self, color: ColorRGB);
fn set_all_individual(&mut self, colors: &[ColorRGB]);
fn set_individual(&mut self, i: usize, color: ColorRGB);
fn commit(&mut self) -> Result<()>;
fn reset(&mut self) -> Result<()>;
}
// Controller WS2811
// <editor-fold>
#[cfg(feature = "controller_ws2811")]
pub struct ControllerWs2811 {
inner: rs_ws281x::Controller,
}
#[cfg(feature = "controller_ws2811")]
unsafe impl Send for ControllerWs2811 {}
#[cfg(feature = "controller_ws2811")]
impl ControllerWs2811 {
// Default: 800kHz
const LED_FREQ: u32 = 800_000;
// DO NOT USE 5 on RPi
const LED_DMA: i32 = 10;
// GPIO18
const LED_PIN: i32 = 18;
// Don't change
const LED_CHANNEL: usize = 0;
pub const COLOR_OFF: RawColor = [0, 0, 0, 0];
pub fn new(led_count: usize, brightness: u8) -> Result<Self> {
let inner = ControllerBuilder::new()
.freq(Self::LED_FREQ)
.dma(Self::LED_DMA)
.channel(
Self::LED_CHANNEL,
ChannelBuilder::new()
.pin(Self::LED_PIN)
.count(led_count as i32)
.strip_type(StripType::Ws2811Gbr)
.invert(false)
.brightness(brightness)
.build(),
)
.build()?;
Ok(Self { inner })
}
}
#[cfg(feature = "controller_ws2811")]
impl LedController for ControllerWs2811 {
fn is_addressable_individually() -> bool {
true
}
fn led_amount(&self) -> usize {
self.inner.leds(Self::LED_CHANNEL).len()
}
fn set_all(&mut self, color: ColorRGB) {
let raw = [color.r, color.g, color.b, 0];
for led in self.inner.leds_mut(Self::LED_CHANNEL) {
*led = raw;
}
}
fn set_all_individual(&mut self, colors: &[ColorRGB]) {
for (i, led) in self
.inner
.leds_mut(Self::LED_CHANNEL)
.iter_mut()
.enumerate()
{
*led = [colors[i].r, colors[i].g, colors[i].b, 0];
}
}
fn set_individual(&mut self, i: usize, color: ColorRGB) {
self.inner.leds_mut(Self::LED_CHANNEL)[i] = [color.r, color.g, color.b, 0];
}
fn commit(&mut self) -> Result<()> {
self.inner.render()?;
self.inner.wait()?;
Ok(())
}
fn reset(&mut self) -> Result<()> {
for led in self.inner.leds_mut(Self::LED_CHANNEL) {
*led = Self::COLOR_OFF;
}
self.commit()
}
}
// </editor-fold>
// GPIO Controller
// <editor-fold>
#[cfg(feature = "controller_gpio")]
pub struct ControllerGpio {
gpio: Gpio,
freq: f64,
pins: [OutputPin; 3],
}
#[cfg(feature = "controller_gpio")]
impl ControllerGpio {
pub fn new(freq: f64, red: u8, green: u8, blue: u8) -> Result<Self> {
let gpio = Gpio::new()?;
let red = gpio.get(red)?.into_output();
let green = gpio.get(green)?.into_output();
let blue = gpio.get(blue)?.into_output();
let pins = [red, green, blue];
let mut controller = Self { gpio, freq, pins };
controller.reset()?;
Ok(controller)
}
#[inline]
fn red(&mut self) -> &mut OutputPin {
&mut self.pins[0]
}
#[inline]
fn green(&mut self) -> &mut OutputPin {
&mut self.pins[1]
}
#[inline]
fn blue(&mut self) -> &mut OutputPin {
&mut self.pins[2]
}
}
#[cfg(feature = "controller_gpio")]
impl LedController for ControllerGpio {
fn is_addressable_individually() -> bool {
false
}
fn led_amount(&self) -> usize {
1
}
fn set_all(&mut self, color: ColorRGB) {
let freq = self.freq;
// The actual set_pwm_frequency function always returns Ok, so we can unwrap
self.red()
.set_pwm_frequency(freq, color.r as f64 / 255.0)
.unwrap();
self.green()
.set_pwm_frequency(freq, color.g as f64 / 255.0)
.unwrap();
self.blue()
.set_pwm_frequency(freq, color.b as f64 / 255.0)
.unwrap();
}
fn set_all_individual(&mut self, _: &[ColorRGB]) {
unimplemented!()
}
fn set_individual(&mut self, _: usize, color: ColorRGB) {
self.set_all(color);
}
fn commit(&mut self) -> Result<()> {
// no-op
Ok(())
}
fn reset(&mut self) -> Result<()> {
for pin in self.pins.iter_mut() {
pin.clear_pwm()?;
pin.set_low();
}
Ok(())
}
}
// <editor-fold>