-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTLC5951.cpp
155 lines (120 loc) · 3.1 KB
/
TLC5951.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
#include "tlc5951.h"
#include <SPI.h>
#define bit_set(p,m) ((p) |= (m))
#define bit_clear(p,m) ((p) &= ~(m))
#define bit_write(c,p,m) (c ? bit_set(p,m) : bit_clear(p,m))
void TLC5951::init(uint8_t gslat, uint8_t xblnk) {
_gslat = gslat;
_xblnk = xblnk;
_bufferCount = 0;
pinMode(_gslat, OUTPUT);
pinMode(_xblnk, OUTPUT);
digitalWrite(_gslat, LOW);
digitalWrite(_xblnk, LOW);
}
void TLC5951::init(uint8_t gslat, uint8_t xblnk, uint16_t grayscale) {
_gslat = gslat;
_xblnk = xblnk;
_bufferCount = 0;
pinMode(_gslat, OUTPUT);
pinMode(_xblnk, OUTPUT);
digitalWrite(_gslat, LOW);
digitalWrite(_xblnk, LOW);
setAllGSData(grayscale);
}
void TLC5951::setAllGSData(uint16_t gsvalue) {
for(int8_t a = 0; a < 8; a++) {
for(int8_t b = 0; b < 3; b++) {
_gsData[a][b] = gsvalue;
}
}
}
void TLC5951::updateGS() {
_bufferCount = 0;
digitalWrite(_xblnk, LOW); // Turn off the LED's since we're clocking in data
digitalWrite(_gslat, LOW); // GS Latch low, so it goes into the GS data latch
for(int8_t a = 7; a >= 0; a--) { // We have 8 LED's. Start at the last since thats how we clock data out
for(int8_t b = 2; b >= 0; b--) { // Each with 3 colors
for(int8_t c = 11; c >= 0; c--) { // each consiting of 12 bits
setBuffer((_gsData[a][b] & (1<<c)));
}
}
}
delay(1);
digitalWrite(_gslat, HIGH);
digitalWrite(_gslat, LOW);
digitalWrite(_xblnk, HIGH);
}
void TLC5951::setLED(uint8_t led, uint16_t red, uint16_t green, uint16_t blue) {
_gsData[led][2] = blue;
_gsData[led][1] = green;
_gsData[led][0] = red;
}
void TLC5951::setFunctionData(uint8_t data) {
_functionData = data;
}
void TLC5951::setBrightness(uint8_t red, uint8_t green, uint8_t blue) {
_brightRed = red;
_brightGreen = green;
_brightBlue = blue;
}
void TLC5951::setAllDCData(uint8_t dcvalue) {
for(int8_t a = 0; a < 8; a++) {
for(int8_t b = 0; b < 3; b++) {
_dcData[a][b] = dcvalue;
}
}
}
void TLC5951::updateControl() {
_bufferCount = 0;
digitalWrite(_xblnk, LOW);
digitalWrite(_gslat, HIGH);
// 89 blank bits to get to correct position for DC/DC/FC (Disregard UD) data
for(int8_t a = 0; a < 89; a++) {
setBuffer(0);
}
// 7-bit Function Data
for(int8_t a = 6; a >= 0; a--) {
setBuffer((_functionData & (1<<a)));
}
// Blue Brightness
for(int8_t a = 7; a >= 0; a--) {
setBuffer((_brightBlue & (1<<a)));
}
// Green Brightness
for(int8_t a = 7; a >= 0; a--) {
setBuffer((_brightGreen & (1<<a)));
}
// Red Brightness
for(int8_t a = 7; a >= 0; a--) {
setBuffer((_brightRed & (1<<a)));
}
// Dot Correctness data
for(int8_t a = 7; a >= 0; a--) {
for(int8_t b = 2; b >= 0; b--) {
for(int8_t c = 6; c >= 0; c--) {
setBuffer(_dcData[a][b] & (1<<c));
}
}
}
digitalWrite(_gslat, LOW);
delayMicroseconds(10);
digitalWrite(_gslat, HIGH);
digitalWrite(_gslat, LOW);
digitalWrite(_xblnk, HIGH);
}
void TLC5951::update() {
updateControl();
updateGS();
}
void TLC5951::setBuffer(uint8_t bit){
bitWrite(_buffer, _bufferCount, bit);
_bufferCount++;
if(_bufferCount == 8)
{
SPI.setBitOrder(LSBFIRST);
SPI.transfer(_buffer);
_bufferCount = 0;
_buffer = 0;
}
}