-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEnttecDMX.sc
86 lines (76 loc) · 2.2 KB
/
EnttecDMX.sc
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
/********************************************
EnttecDMX Interface Class
Jonathan Reus, 2016
Usage:
~port = SerialPort.devices[2]; // the serial port where the Enttec is connected
d = EnttecDMX.new(~port, 3); // 3 channel DMX output
d.sendDMX(0.5,0.5,0.5); // set all 3 channels to half brightness
d.sendDMX(0, 0, 0); // set all 3 channels to 0 brightness
d.close(); // close the serial port
*********************************************/
EnttecDMX {
var <num_channels, ch1_idx;
var <port;
var <serial_packet;
var <>brightness;
*new {|portid=nil, numchannels=4, cmdperiod=true|
^super.new.init(portid, numchannels, cmdperiod);
}
// ENTTEC serial protocol
// 126 6 nb 0 0 b1 b2 b3 ... bn 231
// 126 - start byte 0x7E 6
// 6 - send DMX op code
// nb - number of bytes (channels) to send + 1
// 0 0 - start code
// b1 ... bn channel values (each 1 byte)
// 231 - end byte 0xE7
init {|portid, numchannels, cmdperiod|
var tmp;
num_channels = numchannels;
brightness = 1.0;
// Create serial packet array
serial_packet = Int8Array.with(0x7E, 6, numchannels + 1, 0, 0);
ch1_idx = 5;
numchannels.do({|i|
serial_packet = serial_packet.add(0);
});
serial_packet = serial_packet.add(0xE7);
// Create serial port
if(portid.isNil) {
// Use default
tmp = SerialPort.devicePattern;
SerialPort.devicePattern = "/dev/tty.usbserial-EN*";
portid = SerialPort.devices[0];
SerialPort.devicePattern = tmp;
};
("Opening serial port" + portid + " ...").postln;
port = SerialPort.new(portid, 57600);
if(cmdperiod == true) {
CmdPeriod.add({
this.close;
});
};
}
// DMX values from 0.0 - 1.0.
// Any value greater than 1.0 is replaced with the previously
// sent value.
sendDMX {|...vals|
var numargs = vals.size;
if(numargs > num_channels) {
("Too many arguments, max dmx channels is "+num_channels).postln;
} {
vals.do ({|theval, i|
if(theval <= 1.0) {
serial_packet[ch1_idx + i] = (theval * brightness * 255.0).asInteger;
}
});
//("Sending packet "+serial_packet).postln;
port.putAll(serial_packet);
};
}
close {
"Closing serial ports for EnttecDMX...".postln;
SerialPort.closeAll;
this.release;
}
}