-
Notifications
You must be signed in to change notification settings - Fork 2
/
ws2812.cpp
98 lines (87 loc) · 2.41 KB
/
ws2812.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
#include "ws2812.h"
// holds the length of the ledstrip in pixels or chips.
static int ws2812_striplen;
// create a pointer of NeoPixelBus object type.
NeoPixelBus *strip = NULL;
// timing vairiables.
static unsigned long ccurrent = 0;
static unsigned long cprevious = 0;
static int cinterval = 10;
// variable that holds
static uint8_t *colors;
/* setup the neopixel object, and clear the ledstrip. */
void setupWS2812(uint16_t length, uint8_t pin)
{
ws2812_striplen = length;
colors = colorinc();
if(strip == NULL)
{
strip = new NeoPixelBus(length, pin);
}
else
{
delete strip;
strip = new NeoPixelBus(length, pin);
}
strip->Begin();
setWS2812Strip(0, 0, 0);
strip->Show();
}
/* set the ledstrip to a certain (r, g, b) value. */
void setWS2812Strip(int r, int g, int b)
{
for(int i = 0; i < ws2812_striplen;i++)
{
RgbColor color = RgbColor(r, g, b);
strip->SetPixelColor(i, color);
}
}
/* fade the whole ledstrip from one color to another. */
void fadeWS2812(int speed, int brightness)
{
ccurrent = millis();
if((ccurrent - cprevious) >= cinterval)
{
cprevious = ccurrent;
colorinc();
}
cinterval = speed+1;
float brightnessFactor = (float)(((float)brightness)/100);
int r = colors[RED] * brightnessFactor;
int g = colors[GREEN] * brightnessFactor;
int b = colors[BLUE] * brightnessFactor;
setWS2812Strip(r, g, b);
}
/*
fade all the pixels individually from one color to the next.
creating a rainbow like effect.
*/
void rainbowWS2812(int speed, int brightness)
{
cinterval = speed + 1;
ccurrent = millis();
if((ccurrent - cprevious) >= cinterval)
{
cprevious = ccurrent;
static int range = 0xff*3;
uint8_t buffer[3][ws2812_striplen];
int i, s;
colors = colorinc();
for(i = 0; i < ws2812_striplen; i++)
{
for(s = 0; s < range/ws2812_striplen; s++)
colors = colorinc();
float brightnessFactor = (float)(((float)brightness) / 100);
int r = colors[RED] * brightnessFactor;
int g = colors[GREEN] * brightnessFactor;
int b = colors[BLUE] * brightnessFactor;
RgbColor rgbcolor = RgbColor(r, g, b);
strip->SetPixelColor(i, rgbcolor);
}
}
}
/* update the while ledstrip. */
void updateWS2812()
{
strip->Show();
}