1
|
|
|
""" |
2
|
|
|
* PyDMXControl: A Python 3 module to control DMX using OpenDMX or uDMX. |
3
|
|
|
* Featuring fixture profiles, built-in effects and a web control panel. |
4
|
|
|
* <https://github.com/MattIPv4/PyDMXControl/> |
5
|
|
|
* Copyright (C) 2022 Matt Cowley (MattIPv4) ([email protected]) |
6
|
|
|
""" |
7
|
|
|
|
8
|
|
|
from time import sleep |
9
|
|
|
from typing import List |
10
|
|
|
|
11
|
|
|
from ._TransmittingController import TransmittingController |
12
|
|
|
|
13
|
|
|
try: |
14
|
|
|
import ftd2xx |
15
|
|
|
except OSError as e: |
16
|
|
|
print("ftd2xx binaries not present") |
17
|
|
|
|
18
|
|
|
class FTD2XXController(TransmittingController): |
19
|
|
|
|
20
|
|
|
def __init__(self, device_number, *args, **kwargs): |
21
|
|
|
""" |
22
|
|
|
FTD2CXX interface requires a device number to establish connection, e.g. 0 |
23
|
|
|
|
24
|
|
|
Parameters |
25
|
|
|
---------- |
26
|
|
|
device_number: int |
27
|
|
|
""" |
28
|
|
|
|
29
|
|
|
# Store the device_number and device |
30
|
|
|
self.__device_number = device_number |
31
|
|
|
self.__device = None |
32
|
|
|
super().__init__(*args, **kwargs) |
33
|
|
|
|
34
|
|
|
def _connect(self): |
35
|
|
|
# Try to close if exists |
36
|
|
|
if self.__device is not None: |
37
|
|
|
try: |
38
|
|
|
self.__device.close() |
39
|
|
|
except Exception: |
40
|
|
|
pass |
41
|
|
|
|
42
|
|
|
# Get new device |
43
|
|
|
self.__device = ftd2xx.open(self.__device_number) |
44
|
|
|
self.__device.resetDevice() |
45
|
|
|
self.__device.setBaudRate(250000) |
46
|
|
|
self.__device.setDataCharacteristics(8, 2, 0) # 8 bit word, 2 stop bit, no parity |
47
|
|
|
|
48
|
|
|
def _close(self): |
49
|
|
|
self.__device.close() |
50
|
|
|
print("CLOSE: FTD2XX device connection closed") |
51
|
|
|
|
52
|
|
|
def _transmit(self, frame: List[int], first: int): |
53
|
|
|
# Convert to a bytearray and pad the start of the frame |
54
|
|
|
# We're transmitting direct DMX data here, so a frame must start at channel 1, but can end early |
55
|
|
|
data = bytearray(([0] * (first - 1)) + frame) |
56
|
|
|
|
57
|
|
|
# The first byte in the type, and is `0` for normal DMX data |
58
|
|
|
data.insert(0, 0) |
59
|
|
|
|
60
|
|
|
# Write |
61
|
|
|
self.__device.setBreakOn() |
62
|
|
|
self.__device.setBreakOff() |
63
|
|
|
self.__device.write(bytes(data)) |
64
|
|
|
|