[fix] Reworks some generators to use the library object

This commit is contained in:
alban 2020-11-28 21:44:10 +01:00
parent 499bde0d84
commit 48770457fc
5 changed files with 48 additions and 200 deletions

View File

@ -35,10 +35,9 @@ args = argsparser.parse_args()
fps=args.fps
verbose=args.verbose
optimal_looptime = 1 / fps
cli = Clitools({
"verbose" : verbose,
"looptime" : looptime,
"looptime" : 1 / fps,
"name" : name
})

View File

@ -24,7 +24,10 @@ import json
import redis
import sys
import time
name="generator::fromRedis"
from os import path, getcwd
abspath, filename = path.split(path.realpath(__file__ ))
sys.path.insert(0, path.join(abspath,"../lib"))
from clitools import Clitools
def debug(*args, **kwargs):
@ -46,15 +49,18 @@ verbose = args.verbose
key = args.key
ip = args.ip
port = args.port
optimal_looptime = 1 / fps
debug(name+" optimal looptime "+str(optimal_looptime))
cli = Clitools({
"verbose" : verbose,
"looptime" : 1 / fps,
"name" : "generator::fromRedis"
})
r = redis.Redis(
host=ip,
port=port)
r = redis.Redis( host=ip, port=port )
while True:
start = time.time()
cli.startFrame()
# Read from Redis
line = r.get(key)
# Decode as list of tuples
@ -63,10 +69,6 @@ while True:
pointsList = [list(elem) for elem in pointsList]
# Convert to JSON string
line = json.dumps( pointsList )
debug(name,"Key:{} line:{}".format(key,line))
cli.debug(name,"Key:{} line:{}".format(key,line))
print(line, flush=True);
looptime = time.time() - start
if( looptime < optimal_looptime ):
time.sleep( optimal_looptime - looptime)
debug(name+" micro sleep:"+str( optimal_looptime - looptime))
cli.endFrame()

View File

@ -1,136 +0,0 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# -*- mode: Python -*-
'''
Forward pointlist to redis key
END POINT Format : (x,y,color)
/pl/0/0 "[(150.0, 230.0, 255), (170.0, 170.0, 255), (230.0, 170.0, 255), (210.0, 230.0, 255), (150.0, 230.0, 255)]"
v0.1.0
Licensed under GNU GPLv3
by Cocoa, Sam Neurohack
'''
import sys
from os import path, getcwd
abspath, filename = path.split(path.realpath(__file__ ))
sys.path.insert(0, path.join(abspath,"../lib"))
from clitools import Clitools
from OSC3 import OSCServer, OSCClient, OSCMessage
from time import sleep
import ast
import redis
import argparse
argsparser = argparse.ArgumentParser(description="osc2redis generator")
argsparser.add_argument("-i","--ip",help="IP to bind to (0.0.0.0 by default)",default="0.0.0.0",type=str)
argsparser.add_argument("-p","--port",help="OSC port to bind to (9002 by default)",default=9002,type=str)
argsparser.add_argument("-r","--rip",help="Redis server IP (127.0.0.1 by default)",default="127.0.0.1",type=str)
argsparser.add_argument("-o","--rout",help="Redis port (6379 by default)",default=6379,type=str)
argsparser.add_argument("-v","--verbose",action="store_true",help="Verbose output")
args = argsparser.parse_args()
verbose = args.verbose
ip = args.ip
port = int(args.port)
rip = args.rip
rport = int(args.rout)
r = redis.StrictRedis(host=rip, port=rport, db=0)
def debug(msg):
if( verbose == False ):
return
print(msg)
oscserver = OSCServer( (ip, port) )
oscserver.timeout = 0
run = True
# this method of reporting timeouts only works by convention
# that before calling handle_request() field .timed_out is
# set to False
def handle_timeout(self):
self.timed_out = True
# funny python's way to add a method to an instance of a class
import types
oscserver.handle_timeout = types.MethodType(handle_timeout, oscserver)
def validate(pointlist):
state = True
if len(pointlist)<9:
state = False
try:
pl = bytes(pointlist, 'ascii')
check = ast.literal_eval(pl.decode('ascii'))
except:
state = False
return state
# 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()
# default handler
def OSChandler(oscpath, tags, args, source):
oscaddress = ''.join(oscpath.split("/"))
print("fromOSC Default OSC Handler got oscpath :", oscpath, "from :" + str(source[0]), "args :", args)
print(oscpath.find("/pl/"), len(oscpath))
if oscpath.find("/pl/") ==0 and len(args)==1:
print("correct OSC type :'/pl/")
if validate(args[0]) == True and len(oscpath) == 7:
print("new pl for key ", oscpath, ":", args[0])
if r.set(oscpath,args[0])==True:
debug("exports::redis set("+str(oscpath)+") to "+args[0])
else:
print("Bad pointlist -> msg trapped.")
else:
print("BAD OSC Message :", oscpath)
oscserver.addMsgHandler( "default", OSChandler )
# simulate a "game engine"
while run:
# do the game stuff:
sleep(0.01)
# call user script
OSC_frame()
oscserver.close()

