forked from adafruit/Adafruit_TLC59711
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adafruit_TLC59711.cpp
220 lines (187 loc) · 5.03 KB
/
Adafruit_TLC59711.cpp
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
/*!
* @file Adafruit_TLC59711.cpp
*
* @mainpage Adafruit TLC59711 PWM/LED driver
*
* @section intro_sec Introduction
*
* This is a library for our Adafruit 12-channel PWM/LED driver
*
* Pick one up today in the adafruit shop!
* ------> http://www.adafruit.com/products/1455
*
* Two SPI Pins are required to send data: clock and data pin.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* @section author Author
*
* Written by Limor Fried/Ladyada for Adafruit Industries.
*
* @section license License
*
* BSD license, all text above must be included in any redistribution
*/
#include <Adafruit_TLC59711.h>
/*!
* @brief Instantiates a new Adafruit_TLC59711 class
* @param n
* number of connected drivers
* @param c
* clock pin
* @param d
* data pin
*/
Adafruit_TLC59711::Adafruit_TLC59711(uint8_t n, uint8_t c, uint8_t d) {
numdrivers = n;
BCr = BCg = BCb = 0x7F; // default 100% brigthness
pwmbuffer = (uint16_t *)calloc(2, 12 * n);
_spi_dev = new Adafruit_SPIDevice(-1, c, -1, d, 1000000);
}
/*!
* @brief Instantiates a new Adafruit_TLC59711 class using provided SPI
* @param n
* number of connected drivers
* @param *theSPI
* spi object
*/
Adafruit_TLC59711::Adafruit_TLC59711(uint8_t n, SPIClass *theSPI) {
numdrivers = n;
BCr = BCg = BCb = 0x7F; // default 100% brigthness
pwmbuffer = (uint16_t *)calloc(2, 12 * n);
_spi_dev = new Adafruit_SPIDevice(-1, 1000000, SPI_BITORDER_MSBFIRST,
SPI_MODE0, theSPI);
}
/*!
* @brief Writes PWM buffer to board
*/
void Adafruit_TLC59711::write() {
uint32_t command;
// Magic word for write
command = 0x25;
command <<= 5;
// OUTTMG = 1, EXTGCK = 0, TMGRST = 1, DSPRPT = 1, BLANK = 0 -> 0x16
command |= 0x16;
command <<= 7;
command |= BCr;
command <<= 7;
command |= BCg;
command <<= 7;
command |= BCb;
noInterrupts();
_spi_dev->beginTransaction();
for (uint8_t n = 0; n < numdrivers; n++) {
_spi_dev->transfer(command >> 24);
_spi_dev->transfer(command >> 16);
_spi_dev->transfer(command >> 8);
_spi_dev->transfer(command);
// 12 channels per TLC59711
for (int8_t c = 11; c >= 0; c--) {
// 16 bits per channel, send MSB first
_spi_dev->transfer(pwmbuffer[n * 12 + c] >> 8);
_spi_dev->transfer(pwmbuffer[n * 12 + c]);
}
}
delayMicroseconds(200);
_spi_dev->endTransaction();
interrupts();
}
/*!
* @brief Set PWM value on selected channel
* @param chan
* one from 12 channel (per driver) so there is 12 * number of drivers
* @param pwm
* pwm value
*/
void Adafruit_TLC59711::setPWM(uint16_t chan, uint16_t pwm) {
if (chan > 12 * numdrivers)
return;
pwmbuffer[chan] = pwm;
}
/*!
* @brief Set RGB value for selected LED
* @param lednum
* selected LED number that for which value will be set
* @param r
* red value
* @param g
* green value
* @param b
* blue value
*/
void Adafruit_TLC59711::setLED(uint16_t lednum, uint16_t r, uint16_t g,
uint16_t b) {
setPWM(lednum * 3, r);
setPWM(lednum * 3 + 1, g);
setPWM(lednum * 3 + 2, b);
}
/*!
* @brief Get RGB value for selected LED
* @param lednum
* selected LED number that for which value will be set
* @param r
* red value
* @param g
* green value
* @param b
* blue value
*/
void Adafruit_TLC59711::getLED(uint16_t lednum, uint16_t &r, uint16_t &g,
uint16_t &b) {
r = pwmbuffer[lednum * 3];
g = pwmbuffer[lednum * 3 + 1];
b = pwmbuffer[lednum * 3 + 2];
}
/*!
* @brief Set the brightness of LED channels to same value
* @param BC
* Brightness Control value
*/
void Adafruit_TLC59711::simpleSetBrightness(uint8_t BC) {
if (BC > 127) {
BC = 127; // maximum possible value since BC can only be 7 bit
} else if (BC < 0) {
BC = 0;
}
BCr = BCg = BCb = BC;
}
/*!
* @brief Set the brightness of LED channels to specific value
* @param bcr
* Brightness Control Red value
* @param bcg
* Brightness Control Green value
* @param bcb
* Brightness Control Blue value
*/
void Adafruit_TLC59711::setBrightness(uint8_t bcr, uint8_t bcg, uint8_t bcb) {
if (bcr > 127) {
bcr = 127; // maximum possible value since BC can only be 7 bit
} else if (bcr < 0) {
bcr = 0;
}
BCr = bcr;
if (bcg > 127) {
bcg = 127; // maximum possible value since BC can only be 7 bit
} else if (bcg < 0) {
bcg = 0;
}
BCg = bcg;
if (bcb > 127) {
bcb = 127; // maximum possible value since BC can only be 7 bit
} else if (bcb < 0) {
bcb = 0;
}
BCb = bcb;
}
/*!
* @brief Begins SPI connection if there is not empty PWM buffer
* @return If successful returns true, otherwise false
*/
bool Adafruit_TLC59711::begin() {
if (!pwmbuffer)
return false;
return _spi_dev->begin();
}