vpu_live4.py 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996
  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 draw_img(self,wn,img_meta):
  607. self.img_meta["pos"] = [self.x,self.y]
  608. self.img_meta["scale"] = self.scale #= 50 #%
  609. self.img_meta["dim"] = self.dim #255
  610. self.img_meta["ang"] = self.angle # 0
  611. if 1: #self.is_change():
  612. im2 = self.img_meta["raw_img"]
  613. im2 = img_rescale(im2, percent=self.img_meta["scale"])
  614. # add DIMER to videplayer
  615. im2 = cv2.normalize(im2, im2, 0, self.img_meta["dim"], cv2.NORM_MINMAX)
  616. self.img_meta["img"] = im2
  617. img_draw_raw(self.img_meta)
  618. self.change_reset()
  619. def is_change(self):
  620. #if self.frame_change or self.dmx_change:
  621. if self.frame_change or self.dmx_change:
  622. #if self.dmx_change:
  623. return True
  624. def change_reset(self):
  625. if self.frame_change:
  626. print("media frame change")
  627. if self.dmx_change:
  628. print("media dmx change")
  629. self.frame_change = 0
  630. self.dmx_change = 0
  631. def draw(self,wn=None):
  632. if self.img_meta:
  633. self.draw_img(wn,self.img_meta)
  634. return
  635. if self.dim <= 1:
  636. return
  637. self.calc_im_size()
  638. im = self.im
  639. x=self.x
  640. y=self.y
  641. #w,h = self.calc_zoom(x,y)
  642. w = self.shape_x
  643. h = self.shape_y
  644. if self.success and wn and im:
  645. if self.fname.startswith("cam_"):
  646. self.draw_cam(wn,im,x,y,w,h)
  647. else:
  648. self.draw_video(wn,im,x,y,w,h)
  649. self.change_reset()
  650. def get_meta(self):
  651. out = {}
  652. mode="x"
  653. if self._run:
  654. mode = "run"
  655. else:
  656. mode = "pause"
  657. out["frames"] = len(self.buffer)
  658. out["mode"] = mode
  659. out["fname"] = self.fname
  660. out["scale"] =self.scale
  661. out["fps"] = int(self.fps)
  662. out["pos"] = self.pos
  663. out["dim"] = self.dim
  664. out["run"] = self._run
  665. out["cur"] = 0
  666. if self.fps > 0: # check if div zerro
  667. out["cur"] = (self.pos/self.fps)
  668. out["x"] = self.x
  669. out["y"] = self.y
  670. out["id"] = self._id
  671. out["nr"] = self._media_nr
  672. out["pz"] = 0
  673. return out
  674. def _overlay_text(self):
  675. meta = self.get_meta()
  676. keys = ["fps","pos","frames","cur","pz","x","y","scale","dim"]
  677. _line = create_format_line(meta,keys)
  678. keys= ["id","nr","mode","fname"]
  679. _line2 = create_format_line(meta,keys)
  680. return [_line,_line2]
  681. def create_format_line(meta,keys):
  682. _line = []
  683. for k in keys:
  684. v=meta.get(k,"-")
  685. _line.append(str(k))
  686. _line.append(":")
  687. if type(v) is float:
  688. _line.append("{:02f}".format(v))
  689. else:
  690. _line.append(str(v))
  691. _line.append(" ")
  692. _line = "".join(_line)
  693. return _line
  694. PixelMedia = PixelMedia
  695. VIDEO = []
  696. videoplayer=[]
  697. cv2 = None
  698. FileVideoStream = None
  699. _vid = 0
  700. if type(options.videoplayer) is str:
  701. try:
  702. import cv2
  703. except Exception as e:
  704. cprint("Except Import:",e,color="red")
  705. try:
  706. # faster video reading ... ???
  707. from imutils.video import FileVideoStream
  708. except Exception as e:
  709. cprint("Except Import:",e,color="red")
  710. max_videoplayer = 4
  711. dmx_start = options.videoplayer.split(",")
  712. for cdmx in dmx_start:
  713. if len(videoplayer) > max_videoplayer:
  714. print("-- videoplayer max count {} !! break".format(max_videoplayer))
  715. break
  716. print("-- videoplayer dmx:",cdmx)
  717. try:
  718. cdmx = int(cdmx)
  719. videoplayer.append( PixelMedia(cdmx,_id=_vid) )
  720. _vid += 1
  721. _tmp = {"DMX":cdmx,"DIM":0,"PAN":127,"TILT":127
  722. ,"CONTROL":0,"SEC":10,"VIDEO":"3"
  723. ,"RED":255,"GREEN":255,"BLUE":255
  724. ,"_time":time.time()
  725. ,"_RUN":0,"_SEC":">{}<".format(cdmx)
  726. }
  727. VIDEO.append(_tmp)
  728. except Exception as e:
  729. cprint("EXCEPTION COUNTER INIT ",cdmx,color="red")
  730. def loop_videoplayer():
  731. while 1:
  732. _videoplayer = videoplayer[:]
  733. #print(".")
  734. ok = 0
  735. for i in _videoplayer: #.append( PixelMedia(cdmx,_id=_vid) )
  736. try:
  737. r = i.read() # read next frame from file
  738. if r:
  739. ok = 1
  740. except Exception as e:
  741. cprint("EXCEPTION loop_videoplayer ",e,color="red")
  742. if ok == 0:
  743. time.sleep(0.1)
  744. else:
  745. time.sleep(0.005)
  746. if QUIT:
  747. break
  748. thread.start_new_thread(loop_videoplayer,())
  749. # ===== ======
  750. #self.fname = MEDIALIST[0]
  751. videoplayer2 = []
  752. def loop2_videoplayer():
  753. while 1:
  754. print()
  755. print()
  756. print()
  757. for i,v in enumerate(MEDIALIST):
  758. try:
  759. video1 = videoplayer2[i]
  760. video1.select_video(i)
  761. except:
  762. vi = PixelMedia(181,1)
  763. videoplayer2.append( vi )
  764. _videoplayer = videoplayer2[:]
  765. ok = 0
  766. j =0
  767. for i in _videoplayer: #.append( PixelMedia(cdmx,_id=_vid) )
  768. try:
  769. r = i.read() # read next frame from file
  770. if r:
  771. print(j,len(videoplayer2),i,len(i.buffer))
  772. ok = 1
  773. except Exception as e:
  774. cprint("EXCEPTION loop2_videoplayer ",e,color="red")
  775. time.sleep(0.002)
  776. j += 1
  777. p = 16
  778. block = [p,p]
  779. _x = 8
  780. _y = 8
  781. #HD = "0"
  782. if options.mode:
  783. try:
  784. HD = options.mode
  785. p,_x,_y = HD.split(",")
  786. _x = int(_x)
  787. _y = int(_y)
  788. p = int(p)
  789. block = [p,p]
  790. except Exception as e:
  791. cprint( "Exc",options.mode,e,color="red")
  792. # PARSE COMMANDLINE ARGUMENTS
  793. CFG_IN = {"name":"CFG_IN","x1":40,"y1":60,"x2":300,"y2":300 ,"w":300,"h":300}
  794. CFG_OUT = {"name":"CFG_OUT","x1":40,"y1":60,"x2":300,"y2":300 ,"w":300,"h":300,"on":0}
  795. #CFG_OUT2 = {"name":"CFG_OUT2","x1":p*8+142,"y1":60,"x2":300,"y2":300 ,"w":300,"h":300,"on":0}
  796. CFG_OUT2 = {"name":"CFG_OUT2","x1":p*8+(16*2)+8,"y1":60,"x2":300,"y2":300 ,"w":300,"h":300,"on":0}
  797. CFG_BLOCK = {"name":"CFG_BLOCK","size":16,"h-split":2,"v-split":2,"h-count":8,"v-count":8}
  798. if _x < 1:
  799. _x = 1
  800. if _y < 1:
  801. _y = 1
  802. CFG_BLOCK["h-count"] = _x
  803. CFG_BLOCK["v-count"] = _y # int(8*8/_x) #/_x+0.5) #_y
  804. CFG_BLOCK["size"] = p
  805. print( [options.xsplit])
  806. print( [options.ysplit])
  807. try:
  808. if options.xsplit:
  809. CFG_BLOCK["h-split"] = int(options.xsplit)
  810. if options.ysplit:
  811. CFG_BLOCK["v-split"] = int(options.ysplit)
  812. except Exception as e:
  813. cprint( "Exc",options.mode,e,color="red")
  814. print("HD",CFG_BLOCK["h-split"],CFG_BLOCK["v-split"])
  815. print("xy",_x,_y)
  816. print("++++++++++++++++++", p,_x,_y)
  817. _x2 = _x
  818. try:
  819. if options.XX:
  820. pass#_x2 = int(options.XX)
  821. except Exception as e:
  822. cprint( "Exc",options.mode,e,color="red")
  823. print("_x2 , -X",_x2)
  824. # ===== GUI =========
  825. import pygame
  826. import pygame.gfxdraw
  827. import pygame.font
  828. clock = pygame.time.Clock()
  829. os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (200,164)
  830. if options.win_pos:
  831. if "," in options.win_pos:
  832. win_pos = options.win_pos.split(",")
  833. try:
  834. WIN_POS = '%i,%i' % (int(win_pos[0]),int(win_pos[1]) )
  835. os.environ['SDL_VIDEO_WINDOW_POS'] = WIN_POS
  836. except Excetpion as e:
  837. cprint("win_pos",win_pos,e,color="red")
  838. os.environ['SDL_VIDEO_CENTERED'] = '0'
  839. pg = pygame
  840. pygame.init()
  841. pygame.mixer.quit()
  842. f = pygame.font.get_fonts()
  843. for i in f:
  844. if "mono" in i.lower():
  845. print(i)
  846. font = pygame.font.SysFont("freemonobold",22)
  847. font10 = pygame.font.SysFont("freemonobold",10)
  848. font12 = pygame.font.SysFont("freemonobold",12)
  849. font12l = pygame.font.SysFont("freemono",12)
  850. font15 = pygame.font.SysFont("freemonobold",15)
  851. font40 = pygame.font.SysFont("freemonobold",40)
  852. font80 = pygame.font.SysFont("freemonobold",70)
  853. #font = pygame.font.SysFont(None,30)
  854. fr = font.render("hallo" ,1, (200,0,255))
  855. def clean_path(path):
  856. _path = path[:]
  857. _path = _path.replace("/","-")
  858. _path = _path.replace(".","-")
  859. _path = _path.replace("\"","-")
  860. _path = _path.replace("'","-")
  861. return _path
  862. PIXEL_MAPPING = 0
  863. vpu_wall_conf_file = HOME+"/LibreLight/vpu_wall_conf.csv"
  864. grid_file = "/tmp/vpu_grid_hd.csv"
  865. text_file = HOME+"/LibreLight/vpu_text_hd.csv"
  866. media_list = "/tmp/vpu_medialist_hd.csv"
  867. media_list = HOME+"/LibreLight/video/" #.format(path)
  868. pm_wy = 0
  869. if options.pixel_mapping:
  870. PIXEL_MAPPING = 1
  871. CFG_OUT["on"] = 1
  872. path = options.pixel_mapping
  873. path = clean_path(path)
  874. grid_file = HOME+"/LibreLight/vpu_grid_hd{}.csv".format(path)
  875. if options.dual_vpu:
  876. grid_file = HOME+"/LibreLight/vpu_grid_dual{}.csv".format(path)
  877. text_file = HOME+"/LibreLight/vpu_text_hd{}.csv".format(path)
  878. media_list = HOME+"/LibreLight/vpu_medialist_hd{}.csv".format(path)
  879. media_list = HOME+"/LibreLight/video/" #.format(path)
  880. #_x = 8
  881. #_y = 8
  882. if options.dual_vpu:
  883. CFG_OUT2["on"] = 1
  884. print(" ",[options.pixel_mapping],"grid_file",grid_file)
  885. #grid_file = HOME+"/LibreLight/vpu_grid_hd.csv"
  886. MAIN_SIZE=(600,500)
  887. try:
  888. if _x < 8 and PIXEL_MAPPING >= 1:
  889. wx = 30+30+block[0] * 8
  890. CFG_IN["w"] = CFG_BLOCK["size"] * 8
  891. else:
  892. wx = 30+30+block[0] * _x
  893. wy = 40+40+block[1] * _y
  894. if type(options.videoplayer) is str:
  895. wy += 150 # video medialist
  896. MAIN_SIZE=(wx,wy)
  897. if PIXEL_MAPPING >= 1:
  898. pm_wy = 11*p #+ p*3
  899. CFG_IN["y1"] += 11*p
  900. if options.dual_vpu:
  901. if wx < p*16+155:
  902. wx = p*16+155
  903. MAIN_SIZE=(wx,wy+pm_wy)
  904. except Exception as e:
  905. cprint("Exception:",e,color="red")
  906. CFG_IN["w"] = CFG_BLOCK["size"] * CFG_BLOCK["h-count"]
  907. CFG_IN["h"] = CFG_BLOCK["size"] * CFG_BLOCK["v-count"]
  908. CFG_OUT["w"] = CFG_BLOCK["size"] * 8
  909. CFG_OUT["h"] = CFG_BLOCK["size"] * 8
  910. CFG_OUT2["w"] = CFG_BLOCK["size"] * 8
  911. CFG_OUT2["h"] = CFG_BLOCK["size"] * 8
  912. def CFG_CALC_P(CFG):
  913. CFG["x2"] = CFG["x1"]+CFG["w"]
  914. CFG["y2"] = CFG["y1"]+CFG["h"]
  915. CFG["p1"] = [CFG["x1"] ,CFG["y1"]]
  916. CFG["p2"] = [CFG["x2"] ,CFG["y2"]]
  917. print("CFG",CFG)
  918. # ?
  919. #CFG_IN["w"] = int(CFG_BLOCK["v-count"]-2) * CFG_BLOCK["size"]
  920. CFG_CALC_P(CFG_IN)
  921. CFG_CALC_P(CFG_OUT)
  922. CFG_CALC_P(CFG_OUT2)
  923. print("CFG_BLOCK",CFG_BLOCK)
  924. print()
  925. print()
  926. print()
  927. print()
  928. print()
  929. window = pygame.display.set_mode(MAIN_SIZE,pg.RESIZABLE)#,32)#,pygame.FULLSCREEN) #x left->right ,y top-> bottom
  930. pg.display.set_caption('LibreLight VPU-{}'.format(options.title))
  931. class Fix():
  932. def __init__(self,_id,pos,block=[16,16],univ=0,dmx=0,ch=4):
  933. #print("Fix",_id)
  934. self._id = _id
  935. self.dmx = (_id-1) * ch +1 #dmx
  936. self.univ = univ
  937. self.ch = ch
  938. self.pos = pos
  939. self.rgb = [0,0,0]
  940. self.block = block #[10,10]
  941. self.x = pos[0]
  942. self.y = pos[1]
  943. self._x = 0
  944. self._y = 0
  945. self.strobo = time.time()
  946. self.bmp = 250
  947. self.sub_fix = []
  948. sub_block =[block[0]/CFG_BLOCK["h-split"],block[1]/CFG_BLOCK["v-split"]]
  949. if _id <= 0: #exit
  950. return
  951. spalte = (_id-1)%_y +1
  952. zeile = int((_id-1)/_x2) #+1
  953. add_row = _x*CFG_BLOCK["h-split"]*CFG_BLOCK["v-split"]
  954. #zeile 1
  955. sid = (_id-1)*2 + zeile*CFG_BLOCK["h-split"]*_x2
  956. sid = sid+1
  957. sub_pos= [pos[0]*block[0],pos[1]*block[1]]
  958. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  959. self.sub_fix.append(sub_fix)
  960. sid = sid+1
  961. sub_pos= [pos[0]*block[0]+block[0]/2,pos[1]*block[1]]
  962. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  963. self.sub_fix.append(sub_fix)
  964. #zeile 2
  965. sid = (_id-1)*2+1 + _x2*CFG_BLOCK["h-split"] + zeile*CFG_BLOCK["h-split"]*_x2 # int(add_row)
  966. sub_pos= [pos[0]*block[0],pos[1]*block[1]+block[1]/2]
  967. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  968. self.sub_fix.append(sub_fix)
  969. #sid = sid+1
  970. sid = sid+1
  971. sub_pos= [pos[0]*block[0]+block[0]/2,pos[1]*block[1]+block[1]/2]
  972. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  973. self.sub_fix.append(sub_fix)
  974. def __repr__(self):
  975. return "<Fix: {} {} {} {}>".format(id(self),self.x,self.y,self.dmx)
  976. def calc(self,data):
  977. _rgb = [0,255,0]
  978. return _rgb
  979. def sub_calc(self,data):
  980. _rgb = [0,255,0]
  981. for sub_fix in self.sub_fix:
  982. sub_fix.block = self.block[:]
  983. _rgb = sub_fix.dmx_calc(data)
  984. return _rgb
  985. def _calc(self,a,b):
  986. A = (self.pos[0])*self.block[0]
  987. B = (self.pos[1])*self.block[1]
  988. C = self.block[0]-a
  989. D = self.block[1]-b
  990. return A,B,C,D
  991. def POS(self,x=None,y=None,a=0,b=0):
  992. if type(x) is int:
  993. self._x=x
  994. if type(y) is int:
  995. self._y=y
  996. A,B,C,D = self._calc(a,b)
  997. return [self._x+A,self._y+B,C,D]
  998. def XXXsubPOS(self,x=0,y=0,a=0,b=0):
  999. __out = []
  1000. for sub_fix in self.sub_fix:
  1001. __out.append( sub_fix.POS(x,y,a,b) )
  1002. return __out
  1003. class SubFix():
  1004. def __init__(self,_id,pos,block=[16,16],univ=0,dmx=0,ch=4):
  1005. #print("Fix",_id)
  1006. self._id = _id
  1007. self.dmx = (_id-1) * ch +1 #dmx
  1008. self.univ = univ
  1009. self.ch = ch
  1010. self.pos = pos
  1011. self.rgb = [0,0,40]
  1012. self.block = block #[10,10]
  1013. self.x = pos[0]
  1014. self.y = pos[1]
  1015. self._x = 0
  1016. self._y = 0
  1017. self.strobo = time.time()
  1018. self.bmp = 250
  1019. def dmx_calc(self,data):
  1020. #return [130,30,20]
  1021. dmx_sub = [30]*10
  1022. dmx = self.dmx -1
  1023. _dmx_sub = []
  1024. if self.dmx >= 0:
  1025. dmx = rDMX(self.univ,self.dmx)-1
  1026. if dmx+self.ch < len(data):
  1027. _dmx_sub = data[dmx:dmx+self.ch]
  1028. if _dmx_sub:
  1029. dmx_sub = _dmx_sub
  1030. dim = dmx_sub[0]/255
  1031. r = dmx_sub[1]*dim
  1032. g = dmx_sub[2]*dim
  1033. b = dmx_sub[3]*dim
  1034. r = int(r)
  1035. g = int(g)
  1036. b = int(b)
  1037. self.rgb = [r,g,b]
  1038. return self.rgb
  1039. def _calc(self,a,b):
  1040. A = (self.pos[0]) #+self.block[0]
  1041. B = (self.pos[1]) #+self.block[1]
  1042. C = self.block[0]-a
  1043. D = self.block[1]-b
  1044. return A,B,C,D
  1045. def POS(self,x=None,y=None,a=0,b=0):
  1046. if type(x) is int:
  1047. self._x=x
  1048. if type(y) is int:
  1049. self._y=y
  1050. A,B,C,D = self._calc(a,b)
  1051. #if NR:
  1052. # C-=1
  1053. # D-=1
  1054. return [int(self._x+A),int(self._y+B),int(C),int(D)]
  1055. class POINTER():
  1056. def __init__(self):
  1057. self.pos = [0,0,0,0]
  1058. self.on = 0
  1059. self.rgb = [0,100,10]
  1060. self._x = 0
  1061. self._y = 0
  1062. self.x = 0
  1063. self.y = 0
  1064. self.fix = Fix(0 ,[999,999],[16,16],0,0,0)
  1065. def row_move(self,x,y):
  1066. self._x = x
  1067. self._y = y
  1068. def move(self,pos):
  1069. self.pos = pos
  1070. self.on = 1
  1071. def cross(self,x,y):
  1072. self.x = x
  1073. self.y = y
  1074. def draw(self,x,y):
  1075. pos = self.pos[:]
  1076. #print("draw",x,y,pos)
  1077. pos[0] += x
  1078. pos[1] += y
  1079. fix_x= self.fix.x
  1080. fix_y= self.fix.y +y
  1081. if 0:# self.on:
  1082. pygame.draw.rect(window,self.rgb,pos)
  1083. # mouse grid posision
  1084. fr = font15.render("{}/{}".format(fix_x+1,fix_y) ,1, (200,200,200))
  1085. _nr = fix_y * _x + fix_x +1
  1086. fr = font15.render("{:02}".format(_nr ) ,1, (200,200,200))
  1087. window.blit(fr,(pos[0]+2,pos[1]+2 ))
  1088. window.blit(fr,(130,1))
  1089. if 1:# lupe / looking-glass / lens
  1090. __t = 60
  1091. rgb=[255,0,0]
  1092. pos=[400,400,160,160]
  1093. draw_box_frame(window,rgb,pos,offset=1)
  1094. r_sub = grab(x=self._x-__t/2,y=self._y-__t/2,w=__t,h=__t)
  1095. if r_sub:
  1096. r_sub = pygame.transform.scale(r_sub,(160,160))
  1097. if r_sub: # target
  1098. window.blit(r_sub, (400,400))
  1099. #pos=[400+40,400+40,80,80]
  1100. pos=[400+80,400+80,80,80]
  1101. draw_box_frame(window,rgb,pos,offset=3)
  1102. draw_box_frame(window,rgb,pos,offset=2)
  1103. draw_box_frame(window,rgb,pos,offset=0)
  1104. draw_box_frame(window,rgb,pos,offset=1)
  1105. # fix pos
  1106. #txt=str(pos) #"[0, 0, 0, 0]"
  1107. #fr = font15.render(txt ,1, (200,200,200))
  1108. #window.blit(fr,(10,1))
  1109. # pointer
  1110. fr = font15.render("X:{:03}".format(self._x) ,1, (200,200,200))
  1111. window.blit(fr,(10,30))
  1112. fr = font15.render("Y:{:03}".format(self._y) ,1, (200,200,200))
  1113. window.blit(fr,(10,40))
  1114. if 1: #self._x > 30 and self._y > 230:
  1115. # pointer
  1116. _x = self._x-INPUT_START_POS_X
  1117. _y = self._y-INPUT_START_POS_Y
  1118. rgb = [200,200,200]
  1119. if _x < 0:
  1120. rgb = [200,0,0]
  1121. fr = font80.render("X:{:03}".format(_x) ,1, rgb)
  1122. window.blit(fr,(200,400))
  1123. rgb = [200,200,200]
  1124. if _y < 0:
  1125. rgb = [200,0,0]
  1126. fr = font80.render("Y:{:03}".format(_y) ,1, rgb)
  1127. window.blit(fr,(200,450))
  1128. p=110
  1129. # crosshair
  1130. self.rgb = [0,0,200]
  1131. pygame.draw.line(window,self.rgb, (self.x-p,self.y) , (self.x-2,self.y),2 )
  1132. pygame.draw.line(window,self.rgb, (self.x,self.y-p) , (self.x,self.y-2),2 )
  1133. self.rgb = [0,200,0]
  1134. pygame.draw.line(window,self.rgb, (self.x+2,self.y) , (self.x+p,self.y),2 )
  1135. pygame.draw.line(window,self.rgb, (self.x,self.y+2) , (self.x,self.y+p),2 )
  1136. self.rgb = [200,0,0]
  1137. pointer = POINTER()
  1138. NR = 0
  1139. running = True
  1140. def event():
  1141. global NR,running
  1142. for event in pygame.event.get():
  1143. #print(event.dict)
  1144. _button = None
  1145. if "button" in event.dict:
  1146. _button = event.dict["button"]
  1147. _state = None
  1148. if "state" in event.dict:
  1149. _state = event.state
  1150. _key = None
  1151. if "key" in event.dict:
  1152. _key = event.key
  1153. _pos = None
  1154. if "pos" in event.dict:
  1155. _pos = event.pos
  1156. _type = None
  1157. if "type" in event.dict:
  1158. _type = event.type
  1159. _type = event.type
  1160. _mod = None
  1161. if "mod" in event.dict:
  1162. _mod = event.mod
  1163. if 0:
  1164. print( " ")
  1165. print( "{:.02f}".format( time.time() - START ))
  1166. print("button -",_button,end="\t| ")
  1167. #print("state -",_state)
  1168. print("pos -",_pos)
  1169. print("type -",_type, end="\t| ")
  1170. print("key -",_key)
  1171. print("mod -",_mod)
  1172. try:
  1173. if _type in [5,1025]:
  1174. if _button == 1:
  1175. NR += 1
  1176. if NR > 1:
  1177. NR = 0
  1178. if _button == 3:
  1179. NR -= 1
  1180. if NR < 0:
  1181. NR = 1
  1182. if _pos:
  1183. posA = _pos
  1184. fix = find_pix(_pos[0]-40,_pos[1]-60+pm_wy)
  1185. if fix:
  1186. #pos = fix.POS(40,60+pm_wy)
  1187. pos = fix.POS() #(CFG_IN["x1"],CFG_IN["y1"])#0,0) #+pm_wy)
  1188. rgb = [0,0,0]
  1189. pointer.move(pos)
  1190. pointer.fix = fix
  1191. else:
  1192. pointer.on = 0
  1193. pointer.row_move(_pos[0],_pos[1])
  1194. pointer.cross(_pos[0],_pos[1])
  1195. if event.type == pygame.VIDEORESIZE:
  1196. window = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)
  1197. except Exception as e:
  1198. cprint(e,color="red")
  1199. if event.type==pygame.QUIT:
  1200. global QUIT
  1201. QUIT = 1
  1202. cprint("QUIT",color="red")
  1203. running=False
  1204. #time.sleep(1)
  1205. #sys.exit()
  1206. fps = 0
  1207. fps2 = 0
  1208. frame = 0
  1209. frame2 = 0
  1210. frame_t = time.time()
  1211. frame2_t = time.time()
  1212. IP = "yyy"
  1213. vplay1=0
  1214. vplay2=0
  1215. if options.videoplayer:
  1216. try:
  1217. t=options.videoplayer.split(",")
  1218. vplay1=t[0]
  1219. vplay2=t[1]
  1220. except:pass
  1221. def output_mask(): # oben
  1222. RGB = [0,0,50]
  1223. #RGB = [120,0,0] # test
  1224. start = 0
  1225. # oben
  1226. pygame.draw.rect(window,RGB,[0,start,MAIN_SIZE[0]+200,57]) # background
  1227. # unten
  1228. pygame.draw.rect(window,RGB,[0,start+192,MAIN_SIZE[0]+200,35]) # background
  1229. # links
  1230. pygame.draw.rect(window,RGB,[0,start+0,35,235]) # background
  1231. def input_mask(): # unten
  1232. RGB = [0,0,50]
  1233. #RGB = [120,0,110] # test
  1234. start = 227
  1235. # oben
  1236. pygame.draw.rect(window,RGB,[0,start,MAIN_SIZE[0]+200,7]) # background
  1237. # links
  1238. pygame.draw.rect(window,RGB,[0,start,35,200]) # background
  1239. # unten
  1240. pygame.draw.rect(window,RGB,[0,start+173,MAIN_SIZE[0]+200,198]) # background
  1241. def draw_main_overlay():
  1242. global fps,fps2
  1243. fr = font15.render("DMX-FPS: {}".format(fps) ,1, (200,0,255))
  1244. window.blit(fr,(10,2))
  1245. fr = font15.render("GUI-FPS : {}".format(fps2) ,1, (200,0,255))
  1246. window.blit(fr,(10,12))
  1247. #fr = font15.render("ip:{}".format(IP) ,1, (200,0,255))
  1248. #window.blit(fr,(100,2))
  1249. fr = font15.render("start-uni: {:}.xx dRGB".format(START_UNIV) ,1, (200,0,255))
  1250. window.blit(fr,(90,2))
  1251. fr = font15.render("a1_idim: {}.{:}".format(START_UNIV_A,options.grid_a1_idim) ,1, (200,0,255))
  1252. window.blit(fr,(90,12))
  1253. fr = font15.render("a2_idim: {}.{:}".format(START_UNIV_A,options.grid_a2_idim) ,1, (200,0,255))
  1254. window.blit(fr,(90,22))
  1255. fr = font15.render("gobo_ch1: {}.{:}".format(START_UNIV_A,options.gobo_ch) ,1, (200,0,255))
  1256. window.blit(fr,(180,12))
  1257. fr = font15.render("gobo_ch2: {}.{:}".format(START_UNIV_A,options.gobo_ch2) ,1, (200,0,255))
  1258. window.blit(fr,(180,22))
  1259. fr = font15.render("v-play1: {}.{:}".format(START_UNIV_A,vplay1) ,1, (200,0,255))
  1260. window.blit(fr,(270,12))
  1261. fr = font15.render("v-play2: {}.{:}".format(START_UNIV_A,vplay2) ,1, (200,0,255))
  1262. window.blit(fr,(270,22))
  1263. try:
  1264. global pointer
  1265. # pointer
  1266. fr = font15.render("X:{:03}".format(pointer._x) ,1, (200,200,200))
  1267. window.blit(fr,(10,30))
  1268. fr = font15.render("Y:{:03}".format(pointer._y) ,1, (200,200,200))
  1269. window.blit(fr,(10,40))
  1270. except Exception as e:
  1271. print("Overlay err:",e)
  1272. def draw_box_frame(window,rgb,pos,offset=0):
  1273. p1 = [pos[0],pos[1]]
  1274. p2 = [pos[0]+pos[2],pos[1]+pos[3]]
  1275. o = offset
  1276. _p1 = [p1[0]-o,p1[1]-o]
  1277. _p2 = [p1[0]-o,p2[1]+o]
  1278. pygame.draw.line(window,rgb,_p1,_p2) # left
  1279. _p1 = [p2[0]+o,p2[1]+o]
  1280. pygame.draw.line(window,rgb,_p1,_p2) # bottom
  1281. _p2 = [p2[0]+o,p1[1]-o]
  1282. pygame.draw.line(window,rgb,_p1,_p2) # right
  1283. _p1 = [p1[0]-o,p1[1]-o]
  1284. pygame.draw.line(window,rgb,_p1,_p2) # top
  1285. def draw_frame(window,rgb,p1,p2,offset=0):
  1286. o = offset
  1287. _p1 = [p1[0]-o,p1[1]-o]
  1288. _p2 = [p1[0]-o,p2[1]+o]
  1289. #pygame.draw.line(window,rgb,_p1,_p2) # left
  1290. _p1 = [p2[0]+o,p2[1]+o]
  1291. pygame.draw.line(window,rgb,_p1,_p2) # bottom
  1292. _p2 = [p2[0]+o,p1[1]-o]
  1293. #pygame.draw.line(window,rgb,_p1,_p2) # right
  1294. _p1 = [p1[0]-o,p1[1]-o]
  1295. pygame.draw.line(window,rgb,_p1,_p2) # top
  1296. def calc_fps():
  1297. global fps,frame,frame_t
  1298. t = time.time()
  1299. if frame_t+1 < t:
  1300. fps = frame #frame_t- t #frame
  1301. frame = 1
  1302. frame_t = time.time()
  1303. def calc_fps2():
  1304. global fps2,frame2,frame2_t
  1305. t = time.time()
  1306. if frame2_t+0.1 < t:
  1307. fps2 = frame2*10 #frame_t- t #frame
  1308. frame2 = 1
  1309. frame2_t = t #time.time()
  1310. TEXT_BLOCK = []
  1311. def _create_text_block():
  1312. print("======== CREATE NEW TEXT FILE !!",text_file)
  1313. f = open(text_file,"w")
  1314. for i in range(10):
  1315. f.write("TEXT {}\n".format(i+1))
  1316. f.close()
  1317. def open_text_block():
  1318. print("======== OPEN TEXT FILE !!",text_file)
  1319. _lines = []
  1320. try:
  1321. f = open(text_file,"r")
  1322. _lines = f.readlines()
  1323. f.close()
  1324. except FileNotFoundError as e:
  1325. print("TEXT",e)
  1326. _create_text_block()
  1327. if len(_lines) <= 0:
  1328. _create_text_block()
  1329. lines = []
  1330. for l in _lines:
  1331. #print(">> ",l.strip())
  1332. lines.append(l.strip())
  1333. if len(lines) <= 10:
  1334. for i in range(10-len(lines)):
  1335. lines.append("LINE ERROR")
  1336. return lines
  1337. TEXT_BLOCK = open_text_block()
  1338. TEXT_BLOCK_TIME = time.time()
  1339. # video medialist
  1340. MEDIALIST = []
  1341. def open_medialist():
  1342. print("======== OPEN MEDIALIST DIR !!",media_list)
  1343. if not os.path.isdir(media_list):
  1344. os.system("mkdir -p {}".format(media_list))
  1345. _lines = os.listdir(media_list)
  1346. _lines.sort()
  1347. lines = ['']*25
  1348. i=0
  1349. for l in _lines:
  1350. l = l.strip()
  1351. if "_" in l:
  1352. ll = l.split("_",1)
  1353. print(">> ",ll)
  1354. try:
  1355. lll = int(ll[0])
  1356. lines[lll] = l
  1357. except:pass
  1358. if len(lines) <= 10:
  1359. for i in range(10-len(lines)):
  1360. lines.append("")#"LINE ERROR")
  1361. return lines
  1362. MEDIALIST_TIME = time.time()
  1363. MEDIALIST = open_medialist()
  1364. # ===== GUI =========
  1365. def draw_circle(surface,color, pos, radius):
  1366. x,y=pos
  1367. pygame.gfxdraw.aacircle(surface, int(x), int(y), radius-1, color)
  1368. pygame.gfxdraw.filled_circle(surface, int(x), int(y), radius-1, color)
  1369. def rDMX(univ,dmx):
  1370. return univ*512+dmx
  1371. def generate_grid(mapping=0):
  1372. _log = []
  1373. #if PIXEL_MAPPING:
  1374. # log = open(grid_file,"w")
  1375. head = "i,univ,dmx,x,y,ch\n"
  1376. head = "i,univ,dmx,ch\n"
  1377. head = "univ,dmx,x,y,ch\n"
  1378. head = "nr,id,info\n"
  1379. print("csv:",head)
  1380. _log.append(head)
  1381. dmx = 1-1
  1382. ch = 4
  1383. y=0
  1384. x=0
  1385. for i in range((_y)*(_x)):
  1386. #if x > _x and i%_x == 0:
  1387. if x > 8 and i%8 == 0:
  1388. #print("--> -->")
  1389. x=0
  1390. y+=1
  1391. _univ = int(dmx/512)
  1392. _dmx = dmx - (_univ)*512
  1393. pos=[x,y]
  1394. line="{},{},x\n".format(i+1,i+1)
  1395. _log.append(line)
  1396. dmx += ch
  1397. x+=1
  1398. if mapping and PIXEL_MAPPING:
  1399. print("CREATE NEW PIXELMAP FILE !!",grid_file)
  1400. log = open(grid_file,"w")
  1401. log.writelines(_log)
  1402. log.close()
  1403. return _log[:] #GRID
  1404. def init_grid(mapping=0,_x=4,_y=4,_xp=0,_yp=0,start=0,name="init_gird"):
  1405. if mapping and PIXEL_MAPPING:
  1406. try:
  1407. log = open(grid_file,"r")
  1408. except:
  1409. generate_grid(mapping=mapping)
  1410. log = open(grid_file,"r")
  1411. lines = log.readlines()
  1412. else:
  1413. lines = generate_grid()
  1414. GRID = []
  1415. y=0
  1416. x=0
  1417. print("CSV header",[lines[0]],[PIXEL_MAPPING,"len:",len(lines),"start:",start,name])
  1418. for i,line in enumerate(lines[1+start:]): #exclude first line
  1419. #print("rcsv",[line])
  1420. line = line.strip()
  1421. line = line.split(",") # csv
  1422. if i >= _x and i%_x == 0:
  1423. x=0
  1424. y+=1
  1425. if y >= _y:
  1426. break
  1427. _id = int(line[1])
  1428. pos = [x+_xp,y+_yp]
  1429. f = Fix(_id,pos,block) #pos,univ,dmx,ch)
  1430. GRID.append(f)
  1431. x+=1
  1432. return GRID
  1433. def find_pix(x,y):
  1434. global GRID
  1435. for fix in GRID:
  1436. X = 0
  1437. Y = 0
  1438. pos = fix.POS()
  1439. if x > pos[0] and x < pos[0]+pos[2]:
  1440. X = 1
  1441. if y > pos[1] and y < pos[1]+pos[3]:
  1442. Y = 1
  1443. if X and Y:
  1444. return fix
  1445. GRID = []
  1446. GRID_A = []
  1447. NR = 0
  1448. START_UNIV_A=1
  1449. START_UNIV=2
  1450. if options.start_univ:
  1451. try:
  1452. START_UNIV=int(options.start_univ)
  1453. except Exception as e:
  1454. print("Exception START UNIV",e)
  1455. gobo_ch=1
  1456. if options.gobo_ch:
  1457. try:
  1458. gobo_ch=int(options.gobo_ch)
  1459. except Exception as e:
  1460. print("Exception gobo_ch",e)
  1461. if gobo_ch <= 0:
  1462. gobo_ch = 1
  1463. def draw_box(pos1,pos2,color=[128,128,128],text=1):
  1464. color = [200,0,0,127]
  1465. if text:
  1466. fr = font15.render("A" ,1, (200,200,200))
  1467. window.blit(fr,pos1)
  1468. fr = font15.render("B" ,1, (200,200,200))
  1469. window.blit(fr,[pos2[0]-10,pos2[1]-10])
  1470. # h unten
  1471. _pos1 = [pos1[0],pos2[1]]
  1472. _pos2 = [pos2[0],pos2[1]]
  1473. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1474. color = [255,255,0,127]
  1475. # h rechts
  1476. _pos1 = [pos2[0],pos1[1]]
  1477. _pos2 = [pos2[0],pos2[1]]
  1478. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1479. color = [0,200,0,127]
  1480. # h links
  1481. _pos1 = [pos1[0],pos1[1]]
  1482. _pos2 = [pos1[0],pos2[1]]
  1483. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1484. color = [0,0,200,127]
  1485. # h oben
  1486. _pos1 = [pos1[0],pos1[1]]
  1487. _pos2 = [pos2[0],pos1[1]]
  1488. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1489. def grab(x=55,y=55,w=60,h=60):
  1490. crop = None
  1491. rect = pygame.Rect(x, y, w, h)
  1492. try:
  1493. sub = window.subsurface(rect)
  1494. crop = pygame.Surface((w,h))
  1495. crop.blit(sub, (0,0))
  1496. except ValueError as e:
  1497. pass
  1498. return crop
  1499. vpu_wall_txt="""---,quelle,,---,ziel,---,---
  1500. out,x,y,ang,row,col,info
  1501. Wall1,0,0,0,1,1,
  1502. Wall1,0,0,0,2,2,
  1503. Wall1,0,0,0,3,3,
  1504. Wall1,0,0,0,4,4,
  1505. Wall1,0,0,0,5,5,
  1506. Wall1,0,0,0,6,6,
  1507. Wall1,0,0,0,7,7,
  1508. ,,,,,,
  1509. Wall2,0,0,0,4,1,----
  1510. Wall2,0,0,0,5,2,
  1511. Wall2,4,4,0,6,3,
  1512. Wall2,0,0,0,7,4,
  1513. Wall2,0,0,0,8,5,
  1514. Wall2,0,0,0,9,6,
  1515. ,,,,,,
  1516. ,,,,,,
  1517. Wall3,64,48,0,12,1,----
  1518. Wall3,64,64,0,12,2,
  1519. Wall3,64,80,0,12,3,
  1520. Wall3,64,96,0,12,4,
  1521. Wall3,64,112,0,12,5,
  1522. Wall3,64,128,0,12,6,"""
  1523. def create_vpu_wall_conf():
  1524. f=open(vpu_wall_conf_file,"w")
  1525. f.write(vpu_wall_txt)
  1526. f.close()
  1527. print("CREATE VPU CONFIG !")
  1528. wall_data = []
  1529. _last_wall_config_time = time.time()
  1530. def read_wall_pix(init=0):
  1531. global _last_wall_config_time
  1532. global wall_data, NR
  1533. grid = []
  1534. #if not NR and init==0:
  1535. # return
  1536. ok = 0
  1537. if init:
  1538. ok = 1
  1539. if NR:
  1540. if time.time()-_last_wall_config_time > 3:
  1541. ok = 1
  1542. else:
  1543. if time.time()-_last_wall_config_time > 10:
  1544. ok = 1
  1545. if not ok:
  1546. return
  1547. _last_wall_config_time = time.time()
  1548. if not os.path.isfile(vpu_wall_conf_file):
  1549. create_vpu_wall_conf()
  1550. f=open(vpu_wall_conf_file,"r")
  1551. lines = f.readlines()
  1552. for i,line in enumerate(lines):
  1553. line = line.strip()
  1554. if not line:
  1555. continue
  1556. line = line.split(",")
  1557. if not line[0]:
  1558. continue
  1559. #print(i,line)
  1560. if len(line) >= 3:
  1561. _l = []
  1562. for i in line:
  1563. try:
  1564. _l.append(int(i))
  1565. except Exception as e:
  1566. _l.append(i)
  1567. grid.append(_l)
  1568. for i,v in enumerate(grid):
  1569. print(i,v)
  1570. wall_data = grid
  1571. read_wall_pix(init=1)
  1572. def reshape2(GRID,GRID_OUT,_x,_y,name="GRID_Z"):
  1573. _w = 16
  1574. _h = 16
  1575. from_x_min = INPUT_START_POS_X
  1576. from_y_min = INPUT_START_POS_Y
  1577. target_x = 40 # min
  1578. target_y = 60 # min
  1579. ang=0
  1580. #read_block_pix()
  1581. read_wall_pix()
  1582. _wall_data = wall_data[:]
  1583. for i,_wall in enumerate(_wall_data):
  1584. #print(_wall)
  1585. if len(_wall) <=6:
  1586. continue
  1587. try:
  1588. if type(_wall[1]) is str:
  1589. continue
  1590. if type(_wall[2]) is str:
  1591. continue
  1592. if type(_wall[3]) is str:
  1593. continue
  1594. if type(_wall[4]) is str:
  1595. continue
  1596. if type(_wall[5]) is str:
  1597. continue
  1598. from_x=from_x_min+_wall[1]#*_w
  1599. from_y=from_y_min+_wall[2]#*_h
  1600. _a= _wall[3]
  1601. ang=0
  1602. if _a < 0:
  1603. ang=0
  1604. elif _a > 0 and _a <= 90:
  1605. ang = 90
  1606. elif _a > 90 and _a <= 180:
  1607. ang = 180
  1608. elif _a > 180 and _a <= 270:
  1609. ang = 270
  1610. target_x=40+(_wall[4]-1)*_w
  1611. target_y=60+(_wall[5]-1)*_h
  1612. sub = grab(from_x,from_y,_w,_h)
  1613. #draw_box_frame(window,[0,40,0],[from_x+x,from_y+y,_w,_h])
  1614. if sub:
  1615. sub = pygame.transform.rotate(sub,ang)
  1616. window.blit(sub,(target_x,target_y))
  1617. except Exception as e:
  1618. print(" ",_wall)
  1619. print("WALL err:",e)
  1620. def reshape(GRID,GRID_OUT,_x,_y,name="GRID_Z"):
  1621. """reshape LED-WALL Block/Pixel mapping"""
  1622. if PIXEL_MAPPING <= 0:
  1623. return None
  1624. x = _x
  1625. y = _y
  1626. i = 0
  1627. counter = 0
  1628. z=0
  1629. x_min = 99999
  1630. x_max = 0
  1631. y_min = 99999
  1632. y_max = 0
  1633. pos= [0,0]
  1634. pos[0] = _x+p*3 #+10
  1635. pos[1] = p*8+65 #-10 #_y+p*8+80
  1636. tmp_font = pygame.font.SysFont("freemonobold",int(p*0.8))
  1637. #fr = tmp_font.render("OUTPUT: "+str(name) ,1, (255,255,255))
  1638. #fr_r = fr.get_rect(center=(int(wx/2),int(0+pm_wy-p*0-10)))
  1639. #window.blit(fr,pos)
  1640. fr = tmp_font.render("INPUT" ,1, (255,255,255))
  1641. fr_r = fr.get_rect(center=(int(wx/2),int(60+pm_wy-p/2)))
  1642. window.blit(fr,fr_r)
  1643. j = 0
  1644. for fix in GRID_OUT:
  1645. if j >= 64:
  1646. break
  1647. j+=1
  1648. ii = i
  1649. #pos = fix.POS(40,60)
  1650. pos = fix.POS() #(0,0)
  1651. rgb = fix.rgb
  1652. pos[0]+=_x
  1653. pos[1]+=_y
  1654. # green
  1655. pygame.draw.rect(window,[0,40,0],pos)
  1656. xposs = [] #None #pos[:]
  1657. for fix2 in GRID:
  1658. if fix._id == fix2._id:
  1659. #_xposs = fix2.POS(40,60)
  1660. #_xposs = fix2.POS(0,0)#_x,_y)
  1661. _xposs = fix2.POS() #(CFG_IN["x1"],CFG_IN["y1"])
  1662. xposs.append( _xposs) #fix2.POS(40,60) )
  1663. # ToDo
  1664. #sub = grab(10,10,60,60)
  1665. #sub = pygame.transform.rotate(sub,90)
  1666. #wn.blit(sub, (500,10))
  1667. for xpos in xposs:
  1668. #sub = grab(xpos[0],xpos[1]+pm_wy,xpos[2],xpos[3])
  1669. sub = grab(xpos[0],xpos[1],xpos[2],xpos[3])
  1670. if NR:
  1671. pygame.draw.rect(window,[255,0,255],xpos,1) # frame on grab area
  1672. if sub:
  1673. window.blit(sub, (pos[0]+z,pos[1]+z))
  1674. else:
  1675. # red
  1676. pygame.draw.rect(window,[40,0,0],pos) #[x+pos[0]+2+z,y+pos[1]+2+z-pm_wy,12,9])
  1677. if xpos[0] < x_min:
  1678. x_min = xpos[0]
  1679. if xpos[0] > x_max:
  1680. x_max += xpos[2]
  1681. if xpos[1] < y_min:
  1682. y_min = xpos[1]
  1683. if xpos[1] > x_max:
  1684. y_max += xpos[3]
  1685. # DRAW FIX NUMBER on TOP
  1686. #apos = fix.POS(40,60)#+pm_wy)
  1687. apos = fix.POS() #0,0)#+pm_wy)
  1688. apos[0]+=_x
  1689. apos[1]+=_y
  1690. argb = fix.rgb
  1691. # overwrite number overlay
  1692. if NR:
  1693. pygame.draw.rect(window,[20,20,20],[apos[0],apos[1],12,8])
  1694. fix_id = fix._id
  1695. if fix_id > 8*8:
  1696. fix_id -= 8*8
  1697. if NR:# == 2: # sub fix_nr
  1698. if fix_id != i+1:
  1699. fr = font12.render("{:02}".format(fix._id) ,1, (255,255,0))
  1700. window.blit(fr,(apos[0],apos[1]))
  1701. else:
  1702. fr = font12.render("{:02}".format(fix._id) ,1, (100,100,255))
  1703. window.blit(fr,(apos[0],apos[1]))
  1704. i += 1
  1705. #print("--#")
  1706. # frame box
  1707. pos1= [x+x_min,y+x_min]
  1708. pos2= [x_max+x_min,y_min+y_max]
  1709. class Timer():
  1710. def __init__(self,start=120):
  1711. self.start = start
  1712. self.timer = self.start
  1713. self.timer_t = time.time()
  1714. def reset(self):
  1715. self.timer = self.start
  1716. def get(self):
  1717. self.timer -= time.time()-self.timer_t
  1718. self.timer_t = time.time()
  1719. if self.timer <= 0:
  1720. self.reset()
  1721. return self.timer
  1722. def reload_grid():
  1723. print("==== reload_grid")
  1724. global GRID
  1725. global GRID_A
  1726. try:
  1727. GRID = init_grid(_x=_x,_y=_y,name="GRID") #init_gird()
  1728. GRID_A = init_grid(_x=8,_y=16,mapping=1,name="GRID_A") #init_gird()
  1729. except Exception as e:
  1730. print("Except: grid re init",e)
  1731. VPU_TEXT = []
  1732. def load_vpu_text(nr=0):
  1733. txt = "NONE"
  1734. if len(VPU_TEXT) > nr:
  1735. txt = VPU_TEXT[0]
  1736. return txt
  1737. import string
  1738. def draw_kachel_nr(): #GRID,_x=0,_y=0):
  1739. if not NR:
  1740. return
  1741. n = 0
  1742. _max_row = 40
  1743. _y=0
  1744. for i in range(8): # ROW / ZEILEN -> ->
  1745. _x=0
  1746. for j in range(30): # COLUMS / SPALTEN | |
  1747. pygame.draw.rect(window,[10,20,10],[40+_x+2,60+_y+2,14,9]) #background
  1748. _j=j #string.ascii_uppercase[j]
  1749. fr = font12.render("{}:{}".format(_j+1,i+1) ,1, (200,200,55))
  1750. window.blit(fr,(40+_x+2,60+_y+2))
  1751. #fr = font12.render("{}".format(_j) ,1, (200,200,55))
  1752. #window.blit(fr,(40+_x+2,60+_y+2))
  1753. #fr = font12.render(":{}".format(i+1) ,1, (200,200,55))
  1754. #window.blit(fr,(40+_x+2+6,60+_y+2))
  1755. _x+=16 # pix
  1756. _y +=16 # pix
  1757. grid_counter = time.time()
  1758. def draw_counter(COUNTER):
  1759. for count in COUNTER:
  1760. cpan = 0
  1761. ctilt = 0
  1762. cr=255
  1763. cg=255
  1764. cb=255
  1765. csize=10
  1766. cdim=0
  1767. k = "DIM"
  1768. if k in count:
  1769. cdim = int(count[k])
  1770. k = "RED"
  1771. if k in count:
  1772. cr = int(count[k])
  1773. k = "GREEN"
  1774. if k in count:
  1775. cg = int(count[k])
  1776. k = "BLUE"
  1777. if k in count:
  1778. cb = int(count[k])
  1779. k = "SIZE"
  1780. if k in count:
  1781. csize = int(count[k]) #/60*p)
  1782. if csize < 5:
  1783. csize = 5
  1784. k = "PAN"
  1785. if k in count:
  1786. cpan = int(count[k])/255*(block[0] *(_x))
  1787. #cpan = int(cpan * 1.2)
  1788. cpan = int(cpan)
  1789. k = "TILT"
  1790. if k in count:
  1791. ctilt = int(count[k])/255*(block[1] *(_y))
  1792. #ctilt = int(cpan * 1.2)
  1793. ctilt = int(ctilt)
  1794. ddim = cdim/255
  1795. if "DIM" in count and count["DIM"] > 0:
  1796. tmp_font = pygame.font.SysFont("freemonobold",int(p/100*csize))
  1797. rgb =(int(cr*ddim),int(cg*ddim),int(cb*ddim),cdim)
  1798. _sec = count["_SEC"]
  1799. try:
  1800. _sec = int(count["_SEC"])
  1801. except:
  1802. pass
  1803. if type(_sec) is int:
  1804. #print(_sec)
  1805. if _sec <= 60:
  1806. fr = tmp_font.render("{:0}".format(_sec) ,1, rgb)
  1807. else:
  1808. _xx = time.strftime("%M:%S",time.localtime(_sec))
  1809. #print("_xx",_xx)
  1810. fr = tmp_font.render("{}".format(_xx) ,1, rgb)
  1811. else:
  1812. fr = tmp_font.render("{}".format((count["_SEC"])) ,1, rgb)
  1813. fr_r = fr.get_rect(center=(40+cpan-(block[0]),60+ctilt+pm_wy))
  1814. pygame.draw.rect(window,[0,0,0],fr_r)
  1815. window.blit(fr,fr_r)
  1816. class FADE():
  1817. def __init__(self,sec):
  1818. self.start = time.time()
  1819. self.sec = sec
  1820. self.val
  1821. def next(self):
  1822. pass
  1823. def video_overlay_draw(wn=None,lines=["line1","line2"],i=0):
  1824. # overlay
  1825. pygame.draw.rect(wn,[255,200,0],[5,MAIN_SIZE[1]-(i+1)*35,380,28])
  1826. font15 = pygame.font.SysFont("freemonobold",17)
  1827. rgb = [255,100,0]
  1828. pygame.draw.rect(wn,rgb,[5,MAIN_SIZE[1]-(i+1)*35,200,13])
  1829. _line = lines[0]
  1830. fr = font15.render(_line ,1, (0,0,0))
  1831. wn.blit(fr,(10,MAIN_SIZE[1]-(i+1)*35))
  1832. _line = lines[1]
  1833. fr = font15.render(_line ,1, (0,0,0))
  1834. wn.blit(fr,(10,MAIN_SIZE[1]-(i+1)*35+15))
  1835. #_line = lines[2]
  1836. #fr = font15.render(_line ,1, (0,0,0))
  1837. #wn.blit(fr,(75,MAIN_SIZE[1]-(i+1)*35+15))
  1838. def draw_all_video_overlay(VIDEO):
  1839. global videplayer
  1840. i=0
  1841. for count in VIDEO:
  1842. video1 = videoplayer[i]
  1843. #video1.overlay_draw(window,"run") # old
  1844. lines = video1._overlay_text()
  1845. video_overlay_draw(wn=window,lines=lines,i=i)
  1846. i += 1
  1847. def del_all_video():
  1848. global videplayer
  1849. i = 0
  1850. print()
  1851. cprint("del_all_video")
  1852. for video in videoplayer:
  1853. cprint("- del video",video)
  1854. video._del()
  1855. def draw_all_video(VIDEO):
  1856. global videplayer
  1857. i = 0
  1858. # set DMX-VALUE to videoplayer Object
  1859. for count in VIDEO:
  1860. cpan = 0
  1861. ctilt = 0
  1862. cr=255
  1863. cg=255
  1864. cb=255
  1865. csize=10
  1866. cang=0
  1867. cdim=0
  1868. video1 = videoplayer[i]
  1869. k = "VIDEO"
  1870. if k in count:
  1871. #video1.select_video(count[k])
  1872. play_nr = int(count[k]/10)
  1873. if play_nr != video1._media_nr:
  1874. print( "+ + + + + + + + + + +", play_nr , video1._media_nr ,str(video1))
  1875. _vid = video1._id
  1876. _cdmx = video1.dmx
  1877. del_video1 = video1
  1878. video1 = PixelMedia(cdmx,_id=_vid)
  1879. video1.select_video(count[k])
  1880. videoplayer[i] = video1
  1881. del_video1._del()
  1882. del del_video1
  1883. k = "DIM"
  1884. if k in count:
  1885. cdim = int(count[k])
  1886. #video1.dim = cdim
  1887. #if i == 0:
  1888. # print(i,cdim)
  1889. k = "ANG"
  1890. if k in count:
  1891. cang = int(count[k])
  1892. k = "SIZE"
  1893. if k in count:
  1894. csize = int(count[k]/16*p)
  1895. if csize < 5:
  1896. csize = 5
  1897. k = "PAN"
  1898. cpan_max = block[0] *(_x) #+block[0]
  1899. if k in count:
  1900. #if 0:#video1.fname.startswith("cam_"):
  1901. # cpan = int(count[k]) #/ 255*cpan_max
  1902. #else:
  1903. cpan = int(count[k]) / 255*cpan_max
  1904. cpan = int(cpan)
  1905. k = "TILT"
  1906. ctilt_max = block[1] *(_y) #+block[1]
  1907. if k in count:
  1908. ctilt = int(count[k]) / 255*ctilt_max
  1909. ctilt = int(ctilt)
  1910. k = "_reset"
  1911. if k in count:
  1912. if count[k]:
  1913. count[k] = 0
  1914. video1.restart()
  1915. k = "_RUN"
  1916. if k in count:
  1917. video1._run = count[k]
  1918. #video1.pos
  1919. #video1.x=40+0+cpan
  1920. #video1.y=60+0+pm_wy+ctilt
  1921. x=CFG_IN["x1"]+cpan
  1922. y=CFG_IN["y1"]+ctilt
  1923. scale = int((csize))
  1924. angle = int(360-(cang))
  1925. video1.update_dmx(x,y,scale,angle,cdim)
  1926. if cdim:
  1927. video1.next()
  1928. i += 1
  1929. # draw
  1930. i=0
  1931. for count in VIDEO:
  1932. video1 = videoplayer[i]
  1933. video1.draw(window) #,x=0,y=0)
  1934. i+=1
  1935. def counter_dmx(COUNTER,dataA):
  1936. for count in COUNTER:
  1937. cDMX=count["DMX"]-1
  1938. try:
  1939. count["DIM"] = dataA[cDMX]
  1940. count["PAN"] = dataA[cDMX+1]
  1941. count["TILT"] = dataA[cDMX+2]
  1942. count["CONTROL"] = dataA[cDMX+3]
  1943. count["SIZE"] = dataA[cDMX+4]
  1944. count["SEC"] = dataA[cDMX+5]
  1945. count["RED"] = dataA[cDMX+6]
  1946. count["GREEN"] = dataA[cDMX+7]
  1947. count["BLUE"] = dataA[cDMX+8]
  1948. if count["CONTROL"] >= 10 and count["CONTROL"] < 20: # RESET COUNTER
  1949. count["_RUN"] = 0
  1950. count["_pause"] = 0
  1951. count["_time"] = time.time()
  1952. count["_start"] = int(count["SEC"])
  1953. count["_SEC"] = int(count["_start"])
  1954. if count["CONTROL"] >= 20 and count["CONTROL"] < 30: # PAUSE COUNTER
  1955. count["_RUN"] = 0
  1956. if count["CONTROL"] >= 30 and count["CONTROL"] < 40: # PLAY COUNTER
  1957. count["_RUN"] = 1
  1958. if count["_RUN"]:
  1959. try:
  1960. if count["_pause"] > 0:
  1961. count["_time"] += time.time()-count["_pause"]
  1962. count["_pause"] = 0
  1963. count["_SEC"] = int(count["_start"] - (time.time() - count["_time"]))
  1964. except Exception as e:
  1965. pass
  1966. else:
  1967. if count["_pause"] == 0:
  1968. count["_pause"] = time.time()
  1969. if type(count["_SEC"]) is int:
  1970. if count["_SEC"] < 0:
  1971. count["_SEC"] = 0
  1972. for ti in range(10):
  1973. #print(ti,(ti+6)*10)
  1974. if count["CONTROL"] >= (ti+6)*10 and count["CONTROL"] < (ti+7)*10:
  1975. count["_SEC"] = "----" #text 1
  1976. try:
  1977. count["_SEC"] = TEXT_BLOCK[ti]
  1978. except Exception as e:
  1979. pass
  1980. if count["CONTROL"] >= 250 and count["CONTROL"] < 256:
  1981. count["_SEC"] = "DMX:{}-{}".format(cDMX+1,cDMX+8+1)
  1982. except Exception as e:
  1983. print("EXC FUNC",e,count)
  1984. #print(count)
  1985. def video_dmx(VIDEO,dataA):
  1986. for count in VIDEO:
  1987. cDMX=count["DMX"]-1
  1988. try:
  1989. count["DIM"] = dataA[cDMX]
  1990. count["PAN"] = dataA[cDMX+1]
  1991. count["TILT"] = dataA[cDMX+2]
  1992. count["CONTROL"] = dataA[cDMX+3]
  1993. if count["CONTROL"] >= 10 and count["CONTROL"] < 20:
  1994. count["_reset"] = 1
  1995. if count["CONTROL"] >= 20 and count["CONTROL"] < 30:
  1996. if count["_RUN"] == 1:
  1997. print( "_RUN:0",count["DMX"]-1)
  1998. count["_RUN"] = 0
  1999. if count["CONTROL"] >= 30 and count["CONTROL"] < 40:
  2000. if count["_RUN"] == 0:
  2001. print( "_RUN:1",count["DMX"]-1)
  2002. count["_RUN"] = 1
  2003. count["SIZE"] = dataA[cDMX+4]#*2
  2004. count["SEC"] = dataA[cDMX+5]
  2005. #count["RED"] = dataA[cDMX+6]
  2006. count["VIDEO"] = dataA[cDMX+6]
  2007. #count["GREEN"] = dataA[cDMX+7]
  2008. count["ANG"] = (int(dataA[cDMX+7])/255*360)
  2009. #count["BLUE"] = dataA[cDMX+8]
  2010. except Exception as e:
  2011. print("VIDEOPLAYER EXCEPT FUNC",e,count)
  2012. #print(count)
  2013. def read_dmx_data(ip,ips):
  2014. ip = select_ip(ips,univ=START_UNIV)
  2015. IP = ip
  2016. data = read_dmx(ip)
  2017. ip = select_ip(ips,univ=START_UNIV+1)
  2018. data3 = read_dmx(ip)
  2019. data.extend(data3)
  2020. ip = select_ip(ips,univ=START_UNIV+2)
  2021. data3 = read_dmx(ip)
  2022. data.extend(data3)
  2023. ip = select_ip(ips,univ=START_UNIV+3)
  2024. data3 = read_dmx(ip)
  2025. data.extend(data3)
  2026. if options.dual_vpu:
  2027. ip = select_ip(ips,univ=START_UNIV+4)
  2028. data3 = read_dmx(ip)
  2029. data.extend(data3)
  2030. ip = select_ip(ips,univ=START_UNIV+5)
  2031. data3 = read_dmx(ip)
  2032. data.extend(data3)
  2033. return data
  2034. def set_pos(GRID,_x=0,_y=0):
  2035. for fix in GRID:
  2036. pos = fix.POS(_x,_y)#40,60+pm_wy)
  2037. for subfix in fix.sub_fix:
  2038. #subfix.dmc_calc(data)
  2039. spos = subfix.POS(_x,_y)
  2040. def draw_gobo(GRID,data,_x=0,_y=0):
  2041. i = 0
  2042. dmx = 1
  2043. h = 1
  2044. v = 1
  2045. for fix in GRID:
  2046. rgb = fix.rgb
  2047. pos = fix.POS() #CFG_IN["x1"],CFG_IN["y1"])
  2048. pygame.draw.rect(window,rgb,pos)
  2049. # DRAW SUB-FIXTURE
  2050. j = 0
  2051. for subfix in fix.sub_fix:#calc(data):
  2052. subfix.dmx_calc(data)
  2053. spos = subfix.POS() #_x,_y)#+pm_wy)
  2054. srgb = subfix.rgb
  2055. if FUNC > 10 and FUNC <= 20: # big dot
  2056. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  2057. elif FUNC > 20 and FUNC <= 30:#small dot
  2058. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  2059. elif FUNC > 30 and FUNC <= 40:#donut
  2060. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  2061. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  2062. elif FUNC > 40 and FUNC <= 50: # rec with hole
  2063. pygame.draw.rect(window,srgb,spos)
  2064. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  2065. elif FUNC > 50 and FUNC <= 60: # rec with big hole
  2066. pygame.draw.rect(window,srgb,spos)
  2067. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  2068. elif FUNC > 60 and FUNC <= 70: # rec with donat
  2069. pygame.draw.rect(window,srgb,spos)
  2070. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  2071. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  2072. elif FUNC > 70 and FUNC <= 80: # rec boarder
  2073. pygame.draw.rect(window,srgb,[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  2074. elif FUNC > 80 and FUNC <= 90: # rec big boarder
  2075. pygame.draw.rect(window,srgb,[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  2076. elif FUNC > 90 and FUNC <= 100: # rec thin line
  2077. pygame.draw.rect(window,srgb,spos)
  2078. pygame.draw.rect(window,[0,0,0],[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  2079. elif FUNC > 100 and FUNC <= 110: # rec big line
  2080. pygame.draw.rect(window,srgb,spos)
  2081. pygame.draw.rect(window,[0,0,0],[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  2082. elif FUNC > 110 and FUNC <= 120: # rec with dot
  2083. pygame.draw.rect(window,srgb,spos)
  2084. pygame.draw.rect(window,[0,0,0],[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  2085. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  2086. elif FUNC > 120 and FUNC <= 130: # rec inline
  2087. pygame.draw.rect(window,srgb,[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  2088. pygame.draw.rect(window,[0,0,0],[spos[0]+3,spos[1]+3,spos[2]-6,spos[3]-6])
  2089. elif FUNC > 130 and FUNC <= 140: # 3 dot (heart)
  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)-2,spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  2092. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)+2),int(spos[3]/3.5))
  2093. else:
  2094. pygame.draw.rect(window,srgb,spos)
  2095. i += 1
  2096. def grid_label(GRID,_x=0,_y=0):
  2097. for fix in GRID:
  2098. #pos = fix.POS(_x,_y)
  2099. pos = fix.POS() #CFG_IN["x1"],CFG_IN["y1"])
  2100. rgb = fix.rgb
  2101. # draw row/col grid number
  2102. if pos[0] == 0:
  2103. fr = font12.render("{}".format(pos[1]+1) ,1, (200,200,200))
  2104. window.blit(fr,(10,pos[1]+3 ))
  2105. if pos[1] == 0:
  2106. fr = font12.render("{}".format(pos[0]+1) ,1, (200,200,200))
  2107. window.blit(fr,(pos[0]+2,35 ))
  2108. #pygame.draw.rect(window,rgb,pos)
  2109. def grid_sub_label(GRID,_x=0,_y=0):
  2110. h = 1
  2111. v = 1
  2112. for fix in GRID:
  2113. j = 0
  2114. #pos = fix.POS(_x,_y)
  2115. pos = fix.POS() #CFG_IN["x1"],CFG_IN["y1"])
  2116. for subfix in fix.sub_fix:
  2117. subfix.dmx_calc(data)
  2118. spos = subfix.POS() #(_x,_y)
  2119. srgb = subfix.rgb
  2120. # draw row/col grid number
  2121. if subfix.pos[0] == 0:
  2122. fr = font12.render("{}".format(v ) ,1, (200,200,200))
  2123. window.blit(fr,(25,spos[1] ))
  2124. v += 1
  2125. if subfix.pos[1] == 0:
  2126. fr = font12.render("{}".format(1) ,1, (200,200,200))
  2127. fr = font12.render("{}".format(h ) ,1, (200,200,200))
  2128. h+=1
  2129. window.blit(fr,(spos[0],50 ))
  2130. if p >= 40:
  2131. if NR:
  2132. fr = font15.render("{:02}".format(subfix._id) ,1, (250,200,5))
  2133. window.blit(fr,(spos[0]+2,spos[1]+10))
  2134. j += 1
  2135. def frame_area():
  2136. rgb = [255,255,0]
  2137. CFG = CFG_IN
  2138. p1 = CFG_IN["p1"]
  2139. p2 = CFG_IN["p2"]
  2140. draw_frame(window,rgb,p1,p2,offset=2)
  2141. rgb = [255,0,0]
  2142. #draw_frame(window,rgb,p1,p2,offset=4)
  2143. if CFG_OUT["on"]:
  2144. rgb = [255,0,0]
  2145. p1 = CFG_OUT["p1"]
  2146. p2 = CFG_OUT["p2"]
  2147. #pygame.draw.line(window,rgb,p1,p2)
  2148. draw_frame(window,rgb,p1,p2,offset=3)
  2149. if CFG_OUT2["on"]:
  2150. rgb = [255,0,0]
  2151. p1 = CFG_OUT2["p1"]
  2152. p2 = CFG_OUT2["p2"]
  2153. #pygame.draw.line(window,rgb,p1,p2)
  2154. draw_frame(window,rgb,p1,p2,offset=3)
  2155. ips=[]
  2156. dataA=[]
  2157. data=[]
  2158. def dmx_raw():
  2159. global frame
  2160. frame += 1
  2161. global ips,dataA,data
  2162. ips = read_index()
  2163. # ----
  2164. ip = select_ip(ips,univ=1) # univ 1 gobo
  2165. dataA = read_dmx(ip)
  2166. # ----
  2167. data = read_dmx_data(ip,ips)
  2168. if options.countdown:
  2169. counter_dmx(COUNTER,dataA)
  2170. if len(VIDEO) > 0:
  2171. video_dmx(VIDEO,dataA)
  2172. return ips,dataA,data
  2173. def dmx_loop():
  2174. while 1:
  2175. dmx_raw()
  2176. time.sleep(1/40) # fast high cpu
  2177. #time.sleep(1/25)
  2178. if QUIT:
  2179. break
  2180. dmx_raw()
  2181. thread.start_new_thread(dmx_loop,())
  2182. def draw_output_label(CFG,value,dmx="0.0",name="GRID_XX"):
  2183. dim_raw = value
  2184. pygame.draw.rect(window,[5,5,155],[CFG["x1"]-5,CFG["y2"]+4,130,35]) # background
  2185. # GRID_A1_DIM_LABEL
  2186. fr = font15.render("Output:"+name,1, (255,255,255))
  2187. fr_r = fr.get_rect(center=(int(wx/2),int(0+pm_wy-p*0-10)))
  2188. window.blit(fr,(CFG["x1"],CFG["y2"]+5))
  2189. fr = font15.render("inv-dim: "+str(dim_raw) ,1, (255,255,255))
  2190. fr_r = fr.get_rect(center=(int(wx/2),int(0+pm_wy-p*0-10)))
  2191. window.blit(fr,(CFG["x1"]+60,CFG["y2"]+15))
  2192. fr = font15.render("dmx: {}.{}".format(START_UNIV_A,dmx) ,1, (255,255,255))
  2193. window.blit(fr,(CFG["x1"],CFG["y2"]+15))
  2194. def GRID_DIM(dim,x,y):
  2195. try:
  2196. dim = int(grid_dim_v)
  2197. if dim > 255:
  2198. dim = 255
  2199. if dim <=0:
  2200. dim = 0
  2201. except:pass
  2202. dim_raw = dim*-1
  2203. s = pygame.Surface((p*8,p*8)) # the size of your rect
  2204. s.set_alpha(dim) # alpha level
  2205. s.fill((0,0,0)) # this fills the entire surface
  2206. window.blit(s, (x,y))
  2207. def GRID_A1_DIM():
  2208. # GRID_A1 DIM
  2209. if options.grid_a1_idim:
  2210. CFG=CFG_OUT
  2211. grid_dim_ch = int(options.grid_a1_idim)
  2212. grid_dim_v = dataA[grid_dim_ch-1]
  2213. dim=255
  2214. dim=grid_dim_v
  2215. #dim=120
  2216. GRID_DIM(dim,x=CFG["x1"],y=CFG["y2"]-p*8)
  2217. a=options.grid_a1_idim
  2218. draw_output_label(CFG,dim,a,name="GRID_A1_DIM")
  2219. def GRID_A2_DIM():
  2220. # GRID_A2 DIM
  2221. if options.grid_a2_idim:
  2222. CFG=CFG_OUT2
  2223. grid_dim_ch = int(options.grid_a2_idim)
  2224. grid_dim_v = dataA[grid_dim_ch-1]
  2225. dim=255
  2226. try:
  2227. dim = int(grid_dim_v)
  2228. if dim > 255:
  2229. dim = 255
  2230. if dim <=0:
  2231. dim = 0
  2232. except:pass
  2233. dim_raw = dim*-1
  2234. s = pygame.Surface((p*8,p*8)) # the size of your rect
  2235. s.set_alpha(dim) # alpha level
  2236. s.fill((0,0,0)) # this fills the entire surface
  2237. window.blit(s, (CFG["x1"],CFG["y2"]-p*8))
  2238. a=options.grid_a2_idim
  2239. draw_output_label(CFG,dim_raw,a,name="GRID_A2_DIM")
  2240. def cvImageToSurface(cvImage):
  2241. if cvImage.dtype.name == 'uint16':
  2242. cvImage = (cvImage / 256).astype('uint8')
  2243. size = cvImage.shape[1::-1]
  2244. if len(cvImage.shape) == 2:
  2245. cvImage = np.repeat(cvImage.reshape(size[1], size[0], 1), 3, axis = 2)
  2246. format = 'RGB'
  2247. else:
  2248. format = 'RGBA' if cvImage.shape[2] == 4 else 'RGB'
  2249. cvImage[:, :, [0, 2]] = cvImage[:, :, [2, 0]]
  2250. surface = pygame.image.frombuffer(cvImage.flatten(), size, format)
  2251. return surface.convert_alpha() if format == 'RGBA' else surface.convert()
  2252. def create_img_meta():
  2253. out = {}
  2254. #40 235
  2255. out["pos"] = (0,0) # x,y
  2256. out["ipos"] = (0,0) # x,y
  2257. out["scale"] = 50
  2258. out["dim"] = 100
  2259. out["ang"] = 0
  2260. out["_fpath"] = '/opt/LibreLight/Xdesk/icon/'
  2261. out["fname"] = "picker.png"
  2262. out["raw_img"] = ""
  2263. out["img"] = ""
  2264. out["err"] = []
  2265. return out
  2266. def img_open(img_meta):
  2267. cprint("img_open",img_meta["fname"],"-------")
  2268. fname = img_meta["_fpath"] +"/"+ img_meta["fname"] #'/opt/LibreLight/Xdesk/icon/picker.png'
  2269. im2 = cv2.imread(fname)
  2270. width = int(im2.shape[1])
  2271. height = int(im2.shape[0])
  2272. w_max = 480
  2273. h_max = 320
  2274. #w_max = 600
  2275. #h_max = 400
  2276. if width > w_max:
  2277. h = int(height*(w_max/width))
  2278. w = w_max
  2279. im2 = cv2.resize(im2, (w,h), interpolation =cv2.INTER_LINEAR)
  2280. if height > h_max:
  2281. w = int(width*(h_max/height))
  2282. h = h_max
  2283. if w < w_max:
  2284. im2 = cv2.resize(im2, (w,h), interpolation =cv2.INTER_LINEAR)
  2285. img_meta["raw_img"] = im2
  2286. img_meta["img"] = im2
  2287. return im2
  2288. def img_jitter(img_meta):
  2289. r1=random.randint(0,3)
  2290. r2=random.randint(0,3)
  2291. img_meta["ipos"] = [r1,r2]
  2292. def img_rescale_width(img_meta, _width):
  2293. frame = img_meta["raw_img"]
  2294. width = int(frame.shape[1])
  2295. height = int(frame.shape[0])
  2296. dim = (width, height)
  2297. if 1: #width > _width:
  2298. h = int(height*( width/_width))
  2299. dim = (_width, h)
  2300. im = cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
  2301. else:
  2302. im = cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
  2303. return im
  2304. def img_rescale(img, percent=75):
  2305. frame = img
  2306. #frame = img_meta["raw_img"]
  2307. width = int(frame.shape[1] * percent/ 100)
  2308. height = int(frame.shape[0] * percent/ 100)
  2309. dim = (width, height)
  2310. return cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
  2311. def img_draw_raw(img_meta):
  2312. #img_jitter(img_meta)
  2313. pos = img_meta["pos"]
  2314. ipos = img_meta["ipos"]
  2315. im2 = img_meta["img"]
  2316. dim = img_meta["dim"]
  2317. im2 = cvImageToSurface(im2)
  2318. w2 = int(im2.get_width()/2)
  2319. h2 = int(im2.get_height()/2)
  2320. x=pos[0]+ipos[0]
  2321. y=pos[1]+ipos[1]
  2322. window.blit(im2, (x-w2,y-h2))
  2323. img_meta_a = create_img_meta()
  2324. img_open(img_meta_a)
  2325. t1 = Timer(143)
  2326. time.sleep(0.33)
  2327. t2 = Timer(11)
  2328. count_tilt = 0
  2329. def main():
  2330. global IP
  2331. global GRID
  2332. global GRID_A
  2333. global FUNC
  2334. global count_tilt
  2335. global TEXT_BLOCK
  2336. global TEXT_BLOCK_TIME
  2337. global MEDIALIST
  2338. global MEDIALIST_TIME
  2339. global dataA
  2340. global frame2
  2341. global runnung
  2342. reload_grid()
  2343. s=time.time()
  2344. r = ""
  2345. IP = "xx"
  2346. while running:
  2347. if TEXT_BLOCK_TIME+5 < time.time():
  2348. TEXT_BLOCK = open_text_block()
  2349. TEXT_BLOCK_TIME = time.time()
  2350. if MEDIALIST_TIME+6 < time.time():
  2351. MEDIALIST = open_medialist()
  2352. MEDIALIST_TIME = time.time()
  2353. pygame.display.flip()
  2354. set_pos(GRID,_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2355. #set_pos(GRID,600,600)#_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2356. event()
  2357. window.fill((10,0,30))
  2358. calc_fps()
  2359. calc_fps2()
  2360. #draw_overlay()
  2361. # GRID loop
  2362. try:
  2363. FUNC = dataA[gobo_ch-1]
  2364. FUNC2 = FUNC
  2365. except Exception as e:
  2366. print("EXC FUNC",e)
  2367. #if int(time.time()) % 2: # BLINK
  2368. #set_gobo_pos(GRID,_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2369. draw_gobo(GRID,data) #,_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2370. #if int(time.time()) % 2: # BLINK
  2371. grid_label(GRID,_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2372. #grid_sub_label(GRID) #,_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2373. if VIDEO:
  2374. draw_all_video(VIDEO)
  2375. #draw_all_video_overlay(VIDEO)
  2376. #img_draw(img_meta_a)
  2377. if options.countdown:
  2378. draw_counter(COUNTER)
  2379. # output background mask
  2380. pygame.draw.rect(window,[0,0,0],[35,57,MAIN_SIZE[0]+200-35,135]) # background
  2381. #pygame.draw.rect(window,[5,5,5],[0,60,MAIN_SIZE[0],p*9]) # background
  2382. pointer.draw(0,pm_wy) #wy
  2383. spos = [0,0,0,0]
  2384. if PIXEL_MAPPING >= 1:
  2385. #try:
  2386. if options.dual_vpu:
  2387. GRID_X = GRID_A[8*8:]
  2388. xx = p*8
  2389. #reshape(GRID,GRID_X,CFG_OUT2["x1"],CFG_OUT2["y1"]-p*8,name="GRID_A2") #start pos
  2390. #except Exception as e:
  2391. # print("Exception 23123",e)
  2392. reshape2(GRID,GRID_A,CFG_OUT["x1"],CFG_OUT["y1"],name="GRID_A1") #start pos
  2393. #reshape(GRID,GRID_A,CFG_OUT["x1"],CFG_OUT["y1"],name="GRID_A1") #start pos
  2394. else:
  2395. pass #reshape(GRID,GRID_A,spos[0]+spos[2]+20,10) #start pos
  2396. frame_area()
  2397. # OUTPUT -> MAPING
  2398. draw_kachel_nr()
  2399. input_mask() # mask background
  2400. output_mask() # mask background
  2401. GRID_A1_DIM() # overlay
  2402. GRID_A2_DIM() # overlay
  2403. if VIDEO:
  2404. draw_all_video_overlay(VIDEO)
  2405. draw_main_overlay()
  2406. pointer.draw(0,pm_wy) #wy
  2407. pygame.display.flip()
  2408. clock.tick(25)
  2409. if 'SDL_VIDEO_WINDOW_POS' in os.environ:
  2410. del os.environ['SDL_VIDEO_WINDOW_POS'] #= '%i,%i' % (200,164)
  2411. frame2 += 1
  2412. if __name__ == "__main__":
  2413. try:
  2414. main()
  2415. finally:
  2416. del_all_video()