diff --git a/PyDMXControl/controllers/_FTD2XXController.py b/PyDMXControl/controllers/_FTD2XXController.py new file mode 100644 index 0000000..588d9ad --- /dev/null +++ b/PyDMXControl/controllers/_FTD2XXController.py @@ -0,0 +1,63 @@ +""" + * PyDMXControl: A Python 3 module to control DMX using OpenDMX or uDMX. + * Featuring fixture profiles, built-in effects and a web control panel. + * + * Copyright (C) 2022 Matt Cowley (MattIPv4) (me@mattcowley.co.uk) +""" + +from time import sleep +from typing import List + +from ._TransmittingController import TransmittingController + +try: + import ftd2xx +except OSError as e: + print("ftd2xx binaries not present") + +class FTD2XXController(TransmittingController): + + def __init__(self, device_number, *args, **kwargs): + """ + FTD2CXX interface requires a device number to establish connection, e.g. 0 + + Parameters + ---------- + device_number: int + """ + + # Store the device_number and device + self.__device_number = device_number + self.__device = None + super().__init__(*args, **kwargs) + + def _connect(self): + # Try to close if exists + if self.__device is not None: + try: + self.__device.close() + except Exception: + pass + + # Get new device + self.__device = ftd2xx.open(self.__device_number) + self.__device.resetDevice() + self.__device.setBaudRate(250000) + self.__device.setDataCharacteristics(8, 2, 0) # 8 bit word, 2 stop bit, no parity + + def _close(self): + self.__device.close() + print("CLOSE: FTD2XX device connection closed") + + def _transmit(self, frame: List[int], first: int): + # Convert to a bytearray and pad the start of the frame + # We're transmitting direct DMX data here, so a frame must start at channel 1, but can end early + data = bytearray(([0] * (first - 1)) + frame) + + # The first byte in the type, and is `0` for normal DMX data + data.insert(0, 0) + + # Write + self.__device.setBreakOn() + self.__device.setBreakOff() + self.__device.write(bytes(data)) diff --git a/PyDMXControl/controllers/__init__.py b/PyDMXControl/controllers/__init__.py index 9c4a312..50884a1 100644 --- a/PyDMXControl/controllers/__init__.py +++ b/PyDMXControl/controllers/__init__.py @@ -23,3 +23,4 @@ from ._uDMXController import uDMXController from ._OpenDMXController import OpenDMXController from ._SerialController import SerialController +from ._FTD2XXController import FTD2XXController diff --git a/requirements.txt b/requirements.txt index 9eeab4f..7f97355 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,4 +9,7 @@ pyftdi>=0.54.0,<=0.55.0 pyserial>=3.5,<4.0 # Web -Flask>=2.3,<3.0 \ No newline at end of file +Flask>=2.3,<3.0 + +#FTD2XX +ftd2xx>=1.3.6 \ No newline at end of file