-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapa102LEDStrip.cpp
63 lines (56 loc) · 1.66 KB
/
apa102LEDStrip.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
#include "apa102LEDStrip.h"
apa102LEDStrip::apa102LEDStrip()
{
}
void apa102LEDStrip::init(unsigned short int numLEDs, byte bytesPerLED, byte globalBrightness)
{
_numLEDs = numLEDs;
_bytesPerLED = bytesPerLED;
_endFrameLength = 1;//round( (numLEDs/2)/8 );
_frameLength = (1+numLEDs+_endFrameLength)*bytesPerLED;
_globalBrightness = globalBrightness;
LEDs = new byte[_frameLength];
//Start Frame
LEDs[0] = 0;
LEDs[1] = 0;
LEDs[2] = 0;
LEDs[3] = 0;
//Driver frame+PIXEL frames
for(_counter=_bytesPerLED; _counter<_frameLength-(_endFrameLength*_bytesPerLED); _counter+=_bytesPerLED)
{
LEDs[_counter] = _globalBrightness;
LEDs[_counter+1] = 0;
LEDs[_counter+2] = 0;
LEDs[_counter+3] = 0;
}
//END frames
for(_counter=_frameLength-(_endFrameLength*_bytesPerLED); _counter<_frameLength; _counter+=_bytesPerLED)
{
LEDs[_counter] = 255;
LEDs[_counter+1] = 255;
LEDs[_counter+2] = 255;
LEDs[_counter+3] = 255;
}
}
void apa102LEDStrip::setPixel(short int pixelIndex, byte *pixelColour)
{
_counter = 4*(pixelIndex+1);
LEDs[ _counter + 1 ] = pixelColour[2];
LEDs[ _counter + 2 ] = pixelColour[1];
LEDs[ _counter + 3 ] = pixelColour[0];
}
void apa102LEDStrip::getPixel(short int pixelIndex, byte *pixelColour)
{
_counter = 4*(pixelIndex+1);
pixelColour[2] = LEDs[ _counter + 1 ];
pixelColour[1] = LEDs[ _counter + 2 ];
pixelColour[0] = LEDs[ _counter + 3 ];
}
void apa102LEDStrip::setGlobalBrightness(byte globalBrightness)
{
_globalBrightness = globalBrightness;
for(_index=_bytesPerLED; _index<(_frameLength-(_endFrameLength*_bytesPerLED)); _index+=_bytesPerLED)
{
LEDs[_index] = _globalBrightness;
}
}