LJ/plugins/pysimu.py

298 lines
6.1 KiB
Python
Raw Normal View History

2019-08-06 01:08:54 +00:00
#!/usr/bin/python3
2019-03-10 22:06:04 +00:00
# -*- coding: utf-8 -*-
# -*- mode: Python -*-
'''
LJ v0.8.0
Pygame Simulator plugin for LJ
LICENCE : CC
Sam Neurohack, loloster,
OSC server with :
/simu/quit
/simu/newpl new pl number to draw
/simu/newclient new client number to draw
2019-08-06 01:08:54 +00:00
/ping
2019-03-10 22:06:04 +00:00
'''
2019-08-06 01:08:54 +00:00
2019-03-10 22:06:04 +00:00
#from __future__ import print_function
import time
import math
import random
import itertools
2019-08-06 01:08:54 +00:00
import traceback
2019-03-10 22:06:04 +00:00
import sys
import os
#import thread
import redis
2019-08-06 01:08:54 +00:00
2019-03-10 22:06:04 +00:00
import pdb
2019-08-06 01:08:54 +00:00
import types, ast, argparse, struct
import numpy as np
sys.path.append('../libs')
# import from LJ
ljpath = r'%s' % os.getcwd().replace('\\','/')
sys.path.append(ljpath +'/libs/')
#print ljpath+'/libs/'
import lj23 as lj
is_py2 = sys.version[0] == '2'
if is_py2:
from OSC import OSCServer, OSCClient, OSCMessage
#print ("Importing lj23 and OSC from libs...")
else:
from OSC3 import OSCServer, OSCClient, OSCMessage
2019-03-10 22:06:04 +00:00
screen_size = [750,750]
2019-03-10 22:06:04 +00:00
pl = [[],[],[],[]]
2019-03-24 16:24:52 +00:00
2019-03-10 22:06:04 +00:00
print ("")
print ("LJ v0.8.0 : Pygame simulator")
print ("")
2019-08-06 01:08:54 +00:00
import pygame
2019-03-10 22:06:04 +00:00
print ("Arguments parsing if needed...")
2019-08-06 01:08:54 +00:00
2019-03-10 22:06:04 +00:00
#
# Arguments parsing
#
2019-03-24 16:24:52 +00:00
argsparser = argparse.ArgumentParser(description="One laser Simulator for LJ")
2019-03-10 22:06:04 +00:00
argsparser.add_argument("-r","--redisIP",help="IP of the Redis server used by LJ (127.0.0.1 by default) ",type=str)
2019-03-24 16:24:52 +00:00
argsparser.add_argument("-m","--myIP",help="Local IP (127.0.0.1 by default) ",type=str)
2019-03-10 22:06:04 +00:00
argsparser.add_argument("-c","--client",help="LJ client number (0 by default)",type=int)
argsparser.add_argument("-l","--laser",help="Laser number to be displayed (0 by default)",type=int)
argsparser.add_argument("-v","--verbose",help="Verbosity level (0 by default)",type=int)
args = argsparser.parse_args()
if args.client:
ljclient = args.client
else:
ljclient = 0
# Laser choice
if args.laser:
simuPL = args.laser
else:
simuPL = 0
# Debug ?
if args.verbose:
debug = args.verbose
else:
debug = 0
# Redis Computer IP
if args.redisIP != None:
redisIP = args.redisIP
else:
redisIP = '127.0.0.1'
r = redis.StrictRedis(host=redisIP, port=6379, db=0)
2019-08-06 01:08:54 +00:00
lj.Config(redisIP,0,"simu")
2019-03-10 22:06:04 +00:00
2019-03-24 16:24:52 +00:00
# myIP
if args.myIP != None:
myIP = args.myIP
else:
myIP = '127.0.0.1'
2019-03-10 22:06:04 +00:00
#
# OSC
#
2019-03-24 16:24:52 +00:00
oscIPin = myIP
2019-03-10 22:06:04 +00:00
oscPORTin = 8008
2019-03-24 16:24:52 +00:00
ljIP = redisIP
2019-03-10 22:06:04 +00:00
ljPort = 8002
print ("")
print ("Receiving on ", oscIPin, ":",str(oscPORTin))
oscserver = OSCServer( (oscIPin, oscPORTin) )
oscserver.timeout = 0
2019-08-06 01:08:54 +00:00
oscrun = True
2019-03-10 22:06:04 +00:00
def handle_timeout(self):
self.timed_out = True
oscserver.handle_timeout = types.MethodType(handle_timeout, oscserver)
2019-08-06 01:08:54 +00:00
def SendLJ(address, args):
2019-03-10 22:06:04 +00:00
if debug >0:
2019-08-06 01:08:54 +00:00
print("Sending to LJ...", address, args)
2019-03-10 22:06:04 +00:00
osclientlj = OSCClient()
osclientlj.connect((ljIP, ljPort))
oscmsg = OSCMessage()
oscmsg.setAddress(address)
oscmsg.append(args)
try:
osclientlj.sendto(oscmsg, (ljIP, ljPort))
oscmsg.clearData()
return True
except:
2019-08-06 01:08:54 +00:00
print('Connection to LJ IP', ljIP,'port', ljPort, 'refused : died ?')
2019-03-10 22:06:04 +00:00
return False
2019-08-06 01:08:54 +00:00
def WebStatus(message):
SendLJ("/status",message)
2019-03-10 22:06:04 +00:00
# RAW OSC Frame available ?
def osc_frame():
# clear timed_out flag
oscserver.timed_out = False
# handle all pending requests then return
while not oscserver.timed_out:
oscserver.handle_request()
# /quit
def quit(path, tags, args, source):
2019-08-06 01:08:54 +00:00
global oscrun
2019-03-10 22:06:04 +00:00
2019-08-06 01:08:54 +00:00
oscrun = False
2019-03-10 22:06:04 +00:00
pygame.quit()
2019-08-06 01:08:54 +00:00
lj.ClosePlugin()
2019-03-10 22:06:04 +00:00
# /newPL pointlistnumber
def newPL(path, tags, args, source):
user = ''.join(path.split("/"))
2019-08-06 01:08:54 +00:00
print ("")
print (user,path,args)
print ("Simulator got a new point list number :", args[0])
2019-03-10 22:06:04 +00:00
simuPL = args[0]
# /newClient clientnumber
def newClient(path, tags, args, source):
user = ''.join(path.split("/"))
2019-08-06 01:08:54 +00:00
print ("")
print (user,path,args)
print ("Simulator got a new client number : ", args[0])
2019-03-10 22:06:04 +00:00
ljclient = args[0]
2019-08-06 01:08:54 +00:00
# Redis key 'n' -> numpy array a
# array 2 dimensions is also store in redis key : h time w values
def fromRedis(n):
print ("get key", n)
encoded = r.get(n)
h, w = struct.unpack('>II',encoded[:8])
print(h,w)
a = np.frombuffer(encoded, dtype=np.int16, offset=8).reshape(h,w)
return a
2019-03-10 22:06:04 +00:00
#
# Pygame screen
#
def RenderScreen(surface):
if len(pl[simuPL]):
xyc_prev = pl[simuPL][0]
#pygame.draw.line(surface,self.black_hole_color,(x_bh_cur, y_bh_cur), (x_bh_next, y_bh_next))
#pygame.draw.line(surface,self.spoke_color,(x_bh_cur, y_bh_cur), (x_area_cur, y_area_cur))
for xyc in pl[simuPL]:
c = int(xyc[2])
if c: pygame.draw.line(surface,c,xyc_prev[:2],xyc[:2],3)
xyc_prev = xyc
2019-08-06 01:08:54 +00:00
#
# Startup
#
2019-03-10 22:06:04 +00:00
2019-08-06 01:08:54 +00:00
SendLJ("/simu/start",1)
WebStatus("pysimu startup...")
2019-03-10 22:06:04 +00:00
pygame.init()
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("LJ Simulator")
clock = pygame.time.Clock()
update_screen = False
2019-08-06 01:08:54 +00:00
oscserver.addMsgHandler( "/quit", lj.OSCquit )
oscserver.addMsgHandler( "/ping", lj.OSCping )
#oscserver.addMsgHandler( "/simu/start", start )
oscserver.addMsgHandler( "/simu/newpl", newPL )
oscserver.addMsgHandler( "/simu/newclient", newClient )
2019-03-10 22:06:04 +00:00
print ("Simulator displays client", ljclient, "point list", str(simuPL))
2019-08-06 01:08:54 +00:00
WebStatus("pySimu "+ str(ljclient) + " " + str(simuPL))
2019-03-10 22:06:04 +00:00
#
# Main
#
try:
2019-08-06 01:08:54 +00:00
while lj.oscrun:
2019-03-10 22:06:04 +00:00
# pending osc message ?
osc_frame()
# Pygame events
for event in pygame.event.get():
if event.type == pygame.QUIT:
break
screen.fill(0)
2019-08-06 01:08:54 +00:00
#print("/pl/"+ str(ljclient) + "/" + str(simuPL))
#print r.get("/pl/"+ str(ljclient) + "/" + str(simuPL))
if is_py2:
pl[simuPL] = ast.literal_eval(r.get("/pl/"+ str(ljclient) + "/" + str(simuPL)))
else:
pl[simuPL] = eval(r.get("/pl/"+ str(ljclient) + "/" + str(simuPL)))
#pl[simuPL] = fromRedis("/pl/"+ str(ljclient) + "/" + str(simuPL))
2019-03-10 22:06:04 +00:00
if update_screen:
update_screen = False
RenderScreen(screen)
pygame.display.flip()
else:
update_screen = True
clock.tick(30)
2019-08-06 01:08:54 +00:00
time.sleep(0.001)
2019-03-10 22:06:04 +00:00
except KeyboardInterrupt:
pass
2019-08-06 01:08:54 +00:00
except Exception:
traceback.print_exc()
2019-03-10 22:06:04 +00:00
finally:
pygame.quit()
2019-08-06 01:08:54 +00:00
lj.ClosePlugin()
2019-03-10 22:06:04 +00:00