From 48f44670d6aca468af41f9d3d3e9ec554c367a6f Mon Sep 17 00:00:00 2001 From: Toby Patterson Date: Sun, 5 Jun 2016 21:51:51 -0400 Subject: [PATCH 1/2] adding support for Python3 --- pysimpledmx/pysimpledmx.py | 24 ++++++++++++++++++------ setup.py | 4 +++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/pysimpledmx/pysimpledmx.py b/pysimpledmx/pysimpledmx.py index c9a18d4..5e6ad6e 100644 --- a/pysimpledmx/pysimpledmx.py +++ b/pysimpledmx/pysimpledmx.py @@ -1,3 +1,4 @@ +from __future__ import print_function import serial, sys START_VAL = 0x7E @@ -19,7 +20,10 @@ class DMXConnection(object): - def __init__(self, comport = None): + + auto_render = False + + def __init__(self, comport = None, autorender=False): ''' On Windows, the only argument is the port number. On *nix, it's the path to the serial device. For example: @@ -28,14 +32,15 @@ def __init__(self, comport = None): DMXConnection("/dev/ttyUSB0") # Linux ''' self.dmx_frame = [0] * DMX_SIZE + self.auto_render = autorender try: self.com = serial.Serial(comport, baudrate = COM_BAUD, timeout = COM_TIMEOUT) except: com_name = 'COM%s' % (comport + 1) if type(comport) == int else comport - print "Could not open device %s. Quitting application." % com_name - sys.exit(0) + print("Could not open device %s. Quitting application." % com_name) + # sys.exit(0) - print "Opened %s." % (self.com.portstr) + print("Opened %s." % (self.com.portstr)) def setChannel(self, chan, val, autorender = False): @@ -44,12 +49,13 @@ def setChannel(self, chan, val, autorender = False): DMX frame, to be rendered the next time the render() method is called. ''' if not 1 <= chan-1 <= DMX_SIZE: - print 'Invalid channel specified: %s' % chan-1 + print('Invalid channel specified: %s' % chan-1) return # clamp value val = max(0, min(val, 255)) self.dmx_frame[chan-1] = val - if autorender: self.render() + if autorender or self.auto_render: + self.render() def clear(self, chan = 0): ''' @@ -76,6 +82,12 @@ def render(self): packet.append(END_VAL) packet = map(chr, packet) + data = ''.join(packet) + + # Python 3, need to encode unicode to bytes + if sys.version_info >= (3, 0): + data = data.encode('ascii') + print(data) self.com.write(''.join(packet)) def close(self): diff --git a/setup.py b/setup.py index 7195bf3..18bba6f 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ setup( name="pysimpledmx", - version="0.1.0", + version="0.2.0", description="simple dmx control for the Enttec DMX USB Pro", license="GPLV3", author="c0z3n", @@ -18,6 +18,8 @@ "Programming Language :: Python", 'Programming Language :: Python :: 2', "Programming Language :: Python :: 2.7", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.5", "Intended Audience :: Other Audience", "Topic :: Artistic Software", ] From 7e6d3efba810e40b84e8831333dd98a886696f11 Mon Sep 17 00:00:00 2001 From: Toby Patterson Date: Thu, 9 Jun 2016 20:44:18 -0400 Subject: [PATCH 2/2] changing usage of chr() to bytearray() --- pysimpledmx/pysimpledmx.py | 164 +++++++++++++++++-------------------- 1 file changed, 77 insertions(+), 87 deletions(-) diff --git a/pysimpledmx/pysimpledmx.py b/pysimpledmx/pysimpledmx.py index 5e6ad6e..3191c49 100644 --- a/pysimpledmx/pysimpledmx.py +++ b/pysimpledmx/pysimpledmx.py @@ -1,94 +1,84 @@ from __future__ import print_function import serial, sys +import logging -START_VAL = 0x7E -END_VAL = 0xE7 +START_VAL = 0x7E +END_VAL = 0xE7 -COM_BAUD = 57600 +COM_BAUD = 57600 COM_TIMEOUT = 1 -COM_PORT = 7 -DMX_SIZE = 512 +COM_PORT = 7 +DMX_SIZE = 512 LABELS = { - 'GET_WIDGET_PARAMETERS' :3, #unused - 'SET_WIDGET_PARAMETERS' :4, #unused - 'RX_DMX_PACKET' :5, #unused - 'TX_DMX_PACKET' :6, - 'TX_RDM_PACKET_REQUEST' :7, #unused - 'RX_DMX_ON_CHANGE' :8, #unused - } - - -class DMXConnection(object): - - auto_render = False - - def __init__(self, comport = None, autorender=False): - ''' - On Windows, the only argument is the port number. On *nix, it's the path to the serial device. - For example: - DMXConnection(4) # Windows - DMXConnection('/dev/tty2') # Linux - DMXConnection("/dev/ttyUSB0") # Linux - ''' - self.dmx_frame = [0] * DMX_SIZE - self.auto_render = autorender - try: - self.com = serial.Serial(comport, baudrate = COM_BAUD, timeout = COM_TIMEOUT) - except: - com_name = 'COM%s' % (comport + 1) if type(comport) == int else comport - print("Could not open device %s. Quitting application." % com_name) - # sys.exit(0) - - print("Opened %s." % (self.com.portstr)) - - - def setChannel(self, chan, val, autorender = False): - ''' - Takes channel and value arguments to set a channel level in the local - DMX frame, to be rendered the next time the render() method is called. - ''' - if not 1 <= chan-1 <= DMX_SIZE: - print('Invalid channel specified: %s' % chan-1) - return - # clamp value - val = max(0, min(val, 255)) - self.dmx_frame[chan-1] = val - if autorender or self.auto_render: - self.render() - - def clear(self, chan = 0): - ''' - Clears all channels to zero. blackout. - With optional channel argument, clears only one channel. - ''' - if chan == 0: - self.dmx_frame = [0] * DMX_SIZE - else: - self.dmx_frame[chan-1] = 0 - - - def render(self): - '''' - Updates the DMX output from the USB DMX Pro with the values from self.dmx_frame. - ''' - packet = [ - START_VAL, - LABELS['TX_DMX_PACKET'], - len(self.dmx_frame) & 0xFF, - (len(self.dmx_frame) >> 8) & 0xFF, - ] - packet += self.dmx_frame - packet.append(END_VAL) - - packet = map(chr, packet) - data = ''.join(packet) - - # Python 3, need to encode unicode to bytes - if sys.version_info >= (3, 0): - data = data.encode('ascii') - print(data) - self.com.write(''.join(packet)) - - def close(self): - self.com.close() + 'GET_WIDGET_PARAMETERS': 3, # unused + 'SET_WIDGET_PARAMETERS': 4, # unused + 'RX_DMX_PACKET': 5, # unused + 'TX_DMX_PACKET': 6, 'TX_RDM_PACKET_REQUEST': 7, # unused + 'RX_DMX_ON_CHANGE': 8, # unused +} + + +class DMXConnection( object ): + auto_render = False + + def __init__( self, comport=None, autorender=False ): + ''' + On Windows, the only argument is the port number. On *nix, it's the path to the serial device. + For example: + DMXConnection(4) # Windows + DMXConnection('/dev/tty2') # Linux + DMXConnection("/dev/ttyUSB0") # Linux + ''' + self.dmx_frame = [ 0 ] * DMX_SIZE + self.auto_render = autorender + try: + self.com = serial.Serial( comport, baudrate=COM_BAUD, timeout=COM_TIMEOUT ) + except: + com_name = 'COM%s' % (comport + 1) if type( comport ) == int else comport + logging.error( "Could not open device %s. Quitting application." % com_name ) + # sys.exit(0) + + logging.info( "Opened %s." % (self.com.portstr) ) + + def setChannel( self, chan, val, autorender=False ): + ''' + Takes channel and value arguments to set a channel level in the local + DMX frame, to be rendered the next time the render() method is called. + ''' + if not 1 <= chan - 1 <= DMX_SIZE: + logging.error( 'Invalid channel specified: %s' % chan - 1 ) + return + # clamp value + val = max( 0, min( val, 255 ) ) + self.dmx_frame[ chan - 1 ] = val + if autorender or self.auto_render: + self.render( ) + + def clear( self, chan=0 ): + ''' + Clears all channels to zero. blackout. + With optional channel argument, clears only one channel. + ''' + if chan == 0: + self.dmx_frame = [ 0 ] * DMX_SIZE + else: + self.dmx_frame[ chan - 1 ] = 0 + + def render( self ): + '''' + Updates the DMX output from the USB DMX Pro with the values from self.dmx_frame. + ''' + packet = [ START_VAL, LABELS[ 'TX_DMX_PACKET' ], len( self.dmx_frame ) & 0xFF, + (len( self.dmx_frame ) >> 8) & 0xFF, ] + packet += self.dmx_frame + packet.append( END_VAL ) + + data = bytearray(packet) + + logging.debug( packet ) + logging.debug( data ) + self.com.write( data ) + + def close( self ): + self.com.close( )