-
Notifications
You must be signed in to change notification settings - Fork 0
/
led_example.c
216 lines (129 loc) · 4.28 KB
/
led_example.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
#ifdef __AVR__DONT_INCLUDE_
#include "led.h"
/*
That is the whole API. What follows are some demo functions rewriten from the AdaFruit strandtest code...
https://github.com/adafruit/Adafruit_NeoPixel/blob/master/examples/strandtest/strandtest.ino
Note that we always turn off interrupts while we are sending pixels becuase an interupt
could happen just when we were in the middle of somehting time sensitive.
If we wanted to minimize the time interrupts were off, we could instead
could get away with only turning off interrupts just for the very brief moment
when we are actually sending a 0 bit (~1us), as long as we were sure that the total time
taken by any interrupts + the time in our pixel generation code never exceeded the reset time (5us).
*/
// Display a single color on the whole string
void showColor( unsigned char r , unsigned char g , unsigned char b ) {
cli();
for( int p=0; p<LED_TOTAL; p++ ) {
sendPixel( r , g , b );
}
sei();
led_show();
}
// Fill the dots one after the other with a color
// rewrite to lift the compare out of the loop
void colorWipe(unsigned char r , unsigned char g, unsigned char b, unsigned char wait ) {
for(unsigned int i=0; i<LED_TOTAL; i+= (LED_TOTAL/60) ) {
cli();
unsigned int p=0;
while (p++<=i) {
sendPixel(r,g,b);
}
while (p++<=LED_TOTAL) {
sendPixel(0,0,0);
}
sei();
led_show();
delay(wait);
}
}
// Theatre-style crawling lights.
// Changes spacing to be dynmaic based on string size
#define THEATER_SPACING (LED_TOTAL/20)
void theaterChase( unsigned char r , unsigned char g, unsigned char b, unsigned char wait ) {
for (int j=0; j< 3 ; j++) {
for (int q=0; q < THEATER_SPACING ; q++) {
unsigned int step=0;
cli();
for (int i=0; i < LED_TOTAL ; i++) {
if (step==q) {
sendPixel( r , g , b );
} else {
sendPixel( 0 , 0 , 0 );
}
step++;
if (step==THEATER_SPACING) step =0;
}
sei();
led_show();
delay(wait);
}
}
}
// I rewrite this one from scrtach to use high resolution for the color wheel to look nicer on a *much* bigger string
void rainbowCycle(unsigned char frames , unsigned int frameAdvance, unsigned int pixelAdvance ) {
// Hue is a number between 0 and 3*256 than defines a mix of r->g->b where
// hue of 0 = Full red
// hue of 128 = 1/2 red and 1/2 green
// hue of 256 = Full Green
// hue of 384 = 1/2 green and 1/2 blue
// ...
unsigned int firstPixelHue = 0; // Color for the first pixel in the string
for(unsigned int j=0; j<frames; j++) {
unsigned int currentPixelHue = firstPixelHue;
cli();
for(unsigned int i=0; i< LED_TOTAL; i++) {
if (currentPixelHue>=(3*256)) { // Normalize back down incase we incremented and overflowed
currentPixelHue -= (3*256);
}
unsigned char phase = currentPixelHue >> 8;
unsigned char step = currentPixelHue & 0xff;
switch (phase) {
case 0:
sendPixel( ~step , step , 0 );
break;
case 1:
sendPixel( 0 , ~step , step );
break;
case 2:
sendPixel( step ,0 , ~step );
break;
}
currentPixelHue+=pixelAdvance;
}
sei();
led_show();
firstPixelHue += frameAdvance;
}
}
// I added this one just to demonstrate how quickly you can flash the string.
// Flashes get faster and faster until *boom* and fade to black.
void detonate( unsigned char r , unsigned char g , unsigned char b , unsigned int startdelayms) {
while (startdelayms) {
showColor( r , g , b ); // Flash the color
showColor( 0 , 0 , 0 );
delay( startdelayms );
startdelayms = ( startdelayms * 4 ) / 5 ; // delay between flashes is halved each time until zero
}
// Then we fade to black....
for( int fade=256; fade>0; fade-- ) {
showColor( (r * fade) / 256 ,(g*fade) /256 , (b*fade)/256 );
}
showColor( 0 , 0 , 0 );
}
void setup_avr() {
ledsetup();
}
void loop_avr() {
// Some example procedures showing how to display to the LED_TOTAL:
colorWipe(255, 0, 0, 0); // Red
colorWipe(0, 255, 0, 0); // Green
colorWipe(0, 0, 255, 0); // Blue
// Send a theater pixel chase in...
theaterChase(127, 127, 127, 0); // White
theaterChase(127, 0, 0, 0); // Red
theaterChase( 0, 0, 127, 0); // Blue
rainbowCycle(1000 , 20 , 5 );
detonate( 255 , 255 , 255 , 1000);
return;
}
#endif//__AVR__DONT_INCLUDE_