forked from Lmiller1708/WalgreenLights
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBriteStarLights.ino
326 lines (268 loc) · 6.6 KB
/
BriteStarLights.ino
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// code to bit bang the protocol for the Brite Star Christmas lights
// Modified from WalgreenChristmasLights.ino
// https://github.com/jbrown123/WalgreenLights/blob/master/WalgreenChristmasLights.ino
class BriteStarLights{
private:
int pin; // the pin to write to
int numLights;
volatile uint8_t *pinPort;
int pinMask;
void SendPacket(word val)
{
register volatile uint8_t *port = pinPort;
register int mask = pinMask;
/////////////////////////////////////////////
// ********** WARNING **********
// This code runs with INTERRUPTS DISABLED
////////////////////////////////////////////
uint8_t oldSREG = SREG; // get interrupt register state
cli(); // disable interrupts
//START BITS
// 3 pulses of H 6.25us + L 6.67us
// I measured 4 pluses of H 6.16us and L 6.56us
// But 6 for each works just fine
for (int i = 0; i < 3; i++)
{
*port |= mask; // HIGH
delayMicroseconds(6.16);
*port &= ~mask; // LOW
delayMicroseconds(6.56);
}
//This 2us delay is needed
delayMicroseconds(2);
double dlow = 4; // LOW Delay
double dhigh = 1; // HIGH Delay
// Bits 1-4 Red
// Bits 5-8 Blue
// Bits 9-12 Green
for (int i = 0; i < 12; i++)
{
*port |= mask; // HIGH
delayMicroseconds((val & 1) ? dlow : dhigh);
*port &= ~mask; // LOW
delayMicroseconds(dlow);
val >>= 1; //Right Shift
}
*port &= ~mask; // LOW
SREG = oldSREG; // restore interrupts
// hold for at least one full packet time
// this allows the downstream neighbor to forward on
// his state to the next guy
// could be 60us if we wanted to get really tight
// 100us seems like plenty
// default system waits 1.13ms between packets
// Seems to work best at 1.13ms
delayMicroseconds(1130);
}
void InitString(void)
{
// these delay values were determined by monitoring a set of lights
digitalWrite(pin, HIGH);
delay(150);
digitalWrite(pin, LOW);
delay(225);
for (int i = 0; i < numLights; i++)
{
SendPacket(0);
}
}
// set the value on one color of lights only
// all others at 'other' (default 0 = off)
// offsets: 0=White, 1=Orange, 2=Blue, 3=Green, 4=Red
void OneColor(int offset, word val, word other = 0)
{
for (int i = 0; i < numLights; i++)
{
if (i % 5 == offset)
SendPacket(val);
else
SendPacket(other);
}
}
public:
BriteStarLights(int _pin, int _numLights)
{
pin = _pin;
numLights = _numLights;
pinPort = (pin < 8) ? &PORTD : &PORTB;
pinMask = (pin < 8) ? 1 << pin : 1 << (pin - 8);
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
InitString();
}
~BriteStarLights(void)
{
digitalWrite(pin, LOW);
pinMode(pin, INPUT);
}
// valPtr points to an array of char
// each char holds the value (0-15) for
// the bulbs in the string
void SendValue(word *valPtr)
{
for (int i = 0; i < numLights; i++)
{
SendPacket(*valPtr++);
}
}
void ColorWheel(word val)
{
for (word t = 4095; t > 0; t-=1)
{
for (int i = 0; i < numLights; i++)
{
SendPacket(t);
//val <<= 1;
}
delay(50);
}
}
word GetRGBColor (word red, word green, word blue)
{
red <<=8;
blue <<=4;
word val;
val = red;
val |= blue;
val |= green;
return val;
}
void SetColorToOne(int LEDNum, word LastState, word red, word green, word blue)
{
word val = GetRGBColor(red,green,blue);
for (int i = 0; i < numLights; i++)
{
if (i == LEDNum)
SendPacket(val);
else
SendPacket(LastState) ;
}
}
word SetColorToAll(word red, word green, word blue)
{
word val = GetRGBColor(red,green,blue);
for (int i = 0; i < numLights; i++)
{
SendPacket(val);
}
return val;
}
void SwipeAllToColor(word laststate, int wtime, word red, word green, word blue)
{
word val = GetRGBColor(red,green,blue);
if (laststate == 0){
for (int i = 0; i < numLights + 1; i++)
{
for (int p = 0; p < i; p++)
{
SendPacket(val);
}
delay(wtime);
}
}
else{
for (int i = 0; i < numLights + 1; i++)
{
SendPacket(val);
for (int p = numLights - i; p > 0; p--)
{
SendPacket(laststate);
}
delay(wtime);
}
}
}
// set the value on the red lights only
// Pass in Brightness
void RedOnly(word val)
{
val <<= 8; //Left Shift
for (int i = 0; i < numLights; i++)
{
SendPacket(val);
}
}
void BlueOnly(word val)
{
val <<= 4; //Left Shift
for (int i = 0; i < numLights; i++)
{
SendPacket(val);
}
}
void GreenOnly(word val)
{
val <<= 0; //Left Shift
for (int i = 0; i < numLights; i++)
{
SendPacket(val);
}
}
void WhiteOnly(word val)
{
val = GetRGBColor(val,val,val);
for (int i = 0; i < numLights; i++)
{
SendPacket(val);
}
}
void AllOn(int Power)
{
SetColorToAll(Power,Power,Power);
}
void AllOff(void)
{
SetColorToAll(0,0,0);
}
};
class BriteStarLights *lights;//, *lights2;
void setup()
{
lights = new BriteStarLights(13, 10);
//lights2 = new BriteStarLights(12, 10);
}
void loop()
{
word LastState;
// ramp up / down
for (int v = 0; v < 15; v++)
{
LastState = lights->SetColorToAll(v,0,0); delay(100);
}
delay(500);
//Flash Color
for (int v = 1; v >= -1; v--)
{
lights->SetColorToAll(15,0,0); delay(100);
lights->SetColorToAll(0,15,0); delay(100);
lights->SetColorToAll(0,0,15); delay(100);
lights->SetColorToAll(15,15,15); delay(100);
}
//Swipe
for (int v = 1; v >= -1; v--)
{
lights->SwipeAllToColor(0,100,0,0,15); delay(250);
LastState = lights->GetRGBColor(0,0,15);
lights->SwipeAllToColor(LastState,100,0,15,0); delay(250);
}
delay(500);
//Sparkle
for (int x = 1; x >= -1; x--)
{
for (int v = 10; v >= -1; v--)
{
lights->SetColorToOne(v,LastState,15,0,0); delay(100);
lights->SetColorToOne(v,LastState,0,15,0); delay(100);
lights->SetColorToOne(v,LastState,0,0,15); delay(100);
lights->SetColorToOne(v,LastState,15,15,15); delay(100);
}
}
lights->SwipeAllToColor(0,100,15,0,0); delay(250);
delay(500);
// ramp up / down
for (int v = 15; v > 0; v--)
{
LastState = lights->SetColorToAll(v,0,0); delay(100);
}
delay(500);
}