vpu_live4.py 79 KB

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