|
| 1 | +from enum import Enum |
| 2 | + |
| 3 | + |
| 4 | +# Message Labels <= 12 correspond to the v1 API. > 12 correspond to v2 API |
| 5 | +from typing import Optional |
| 6 | + |
| 7 | +import serial |
| 8 | + |
| 9 | +from DMXEnttecPro.utils import ( |
| 10 | + least_significant_bit_for_size, |
| 11 | + most_significant_bit_for_size, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +class Mk2MessageLabels(int, Enum): |
| 16 | + GET_PORT_WIDGET_PARAMETERS_REQUEST_REPLY_PORT1 = 3 |
| 17 | + GET_PORT_WIDGET_PARAMETERS_REQUEST_REPLY_PORT2 = 196 |
| 18 | + SET_PORT_WIDGET_PARAMETERS_REQUEST_PORT1 = 4 |
| 19 | + SET_PORT_WIDGET_PARAMETERS_REQUEST_PORT2 = 156 |
| 20 | + RECEIVED_DMX_PACKET_PORT1 = 5 |
| 21 | + RECEIVED_DMX_PACKET_PORT2 = 210 |
| 22 | + OUTPUT_ONLY_SEND_DMX_PACKET_REQUEST_PORT1 = 6 |
| 23 | + OUTPUT_ONLY_SEND_DMX_PACKET_REQUEST_PORT2 = 132 |
| 24 | + SEND_RDM_PACKET_REQUEST_PORT1 = 7 |
| 25 | + SEND_RDM_PACKET_REQUEST_PORT2 = 226 |
| 26 | + RECEIVE_DMX_ON_CHANGE_PORT1 = 8 |
| 27 | + RECEIVE_DMX_ON_CHANGE_PORT2 = 128 |
| 28 | + RECEIVED_DMX_CHANGE_OF_STATE_PACKET_PORT1 = 9 |
| 29 | + RECEIVED_DMX_CHANGE_OF_STATE_PACKET_PORT2 = 22 |
| 30 | + GET_WIDGET_SERIAL_NUMBER_REQUEST_REPLY = 10 |
| 31 | + SEND_RDM_DISCOVERY_REQUEST_PORT1 = 11 |
| 32 | + SEND_RDM_DISCOVERY_REQUEST_PORT2 = 208 |
| 33 | + RDM_CONTROLLER_RECEIVE_TIMEOUT_PORT1 = 12 |
| 34 | + RDM_CONTROLLER_RECEIVE_TIMEOUT_PORT2 = 209 |
| 35 | + SET_API_KEY_REQUEST = 13 |
| 36 | + QUERY_HARDWARE_VERSION_REQUEST_REPLY = 14 |
| 37 | + GET_PORT_ASSIGNMENT_REQUEST_REPLY = 220 |
| 38 | + SET_PORT_ASSIGNMENT_REQUEST = 201 |
| 39 | + RECEIVED_MIDI = 225 |
| 40 | + SEND_MIDI_REQUEST = 191 |
| 41 | + SHOW_QUERY_REQUEST_REPLY = 139 |
| 42 | + SHOW_BLOCK_ERASE_REQUEST = 129 # Distinguished by subcommand keyword "ERAS" |
| 43 | + SHOW_SECTOR_ERASE_REQUEST = 129 # Distinguished by subcommand keyword "ERSE" |
| 44 | + SHOW_WRITE_REQUEST = 129 # Distinguished by subcommand keyword "WRIT" |
| 45 | + SHOW_READ_REQUEST_REPLY = 203 |
| 46 | + START_SHOW_REQUEST = 129 # Distinguished by subcommand keyword "STAR" |
| 47 | + STOP_SHOW_REQUEST = 129 # Distinguished by subcommand keyword "STOP" |
| 48 | + |
| 49 | + |
| 50 | +class Mk2Controller(object): |
| 51 | + def __init__( |
| 52 | + self, |
| 53 | + port_string: str, |
| 54 | + dmx_size: int = 512, |
| 55 | + baudrate: int = 57600, |
| 56 | + timeout: int = 1, |
| 57 | + auto_submit: bool = False, |
| 58 | + ): |
| 59 | + if not (24 <= dmx_size <= 512): |
| 60 | + raise ValueError("Size of DMX channel frame must be between 24 and 512") |
| 61 | + self.dmx_size = dmx_size |
| 62 | + self.baudrate = baudrate |
| 63 | + self.timeout = timeout |
| 64 | + self.auto_submit = auto_submit |
| 65 | + |
| 66 | + self._conn = serial.Serial( |
| 67 | + port_string, baudrate=self.baudrate, timeout=self.timeout |
| 68 | + ) |
| 69 | + |
| 70 | + self.channels = bytearray(self.dmx_size) |
| 71 | + self._last_submitted_channels = bytearray(self.dmx_size) |
| 72 | + self._signal_start = bytearray([0x7E]) |
| 73 | + self._signal_end = bytearray([0xE7]) |
| 74 | + |
| 75 | + def set_port_widget_parameters( |
| 76 | + self, |
| 77 | + *, |
| 78 | + port: int = 1, |
| 79 | + break_time: int = 9, |
| 80 | + mab_time: int = 1, |
| 81 | + rate: int = 40, |
| 82 | + user_defined_bytes: Optional[bytearray] = None, |
| 83 | + ): |
| 84 | + if user_defined_bytes is None: |
| 85 | + user_defined_bytes = bytearray() |
| 86 | + else: |
| 87 | + if len(user_defined_bytes) > 512: |
| 88 | + raise ValueError( |
| 89 | + "Length of user_defined_bytes must not be greater than 512" |
| 90 | + ) |
| 91 | + if port not in (1, 2): |
| 92 | + raise ValueError("port must be 1 or 2") |
| 93 | + if not (9 <= break_time <= 127): |
| 94 | + raise ValueError("output_break_time must be between 9 and 127") |
| 95 | + if not (1 <= mab_time <= 127): |
| 96 | + raise ValueError("mab_time must be between 1 and 127") |
| 97 | + if not (0 <= rate <= 40): |
| 98 | + raise ValueError("output_rate must be between 0 and 40") |
| 99 | + label = Mk2MessageLabels.SET_PORT_WIDGET_PARAMETERS_REQUEST_PORT1 |
| 100 | + if port == 2: |
| 101 | + label = Mk2MessageLabels.SET_PORT_WIDGET_PARAMETERS_REQUEST_PORT2 |
| 102 | + udb_len = len(user_defined_bytes) |
| 103 | + msg = ( |
| 104 | + self._signal_start |
| 105 | + + bytearray( |
| 106 | + [ |
| 107 | + label.value, |
| 108 | + least_significant_bit_for_size(udb_len + 5), |
| 109 | + most_significant_bit_for_size(udb_len + 5), |
| 110 | + least_significant_bit_for_size(udb_len), |
| 111 | + most_significant_bit_for_size(udb_len), |
| 112 | + break_time, |
| 113 | + mab_time, |
| 114 | + rate, |
| 115 | + ] |
| 116 | + ) |
| 117 | + + user_defined_bytes |
| 118 | + + self._signal_end |
| 119 | + ) |
| 120 | + self._conn.write(msg) |
0 commit comments