-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathlightning.js
More file actions
150 lines (131 loc) · 4.32 KB
/
lightning.js
File metadata and controls
150 lines (131 loc) · 4.32 KB
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
/**
* lightning.js
*
* Lightning 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
*
* NOTE: I had to refactor this significantly from the original code due
* to differences between Arduino's synchronous loop() and JavaScript's
* asynchronous requestAnimationFrame(). I'm implementing the delay()
* functionality more in terms of a state machine.
*
* States: IDLE -> FLASHINIT -> FLASHOFF -> FLASHON -> FLASHOFF -> [ FLASHON | IDLE ]
*/
function Lightning (ledstrip, opts) {
opts = opts || {};
this.ledstrip = ledstrip;
this.ledstrip.clear();
this.NUM_LEDS = this.ledstrip.size();
this.FREQUENCY = 100; // Controls the interval between strikes
this.FLASHES = 8; // Upper limit of flashes per strike
this.dimmer = 1;
// State machine vars
this.STATE = 'IDLE'; // Current state
this.TIMEOUT = 0; // Time for next state check
this.FLASHCOUNT = 0; // Instance flash count (randomly set)
this.t = 0; // tick counter
return this;
}
Lightning.prototype.init = function() {
this.TIMEOUT = this.addTime(3000); // start with a 3-sec delay
this.STATE = 'IDLE';
this.FLASHCOUNT = 0;
this.ledstrip.clear();
return this;
}
Lightning.prototype.setFrequency = function (F) {
this.FREQUENCY = F;
}
Lightning.prototype.setFlashes = function (F) {
this.FLASHES = F;
}
// Get a timestamp for ms milliseconds from now
Lightning.prototype.addTime = function (ms) {
return (new Date()).valueOf() + ms;
}
// Replicate random8() function
Lightning.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
Lightning.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;
}
Lightning.prototype.animate = function() {
animation = requestAnimationFrame(this.animate.bind(this)); // preserve our context
switch (this.STATE) {
case 'IDLE':
if ((new Date()).valueOf() > this.TIMEOUT) {
/* go to FLASHINIT state next */
// How many flashes for this time around?
this.FLASHCOUNT = this.random8(3, this.FLASHES); // Reset flash count
this.TIMEOUT = this.addTime(this.random8(4,10)); // Time until next action
this.dimmer = 5; // lead strike is dimmer
this.ledstrip.showColor(this.ledstrip.hsl2rgb(359, 0, Math.floor((255/this.dimmer)/255))); // on
this.STATE = 'FLASHINIT'; // next state on timeout
//console.log('IDLE -> FLASHINIT');
}
break;
case 'FLASHINIT':
if ((new Date()).valueOf() > this.TIMEOUT) {
this.TIMEOUT = this.addTime(150); // initial delay is longer
this.ledstrip.showColor(this.ledstrip.hsl2rgb(359, 0, 0)); // off
this.STATE = 'FLASHOFF';
//console.log('FLASHINIT -> FLASHOFF');
}
break;
case 'FLASHON':
if ((new Date()).valueOf() > this.TIMEOUT) {
this.TIMEOUT = this.addTime(50 + this.random8(0, 100)); // delay between strokes
this.ledstrip.showColor(this.ledstrip.hsl2rgb(359, 0, 0)); // off
this.STATE = 'FLASHOFF';
//console.log('FLASHON -> FLASHOFF');
}
break;
case 'FLASHOFF':
if ((new Date()).valueOf() > this.TIMEOUT) {
if (this.FLASHCOUNT > 0) {
this.FLASHCOUNT--; // We've completed a stroke!
this.TIMEOUT = this.addTime(this.random8(4,10)); // delay between strokes
this.dimmer = this.random8(1,3);
this.ledstrip.showColor(this.ledstrip.hsl2rgb(359, 0, Math.floor(255/this.dimmer)/255)); // on again
this.STATE = 'FLASHON'; // next stroke...
//console.log('FLASHOFF -> FLASHON');
} else {
this.FLASHCOUNT = 0; // Completed all strokes. Go idle.
this.TIMEOUT = this.addTime(this.random8(this.FREQUENCY) * 100);
this.STATE = 'IDLE';
//console.log('FLASHOFF -> IDLE');
}
}
break;
default: // unknown state. this should never happen.
this.ledstrip.clear();
console.error('What the?');
}
this.ledstrip.send(); // display the LED state
this.t++; // increment tick
}