forked from adafruit/Adafruit_TLC59711
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdafruit_TLC59711.cpp
148 lines (111 loc) · 3.08 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
/***************************************************
This is a library for our Adafruit 24-channel PWM/LED driver
Pick one up today in the adafruit shop!
------> http://www.adafruit.com/products/1455
These drivers uses SPI to communicate, 3 pins are required to
interface: Data, Clock and Latch. The boards are chainable
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#include <Adafruit_TLC59711.h>
#include <SPI.h>
//Can work up to 10 MHz
SPISettings SPI_SETTINGS(10000000, MSBFIRST, SPI_MODE0);
Adafruit_TLC59711::Adafruit_TLC59711(uint8_t n, uint8_t c, uint8_t d) :
spidev(NULL) {
numdrivers = n;
_clk = c;
_dat = d;
BCr = BCg = BCb = 0x7F;
pwmbuffer = (uint16_t *)calloc(2, 12*n);
}
Adafruit_TLC59711::Adafruit_TLC59711(uint8_t n, SPIClass *spidev) :
spidev(spidev) {
numdrivers = n;
_clk = -1;
_dat = -1;
BCr = BCg = BCb = 0x7F;
pwmbuffer = (uint16_t *)calloc(2, 12*n);
}
void Adafruit_TLC59711::spiwriteMSB(uint32_t d) {
if (_clk >= 0) {
uint32_t b = 0x80;
// b <<= (bits-1);
for (; b!=0; b>>=1) {
digitalWrite(_clk, LOW);
if (d & b)
digitalWrite(_dat, HIGH);
else
digitalWrite(_dat, LOW);
digitalWrite(_clk, HIGH);
}
} else {
spidev->transfer(d);
}
}
void Adafruit_TLC59711::write(void) {
uint32_t command;
spidev->beginTransaction(SPI_SETTINGS);
// 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();
for (uint8_t n=0; n<numdrivers; n++) {
spiwriteMSB(command >> 24);
spiwriteMSB(command >> 16);
spiwriteMSB(command >> 8);
spiwriteMSB(command);
// 12 channels per TLC59711
for (int8_t c=11; c >= 0 ; c--) {
// 16 bits per channel, send MSB first
spiwriteMSB(pwmbuffer[n*12+c]>>8);
spiwriteMSB(pwmbuffer[n*12+c]);
}
}
if (_clk >= 0) {
delayMicroseconds(200);
}
else {
delayMicroseconds(2);
}
spidev->endTransaction();
interrupts();
}
void Adafruit_TLC59711::setPWM(uint8_t chan, uint16_t pwm) {
if (chan > 12*numdrivers) return;
pwmbuffer[chan] = pwm;
}
void Adafruit_TLC59711::setLED(uint8_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);
}
boolean Adafruit_TLC59711::begin() {
if (!pwmbuffer) return false;
if (_clk >= 0) {
pinMode(_clk, OUTPUT);
pinMode(_dat, OUTPUT);
} else {
spidev->begin();
}
return true;
}
void Adafruit_TLC59711::setBrightness(uint8_t rgb) {
BCr = BCg = BCb = rgb >> 1;
}
void Adafruit_TLC59711::setBrightness(uint8_t r, uint8_t g, uint8_t b) {
BCr = r >> 1;
BCg = g >> 1;
BCb = b >> 1;
}