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
#![no_std]
use embedded_hal as hal;
#[derive(Debug)]
pub enum Error<CommE> {
Comm(CommE),
}
pub struct NCP5623C<I2C> {
i2c_port: I2C,
address: u8,
}
impl<I2C, CommE> NCP5623C<I2C>
where
I2C: hal::blocking::i2c::Write<Error = CommE>
+ hal::blocking::i2c::Read<Error = CommE>
+ hal::blocking::i2c::WriteRead<Error = CommE>,
{
pub fn default(i2c_port: I2C) -> Result<Self, Error<CommE>> {
Self::new(i2c_port, DEFAULT_I2C_ADDRESS)
}
pub fn new(i2c_port: I2C, address: u8) -> Result<Self, Error<CommE>> {
let mut inst = Self {
i2c_port,
address
};
inst.reset()?;
Ok(inst)
}
pub fn reset(&mut self) -> Result<(), Error<CommE>> {
self.set_register_bits(IREG_SHUTDOWN, 0)
}
pub fn set_brightness(&mut self, current: u8) -> Result<(), Error<CommE>> {
self.set_register_bits( IREG_LED_CURRENT, current)
}
pub fn set_register_bits(&mut self, reg: u8, val: u8) -> Result<(), Error<CommE>> {
let write_buf = [(reg & IREG_REG_MASK) | (val & IREG_VAL_MASK)];
self.i2c_port
.write(self.address, &write_buf)
.map_err(Error::Comm)?;
Ok(())
}
pub fn set_color_brightness(&mut self, bright: u8, red: u8, green: u8, blue: u8)
-> Result<(), Error<CommE>> {
let write_buf = [
IREG_LED_CURRENT | (bright & IREG_VAL_MASK),
IREG_DIM_STEP_RUN,
IREG_PWM1 | (red & IREG_VAL_MASK),
IREG_DIM_STEP_RUN,
IREG_PWM2 | (green & IREG_VAL_MASK),
IREG_DIM_STEP_RUN,
IREG_PWM3 | (blue & IREG_VAL_MASK),
];
self.i2c_port
.write(self.address, &write_buf)
.map_err(Error::Comm)?;
Ok(())
}
}
const DEFAULT_I2C_ADDRESS: u8 = 0x39;
const IREG_VAL_MASK: u8 = 0x1f;
const IREG_REG_MASK: u8 = 0xe0;
pub const BRIGHTNESS_MAX: u8 = 0x1f;
pub const BRIGHTNESS_HALF: u8 = 0x18;
pub const BRIGHTNESS_LOW: u8 = 0x0f;
pub const BRIGHTNESS_OFF: u8 = 0x00;
pub const IREG_SHUTDOWN: u8 = 0x00;
pub const IREG_LED_CURRENT: u8 = 0x20;
pub const IREG_PWM1: u8 = 0x40;
pub const IREG_PWM2: u8 = 0x60;
pub const IREG_PWM3: u8 = 0x80;
pub const IREG_UPWARD_LEND: u8 = 0xA0;
pub const IREG_DOWNWARD_LEND: u8 = 0xC0;
pub const IREG_DIM_STEP_RUN: u8 = 0xE0;