-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathholiday_lights.device.nut
363 lines (285 loc) · 13.2 KB
/
holiday_lights.device.nut
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
358
359
360
361
// -----------------------------------------------------------------------------
const BULBS = 25;
const FPS = 5;
// ----------------------------------------------------------------------------
class colorEffects {
spi = null;
drawBuffer = null;
updateBufferTimer = FPS;
aniFrame = 0;
aniQueue = null;
aniSequence = null;
// ........................................................................
constructor(_bulbs = BULBS) {
// Initialise the hardware
spi = hardware.spi257;
spi.configure(CLOCK_IDLE_LOW, 15000);
// Initialise the buffers
drawBuffer = bulbBuffer(_bulbs);
renderBuffer();
aniQueue = queue();
// Start the drawing
imp.wakeup(0, updateBuffer.bindenv(this));
// Let the agent know we are online and handle incoming requests
agent.on("pushQueue", aniQueue.push.bindenv(aniQueue));
agent.on("clearQueue", aniQueue.clear.bindenv(aniQueue));
agent.send("status", "online");
}
// ........................................................................
function updateBuffer() {
aniSequence = aniQueue.pop(aniSequence);
// server.log(aniSequence == null ? "Null" : (aniSequence.animation + ":" + aniSequence.frames));
if (aniSequence != null) {
if (!("animation" in aniSequence)) aniSequence.animation <- "walk";
if (!("color1" in aniSequence)) aniSequence.color1 <- "white";
if (!("color2" in aniSequence)) aniSequence.color2 <- "blue";
if (!("steps" in aniSequence)) aniSequence.steps <- 1;
if (!("frames" in aniSequence)) aniSequence.frames <- null;
if (!("speed" in aniSequence)) aniSequence.speed <- FPS;
if (!("brightness" in aniSequence)) aniSequence.brightness <- 100;
if (!("new" in aniSequence)) aniSequence.new <- false;
aniSequence.steps = aniSequence.steps.tointeger();
aniSequence.speed = aniSequence.speed.tointeger();
aniSequence.brightness = aniSequence.brightness.tointeger();
if ("frames" in aniSequence && aniSequence.frames != null) {
aniSequence.frames = aniSequence.frames.tointeger();
}
if (aniSequence.new) aniFrame = 0;
updateBufferTimer = aniSequence.speed;
switch (aniSequence.animation)
{
case "fixed":
for (local i = 0; i < drawBuffer.bulbs; i++) {
drawBuffer.setBulb(i, aniSequence.color1, aniSequence.brightness);
}
break;
case "walk":
for (local i = 0; i < drawBuffer.bulbs; i++) {
if ((i+aniFrame) % aniSequence.steps == 0) {
drawBuffer.setBulb(i, aniSequence.color1, aniSequence.brightness);
} else if ((i+aniFrame) % aniSequence.steps == 1) {
drawBuffer.setBulb(i, aniSequence.color2, aniSequence.brightness);
} else if ("color3" in aniSequence) {
drawBuffer.setBulb(i, aniSequence.color3, aniSequence.brightness);
} else {
drawBuffer.setBulb(i, aniSequence.color2, aniSequence.brightness);
}
}
break;
case "twinkle":
for (local i = 0; i < drawBuffer.bulbs; i++) {
drawBuffer.setBulb(i, aniSequence.color1, aniSequence.brightness);
}
for (local i = 0; i < math.rand() % drawBuffer.bulbs; i++) {
drawBuffer.setBulb(math.rand() % drawBuffer.bulbs, aniSequence.color2, aniSequence.brightness);
}
break;
case "pastel":
for (local i = 0; i < drawBuffer.bulbs; i++) {
drawBuffer.setBulb(i, math.rand() % 0x7FFF, aniSequence.brightness);
}
break;
case "random":
for (local i = 0; i < drawBuffer.bulbs; i++) {
local ci1 = math.rand() % drawBuffer.colors.len();
local ci2 = 0;
local color = "";
foreach (coli,colr in drawBuffer.colors) {
if (ci1 == ci2++) color = coli;
}
drawBuffer.setBulb(i, color, aniSequence.brightness);
}
break;
case "fadein":
aniSequence.steps = aniSequence.steps <= 0 ? 1 : aniSequence.steps;
for (local i = 0; i < drawBuffer.bulbs; i++) {
drawBuffer.setBulb(i, aniSequence.color1, (aniFrame*aniSequence.steps) % 100);
}
break;
case "fadeout":
aniSequence.steps = aniSequence.steps <= 0 ? 1 : aniSequence.steps;
for (local i = 0; i < drawBuffer.bulbs; i++) {
drawBuffer.setBulb(i, aniSequence.color1, 100 - ((aniFrame*aniSequence.steps) % 100));
}
break;
case "smooth":
local color1 = drawBuffer.toColorRgb(aniSequence.color1);
local color2 = drawBuffer.toColorRgb(aniSequence.color2);
if (aniSequence.steps <= 0) aniSequence.steps = 1;
if (aniSequence.frames == null) {
local distance = 0;
for (local i = 0; i < 3; i++) {
local d = math.abs(color1[0] - color2[0]);
if (d > distance) distance = d;
}
aniSequence.frames = (1.0 * distance / aniSequence.steps).tointeger() + 1;
}
local step = aniFrame * aniSequence.steps;
local r = (color1[0] + (color2[0] - color1[0]) * (step % (0xFF + 1)) / 0xFF).tointeger();
local g = (color1[1] + (color2[1] - color1[1]) * (step % (0xFF + 1)) / 0xFF).tointeger();
local b = (color1[2] + (color2[2] - color1[2]) * (step % (0xFF + 1)) / 0xFF).tointeger();
// server.log(format(" %02d [ %02X,%02X,%02X ] + [ %02X,%02X,%02X ] = [ %02X,%02X,%02X ]", aniFrame, color1[0], color1[1], color1[2], color2[0], color2[1], color2[2], r, g, b));
local color = [r, g, b];
for (local i = 0; i < drawBuffer.bulbs; i++) {
drawBuffer.setBulb(i, color);
}
break;
}
// Render the buffer to the serial port
renderBuffer();
}
aniFrame++;
imp.wakeup(1.0 / updateBufferTimer, updateBuffer.bindenv(this));
}
// ........................................................................
function renderBuffer() {
// server.log("Render");
local out = drawBuffer.render();
spi.write("\x00\x00\x00\x00"); // 32 bits of zero
spi.write(out);
spi.write("\x00\x00\x00\x00"); // 32 bits of zero
}
}
// ----------------------------------------------------------------------------
class queue {
items = null;
// ........................................................................
constructor() {
items = [];
}
// ........................................................................
function clear(dummy = null) {
items.clear();
}
// ........................................................................
function push(item) {
items.push(item);
}
// ........................................................................
function pop(sequence = null) {
// Check if we have still got more time in the current animation
if ("frames" in sequence && sequence.frames != null && --sequence.frames > 0) {
// As you were
} else {
if ("frames" in sequence) sequence.frames = 0;
if (items.len() > 0) {
// Now remove that item
local item = items[0];
items.remove(0);
item.new <- true;
return item;
}
}
if ("new" in sequence) sequence.new = false;
return sequence;
}
}
// ----------------------------------------------------------------------------
class bulbBuffer {
bulbs = 0;
bulbDetails = null;
colors = {
"red": [0xFF, 0x00, 0x00],
"green": [0x00, 0xFF, 0x00],
"blue": [0x00, 0x00, 0xFF],
"aqua": [0x00, 0xFF, 0xFF],
"yellow": [0xFF, 0xFF, 0x00],
"magenta":[0xFF, 0x00, 0xFF],
"white": [0xFF, 0xC0, 0x80],
"black": [0x00, 0x00, 0x00],
"pink": [0xC0, 0x10, 0x80],
"purple": [0x80, 0x00, 0x80],
"teal": [0x00, 0x80, 0x80],
"skyblue":[0x00, 0xBF, 0xFF],
"orange": [0xFF, 0x20, 0x00]
};
// ........................................................................
constructor(_bulbs) {
bulbs = _bulbs;
// Initialise the bulb array
bulbDetails = [];
for (local i = 0; i < bulbs; i++) {
local newbulb = { "color": 0x00, "brightness": 0x00, "changed": true };
bulbDetails.push(newbulb);
}
}
// ........................................................................
// drawBuffer.setBulb(i, [red, green, blue]);
// drawBuffer.setBulb(i, 0xFFF);
// drawBuffer.setBulb(i, "pink");
function setBulb(position, color = 0x00, brightness = 100) {
if (position >= 0 && position < bulbs) {
color = toColorHex(color);
brightness = (1.0 * brightness / 100.00 * 0xFF).tointeger();
if (brightness > 0xFF) brightness = 0xFF;
if (brightness < 0) brightness = 0x00;
if ((bulbDetails[position].color != color) || (bulbDetails[position].brightness != brightness)) {
bulbDetails[position].color = color;
bulbDetails[position].brightness = brightness;
bulbDetails[position].changed = true;
}
}
}
// ........................................................................
function render() {
local out = blob(2 * bulbs);
for(local i=0; i<bulbs; i++) {
// Seek and write the buffer
out.writen((bulbDetails[i].color >> 8) & 0xFF, 'b');
out.writen(bulbDetails[i].color & 0xFF, 'b');
}
return out;
}
// ........................................................................
function toColorHex(color) {
// Convert strings first
if (typeof color == "string") {
if (color in colors) {
color = colors[color];
} else {
color = 0x0;
}
}
// Now convert arrays, tables and integers.
local newcolor = null;
if (typeof color == "array") {
newcolor = ((color[0].tointeger() & 0xF8) << 7) | ((color[1].tointeger() & 0xF8) << 2) | ((color[2].tointeger() & 0xF8) >> 3);
}
else if (typeof color == "table") {
newcolor = ((color.r.tointeger() & 0xF8) << 7) | ((color.g.tointeger() & 0xF8) << 2) | ((color.b.tointeger() & 0xF8) >> 3);
}
else if (typeof color == "integer") {
newcolor = color;
}
return (0x8000 | newcolor);
}
// ........................................................................
function toColorRgb(color) {
local newcolor = null;
if (typeof color == "string") {
if (color in colors) {
newcolor = colors[color];
} else {
newcolor = [0, 0, 0];
}
}
if (typeof color == "integer") {
newcolor = [];
newcolor.push((color >> 7) & 0xF8);
newcolor.push((color >> 2) & 0xF8);
newcolor.push((color << 3) & 0xF8);
}
return newcolor;
}
}
// ----------------------------------------------------------------------------
// Regularly update the server
function regular_update() {
imp.wakeup(30, regular_update);
agent.send("update", {});
}
// ----------------------------------------------------------------------------
imp.enableblinkup(true);
c <- colorEffects(48);
regular_update();
server.log("Device ready!");