2014-02-02 04:16:30 +00:00
|
|
|
"use strict"
|
|
|
|
|
|
|
|
var FTDI = require('ftdi')
|
|
|
|
|
2014-02-22 23:32:46 +00:00
|
|
|
function EnttecOpenUsbDMX(device_id, cb) {
|
2014-04-19 21:49:17 +00:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
cb = cb || function() {}
|
|
|
|
self.universe = new Buffer(513);
|
|
|
|
self.universe.fill(0);
|
|
|
|
self.sleepTime = 20;
|
|
|
|
self.timeout;
|
|
|
|
|
|
|
|
self.dev = new FTDI.FtdiDevice(device_id)
|
|
|
|
self.dev.open({
|
|
|
|
'baudrate': 115200 / 2,
|
|
|
|
'databits': 8,
|
|
|
|
'stopbits': 2,
|
|
|
|
'parity': 'none'
|
|
|
|
}, function(err) {
|
|
|
|
cb(err, device_id)
|
|
|
|
if(!err) {
|
|
|
|
self.loopUniverse()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
EnttecOpenUsbDMX.prototype.loopUniverse = function(){
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
self.dev.write(self.universe);
|
|
|
|
self.timeout = setTimeout(function(){
|
|
|
|
self.loopUniverse();
|
|
|
|
}, self.sleepTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
EnttecOpenUsbDMX.prototype.pause = function(){
|
|
|
|
clearTimeout(self.timeout);
|
2014-02-02 04:16:30 +00:00
|
|
|
}
|
|
|
|
|
2014-04-19 21:49:17 +00:00
|
|
|
EnttecOpenUsbDMX.prototype.close = function(cb){
|
|
|
|
self.dev.close(function(err){
|
|
|
|
cb(err)
|
|
|
|
})
|
2014-02-02 04:16:30 +00:00
|
|
|
}
|
|
|
|
|
2014-02-22 23:32:46 +00:00
|
|
|
EnttecOpenUsbDMX.prototype.update = function(u) {
|
2014-04-19 21:49:17 +00:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
for(var c in u) {
|
|
|
|
self.universe[(c + 1)] = u[self.toHex(c)];
|
|
|
|
}
|
2014-02-02 04:16:30 +00:00
|
|
|
}
|
|
|
|
|
2014-02-22 23:32:46 +00:00
|
|
|
EnttecOpenUsbDMX.prototype.get = function(c) {
|
2014-04-19 21:49:17 +00:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return self.universe[(c + 1)];
|
|
|
|
}
|
|
|
|
|
|
|
|
EnttecOpenUsbDMX.prototype.toHex = function(number){
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
var octet = parseInt(number).toString(16);
|
|
|
|
if(octet.length == 1){ octet = "0" + octet; }
|
|
|
|
var fullOctet = "0x" + octet;
|
|
|
|
return eval(fullOctet); // TODO: dangerous!
|
2014-02-02 04:16:30 +00:00
|
|
|
}
|
|
|
|
|
2014-04-19 21:49:17 +00:00
|
|
|
module.exports = EnttecOpenUsbDMX
|