easy examples in different languagess

This commit is contained in:
Sam 2023-07-17 01:22:27 +02:00
parent e45ab18f5d
commit 9b0c8ffc86
224 changed files with 11562 additions and 133 deletions

93
examples/python/dummy.py Normal file
View file

@ -0,0 +1,93 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# -*- mode: Python -*-
'''
This is the most basic generator you can imagine: straight up static!
v0.1.0
LICENCE : CC
by cocoa
'''
import time
import argparse
import sys
import redis
argsparser = argparse.ArgumentParser(description="dummy generator")
argsparser.add_argument("-f","--fps",help="Frame Per Second (30)",default=30,type=int)
argsparser.add_argument("-s","--speed",help="point per frame progress",default=3,type=int)
argsparser.add_argument("-v","--verbose",action="store_true",default="True",help="Verbose output")
argsparser.add_argument("-a","--algo",help="Algorithm : mire, square,line",default="mire",type=str)
argsparser.add_argument("-i","--ip",help="IP address of the Redis server (127.0.0.1)",default="127.0.0.1",type=str)
argsparser.add_argument("-p","--port",help="Port of the Redis server (6379)",default="6379",type=str)
argsparser.add_argument("-k","--key",help="Redis key to update, default (/pl/0/0)",default="/pl/0/0",type=str)
args = argsparser.parse_args()
r=redis.StrictRedis(host=args.ip, port=args.port, db=0)
fps=args.fps
verbose=args.verbose
optimal_looptime = 1 / fps
color = 16777215
square = [(0.0, 0.0, color), (0.0, 300.0, color), (300.0, 300.0, color), (300.0, 0.0, color), (0.0, 0.0, color)]
line =[]
for i in range(00,800,int(800/120)):
line.append((i, 400, color))
mire = [
(600-400,600-400,0),
(600-400,600-400,color),
(700-400,600-400,color),
(700-400,700-400,color),
(600-400,700-400,color),
(600-400,600-400,color),
(100-400,100-400,0),
(100-400,100-400,color),
(200-400,100-400,color),
(200-400,200-400,color),
(100-400,200-400,color),
(100-400,100-400,color),
(0-400,0-400,0),
(0-400,0-400,color),
(800-400,0-400,color),
(800-400,800-400,color),
(0-400,800-400,color),
(0-400,0-400,color),
(350-400,400-400,0),
(350-400,400-400,color),
(450-400,400-400,color),
(400-400,350-400,0),
(400-400,350-400,color),
(400-400,450-400,color)
]
match args.algo:
case "mire":
shape = str(mire)
case 'line':
shape = str(line)
case "square":
shape = str(square)
def debug(text):
if args.verbose:
print(text)
while True:
start = time.time()
if r.set(args.key,shape)==True:
debug("redis set("+str(args.key)+") to "+shape)
looptime = time.time() - start
if( looptime < optimal_looptime ):
time.sleep( optimal_looptime - looptime)
debug("micro sleep: "+str( optimal_looptime - looptime))

231
examples/python/sphere.py Normal file
View file

