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);
self.sleepTime = 20;
self.timeout;
self.dev = new FTDI.FtdiDevice(device_id)
self.dev.open({
'baudrate': 115200 / 2,
'databits': 8, 'databits': 8,
'stopbits': 2, 'stopbits': 2,
'parity': 'none' 'parity': 'none'
}, function(err) { }, function(err) {
cb(err, device_id) cb(err, device_id)
if(!err) { if(!err) {
self.send_universe() 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) {
var self = this;
for(var c in u) { for(var c in u) {
this.universe[c] = u[c] self.universe[(c + 1)] = u[self.toHex(c)];
} }
this.send_universe()
} }
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