-
Notifications
You must be signed in to change notification settings - Fork 3
/
TLC5973.cpp
93 lines (79 loc) · 1.71 KB
/
TLC5973.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
#include "TLC5973.h"
#define NOP __asm__ __volatile__ ("nop\n\t")
TLC5973::TLC5973(uint16_t n, uint8_t p)
{
pin = p;
numLEDs = n;
updateLength();
}
void TLC5973::updateLength() {
if(pixels) free(pixels); // Free existing data (if any)
numWords = numLEDs * 3;
if((pixels = (uint16_t *)malloc(numWords)))
memset(pixels, 0, numWords);
else numLEDs = numWords = 0;
}
void TLC5973::begin(void) {
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
port = portOutputRegister(digitalPinToPort(pin));
pinMask = digitalPinToBitMask(pin);
}
// Set pixel color from separate R,G,B components:
void TLC5973::setPixelColor(uint16_t n, uint16_t r, uint16_t g, uint16_t b) {
if(n < numLEDs) {
uint16_t *p;
p = &pixels[n * 3]; // 3 bytes per pixel
p[2] = r; // R,G,B always stored
p[1] = g;
p[0] = b;
}
}
void TLC5973::pulse()
{
*port|=pinMask;
*port&=~pinMask;
}
void TLC5973::writeZero(){
pulse();
NOP;
NOP;
}
void TLC5973::writeNone(){
NOP;
NOP;
NOP;
NOP;
}
void TLC5973::writeOne(){
pulse();
pulse();
}
void TLC5973::waitGSLAT(uint8_t num){
unsigned char i;
for(i = 0; i < num; i++){
writeNone();
}
}
void TLC5973::writeWord(uint16_t word){
unsigned char i;
for(i = 0; i < 12; i++){
if(word & 0x800)
writeOne();
else
writeZero();
word <<= 1;
}
}
void TLC5973::show(){
noInterrupts(); // Need 100% focus on instruction timing
for(uint16_t i = 0; i < numWords; i=i+3){
writeWord( 0x03AA);
writeWord( pixels[i] );
writeWord( pixels[i+1] );
writeWord( pixels[i+2] );
waitGSLAT(4);
}
waitGSLAT(4);
interrupts();
}