-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-dmx-test.js
88 lines (73 loc) · 1.64 KB
/
node-dmx-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// A simple DMX example which will turn all the lights on and off every second
// You can use this as a fork within another application as well (cluster-awareness)
////////////
// dmx.js //
////////////
var ftdi = require('ftdi');
var cluster = require('cluster');
var settings = {
// 'baudrate': 250000,
// 'baudrate': 250000 / 4,
'baudrate': 115200 / 2,
'databits': 8,
'stopbits': 2,
'parity' : 'none',
};
var sleepTime = 0.026 * 1000;
var device;
var universe = new Buffer(512, 'binary');
var on = false;
var loopTimer;
function writeLoop(){
clearTimeout(loopTimer);
// device.write([]);
setTimeout(function(){
device.write([0x00]);
device.write(universe);
if(cluster.isMaster){ process.stdout.write("."); }
loopTimer = setTimeout(writeLoop, sleepTime);
}, 88);
}
function set(k,v){
universe[parseInt(k) - 1] = parseInt(v);
console.log('[DMX] ' + k + ':' + v);
}
function setAll(v){
var i = 0;
while(i < universe.length){
universe[i] = parseInt(v);
i++;
}
console.log('[DMX] all:' + v);
}
var flop = function(){
if(on === true){
setAll(0);
}else{
setAll(250);
}
on = !on;
};
////////
// GO //
////////
setAll(0);
ftdi.find(function(err, devices){
console.log(devices);
device = new ftdi.FtdiDevice(devices[0]);
device.open(settings, function(){
writeLoop();
if(cluster.isMaster){
setInterval(flop, 500);
}else{
process.on('message', function(message){
message = JSON.parse(message);
if(message.all === true){
setAll(message.power);
}else{
set(message.channel, message.power);
}
});
}
});
});