dmx-flok/drivers/dmx4all.js

68 lines
1.2 KiB
JavaScript
Raw Normal View History

2016-11-12 16:31:45 +00:00
"use strict"
var SerialPort = require("serialport")
var UNIVERSE_LEN = 512
2016-11-12 16:31:45 +00:00
function DMX4ALL(device_id, options) {
var self = this
options = options || {}
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,
'dataBits': 8,
'stopBits': 1,
2016-11-12 16:31:45 +00:00
'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
}
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
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){
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