-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy patheyeblink.js
164 lines (142 loc) · 5.21 KB
/
eyeblink.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
/**
* eyeblink.js
*
* Populate strip with pairs of blinking eyes.
*
* @author Dougal Campbell
*/
function Eyeblinks(ledstrip) {
this.ledstrip = ledstrip;
this.size = ledstrip.size();
this.count = 0;
// Initialize some sub-animations
this.eyeblinks = [
new this.Eyeblink({startPos: 0, color: [255, 0, 0], eyeSep: 2, fadeOutTime: 8000, blinksMax: 6}),
new this.Eyeblink({startPos: 5, color: [0, 255, 0], eyeSep: 3, fadeInTime: 0, fadeOutTime: 3000}),
new this.Eyeblink({startPos: 13, color: [255, 0, 127], eyeSep: 1, findInTime: 0, fadeOutTime: 0}),
new this.Eyeblink({startPos: 19, color: [127, 0, 255], eyeSep: 4, fadeInTime: 5000, blinksMin: 3, blinksMax: 8}),
new this.Eyeblink({startPos: 27, color: [255, 127, 0], eyeSep: 2})
];
for (var idx = 0, len = this.eyeblinks.length; idx < len; ++idx) {
this.eyeblinks[idx].parent = this;
}
}
Eyeblinks.prototype.init = function() {
}
Eyeblinks.prototype.animate = function() {
animation = requestAnimationFrame(this.animate.bind(this));
// slow things down. 1 == full speed
if ((this.count++ % 3)) return;
this.ledstrip.clear();
// Step through each sub-animation
for ( var i = 0; i < this.eyeblinks.length; i++ ) {
this.eyeblinks[i].step(this.ledstrip.buffer);
}
this.ledstrip.send();
}
/**
* Eyeblink prototype
*/
Eyeblinks.prototype.Eyeblink = function(opts) {
this.parent = {};
// States
this.WAITING = 0; // Next = 1
this.FADEIN = 1; // Next = 2
this.ON = 2; // Next = 3, 4
this.OFF = 3; // Next = 2
this.FADEOUT = 4; // Next = 0
this.state = this.WAITING;
this.colorCurrent = [0, 0, 0]; // start at black
this.fadeInTime = opts.fadeInTime || 200;
this.fadeOutTime = opts.fadeOutTime || 1200;
this.color = opts.color || [255, 0, 0];
this.blinksMin = opts.blinkMin || 1;
this.blinksMax = opts.blinksMax || 6;
this.startPos = opts.startPos || 0;
this.eyeSep = opts.eyeSep || random(1,6);
this.blinkCount = random(this.blinksMin, this.blinksMax+1);
this.startEvent = millis();
this.nextEvent = millis() + random(500, 8000);
return this;
}
Eyeblinks.prototype.Eyeblink.prototype.step = function(buf) {
this._step();
this._draw(buf);
}
Eyeblinks.prototype.Eyeblink.prototype._step = function() {
switch (this.state) {
case this.WAITING:
if (millis() > this.nextEvent) {
this.state = this.FADEIN;
this.nextEvent = millis() + this.fadeInTime;
this.startEvent = millis();
}
break;
case this.FADEIN:
if (millis() > this.nextEvent) {
this.state = this.ON;
this.colorCurrent = this.color;
this.nextEvent = millis() + random(1000, 4000);
this.startEvent = millis();
} else {
// scale color according to current time, in relation to start/end time
this.colorCurrent = this.scaleColor(this.color, millis(), this.startEvent, this.nextEvent);
}
break;
case this.ON:
if (millis() > this.nextEvent) {
if (this.blinkCount > 0) {
// blinking...
this.state = this.OFF;
this.colorCurrent = [0, 0, 0];
this.nextEvent = millis() + random(50, 100);
this.startEvent = millis();
} else {
// fade out...
this.state = this.FADEOUT
this.nextEvent = millis() + this.fadeOutTime;
this.startEvent = millis();
}
}
break;
case this.OFF:
if (millis() > this.nextEvent) {
this.state = this.ON;
this.colorCurrent = this.color;
this.nextEvent = millis() + random(500, 3000);
this.startEvent = millis();
--this.blinkCount;
}
break;
case this.FADEOUT:
if (millis() > this.nextEvent) {
this.state = this.WAITING;
this.colorCurrent = [0, 0, 0];
this.nextEvent = millis() + random(3000, 60000);
this.startEvent = millis();
this.blinkCount = random(this.blinksMin, this.blinksMax+1);
} else {
// scale color down according to current time, in relation to start/end time
this.colorCurrent = this.scaleColor(this.color, millis(), this.nextEvent, this.startEvent);
}
break;
default:
// this should never happen! but reset, if it does...
this.state = this.WAITING;
this.nextEvent = millis() + 5000; // wait 5 seconds
this.startEvent = millis();
}
}
Eyeblinks.prototype.Eyeblink.prototype._draw = function(buf) {
buf[this.startPos] = this.colorCurrent;
buf[this.startPos + this.eyeSep] = this.colorCurrent;
}
Eyeblinks.prototype.Eyeblink.prototype.scaleColor = function(color, scale, min, max) {
var r = color[0], g = color[1], b = color[2];
var factor = map(scale, min, max, 0, 255);
r = Math.floor(r * factor / 255);
g = Math.floor(g * factor / 255);
b = Math.floor(b * factor / 255);
return [r, g, b];
}
// -fin-