-
Notifications
You must be signed in to change notification settings - Fork 10
/
MEOG35String.cpp
357 lines (326 loc) · 5.95 KB
/
MEOG35String.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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
G35: An Arduino library for GE Color Effects G-35 holiday lights.
Copyright © 2011 The G35 Authors. Use, modification, and distribution are
subject to the BSD license as described in the accompanying LICENSE file.
Original version by Paul Martis (http://www.digitalmisery.com). See
README for complete attributions.
Special thanks to Richard <[email protected]> and "slinky" for the unrolled-
loop protocol code that seems to work very well!
*/
#include <MEOG35String.h>
#define MHZ_16 (1) // standard Arduino/Teensy
#if MHZ_16
#define DELAYLONG 17 // should be ~ 20uS long
#define DELAYSHORT 7 // should be ~ 10uS long
#else // 20MHz
#define DELAYLONG 25 // should be ~ 20uS long
#define DELAYSHORT 11 // should be ~ 10uS long
#endif
#define DELAYEND 40 // should be ~ 30uS long
#define ZERO(x) digitalWrite(x, LOW); \
delayMicroseconds(DELAYSHORT); \
digitalWrite(x, HIGH); \
delayMicroseconds(DELAYLONG);
#define ONE(x) digitalWrite(x, LOW); \
delayMicroseconds(DELAYLONG); \
digitalWrite(x, HIGH); \
delayMicroseconds(DELAYSHORT);
MEOG35String::MEOG35String(uint8_t pin, uint8_t light_count,
uint8_t physical_light_count,
uint8_t bulb_zero, bool is_forward)
: MEOG35(), pin_(pin), physical_light_count_(physical_light_count),
bulb_zero_(bulb_zero), is_forward_(is_forward)
{
pinMode(pin, OUTPUT);
light_count_ = light_count;
}
MEOG35String::MEOG35String(uint8_t pin, uint8_t light_count)
: MEOG35(), pin_(pin), physical_light_count_(light_count),
bulb_zero_(0), is_forward_(true)
{
pinMode(pin, OUTPUT);
light_count_ = light_count;
}
void MEOG35String::set_color(uint8_t bulb, uint8_t intensity, color_t color)
{
bulb += bulb_zero_;
uint8_t r, g, b;
r = color & 0x0F;
g = (color >> 4) & 0x0F;
b = (color >> 8) & 0x0F;
if (intensity > MAX_INTENSITY)
{
intensity = MAX_INTENSITY;
}
noInterrupts();
digitalWrite(pin_, HIGH);
delayMicroseconds(DELAYSHORT);
// LED Address
if (bulb & 0x20)
{
ONE(pin_);
}
else
{
ZERO(pin_);
}
if (bulb & 0x10)
{
ONE(pin_);
}
else
{
ZERO(pin_);
}
if (bulb & 0x08)
{
ONE(pin_);
}
else
{
ZERO(pin_);
}
if (bulb & 0x04)
{
ONE(pin_);
}
else
{
ZERO(pin_);
}
if (bulb & 0x02)
{
ONE(pin_);
}
else
{
ZERO(pin_);
}
if (bulb & 0x01)
{
ONE(pin_);
}
else
{
ZERO(pin_);
}
// Brightness
if (intensity & 0x80)
{
ONE(pin_);
}
else
{
ZERO(pin_);
}
if (intensity & 0x40)
{
ONE(pin_);
}
else
{
ZERO(pin_);
}
if (intensity & 0x20)
{
ONE(pin_);
}
else
{
ZERO(pin_);
}
if (intensity & 0x10)
{
ONE(pin_);
}
else
{
ZERO(pin_);
}
if (intensity & 0x08)
{
ONE(pin_);
}
else
{
ZERO(pin_);
}
if (intensity & 0x04)
{
ONE(pin_);
}
else
{
ZERO(pin_);
}
if (intensity & 0x02)
{
ONE(pin_);
}
else
{
ZERO(pin_);
}
if (intensity & 0x01)
{
ONE(pin_);
}
else
{
ZERO(pin_);
}
// Blue
if (b & 0x8)
{
ONE(pin_);
}
else
{
ZERO(pin_);
}
if (b & 0x4)
{
ONE(pin_);
}
else
{
ZERO(pin_);
}
if (b & 0x2)
{
ONE(pin_);
}
else
{
ZERO(pin_);
}
if (b & 0x1)
{
ONE(pin_);
}
else
{
ZERO(pin_);
}
// Green
if (g & 0x8)
{
ONE(pin_);
}
else
{
ZERO(pin_);
}
if (g & 0x4)
{
ONE(pin_);
}
else
{
ZERO(pin_);
}
if (g & 0x2)
{
ONE(pin_);
}
else
{
ZERO(pin_);
}
if (g & 0x1)
{
ONE(pin_);
}
else
{
ZERO(pin_);
}
// Red
if (r & 0x8)
{
ONE(pin_);
}
else
{
ZERO(pin_);
}
if (r & 0x4)
{
ONE(pin_);
}
else
{
ZERO(pin_);
}
if (r & 0x2)
{
ONE(pin_);
}
else
{
ZERO(pin_);
}
if (r & 0x1)
{
ONE(pin_);
}
else
{
ZERO(pin_);
}
digitalWrite(pin_, LOW);
delayMicroseconds(DELAYEND);
interrupts();
}
void MEOG35String::enumerate()
{
enumerate(is_forward_);
}
void MEOG35String::enumerate(bool forward)
{
uint8_t count = physical_light_count_;
uint8_t bulb = forward ? 0 : light_count_ - 1;
int8_t delta = forward ? 1 : -1;
while (count--)
{
set_color(bulb, MAX_INTENSITY, COLOR_RED);
bulb += delta;
}
}
void MEOG35String::enumerate_forward()
{
enumerate(true);
}
void MEOG35String::enumerate_reverse()
{
enumerate(false);
}
void MEOG35String::do_test_patterns()
{
const uint8_t d = 1000 / light_count_;
const uint8_t last_light = light_count_ - 1;
fill_sequence(0, light_count_, 0, 1, 0, rainbow_color);
for (int i = 0; i <= MAX_INTENSITY; ++i)
{
set_color(BROADCAST_BULB, i, COLOR_BLACK);
delay(1);
}
delay(500);
for (int i = MAX_INTENSITY; i >= 0; --i)
{
set_color(BROADCAST_BULB, i, COLOR_BLACK);
delay(1);
}
for (uint8_t i = 0; i < light_count_; ++i)
{
set_color(i, MAX_INTENSITY, COLOR_GREEN);
set_color(last_light - i, MAX_INTENSITY, COLOR_BLUE);
if (i > 0)
{
set_color(i - 1, MAX_INTENSITY, COLOR_BLACK);
set_color(last_light - i + 1, MAX_INTENSITY, COLOR_BLACK);
}
delay(d);
}
fill_color(0, light_count_, MAX_INTENSITY, COLOR_BLACK);
}