From 14f33da4dc5580ad8bf5f9caca8997190324871a Mon Sep 17 00:00:00 2001 From: Sebastian Wiedenroth Date: Fri, 15 May 2015 14:34:24 +0200 Subject: [PATCH] similarize drivers, needs more testing --- drivers/enttec-open-usb-dmx.js | 92 ++++++++++++++-------------------- drivers/enttec-usb-dmx-pro.js | 11 +++- drivers/null.js | 20 +++++++- 3 files changed, 67 insertions(+), 56 deletions(-) diff --git a/drivers/enttec-open-usb-dmx.js b/drivers/enttec-open-usb-dmx.js index 4e59985..40d24e8 100644 --- a/drivers/enttec-open-usb-dmx.js +++ b/drivers/enttec-open-usb-dmx.js @@ -2,77 +2,61 @@ var FTDI = require('ftdi') -function EnttecOpenUsbDMX(locationId, cb) { - var self = this; +function EnttecOpenUsbDMX(device_id, cb) { + var self = this - cb = cb || function() {} - self.universe = Array(513); - self.universe[0] = 0; - self.updateAll(0); - self.sleepTime = 24; - self.timeout; - self.dev = new FTDI.FtdiDevice(locationId) - self.dev.open({ - 'baudrate': 115200 / 2, - 'databits': 8, - 'stopbits': 2, - 'parity': 'none' - }, function(err) { - cb(err, locationId) - if(!err) { - self.loopUniverse() - } - }) + cb = cb || function() {} + this.universe = new Buffer(512) + this.universe.fill(0) + + self.sleepTime = 24 + self.timeout + + self.dev = new FTDI.FtdiDevice(device_id) + self.dev.open({ + 'baudrate': 57600, + 'databits': 8, + 'stopbits': 2, + 'parity': 'none' + }, function(err) { + cb(err, device_id) + if(!err) { + self.start() + } + }) } -EnttecOpenUsbDMX.prototype.loopUniverse = function(){ - var self = this; - clearTimeout(self.timeout); - - self.dev.write(self.universe); - - self.timeout = setTimeout(function(){ - self.loopUniverse(); - }, self.sleepTime) +EnttecOpenUsbDMX.prototype.send_universe = function() { + this.dev.write(this.universe) } -EnttecOpenUsbDMX.prototype.pause = function(){ - var self = this; - - clearTimeout(self.timeout); +EnttecOpenUsbDMX.prototype.start = function() { + this.timeout = setInterval(this.send_universe.bind(this), this.sleepTime) } -EnttecOpenUsbDMX.prototype.close = function(cb){ - var self = this; +EnttecOpenUsbDMX.prototype.stop = function() { + clearInterval(this.timeout) +} - self.pause(); - self.dev.close(function(err){ - cb(err) - }) +EnttecOpenUsbDMX.prototype.close = function(cb) { + this.stop() + this.dev.close(cb) } EnttecOpenUsbDMX.prototype.update = function(u) { - var self = this; - - for(var c in u) { - self.universe[(c + 1)] = u[c]; - } + for(var c in u) { + this.universe[c] = u[c] + } } EnttecOpenUsbDMX.prototype.updateAll = function(v) { - var self = this; - - var i = 1; - while(i < self.universe.length){ - self.universe[i] = v; - i++; - } + for(var i = 0; i < 512; i++) { + this.universe[i] = v + } } EnttecOpenUsbDMX.prototype.get = function(c) { - var self = this; - - return self.universe[(c + 1)]; + return this.universe[c] } module.exports = EnttecOpenUsbDMX diff --git a/drivers/enttec-usb-dmx-pro.js b/drivers/enttec-usb-dmx-pro.js index b13d2bd..0d8a160 100644 --- a/drivers/enttec-usb-dmx-pro.js +++ b/drivers/enttec-usb-dmx-pro.js @@ -46,6 +46,13 @@ EnttecUSBDMXPRO.prototype.send_universe = function() { this.dev.write(msg) } +EnttecUSBDMXPRO.prototype.start = function() {} +EnttecUSBDMXPRO.prototype.stop = function() {} + +EnttecUSBDMXPRO.prototype.close = function(cb) { + this.dev.close(cb) +} + EnttecUSBDMXPRO.prototype.update = function(u) { for(var c in u) { this.universe[c] = u[c] @@ -54,7 +61,9 @@ EnttecUSBDMXPRO.prototype.update = function(u) { } EnttecUSBDMXPRO.prototype.updateAll = function(v){ - // TODO + for(var i = 0; i < 512; i++) { + this.universe[i] = v + } } EnttecUSBDMXPRO.prototype.get = function(c) { diff --git a/drivers/null.js b/drivers/null.js index 8259b6b..4b3c2ba 100644 --- a/drivers/null.js +++ b/drivers/null.js @@ -5,6 +5,22 @@ function Null(device_id, cb) { cb = cb || function() {} this.universe = new Buffer(512) this.universe.fill(0) + self.start() +} + +Null.prototype.start = function() { + var self = this + self.timeout = setInterval(function() { + console.log(self.universe) + }, 1000) +} + +Null.prototype.stop = function() { + clearInterval(this.timeout) +} + +Null.prototype.close = function(cb) { + cb(null) } Null.prototype.update = function(u) { @@ -15,7 +31,9 @@ Null.prototype.update = function(u) { } Null.prototype.updateAll = function(v){ - // TODO + for(var i = 0; i < 512; i++) { + this.universe[i] = v + } } Null.prototype.get = function(c) {