-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathws2801.cpp
123 lines (111 loc) · 3.25 KB
/
ws2801.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
#include "ws2801.h"
// hold array of colors
// r[], g[], b[]
static uint8_t *buffer[3] = {NULL, NULL, NULL};
// length of ledstrip in chips
static int ws2801_striplen;
// timing variables.
static unsigned long ccurrent = 0;
static unsigned long cprevious = 0;
static int cinterval = 10;
// pointer to generated colors.
static uint8_t *colors = NULL;
/* setup basics like spi, allocate space for the pixels.*/
void setupWS2801(int freq, int len)
{
SPI.begin();
SPI.setFrequency(1e6);
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
ws2801_striplen = len;
colors = colorinc();
// allocate space if run first time else free and allocate.
if(buffer[0] == NULL && buffer[1] == NULL && buffer[2] == NULL)
{
// allocate space for buffer
buffer[0] = (uint8_t *)malloc(sizeof(uint8_t)*len);
buffer[1] = (uint8_t *)malloc(sizeof(uint8_t)*len);
buffer[2] = (uint8_t *)malloc(sizeof(uint8_t)*len);
}
else
{
free(buffer[0]);
free(buffer[1]);
free(buffer[2]);
// allocate space for buffer
buffer[0] = (uint8_t *)malloc(sizeof(uint8_t)*len);
buffer[1] = (uint8_t *)malloc(sizeof(uint8_t)*len);
buffer[2] = (uint8_t *)malloc(sizeof(uint8_t)*len);
}
setWS2801Strip(0, 0, 0);
updateWS2801();
}
/* set a certain pixel to certain (r, g, b) values.*/
void setWS2801Pixel(int pos, int r, int g, int b)
{
buffer[RED][pos] = r;
buffer[GREEN][pos] = g;
buffer[BLUE][pos] = b;
}
/* set a whole ledstrip to a single color.*/
void setWS2801Strip(int r, int g, int b)
{
for(int i = 0; i < ws2801_striplen; i++)
{
setWS2801Pixel(i, r, g, b);
}
}
/* effect that fades all the leds from one color to another.*/
void fadeWS2801(int speed, int brightness)
{
ccurrent = millis();
if((ccurrent - cprevious) >= cinterval)
{
cprevious = ccurrent;
colors = 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;
setWS2801Strip(r, g, b);
}
/*
fade all the pixels individually from one color to the next.
creating a rainbow like effect.
*/
void rainbowWS2801(int speed, int brightness)
{
cinterval = speed + 1;
ccurrent = millis();
if((ccurrent - cprevious) >= cinterval)
{
cprevious = ccurrent;
static int range = 0xff*3;
uint8_t buffer[3][ws2801_striplen];
int i, s;
colors = colorinc();
for(i = 0; i < ws2801_striplen; i++)
{
for(s = 0; s < range/ws2801_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;
setWS2801Pixel(i, r, g, b);
}
}
}
/* update the whole ledstrip with it's colors.*/
void updateWS2801()
{
for(int i = 0; i < ws2801_striplen; i++)
{
SPI.transfer(buffer[RED][i]);
SPI.transfer(buffer[GREEN][i]);
SPI.transfer(buffer[BLUE][i]);
}
delay(1);
}