@ -0,0 +1,231 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# -*- mode: Python -*-
'''
Points around a sphere
v0.1.0
Basic run
- to lasers :
python3 generators/sphere.py | python3 exports/toRedis.py -v
- to LJ nano simulator :
python3 generators/sphere.py | python3 exports/tonano.py
Open/reload www/simulocal.html in a browser.
Licensed under GNU GPLv3
by cocoa, Sam Neurohack
'''
import sys
import traceback
import argparse
import time
import numpy as np
import math, random
import redis
from operator import itemgetter
argsparser = argparse.ArgumentParser(description="3D sphere")
argsparser.add_argument("-f","--fps",help="Frame Per Second (30)",default=30,type=int)
argsparser.add_argument("-s","--speed",help="point per frame progress",default=3,type=int)
argsparser.add_argument("-v","--verbose",action="store_true",default="True",help="Verbose output")
argsparser.add_argument("-i","--ip",help="IP address of the Redis server (127.0.0.1)",default="127.0.0.1",type=str)
argsparser.add_argument("-p","--port",help="Port of the Redis server (6379)",default="6379",type=str)
argsparser.add_argument("-k","--key",help="Redis key to update, default (/pl/0/0)",default="/pl/0/0",type=str)
args = argsparser.parse_args()
fps=args.fps
looptime = 1 / fps
verbose=args.verbose
ip = args.ip
port = args.port
key = args.key
# lhc variables Line style
lhcincspeed = 10
lhccurrentspeed = 25
lhclightspeed = 300
# lhc variables Circle style
lhcincspeed = 0.2
lhccurrentspeed = 11
lhclightspeed = 21
lhccirclesteps = 15
lhcradiusL = 250
lhcradiusR = 300
width = 600
height = 600
centerX = width / 2
centerY = height / 2
# 3D to 2D projection parameters
fov = 256
viewer_distance = 2.2
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))
color = 65280
L = [[100.0, 150.0, 0], [120.0, 150.0, 5], [140.0, 150.0, 10]]
R = [[460.0, 240.0, 350], [480.0, 240.0, 355], [500.0, 240.0, 360]]
particles = [L[0], L[1], L[2], R[0], R[1], R[2], R[2], R[1], R[0], L[2], L[1], L[0]]
circlepart = [[0.,lhcradiusL,color,0.,0.],[5.,lhcradiusL,color,0.,0.],[ 10.,lhcradiusL,color,0.,0.],[40. ,lhcradiusL,0,0.,0.], [80.,lhcradiusL,0,0.,0.],[120., lhcradiusL, 0,0.,0.],[160. ,lhcradiusL, 0,0.,0.],[200.,lhcradiusL,0,0.,0.],[240.,lhcradiusL,0,0.,0.],[280.,lhcradiusL,0,0.,0.],[320.,lhcradiusL,0,0.,0.],[350,lhcradiusR,color,0.,0.],[355,lhcradiusR,color,0.,0.],[360.,lhcradiusR,color,0.,0.] ]
def startFrame():
return time.time()
def endFrame(timer):
if not looptime :
debug( "No looptime provided at init.")
return
elapsed = time.time() - timer
if( elapsed < looptime ):
delta = looptime - elapsed
time.sleep( delta )
def msNow():
return time.time()
def debug(text):
if verbose:
print(text)
# use rad
def Proj3D(coord,angleX=0,angleY=0, angleZ=0):
#cli.debug(coord)
x = coord[0] #+ transx
y = coord[1] #+ transy
z = coord[2] + transz
cosa = math.cos(angleX)
sina = math.sin(angleX)
y2 = y
y = y2 * cosa - z * sina
z = y2 * sina + z * cosa
cosa = math.cos(angleY)
sina = math.sin(angleY)
z2 = z
z = z2 * cosa - x * sina
x = z2 * sina + x * cosa
cosa = math.cos(angleZ)
sina = math.sin(angleZ)
x2 = x
x = x2 * cosa - y * sina
y = x2 * sina + y * cosa
return x,y
def Circle(radius,angle):
rad = angle * math.pi / 180
x = radius * math.cos(rad)
y = radius * math.sin(rad)
return x,y
def phase_lhc():
global lhccurrentspeed
# cli.debug(L,R)
# Circle edition
for l in L:
l[2] += lhccurrentspeed
if l[2] > 360:
l[2] = 0
lhccurrentspeed += lhcincspeed
l[0],l[1]= Circle(lhcradiusL,l[2])
#cli.debug(l)
# decrement R points
for r in R:
r[2] -= lhccurrentspeed
if r[2] < 0:
r[2] = 360
lhccurrentspeed += lhcincspeed
r[0],r[1]= Circle(lhcradiusR,r[2])
# Optimized Circle edition
for p in circlepart:
# proton point
if p[2] != 0:
# turning CW lhcradiusL
if p[1] == lhcradiusL:
p[0] += lhccurrentspeed
if p[0] > 360:
p[0] = 0
lhccurrentspeed += lhcincspeed
#cli.debug(l)
# turning CCW lhcradiusR
if p[1] == lhcradiusR:
p[0] -= lhccurrentspeed
if p[0] < 0:
p[0] = 360
lhccurrentspeed += lhcincspeed
p[3], p[4] = Circle(p[1],p[0])
circlepl = sorted(circlepart,key=itemgetter(0))
#cli.debug(circlepl)
pl = []
for p in circlepl:
pl.append((p[3]+1, p[4]+1, 0))
pl.append((p[3]+1, p[4]+1, p[2]))
pl.append((p[3], p[4], p[2]))
return pl
if __name__ == "__main__":
r=redis.StrictRedis(host=ip, port=port, db=0)
try:
while True:
timer = startFrame()
pts = str(phase_lhc())
debug(pts)
if r.set(key,pts)==True:
debug("redis set("+pts+") to "+pts)
endFrame(timer)
except Exception:
debug(traceback.print_exc())
except KeyboardInterrupt:
sys.exit(0)
finally:
debug("End")
sys.exit(0)

