-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathLEDController.cpp
131 lines (106 loc) · 3.5 KB
/
LEDController.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
#include "LEDController.h"
#include "Logging.h"
#include "HardwareConfig.h"
#include <boost/timer.hpp>
LEDController* LEDController::Instance = 0;
// wiringPi only compiles on the RASPBERRY_PI
#ifdef RASPBERRY_PI
#include <wiringPi.h>
#endif
LEDController::LEDController() : m_isSetup(true), m_prevColorBuffer(0)
{
LOG_INFO("LEDController: setting up with ClockPin1=" << GPIO_CLOCK_PIN1 << " DataPin1=" << GPIO_DATA_PIN1
<< " ClockPin2=" << GPIO_CLOCK_PIN2 << " DataPin2=" << GPIO_DATA_PIN2);
Instance = this;
// Prev color buffer for interpolation
m_prevColorBuffer = new Color[TOTAL_NUM_LEDS];
for (int i=0; i<TOTAL_NUM_LEDS; i++)
m_prevColorBuffer[i] = Color(0, 0, 0);
#ifdef RASPBERRY_PI
if (wiringPiSetup() < 0)
{
LOG_ERROR("LEDController: Failed to setup wiringPi");
m_isSetup = false;
return;
}
pinMode(GPIO_CLOCK_PIN1, OUTPUT);
pinMode(GPIO_CLOCK_PIN2, OUTPUT);
pinMode(GPIO_DATA_PIN1, OUTPUT);
pinMode(GPIO_DATA_PIN2, OUTPUT);
#endif
}
void LEDController::UpdateLeds(Color* colorBuffer, float deltaTime, float fadeTimeMS)
{
if (!m_isSetup)
return;
if (colorBuffer == 0)
return;
// This will update the led strands of 25 separately
// I could have just attached the two strands together making it
// one big strand of 50, which uses just on data and one clock pin.
// But unfortunately I was getting a lot of noise at the end of the
// strand, so I split it up
// Linear interpolation term for smoothness!
float deltaTimeMS = deltaTime * 1000.0f;
if (fadeTimeMS < deltaTimeMS)
fadeTimeMS = deltaTimeMS;
float lerpTerm = deltaTimeMS/fadeTimeMS;
// Update first strand of 25
for (int i=0; i<NUM_LEDS_PER_STRAND; ++i)
{
Color color = lerpColor(m_prevColorBuffer[i], colorBuffer[i], lerpTerm);
ShiftOut8Bits(GPIO_CLOCK_PIN1, GPIO_DATA_PIN1, color.ByteR());
ShiftOut8Bits(GPIO_CLOCK_PIN1, GPIO_DATA_PIN1, color.ByteG());
ShiftOut8Bits(GPIO_CLOCK_PIN1, GPIO_DATA_PIN1, color.ByteB());
m_prevColorBuffer[i] = color;
}
// Update second strand of 25
for (int i=NUM_LEDS_PER_STRAND; i<TOTAL_NUM_LEDS; ++i)
{
Color color = lerpColor(m_prevColorBuffer[i], colorBuffer[i], lerpTerm);
ShiftOut8Bits(GPIO_CLOCK_PIN2, GPIO_DATA_PIN2, color.ByteR());
ShiftOut8Bits(GPIO_CLOCK_PIN2, GPIO_DATA_PIN2, color.ByteG());
ShiftOut8Bits(GPIO_CLOCK_PIN2, GPIO_DATA_PIN2, color.ByteB());
m_prevColorBuffer[i] = color;
}
#ifdef RASPBERRY_PI
digitalWrite(GPIO_CLOCK_PIN1, 0);
digitalWrite(GPIO_CLOCK_PIN2, 0);
delay(1);
#endif
}
void LEDController::UpdateLedsFixed(Color fixedColor, float deltaTime)
{
// Update first strand of 25
for (int i=0; i<NUM_LEDS_PER_STRAND; ++i)
{
ShiftOut8Bits(GPIO_CLOCK_PIN1, GPIO_DATA_PIN1, fixedColor.ByteR());
ShiftOut8Bits(GPIO_CLOCK_PIN1, GPIO_DATA_PIN1, fixedColor.ByteG());
ShiftOut8Bits(GPIO_CLOCK_PIN1, GPIO_DATA_PIN1, fixedColor.ByteB());
}
// Update second strand of 25
for (int i=NUM_LEDS_PER_STRAND; i<TOTAL_NUM_LEDS; ++i)
{
ShiftOut8Bits(GPIO_CLOCK_PIN2, GPIO_DATA_PIN2, fixedColor.ByteR());
ShiftOut8Bits(GPIO_CLOCK_PIN2, GPIO_DATA_PIN2, fixedColor.ByteG());
ShiftOut8Bits(GPIO_CLOCK_PIN2, GPIO_DATA_PIN2, fixedColor.ByteB());
}
#ifdef RASPBERRY_PI
digitalWrite(GPIO_CLOCK_PIN1, 0);
digitalWrite(GPIO_CLOCK_PIN2, 0);
delay(1);
#endif
}
void LEDController::ShiftOut8Bits(int clockPin, int dataPin, uint8_t c)
{
#ifdef RASPBERRY_PI
for (int bit = 0; bit < 8; bit++)
{
bool val = (c >> (7 - bit)) & 1;
digitalWrite(clockPin, 0);
digitalWrite(dataPin, val);
nanosleep(100, NULL);
digitalWrite(clockPin, 1);
}
#endif
}