dmx-flok/drivers/enttec-open-usb-dmx.js

76 lines
1.4 KiB
JavaScript
Raw Normal View History

2014-02-02 04:16:30 +00:00
"use strict"
var SerialPort = require("serialport").SerialPort
2014-02-02 04:16:30 +00:00
function EnttecOpenUsbDMX(device_id, options) {
2015-05-15 12:34:24 +00:00
var self = this
options = options || {}
2015-05-15 12:34:24 +00:00
this.universe = new Buffer(512)
this.universe.fill(0)
2016-10-12 15:23:16 +00:00
self.interval = 46
2015-05-15 12:34:24 +00:00
this.dev = new SerialPort(device_id, {
2016-10-12 15:23:16 +00:00
'baudrate': 250000,
2015-05-15 12:34:24 +00:00
'databits': 8,
'stopbits': 2,
'parity': 'none'
}, true, function(err) {
2016-10-13 21:36:49 +00:00
if(err) {
console.log(err)
return
2015-05-15 12:34:24 +00:00
}
2016-10-13 21:36:49 +00:00
self.start()
2015-05-15 12:34:24 +00:00
})
2014-04-19 21:49:17 +00:00
}
2015-05-15 12:34:24 +00:00
EnttecOpenUsbDMX.prototype.send_universe = function() {
2016-06-02 19:28:23 +00:00
var self = this
if(!this.dev.isOpen()) {
return
}
// toggle break
2016-06-02 19:28:23 +00:00
self.dev.set({brk: true}, function(err, r) {
2016-10-12 15:23:16 +00:00
setTimeout(function() {
self.dev.set({brk: false}, function(err, r) {
setTimeout(function() {
self.dev.write(Buffer.concat([Buffer([0]), self.universe]))
}, 1)
})
}, 1)
})
2014-04-19 21:49:17 +00:00
}
2015-05-15 12:34:24 +00:00
EnttecOpenUsbDMX.prototype.start = function() {
2016-04-09 16:11:48 +00:00
this.intervalhandle = setInterval(this.send_universe.bind(this), this.interval)
2014-02-02 04:16:30 +00:00
}
2015-05-15 12:34:24 +00:00
EnttecOpenUsbDMX.prototype.stop = function() {
2016-04-09 16:11:48 +00:00
clearInterval(this.intervalhandle)
2015-05-15 12:34:24 +00:00
}
2015-05-15 12:34:24 +00:00
EnttecOpenUsbDMX.prototype.close = function(cb) {
this.stop()
this.dev.close(cb)
2014-02-02 04:16:30 +00:00
}
2014-02-22 23:32:46 +00:00
EnttecOpenUsbDMX.prototype.update = function(u) {
2015-05-15 12:34:24 +00:00
for(var c in u) {
this.universe[c] = u[c]
}
2014-02-02 04:16:30 +00:00
}
EnttecOpenUsbDMX.prototype.updateAll = function(v) {
2015-05-15 12:34:24 +00:00
for(var i = 0; i < 512; i++) {
this.universe[i] = v
}
2014-04-19 21:49:17 +00:00
}
EnttecOpenUsbDMX.prototype.get = function(c) {
2015-05-15 12:34:24 +00:00
return this.universe[c]
2014-02-02 04:16:30 +00:00
}
2014-04-19 21:49:17 +00:00
module.exports = EnttecOpenUsbDMX