91
examples/python/text.py Normal file
View file

@ -0,0 +1,91 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# -*- mode: Python -*-
'''
Static text writer
pip3 install Hershey-Fonts
v0.1.0
Font list :
'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'
Licensed under GNU GPLv3
by cocoa and Sam Neurohack
'''
import time
import argparse
from HersheyFonts import HersheyFonts
import redis
argsparser = argparse.ArgumentParser(description="Text generator")
argsparser.add_argument("-s","--speed",help="Frame Per Second (30)",default=30,type=int)
argsparser.add_argument("-x","--xpos",help="Position X (0)",default=0,type=int)
argsparser.add_argument("-y","--ypos",help="Position Y (0)",default=0,type=int)
argsparser.add_argument("-v","--verbose",action="store_true",default="True",help="Verbose output (true)")
argsparser.add_argument("-t","--text",help="Text to display, default hello",default="hello",type=str)
argsparser.add_argument("-f","--font",help="Herschey font to use. (futural) One of ({})".format(", ".join(HersheyFonts().default_font_names)),default="futural",type=str)
argsparser.add_argument("-i","--ip",help="IP address of the Redis server (127.0.0.1)",default="127.0.0.1",type=str)
argsparser.add_argument("-p","--port",help="Port of the Redis server (6379)",default="6379",type=str)
argsparser.add_argument("-k","--key",help="Redis key to update, default (/pl/0/0)",default="/pl/0/0",type=str)
args = argsparser.parse_args()
fps = args.speed
verbose = args.verbose
text = args.text
fontname = args.font
looptime = 1 / fps
r=redis.StrictRedis(host=args.ip, port=args.port, db=0)
def debug(text):
if verbose:
print(text)
def startFrame():
return time.time()
def endFrame(timer):
if not looptime :
debug( "No looptime provided at init.")
return
elapsed = time.time() - timer
if( elapsed < looptime ):
delta = looptime - elapsed
time.sleep( delta )
# Useful variables init.
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))
color = 65280
shape = []
thefont = HersheyFonts()
#thefont.load_default_font()
thefont.load_default_font(fontname)
thefont.normalize_rendering(120)
for (x1, y1), (x2, y2) in thefont.lines_for_text(text):
shape.append((x1+args.xpos, -y1+args.ypos, color))
shape.append((x2+args.xpos ,-y2+args.ypos, color))
while True:
timer = startFrame()
if r.set(args.key,str(shape))==True:
debug("redis set("+str(args.key)+") to "+str(shape))
print(shape, flush = True);
endFrame(timer)