1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Chat GPTで UIFlow Art-Net送出プログラム

Last updated at Posted at 2023-03-18

Chat GPTで 検索 ( M5stack とは 言っていません )

image.png


from m5stack import *
from m5ui import *
from uiflow import *
import wifiCfg
import network
import socket

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('wifi_ssid', 'wifi_password')
while not wlan.isconnected():
    pass

def sendData(ip_address='192.168.1.10', port=6454, dmx_data=[255, 128, 0, 64]):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    packet = bytearray([65, 114, 116, 45, 78, 101, 116, 0, 0, 80, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 


image.png

image.png

image.png


# -*- coding: utf-8 -*-

import network
import socket
import struct
import time

from micropython import const
from ustruct import unpack_from
from custom_module import CustomModule # カスタムブロックを使用するためのimport文

from artnet import Packet, DmxPacket, ArtNetTypeCode
from artnet.nodes import Node


ARTNET_PORT = const(6454)
ARTNET_UNIVERSE_SIZE = const(512)
ARTNET_DMX_HEADER_SIZE = const(18)
ARTNET_MAX_PACKET_SIZE = ARTNET_DMX_HEADER_SIZE + ARTNET_UNIVERSE_SIZE


# カスタムブロックの定義
class ArtNetSender(CustomModule):
    def __init__(self):
        super().__init__()
        self._node = Node('Art-Net Sender')
        self._node.start()
        self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)

    def _send_packet(self, packet):
        buffer = bytearray(packet.pack())
        self._socket.sendto(buffer, ('<broadcast>', ARTNET_PORT))

    def send_dmx(self, universe, data):
        packet = Packet(type_code=ArtNetTypeCode.Dmx)
        dmx = DmxPacket(universe=universe, data=data)
        packet.dmx = dmx
        self._send_packet(packet)


# カスタムブロックの作成
artnet_sender = ArtNetSender()


# メインループ
while True:
    # 例として、ユニバース0にRGBの色を送信する
    data = bytearray(ARTNET_UNIVERSE_SIZE)
    data[0] = 255  # R
    data[1] = 0    # G
    data[2] = 0    # B
    artnet_sender.send_dmx(0, data)
    time.sleep(1)  # 1秒間待機

解りにくいので GPT-4で 再検索 

image.png

import socket
import struct

# Art-Netの設定
ARTNET_HEADER = b'Art-Net\x00'
OP_OUTPUT = 0x5000
PROTOCOL_VERSION = 14
SEQUENCE = 0
PHYSICAL = 0
SUBNET = 0
UNIVERSE = 0
CHANNEL_COUNT = 512

# UDPの設定
UDP_IP = "xxx.xxx.xxx.xxx"  # Art-Net対応デバイスのIPアドレス
UDP_PORT = 6454

def create_artnet_packet(data):
    global SEQUENCE
    artnet_data = bytearray()
    artnet_data.extend(ARTNET_HEADER)
    artnet_data.extend(struct.pack(">H", OP_OUTPUT))
    artnet_data.extend(struct.pack(">H", PROTOCOL_VERSION))
    artnet_data.append(SEQUENCE)
    artnet_data.append(PHYSICAL)
    artnet_data.extend(struct.pack(">H", (SUBNET << 4) | UNIVERSE))
    artnet_data.extend(struct.pack(">H", CHANNEL_COUNT + 1))
    artnet_data.append(0)  # Start Code (DMX512)
    artnet_data.extend(data)
    SEQUENCE = (SEQUENCE + 1) % 256
    return artnet_data

def send_artnet_packet(data):
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.sendto(create_artnet_packet(data), (UDP_IP, UDP_PORT))
    sock.close()

# テストデータを送信
dmx_data = bytearray([0] * CHANNEL_COUNT)
dmx_data[0] = 255  # DMXチャンネル1をフル出力に
send_artnet_packet(dmx_data)

image.png

image.png

image.png

大きく進歩 現在のUIFlowでの 製作事例に なってきました

さらに 解説を要求

image.png

今まで 自分なりに理解している事柄が整理され表示され再確認できますね 素晴らしいです

image.png

image.png


import struct

def create_artnet_packet(sequence, universe, subnet, dmx_data):
    header = b"Art-Net\x00"  # Art-Net header
    opcode = 0x5000  # Art-Net OpCode for OP_OUTPUT / DMX data
    protocol_version = 14  # Art-Net protocol version
    
    # Art-Net header, OpCode, and protocol version
    packet = struct.pack(">7sBHHBBH", header, opcode, protocol_version, sequence, 0, ((subnet << 4) | universe), len(dmx_data))
    
    # Append DMX data to the packet
    packet += dmx_data

    return packet

import socket

def send_artnet_packet(ip, port, packet):
    # Create a UDP socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    # Send the Art-Net packet to the specified IP address and port
    sock.sendto(packet, (ip, port))

    # Close the socket
    sock.close()

# Create a DMX data packet (512 channels) with all channels set to 0, except channel 1 set to 255
dmx_data = bytearray([0] * 512)
dmx_data[0] = 255

# Create the Art-Net packet
artnet_packet = create_artnet_packet(sequence=1, universe=0, subnet=0, dmx_data=dmx_data)

# Send the Art-Net packet to the specified IP address and port
send_artnet_packet(ip="192.168.1.100", port=6454, packet=artnet_packet)

 GPTのサンプルソースそのままでは動かず修正 

image.png

image.png

image.png

以下 block-maker Code 


import socket
import struct
import time

# Art-Netの設定
ARTNET_HEADER = b'Art-Net\x00'
OP_OUTPUT = 0x0050
PROTOCOL_VERSION = 14
SEQUENCE = 0
PHYSICAL = 0
SUBNET = 0
UNIVERSE = 0
CHANNEL_COUNT = 512

# UDPの設定
UDP_IP = "192.168.0.255"  # Art-Net対応デバイスのIPアドレス
UDP_PORT = 6454

def create_artnet_packet(data):
    global SEQUENCE
    artnet_data = bytearray()
    artnet_data.extend(ARTNET_HEADER)
    artnet_data.extend(struct.pack(">H", OP_OUTPUT))
    artnet_data.extend(struct.pack(">H", PROTOCOL_VERSION))
    artnet_data.append(SEQUENCE)
    artnet_data.append(PHYSICAL)
    artnet_data.extend(struct.pack(">H", (SUBNET << 4) | UNIVERSE))
    artnet_data.extend(struct.pack(">H", CHANNEL_COUNT + 0))
    #artnet_data.append(0)  # Start Code (DMX512)
    artnet_data.extend(data)
    SEQUENCE = (SEQUENCE + 1) % 256
    return artnet_data

def send_artnet_packet(data):
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.sendto(create_artnet_packet(data), (UDP_IP, UDP_PORT))
    sock.close()

# テストデータを送信
dmx_data = bytearray([0] * CHANNEL_COUNT)
for d in range(255):
    for i in range(511):
        dmx_data[i] = d
    send_artnet_packet(dmx_data)
    time.sleep(0.1)  # 0.1秒間隔で表示(必要に応じて調整)
for d in range(255, -1, -1):
    for i in range(511):
        dmx_data[i] = d
    send_artnet_packet(dmx_data)
    time.sleep(0.1)  # 0.1秒間隔で表示(必要に応じて調整)

最後に今回GPTを使った UIFlowでのプログラム製作を、お試しで行いましたが。

GPT4でプログラム製作を進めるのはかなり効率的だと感じました。
プログラム製作に役立てたいと思います。

UDP Art-Netの 確認で Wiresharkで このように受信出来れば 正解です。

image.png

image.png

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?