DMX512 单元

DMX Unit 是一款专为 DMX-512 数据传输场景设计的通信单元。它集成了 CA-IS3092W 隔离型半双工 RS-485 收发器,提供高达 5 kVrms 的电气隔离保护。板载 120Ω 终端电阻开关可匹配信号传输线的特性阻抗,防止信号反射与失真,并可根据使用场景进行连接。

支持以下产品:

DMX

MicroPython 应用示例 发送数据:

 1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from unit import DMX512Unit
 9import time
10
11
12Title = None
13label1 = None
14label0 = None
15dmx_0 = None
16
17
18ch_data = None
19
20
21def setup():
22    global Title, label1, label0, dmx_0, ch_data
23
24    M5.begin()
25    Widgets.fillScreen(0x222222)
26    Title = Widgets.Title("DMX512Unit Send example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18)
27    label1 = Widgets.Label("Not Sent", 2, 129, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
28    label0 = Widgets.Label(
29        "Not initialized", 2, 76, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18
30    )
31
32    dmx_0 = DMX512Unit(1, port=(13, 14), mode=1)
33    label0.setText(str("Initialized"))
34    ch_data = 0
35
36
37def loop():
38    global Title, label1, label0, dmx_0, ch_data
39    M5.update()
40    label1.setText(str("Not Sent"))
41    dmx_0.write_data(1, ch_data)
42    dmx_0.write_data(2, ch_data)
43    dmx_0.write_data(3, ch_data)
44    ch_data = (ch_data if isinstance(ch_data, (int, float)) else 0) + 1
45    if ch_data >= 255:
46        ch_data = 0
47    label1.setText(str("Sent"))
48    time.sleep(0.7)
49
50
51if __name__ == "__main__":
52    try:
53        setup()
54        while True:
55            loop()
56    except (Exception, KeyboardInterrupt) as e:
57        try:
58            from utility import print_error_msg
59
60            print_error_msg(e)
61        except ImportError:
62            print("please update to latest firmware")

MicroPython 应用示例 接收数据:

 1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from unit import DMX512Unit
 9
10
11Title = None
12label0 = None
13label1 = None
14dmx_0 = None
15
16
17dmx_data = None
18dmx_data2 = None
19
20
21def dmx_0_channel1_receive_event(received_data):
22    global Title, label0, label1, dmx_0, dmx_data, dmx_data2
23    dmx_data = received_data
24    label0.setText(str((str("Channel 1:") + str(dmx_data))))
25
26
27def dmx_0_channel2_receive_event(received_data):
28    global Title, label0, label1, dmx_0, dmx_data, dmx_data2
29    dmx_data2 = received_data
30    label1.setText(str((str("Channel 2:") + str(dmx_data2))))
31
32
33def setup():
34    global Title, label0, label1, dmx_0, dmx_data, dmx_data2
35
36    M5.begin()
37    Widgets.fillScreen(0x222222)
38    Title = Widgets.Title("DMX512Unit Rec example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18)
39    label0 = Widgets.Label("Channel 1:", 0, 57, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
40    label1 = Widgets.Label("Channel 2:", 2, 109, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
41
42    dmx_0 = DMX512Unit(1, port=(13, 14), mode=2)
43    dmx_0.attach_channel(1, dmx_0_channel1_receive_event)
44    dmx_0.attach_channel(2, dmx_0_channel2_receive_event)
45    dmx_0.receive_none_block()
46
47
48def loop():
49    global Title, label0, label1, dmx_0, dmx_data, dmx_data2
50    M5.update()
51
52
53if __name__ == "__main__":
54    try:
55        setup()
56        while True:
57            loop()
58    except (Exception, KeyboardInterrupt) as e:
59        try:
60            from utility import print_error_msg
61
62            print_error_msg(e)
63        except ImportError:
64            print("please update to latest firmware")

UiFlow2 Master 应用示例:

tx_example.png

UIFLOW2 从机示例:

rx_example.png

dmx512_core2_send_example.m5f2

dmx512_core2_receive_example.m5f2

class DMX512Unit

Constructors

class DMX512Unit(id, port, mode=DMX_MASTER)

使用指定的 UART ID 和端口引脚初始化 DMX512 单元。

参数:
  • id (Literal[0,1,2]) – UART 设备 ID(DMX 端口 ID)。

  • port (list|tuple) – UART TX 和 RX 引脚。

  • mode (int) – 工作模式(1 表示 Master,2 表示 Slave)。

UIFLOW2:

init.png

Methods

DMX512Unit.dmx_init(mode) None

使用 UART 引脚和模式初始化 DMX512 通信。

参数:

mode – 工作模式(1 表示 Master,2 表示 Slave)。

UIFLOW2:

dmx_init.png

DMX512Unit.deinit() None

反初始化 DMX512 单元,并停止任何正在进行的操作。

UIFLOW2:

deinit.png

DMX512Unit.write_data(channel, data) None

更新指定 DMX 通道的数据。数据将在下一次更新周期发送。

参数:
  • channel – DMX 通道编号(1-512)。

  • data – 要发送的数据值(0-255)。如果通道号超出范围,则会引发 ValueError。

UIFLOW2:

write_data.png

DMX512Unit.clear_buffer() None

清除 DMX 缓冲区并重置数据。

UIFLOW2:

clear_buffer.png

DMX512Unit.read_data(channel) int

在从机模式下从指定的 DMX 通道读取数据。

参数:

channel – DMX 通道编号(1-512)。

UIFLOW2:

read_data.png

DMX512Unit.receive_none_block() None

为指定通道启动非阻塞数据接收,并关联相应的回调函数。

UIFLOW2:

receive_none_block.png

DMX512Unit.attach_channel(channel, callback) None

为指定的 DMX 通道绑定一个回调函数。

参数:
  • channel – 要将回调函数绑定到的 DMX 通道号(1-512)。

  • callback – 当指定通道上的数据发生变化时要调用的函数。

UIFLOW2:

receive_data_event.png

DMX512Unit.stop_receive() None

停止非阻塞数据接收任务。

UIFLOW2:

stop_receive.png

DMX512Unit.detach_channel(channel) None

从指定的 DMX 通道解绑回调函数。

参数:

channel – 要从中解绑回调函数的 DMX 通道号(1-512)。