-
Notifications
You must be signed in to change notification settings - Fork 18
/
twinklesparkle.js
180 lines (146 loc) · 4.65 KB
/
twinklesparkle.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
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
/**
* twinklesparkle.js
*
* TwinkleSparkle by Danny Wilson, originally for the FastLED library
*
* Originally by Danny Wilson - see https://github.com/fibonacci162/LEDs
*
* LEDstrip plugin
*
* Copyright (c) 2013 Dougal Campbell
*
* Distributed under the MIT License
*/
function TwinkleSparkle (ledstrip, opts) {
opts = opts || {};
this.ledstrip = ledstrip;
this.ledstrip.clear();
this.NUM_LEDS = this.ledstrip.size();
this.FRAMES_PER_SECOND = 30;
this.COOLING = 5; // controls how quickly LEDs dim
this.TWINKLING = 150; // controls how many new LEDs twinkle
this.FLICKER = 50; // controls how "flickery" each individual LED is
this.beatInterval = 8912; // the interval at which you want the strip to "sparkle"
this.nextBeat = 0;
this.nextTwinkle = 3000; // twinkling doesn't start until after the sanity check delay
this.seeds = 0;
this.loops = 0;
this.deltaTimeTwinkle = 0;
this.deltaTimeSparkle = 0;
this.beatStarted = false;
this.heat = [];
this.t = 0; // tick counter
return this;
}
TwinkleSparkle.prototype.init = function() {
return this;
}
// Get a timestamp for ms milliseconds from now
TwinkleSparkle.prototype.addTime = function (ms) {
return (new Date()).valueOf() + ms;
}
// Replicate random8() function
TwinkleSparkle.prototype.random8 = function(min, max) {
if (min === undefined) {
min = 0;
max = 255;
}
if (max === undefined) {
max = min;
min = 0;
}
return (Math.round(Math.random() * (max - min)) + min) & 255;
}
// Replicate random16() function
TwinkleSparkle.prototype.random16 = function(min, max) {
if (min === undefined) {
min = 0;
max = 65535;
}
if (max === undefined) {
max = min;
min = 0;
}
return (Math.round(Math.random() * (max - min)) + min) & 65535;
}
TwinkleSparkle.prototype.qadd8 = function(a, b) {
var tmp = Math.round(a + b);
if (tmp > 255) tmp = 255;
return tmp;
}
TwinkleSparkle.prototype.qsub8 = function(a, b) {
var tmp = Math.round(a - b);
if (tmp < 0) tmp = 0;
return tmp;
}
TwinkleSparkle.prototype.millis = function() {
return (new Date()).valueOf();
}
TwinkleSparkle.prototype.Twinkle = function() {
// Step 1. Create a randome number of seeds
this.seeds = this.random8(10,this.NUM_LEDS-10);
// Step 2. "Cool" down every location on the strip a little
for( var i = 0; i < this.NUM_LEDS; i++) {
this.heat[i] = this.qsub8( this.heat[i], this.COOLING);
}
// Step 3. Make the seeds into heat on the string
for ( var j = 0 ; j < this.seeds ; j++) {
if (this.random16() < this.TWINKLING) {
this.heat[this.random8(this.NUM_LEDS)] = this.random8(50,255);
}
}
// Step 4. Add some "flicker" to LEDs that are already lit
// Note: this is most visible in dim LEDs
for ( var k = 0 ; k < this.NUM_LEDS ; k++ ) {
if (this.heat[k] > 0 && this.random8() < this.FLICKER) {
this.heat[k] = this.qadd8(this.heat[k] , 10);
}
}
// Step 5. Map from heat cells to LED colors
for( var j = 0; j < this.NUM_LEDS; j++) {
this.ledstrip.buffer[j] = this.HeatColor( this.heat[j] );
}
this.nextTwinkle += 1000 / this.FRAMES_PER_SECOND ; // assign the next time Twinkle() should happen
}
TwinkleSparkle.prototype.Sparkle = function() {
// Step 1. Make a random numnber of seeds
this.seeds = this.random8(this.NUM_LEDS - 20 , this.NUM_LEDS);
// Step 2. Increase the heat at those locations
for ( var i = 0 ; i < seeds ; i++) {
var pos = this.random8(this.NUM_LEDS);
this.heat[pos] = this.random8(50,255);
}
this.nextBeat += this.beatInterval; // assign the next time Twinkle() should happen
this.loops++ ;
}
//Play with this for different strip colors
TwinkleSparkle.prototype.HeatColor = function(temperature) {
return this.ledstrip.hsl2rgb(29 * (360 / 255), 200/255, temperature/255);
}
TwinkleSparkle.prototype.animate = function() {
animation = requestAnimationFrame(this.animate.bind(this)); // preserve our context
// Wait for something in the serial monitor before "Sparkling" the first time.
// This lets you time the sparkle to a particular beat in music.
// In practice, just type a letter into the serial monitor and press enter
// when you want the first sparkle to start.
if (this.loops == 0) {
this.nextBeat = this.millis();
}
else {
if (this.loops == 0 && this.beatStarted == false) {
this.nextBeat = this.millis();
this.beatStarted == true;
this.Sparkle();
}
else {
this.deltaTimeSparkle = this.millis() - this.nextBeat;
if ( this.deltaTimeSparkle > 0 ) this.Sparkle(); // if more time than
}
}
this.deltaTimeTwinkle = this.millis() - this.nextTwinkle;
if ( this.deltaTimeTwinkle > 0 ) {
this.Twinkle();
}
this.ledstrip.send(); // display the LED state
this.t++; // increment tick
}