Update enttec-open-usb-dmx.js

This commit is contained in:
Evan Tahler 2014-04-19 14:49:17 -07:00
parent 519d1ee0d7
commit 9de02a0240

View file

@ -2,59 +2,69 @@
var FTDI = require('ftdi') var FTDI = require('ftdi')
var ENTTEC_PRO_DMX_STARTCODE = 0x00
, ENTTEC_PRO_START_OF_MSG = 0x7e
, ENTTEC_PRO_END_OF_MSG = 0xe7
, ENTTEC_PRO_SEND_DMX_RQ = 0x06
;
function EnttecOpenUsbDMX(device_id, cb) { function EnttecOpenUsbDMX(device_id, cb) {
var self = this var self = this;
cb = cb || function() {}
this.universe = new Buffer(512)
this.universe.fill(0)
this.dev = new FTDI.FtdiDevice(device_id) cb = cb || function() {}
this.dev.open({ self.universe = new Buffer(513);
'baudrate': 115200, self.universe.fill(0);
'databits': 8, self.sleepTime = 20;
'stopbits': 2, self.timeout;
'parity': 'none'
}, function(err) { self.dev = new FTDI.FtdiDevice(device_id)
cb(err, device_id) self.dev.open({
if(!err) { 'baudrate': 115200 / 2,
self.send_universe() 'databits': 8,
} 'stopbits': 2,
}) 'parity': 'none'
}, function(err) {
cb(err, device_id)
if(!err) {
self.loopUniverse()
}
})
} }
EnttecOpenUsbDMX.prototype.send_universe = function() { EnttecOpenUsbDMX.prototype.loopUniverse = function(){
var hdr = Buffer([ var self = this;
ENTTEC_PRO_START_OF_MSG,
ENTTEC_PRO_SEND_DMX_RQ,
// (this.universe.length + 1) & 0xff,
((this.universe.length + 1) >> 0) & 0xff,
((this.universe.length + 1) >> 8) & 0xff,
ENTTEC_PRO_DMX_STARTCODE
])
var msg = Buffer.concat([ self.dev.write(self.universe);
hdr, self.timeout = setTimeout(function(){
this.universe, self.loopUniverse();
Buffer([ENTTEC_PRO_END_OF_MSG]) }, self.sleepTime)
]) }
this.dev.write(msg)
EnttecOpenUsbDMX.prototype.pause = function(){
clearTimeout(self.timeout);
}
EnttecOpenUsbDMX.prototype.close = function(cb){
self.dev.close(function(err){
cb(err)
})
} }
EnttecOpenUsbDMX.prototype.update = function(u) { EnttecOpenUsbDMX.prototype.update = function(u) {
for(var c in u) { var self = this;
this.universe[c] = u[c]
} for(var c in u) {
this.send_universe() self.universe[(c + 1)] = u[self.toHex(c)];
}
} }
EnttecOpenUsbDMX.prototype.get = function(c) { EnttecOpenUsbDMX.prototype.get = function(c) {
return this.universe[c] 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!
} }
module.exports = EnttecOpenUsbDMX module.exports = EnttecOpenUsbDMX