View File

@ -4,9 +4,7 @@
'''
Experimental Laserized Turtle graphics library
See turtle1.py for example
Static text writer
pip3 install Hershey-Fonts
@ -29,47 +27,37 @@ import time
import argparse
import sys
from HersheyFonts import HersheyFonts
name="generator::text"
def debug(*args, **kwargs):
if( verbose == False ):
return
print(*args, file=sys.stderr, **kwargs)
from os import path, getcwd
abspath, filename = path.split(path.realpath(__file__ ))
sys.path.insert(0, path.join(abspath,"../lib"))
from clitools import Clitools
argsparser = argparse.ArgumentParser(description="Text generator")
argsparser.add_argument("-f","--fps",help="Frame Per Second",default=30,type=int)
argsparser.add_argument("-v","--verbose",action="store_true",help="Verbose output")
argsparser.add_argument("-t","--text",help="Text to display",default="hello",type=str)
argsparser.add_argument("-p","--police",help="Herschey font to use",default="futural",type=str)
argsparser.add_argument("-p","--police",help="Herschey font to use. One of ({})".format(", ".join(HersheyFonts().default_font_names)),default="futural",type=str)
args = argsparser.parse_args()
fps = args.fps
verbose = args.verbose
text = args.text
fontname = args.police
optimal_looptime = 1 / fps
debug(name+" optimal looptime "+str(optimal_looptime))
cli = Clitools({
"verbose" : verbose,
"looptime" : 1 / fps,
"name" : "generator::text"
})
def rgb2int(rgb):
return int('0x%02x%02x%02x' % tuple(rgb),0)
# Useful variables init.
white = rgb2int((255,255,255))
red = rgb2int((255,0,0))
blue = rgb2int((0,0,255))
green = rgb2int((0,255,0))
white = cli.rgb2int((255,255,255))
red = cli.rgb2int((255,0,0))
blue = cli.rgb2int((0,0,255))
green = cli.rgb2int((0,255,0))
color = 65280
shape = []
Allfonts = ['futural', 'astrology', 'cursive', 'cyrilc_1', 'cyrillic', 'futuram', 'gothgbt', 'gothgrt', 'gothiceng', 'gothicger', 'gothicita', 'gothitt', 'greek', 'greekc', 'greeks', 'japanese', 'markers', 'mathlow', 'mathupp', 'meteorology', 'music', 'rowmand', 'rowmans', 'rowmant', 'scriptc', 'scripts', 'symbolic', 'timesg', 'timesi', 'timesib', 'timesr', 'timesrb']
thefont = HersheyFonts()
#thefont.load_default_font()
thefont.load_default_font(fontname)
@ -81,14 +69,6 @@ for (x1, y1), (x2, y2) in thefont.lines_for_text(text):
while True:
start = time.time()
cli.startFrame()
print(shape, flush = True);
looptime = time.time() - start
if( looptime < optimal_looptime ):
time.sleep( optimal_looptime - looptime)
debug(name+" micro sleep:"+str( optimal_looptime - looptime))
#[[14.285714285714286, 100.0, 14.285714285714286, 25.0, 65280], [64.28571428571429, 100.0, 64.28571428571429, 25.0, 65280], [14.285714285714286, 64.28571428571429, 64.28571428571429, 64.28571428571429, 65280], [89.28571428571428, 53.57142857142858, 132.14285714285714, 53.57142857142858, 65280], [132.14285714285714, 53.57142857142858, 132.14285714285714, 60.714285714285715, 65280], [132.14285714285714, 60.714285714285715, 128.57142857142856, 67.85714285714286, 65280], [128.57142857142856, 67.85714285714286, 125.0, 71.42857142857143, 65280], [125.0, 71.42857142857143, 117.85714285714286, 75.0, 65280], [117.85714285714286, 75.0, 107.14285714285714, 75.0, 65280], [107.14285714285714, 75.0, 100.0, 71.42857142857143, 65280], [100.0, 71.42857142857143, 92.85714285714286, 64.28571428571429, 65280], [92.85714285714286, 64.28571428571429, 89.28571428571428, 53.57142857142858, 65280], [89.28571428571428, 53.57142857142858, 89.28571428571428, 46.42857142857143, 65280], [89.28571428571428, 46.42857142857143, 92.85714285714286, 35.714285714285715, 65280], [92.85714285714286, 35.714285714285715, 100.0, 28.571428571428573, 65280], [100.0, 28.571428571428573, 107.14285714285714, 25.0, 65280], [107.14285714285714, 25.0, 117.85714285714286, 25.0, 65280], [117.85714285714286, 25.0, 125.0, 28.571428571428573, 65280], [125.0, 28.571428571428573, 132.14285714285714, 35.714285714285715, 65280]]
cli.endFrame()

View File

@ -30,3 +30,6 @@ class Clitools:
delta = self.looptime - elapsed
time.sleep( delta )
self.debug(self.name + " micro sleep:" + str( delta ))
def rgb2int(rgb):
return int('0x%02x%02x%02x' % tuple(rgb),0)