-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
106 lines (79 loc) · 1.94 KB
/
server.js
File metadata and controls
106 lines (79 loc) · 1.94 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
'use strict';
var express = require('express'),
app = express(),
http = require('http').Server(app),
io = require('socket.io')(http),
fadecandyClient = require('nd-fadecandy-client')
;
// sendFile options
var options = {
root: __dirname + '/public/',
dotfiles: 'deny',
headers: {
'x-timestamp': Date.now(),
'x-sent': true
}
};
// Enable static files
app.use(express.static('public/asset'));
// Send the index.html to new clients
app.get('/', function(req, res){
res.sendFile('index.html', options);
});
http.listen(1337, function(){
console.log('listening on *:1337');
});
var pixels = null;
var color = { red : 0, green : 0, blue : 0 };
// Connect to the Fadecandy Server
var client = new fadecandyClient({
host: 'localhost',
port: 7890,
debug : false
});
/**
* Connections from the browser (NERDDISCO Studio)
*/
// Create custom namespace
var socket_studio = io.of('/NERDDISCO-Studio');
// Listen to connections on this custom namespace
socket_studio.on('connection', function(socket) {
console.log('NERD DISCO - Studio connected');
// Received and LED array from the client
socket.on('NERDDISCO.input', function(data) {
pixels = data;
draw();
});
});
/**
* Extract the pixel data from the given position
*
* @param Interger position
*
* @return Object pixel
*/
function getPixel(position) {
var pixel = {};
var _position = position * 3;
// Pixel exists
if (pixels !== null && pixels[_position - 1] !== undefined) {
// Blue
pixel.b = pixels[_position - 1];
// Green
pixel.g = pixels[_position - 2];
// Red
pixel.r = pixels[_position - 3];
} else {
// Black
pixel.a = pixel.r = pixel.g = pixel.b = 0;
}
return pixel;
}
function draw() {
var amount = 30 * 8;
for (var pixel = 0; pixel < amount; pixel++) {
color = getPixel(pixel + 1);
client.setPixel(pixel, color.r, color.g, color.b);
}
client.writePixels();
}