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 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) {
var self = this
cb = cb || function() {}
this.universe = new Buffer(512)
this.universe.fill(0)
var self = this;
this.dev = new FTDI.FtdiDevice(device_id)
this.dev.open({
'baudrate': 115200,
'databits': 8,
'stopbits': 2,
'parity': 'none'
}, function(err) {
cb(err, device_id)
if(!err) {
self.send_universe()
}
})
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.send_universe = function() {
var hdr = Buffer([
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
])
EnttecOpenUsbDMX.prototype.loopUniverse = function(){
var self = this;
var msg = Buffer.concat([
hdr,
this.universe,
Buffer([ENTTEC_PRO_END_OF_MSG])
])
this.dev.write(msg)
self.dev.write(self.universe);
self.timeout = setTimeout(function(){
self.loopUniverse();
}, self.sleepTime)
}
EnttecOpenUsbDMX.prototype.pause = function(){
clearTimeout(self.timeout);
}
EnttecOpenUsbDMX.prototype.close = function(cb){
self.dev.close(function(err){
cb(err)
})
}
EnttecOpenUsbDMX.prototype.update = function(u) {
for(var c in u) {
this.universe[c] = u[c]
}
this.send_universe()
var self = this;
for(var c in u) {
self.universe[(c + 1)] = u[self.toHex(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