2016-11-12 16:31:45 +00:00
|
|
|
"use strict"
|
|
|
|
|
|
|
|
var SerialPort = require("serialport")
|
|
|
|
|
2017-07-30 15:17:05 +00:00
|
|
|
var UNIVERSE_LEN = 512
|
|
|
|
|
2016-11-12 16:31:45 +00:00
|
|
|
function DMX4ALL(device_id, options) {
|
|
|
|
var self = this
|
|
|
|
options = options || {}
|
2017-07-30 15:17:05 +00:00
|
|
|
this.universe = new Buffer(UNIVERSE_LEN + 1)
|
2016-11-12 16:31:45 +00:00
|
|
|
this.universe.fill(0)
|
|
|
|
|
|
|
|
this.dev = new SerialPort(device_id, {
|
2018-01-10 20:28:44 +00:00
|
|
|
'baudRate': 38400,
|
2016-11-12 16:31:45 +00:00
|
|
|
'databits': 8,
|
|
|
|
'stopbits': 1,
|
|
|
|
'parity': 'none'
|
|
|
|
}, function(err) {
|
|
|
|
if(!err) {
|
|
|
|
self.send_universe()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
this.dev.on('data', function(data) {
|
|
|
|
//process.stdout.write(data.toString('ascii'))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
DMX4ALL.prototype.send_universe = function() {
|
2018-01-10 20:28:44 +00:00
|
|
|
if(!this.dev.writable) {
|
2016-11-12 16:31:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-30 15:17:05 +00:00
|
|
|
var msg = Buffer(UNIVERSE_LEN * 3)
|
|
|
|
for(var i = 0; i < UNIVERSE_LEN; i++) {
|
2016-11-12 16:31:45 +00:00
|
|
|
msg[i * 3 + 0] = (i < 256) ? 0xE2 : 0xE3
|
|
|
|
msg[i * 3 + 1] = i
|
2017-07-30 15:17:05 +00:00
|
|
|
msg[i * 3 + 2] = this.universe[i + 1]
|
2016-11-12 16:31:45 +00:00
|
|
|
}
|
|
|
|
this.dev.write(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
DMX4ALL.prototype.start = function() {}
|
|
|
|
DMX4ALL.prototype.stop = function() {}
|
|
|
|
|
|
|
|
DMX4ALL.prototype.close = function(cb) {
|
|
|
|
this.dev.close(cb)
|
|
|
|
}
|
|
|
|
|
|
|
|
DMX4ALL.prototype.update = function(u) {
|
|
|
|
for(var c in u) {
|
|
|
|
this.universe[c] = u[c]
|
|
|
|
}
|
|
|
|
this.send_universe()
|
|
|
|
}
|
|
|
|
|
|
|
|
DMX4ALL.prototype.updateAll = function(v){
|
2017-07-30 15:17:05 +00:00
|
|
|
for(var i = 1; i <= 512; i++) {
|
2016-11-12 16:31:45 +00:00
|
|
|
this.universe[i] = v
|
|
|
|
}
|
|
|
|
this.send_universe()
|
|
|
|
}
|
|
|
|
|
|
|
|
DMX4ALL.prototype.get = function(c) {
|
|
|
|
return this.universe[c]
|
|
|
|
}
|
|
|
|
|
2018-01-10 20:28:44 +00:00
|
|
|
module.exports = DMX4ALL
|