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

### Chat GPTで 検索　（　M5stack　とは　言っていません　）

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/378482/f8e0cea5-adbc-de18-841c-76c0baa40fbc.png)


```python

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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/378482/66febe12-c09a-fc09-f023-201d511cbc3e.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/378482/6a4e02d0-ac13-397f-c220-855bc8b55ef2.png)


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/378482/3effcaa7-a7f5-f3d0-84a2-b41b2ffd39f9.png)

```python

# -*- 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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/378482/581a1115-0e07-038d-a2d8-457e8af28c94.png)


```python
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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/378482/07dbb7cc-7335-71d1-dd53-32535f2b0a79.png)


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/378482/9787ac7d-f449-a3f6-ea09-fa99bdf1be68.png)


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/378482/795cd250-d23f-aed9-cc32-63ddc0e0bf92.png)

#### 大きく進歩　現在のUIFlowでの　製作事例に　なってきました
さらに　解説を要求

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/378482/329c6f99-6390-2389-746d-2ec28d364d23.png)

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


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/378482/19c943db-374e-13c0-4af0-f7afc03f3dfa.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/378482/1a250bbe-f835-d4e9-25da-6faaac47f882.png)


```python

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

```

```python
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()

```

```python
# 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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/378482/71106e2f-9c44-d93e-1c25-1ed446590cf7.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/378482/eea8a6f0-bb1b-5e7a-5b63-6c1393f97eaf.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/378482/9144e2c9-344a-4a48-8388-2dc5ae3d1e57.png)

以下　block-maker Code　

```python

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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/378482/fabc989f-b665-115b-f7d3-2b1976fb2704.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/378482/49418c59-1438-4997-8555-d9eb0ad49bf9.png)