forked from CrosseyeJack/RaspberryPi-NeoPixel-WS2812
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws2812-RPi-effects.c
246 lines (219 loc) · 6.24 KB
/
ws2812-RPi-effects.c
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
// =================================================================================================
// ___________ _____ _____ __
// \_ _____// ____\/ ____\____ _____/ |_ ______
// | __)_\ __\\ __\/ __ \_/ ___\ __\/ ___/
// | \| | | | \ ___/\ \___| | \___ \
// /_______ /|__| |__| \___ >\___ >__| /____ >
// \/ \/ \/ \/
// =================================================================================================
// The effects in this section are adapted from the Adafruit NeoPixel library at:
// https://github.com/adafruit/Adafruit_NeoPixel/blob/master/examples/strandtest/strandtest.ino
*/
#include "ws2812-RPi.h"
#include "ws2812-RPi-effects.h"
float twinklearray[LED_BUFFER_LENGTH];
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
Color_t Wheel(uint8_t WheelPos) {
if(WheelPos < 85) {
return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
// Fill the dots one after the other with a color
void colorWipe(Color_t c, uint8_t wait) {
uint16_t i;
for(i=0; i<numPixels(); i++) {
setPixelColorT(i, c);
show();
usleep(wait * 1000);
}
}
// Fill the dots one after the other with a color
// Same as above - just the other way around the chain
void colorWipe_r(Color_t c, uint8_t wait) {
int16_t i;
for(i=numPixels()-1; i >= 0; i--) {
setPixelColorT(i, c);
show();
usleep(wait * 1000);
}
}
// Rainbow
void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<numPixels(); i++) {
setPixelColorT(i, Wheel((i+j) & 255));
}
show();
usleep(wait * 1000);
}
}
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
j = 0;
for(i=0; i<numPixels(); i++) {
setPixelColorT(i, Wheel(((i * 256 / numPixels()) + j) & 255));
show();
usleep(50000);
}
for(j = 0; j<256*5; j++) { // 5 cycles of all colors on wheel
for(i=0; i<numPixels(); i++) {
setPixelColorT(i, Wheel(((i * 256 / numPixels()) + j) & 255));
}
show();
usleep(wait * 1000);
}
}
// Slightly different, this makes the rainbow equally distributed throughout
// Same as above - just the other way around the chain
void rainbowCycle_r(uint8_t wait) {
uint16_t i, j;
for(j=256*5; j>0; j--) { // 5 cycles of all colors on wheel
for(i=0; i<numPixels(); i++) {
setPixelColorT(i, Wheel(((i * 256 / numPixels()) + j) & 255));
}
show();
usleep(wait * 1000);
}
}
// Slightly different, this makes the rainbow equally distributed throughout
// Same as rainbowCycle() but does a colour wipe with a rainbow effect before starting the Cycle
void rainbowCycle_wipe(uint8_t wait) {
uint16_t i, j;
j = 0;
for(i=numPixels(); i>0; i--) {
setPixelColorT(i, Wheel(((i * 256 / numPixels()) + j) & 255));
show();
usleep(50000);
}
// for(j; j<256*5; j++) { // 5 cycles of all colors on wheel
// for(i=0; i<numPixels(); i++) {
// setPixelColorT(i, Wheel(((i * 256 / numPixels()) + j) & 255));
// }
// show();
// usleep(wait * 1000);
// }
}
//Theatre-style crawling lights.
void theaterChase(Color_t c, uint8_t wait) {
unsigned int j, q, i;
for (j=0; j<15; j++) { //do this many cycles of chasing
for (q=0; q < 3; q++) {
for (i=0; i < numPixels(); i=i+3) {
setPixelColorT(i+q, c); // Turn every third pixel on
}
show();
usleep(wait * 1000);
for (i=0; i < numPixels(); i=i+3) {
setPixelColor(i+q, 0, 0, 0); // Turn every third pixel off
}
}
}
}
//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
int j, q, i;
for (j=0; j < 256; j+=4) { // cycle through every 4th color on the wheel
for (q=0; q < 3; q++) {
for (i=0; i < numPixels(); i=i+3) {
setPixelColorT(i+q, Wheel((i+j) % 255)); //turn every third pixel on
}
show();
usleep(wait * 1000);
for (i=0; i < numPixels(); i=i+3) {
setPixelColor(i+q, 0, 0, 0); //turn every third pixel off
}
}
}
}
/**
* "Rain Fall" effect - Test
* @param c
* @param wait [in ms]
* @param sleepafert [in s]
*/
void RainFall(Color_t c,uint8_t wait,int sleepafter) {
int j, k;
j = (numPixels() / 2) - 1;
k = j + 1;
for (; j >= 0; j--,k++) {
// printf("Pixel - %i %i\n", j, k);
setPixelColorT(j, c);
setPixelColorT(k, c);
show();
usleep(wait * 1000);
}
sleep(sleepafter);
//printf("Done:\n");
}
void Twinkle() {
int j;
for (j=0;j<numPixels();j++) {
if (twinklearray[j] == 0) { // LED is off - Give it a chance to turn on.
if ((rand() % 20) == 1) { // LED Got the change to be turned on now lets give it a random brightness level
twinklearray[j] = (float) rand()/RAND_MAX;
setPixelColor_B(j, 255, 255, 255,twinklearray[j]);
}
}
else if (twinklearray[j] < 0.000) {
twinklearray[j] = 0;
setPixelColor(j, 0, 0, 0);
}
else if (twinklearray[j] > 0.000) {
twinklearray[j] = twinklearray[j] - 0.1;
if (twinklearray[j]<0) twinklearray[j] = 0;
setPixelColor_B(j, 255, 255, 255,twinklearray[j]);
}
}
show();
usleep(100000);
}
void Twinkle_Fade() {
int j,i;
for (i=0;i<15;i++) {
for (j=0;j<numPixels();j++) {
if (twinklearray[j] < 0.000) {
twinklearray[j] = 0;
setPixelColor(j, 0, 0, 0);
}
else if (twinklearray[j] > 0.000) {
twinklearray[j] = twinklearray[j] - 0.1;
if (twinklearray[j]<0) twinklearray[j] = 0;
setPixelColor_B(j, 255, 255, 255,twinklearray[j]);
}
}
show();
usleep(100000);
}
}
void Twinkle_T(Color_t c) {
int j;
for (j=0;j<numPixels();j++) {
if (twinklearray[j] == 0) { // LED is off - Give it a chance to turn on.
if ((rand() % 15) == 1) { // LED Got the change to be turned on now lets give it a random brightness level
twinklearray[j] = (float) rand()/RAND_MAX;
setPixelColorT_B(j, c, twinklearray[j]);
}
}
else if (twinklearray[j] < 0.000) {
twinklearray[j] = 0;
setPixelColor(j, 0, 0, 0);
}
else if (twinklearray[j] > 0.000) {
twinklearray[j] = twinklearray[j] - 0.1;
if (twinklearray[j]<0) twinklearray[j] = 0;
setPixelColorT_B(j, c, twinklearray[j]);
}
}
show();
usleep(50000);
}