-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPiLED.cpp
80 lines (65 loc) · 1.98 KB
/
PiLED.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
#include "PiLED.h"
#include <unistd.h>
#include <cerrno>
#include <wiringPiSPI.h>
PiLED::PiLED() {
m_Scale = 255;
colorCorrection = CRGB::White;
colorTemperature = CRGB::White;
spiFileDescriptor = wiringPiSPISetup(0, 6000000); //Channel 0, 6MHz clock
if (spiFileDescriptor < 0) {
fprintf(stderr, "SPI Setup failed: %s\n", strerror(errno));
}
}
PiLED::PiLED(struct CRGB *data, int nLeds) : PiLED() {
this->data = data;
this->nLeds = nLeds;
}
void PiLED::show(uint8_t brightness) {
show(getAdjustment(brightness));
}
void PiLED::showColor(const struct CRGB & color, uint8_t brightness) {
CRGB scale = getAdjustment(brightness);
uint8_t ledBuffer[4] = {255, scale8(color.b, scale.b), scale8(color.g, scale.g), scale8(color.r, scale.r)};
write(spiFileDescriptor, (const uint8_t[]){0, 0, 0, 0}, 4); //start boundary
for (int i=0; i<nLeds; i++) {
write(spiFileDescriptor, ledBuffer, 4);
}
}
void PiLED::clear(bool writeData) {
if (writeData) {
showColor(CRGB(0,0,0), 0);
}
clearData();
}
void PiLED::clearData() {
if (data) {
memset8((void*)data, 0, sizeof(struct CRGB) * nLeds);
}
}
void PiLED::setTemperature(const struct CRGB & temp) {
colorTemperature = temp;
}
void PiLED::setCorrection(const struct CRGB & correction) {
colorCorrection = correction;
}
void PiLED::show(CRGB scale) {
write(spiFileDescriptor, (const uint8_t[]){0, 0, 0, 0}, 4); //start boundary
if (data) {
uint8_t ledBuffer[4];
for (int i=0; i<nLeds; i++) {
ledBuffer[0] = 255;
ledBuffer[1] = scale8(data[i].b, scale.b);
ledBuffer[2] = scale8(data[i].g, scale.g);
ledBuffer[3] = scale8(data[i].r, scale.r);
// ledBuffer[1] = data[i].b;
// ledBuffer[2] = data[i].g;
// ledBuffer[3] = data[i].r;
write(spiFileDescriptor, ledBuffer, 4);
}
}
}
/// Get the combined brightness/color adjustment
CRGB PiLED::getAdjustment(uint8_t scale) {
return computeAdjustment(scale, colorCorrection, colorTemperature);
}