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
#![no_std]
use embedded_hal as hal;
#[derive(Debug)]
pub enum Error<CommE> {
Comm(CommE),
}
pub struct TCA62724FMG<I2C> {
i2c_port: I2C,
address: u8,
}
impl<I2C, CommE> TCA62724FMG<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.get_enabled()?;
self.set_enable(false)?;
self.set_white_brightness(BRIGHTNESS_OFF)
}
pub fn set_white_brightness(&mut self, brightness: u8) -> Result<(), Error<CommE>> {
let bright = BRIGHTNESS_MAX & brightness;
self.set_color_brightness(bright, bright, bright)
}
pub fn set_enable(&mut self, enable: bool) -> Result<(), Error<CommE>> {
let settings =
if enable {
BIT_SHDN | BIT_ENABLE
}
else {
0
};
self.write_sub_address(SUBADDR_NAA_SETTINGS, settings)
}
pub fn write_sub_address(&mut self, subaddr: u8, val: u8) -> Result<(), Error<CommE>> {
let write_buf = [subaddr, val];
self.i2c_port
.write(self.address, &write_buf)
.map_err(Error::Comm)?;
Ok(())
}
pub fn set_color_brightness(&mut self, red: u8, green: u8, blue: u8)
-> Result<(), Error<CommE>> {
let write_buf = [
SUBADDR_AA_PWM0,
blue & BRIGHTNESS_MAX,
green & BRIGHTNESS_MAX,
red & BRIGHTNESS_MAX,
];
self.i2c_port
.write(self.address, &write_buf)
.map_err(Error::Comm)?;
Ok(())
}
pub fn get_enabled(&mut self) -> Result<bool, Error<CommE>> {
let mut read_buf = [0u8; 2];
self.i2c_port
.read(self.address, &mut read_buf)
.map_err(Error::Comm)?;
let enabled = (read_buf[0] & (BIT_ENABLE | BIT_SHDN)) == (BIT_ENABLE | BIT_SHDN);
Ok(enabled)
}
}
const DEFAULT_I2C_ADDRESS: u8 = 0x55;
const SUBADDR_AA_PWM0: u8 = 0x01;
const SUBADDR_AA_PWM1: u8 = 0x02;
const SUBADDR_AA_PWM2: u8 = 0x03;
const AUTO_INCREMENT_OFF: u8 = 0x80;
const SUBADDR_PWM0 : u8 = (SUBADDR_AA_PWM0 | AUTO_INCREMENT_OFF);
const SUBADDR_NAA_SETTINGS : u8 = (0x04 | AUTO_INCREMENT_OFF);
pub const BRIGHTNESS_MAX: u8 = 0x0f;
pub const BRIGHTNESS_HALF: u8 = 0x07;
pub const BRIGHTNESS_LOW: u8 = 0x0f;
pub const BRIGHTNESS_OFF: u8 = 0x00;
const SET_POWERSAVE_OFF: u8 = 0x01;
const SET_ENABLE: u8 = 0x02;
const BIT_SHDN: u8 = (1 << 0);
const BIT_ENABLE: u8 = (1 << 1);