-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
55 lines (52 loc) · 1.61 KB
/
server.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
var static = require('node-static')
,file = new(static.Server)('./public')
,exec = require('child_process').exec
,nowjs = require("now")
,firmata = require('firmata');
var httpServer = require('http').createServer(function (request, response) {
request.addListener('end', function () {
//
// Serve files!
//
file.serve(request, response);
});
});
var everyone = nowjs.initialize(httpServer);
var currentBoard;
everyone.now.getPins=function(port,callback){
if(!currentBoard){
currentBoard = new firmata.Board(port,function(){
everyone.now.MODES=currentBoard.MODES;
callback(currentBoard.pins);
currentBoard.on('analog-read',function(e){
everyone.now.analogRead(e.pin,e.value);
});
currentBoard.on('digital-read',function(e){
console.log(e);
everyone.now.digitalRead(e.pin,e.value);
});
});
}else{
callback(currentBoard.pins);
}
};
everyone.now.pinMode=function(pin,mode){
currentBoard.pinMode(pin,mode);
};
everyone.now.digitalWrite=function(pin,value){
currentBoard.digitalWrite(pin,value);
};
everyone.now.analogWrite=function(pin,value){
currentBoard.analogWrite(pin,value);
};
everyone.now.getUSBPorts = function(callback){
exec('ls /dev/ttyS*',function(error,stdout,stderror){
var portArray = stdout.trim().split('\n');
var ports =[];
for(var i=0,length = portArray.length; i < length; i++){
ports.push({port:portArray[i]});
}
callback(ports);
});
};
httpServer.listen(8080);