/
boneServer.js
133 lines (110 loc) · 3.79 KB
/
boneServer.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
#!/usr/bin/node
// From Getting Started With node.js and socket.io
// http://codehenge.net/blog/2011/12/getting-started-with-node-js-and-socket-io-v0-7-part-2/
// This is a general server for the various web frontends
// buttonBox, ioPlot, realtimeDemo
"use strict";
var port = 8080, // Port to listen on
http = require('http'),
url = require('url'),
fs = require('fs'),
b = require('bonescript'),
//child_process = require('child_process'),
server,
connectCount = 0, // Number of connections to server
NUMLIGHTS = 160, // Number of LEDs in the LED string
MAX_TOTAL_DELAY_SECONDS = 600, // maximum ammount of total delay time allowed in lightDeltasQueue
MAX_TOTAL_DELTAS = 10000, // maximum size of the light deltas queue
lightsDisplaying = false, // if it is working it's way through the light deltas queue currently
lightDeltasQueue = []; // the list of lights changes / delays to do, in order.
lightDeltasQueue.totalDelay = 0; // miliseconds of delay in the frams in the
// Initialize various IO things.
function initIO() {
// Make sure gpios 7 and 20 are available.
//b.pinMode('P9_42', b.INPUT);
//b.pinMode('P9_41', b.INPUT);
//b.pinMode(pwm, b.INPUT); // PWM
// Initialize pwm
}
function send404(res) {
res.writeHead(404);
res.write('404');
res.end();
}
initIO();
server = http.createServer(function (req, res) {
// server code
var path = url.parse(req.url).pathname;
console.log("path: " + path);
if (path === '/') {
path = '/boneServer.html';
}
fs.readFile(__dirname + path, function (err, data) {
if (err) {return send404(res); }
// console.log("path2: " + path);
res.write(data, 'utf8');
res.end();
});
});
server.listen(port);
console.log("Listening on " + port);
var setLights = function(lightsToChange){
var ledChain = "";
var file ='/sys/firmware/lpd8806/device/rgb';
for (var i = 0; i < lightsToChange.length; i++){
ledChain = "";
if (0 <= Math.round(lightsToChange[i][3]) <= NUMLIGHTS-1){
ledChain +=
(Math.min(Math.max(Math.round(lightsToChange[i][0]),0),127) | 0) + " " +
(Math.min(Math.max(Math.round(lightsToChange[i][1]),0),127) | 0) + " " +
(Math.min(Math.max(Math.round(lightsToChange[i][2]),0),127) | 0) + " " +
(Math.min(Math.max(Math.round(lightsToChange[i][3]),0),NUMLIGHTS-1) | 0);
b.writeTextFile(file, ledChain);
}
}
b.writeTextFile(file, "\n");
};
var processLights = function(){
var currentDelta;
while (lightDeltasQueue.length > 0){
currentDelta= lightDeltasQueue.shift();
setLights(currentDelta.data);
lightDeltasQueue.totalDelay -= (currentDelta.delay | 0);
if (10000 > (currentDelta.delay | 0) > 0 ) {
setTimeout(processLights, currentDelta.delay);
return;
}
}
lightsDisplaying = false;
};
var restartProcessing = function(){
if (lightsDisplaying == false){
lightsDisplaying = true;
processLights();
}
};
// socket.io, I choose you
var io = require('socket.io').listen(server);
io.set('log level', 2);
// See https://github.com/LearnBoost/socket.io/wiki/Exposed-events
// for Exposed events
// on a 'connection' event
io.sockets.on('connection', function (socket) {
console.log("Connection " + socket.id + " accepted.");
socket.on('LEDChain2', function (params) {
if (lightDeltasQueue.totalDelay < 1000 * MAX_TOTAL_DELAY_SECONDS && lightDeltasQueue.length < MAX_TOTAL_DELTAS){
lightDeltasQueue.push(params);
lightDeltasQueue.totalDelay += (params.delay | 0);
restartProcessing();
} else {
console.log("dropping frame. current total delay: " + lightDeltasQueue.totalDelay + ", current frame count: " + lightDeltasQueue.length);
}
});
socket.on('disconnect', function () {
console.log("Connection " + socket.id + " terminated.");
connectCount--;
console.log("connectCount = " + connectCount);
});
connectCount++;
console.log("connectCount = " + connectCount);
});