vpu_live4.py 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993
  1. #!/usr/bin/python3
  2. import math
  3. import random
  4. import time
  5. import os
  6. import sys
  7. import traceback
  8. import gc
  9. sys.path.insert(0,"/opt/LibreLight/Xdesk/")
  10. from lib.cprint import cprint
  11. HOME = os.getenv('HOME')
  12. import _thread as thread
  13. from optparse import OptionParser
  14. ...
  15. parser = OptionParser()
  16. parser.add_option("-m", "--mode", dest="mode",
  17. help="pixel mode pix,x,y --mode 40,10,8") #, metavar="FILE")
  18. parser.add_option("-X", "--XX", dest="XX", #default=1,
  19. help="x-split") #, metavar="FILE")
  20. parser.add_option("-x", "--xx", dest="xsplit", #default=1,
  21. help="x-split") #, metavar="FILE")
  22. parser.add_option("-y", "--yy", dest="ysplit",#default=1,
  23. help="y-split") #, metavar="FILE")
  24. parser.add_option("", "--start-univ", dest="start_univ",default=2,
  25. help="set start-univers default=2") #, metavar="FILE")
  26. parser.add_option("", "--gobo-ch", dest="gobo_ch",default=0,
  27. help="gobo ch univ on 1") #, metavar="FILE")
  28. parser.add_option("", "--gobo-ch2", dest="gobo_ch2",default=0,
  29. help="gobo ch univ on 1") #, metavar="FILE")
  30. #os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (200,164)
  31. parser.add_option("", "--win-pos", dest="win_pos",default="200,164",
  32. help="SDL_VIDEO_WINDOW_POS --win-pos=200,164") #, metavar="FILE")
  33. parser.add_option("", "--pixel-mapping", dest="pixel_mapping",default=0,
  34. help="pixel_mapping file/on --pixel-mapping=_x") #, metavar="FILE")
  35. parser.add_option("", "--dual-vpu", dest="dual_vpu",default=0,
  36. help=" --dual-vpu=1/0") #, metavar="FILE")
  37. parser.add_option("", "--grid-a1-idim", dest="grid_a1_idim",default=0,
  38. help=" grid_a1_idim") #, metavar="FILE")
  39. parser.add_option("", "--grid-a2-idim", dest="grid_a2_idim",default=0,
  40. help=" grid_a2_idim") #, metavar="FILE")
  41. parser.add_option("", "--countdown", dest="countdown",#default=1,
  42. help="enable countdown") #, metavar="FILE")
  43. parser.add_option("", "--videoplayer", dest="videoplayer",#default=1,
  44. help="enable videoplayer") #, metavar="FILE")
  45. parser.add_option("", "--title", dest="title",default="SCREEN",
  46. help="set title") #, metavar="FILE")
  47. (options, args) = parser.parse_args()
  48. import numpy
  49. QUIT = 0
  50. for o in dir(options):
  51. if "_" in o:
  52. continue
  53. print(o,options.__getattribute__(o))
  54. START = time.time()
  55. # ===== ARTNET DMX =========
  56. import memcache
  57. mc = memcache.Client(['127.0.0.1:11211'], debug=0)
  58. def read_index():
  59. ips=mc.get("index")#cmd)
  60. if ips is None:
  61. ips = {}
  62. return ips
  63. def select_ip(ips, univ=2): # artnet univ
  64. _univ = ":{}".format(univ)
  65. for ip in ips: #high priority
  66. if "2.0.0" in ip and _univ in ip:
  67. return ip
  68. for ip in ips:
  69. if "ltp-out" in ip and _univ in ip:
  70. return ip
  71. INPUT_START_POS_X = 40
  72. INPUT_START_POS_Y = 236 #320
  73. FUNC = 0
  74. COUNTER = []
  75. if options.countdown:
  76. cdmx_start = options.countdown.split(",")
  77. for cdmx in cdmx_start:
  78. try:
  79. cdmx = int(cdmx)
  80. _tmp={"DMX":cdmx,"DIM":0,"PAN":127,"TILT":127
  81. ,"CONTROL":0,"SEC":10
  82. ,"RED":255,"GREEN":255,"BLUE":255
  83. ,"_time":time.time(),"_RUN":0,"_pause":0,"_start":0
  84. ,"_SEC":">{}<".format(cdmx)
  85. }
  86. COUNTER.append(_tmp)
  87. except Exception as e:
  88. cprint("EXCEPTION COUNTER INIT ",cdmx,color="red")
  89. def read_dmx(ip):
  90. r = ""
  91. if ip:
  92. #t = int(math.sin(time.time() - s)*10)
  93. r = mc.get(ip) #"2.0.0.13:2")
  94. rr = [0]*512
  95. for i,v in enumerate(r):
  96. try: #cleanup ltp-out to int
  97. v = int(v)
  98. rr[i] = v
  99. except:pass
  100. r = rr
  101. if not r:
  102. c = 0
  103. #time.sleep(0.1)
  104. r = [0] *512
  105. for i in range(12*8+1):
  106. dmx = i*4
  107. #print(dmx)
  108. r[dmx:dmx+4] = [255,10,10,40]
  109. return r
  110. # ===== ARTNET DMX =========
  111. MEDIALIST = []
  112. try:
  113. MEDIALIST = open_medialist()
  114. except:pass
  115. #import json
  116. #import pickle
  117. from tool.video_capture import scan_capture #as scan_capture
  118. def _cam_cfg_new(fn):
  119. #fn = HOME+'/LibreLight/video_in.txt'
  120. txt = [
  121. "# Example",
  122. "d0 w640 h480 # PAL",
  123. "d0 w800 h600 # SVGA",
  124. "d0 w960 h540 # 540p",
  125. "d0 w1024 h768 # XGA <---",
  126. "d0 w1280 h720 # 720p",
  127. "d0 w1440 h900 # WSXGA+",
  128. "d0 w1600 h900 # WSXGA - Laptop 16:9",
  129. "d0 w1920 h1080 # HD 1080p",
  130. "",
  131. "# config",
  132. "d0 w1024 h768 # XGA",
  133. ]
  134. f = open(fn ,"w")
  135. f.write("\n".join(txt))
  136. f.close()
  137. def VideoMeta():
  138. out = {}
  139. out["run"] = 1
  140. out["x"] = 0
  141. out["y"] = 0
  142. out["ang"] = 0
  143. out["scale"] = 0
  144. out["fname"] = "xxx.mp4"
  145. out["fpath"] = "xxx.mp4"
  146. class VideoPlayer(): # concept / mokup
  147. def __init__():
  148. self.data = VideoMeta()
  149. def play(self):
  150. pass
  151. def pause(self):
  152. pass
  153. def reset(self):
  154. pass
  155. def select(self):
  156. pass
  157. def draw(self):
  158. pass
  159. def _rescale_frame_by_width(frame, width):
  160. #print(type(frame)) #np array
  161. height = int(frame.shape[0]/frame.shape[1] * width )
  162. dim = (width, height)
  163. #return cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)
  164. return cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
  165. def _rescale_frame_by_procent(frame, percent=75):
  166. #print(type(frame))
  167. width = int(frame.shape[1] * percent/ 100)
  168. height = int(frame.shape[0] * percent/ 100)
  169. dim = (width, height)
  170. return cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
  171. def _moveImage(img,x=0,y=0,shape=None):
  172. #print(type(img)) #np.array
  173. # Creating a translation matrix
  174. np = numpy
  175. translation_matrix = np.float32([ [1,0,x], [0,1,y] ])
  176. if shape is None:
  177. num_cols,num_rows = img.shape[1::-1]
  178. else:
  179. num_cols,num_rows = shape #[1::-1]
  180. # Image translation
  181. frame = cv2.warpAffine(img, translation_matrix, (num_cols,num_rows))
  182. return frame
  183. def _rotateImage(image, angle):
  184. #print(type(image)) # nm.array
  185. if angle in [0,360]:
  186. pass#return image
  187. try:
  188. #print("EE",image.shape)
  189. #print("EE",dir(image))
  190. shape = list(image.shape[1::-1])
  191. bg_shape = shape[:]
  192. delta_x = 0 # + nach rechts
  193. delta_y = 0 # + nach unten
  194. r = []
  195. r.extend(list(range(0-3,0+3)))
  196. r.extend(list(range(180-3,180+3)))
  197. r.extend(list(range(360-3,360+3)))
  198. r2 = []
  199. r2.extend(list(range(90-3,90+3)))
  200. r2.extend(list(range(270-3,270+3)))
  201. if angle in r:
  202. pass
  203. elif 0:#angle in r2:
  204. bg_shape = bg_shape[::-1]
  205. delta_y = -65 #shape[1]/2 # # + nach unten
  206. delta_x = 30 #shape[1]/2 # # + nach unten
  207. delta_y -= bg_shape[1]/2 - 20
  208. delta_x = bg_shape[0]/2 - 10
  209. pass
  210. else:
  211. if shape[0] > shape[1]:
  212. # landscape video
  213. bg_shape = (shape[0],shape[0])
  214. delta_y = (shape[0]-shape[1])/2
  215. else:
  216. # portrait video
  217. bg_shape = (shape[1],shape[1])
  218. delta_x = (shape[1]-shape[0])/2
  219. center = (shape[0]/2+delta_x,shape[1]/2+delta_y)
  220. image = _moveImage(image,x=delta_x,y=delta_y,shape=bg_shape)
  221. bg_shape = tuple(bg_shape) # exception ... if angle 0 no tuple !!!
  222. rot_mat = cv2.getRotationMatrix2D(center,angle,1.0)
  223. frame = cv2.warpAffine(image, rot_mat, bg_shape ) #,flags=cv2.INTER_LINEAR)
  224. return frame
  225. except Exception as e:
  226. raise(e)
  227. def _cam_cfg():
  228. fn = HOME+'/LibreLight/video_in.txt'
  229. if not os.path.isfile(fn):
  230. _cam_cfg_new(fn)
  231. f = open(fn ,"r")
  232. lines = f.readlines()
  233. f.close()
  234. cfg = ""
  235. for i in lines:
  236. if i.startswith("d"):
  237. cfg = i
  238. cfg = cfg.split()
  239. capture = scan_capture(name="MiraBox",serial="")
  240. #capture = scan_capture(name="Integrated",serial="")
  241. if capture:
  242. print("capture",capture)
  243. try:
  244. cfg.append(capture[0][0])
  245. except Exception as e:
  246. cprint("except 2992",e,color="red")
  247. else:
  248. cprint("---- ERROR NO VIDEO CAPTURE DEV - FOUND ----",color="red")
  249. print("_cam_cfg:",len(cfg),[cfg])
  250. return cfg
  251. def _open_cam(obj):
  252. # LIVE VIDEO INPUT UVC DEVICE
  253. cfg = _cam_cfg()
  254. print("III",cfg)
  255. res = "HD"
  256. #res = "4K"
  257. #res = ""
  258. if res == "HD":
  259. # HD
  260. w = 1920
  261. h = 1080
  262. elif res == "4K":
  263. # 4K
  264. w = 3840
  265. h = 2160
  266. else:
  267. # LOW
  268. w = 1280
  269. h = 720
  270. d = 0 # VIDEO INPUT DEV
  271. for c in cfg:
  272. try:
  273. if c.startswith("d"):
  274. d = int(c[1:])
  275. if c.startswith("h"):
  276. h = int(c[1:])
  277. if c.startswith("w"):
  278. w = int(c[1:])
  279. if c.startswith("video"):
  280. d = int(c[-1])
  281. except Exception as e:
  282. cprint("Exception", "cam_cfg 878",e,color="red")
  283. df = "video{}".format(d)
  284. if obj._stop:
  285. df += "_stop"
  286. elif os.path.exists("/dev/{}".format(df)):
  287. obj.Rcap = cv2.VideoCapture(d)
  288. obj.Rcap.set(cv2.CAP_PROP_FRAME_WIDTH, w)
  289. obj.Rcap.set(cv2.CAP_PROP_FRAME_HEIGHT, h)
  290. else:
  291. #obj.Rcap = None
  292. #obj.end = 1
  293. df += "_error"
  294. if obj.fname.startswith("cam_"):
  295. obj.fname = "cam_{}".format(df)
  296. def calc_dim(dim=1,rgb=[255,255,255]):
  297. rgb[0] = int(rgb[0]*dim/255)
  298. rgb[1] = int(rgb[1]*dim/255)
  299. rgb[2] = int(rgb[2]*dim/255)
  300. return rgb
  301. def draw_media_background(wn,x,y,w,h,rgb):
  302. hx=int(w/2)
  303. hy=int(h/2)
  304. p1 = [x-hx ,y-hy] # links oben
  305. p2 = [x+hx ,y+hy] # recht unten
  306. p3 = [x+hx ,y-hy] # rechts oben
  307. p4 = [x-hx ,y+hy] # links unten
  308. bg_on=0
  309. if bg_on:
  310. #pygame.draw.rect(wn,rgb ,[p1[0] ,p1[1] ,__xw ,__yw])
  311. #pygame.draw.rect(wn,[25,20,20],[p1[0]+1,p1[1]+1,hx-2,hx-2])
  312. pass
  313. # frame border
  314. pygame.draw.line(wn,rgb ,p1 ,p3) # top
  315. pygame.draw.line(wn,rgb ,p2 ,p3) # right
  316. pygame.draw.line(wn,rgb ,p2 ,p4) # bottom
  317. pygame.draw.line(wn,rgb ,p1 ,p4) # right
  318. # cross lines
  319. pygame.draw.line(wn,rgb ,p1 ,p2) # tl - br
  320. pygame.draw.line(wn,rgb ,p3 ,p4) # tr - bl
  321. class PixelMedia():
  322. def __init__(self,dmx=None,_id=None):
  323. global MEDIALIST
  324. self._id = _id
  325. self.fpath = HOME+'/Downloads/'
  326. self.fpath = HOME+'/LibreLight/video/'
  327. self.fname = '' #'bbb_sunflower_480x320.mp4'
  328. #self.fname = 'no-video.mp4'
  329. try:
  330. self.fname = MEDIALIST[0]
  331. except Exception as e:
  332. cprint("Exception set video from MEDIALIST 5543:",e,color="red")
  333. self.img_meta = {}
  334. self.restart_t = time.time()
  335. self.fps = 0
  336. self.scale = 50 #%
  337. self.angle = 0 #%
  338. self.dmx=dmx
  339. if self._id >= 250:
  340. self.fname = "cam_"
  341. self.dim = 0
  342. self.x = 0
  343. self.y = 0
  344. self.init_count = 0
  345. self.cap = None
  346. self.shape = [200,200]
  347. self.img = None
  348. self.success = 1
  349. self._run = 0
  350. self.end = 0
  351. self._media_nr = 0
  352. self._stop = 0 # fix buffer-overflow ... ?? opencv
  353. self.shape_x = 370
  354. self.shape_y = 235
  355. self.dmx_change = 0
  356. self.frame_change = 0
  357. self.init()
  358. def update_dmx(self,x,y,scale,angle,dim):
  359. if int(self.x*100) != int(x*100):
  360. self.x = x
  361. self.dmx_change = 1
  362. if int(self.y*100) != int(y*100):
  363. self.y = y
  364. self.dmx_change = 1
  365. if int(self.scale*100) != int(scale*100):
  366. self.scale = scale
  367. self.dmx_change = 1
  368. if int(self.angle*100) != int(angle*100):
  369. self.angle = angle
  370. self.dmx_change = 1
  371. if int(self.dim*100) != int(dim*100):
  372. self.dim = dim
  373. self.dmx_change = 1
  374. def __repr__(self):
  375. sizeof = int(sys.getsizeof(self.buffer)/8)
  376. return "< id:{} buf:{} run:{} fps:{} scale:{} name:{} {}kb>".format(
  377. id(self),len(self.buffer),self._run,self.fps,self.scale
  378. ,self.fname,sizeof
  379. )
  380. def restart(self):
  381. print(self,"reset()")
  382. self.pos = 0
  383. self.restart_t = time.time()
  384. def init(self):
  385. print("---- ---- PixelMedia.init()",[self.fname,self._media_nr])
  386. print(MEDIALIST)
  387. self.time = 0
  388. self.t_delta = 0
  389. self.t_last = time.time()
  390. self.im = None
  391. self.pos = 0
  392. self.buffer = []
  393. self._init()
  394. self.init_count = 1
  395. self.dmx_change = 0
  396. self.frame_change = 0
  397. def select_video(self,dmx_value):
  398. try:
  399. dmx_value = int(dmx_value/10)
  400. if self._media_nr != dmx_value:
  401. self._media_nr = dmx_value
  402. if self._media_nr >= 25:
  403. self.fname = "cam_"
  404. self.init()
  405. elif self._media_nr < len(MEDIALIST):
  406. self.fname = str(MEDIALIST[self._media_nr])
  407. self.init()
  408. #finally:pass
  409. except Exception as e:
  410. cprint("PixelMedia.select_video()",dmx_value,e,color="red")
  411. def _init(self):
  412. print(self)
  413. print("videoplayer.init()",self.fpath,self.fname)
  414. if not os.path.isfile(self.fpath+self.fname):
  415. print()
  416. cprint("video file does not exits !! >",self.fpath,self.fname,color="red")
  417. print()
  418. #exit()
  419. self.Rsuccess = 0
  420. if not cv2:
  421. return
  422. if self._stop:
  423. return
  424. if self.fname.endswith(".jpg"):
  425. self.img_meta = create_img_meta()
  426. if self.img_meta["fname"] == self.fname:
  427. return
  428. self.img_meta["_fpath"] = self.fpath
  429. self.img_meta["fname"] = self.fname
  430. img_open(self.img_meta)
  431. self.buffer = [1] #,2,3]
  432. return # STOP
  433. if self.fname.startswith("cam_"):
  434. _open_cam(obj=self)
  435. self.img_meta = {}
  436. else:
  437. self.Rcap = cv2.VideoCapture(self.fpath+self.fname, cv2.CAP_FFMPEG)
  438. #FFMPEG malloc(): unsorted double linked list corrupted ... Abgebrochen
  439. self.img_meta = {}
  440. print("_init ?",self.Rcap)
  441. print("open ?" ,self.Rcap.isOpened())
  442. self.Rcap.read()
  443. #self.Rfvs = FileVideoStream(self.fpath+self.fname).start()
  444. self.Rsuccess = 1
  445. self.read()
  446. def _del(self):
  447. if self.img_meta:
  448. return
  449. self._stop = 1
  450. self.buffer = [] #.append(self.img)
  451. self.Rcap.release()
  452. self.Rcap.retrieve()
  453. self.Rcap = None
  454. self.cap = None
  455. del self.Rcap #.release()
  456. gc.collect()
  457. def read(self):
  458. if self.img_meta:
  459. return
  460. success = self.Rsuccess
  461. ok = 0
  462. if success and self.fname:
  463. cap = self.Rcap
  464. #fvs = self.Rfvs
  465. _break = 0
  466. try:
  467. success, img = cap.read()
  468. if not success:
  469. self.Rcap.release()
  470. self.Rcap.retrieve()
  471. self.end = 1
  472. #j = json.dumps(self.buffer) #.append(img)
  473. #f = open("/tmp/buff")
  474. #f.write(j)
  475. #f.close()
  476. return
  477. if self.fps == 0:
  478. self.fps = cap.get(cv2.CAP_PROP_FPS)
  479. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  480. if self.fname.startswith("cam_"):
  481. pass
  482. #img = _rescale_frame_by_width(img, 1200)
  483. else:
  484. img = _rescale_frame_by_width(img, 200)
  485. #img = _rescale_frame_by_width(img, 1200)
  486. # store frame into buffer list
  487. if self.fname.startswith("cam_"):
  488. self.buffer = [ img ]
  489. else:
  490. self.buffer.append(img)
  491. ok = 1
  492. if len(self.buffer) % 100 == 0:
  493. _id = str(self.__repr__)[-5:-1]
  494. print(_id,"video read",self.dmx,len(self.buffer),self.fname,"fps",self.fps,self.dim)
  495. except Exception as e:
  496. cprint("Exception","_init",self,e,end="",color="red")
  497. self.success = 1
  498. return ok
  499. def prev(self):
  500. self.pos -= 1
  501. if self.pos < 0:
  502. self.pos = len(self.buffer)-1
  503. if self.pos >= len(self.buffer):
  504. self.pos = len(self.buffer)-1
  505. self.im = self.buffer[int(self.pos)]
  506. def _rotateImage(self,image, angle):
  507. if self.img_meta:
  508. return
  509. return _rotateImage(image, angle)
  510. def pause(self):
  511. if self.img_meta:
  512. return
  513. #print("pause",self.t_last)
  514. t = time.time()
  515. self.t_delta = 0
  516. self.t_last = t
  517. def next_frame(self):
  518. if self.img_meta:
  519. return
  520. #return 0
  521. if self._run and len(self.buffer) > 30:
  522. t = time.time()
  523. self.t_delta = t-self.t_last
  524. self.pos += int(self.t_delta*self.fps)
  525. self.t_last = t
  526. #self.frame_change = 1
  527. else:
  528. self.pause()
  529. if self.restart_t > time.time()-0.5:
  530. # prevent flickerung by reset
  531. #self.pause()
  532. self.pos = 5
  533. pass
  534. # restart at the end
  535. if int(self.pos) >= len(self.buffer)-1:
  536. self.pos = 0
  537. self.img = None
  538. if self.buffer:
  539. self._img = self.buffer[int(self.pos)]
  540. self.img = self._img
  541. def check_cv2_check(self):
  542. if not cv2:
  543. cprint("cv2 not importet ! no VIDEO support !",color="red")
  544. return 0
  545. return 1
  546. def next(self):
  547. if self.img_meta:
  548. self.next_frame()
  549. return
  550. try:
  551. self.next_frame()
  552. if self.img is None:
  553. return
  554. self.img = _rescale_frame_by_procent(self.img, percent=self.scale)
  555. # rotate frame BUG: x,y offset ??? !!!
  556. self.img = self._rotateImage(self.img, self.angle)
  557. self.shape = self.img.shape[1::-1]
  558. if len(self.buffer) % 100 == 0:
  559. _id = str(self.__repr__)[-5:-1]
  560. print("next",_id)
  561. # add DIMER to videplayer
  562. cv2.normalize(self.img, self.img, 0, self.dim, cv2.NORM_MINMAX)
  563. img = self.img
  564. if img is None:
  565. return
  566. self.im = None
  567. self.im = pygame.image.frombuffer(img.tobytes(), self.shape, "RGB")
  568. #self.next_frame()
  569. except AttributeError as e:
  570. time.sleep(.05)
  571. #if self.init_count % 100 == 0:
  572. cprint("except 776",e,color="red")
  573. #self.init()
  574. except Exception as e:
  575. cprint("except 756",e,color="red")
  576. cprint(traceback.format_exc(),color="red")
  577. cprint(sys.exc_info()[2],color="red")
  578. cprint()
  579. def calc_im_size(self):
  580. try:
  581. xx ,yy = self.im.get_size()[:2]
  582. self.shape_x = xx
  583. self.shape_y = yy
  584. except AttributeError as e:
  585. pass
  586. def calc_zoom(self,xx,yy):
  587. if self.scale < 255*.05:
  588. self.scale = 255*0.05
  589. xx = int(xx*self.scale/255)
  590. yy = int(yy*self.scale/255)
  591. return xx,yy
  592. def draw_video(self,wn,im,x,y,w,h):
  593. #rgb = [255,0,0]
  594. #rgb = calc_dim(self.dim,rgb)
  595. #draw_media_background(wn,x,y,w=w,h=h,rgb=rgb)
  596. xh=int(w/2) # half
  597. yh=int(h/2) # half
  598. wn.blit(im, (int(x-xh), int(y-yh))) # draw frame
  599. def draw_cam(self,wn,im,x,y,w,h):
  600. if not self.fname.startswith("cam_"):
  601. return
  602. xx = w #self.shape_x
  603. yy = h #self.shape_y
  604. xx,yy = self.calc_zoom(xx,yy)
  605. wn.blit(im, (int(x-xx/2), int(y-yy))) # draw frame
  606. def is_change(self):
  607. #if self.frame_change or self.dmx_change:
  608. if self.frame_change or self.dmx_change:
  609. #if self.dmx_change:
  610. return True
  611. def change_reset(self):
  612. if self.frame_change:
  613. print("media frame change")
  614. if self.dmx_change:
  615. print("media dmx change")
  616. self.frame_change = 0
  617. self.dmx_change = 0
  618. def draw(self,wn=None):
  619. if self.img_meta:
  620. self.img_meta["pos"] = [self.x,self.y]
  621. self.img_meta["scale"] = self.scale #= 50 #%
  622. self.img_meta["dim"] = self.dim #255
  623. self.img_meta["ang"] = self.angle # 0
  624. #if self.is_change():
  625. img = self.img_meta["raw_img"]
  626. im2 = img_rescale(img, percent=self.img_meta["scale"])
  627. #
  628. # # add DIMER to videplayer
  629. im2 = cv2.normalize(im2, im2, 0, self.img_meta["dim"], cv2.NORM_MINMAX)
  630. self.img_meta["img"] = im2
  631. img_draw_raw(self.img_meta)
  632. self.change_reset()
  633. return
  634. if self.dim <= 1:
  635. return
  636. self.calc_im_size()
  637. im = self.im
  638. x=self.x
  639. y=self.y
  640. #w,h = self.calc_zoom(x,y)
  641. w = self.shape_x
  642. h = self.shape_y
  643. if self.success and wn and im:
  644. if self.fname.startswith("cam_"):
  645. self.draw_cam(wn,im,x,y,w,h)
  646. else:
  647. self.draw_video(wn,im,x,y,w,h)
  648. self.change_reset()
  649. def get_meta(self):
  650. out = {}
  651. mode="x"
  652. if self._run:
  653. mode = "run"
  654. else:
  655. mode = "pause"
  656. out["frames"] = len(self.buffer)
  657. out["mode"] = mode
  658. out["fname"] = self.fname
  659. out["scale"] =self.scale
  660. out["fps"] = int(self.fps)
  661. out["pos"] = self.pos
  662. out["dim"] = self.dim
  663. out["run"] = self._run
  664. out["cur"] = 0
  665. if self.fps > 0: # check if div zerro
  666. out["cur"] = (self.pos/self.fps)
  667. out["x"] = self.x
  668. out["y"] = self.y
  669. out["id"] = self._id
  670. out["nr"] = self._media_nr
  671. out["pz"] = 0
  672. return out
  673. def _overlay_text(self):
  674. meta = self.get_meta()
  675. keys = ["fps","pos","frames","cur","pz","x","y","scale","dim"]
  676. _line = create_format_line(meta,keys)
  677. keys= ["id","nr","mode","fname"]
  678. _line2 = create_format_line(meta,keys)
  679. return [_line,_line2]
  680. def create_format_line(meta,keys):
  681. _line = []
  682. for k in keys:
  683. v=meta.get(k,"-")
  684. _line.append(str(k))
  685. _line.append(":")
  686. if type(v) is float:
  687. _line.append("{:02f}".format(v))
  688. else:
  689. _line.append(str(v))
  690. _line.append(" ")
  691. _line = "".join(_line)
  692. return _line
  693. PixelMedia = PixelMedia
  694. VIDEO = []
  695. videoplayer=[]
  696. cv2 = None
  697. FileVideoStream = None
  698. _vid = 0
  699. if type(options.videoplayer) is str:
  700. try:
  701. import cv2
  702. except Exception as e:
  703. cprint("Except Import:",e,color="red")
  704. try:
  705. # faster video reading ... ???
  706. from imutils.video import FileVideoStream
  707. except Exception as e:
  708. cprint("Except Import:",e,color="red")
  709. max_videoplayer = 4
  710. dmx_start = options.videoplayer.split(",")
  711. for cdmx in dmx_start:
  712. if len(videoplayer) > max_videoplayer:
  713. print("-- videoplayer max count {} !! break".format(max_videoplayer))
  714. break
  715. print("-- videoplayer dmx:",cdmx)
  716. try:
  717. cdmx = int(cdmx)
  718. videoplayer.append( PixelMedia(cdmx,_id=_vid) )
  719. _vid += 1
  720. _tmp = {"DMX":cdmx,"DIM":0,"PAN":127,"TILT":127
  721. ,"CONTROL":0,"SEC":10,"VIDEO":"3"
  722. ,"RED":255,"GREEN":255,"BLUE":255
  723. ,"_time":time.time()
  724. ,"_RUN":0,"_SEC":">{}<".format(cdmx)
  725. }
  726. VIDEO.append(_tmp)
  727. except Exception as e:
  728. cprint("EXCEPTION COUNTER INIT ",cdmx,color="red")
  729. def loop_videoplayer():
  730. while 1:
  731. _videoplayer = videoplayer[:]
  732. #print(".")
  733. ok = 0
  734. for i in _videoplayer: #.append( PixelMedia(cdmx,_id=_vid) )
  735. try:
  736. r = i.read() # read next frame from file
  737. if r:
  738. ok = 1
  739. except Exception as e:
  740. cprint("EXCEPTION loop_videoplayer ",e,color="red")
  741. if ok == 0:
  742. time.sleep(0.1)
  743. else:
  744. time.sleep(0.005)
  745. if QUIT:
  746. break
  747. thread.start_new_thread(loop_videoplayer,())
  748. # ===== ======
  749. #self.fname = MEDIALIST[0]
  750. videoplayer2 = []
  751. def loop2_videoplayer():
  752. while 1:
  753. print()
  754. print()
  755. print()
  756. for i,v in enumerate(MEDIALIST):
  757. try:
  758. video1 = videoplayer2[i]
  759. video1.select_video(i)
  760. except:
  761. vi = PixelMedia(181,1)
  762. videoplayer2.append( vi )
  763. _videoplayer = videoplayer2[:]
  764. ok = 0
  765. j =0
  766. for i in _videoplayer: #.append( PixelMedia(cdmx,_id=_vid) )
  767. try:
  768. r = i.read() # read next frame from file
  769. if r:
  770. print(j,len(videoplayer2),i,len(i.buffer))
  771. ok = 1
  772. except Exception as e:
  773. cprint("EXCEPTION loop2_videoplayer ",e,color="red")
  774. time.sleep(0.002)
  775. j += 1
  776. p = 16
  777. block = [p,p]
  778. _x = 8
  779. _y = 8
  780. #HD = "0"
  781. if options.mode:
  782. try:
  783. HD = options.mode
  784. p,_x,_y = HD.split(",")
  785. _x = int(_x)
  786. _y = int(_y)
  787. p = int(p)
  788. block = [p,p]
  789. except Exception as e:
  790. cprint( "Exc",options.mode,e,color="red")
  791. # PARSE COMMANDLINE ARGUMENTS
  792. CFG_IN = {"name":"CFG_IN","x1":40,"y1":60,"x2":300,"y2":300 ,"w":300,"h":300}
  793. CFG_OUT = {"name":"CFG_OUT","x1":40,"y1":60,"x2":300,"y2":300 ,"w":300,"h":300,"on":0}
  794. #CFG_OUT2 = {"name":"CFG_OUT2","x1":p*8+142,"y1":60,"x2":300,"y2":300 ,"w":300,"h":300,"on":0}
  795. CFG_OUT2 = {"name":"CFG_OUT2","x1":p*8+(16*2)+8,"y1":60,"x2":300,"y2":300 ,"w":300,"h":300,"on":0}
  796. CFG_BLOCK = {"name":"CFG_BLOCK","size":16,"h-split":2,"v-split":2,"h-count":8,"v-count":8}
  797. if _x < 1:
  798. _x = 1
  799. if _y < 1:
  800. _y = 1
  801. CFG_BLOCK["h-count"] = _x
  802. CFG_BLOCK["v-count"] = _y # int(8*8/_x) #/_x+0.5) #_y
  803. CFG_BLOCK["size"] = p
  804. print( [options.xsplit])
  805. print( [options.ysplit])
  806. try:
  807. if options.xsplit:
  808. CFG_BLOCK["h-split"] = int(options.xsplit)
  809. if options.ysplit:
  810. CFG_BLOCK["v-split"] = int(options.ysplit)
  811. except Exception as e:
  812. cprint( "Exc",options.mode,e,color="red")
  813. print("HD",CFG_BLOCK["h-split"],CFG_BLOCK["v-split"])
  814. print("xy",_x,_y)
  815. print("++++++++++++++++++", p,_x,_y)
  816. _x2 = _x
  817. try:
  818. if options.XX:
  819. pass#_x2 = int(options.XX)
  820. except Exception as e:
  821. cprint( "Exc",options.mode,e,color="red")
  822. print("_x2 , -X",_x2)
  823. # ===== GUI =========
  824. import pygame
  825. import pygame.gfxdraw
  826. import pygame.font
  827. clock = pygame.time.Clock()
  828. os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (200,164)
  829. if options.win_pos:
  830. if "," in options.win_pos:
  831. win_pos = options.win_pos.split(",")
  832. try:
  833. WIN_POS = '%i,%i' % (int(win_pos[0]),int(win_pos[1]) )
  834. os.environ['SDL_VIDEO_WINDOW_POS'] = WIN_POS
  835. except Excetpion as e:
  836. cprint("win_pos",win_pos,e,color="red")
  837. os.environ['SDL_VIDEO_CENTERED'] = '0'
  838. pg = pygame
  839. pygame.init()
  840. pygame.mixer.quit()
  841. f = pygame.font.get_fonts()
  842. for i in f:
  843. if "mono" in i.lower():
  844. print(i)
  845. font = pygame.font.SysFont("freemonobold",22)
  846. font10 = pygame.font.SysFont("freemonobold",10)
  847. font12 = pygame.font.SysFont("freemonobold",12)
  848. font12l = pygame.font.SysFont("freemono",12)
  849. font15 = pygame.font.SysFont("freemonobold",15)
  850. font40 = pygame.font.SysFont("freemonobold",40)
  851. font80 = pygame.font.SysFont("freemonobold",70)
  852. #font = pygame.font.SysFont(None,30)
  853. fr = font.render("hallo" ,1, (200,0,255))
  854. def clean_path(path):
  855. _path = path[:]
  856. _path = _path.replace("/","-")
  857. _path = _path.replace(".","-")
  858. _path = _path.replace("\"","-")
  859. _path = _path.replace("'","-")
  860. return _path
  861. PIXEL_MAPPING = 0
  862. vpu_wall_conf_file = HOME+"/LibreLight/vpu_wall_conf.csv"
  863. grid_file = "/tmp/vpu_grid_hd.csv"
  864. text_file = HOME+"/LibreLight/vpu_text_hd.csv"
  865. media_list = "/tmp/vpu_medialist_hd.csv"
  866. media_list = HOME+"/LibreLight/video/" #.format(path)
  867. pm_wy = 0
  868. if options.pixel_mapping:
  869. PIXEL_MAPPING = 1
  870. CFG_OUT["on"] = 1
  871. path = options.pixel_mapping
  872. path = clean_path(path)
  873. grid_file = HOME+"/LibreLight/vpu_grid_hd{}.csv".format(path)
  874. if options.dual_vpu:
  875. grid_file = HOME+"/LibreLight/vpu_grid_dual{}.csv".format(path)
  876. text_file = HOME+"/LibreLight/vpu_text_hd{}.csv".format(path)
  877. media_list = HOME+"/LibreLight/vpu_medialist_hd{}.csv".format(path)
  878. media_list = HOME+"/LibreLight/video/" #.format(path)
  879. #_x = 8
  880. #_y = 8
  881. if options.dual_vpu:
  882. CFG_OUT2["on"] = 1
  883. print(" ",[options.pixel_mapping],"grid_file",grid_file)
  884. #grid_file = HOME+"/LibreLight/vpu_grid_hd.csv"
  885. MAIN_SIZE=(600,500)
  886. try:
  887. if _x < 8 and PIXEL_MAPPING >= 1:
  888. wx = 30+30+block[0] * 8
  889. CFG_IN["w"] = CFG_BLOCK["size"] * 8
  890. else:
  891. wx = 30+30+block[0] * _x
  892. wy = 40+40+block[1] * _y
  893. if type(options.videoplayer) is str:
  894. wy += 150 # video medialist
  895. MAIN_SIZE=(wx,wy)
  896. if PIXEL_MAPPING >= 1:
  897. pm_wy = 11*p #+ p*3
  898. CFG_IN["y1"] += 11*p
  899. if options.dual_vpu:
  900. if wx < p*16+155:
  901. wx = p*16+155
  902. MAIN_SIZE=(wx,wy+pm_wy)
  903. except Exception as e:
  904. cprint("Exception:",e,color="red")
  905. CFG_IN["w"] = CFG_BLOCK["size"] * CFG_BLOCK["h-count"]
  906. CFG_IN["h"] = CFG_BLOCK["size"] * CFG_BLOCK["v-count"]
  907. CFG_OUT["w"] = CFG_BLOCK["size"] * 8
  908. CFG_OUT["h"] = CFG_BLOCK["size"] * 8
  909. CFG_OUT2["w"] = CFG_BLOCK["size"] * 8
  910. CFG_OUT2["h"] = CFG_BLOCK["size"] * 8
  911. def CFG_CALC_P(CFG):
  912. CFG["x2"] = CFG["x1"]+CFG["w"]
  913. CFG["y2"] = CFG["y1"]+CFG["h"]
  914. CFG["p1"] = [CFG["x1"] ,CFG["y1"]]
  915. CFG["p2"] = [CFG["x2"] ,CFG["y2"]]
  916. print("CFG",CFG)
  917. # ?
  918. #CFG_IN["w"] = int(CFG_BLOCK["v-count"]-2) * CFG_BLOCK["size"]
  919. CFG_CALC_P(CFG_IN)
  920. CFG_CALC_P(CFG_OUT)
  921. CFG_CALC_P(CFG_OUT2)
  922. print("CFG_BLOCK",CFG_BLOCK)
  923. print()
  924. print()
  925. print()
  926. print()
  927. print()
  928. window = pygame.display.set_mode(MAIN_SIZE,pg.RESIZABLE)#,32)#,pygame.FULLSCREEN) #x left->right ,y top-> bottom
  929. pg.display.set_caption('LibreLight VPU-{}'.format(options.title))
  930. class Fix():
  931. def __init__(self,_id,pos,block=[16,16],univ=0,dmx=0,ch=4):
  932. #print("Fix",_id)
  933. self._id = _id
  934. self.dmx = (_id-1) * ch +1 #dmx
  935. self.univ = univ
  936. self.ch = ch
  937. self.pos = pos
  938. self.rgb = [0,0,0]
  939. self.block = block #[10,10]
  940. self.x = pos[0]
  941. self.y = pos[1]
  942. self._x = 0
  943. self._y = 0
  944. self.strobo = time.time()
  945. self.bmp = 250
  946. self.sub_fix = []
  947. sub_block =[block[0]/CFG_BLOCK["h-split"],block[1]/CFG_BLOCK["v-split"]]
  948. if _id <= 0: #exit
  949. return
  950. spalte = (_id-1)%_y +1
  951. zeile = int((_id-1)/_x2) #+1
  952. add_row = _x*CFG_BLOCK["h-split"]*CFG_BLOCK["v-split"]
  953. #zeile 1
  954. sid = (_id-1)*2 + zeile*CFG_BLOCK["h-split"]*_x2
  955. sid = sid+1
  956. sub_pos= [pos[0]*block[0],pos[1]*block[1]]
  957. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  958. self.sub_fix.append(sub_fix)
  959. sid = sid+1
  960. sub_pos= [pos[0]*block[0]+block[0]/2,pos[1]*block[1]]
  961. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  962. self.sub_fix.append(sub_fix)
  963. #zeile 2
  964. sid = (_id-1)*2+1 + _x2*CFG_BLOCK["h-split"] + zeile*CFG_BLOCK["h-split"]*_x2 # int(add_row)
  965. sub_pos= [pos[0]*block[0],pos[1]*block[1]+block[1]/2]
  966. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  967. self.sub_fix.append(sub_fix)
  968. #sid = sid+1
  969. sid = sid+1
  970. sub_pos= [pos[0]*block[0]+block[0]/2,pos[1]*block[1]+block[1]/2]
  971. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  972. self.sub_fix.append(sub_fix)
  973. def __repr__(self):
  974. return "<Fix: {} {} {} {}>".format(id(self),self.x,self.y,self.dmx)
  975. def calc(self,data):
  976. _rgb = [0,255,0]
  977. return _rgb
  978. def sub_calc(self,data):
  979. _rgb = [0,255,0]
  980. for sub_fix in self.sub_fix:
  981. sub_fix.block = self.block[:]
  982. _rgb = sub_fix.dmx_calc(data)
  983. return _rgb
  984. def _calc(self,a,b):
  985. A = (self.pos[0])*self.block[0]
  986. B = (self.pos[1])*self.block[1]
  987. C = self.block[0]-a
  988. D = self.block[1]-b
  989. return A,B,C,D
  990. def POS(self,x=None,y=None,a=0,b=0):
  991. if type(x) is int:
  992. self._x=x
  993. if type(y) is int:
  994. self._y=y
  995. A,B,C,D = self._calc(a,b)
  996. return [self._x+A,self._y+B,C,D]
  997. def XXXsubPOS(self,x=0,y=0,a=0,b=0):
  998. __out = []
  999. for sub_fix in self.sub_fix:
  1000. __out.append( sub_fix.POS(x,y,a,b) )
  1001. return __out
  1002. class SubFix():
  1003. def __init__(self,_id,pos,block=[16,16],univ=0,dmx=0,ch=4):
  1004. #print("Fix",_id)
  1005. self._id = _id
  1006. self.dmx = (_id-1) * ch +1 #dmx
  1007. self.univ = univ
  1008. self.ch = ch
  1009. self.pos = pos
  1010. self.rgb = [0,0,40]
  1011. self.block = block #[10,10]
  1012. self.x = pos[0]
  1013. self.y = pos[1]
  1014. self._x = 0
  1015. self._y = 0
  1016. self.strobo = time.time()
  1017. self.bmp = 250
  1018. def dmx_calc(self,data):
  1019. #return [130,30,20]
  1020. dmx_sub = [30]*10
  1021. dmx = self.dmx -1
  1022. _dmx_sub = []
  1023. if self.dmx >= 0:
  1024. dmx = rDMX(self.univ,self.dmx)-1
  1025. if dmx+self.ch < len(data):
  1026. _dmx_sub = data[dmx:dmx+self.ch]
  1027. if _dmx_sub:
  1028. dmx_sub = _dmx_sub
  1029. dim = dmx_sub[0]/255
  1030. r = dmx_sub[1]*dim
  1031. g = dmx_sub[2]*dim
  1032. b = dmx_sub[3]*dim
  1033. r = int(r)
  1034. g = int(g)
  1035. b = int(b)
  1036. self.rgb = [r,g,b]
  1037. return self.rgb
  1038. def _calc(self,a,b):
  1039. A = (self.pos[0]) #+self.block[0]
  1040. B = (self.pos[1]) #+self.block[1]
  1041. C = self.block[0]-a
  1042. D = self.block[1]-b
  1043. return A,B,C,D
  1044. def POS(self,x=None,y=None,a=0,b=0):
  1045. if type(x) is int:
  1046. self._x=x
  1047. if type(y) is int:
  1048. self._y=y
  1049. A,B,C,D = self._calc(a,b)
  1050. #if NR:
  1051. # C-=1
  1052. # D-=1
  1053. return [int(self._x+A),int(self._y+B),int(C),int(D)]
  1054. class POINTER():
  1055. def __init__(self):
  1056. self.pos = [0,0,0,0]
  1057. self.on = 0
  1058. self.rgb = [0,100,10]
  1059. self._x = 0
  1060. self._y = 0
  1061. self.x = 0
  1062. self.y = 0
  1063. self.fix = Fix(0 ,[999,999],[16,16],0,0,0)
  1064. def row_move(self,x,y):
  1065. self._x = x
  1066. self._y = y
  1067. def move(self,pos):
  1068. self.pos = pos
  1069. self.on = 1
  1070. def cross(self,x,y):
  1071. self.x = x
  1072. self.y = y
  1073. def draw(self,x,y):
  1074. pos = self.pos[:]
  1075. #print("draw",x,y,pos)
  1076. pos[0] += x
  1077. pos[1] += y
  1078. fix_x= self.fix.x
  1079. fix_y= self.fix.y +y
  1080. if 0:# self.on:
  1081. pygame.draw.rect(window,self.rgb,pos)
  1082. # mouse grid posision
  1083. fr = font15.render("{}/{}".format(fix_x+1,fix_y) ,1, (200,200,200))
  1084. _nr = fix_y * _x + fix_x +1
  1085. fr = font15.render("{:02}".format(_nr ) ,1, (200,200,200))
  1086. window.blit(fr,(pos[0]+2,pos[1]+2 ))
  1087. window.blit(fr,(130,1))
  1088. if 1:# lupe / looking-glass / lens
  1089. __t = 60
  1090. rgb=[255,0,0]
  1091. pos=[400,400,160,160]
  1092. draw_box_frame(window,rgb,pos,offset=1)
  1093. r_sub = grab(x=self._x-__t/2,y=self._y-__t/2,w=__t,h=__t)
  1094. if r_sub:
  1095. r_sub = pygame.transform.scale(r_sub,(160,160))
  1096. if r_sub: # target
  1097. window.blit(r_sub, (400,400))
  1098. #pos=[400+40,400+40,80,80]
  1099. pos=[400+80,400+80,80,80]
  1100. draw_box_frame(window,rgb,pos,offset=3)
  1101. draw_box_frame(window,rgb,pos,offset=2)
  1102. draw_box_frame(window,rgb,pos,offset=0)
  1103. draw_box_frame(window,rgb,pos,offset=1)
  1104. # fix pos
  1105. #txt=str(pos) #"[0, 0, 0, 0]"
  1106. #fr = font15.render(txt ,1, (200,200,200))
  1107. #window.blit(fr,(10,1))
  1108. # pointer
  1109. fr = font15.render("X:{:03}".format(self._x) ,1, (200,200,200))
  1110. window.blit(fr,(10,30))
  1111. fr = font15.render("Y:{:03}".format(self._y) ,1, (200,200,200))
  1112. window.blit(fr,(10,40))
  1113. if 1: #self._x > 30 and self._y > 230:
  1114. # pointer
  1115. _x = self._x-INPUT_START_POS_X
  1116. _y = self._y-INPUT_START_POS_Y
  1117. rgb = [200,200,200]
  1118. if _x < 0:
  1119. rgb = [200,0,0]
  1120. fr = font80.render("X:{:03}".format(_x) ,1, rgb)
  1121. window.blit(fr,(200,400))
  1122. rgb = [200,200,200]
  1123. if _y < 0:
  1124. rgb = [200,0,0]
  1125. fr = font80.render("Y:{:03}".format(_y) ,1, rgb)
  1126. window.blit(fr,(200,450))
  1127. p=110
  1128. # crosshair
  1129. self.rgb = [0,0,200]
  1130. pygame.draw.line(window,self.rgb, (self.x-p,self.y) , (self.x-2,self.y),2 )
  1131. pygame.draw.line(window,self.rgb, (self.x,self.y-p) , (self.x,self.y-2),2 )
  1132. self.rgb = [0,200,0]
  1133. pygame.draw.line(window,self.rgb, (self.x+2,self.y) , (self.x+p,self.y),2 )
  1134. pygame.draw.line(window,self.rgb, (self.x,self.y+2) , (self.x,self.y+p),2 )
  1135. self.rgb = [200,0,0]
  1136. pointer = POINTER()
  1137. NR = 0
  1138. running = True
  1139. def event():
  1140. global NR,running
  1141. for event in pygame.event.get():
  1142. #print(event.dict)
  1143. _button = None
  1144. if "button" in event.dict:
  1145. _button = event.dict["button"]
  1146. _state = None
  1147. if "state" in event.dict:
  1148. _state = event.state
  1149. _key = None
  1150. if "key" in event.dict:
  1151. _key = event.key
  1152. _pos = None
  1153. if "pos" in event.dict:
  1154. _pos = event.pos
  1155. _type = None
  1156. if "type" in event.dict:
  1157. _type = event.type
  1158. _type = event.type
  1159. _mod = None
  1160. if "mod" in event.dict:
  1161. _mod = event.mod
  1162. if 0:
  1163. print( " ")
  1164. print( "{:.02f}".format( time.time() - START ))
  1165. print("button -",_button,end="\t| ")
  1166. #print("state -",_state)
  1167. print("pos -",_pos)
  1168. print("type -",_type, end="\t| ")
  1169. print("key -",_key)
  1170. print("mod -",_mod)
  1171. try:
  1172. if _type in [5,1025]:
  1173. if _button == 1:
  1174. NR += 1
  1175. if NR > 1:
  1176. NR = 0
  1177. if _button == 3:
  1178. NR -= 1
  1179. if NR < 0:
  1180. NR = 1
  1181. if _pos:
  1182. posA = _pos
  1183. fix = find_pix(_pos[0]-40,_pos[1]-60+pm_wy)
  1184. if fix:
  1185. #pos = fix.POS(40,60+pm_wy)
  1186. pos = fix.POS() #(CFG_IN["x1"],CFG_IN["y1"])#0,0) #+pm_wy)
  1187. rgb = [0,0,0]
  1188. pointer.move(pos)
  1189. pointer.fix = fix
  1190. else:
  1191. pointer.on = 0
  1192. pointer.row_move(_pos[0],_pos[1])
  1193. pointer.cross(_pos[0],_pos[1])
  1194. if event.type == pygame.VIDEORESIZE:
  1195. window = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)
  1196. except Exception as e:
  1197. cprint(e,color="red")
  1198. if event.type==pygame.QUIT:
  1199. global QUIT
  1200. QUIT = 1
  1201. cprint("QUIT",color="red")
  1202. running=False
  1203. #time.sleep(1)
  1204. #sys.exit()
  1205. fps = 0
  1206. fps2 = 0
  1207. frame = 0
  1208. frame2 = 0
  1209. frame_t = time.time()
  1210. frame2_t = time.time()
  1211. IP = "yyy"
  1212. vplay1=0
  1213. vplay2=0
  1214. if options.videoplayer:
  1215. try:
  1216. t=options.videoplayer.split(",")
  1217. vplay1=t[0]
  1218. vplay2=t[1]
  1219. except:pass
  1220. def output_mask(): # oben
  1221. RGB = [0,0,50]
  1222. #RGB = [120,0,0] # test
  1223. start = 0
  1224. # oben
  1225. pygame.draw.rect(window,RGB,[0,start,MAIN_SIZE[0]+200,57]) # background
  1226. # unten
  1227. pygame.draw.rect(window,RGB,[0,start+192,MAIN_SIZE[0]+200,35]) # background
  1228. # links
  1229. pygame.draw.rect(window,RGB,[0,start+0,35,235]) # background
  1230. def input_mask(): # unten
  1231. RGB = [0,0,50]
  1232. #RGB = [120,0,110] # test
  1233. start = 227
  1234. # oben
  1235. pygame.draw.rect(window,RGB,[0,start,MAIN_SIZE[0]+200,7]) # background
  1236. # links
  1237. pygame.draw.rect(window,RGB,[0,start,35,200]) # background
  1238. # unten
  1239. pygame.draw.rect(window,RGB,[0,start+173,MAIN_SIZE[0]+200,198]) # background
  1240. def draw_main_overlay():
  1241. global fps,fps2
  1242. fr = font15.render("DMX-FPS: {}".format(fps) ,1, (200,0,255))
  1243. window.blit(fr,(10,2))
  1244. fr = font15.render("GUI-FPS : {}".format(fps2) ,1, (200,0,255))
  1245. window.blit(fr,(10,12))
  1246. #fr = font15.render("ip:{}".format(IP) ,1, (200,0,255))
  1247. #window.blit(fr,(100,2))
  1248. fr = font15.render("start-uni: {:}.xx dRGB".format(START_UNIV) ,1, (200,0,255))
  1249. window.blit(fr,(90,2))
  1250. fr = font15.render("a1_idim: {}.{:}".format(START_UNIV_A,options.grid_a1_idim) ,1, (200,0,255))
  1251. window.blit(fr,(90,12))
  1252. fr = font15.render("a2_idim: {}.{:}".format(START_UNIV_A,options.grid_a2_idim) ,1, (200,0,255))
  1253. window.blit(fr,(90,22))
  1254. fr = font15.render("gobo_ch1: {}.{:}".format(START_UNIV_A,options.gobo_ch) ,1, (200,0,255))
  1255. window.blit(fr,(180,12))
  1256. fr = font15.render("gobo_ch2: {}.{:}".format(START_UNIV_A,options.gobo_ch2) ,1, (200,0,255))
  1257. window.blit(fr,(180,22))
  1258. fr = font15.render("v-play1: {}.{:}".format(START_UNIV_A,vplay1) ,1, (200,0,255))
  1259. window.blit(fr,(270,12))
  1260. fr = font15.render("v-play2: {}.{:}".format(START_UNIV_A,vplay2) ,1, (200,0,255))
  1261. window.blit(fr,(270,22))
  1262. try:
  1263. global pointer
  1264. # pointer
  1265. fr = font15.render("X:{:03}".format(pointer._x) ,1, (200,200,200))
  1266. window.blit(fr,(10,30))
  1267. fr = font15.render("Y:{:03}".format(pointer._y) ,1, (200,200,200))
  1268. window.blit(fr,(10,40))
  1269. except Exception as e:
  1270. print("Overlay err:",e)
  1271. def draw_box_frame(window,rgb,pos,offset=0):
  1272. p1 = [pos[0],pos[1]]
  1273. p2 = [pos[0]+pos[2],pos[1]+pos[3]]
  1274. o = offset
  1275. _p1 = [p1[0]-o,p1[1]-o]
  1276. _p2 = [p1[0]-o,p2[1]+o]
  1277. pygame.draw.line(window,rgb,_p1,_p2) # left
  1278. _p1 = [p2[0]+o,p2[1]+o]
  1279. pygame.draw.line(window,rgb,_p1,_p2) # bottom
  1280. _p2 = [p2[0]+o,p1[1]-o]
  1281. pygame.draw.line(window,rgb,_p1,_p2) # right
  1282. _p1 = [p1[0]-o,p1[1]-o]
  1283. pygame.draw.line(window,rgb,_p1,_p2) # top
  1284. def draw_frame(window,rgb,p1,p2,offset=0):
  1285. o = offset
  1286. _p1 = [p1[0]-o,p1[1]-o]
  1287. _p2 = [p1[0]-o,p2[1]+o]
  1288. #pygame.draw.line(window,rgb,_p1,_p2) # left
  1289. _p1 = [p2[0]+o,p2[1]+o]
  1290. pygame.draw.line(window,rgb,_p1,_p2) # bottom
  1291. _p2 = [p2[0]+o,p1[1]-o]
  1292. #pygame.draw.line(window,rgb,_p1,_p2) # right
  1293. _p1 = [p1[0]-o,p1[1]-o]
  1294. pygame.draw.line(window,rgb,_p1,_p2) # top
  1295. def calc_fps():
  1296. global fps,frame,frame_t
  1297. t = time.time()
  1298. if frame_t+1 < t:
  1299. fps = frame #frame_t- t #frame
  1300. frame = 1
  1301. frame_t = time.time()
  1302. def calc_fps2():
  1303. global fps2,frame2,frame2_t
  1304. t = time.time()
  1305. if frame2_t+0.1 < t:
  1306. fps2 = frame2*10 #frame_t- t #frame
  1307. frame2 = 1
  1308. frame2_t = t #time.time()
  1309. TEXT_BLOCK = []
  1310. def _create_text_block():
  1311. print("======== CREATE NEW TEXT FILE !!",text_file)
  1312. f = open(text_file,"w")
  1313. for i in range(10):
  1314. f.write("TEXT {}\n".format(i+1))
  1315. f.close()
  1316. def open_text_block():
  1317. print("======== OPEN TEXT FILE !!",text_file)
  1318. _lines = []
  1319. try:
  1320. f = open(text_file,"r")
  1321. _lines = f.readlines()
  1322. f.close()
  1323. except FileNotFoundError as e:
  1324. print("TEXT",e)
  1325. _create_text_block()
  1326. if len(_lines) <= 0:
  1327. _create_text_block()
  1328. lines = []
  1329. for l in _lines:
  1330. #print(">> ",l.strip())
  1331. lines.append(l.strip())
  1332. if len(lines) <= 10:
  1333. for i in range(10-len(lines)):
  1334. lines.append("LINE ERROR")
  1335. return lines
  1336. TEXT_BLOCK = open_text_block()
  1337. TEXT_BLOCK_TIME = time.time()
  1338. # video medialist
  1339. MEDIALIST = []
  1340. def open_medialist():
  1341. print("======== OPEN MEDIALIST DIR !!",media_list)
  1342. if not os.path.isdir(media_list):
  1343. os.system("mkdir -p {}".format(media_list))
  1344. _lines = os.listdir(media_list)
  1345. _lines.sort()
  1346. lines = ['']*25
  1347. i=0
  1348. for l in _lines:
  1349. l = l.strip()
  1350. if "_" in l:
  1351. ll = l.split("_",1)
  1352. print(">> ",ll)
  1353. try:
  1354. lll = int(ll[0])
  1355. lines[lll] = l
  1356. except:pass
  1357. if len(lines) <= 10:
  1358. for i in range(10-len(lines)):
  1359. lines.append("")#"LINE ERROR")
  1360. return lines
  1361. MEDIALIST_TIME = time.time()
  1362. MEDIALIST = open_medialist()
  1363. # ===== GUI =========
  1364. def draw_circle(surface,color, pos, radius):
  1365. x,y=pos
  1366. pygame.gfxdraw.aacircle(surface, int(x), int(y), radius-1, color)
  1367. pygame.gfxdraw.filled_circle(surface, int(x), int(y), radius-1, color)
  1368. def rDMX(univ,dmx):
  1369. return univ*512+dmx
  1370. def generate_grid(mapping=0):
  1371. _log = []
  1372. #if PIXEL_MAPPING:
  1373. # log = open(grid_file,"w")
  1374. head = "i,univ,dmx,x,y,ch\n"
  1375. head = "i,univ,dmx,ch\n"
  1376. head = "univ,dmx,x,y,ch\n"
  1377. head = "nr,id,info\n"
  1378. print("csv:",head)
  1379. _log.append(head)
  1380. dmx = 1-1
  1381. ch = 4
  1382. y=0
  1383. x=0
  1384. for i in range((_y)*(_x)):
  1385. #if x > _x and i%_x == 0:
  1386. if x > 8 and i%8 == 0:
  1387. #print("--> -->")
  1388. x=0
  1389. y+=1
  1390. _univ = int(dmx/512)
  1391. _dmx = dmx - (_univ)*512
  1392. pos=[x,y]
  1393. line="{},{},x\n".format(i+1,i+1)
  1394. _log.append(line)
  1395. dmx += ch
  1396. x+=1
  1397. if mapping and PIXEL_MAPPING:
  1398. print("CREATE NEW PIXELMAP FILE !!",grid_file)
  1399. log = open(grid_file,"w")
  1400. log.writelines(_log)
  1401. log.close()
  1402. return _log[:] #GRID
  1403. def init_grid(mapping=0,_x=4,_y=4,_xp=0,_yp=0,start=0,name="init_gird"):
  1404. if mapping and PIXEL_MAPPING:
  1405. try:
  1406. log = open(grid_file,"r")
  1407. except:
  1408. generate_grid(mapping=mapping)
  1409. log = open(grid_file,"r")
  1410. lines = log.readlines()
  1411. else:
  1412. lines = generate_grid()
  1413. GRID = []
  1414. y=0
  1415. x=0
  1416. print("CSV header",[lines[0]],[PIXEL_MAPPING,"len:",len(lines),"start:",start,name])
  1417. for i,line in enumerate(lines[1+start:]): #exclude first line
  1418. #print("rcsv",[line])
  1419. line = line.strip()
  1420. line = line.split(",") # csv
  1421. if i >= _x and i%_x == 0:
  1422. x=0
  1423. y+=1
  1424. if y >= _y:
  1425. break
  1426. _id = int(line[1])
  1427. pos = [x+_xp,y+_yp]
  1428. f = Fix(_id,pos,block) #pos,univ,dmx,ch)
  1429. GRID.append(f)
  1430. x+=1
  1431. return GRID
  1432. def find_pix(x,y):
  1433. global GRID
  1434. for fix in GRID:
  1435. X = 0
  1436. Y = 0
  1437. pos = fix.POS()
  1438. if x > pos[0] and x < pos[0]+pos[2]:
  1439. X = 1
  1440. if y > pos[1] and y < pos[1]+pos[3]:
  1441. Y = 1
  1442. if X and Y:
  1443. return fix
  1444. GRID = []
  1445. GRID_A = []
  1446. NR = 0
  1447. START_UNIV_A=1
  1448. START_UNIV=2
  1449. if options.start_univ:
  1450. try:
  1451. START_UNIV=int(options.start_univ)
  1452. except Exception as e:
  1453. print("Exception START UNIV",e)
  1454. gobo_ch=1
  1455. if options.gobo_ch:
  1456. try:
  1457. gobo_ch=int(options.gobo_ch)
  1458. except Exception as e:
  1459. print("Exception gobo_ch",e)
  1460. if gobo_ch <= 0:
  1461. gobo_ch = 1
  1462. def draw_box(pos1,pos2,color=[128,128,128],text=1):
  1463. color = [200,0,0,127]
  1464. if text:
  1465. fr = font15.render("A" ,1, (200,200,200))
  1466. window.blit(fr,pos1)
  1467. fr = font15.render("B" ,1, (200,200,200))
  1468. window.blit(fr,[pos2[0]-10,pos2[1]-10])
  1469. # h unten
  1470. _pos1 = [pos1[0],pos2[1]]
  1471. _pos2 = [pos2[0],pos2[1]]
  1472. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1473. color = [255,255,0,127]
  1474. # h rechts
  1475. _pos1 = [pos2[0],pos1[1]]
  1476. _pos2 = [pos2[0],pos2[1]]
  1477. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1478. color = [0,200,0,127]
  1479. # h links
  1480. _pos1 = [pos1[0],pos1[1]]
  1481. _pos2 = [pos1[0],pos2[1]]
  1482. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1483. color = [0,0,200,127]
  1484. # h oben
  1485. _pos1 = [pos1[0],pos1[1]]
  1486. _pos2 = [pos2[0],pos1[1]]
  1487. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1488. def grab(x=55,y=55,w=60,h=60):
  1489. crop = None
  1490. rect = pygame.Rect(x, y, w, h)
  1491. try:
  1492. sub = window.subsurface(rect)
  1493. crop = pygame.Surface((w,h))
  1494. crop.blit(sub, (0,0))
  1495. except ValueError as e:
  1496. pass
  1497. return crop
  1498. vpu_wall_txt="""---,quelle,,---,ziel,---,---
  1499. out,x,y,ang,row,col,info
  1500. Wall1,0,0,0,1,1,
  1501. Wall1,0,0,0,2,2,
  1502. Wall1,0,0,0,3,3,
  1503. Wall1,0,0,0,4,4,
  1504. Wall1,0,0,0,5,5,
  1505. Wall1,0,0,0,6,6,
  1506. Wall1,0,0,0,7,7,
  1507. ,,,,,,
  1508. Wall2,0,0,0,4,1,----
  1509. Wall2,0,0,0,5,2,
  1510. Wall2,4,4,0,6,3,
  1511. Wall2,0,0,0,7,4,
  1512. Wall2,0,0,0,8,5,
  1513. Wall2,0,0,0,9,6,
  1514. ,,,,,,
  1515. ,,,,,,
  1516. Wall3,64,48,0,12,1,----
  1517. Wall3,64,64,0,12,2,
  1518. Wall3,64,80,0,12,3,
  1519. Wall3,64,96,0,12,4,
  1520. Wall3,64,112,0,12,5,
  1521. Wall3,64,128,0,12,6,"""
  1522. def create_vpu_wall_conf():
  1523. f=open(vpu_wall_conf_file,"w")
  1524. f.write(vpu_wall_txt)
  1525. f.close()
  1526. print("CREATE VPU CONFIG !")
  1527. wall_data = []
  1528. _last_wall_config_time = time.time()
  1529. def read_wall_pix(init=0):
  1530. global _last_wall_config_time
  1531. global wall_data, NR
  1532. grid = []
  1533. #if not NR and init==0:
  1534. # return
  1535. ok = 0
  1536. if init:
  1537. ok = 1
  1538. if NR:
  1539. if time.time()-_last_wall_config_time > 3:
  1540. ok = 1
  1541. else:
  1542. if time.time()-_last_wall_config_time > 10:
  1543. ok = 1
  1544. if not ok:
  1545. return
  1546. _last_wall_config_time = time.time()
  1547. if not os.path.isfile(vpu_wall_conf_file):
  1548. create_vpu_wall_conf()
  1549. f=open(vpu_wall_conf_file,"r")
  1550. lines = f.readlines()
  1551. for i,line in enumerate(lines):
  1552. line = line.strip()
  1553. if not line:
  1554. continue
  1555. line = line.split(",")
  1556. if not line[0]:
  1557. continue
  1558. #print(i,line)
  1559. if len(line) >= 3:
  1560. _l = []
  1561. for i in line:
  1562. try:
  1563. _l.append(int(i))
  1564. except Exception as e:
  1565. _l.append(i)
  1566. grid.append(_l)
  1567. for i,v in enumerate(grid):
  1568. print(i,v)
  1569. wall_data = grid
  1570. read_wall_pix(init=1)
  1571. def reshape2(GRID,GRID_OUT,_x,_y,name="GRID_Z"):
  1572. _w = 16
  1573. _h = 16
  1574. from_x_min = INPUT_START_POS_X
  1575. from_y_min = INPUT_START_POS_Y
  1576. target_x = 40 # min
  1577. target_y = 60 # min
  1578. ang=0
  1579. #read_block_pix()
  1580. read_wall_pix()
  1581. _wall_data = wall_data[:]
  1582. for i,_wall in enumerate(_wall_data):
  1583. #print(_wall)
  1584. if len(_wall) <=6:
  1585. continue
  1586. try:
  1587. if type(_wall[1]) is str:
  1588. continue
  1589. if type(_wall[2]) is str:
  1590. continue
  1591. if type(_wall[3]) is str:
  1592. continue
  1593. if type(_wall[4]) is str:
  1594. continue
  1595. if type(_wall[5]) is str:
  1596. continue
  1597. from_x=from_x_min+_wall[1]#*_w
  1598. from_y=from_y_min+_wall[2]#*_h
  1599. _a= _wall[3]
  1600. ang=0
  1601. if _a < 0:
  1602. ang=0
  1603. elif _a > 0 and _a <= 90:
  1604. ang = 90
  1605. elif _a > 90 and _a <= 180:
  1606. ang = 180
  1607. elif _a > 180 and _a <= 270:
  1608. ang = 270
  1609. target_x=40+(_wall[4]-1)*_w
  1610. target_y=60+(_wall[5]-1)*_h
  1611. sub = grab(from_x,from_y,_w,_h)
  1612. #draw_box_frame(window,[0,40,0],[from_x+x,from_y+y,_w,_h])
  1613. if sub:
  1614. sub = pygame.transform.rotate(sub,ang)
  1615. window.blit(sub,(target_x,target_y))
  1616. except Exception as e:
  1617. print(" ",_wall)
  1618. print("WALL err:",e)
  1619. def reshape(GRID,GRID_OUT,_x,_y,name="GRID_Z"):
  1620. """reshape LED-WALL Block/Pixel mapping"""
  1621. if PIXEL_MAPPING <= 0:
  1622. return None
  1623. x = _x
  1624. y = _y
  1625. i = 0
  1626. counter = 0
  1627. z=0
  1628. x_min = 99999
  1629. x_max = 0
  1630. y_min = 99999
  1631. y_max = 0
  1632. pos= [0,0]
  1633. pos[0] = _x+p*3 #+10
  1634. pos[1] = p*8+65 #-10 #_y+p*8+80
  1635. tmp_font = pygame.font.SysFont("freemonobold",int(p*0.8))
  1636. #fr = tmp_font.render("OUTPUT: "+str(name) ,1, (255,255,255))
  1637. #fr_r = fr.get_rect(center=(int(wx/2),int(0+pm_wy-p*0-10)))
  1638. #window.blit(fr,pos)
  1639. fr = tmp_font.render("INPUT" ,1, (255,255,255))
  1640. fr_r = fr.get_rect(center=(int(wx/2),int(60+pm_wy-p/2)))
  1641. window.blit(fr,fr_r)
  1642. j = 0
  1643. for fix in GRID_OUT:
  1644. if j >= 64:
  1645. break
  1646. j+=1
  1647. ii = i
  1648. #pos = fix.POS(40,60)
  1649. pos = fix.POS() #(0,0)
  1650. rgb = fix.rgb
  1651. pos[0]+=_x
  1652. pos[1]+=_y
  1653. # green
  1654. pygame.draw.rect(window,[0,40,0],pos)
  1655. xposs = [] #None #pos[:]
  1656. for fix2 in GRID:
  1657. if fix._id == fix2._id:
  1658. #_xposs = fix2.POS(40,60)
  1659. #_xposs = fix2.POS(0,0)#_x,_y)
  1660. _xposs = fix2.POS() #(CFG_IN["x1"],CFG_IN["y1"])
  1661. xposs.append( _xposs) #fix2.POS(40,60) )
  1662. # ToDo
  1663. #sub = grab(10,10,60,60)
  1664. #sub = pygame.transform.rotate(sub,90)
  1665. #wn.blit(sub, (500,10))
  1666. for xpos in xposs:
  1667. #sub = grab(xpos[0],xpos[1]+pm_wy,xpos[2],xpos[3])
  1668. sub = grab(xpos[0],xpos[1],xpos[2],xpos[3])
  1669. if NR:
  1670. pygame.draw.rect(window,[255,0,255],xpos,1) # frame on grab area
  1671. if sub:
  1672. window.blit(sub, (pos[0]+z,pos[1]+z))
  1673. else:
  1674. # red
  1675. pygame.draw.rect(window,[40,0,0],pos) #[x+pos[0]+2+z,y+pos[1]+2+z-pm_wy,12,9])
  1676. if xpos[0] < x_min:
  1677. x_min = xpos[0]
  1678. if xpos[0] > x_max:
  1679. x_max += xpos[2]
  1680. if xpos[1] < y_min:
  1681. y_min = xpos[1]
  1682. if xpos[1] > x_max:
  1683. y_max += xpos[3]
  1684. # DRAW FIX NUMBER on TOP
  1685. #apos = fix.POS(40,60)#+pm_wy)
  1686. apos = fix.POS() #0,0)#+pm_wy)
  1687. apos[0]+=_x
  1688. apos[1]+=_y
  1689. argb = fix.rgb
  1690. # overwrite number overlay
  1691. if NR:
  1692. pygame.draw.rect(window,[20,20,20],[apos[0],apos[1],12,8])
  1693. fix_id = fix._id
  1694. if fix_id > 8*8:
  1695. fix_id -= 8*8
  1696. if NR:# == 2: # sub fix_nr
  1697. if fix_id != i+1:
  1698. fr = font12.render("{:02}".format(fix._id) ,1, (255,255,0))
  1699. window.blit(fr,(apos[0],apos[1]))
  1700. else:
  1701. fr = font12.render("{:02}".format(fix._id) ,1, (100,100,255))
  1702. window.blit(fr,(apos[0],apos[1]))
  1703. i += 1
  1704. #print("--#")
  1705. # frame box
  1706. pos1= [x+x_min,y+x_min]
  1707. pos2= [x_max+x_min,y_min+y_max]
  1708. class Timer():
  1709. def __init__(self,start=120):
  1710. self.start = start
  1711. self.timer = self.start
  1712. self.timer_t = time.time()
  1713. def reset(self):
  1714. self.timer = self.start
  1715. def get(self):
  1716. self.timer -= time.time()-self.timer_t
  1717. self.timer_t = time.time()
  1718. if self.timer <= 0:
  1719. self.reset()
  1720. return self.timer
  1721. def reload_grid():
  1722. print("==== reload_grid")
  1723. global GRID
  1724. global GRID_A
  1725. try:
  1726. GRID = init_grid(_x=_x,_y=_y,name="GRID") #init_gird()
  1727. GRID_A = init_grid(_x=8,_y=16,mapping=1,name="GRID_A") #init_gird()
  1728. except Exception as e:
  1729. print("Except: grid re init",e)
  1730. VPU_TEXT = []
  1731. def load_vpu_text(nr=0):
  1732. txt = "NONE"
  1733. if len(VPU_TEXT) > nr:
  1734. txt = VPU_TEXT[0]
  1735. return txt
  1736. import string
  1737. def draw_kachel_nr(): #GRID,_x=0,_y=0):
  1738. if not NR:
  1739. return
  1740. n = 0
  1741. _max_row = 40
  1742. _y=0
  1743. for i in range(8): # ROW / ZEILEN -> ->
  1744. _x=0
  1745. for j in range(30): # COLUMS / SPALTEN | |
  1746. pygame.draw.rect(window,[10,20,10],[40+_x+2,60+_y+2,14,9]) #background
  1747. _j=j #string.ascii_uppercase[j]
  1748. fr = font12.render("{}:{}".format(_j+1,i+1) ,1, (200,200,55))
  1749. window.blit(fr,(40+_x+2,60+_y+2))
  1750. #fr = font12.render("{}".format(_j) ,1, (200,200,55))
  1751. #window.blit(fr,(40+_x+2,60+_y+2))
  1752. #fr = font12.render(":{}".format(i+1) ,1, (200,200,55))
  1753. #window.blit(fr,(40+_x+2+6,60+_y+2))
  1754. _x+=16 # pix
  1755. _y +=16 # pix
  1756. grid_counter = time.time()
  1757. def draw_counter(COUNTER):
  1758. for count in COUNTER:
  1759. cpan = 0
  1760. ctilt = 0
  1761. cr=255
  1762. cg=255
  1763. cb=255
  1764. csize=10
  1765. cdim=0
  1766. k = "DIM"
  1767. if k in count:
  1768. cdim = int(count[k])
  1769. k = "RED"
  1770. if k in count:
  1771. cr = int(count[k])
  1772. k = "GREEN"
  1773. if k in count:
  1774. cg = int(count[k])
  1775. k = "BLUE"
  1776. if k in count:
  1777. cb = int(count[k])
  1778. k = "SIZE"
  1779. if k in count:
  1780. csize = int(count[k]) #/60*p)
  1781. if csize < 5:
  1782. csize = 5
  1783. k = "PAN"
  1784. if k in count:
  1785. cpan = int(count[k])/255*(block[0] *(_x))
  1786. #cpan = int(cpan * 1.2)
  1787. cpan = int(cpan)
  1788. k = "TILT"
  1789. if k in count:
  1790. ctilt = int(count[k])/255*(block[1] *(_y))
  1791. #ctilt = int(cpan * 1.2)
  1792. ctilt = int(ctilt)
  1793. ddim = cdim/255
  1794. if "DIM" in count and count["DIM"] > 0:
  1795. tmp_font = pygame.font.SysFont("freemonobold",int(p/100*csize))
  1796. rgb =(int(cr*ddim),int(cg*ddim),int(cb*ddim),cdim)
  1797. _sec = count["_SEC"]
  1798. try:
  1799. _sec = int(count["_SEC"])
  1800. except:
  1801. pass
  1802. if type(_sec) is int:
  1803. #print(_sec)
  1804. if _sec <= 60:
  1805. fr = tmp_font.render("{:0}".format(_sec) ,1, rgb)
  1806. else:
  1807. _xx = time.strftime("%M:%S",time.localtime(_sec))
  1808. #print("_xx",_xx)
  1809. fr = tmp_font.render("{}".format(_xx) ,1, rgb)
  1810. else:
  1811. fr = tmp_font.render("{}".format((count["_SEC"])) ,1, rgb)
  1812. fr_r = fr.get_rect(center=(40+cpan-(block[0]),60+ctilt+pm_wy))
  1813. pygame.draw.rect(window,[0,0,0],fr_r)
  1814. window.blit(fr,fr_r)
  1815. class FADE():
  1816. def __init__(self,sec):
  1817. self.start = time.time()
  1818. self.sec = sec
  1819. self.val
  1820. def next(self):
  1821. pass
  1822. def video_overlay_draw(wn=None,lines=["line1","line2"],i=0):
  1823. # overlay
  1824. pygame.draw.rect(wn,[255,200,0],[5,MAIN_SIZE[1]-(i+1)*35,380,28])
  1825. font15 = pygame.font.SysFont("freemonobold",17)
  1826. rgb = [255,100,0]
  1827. pygame.draw.rect(wn,rgb,[5,MAIN_SIZE[1]-(i+1)*35,200,13])
  1828. _line = lines[0]
  1829. fr = font15.render(_line ,1, (0,0,0))
  1830. wn.blit(fr,(10,MAIN_SIZE[1]-(i+1)*35))
  1831. _line = lines[1]
  1832. fr = font15.render(_line ,1, (0,0,0))
  1833. wn.blit(fr,(10,MAIN_SIZE[1]-(i+1)*35+15))
  1834. #_line = lines[2]
  1835. #fr = font15.render(_line ,1, (0,0,0))
  1836. #wn.blit(fr,(75,MAIN_SIZE[1]-(i+1)*35+15))
  1837. def draw_all_video_overlay(VIDEO):
  1838. global videplayer
  1839. i=0
  1840. for count in VIDEO:
  1841. video1 = videoplayer[i]
  1842. #video1.overlay_draw(window,"run") # old
  1843. lines = video1._overlay_text()
  1844. video_overlay_draw(wn=window,lines=lines,i=i)
  1845. i += 1
  1846. def del_all_video():
  1847. global videplayer
  1848. i = 0
  1849. print()
  1850. cprint("del_all_video")
  1851. for video in videoplayer:
  1852. cprint("- del video",video)
  1853. video._del()
  1854. def draw_all_video(VIDEO):
  1855. global videplayer
  1856. i = 0
  1857. # set DMX-VALUE to videoplayer Object
  1858. for count in VIDEO:
  1859. cpan = 0
  1860. ctilt = 0
  1861. cr=255
  1862. cg=255
  1863. cb=255
  1864. csize=10
  1865. cang=0
  1866. cdim=0
  1867. video1 = videoplayer[i]
  1868. k = "VIDEO"
  1869. if k in count:
  1870. #video1.select_video(count[k])
  1871. play_nr = int(count[k]/10)
  1872. if play_nr != video1._media_nr:
  1873. print( "+ + + + + + + + + + +", play_nr , video1._media_nr ,str(video1))
  1874. _vid = video1._id
  1875. _cdmx = video1.dmx
  1876. del_video1 = video1
  1877. video1 = PixelMedia(cdmx,_id=_vid)
  1878. video1.select_video(count[k])
  1879. videoplayer[i] = video1
  1880. del_video1._del()
  1881. del del_video1
  1882. k = "DIM"
  1883. if k in count:
  1884. cdim = int(count[k])
  1885. #video1.dim = cdim
  1886. #if i == 0:
  1887. # print(i,cdim)
  1888. k = "ANG"
  1889. if k in count:
  1890. cang = int(count[k])
  1891. k = "SIZE"
  1892. if k in count:
  1893. csize = int(count[k]/16*p)
  1894. if csize < 5:
  1895. csize = 5
  1896. k = "PAN"
  1897. cpan_max = block[0] *(_x) #+block[0]
  1898. if k in count:
  1899. #if 0:#video1.fname.startswith("cam_"):
  1900. # cpan = int(count[k]) #/ 255*cpan_max
  1901. #else:
  1902. cpan = int(count[k]) / 255*cpan_max
  1903. cpan = int(cpan)
  1904. k = "TILT"
  1905. ctilt_max = block[1] *(_y) #+block[1]
  1906. if k in count:
  1907. ctilt = int(count[k]) / 255*ctilt_max
  1908. ctilt = int(ctilt)
  1909. k = "_reset"
  1910. if k in count:
  1911. if count[k]:
  1912. count[k] = 0
  1913. video1.restart()
  1914. k = "_RUN"
  1915. if k in count:
  1916. video1._run = count[k]
  1917. #video1.pos
  1918. #video1.x=40+0+cpan
  1919. #video1.y=60+0+pm_wy+ctilt
  1920. x=CFG_IN["x1"]+cpan
  1921. y=CFG_IN["y1"]+ctilt
  1922. scale = int((csize))
  1923. angle = int(360-(cang))
  1924. video1.update_dmx(x,y,scale,angle,cdim)
  1925. if cdim:
  1926. video1.next()
  1927. i += 1
  1928. # draw
  1929. i=0
  1930. for count in VIDEO:
  1931. video1 = videoplayer[i]
  1932. video1.draw(window) #,x=0,y=0)
  1933. i+=1
  1934. def counter_dmx(COUNTER,dataA):
  1935. for count in COUNTER:
  1936. cDMX=count["DMX"]-1
  1937. try:
  1938. count["DIM"] = dataA[cDMX]
  1939. count["PAN"] = dataA[cDMX+1]
  1940. count["TILT"] = dataA[cDMX+2]
  1941. count["CONTROL"] = dataA[cDMX+3]
  1942. count["SIZE"] = dataA[cDMX+4]
  1943. count["SEC"] = dataA[cDMX+5]
  1944. count["RED"] = dataA[cDMX+6]
  1945. count["GREEN"] = dataA[cDMX+7]
  1946. count["BLUE"] = dataA[cDMX+8]
  1947. if count["CONTROL"] >= 10 and count["CONTROL"] < 20: # RESET COUNTER
  1948. count["_RUN"] = 0
  1949. count["_pause"] = 0
  1950. count["_time"] = time.time()
  1951. count["_start"] = int(count["SEC"])
  1952. count["_SEC"] = int(count["_start"])
  1953. if count["CONTROL"] >= 20 and count["CONTROL"] < 30: # PAUSE COUNTER
  1954. count["_RUN"] = 0
  1955. if count["CONTROL"] >= 30 and count["CONTROL"] < 40: # PLAY COUNTER
  1956. count["_RUN"] = 1
  1957. if count["_RUN"]:
  1958. try:
  1959. if count["_pause"] > 0:
  1960. count["_time"] += time.time()-count["_pause"]
  1961. count["_pause"] = 0
  1962. count["_SEC"] = int(count["_start"] - (time.time() - count["_time"]))
  1963. except Exception as e:
  1964. pass
  1965. else:
  1966. if count["_pause"] == 0:
  1967. count["_pause"] = time.time()
  1968. if type(count["_SEC"]) is int:
  1969. if count["_SEC"] < 0:
  1970. count["_SEC"] = 0
  1971. for ti in range(10):
  1972. #print(ti,(ti+6)*10)
  1973. if count["CONTROL"] >= (ti+6)*10 and count["CONTROL"] < (ti+7)*10:
  1974. count["_SEC"] = "----" #text 1
  1975. try:
  1976. count["_SEC"] = TEXT_BLOCK[ti]
  1977. except Exception as e:
  1978. pass
  1979. if count["CONTROL"] >= 250 and count["CONTROL"] < 256:
  1980. count["_SEC"] = "DMX:{}-{}".format(cDMX+1,cDMX+8+1)
  1981. except Exception as e:
  1982. print("EXC FUNC",e,count)
  1983. #print(count)
  1984. def video_dmx(VIDEO,dataA):
  1985. for count in VIDEO:
  1986. cDMX=count["DMX"]-1
  1987. try:
  1988. count["DIM"] = dataA[cDMX]
  1989. count["PAN"] = dataA[cDMX+1]
  1990. count["TILT"] = dataA[cDMX+2]
  1991. count["CONTROL"] = dataA[cDMX+3]
  1992. if count["CONTROL"] >= 10 and count["CONTROL"] < 20:
  1993. count["_reset"] = 1
  1994. if count["CONTROL"] >= 20 and count["CONTROL"] < 30:
  1995. if count["_RUN"] == 1:
  1996. print( "_RUN:0",count["DMX"]-1)
  1997. count["_RUN"] = 0
  1998. if count["CONTROL"] >= 30 and count["CONTROL"] < 40:
  1999. if count["_RUN"] == 0:
  2000. print( "_RUN:1",count["DMX"]-1)
  2001. count["_RUN"] = 1
  2002. count["SIZE"] = dataA[cDMX+4]#*2
  2003. count["SEC"] = dataA[cDMX+5]
  2004. #count["RED"] = dataA[cDMX+6]
  2005. count["VIDEO"] = dataA[cDMX+6]
  2006. #count["GREEN"] = dataA[cDMX+7]
  2007. count["ANG"] = (int(dataA[cDMX+7])/255*360)
  2008. #count["BLUE"] = dataA[cDMX+8]
  2009. except Exception as e:
  2010. print("VIDEOPLAYER EXCEPT FUNC",e,count)
  2011. #print(count)
  2012. def read_dmx_data(ip,ips):
  2013. ip = select_ip(ips,univ=START_UNIV)
  2014. IP = ip
  2015. data = read_dmx(ip)
  2016. ip = select_ip(ips,univ=START_UNIV+1)
  2017. data3 = read_dmx(ip)
  2018. data.extend(data3)
  2019. ip = select_ip(ips,univ=START_UNIV+2)
  2020. data3 = read_dmx(ip)
  2021. data.extend(data3)
  2022. ip = select_ip(ips,univ=START_UNIV+3)
  2023. data3 = read_dmx(ip)
  2024. data.extend(data3)
  2025. if options.dual_vpu:
  2026. ip = select_ip(ips,univ=START_UNIV+4)
  2027. data3 = read_dmx(ip)
  2028. data.extend(data3)
  2029. ip = select_ip(ips,univ=START_UNIV+5)
  2030. data3 = read_dmx(ip)
  2031. data.extend(data3)
  2032. return data
  2033. def set_pos(GRID,_x=0,_y=0):
  2034. for fix in GRID:
  2035. pos = fix.POS(_x,_y)#40,60+pm_wy)
  2036. for subfix in fix.sub_fix:
  2037. #subfix.dmc_calc(data)
  2038. spos = subfix.POS(_x,_y)
  2039. def draw_gobo(GRID,data,_x=0,_y=0):
  2040. i = 0
  2041. dmx = 1
  2042. h = 1
  2043. v = 1
  2044. for fix in GRID:
  2045. rgb = fix.rgb
  2046. pos = fix.POS() #CFG_IN["x1"],CFG_IN["y1"])
  2047. pygame.draw.rect(window,rgb,pos)
  2048. # DRAW SUB-FIXTURE
  2049. j = 0
  2050. for subfix in fix.sub_fix:#calc(data):
  2051. subfix.dmx_calc(data)
  2052. spos = subfix.POS() #_x,_y)#+pm_wy)
  2053. srgb = subfix.rgb
  2054. if FUNC > 10 and FUNC <= 20: # big dot
  2055. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  2056. elif FUNC > 20 and FUNC <= 30:#small dot
  2057. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  2058. elif FUNC > 30 and FUNC <= 40:#donut
  2059. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  2060. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  2061. elif FUNC > 40 and FUNC <= 50: # rec with hole
  2062. pygame.draw.rect(window,srgb,spos)
  2063. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  2064. elif FUNC > 50 and FUNC <= 60: # rec with big hole
  2065. pygame.draw.rect(window,srgb,spos)
  2066. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  2067. elif FUNC > 60 and FUNC <= 70: # rec with donat
  2068. pygame.draw.rect(window,srgb,spos)
  2069. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  2070. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  2071. elif FUNC > 70 and FUNC <= 80: # rec boarder
  2072. pygame.draw.rect(window,srgb,[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  2073. elif FUNC > 80 and FUNC <= 90: # rec big boarder
  2074. pygame.draw.rect(window,srgb,[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  2075. elif FUNC > 90 and FUNC <= 100: # rec thin line
  2076. pygame.draw.rect(window,srgb,spos)
  2077. pygame.draw.rect(window,[0,0,0],[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  2078. elif FUNC > 100 and FUNC <= 110: # rec big line
  2079. pygame.draw.rect(window,srgb,spos)
  2080. pygame.draw.rect(window,[0,0,0],[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  2081. elif FUNC > 110 and FUNC <= 120: # rec with dot
  2082. pygame.draw.rect(window,srgb,spos)
  2083. pygame.draw.rect(window,[0,0,0],[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  2084. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  2085. elif FUNC > 120 and FUNC <= 130: # rec inline
  2086. pygame.draw.rect(window,srgb,[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  2087. pygame.draw.rect(window,[0,0,0],[spos[0]+3,spos[1]+3,spos[2]-6,spos[3]-6])
  2088. elif FUNC > 130 and FUNC <= 140: # 3 dot (heart)
  2089. draw_circle(window,srgb,(spos[0]+int(spos[2]/2)+2,spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  2090. draw_circle(window,srgb,(spos[0]+int(spos[2]/2)-2,spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  2091. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)+2),int(spos[3]/3.5))
  2092. else:
  2093. pygame.draw.rect(window,srgb,spos)
  2094. i += 1
  2095. def grid_label(GRID,_x=0,_y=0):
  2096. for fix in GRID:
  2097. #pos = fix.POS(_x,_y)
  2098. pos = fix.POS() #CFG_IN["x1"],CFG_IN["y1"])
  2099. rgb = fix.rgb
  2100. # draw row/col grid number
  2101. if pos[0] == 0:
  2102. fr = font12.render("{}".format(pos[1]+1) ,1, (200,200,200))
  2103. window.blit(fr,(10,pos[1]+3 ))
  2104. if pos[1] == 0:
  2105. fr = font12.render("{}".format(pos[0]+1) ,1, (200,200,200))
  2106. window.blit(fr,(pos[0]+2,35 ))
  2107. #pygame.draw.rect(window,rgb,pos)
  2108. def grid_sub_label(GRID,_x=0,_y=0):
  2109. h = 1
  2110. v = 1
  2111. for fix in GRID:
  2112. j = 0
  2113. #pos = fix.POS(_x,_y)
  2114. pos = fix.POS() #CFG_IN["x1"],CFG_IN["y1"])
  2115. for subfix in fix.sub_fix:
  2116. subfix.dmx_calc(data)
  2117. spos = subfix.POS() #(_x,_y)
  2118. srgb = subfix.rgb
  2119. # draw row/col grid number
  2120. if subfix.pos[0] == 0:
  2121. fr = font12.render("{}".format(v ) ,1, (200,200,200))
  2122. window.blit(fr,(25,spos[1] ))
  2123. v += 1
  2124. if subfix.pos[1] == 0:
  2125. fr = font12.render("{}".format(1) ,1, (200,200,200))
  2126. fr = font12.render("{}".format(h ) ,1, (200,200,200))
  2127. h+=1
  2128. window.blit(fr,(spos[0],50 ))
  2129. if p >= 40:
  2130. if NR:
  2131. fr = font15.render("{:02}".format(subfix._id) ,1, (250,200,5))
  2132. window.blit(fr,(spos[0]+2,spos[1]+10))
  2133. j += 1
  2134. def frame_area():
  2135. rgb = [255,255,0]
  2136. CFG = CFG_IN
  2137. p1 = CFG_IN["p1"]
  2138. p2 = CFG_IN["p2"]
  2139. draw_frame(window,rgb,p1,p2,offset=2)
  2140. rgb = [255,0,0]
  2141. #draw_frame(window,rgb,p1,p2,offset=4)
  2142. if CFG_OUT["on"]:
  2143. rgb = [255,0,0]
  2144. p1 = CFG_OUT["p1"]
  2145. p2 = CFG_OUT["p2"]
  2146. #pygame.draw.line(window,rgb,p1,p2)
  2147. draw_frame(window,rgb,p1,p2,offset=3)
  2148. if CFG_OUT2["on"]:
  2149. rgb = [255,0,0]
  2150. p1 = CFG_OUT2["p1"]
  2151. p2 = CFG_OUT2["p2"]
  2152. #pygame.draw.line(window,rgb,p1,p2)
  2153. draw_frame(window,rgb,p1,p2,offset=3)
  2154. ips=[]
  2155. dataA=[]
  2156. data=[]
  2157. def dmx_raw():
  2158. global frame
  2159. frame += 1
  2160. global ips,dataA,data
  2161. ips = read_index()
  2162. # ----
  2163. ip = select_ip(ips,univ=1) # univ 1 gobo
  2164. dataA = read_dmx(ip)
  2165. # ----
  2166. data = read_dmx_data(ip,ips)
  2167. if options.countdown:
  2168. counter_dmx(COUNTER,dataA)
  2169. if len(VIDEO) > 0:
  2170. video_dmx(VIDEO,dataA)
  2171. return ips,dataA,data
  2172. def dmx_loop():
  2173. while 1:
  2174. dmx_raw()
  2175. time.sleep(1/40) # fast high cpu
  2176. #time.sleep(1/25)
  2177. if QUIT:
  2178. break
  2179. dmx_raw()
  2180. thread.start_new_thread(dmx_loop,())
  2181. def draw_output_label(CFG,value,dmx="0.0",name="GRID_XX"):
  2182. dim_raw = value
  2183. pygame.draw.rect(window,[5,5,155],[CFG["x1"]-5,CFG["y2"]+4,130,35]) # background
  2184. # GRID_A1_DIM_LABEL
  2185. fr = font15.render("Output:"+name,1, (255,255,255))
  2186. fr_r = fr.get_rect(center=(int(wx/2),int(0+pm_wy-p*0-10)))
  2187. window.blit(fr,(CFG["x1"],CFG["y2"]+5))
  2188. fr = font15.render("inv-dim: "+str(dim_raw) ,1, (255,255,255))
  2189. fr_r = fr.get_rect(center=(int(wx/2),int(0+pm_wy-p*0-10)))
  2190. window.blit(fr,(CFG["x1"]+60,CFG["y2"]+15))
  2191. fr = font15.render("dmx: {}.{}".format(START_UNIV_A,dmx) ,1, (255,255,255))
  2192. window.blit(fr,(CFG["x1"],CFG["y2"]+15))
  2193. def GRID_DIM(dim,x,y):
  2194. try:
  2195. dim = int(grid_dim_v)
  2196. if dim > 255:
  2197. dim = 255
  2198. if dim <=0:
  2199. dim = 0
  2200. except:pass
  2201. dim_raw = dim*-1
  2202. s = pygame.Surface((p*8,p*8)) # the size of your rect
  2203. s.set_alpha(dim) # alpha level
  2204. s.fill((0,0,0)) # this fills the entire surface
  2205. window.blit(s, (x,y))
  2206. def GRID_A1_DIM():
  2207. # GRID_A1 DIM
  2208. if options.grid_a1_idim:
  2209. CFG=CFG_OUT
  2210. grid_dim_ch = int(options.grid_a1_idim)
  2211. grid_dim_v = dataA[grid_dim_ch-1]
  2212. dim=255
  2213. dim=grid_dim_v
  2214. #dim=120
  2215. GRID_DIM(dim,x=CFG["x1"],y=CFG["y2"]-p*8)
  2216. a=options.grid_a1_idim
  2217. draw_output_label(CFG,dim,a,name="GRID_A1_DIM")
  2218. def GRID_A2_DIM():
  2219. # GRID_A2 DIM
  2220. if options.grid_a2_idim:
  2221. CFG=CFG_OUT2
  2222. grid_dim_ch = int(options.grid_a2_idim)
  2223. grid_dim_v = dataA[grid_dim_ch-1]
  2224. dim=255
  2225. try:
  2226. dim = int(grid_dim_v)
  2227. if dim > 255:
  2228. dim = 255
  2229. if dim <=0:
  2230. dim = 0
  2231. except:pass
  2232. dim_raw = dim*-1
  2233. s = pygame.Surface((p*8,p*8)) # the size of your rect
  2234. s.set_alpha(dim) # alpha level
  2235. s.fill((0,0,0)) # this fills the entire surface
  2236. window.blit(s, (CFG["x1"],CFG["y2"]-p*8))
  2237. a=options.grid_a2_idim
  2238. draw_output_label(CFG,dim_raw,a,name="GRID_A2_DIM")
  2239. def cvImageToSurface(cvImage):
  2240. if cvImage.dtype.name == 'uint16':
  2241. cvImage = (cvImage / 256).astype('uint8')
  2242. size = cvImage.shape[1::-1]
  2243. if len(cvImage.shape) == 2:
  2244. cvImage = np.repeat(cvImage.reshape(size[1], size[0], 1), 3, axis = 2)
  2245. format = 'RGB'
  2246. else:
  2247. format = 'RGBA' if cvImage.shape[2] == 4 else 'RGB'
  2248. cvImage[:, :, [0, 2]] = cvImage[:, :, [2, 0]]
  2249. surface = pygame.image.frombuffer(cvImage.flatten(), size, format)
  2250. return surface.convert_alpha() if format == 'RGBA' else surface.convert()
  2251. def create_img_meta():
  2252. out = {}
  2253. #40 235
  2254. out["pos"] = (0,0) # x,y
  2255. out["ipos"] = (0,0) # x,y
  2256. out["scale"] = 50
  2257. out["dim"] = 100
  2258. out["ang"] = 0
  2259. out["_fpath"] = '/opt/LibreLight/Xdesk/icon/'
  2260. out["fname"] = "picker.png"
  2261. out["raw_img"] = ""
  2262. out["img"] = ""
  2263. out["err"] = []
  2264. return out
  2265. def img_open(img_meta):
  2266. fname = img_meta["_fpath"] +"/"+ img_meta["fname"] #'/opt/LibreLight/Xdesk/icon/picker.png'
  2267. im2 = cv2.imread(fname)
  2268. width = int(im2.shape[1])
  2269. height = int(im2.shape[0])
  2270. w_max = 480
  2271. h_max = 320
  2272. #w_max = 600
  2273. #h_max = 400
  2274. if width > w_max:
  2275. h = int(height*(w_max/width))
  2276. w = w_max
  2277. im2 = cv2.resize(im2, (w,h), interpolation =cv2.INTER_LINEAR)
  2278. if height > h_max:
  2279. w = int(width*(h_max/height))
  2280. h = h_max
  2281. if w < w_max:
  2282. im2 = cv2.resize(im2, (w,h), interpolation =cv2.INTER_LINEAR)
  2283. img_meta["raw_img"] = im2
  2284. img_meta["img"] = im2
  2285. return im2
  2286. def img_jitter(img_meta):
  2287. r1=random.randint(0,3)
  2288. r2=random.randint(0,3)
  2289. img_meta["ipos"] = [r1,r2]
  2290. def img_rescale_width(img_meta, _width):
  2291. frame = img_meta["raw_img"]
  2292. width = int(frame.shape[1])
  2293. height = int(frame.shape[0])
  2294. dim = (width, height)
  2295. if 1: #width > _width:
  2296. h = int(height*( width/_width))
  2297. dim = (_width, h)
  2298. im = cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
  2299. else:
  2300. im = cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
  2301. return im
  2302. def img_rescale(img, percent=75):
  2303. frame = img
  2304. #frame = img_meta["raw_img"]
  2305. width = int(frame.shape[1] * percent/ 100)
  2306. height = int(frame.shape[0] * percent/ 100)
  2307. dim = (width, height)
  2308. return cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
  2309. def img_draw_raw(img_meta):
  2310. #img_jitter(img_meta)
  2311. pos = img_meta["pos"]
  2312. ipos = img_meta["ipos"]
  2313. im2 = img_meta["img"]
  2314. dim = img_meta["dim"]
  2315. im2 = cvImageToSurface(im2)
  2316. w2 = int(im2.get_width()/2)
  2317. h2 = int(im2.get_height()/2)
  2318. x=pos[0]+ipos[0]
  2319. y=pos[1]+ipos[1]
  2320. window.blit(im2, (x-w2,y-h2))
  2321. img_meta_a = create_img_meta()
  2322. img_open(img_meta_a)
  2323. t1 = Timer(143)
  2324. time.sleep(0.33)
  2325. t2 = Timer(11)
  2326. count_tilt = 0
  2327. def main():
  2328. global IP
  2329. global GRID
  2330. global GRID_A
  2331. global FUNC
  2332. global count_tilt
  2333. global TEXT_BLOCK
  2334. global TEXT_BLOCK_TIME
  2335. global MEDIALIST
  2336. global MEDIALIST_TIME
  2337. global dataA
  2338. global frame2
  2339. global runnung
  2340. reload_grid()
  2341. s=time.time()
  2342. r = ""
  2343. IP = "xx"
  2344. while running:
  2345. if TEXT_BLOCK_TIME+5 < time.time():
  2346. TEXT_BLOCK = open_text_block()
  2347. TEXT_BLOCK_TIME = time.time()
  2348. if MEDIALIST_TIME+6 < time.time():
  2349. MEDIALIST = open_medialist()
  2350. MEDIALIST_TIME = time.time()
  2351. pygame.display.flip()
  2352. set_pos(GRID,_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2353. #set_pos(GRID,600,600)#_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2354. event()
  2355. window.fill((10,0,30))
  2356. calc_fps()
  2357. calc_fps2()
  2358. #draw_overlay()
  2359. # GRID loop
  2360. try:
  2361. FUNC = dataA[gobo_ch-1]
  2362. FUNC2 = FUNC
  2363. except Exception as e:
  2364. print("EXC FUNC",e)
  2365. #if int(time.time()) % 2: # BLINK
  2366. #set_gobo_pos(GRID,_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2367. draw_gobo(GRID,data) #,_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2368. #if int(time.time()) % 2: # BLINK
  2369. grid_label(GRID,_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2370. #grid_sub_label(GRID) #,_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2371. if VIDEO:
  2372. draw_all_video(VIDEO)
  2373. #draw_all_video_overlay(VIDEO)
  2374. #img_draw(img_meta_a)
  2375. if options.countdown:
  2376. draw_counter(COUNTER)
  2377. # output background mask
  2378. pygame.draw.rect(window,[0,0,0],[35,57,MAIN_SIZE[0]+200-35,135]) # background
  2379. #pygame.draw.rect(window,[5,5,5],[0,60,MAIN_SIZE[0],p*9]) # background
  2380. pointer.draw(0,pm_wy) #wy
  2381. spos = [0,0,0,0]
  2382. if PIXEL_MAPPING >= 1:
  2383. #try:
  2384. if options.dual_vpu:
  2385. GRID_X = GRID_A[8*8:]
  2386. xx = p*8
  2387. #reshape(GRID,GRID_X,CFG_OUT2["x1"],CFG_OUT2["y1"]-p*8,name="GRID_A2") #start pos
  2388. #except Exception as e:
  2389. # print("Exception 23123",e)
  2390. reshape2(GRID,GRID_A,CFG_OUT["x1"],CFG_OUT["y1"],name="GRID_A1") #start pos
  2391. #reshape(GRID,GRID_A,CFG_OUT["x1"],CFG_OUT["y1"],name="GRID_A1") #start pos
  2392. else:
  2393. pass #reshape(GRID,GRID_A,spos[0]+spos[2]+20,10) #start pos
  2394. frame_area()
  2395. # OUTPUT -> MAPING
  2396. draw_kachel_nr()
  2397. input_mask() # mask background
  2398. output_mask() # mask background
  2399. GRID_A1_DIM() # overlay
  2400. GRID_A2_DIM() # overlay
  2401. if VIDEO:
  2402. draw_all_video_overlay(VIDEO)
  2403. draw_main_overlay()
  2404. pointer.draw(0,pm_wy) #wy
  2405. pygame.display.flip()
  2406. clock.tick(25)
  2407. if 'SDL_VIDEO_WINDOW_POS' in os.environ:
  2408. del os.environ['SDL_VIDEO_WINDOW_POS'] #= '%i,%i' % (200,164)
  2409. frame2 += 1
  2410. if __name__ == "__main__":
  2411. try:
  2412. main()
  2413. finally:
  2414. del_all_video()