Lab_interaccio/2022/Particle_mini/test.py

103 lines
4.8 KiB
Python
Raw Normal View History

2025-02-25 21:29:42 +01:00
import socket
import time
import math
localIP = "192.168.2.147"
#localIP = "192.168.1.138"
localPort = 6454
bufferSize = 530
CAP_MINI_01 =b'\x42\x4D\x36\xAC\x00\x00\x00\x00\x00\x00\x36\x00\x00\x00\x28\x00\x00\x00\x39\x00\x00\x00\x00\x01\x00\x00\x01\x00\x18\x00\x00\x00\x00\x00\x00\xAC\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
mode = 1
PARTS = 256
MAX_STRIPS_GROUP = 256
LEDS_TOTAL_1 = 57
LEDS_TOTAL_2 = 43
LEDS_TOTAL_3 = 29
LEDS_TOTAL_4 = 15
RANGE_1 = math.ceil(LEDS_TOTAL_1*3*PARTS/510)
RANGE_2 = math.ceil(LEDS_TOTAL_2*3*PARTS/510)
RANGE_3 = math.ceil(LEDS_TOTAL_3*3*PARTS/510)
RANGE_4 = math.ceil(LEDS_TOTAL_4*3*PARTS/510)
INI_UNIVERSE_1 = 0
INI_UNIVERSE_2 = RANGE_1
INI_UNIVERSE_3 = RANGE_1 + RANGE_2
INI_UNIVERSE_4 = RANGE_1 + RANGE_2 + RANGE_3
END_UNIVERSE = INI_UNIVERSE_4 + RANGE_4
ART_DMX = 0X5000
ART_NET_ID = "Art-Net"
ART_DMX_START = 18
P01_numbers = [ [0 for i in range(530)] for j in range(RANGE_1) ]
P02_numbers = [ [0 for i in range(530)] for j in range(RANGE_2) ]
P03_numbers = [ [0 for i in range(530)] for j in range(RANGE_3) ]
P04_numbers = [ [0 for i in range(530)] for j in range(RANGE_4) ]
#mini01 = ("192.168.1.139", 6454)
mini01 = ("192.168.2.10", 6454)
mini02 = ("192.168.2.11", 6454)
mini03 = ("192.168.2.12", 6454)
mini04 = ("192.168.2.13", 6454)
##msgFromServer = "Hello UDP Client"
##bytesToSend = str.encode(msgFromServer)
# Create a datagram socket
UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
# Bind to address and ip
UDPServerSocket.bind((localIP, localPort))
print("UDP server up and listening")
# Listen for incoming datagrams
while(True):
bytesAddressPair = UDPServerSocket.recvfrom(bufferSize)
message = bytesAddressPair[0]
address = bytesAddressPair[1]
clientMsg = "Message from Client:{}".format(message)
msg = "{}".format(message)
result = msg.find(ART_NET_ID)
if (result>=0):
opcode = message[8] | message[9] << 8
if (opcode == ART_DMX):
incomingUniverse = message[14] | message[15] << 8
dmxDataLength = message[17] | message[16] << 8
#print("{f} found with universe: {u}, DataLength: {n}.".format(f=ART_NET_ID, u= incomingUniverse, n=dmxDataLength))
if mode==0:
for i in range(dmxDataLength):
if message[ART_DMX_START+i]!=P01_numbers[incomingUniverse][i]:
P01_numbers[incomingUniverse] = message[ART_DMX_START:(dmxDataLength+ART_DMX_START)] ##Copia los 512 valores del ARRAY
UDPServerSocket.sendto(message, mini01)
#print("Sended")
break
elif mode==1:
if incomingUniverse<INI_UNIVERSE_2:P01_numbers[incomingUniverse] = message[:] ##Copia todos los valores del ARRAY
elif incomingUniverse<(INI_UNIVERSE_3):P02_numbers[incomingUniverse-INI_UNIVERSE_2] = message[:] ##Copia todos los valores del ARRAY
elif incomingUniverse<(INI_UNIVERSE_4):P03_numbers[incomingUniverse-(INI_UNIVERSE_3)] = message[:] ##Copia todos los valores del ARRAY
elif incomingUniverse<(END_UNIVERSE):P04_numbers[incomingUniverse-(INI_UNIVERSE_4)] = message[:] ##Copia todos los valores del ARRAY
if incomingUniverse==(END_UNIVERSE-1):
#if incomingUniverse==(INI_UNIVERSE_2-1):
#with open('D:\mini01.bmp', 'wb') as fp:
#in_x = 0
#fp.write(CAP_MINI_01)
#for h in range(RANGE_1):
##fp.write(P01_numbers[h][18:528])
#for j in range(ART_DMX_START,528):
#in_x=in_x+1
#fp.write(P01_numbers[h][j:j+1])
#if in_x==171:
#fp.write(b'\x00')
#in_x = 0
#for h in range(RANGE_1):
#if h%3==0: fp.write(b'\x00')
for h in range(RANGE_1):
UDPServerSocket.sendto(P01_numbers[h], mini01)
if h<(RANGE_2-1):
UDPServerSocket.sendto(P02_numbers[h], mini02)
if h<(RANGE_3-1):
UDPServerSocket.sendto(P03_numbers[h], mini03)
if h<(RANGE_4-1):
UDPServerSocket.sendto(P04_numbers[h], mini04)
if h%6==0:
time.sleep(0.006) # Sleep for 3 seconds
time.sleep(0.006) # Sleep for 3 seconds