0
1

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.

Python pygame Art-net受信

Last updated at Posted at 2022-07-31

Windows10 Art-net モニターが pygameで手軽に作れるようになりました。

RaspberryPi4 Python Art-net 受信 モニター試験 反応早く使えます。

python-Raspberry-mon.png

簡易試験用受信モニタープログラム ( mon16x128.py )

#!/usr/bin/env python
#udp_rx_test.py 2021 1/28ok t.kurume
from socket import *
import pygame as pg
from pygame.compat import geterror
import numpy as np

packet = bytearray()
cou = bytearray()

cd = np.zeros((65,128,3),dtype = pg.Color)
clev = np.arange(0,255,30)

WAIT_BASE=0

dmx = np.zeros((4,512),dtype = np.byte )
packe = np.zeros((2048),dtype = np.byte )

if not pg.font:
    print("Warning, fonts disabled")
if not pg.mixer:
    print("Warning, sound disabled")

#main_dir = os.path.split(os.path.abspath(__file__))[0]
#data_dir = os.path.join(main_dir, "data")

pg.init()
screen = pg.display.set_mode((1300,176))#128/64

def wait(i):
    for k in range(i):
        ccc=0
        for j in range(WAIT_BASE):
            ccc=ccc+1
def sub():
    u=0
    for y in range(20,180,10):  #16 72
        n=0
        for x in range(10,1290,10):      #83:20
            if(cd[u][n][0]>=0)&(cd[u][n][0]>=0)&(cd[u][n][0]>=0):
                pg.draw.circle(screen,(cd[u][n][0],cd[u][n][1],cd[u][n][2]), (x, y),4)
                n=n+1
        if u<15:
            u=u+1
            n=0
        else:
            u=0
            n=0

def col_p(rr,gg,bb):
    for u in range(0,16): 
        for n in range(0,80): 
            cd[u][n][0]= rr
            cd[u][n][1]= gg
            cd[u][n][2]= bb
    
def test1(dl):
        col_p(255,255,255)
        sub()
        pg.display.flip()
        wait(dl)
        col_p(0,0,0)
        sub()
        pg.display.flip()
        wait(dl)
   
## UDP受信クラス
class udprecv():
    def __init__(self):
        # ホスト名を取得、表示
        host = gethostbyname(gethostname())
        #print(host)
        # ipアドレスを取得、表示
        SrcIP  = gethostbyname(gethostbyname(host))
        SrcPort = 6454                                 # 受信元ポート番号
        self.SrcAddr = (SrcIP, SrcPort)                # アドレスをtupleに格納
        self.BUFSIZE = 1024                            # バッファサイズ指定
        self.udpServSock = socket(AF_INET, SOCK_DGRAM) # ソケット作成
        self.udpServSock.bind(self.SrcAddr)            # 受信元アドレスでバインド

    def recv(self):
        packe,addr=self.udpServSock.recvfrom(self.BUFSIZE)
        for u in range(0,16): 
                if (packe[0]==ord('A'))  & (packe[1]==ord('r')):
                    #if (u<16) & ((packe[12]%2)== 0) | (u>15) & ((packe[12]%2)== 1):
                    if (u<16) & ((packe[12]%2)== 0) :
                        if packe[14]==u :
                            for n in range(0,128): 
                                cd[u][n][0]= packe[n*3+18]
                                cd[u][n][1]= packe[n*3+19]
                                cd[u][n][2]= packe[n*3+20]
                            if u==15 :
                                sub()
                                pg.display.flip()
def main():
    # Initialize Everything
    pg.display.set_caption("Art-Net LED(128x16) ")
    # Create The Backgound
    background = pg.Surface(screen.get_size())
    background = background.convert()
    background.fill((50, 50, 50))
    # Put Text On The Background, Centered
    if pg.font:
        font = pg.font.Font(None,10)
        for i in range(0,128): 
            text = font.render(str(i), 1, (0,200,200))
            background.blit(text, (8+i*10,5))
        font = pg.font.Font(None,14)
        for i in range(0,64): 
            text = font.render(str(i), 1, (0,200,200))
            background.blit(text, (1290,15+i*10))

    # Display The Background
    screen.blit(background, (0, 0))
    # Prepare Game Objects
    clock = pg.time.Clock()
    # Main Loop
    going = True
    dim = 0
    pg.display.flip()
    udp = udprecv()     # クラス呼び出し
    while going:
        # Handle Input Events
        for event in pg.event.get():
            if event.type == pg.QUIT:
                going = False
        udp.recv()          # 関数実行        #clock.tick(60)
    pg.quit()
if __name__ == "__main__":
    main()
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?