-
Notifications
You must be signed in to change notification settings - Fork 0
/
rgb_led_matrix.js
276 lines (238 loc) · 5.96 KB
/
rgb_led_matrix.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
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
//Producer pushes frames to the stream as simple RGB arrays at a set rate
//Consumer pulls frames from the stream and renders them to devices at a set rate
//When consumer rate is faster than production rate, alpha (opacity) allows frame morphing
//For Windows, Linux and Mac
var DEBUG = false;
const emulator = require('./controllers/EMULATOR.js');
const blinkstick = require('./controllers/BLINKSTICK.js');
const firmata = require('./controllers/FIRMATA.js');
const I2Cbackpack = require('./controllers/I2CBACKPACK.js');
var devices = [];
devices.push(blinkstick);
devices.push(emulator);
devices.push(firmata);
devices.push(I2Cbackpack);
module.exports = {
start: function() {
start();
},
setShader: function(shader, params) {
setShader(shader, params)
},
savePreviousShader: function() {
savePreviousShader();
},
restorePreviousShader: function() {
restorePreviousShader();
},
newFrame: function() {
return newFrame();
},
clearFrame: function(frame)
{
clearFrame(frame);
},
setAlpha: function(a)
{
setAlpha(a);
},
getAlpha: function()
{
return getAlpha();
},
produceFrame: function(frame)
{
produceFrame(frame);
},
setFramerates: function(producer_fps, consumer_fps)
{
setFramerates(producer_fps, consumer_fps);
},
getProducerFramerate: function()
{
return getProducerFramerate();
},
getConsumerFramerate: function()
{
return getConsumerFramerate();
},
setSize: function(w, h)
{
setSize(w, h);
},
getSize: function()
{
return getSize();
},
getWidth: function()
{
return width;
},
getHeight: function()
{
return height;
},
start: function()
{
start();
},
stop: function()
{
stop();
},
isStreaming: function()
{
return streaming;
}
}
var shader = null;
var params = null;
var onFrame = null;
var width = 8; //Default 8 LEDs in a
var height = 1; //single row.
var producer_framerate = 20; //Default low frame production for morphing
var consumer_framerate = 60; //Default high frame rendering for morphing
var alpha = 0.1; //Default is transparent frames for morphing
var stream = []; //Stream for frames
var composite = null; //Composite frame for morphing
var currentFrame = null; //Latest frame from stream
var streaming = false; //Pause
var producer_timer = null;
var consumer_timer = null;
var prevShader = null;
var prevParams = null;
var prevConsumerFramerate = 0;
var prevProducerFramerate = 0;
var prevAlpha = 0;
//Stream Producer
function producer(){
if (streaming)
onFrame(); //Call user defined function
producer_timer = setTimeout(producer, 1000/producer_framerate);
}
//Stream Consumer
function consumer(){
if (streaming)
consumeFrame(); //Render frame to BlinkStick
consumer_timer = setTimeout(consumer, 1000/consumer_framerate);
}
//Create an empty frame
function newFrame(){
return new Buffer(getSize()*3);
}
//Clear a frame
function clearFrame(frame){
if (frame != null)
for (i=0; i<getSize()*3; i++)
frame[i] =0;
}
//Produce frame on stream - called from user-defined Shader()
function produceFrame(frame)
{
if (stream.length<=0) //Skip frame if consumer is falling behind
stream.push(frame);
}
//Consume frame from stream - called from consumer
function consumeFrame()
{
if (stream.length>0) //Check if new frame available
currentFrame = stream.shift();
if (currentFrame != null)
morphFrame(currentFrame); //Morph to the current frame
}
//Morph current frame over composite frame (skip stream transition edge cases)
function morphFrame(current)
{
if (current.length == composite.length){
for (var i = 0; i<getSize(); i++) {
composite[i*3+0] = Math.floor(current[i*3+0]*alpha + composite[i*3+0]*(1-alpha)); // R
composite[i*3+1] = Math.floor(current[i*3+1]*alpha + composite[i*3+1]*(1-alpha)); // G
composite[i*3+2] = Math.floor(current[i*3+2]*alpha + composite[i*3+2]*(1-alpha)); // B
}
for (i=0; i<devices.length; i++)
devices[i].setColors(shader.getName(), width, height, composite);
}
}
//Shader() to stream user-defined shaders
function setShader(s, p)
{
DEBUG && console.log("setShader: "+p);
stop();
savePreviousShader();
shader = s;
params = p;
onFrame = shader.getOnFrame();
setSize(params.width, params.height);
s.start(params);
start();
}
function savePreviousShader(){
DEBUG && console.log("save");
prevShader = shader;
prevParams = params;
prevConsumerFramerate = consumer_framerate;
prevProducerFramerate = producer_framerate;
prevAlpha = alpha;
}
function restorePreviousShader(){
DEBUG && console.log("restore");
consumer_framerate = prevConsumerFramerate;
producer_framerate = prevProducerFramerate;
alpha = prevAlpha;
setShader(prevShader, prevParams);
}
function setFramerates(producer_fps, consumer_fps)
{
consumer_framerate = Math.max(1, Math.min(consumer_fps, 60));
producer_framerate = Math.max(1, Math.min(producer_fps, consumer_fps));
}
function getProducerFramerate()
{
return producer_framerate;
}
function getConsumerFramerate()
{
return consumer_framerate;
}
function setSize(w,h)
{
if (width!=w || height != h){
width = w;
height = h;
composite = newFrame();
}
if (composite == null)
composite = newFrame();
stream = [];
}
function getSize()
{
return width*height;
}
function setAlpha(a)
{
alpha = Math.max(0, Math.min(a, 1)); //Clamp between 0 (invisible) and 1 (opaque)
}
function getAlpha()
{
return alpha;
}
function start()
{
streaming = true;
}
function stop()
{
streaming = false;
}
//Clean exit
process.on('SIGTERM', onExit);
process.on('SIGINT', onExit);
function onExit(){
stop(); //Disable streaming to ensure no pending frames are set after LEDs are turned off
for (i=1; i<devices.length; i++)
devices[i].turnOff(null);
devices[0].turnOff(process.exit);
}
producer();
consumer();