-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplePlayer.js
More file actions
348 lines (294 loc) · 9.09 KB
/
Copy pathsimplePlayer.js
File metadata and controls
348 lines (294 loc) · 9.09 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//file Module
var fs = require('fs');
//render Module
var Renderer = require('./node_modules/renderModule.js');
//serial communication
//var SerialPort = require("serialport").SerialPort;
var DMX = require('dmxhost');
//-----------OPTIONS--------------------------------
//player speed
var fps = 30;
//create a socket based webserver to show arduino output
var enableWebInterface = true;
if(process.argv[2]=="start"){
console.log("start playing")
var playerPaused=false;
}
else{
console.log("pausing player")
var playerPaused=true;
}
/* Windows
//dmx device
DMX.device = "\\.\COM6";
DMX.relayPath="./node_modules/dmxhost.js/dmxhost-serial-relay.py";
DMX.log=true;
*/
// DMX
DMX.log = true;
DMX.device = '/dev/ttyACM0';
DMX.relayResponseTimeout = 10000;
DMX.relayPath = './node_modules/dmxhost/dmxhost-serial-relay.py';
//----------------------------------------------------
var DMXManager=function(){
//initialize and configure DMX Module
this.initialize=function(){
DMX.spawn( null, function( error ){
if ( error ){
console.log("--------DMX BRIDGE COULD NOT BE INITIALIZED -----------");
console.log( "Error:", error );
console.log("-------------------------------------------------------");
return;
}
});
};
//send data
//data should be array with 4*16 bytes
var rgbw = new Array(16*4);
var frameCounter = 0;
this.send=function(frame){
frameCounter++;
frameCounter=frameCounter % 3;
frame.map( function( rgbWindow, index )
{
rgbw[ 4*index + 0 ] = rgbWindow[0];
rgbw[ 4*index + 1 ] = rgbWindow[1];
rgbw[ 4*index + 2 ] = rgbWindow[2];
rgbw[ 4*index + 3 ] = 0;
});
if(enableWebInterface && frameCounter == 0){
//broadcast to all connected clients
app.io.broadcast('newFrame', rgbw);
}
//DMX.ready() && DMX.send( {data: allcolorsSerializedRGBW} );
DMX.ready() && DMX.send( {data: rgbw} );
};
};
var FileManagerObj = function(){
var that=this;
var blendingScene=[];
var scenedirectory="simplePlayer/simpleplayer_scenes/";
var defaultblendingscene=[{"duration":3000,"type":1,"windows":[{"color":[0,0,0]},{"color":[0,0,0]},{"color":[0,0,0]},{"color":[0,0,0]},{"color":[0,0,0]},{"color":[0,0,0]},{"color":[0,0,0]},{"color":[0,0,0]},{"color":[0,0,0]},{"color":[0,0,0]},{"color":[0,0,0]},{"color":[0,0,0]},{"color":[0,0,0]},{"color":[0,0,0]},{"color":[0,0,0]},{"color":[0,0,0]}]}];
var sceneList=[];
//returns blendingscene, if param frame is set function returns blending scene with correct framecount
this.getBlendingScene=function(frames){
if(!frames)
return blendingScene;
else{
newBlendingScene=[];
for(var i=0;i<frames;i++){
newBlendingScene.push(blendingScene[i%blendingScene.length]);
}
return newBlendingScene;
}
};
this.getOrderedSceneList=function(){
if(sceneList.length==0){
var files = fs.readdirSync(scenedirectory);
if(!files||files.length === 0){
console.log("no files in directory or failed to load");
return;
}
for (var i in files) {
if(/^(\d+_)/.test(files[i]))
sceneList.push(files[i]);
}
sceneList.sort();
console.log("files in scene directory:");
console.log(sceneList);
}
return sceneList;
};
this.loadSceneData=function(sceneName,callback) {
//load file
fs.readFile(scenedirectory+sceneName, "utf-8", function (err,result) {
var sceneData=[];
if (err){
console.log("could not load scene:"+sceneName);
console.log("error: "+err);
}
else{
//transform scene from fronted json format to simple array with all frames
sceneData=myRenderer.parse(JSON.parse(result));
}
callback(sceneData);
});
};
//initial loading of blending animation
this.loadBlendingScene=function(frameCount,callback){
fs.readFile(scenedirectory+'_blendingScene', "utf-8", function (err,result) {
if (err){
console.log("there is no blendingSceneFile to load");
console.log(err);
blendingScene=myRenderer.parse(defaultblendingscene);
}
else{
blendingScene=myRenderer.parse(JSON.parse(result));
}
if(callback)
callback(that.getBlendingScene(frameCount));
});
};
};
var PlayerObj = function(fps,fileManager,playerPaused){
var that=this;
var scenelist=[];
var currentSceneNumber=0;
var pausing=playerPaused;
var currentScene=[];
var nextScene=[];
var blendingScene=[];
var blendingSceneLengthInSeconds=4;
var currentSceneFrameNumber=0;
var currentsceneType="";
this.start=function(){
//load the blending scene data
scenelist=fileManager.getOrderedSceneList();
fileManager.loadBlendingScene(fps*blendingSceneLengthInSeconds, function(scenedata){
blendingScene=scenedata;
});
//interval to send frames to arduino
setInterval(that.playerTick,1000/fps);
};
this.pause=function(){
pausing=true;
}
this.play=function(){
pausing=false;
}
this.restart=function(){
console.log("restart");
currentSceneNumber=0;
currentSceneFrameNumber=0;
currentScene=[];
}
this.previous=function(){
console.log("previous");
currentSceneNumber=((currentSceneNumber-2)+scenelist.length)%scenelist.length;
currentSceneFrameNumber=0;
currentScene=[];
currentsceneType="";
}
this.next=function(){
console.log("next");
currentSceneFrameNumber=0;
currentScene=[];
currentsceneType="";
}
this.goToScenNr=function(sceneNumber){
currentSceneNumber=sceneNumber;
currentSceneFrameNumber=0;
currentScene=[];
currentsceneType="";
}
this.loadNextScene=function(){
//if the current scene endet and was of type blendingscene load a sheduled or a ranked scene
if(currentsceneType=="blendingScene"){
currentsceneType="normalScene";
currentScene=nextScene;
console.log("play scene "+scenelist[currentSceneNumber]+" for "+currentScene.length/fps+" seconds");
if(enableWebInterface)
app.io.broadcast('newSceneInfo', "scene "+scenelist[currentSceneNumber]+" ("+currentScene.length/fps+"s)");
currentSceneNumber=(currentSceneNumber+1)%scenelist.length;
}
//if the current scene endet and was of type rankedScene, sheduledscene or undefined then try to find the next scene and add a blendingscene until loading is done
else if(currentsceneType=="normalScene"||currentsceneType === ""){
//tell filemanager to load next scene
if(scenelist[currentSceneNumber]){
fileManager.loadSceneData(scenelist[currentSceneNumber],function(sceneData){
nextScene=sceneData;
});
}
else
console.log("no scenename to load");
console.log("play blendingscene for "+blendingScene.length/fps+" seconds")
if(enableWebInterface)
app.io.broadcast('newSceneInfo', "blendingscene ("+blendingScene.length/fps+"s)");
currentsceneType="blendingScene";
currentScene=blendingScene;
}
};
//player loop
this.playerTick=function(){
//if currentscene is not empty play that scene
if(currentScene.length>0){
//if this is the last frame of the scene load e new one and play the blending scene
if(currentSceneFrameNumber==currentScene.length){
that.loadNextScene();
currentSceneFrameNumber=0;
}
else{
if(currentScene[currentSceneFrameNumber]){
//send frame to arduino with dmx bridge
//console.log("------------ send Frame ----------------");
//console.log(currentScene[currentSceneFrameNumber]);
dmxManager.send(currentScene[currentSceneFrameNumber]);
//send status in percent, disabled for performance
//if(enableWebInterface)
//app.io.broadcast('percent', currentSceneFrameNumber/currentScene.length);
}
else{
console.log("this frame does not exist in current scene");
}
if(!pausing)
currentSceneFrameNumber++;
}
}
else{
//if there is no currentscene try to load one
that.loadNextScene();
}
};
};
//create new Renderer
var myRenderer = new Renderer(fps);
//initialize dmx serial manager
var dmxManager=new DMXManager();
dmxManager.initialize();
//socket io for debugging and testing interface (tower simulation)
if(enableWebInterface){
var express = require('express.io');
var app = express();
}
setTimeout( function()
{
//initialize fileManager
var fileManager=new FileManagerObj(fps);
//initialize player
console.log( "Starting player..." );
var player=new PlayerObj(fps,fileManager,playerPaused);
player.start();
if(enableWebInterface){
//open socket
app.http().io();
app.use(express.static(__dirname + '/simplePlayer/simplePlayer_interface'));
app.io.route('play', function(req) {
player.play();
})
app.io.route('pause', function(req) {
player.pause();
})
app.io.route('restart', function(req) {
player.restart();
})
app.io.route('next', function(req) {
player.next();
})
app.io.route('previous', function(req) {
player.previous();
})
app.io.route('hi', function(req) {
req.io.emit('sceneList', fileManager.getOrderedSceneList())
})
app.io.route('goto', function(req) {
sceneNumber=parseInt(req.data[0]);
console.log("goto scene "+sceneNumber)
player.goToScenNr(sceneNumber);
})
var server = app.listen(3001, function () {
var host = server.address().address;
var port = server.address().port;
console.log('tower app listening at http://%s:%s', host, port);
});
}
}, DMX.relayResponseTimeout );