-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathWalgreenChristmasLights.ino
239 lines (202 loc) · 5.1 KB
/
WalgreenChristmasLights.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
// code to bit bang the protocol for the Walgreen's Christmas lights
class WalgreenLights
{
private:
int pin; // the pin to write to
int numLights;
volatile uint8_t *pinPort;
int pinMask;
void SendPacket(unsigned char 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
// 3 pulses of H 6.25us + L 6.67us
for (int i = 0; i < 3; i++)
{
*port |= mask; // HIGH
delayMicroseconds(6);
*port &= ~mask; // LOW
delayMicroseconds(6);
}
// guess at LSB first
for (int i = 0; i < 4; i++)
{
*port |= mask; // HIGH
delayMicroseconds((val & 1) ? 3 : 1);
*port &= ~mask; // LOW
delayMicroseconds(3);
val >>= 1;
}
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
delayMicroseconds(100);
}
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, unsigned char val, unsigned char other = 0)
{
for (int i = 0; i < numLights; i++)
{
if (i % 5 == offset)
SendPacket(val);
else
SendPacket(other);
}
}
public:
WalgreenLights(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();
}
~WalgreenLights(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(unsigned char *valPtr)
{
for (int i = 0; i < numLights; i++)
{
SendPacket(*valPtr++);
}
}
// set the value on the red lights only
// all others at 'other' (default 0 = off)
void RedOnly(unsigned char val, unsigned char other = 0)
{
OneColor(4, val, other);
}
void GreenOnly(unsigned char val, unsigned char other = 0)
{
OneColor(3, val, other);
}
void BlueOnly(unsigned char val, unsigned char other = 0)
{
OneColor(2, val, other);
}
void OrangeOnly(unsigned char val, unsigned char other = 0)
{
OneColor(1, val, other);
}
void WhiteOnly(unsigned char val, unsigned char other = 0)
{
OneColor(0, val, other);
}
void AllTo(unsigned char val)
{
OneColor(0, val, val);
}
void AllOn(void)
{
AllTo(15);
}
void AllOff(void)
{
AllTo(0);
}
};
class WalgreenLights *lights, *lights2;
void setup()
{
lights = new WalgreenLights(13, 15);
lights2 = new WalgreenLights(12, 15);
}
unsigned char state[15];
void loop()
{
// all on
lights->AllOn();
lights2->AllOff();
delay(1000);
// all off
lights->AllOff();
lights2->AllOn();
delay(1000);
// around the string
for (int i = 0; i < sizeof(state); i++)
state[i] = 0;
for (int k = 0; k < 10; k++)
{
for (int i = 0; i < sizeof(state); i++)
{
if (i == 0)
state[sizeof(state)-1] = 0;
else
state[i-1] = 0;
state[i] = 15;
lights->SendValue(state);
lights2->SendValue(state);
delay(15-k);
}
}
// sparkle
for (int k=0; k < 400; k++)
{
for (int i = 0; i < sizeof(state); i++)
state[i] = random(0, 100) < 80 ? 1 : 15;
lights->SendValue(state);
lights2->AllTo(random(0,15));
delay(1);
}
lights->AllOff();
delay(25);
// ramp up / down
for (int v = 0; v < 16; v++)
{
lights->AllTo(v);
lights2->AllTo(15-v);
delay(50);
}
delay(250);
for (int v = 15; v >= 0; v--)
{
lights->AllTo(v);
lights2->AllTo(15-v);
delay(50);
}
delay(1000);
lights2->WhiteOnly(15, 2);
lights->RedOnly(15, 2); delay(500);
lights2->OrangeOnly(15, 2);
lights->GreenOnly(15, 2); delay(500);
lights2->BlueOnly(15, 2);
lights->BlueOnly(15, 2); delay(500);
lights2->GreenOnly(15, 2);
lights->OrangeOnly(15, 2); delay(500);
lights2->RedOnly(15, 2);
lights->WhiteOnly(15, 2);
delay(1000);
}