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
#![no_std]
#![deny(warnings)]
use core::marker::PhantomData;
use embedded_hal::blocking::delay::DelayMs;
use embedded_hal::digital::v2::OutputPin;
#[derive(Debug)]
pub enum Error {
CommError,
NoInterfaceDefined,
EnableLine,
}
#[derive(Clone, Copy)]
pub enum Model {
LP5009,
LP5012,
}
impl Model {
fn get_pin_count(&self) -> u8 {
match *self {
Model::LP5009 => 9,
Model::LP5012 => 12,
}
}
}
#[derive(Clone, Copy)]
pub enum Address {
Broadcast,
Independent(u8),
}
impl Address {
pub fn into_u8(self) -> u8 {
match self {
Address::Independent(address) => {
if address > 3 {
panic!("LP50XX only supports 4 dedicated addresses, 0b00, 0b01, 0b10 or 0b11")
}
return 0b00010100 | address;
}
Address::Broadcast => 0b0001100,
}
}
}
pub struct DefaultMode {}
pub struct ColorMode {}
impl ColorMode {
pub fn new() -> Self {
Self {}
}
}
pub struct MonochromaticMode {}
impl MonochromaticMode {
pub fn new() -> Self {
Self {}
}
}
pub struct LP50xx<MODE, I2C, EN> {
interface: Option<I2C>,
enable: EN,
transfer_callback: Option<fn(addr: Address, data: &[u8])>,
continuous_addressing: bool,
active_address: Address,
mode: PhantomData<MODE>,
model: Model,
}
impl<I2C, EN> LP50xx<DefaultMode, I2C, EN>
where
EN: OutputPin,
{
pub fn init_with_i2c(model: Model, i2c: I2C, mut en: EN) -> Self {
en.set_low().ok();
Self {
interface: Some(i2c),
enable: en,
transfer_callback: None,
model,
active_address: Address::Broadcast,
continuous_addressing: true,
mode: PhantomData,
}
}
pub fn init_with_callback(
model: Model,
mut en: EN,
callback: fn(addr: Address, data: &[u8]),
) -> Self {
en.set_low().ok();
Self {
interface: None,
enable: en,
transfer_callback: Some(callback),
model,
active_address: Address::Broadcast,
continuous_addressing: true,
mode: PhantomData,
}
}
pub fn set_continuous_addressing(&mut self, state: bool) {
self.continuous_addressing = state;
}
pub fn set_active_address(&mut self, address: Address) {
self.active_address = address;
}
pub fn release(self) -> (Option<I2C>, EN) {
(self.interface, self.enable)
}
}
impl<MODE, I2C, EN> LP50xx<MODE, I2C, EN>
where
I2C: embedded_hal::blocking::i2c::Write<u8>,
EN: OutputPin,
{
pub fn into_color_mode(self) -> LP50xx<ColorMode, I2C, EN> {
self.into_mode::<ColorMode>()
}
pub fn into_monochromatic_mode(self) -> LP50xx<MonochromaticMode, I2C, EN> {
self.into_mode::<MonochromaticMode>()
}
fn into_mode<MODE2>(self) -> LP50xx<MODE2, I2C, EN> {
LP50xx {
interface: self.interface,
enable: self.enable,
transfer_callback: self.transfer_callback,
active_address: self.active_address,
model: self.model,
continuous_addressing: self.continuous_addressing,
mode: PhantomData,
}
}
fn write(&mut self, addr: Address, data: &[u8]) -> Result<(), Error> {
if self.interface.is_some() {
self.interface
.as_mut()
.unwrap()
.write(addr.into_u8(), data)
.map_err(|_| Error::CommError)?;
return Ok({});
}
if self.transfer_callback.is_some() {
self.transfer_callback.unwrap()(addr, data);
return Ok({});
}
return Err(Error::NoInterfaceDefined);
}
pub fn reset<DELAY>(&mut self, delay: &mut DELAY) -> Result<(), Error>
where
DELAY: DelayMs<u8>,
{
self.write(Address::Broadcast, &[0x17, 0xff])?;
delay.delay_ms(1);
self.enable.set_low().map_err(|_| Error::EnableLine)?;
delay.delay_ms(10);
self.enable.set_high().map_err(|_| Error::EnableLine)?;
delay.delay_ms(10);
Ok(())
}
pub fn enable<DELAY>(&mut self, delay: &mut DELAY) -> Result<(), Error>
where
DELAY: DelayMs<u8>,
{
self.enable.set_low().map_err(|_| Error::EnableLine)?;
delay.delay_ms(1);
self.enable.set_high().map_err(|_| Error::EnableLine)?;
delay.delay_ms(10);
self.write(Address::Broadcast, &[0x00, 0b01000000])
}
pub fn configure(
&mut self,
log_scale: bool,
power_save: bool,
auto_incr: bool,
pwm_dithering: bool,
max_current_option: bool,
global_off: bool,
) -> Result<(), Error> {
let value: u8 = 0x00
| (log_scale as u8) << 5
| (power_save as u8) << 4
| (auto_incr as u8) << 3
| (pwm_dithering as u8) << 2
| (max_current_option as u8) << 1
| (global_off as u8) << 0;
self.write(Address::Broadcast, &[0x01, value])
}
}
impl<I2C, EN> LP50xx<ColorMode, I2C, EN>
where
I2C: embedded_hal::blocking::i2c::Write,
EN: OutputPin,
{
pub fn set(
&mut self,
mut channel: u8,
(brightness, [r, g, b]): (u8, [u8; 3]),
) -> Result<(), Error> {
if channel < 1 {
panic!("Specified Channel index must be greater than 0");
}
channel = channel - 1;
let bright_addr = 0x07 + channel as u8;
let color_addr = 0x0b + (channel as u8) * 3;
self.write(self.active_address, &[bright_addr, brightness])?;
self.write(self.active_address, &[color_addr, r, g, b])?;
Ok(())
}
}
impl<I2C, EN> LP50xx<MonochromaticMode, I2C, EN>
where
I2C: embedded_hal::blocking::i2c::Write,
EN: OutputPin,
{
pub fn set(&mut self, led: u8, value: u8) -> Result<(), Error> {
if led == 0 {
panic!("Specified LED index must be greater than 0");
}
if !self.continuous_addressing && led > self.model.get_pin_count() {
panic!("Specified LED is not supported");
}
let led_base_address = 0x0B;
let (address, pin_offset) = if self.continuous_addressing {
let addr_offset = get_led_address_offset(led, self.model);
let addr = Address::Independent(addr_offset);
let pin_offset = led - (addr_offset * self.model.get_pin_count());
(addr, pin_offset)
} else {
(self.active_address, led)
};
self.write(address, &[led_base_address + (pin_offset - 1), value])?;
Ok(())
}
}
fn get_led_address_offset(led_index: u8, model: Model) -> u8 {
let length = model.get_pin_count();
if led_index <= length {
return 0x00;
}
if led_index > length && led_index <= (length * 2) {
return 0x01;
}
return 0x02;
}
#[cfg(test)]
mod tests {
#[test]
fn correct_led_address_offset() {
let offset = super::get_led_address_offset(1, super::Model::LP5012);
assert_eq!(offset, 0x00);
let offset = super::get_led_address_offset(12, super::Model::LP5012);
assert_eq!(offset, 0x00);
let offset = super::get_led_address_offset(13, super::Model::LP5012);
assert_eq!(offset, 0x01);
let offset = super::get_led_address_offset(24, super::Model::LP5012);
assert_eq!(offset, 0x01);
let offset = super::get_led_address_offset(25, super::Model::LP5012);
assert_eq!(offset, 0x02);
}
}