Passed
Pull Request — master (#57)
by
unknown
06:03
created

PyDMXControl.controllers._FTD2XXController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 32
dl 0
loc 64
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A FTD2XXController._transmit() 0 12 1
A FTD2XXController._connect() 0 13 3
A FTD2XXController.__init__() 0 13 1
A FTD2XXController._close() 0 3 1
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