diff --git a/anim.js b/anim.js index ce5efac..103e951 100644 --- a/anim.js +++ b/anim.js @@ -1,56 +1,62 @@ -var ease = require('./easing.js').ease -var resolution = 25; +"use strict" -exports.Anim = function() { - this.fx_stack = []; - this.add = function(to, duration, options) { - var options = options || {}; - var duration = duration || resolution; - options['easing'] = options['easing'] || 'linear'; - this.fx_stack.push({'to': to, 'duration': duration, 'options': options}); - return this; - } - this.delay = function(duration) { - return this.add({}, duration); - } - this.run = function(universe, onFinish) { - var config = {} - , t = 0 - , d = 0 - , a - ; - - var fx_stack = this.fx_stack; - var ani_setup = function() { - a = fx_stack.shift(); - t = 0; - d = a.duration; - config = {} - for(var k in a.to) { - config[k] = { - 'start': universe.get(k), - 'end': a.to[k] - } - } - } - var ani_step = function() { - new_vals = {}; - for(var k in config) { - new_vals[k] = Math.round(config[k].start + ease['linear'](t, 0, 1, d) * (config[k].end - config[k].start)) - } - t = t + resolution; - universe.update(new_vals); - if(t > d) { - if(fx_stack.length > 0) { - ani_setup() - } else { - clearInterval(iid); - if(onFinish) onFinish(); - } - } - } - ani_setup(); - var iid = setInterval(ani_step, resolution); - } +var ease = require('./easing.js').ease +var resolution = 25 + +function Anim() { + this.fx_stack = [] } +Anim.prototype.add = function(to, duration, options) { + var options = options || {} + var duration = duration || resolution + options['easing'] = options['easing'] || 'linear' + this.fx_stack.push({'to': to, 'duration': duration, 'options': options}) + return this +} + +Anim.prototype.delay = function(duration) { + return this.add({}, duration) +} + +Anim.prototype.run = function(universe, onFinish) { + var config = {} + var t = 0 + var d = 0 + var a + + var fx_stack = this.fx_stack; + var ani_setup = function() { + a = fx_stack.shift() + t = 0 + d = a.duration + config = {} + for(var k in a.to) { + config[k] = { + 'start': universe.get(k), + 'end': a.to[k] + } + } + } + var ani_step = function() { + new_vals = {} + for(var k in config) { + new_vals[k] = Math.round(config[k].start + ease['linear'](t, 0, 1, d) * (config[k].end - config[k].start)) + } + t = t + resolution + universe.update(new_vals) + if(t > d) { + if(fx_stack.length > 0) { + ani_setup() + } else { + clearInterval(iid) + if(onFinish) onFinish() + } + } + } + + ani_setup() + var iid = setInterval(ani_step, resolution) +} + +module.exports = Anim \ No newline at end of file diff --git a/dmx.js b/dmx.js index 0dfbd97..a722e75 100644 --- a/dmx.js +++ b/dmx.js @@ -1,27 +1,28 @@ -var events = require('events') - , config = require('./config.js') - , web = require('./web.js') - , setup = require('./setup.js').setup - , devices = require('./devices.js').devices - ; - +"use strict" -var dmx = new events.EventEmitter(); +var util = require('util') +var EventEmitter = require('events').EventEmitter -dmx.config = config; -dmx.setup = setup; -dmx.devices = devices; -dmx.drivers = {}; - - -dmx.update = function(universe, update) { - dmx.drivers[universe].update(update); - dmx.emit('update', universe, update); -} - -for(var universe in setup.universes) { - dmx.drivers[universe] = require('./drivers/' + setup.universes[universe].output.driver + '.js').init(setup.universes[universe].output.device); +function DMX() { + this.universes = {} + this.drivers = {} + this.registerDriver('null', require('./drivers/null')) + this.registerDriver('enttec-usb-dmx-pro', require('./drivers/enttec-usb-dmx-pro')) } +util.inherits(DMX, EventEmitter) -web.init(dmx); +DMX.prototype.registerDriver = function(name, module) { + this.drivers[name] = module +} + +DMX.prototype.addUniverse = function(name, driver, device_id) { + this.universes[name] = new this.drivers[driver](device_id) +} + +DMX.prototype.update = function(universe, channels) { + this.universes[universe].update(channels) + this.emit('update', universe, channels) +} + +module.exports = DMX diff --git a/drivers/enttec-usb-dmx-pro.js b/drivers/enttec-usb-dmx-pro.js index 9dc6e91..514f1dc 100644 --- a/drivers/enttec-usb-dmx-pro.js +++ b/drivers/enttec-usb-dmx-pro.js @@ -1,3 +1,5 @@ +"use strict" + var FTDI = require('ftdi') var ENTTEC_PRO_DMX_STARTCODE = 0x00 @@ -7,51 +9,52 @@ var ENTTEC_PRO_DMX_STARTCODE = 0x00 , ENTTEC_PRO_RECV_DMX_PKT = 0x05 ; - -exports.init = function(dev_id) { - var send_universe = function(dev, universe) { - var hdr = Buffer([ - ENTTEC_PRO_START_OF_MSG, - ENTTEC_PRO_SEND_DMX_RQ, - (universe.length + 1) & 0xff, - ((universe.length + 1) >> 8) & 0xff, - ENTTEC_PRO_DMX_STARTCODE - ]) - - var msg = Buffer.concat([ - hdr, - universe, - Buffer([ENTTEC_PRO_END_OF_MSG]) - ]) - dev.write(msg) - } +function EnttecUSBDMXPRO(device_id, cb) { + var self = this + cb = cb || function() {} + this.universe = new Buffer(512) + this.universe.fill(0) - var universe = new Buffer(512) - universe.fill(0) - - var dev = new FTDI.FtdiDevice(dev_id) - dev.open({ + this.dev = new FTDI.FtdiDevice(device_id) + this.dev.open({ 'baudrate': 250000, 'databits': 8, 'stopbits': 2, 'parity': 'none' }, function(err) { - console.log(err) - }) - - this.update = function(u) { - for(var k in u) { - universe[k] = u[k] + cb(err, device_id) + if(!err) { + self.send_universe() } - } - - this.get = function(c) { - return universe[c] - } - - setInterval(function() { - send_universe(dev, universe); - }, 25) - - return this; + }) } + +EnttecUSBDMXPRO.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) >> 8) & 0xff, + ENTTEC_PRO_DMX_STARTCODE + ]) + + var msg = Buffer.concat([ + hdr, + this.universe, + Buffer([ENTTEC_PRO_END_OF_MSG]) + ]) + this.dev.write(msg) +} + +EnttecUSBDMXPRO.prototype.update = function(u) { + for(var c in u) { + this.universe[c] = u[c] + } + this.send_universe() +} + +EnttecUSBDMXPRO.prototype.get = function(c) { + return this.universe[c] +} + +module.exports = EnttecUSBDMXPRO \ No newline at end of file diff --git a/drivers/null.js b/drivers/null.js index 84fb1d8..c63e4f3 100644 --- a/drivers/null.js +++ b/drivers/null.js @@ -1,19 +1,21 @@ -exports.init = function(dev_id) { - var universe = new Buffer(512) - universe.fill(0) - - this.update = function(u) { - for(var k in u) { - universe[k] = u[k] - } - } +"use strict" - this.get = function(k) { - return universe[k]; - } - - setInterval(function() { - console.log(universe); - }, 1000); - return this; +function Null(device_id, cb) { + var self = this + cb = cb || function() {} + this.universe = new Buffer(512) + this.universe.fill(0) } + +Null.prototype.update = function(u) { + for(var c in u) { + this.universe[c] = u[c] + } + console.log(this.universe) +} + +Null.prototype.get = function(c) { + return this.universe[c] +} + +module.exports = Null \ No newline at end of file diff --git a/index.html b/index.html index a4c2f7d..1eec641 100644 --- a/index.html +++ b/index.html @@ -19,7 +19,7 @@ margin-bottom: 2em; } - input[type=range]:after { + input[type=range]:before { content: attr(value); color: #eee; font-family: monospace; @@ -27,6 +27,8 @@ width: 60px; display: block; color: #fff; + position: absolute; + margin-bottom: 2em; } .device { clear: both; @@ -125,7 +127,7 @@ diff --git a/web.js b/web.js index 51e582f..bfef8b6 100644 --- a/web.js +++ b/web.js @@ -1,11 +1,20 @@ +"use strict" + var http = require('http'), path = require('path'), io = require('socket.io'), fs = require('fs'), A = require('./anim.js').Anim - ; +; -exports.init = function(dmx) { +var DMX = require('./dmx') +var config = require('./config.js') + , web = require('./web.js') + , setup = require('./setup.js').setup + , devices = require('./devices.js').devices + + +function DMXWeb(dmx) { function handler (request, response) { var reqBody = ''; @@ -79,10 +88,10 @@ exports.init = function(dmx) { var app = http.createServer(handler) - app.listen(dmx.config.port, '::', null, function() { + app.listen(config.port, '::', null, function() { try { - process.setgid(dmx.config.gid); - process.setuid(dmx.config.uid); + process.setgid(config.gid); + process.setuid(config.uid); } catch (err) { console.log(err); process.exit(1); @@ -90,12 +99,12 @@ exports.init = function(dmx) { }); io.listen(app).sockets.on('connection', function (socket) { - socket.emit('init', {'devices': dmx.devices, 'setup': dmx.setup}); + socket.emit('init', {'devices': devices, 'setup': setup}); socket.on('request_refresh', function() { - for(var universe in dmx.setup.universes) { - u = {} + for(var universe in setup.universes) { + var u = {} for(var i = 0; i < 256; i++) { - u[i] = dmx.drivers[universe].get(i); + u[i] = dmx.universes[universe].get(i); } console.log('sending update...') console.log(u) @@ -113,8 +122,16 @@ exports.init = function(dmx) { }); } - - - +var dmx = new DMX() + +for(var universe in setup.universes) { + dmx.addUniverse( + universe, + setup.universes[universe].output.driver, + setup.universes[universe].output.device + ) +} + +var web = new DMXWeb(dmx)