forked from davepaul0/DmxPy
-
Notifications
You must be signed in to change notification settings - Fork 4
/
DmxPy.py
39 lines (28 loc) · 1.02 KB
/
DmxPy.py
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
import serial, sys, time
DMXOPEN = bytes([126])
DMXCLOSE = bytes([231])
DMXINTENSITY = bytes([6]) + bytes([1]) + bytes([2])
DMXINIT1 = bytes([3]) + bytes([2]) + bytes([0]) + bytes([0]) + bytes([0])
DMXINIT2 = bytes([10]) + bytes([2]) + bytes([0]) + bytes([0]) + bytes([0])
class DmxPy:
def __init__(self, serialPort):
try:
self.serial = serial.Serial(serialPort, baudrate=57600)
except:
print("Error: could not open Serial Port")
sys.exit(0)
self.serial.write(DMXOPEN + DMXINIT1 + DMXCLOSE)
self.serial.write(DMXOPEN + DMXINIT2 + DMXCLOSE)
self.dmxData = [bytes([0])] * 513 #128 plus "spacer".
def setChannel(self, chan, intensity):
if chan > 512: chan = 512
if chan < 0: chan = 0
if intensity > 255: intensity = 255
if intensity < 0: intensity = 0
self.dmxData[chan] = bytes([intensity])
def blackout(self):
for i in range(1, 512, 1):
self.dmxData[i] = bytes([0])
def render(self):
sdata = b''.join(self.dmxData)
self.serial.write(DMXOPEN + DMXINTENSITY + sdata + DMXCLOSE)