-
Notifications
You must be signed in to change notification settings - Fork 18
/
color_cycle.js
67 lines (57 loc) · 1.36 KB
/
color_cycle.js
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
//
// Copyright (c) 2013 Danny Havenith
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Converted to JavaScript by Dougal Campbell July 15, 2013
function ColorCycle (ledstrip) {
this.ledstrip = ledstrip;
this.ledstrip.clearLeds();
this.sequence = [];
return this;
}
/**
* shift a new color into the buffer.
*/
ColorCycle.prototype.scroll = function (new_color) {
this.ledstrip.leds.unshift(new_color);
this.ledstrip.leds.pop();
this.ledstrip.send();
}
/**
* main animation loop
*/
ColorCycle.prototype.color_cycle = function () {
animation = requestAnimationFrame(this.color_cycle.bind(this));
var idx;
for (idx = 0, seqcount = this.sequence.length - 1; idx < seqcount; ++idx) { //forwards...
this.scroll(this.sequence[idx]);
}
for (idx = this.sequence.length; idx > 0; --idx) { //backwards
this.scroll(this.sequence[idx - 1]);
}
return animation;
}
/**
* initialize color buffer
*/
ColorCycle.prototype.init = function(seq) {
this.sequence = seq || [
[0, 25, 50],
[0, 50, 100],
[0, 75, 150],
[25, 75, 100],
[50, 100, 125],
[75, 125, 100],
[100, 125, 100],
[125, 125, 100],
[150, 125, 100],
[175, 125, 100],
[250, 125, 100],
[250, 100, 75],
[250, 75, 50],
[250, 50, 25],
[250, 0, 0]
];
}