forked from adafruit/Adafruit_TLC5947
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdafruit_TLC5947.cpp
155 lines (121 loc) · 3.85 KB
/
Adafruit_TLC5947.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
/***************************************************
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/1429
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_TLC5947.h>
#include <SPI.h>
Adafruit_TLC5947::Adafruit_TLC5947() {
// constructor empty
// initialize with init() to allow for Serial.debugging
}
// software SPI mode
void Adafruit_TLC5947::init(uint16_t n, uint8_t c, uint8_t d, uint8_t l, uint8_t b) {
Serial.println("Adafruit_TLC5947 init for Software SPI");
numdrivers = n;
_clk = c;
_dat = d;
_lat = l;
_blank = b;
//pwmbuffer = (uint16_t *)calloc(2, 24*n);
pwmbuffer = (uint16_t *)malloc(2 * 24*n);
memset(pwmbuffer, 0, 2*24*n);
}
// hardware SPI mode
void Adafruit_TLC5947::init(uint16_t n, uint8_t l, uint8_t b) {
Serial.println("Adafruit_TLC5947 init for Hardware SPI");
numdrivers = n;
_clk = -1;
_dat = -1;
_lat = l;
_blank = b;
// set SPI settings in write() call to enable multiple parallel SPI calls/settings
// SPI.setBitOrder(MSBFIRST);
// #ifdef __arm__
// SPI.setClockDivider(42);
// #else
// SPI.setClockDivider(SPI_CLOCK_DIV8);
// #endif
// SPI.setDataMode(SPI_MODE0);
pwmbuffer = (uint16_t *)calloc(2, 24*n);
memset(pwmbuffer, 0, 2*24*n);
}
void Adafruit_TLC5947::spiwriteMSB(uint32_t d) {
if (_clk >= 0) {
uint32_t b = 0x80; // 0x80 = 128
// 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 {
SPI.transfer(d);
}
}
void Adafruit_TLC5947::write(void) {
unsigned int chan1 = 0;
unsigned int chan2 = 0;
byte address1 = 0;
byte address2 = 0;
byte address3 = 0;
// packing each 2 channel (12bit*2) to 3 byte (8bit*3) for transfering
SPI.beginTransaction(SPISettings(15000000, MSBFIRST, SPI_MODE0));
digitalWrite(_lat, LOW);
digitalWrite(_blank, LOW);
for (int ledpos = 24/2 * numdrivers - 1; ledpos >= 0; ledpos--) {
chan1 = pwmbuffer[ledpos*2 +1];
chan2 = pwmbuffer[ledpos*2];
address1 = (byte)(chan1 >> 4) ;
address2 = (byte)((chan1 << 4) & (B11110000)) + (byte)((chan2 >> 8) & (B00001111));
address3 = (byte)chan2;
// SPI.transfer(address1); // hardware SPI only
// SPI.transfer(address2);
// SPI.transfer(address3);
spiwriteMSB(address1); // to enable software-SPI as well
spiwriteMSB(address2);
spiwriteMSB(address3);
}
digitalWrite(_blank, HIGH);
digitalWrite(_lat, HIGH); // TSU2, go HIGH, minimum 30ns
SPI.endTransaction();
digitalWrite(_lat, LOW);
digitalWrite(_blank, LOW);
}
void Adafruit_TLC5947::setPWM(uint16_t chan, uint16_t pwm) {
if (pwm > 4095) pwm = 4095;
if (chan >= 24*numdrivers) return;
pwmbuffer[chan] = pwm;
// Serial.print("set ");
// Serial.print(chan);
// Serial.print(" to ");
// Serial.print(pwm);
// Serial.println();
}
void Adafruit_TLC5947::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);
}
boolean Adafruit_TLC5947::begin() {
if (!pwmbuffer) return false;
pinMode(_lat, OUTPUT);
pinMode(_blank, OUTPUT);
if (_clk >= 0) {
pinMode(_clk, OUTPUT);
pinMode(_dat, OUTPUT);
} else {
SPI.begin();
}
return true;
}