vpu_live.py 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357
  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. HOME = os.getenv('HOME')
  10. import _thread as thread
  11. from optparse import OptionParser
  12. ...
  13. parser = OptionParser()
  14. parser.add_option("-m", "--mode", dest="mode",
  15. help="pixel mode pix,x,y --mode 40,10,8") #, metavar="FILE")
  16. parser.add_option("-X", "--XX", dest="XX", #default=1,
  17. help="x-split") #, metavar="FILE")
  18. parser.add_option("-x", "--xx", dest="xsplit", #default=1,
  19. help="x-split") #, metavar="FILE")
  20. parser.add_option("-y", "--yy", dest="ysplit",#default=1,
  21. help="y-split") #, metavar="FILE")
  22. parser.add_option("", "--start-univ", dest="start_univ",#default=1,
  23. help="set start-univers default=2") #, metavar="FILE")
  24. parser.add_option("", "--gobo-ch", dest="gobo_ch",default=0,
  25. help="gobo ch univ on 1") #, metavar="FILE")
  26. parser.add_option("", "--gobo-ch2", dest="gobo_ch2",default=0,
  27. help="gobo ch univ on 1") #, metavar="FILE")
  28. #os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (200,164)
  29. parser.add_option("", "--win-pos", dest="win_pos",default="200,164",
  30. help="SDL_VIDEO_WINDOW_POS --win-pos=200,164") #, metavar="FILE")
  31. parser.add_option("", "--pixel-mapping", dest="pixel_mapping",default=0,
  32. help="pixel_mapping file/on --pixel-mapping=_x") #, metavar="FILE")
  33. parser.add_option("", "--dual-vpu", dest="dual_vpu",default=0,
  34. help=" --dual-vpu=1/0") #, metavar="FILE")
  35. parser.add_option("", "--grid-a1-idim", dest="grid_a1_idim",default=0,
  36. help=" grid_a1_idim") #, metavar="FILE")
  37. parser.add_option("", "--grid-a2-idim", dest="grid_a2_idim",default=0,
  38. help=" grid_a2_idim") #, metavar="FILE")
  39. parser.add_option("", "--countdown", dest="countdown",#default=1,
  40. help="enable countdown") #, metavar="FILE")
  41. parser.add_option("", "--videoplayer", dest="videoplayer",#default=1,
  42. help="enable videoplayer") #, metavar="FILE")
  43. parser.add_option("", "--title", dest="title",default="SCREEN",
  44. help="set title") #, metavar="FILE")
  45. (options, args) = parser.parse_args()
  46. import numpy
  47. for o in dir(options):
  48. if "_" in o:
  49. continue
  50. print(o,options.__getattribute__(o))
  51. START = time.time()
  52. # ===== ARTNET DMX =========
  53. import memcache
  54. mc = memcache.Client(['127.0.0.1:11211'], debug=0)
  55. def read_index():
  56. ips=mc.get("index")#cmd)
  57. if ips is None:
  58. ips = {}
  59. return ips
  60. def select_ip(ips, univ=2): # artnet univ
  61. _univ = ":{}".format(univ)
  62. for ip in ips: #high priority
  63. if "2.0.0" in ip and _univ in ip:
  64. return ip
  65. for ip in ips:
  66. if "ltp-out" in ip and _univ in ip:
  67. return ip
  68. FUNC = 0
  69. COUNTER = []
  70. if options.countdown:
  71. cdmx_start = options.countdown.split(",")
  72. for cdmx in cdmx_start:
  73. try:
  74. cdmx = int(cdmx)
  75. _tmp={"DMX":cdmx,"DIM":0,"PAN":127,"TILT":127
  76. ,"CONTROL":0,"SEC":10
  77. ,"RED":255,"GREEN":255,"BLUE":255
  78. ,"_time":time.time(),"_RUN":0
  79. ,"_SEC":">{}<".format(cdmx)
  80. }
  81. COUNTER.append(_tmp)
  82. except Exception as e:
  83. print("EXCEPTION COUNTER INIT ",cdmx)
  84. def read_dmx(ip):
  85. r = ""
  86. if ip:
  87. #t = int(math.sin(time.time() - s)*10)
  88. r = mc.get(ip) #"2.0.0.13:2")
  89. rr = [0]*512
  90. for i,v in enumerate(r):
  91. try: #cleanup ltp-out to int
  92. v = int(v)
  93. rr[i] = v
  94. except:pass
  95. r = rr
  96. if not r:
  97. c = 0
  98. #time.sleep(0.1)
  99. r = [0] *512
  100. for i in range(12*8+1):
  101. dmx = i*4
  102. #print(dmx)
  103. r[dmx:dmx+4] = [255,10,10,40]
  104. return r
  105. # ===== ARTNET DMX =========
  106. PLAYLIST = []
  107. try:
  108. PLAYLIST = open_playlist()
  109. except:pass
  110. #import json
  111. #import pickle
  112. sys.path.insert(0,"/opt/LibreLight/Xdesk/")
  113. from tool.video_capture import scan_capture #as scan_capture
  114. class Vopen():
  115. def __init__(self,dmx=None,_id=None):
  116. global PLAYLIST
  117. self._id = _id
  118. self.fpath = HOME+'/Downloads/'
  119. self.fpath = HOME+'/LibreLight/video/'
  120. self.fname = '' #'bbb_sunflower_480x320.mp4'
  121. #self.fname = 'no-video.mp4'
  122. try:
  123. self.fname = PLAYLIST[0]
  124. except Exception as e:
  125. print("Exception set video from PLAYLIST 5543:",e)
  126. self.restart_t = time.time()
  127. self.fps = 0
  128. self.scale = 50 #%
  129. self.angle = 0 #%
  130. self.dmx=dmx
  131. if self._id >= 250:
  132. self.fname = "cam_"
  133. self.dim = 0
  134. self.x = 0
  135. self.y = 0
  136. self.init_count = 0
  137. self.cap = None
  138. self.shape = [200,200]
  139. self.img = None
  140. self.success = 1
  141. self.cv2 = None
  142. self._run = 0
  143. self.end = 0
  144. self._video_nr = 0
  145. self._stop = 0 # fix buffer-overflow ... ?? opencv
  146. self.shape_x = 370
  147. self.shape_y = 235
  148. try:
  149. global cv2
  150. self.cv2 = cv2
  151. except:
  152. pass
  153. self.init()
  154. def __repr__(self):
  155. sizeof = int(sys.getsizeof(self.buffer)/8)
  156. return "< id:{} buf:{} run:{} fps:{} scale:{} name:{} {}kb>".format(
  157. id(self),len(self.buffer),self._run,self.fps,self.scale
  158. ,self.fname,sizeof
  159. )
  160. def restart(self):
  161. print(self,"reset()")
  162. self.pos = 0
  163. self.restart_t = time.time()
  164. def init(self):
  165. print("---- ---- Vopen.init()",[self.fname,self._video_nr])
  166. print(PLAYLIST)
  167. self.time = 0
  168. self.t_delta = 0
  169. self.t_last = time.time()
  170. self.im = None
  171. self.pos = 0
  172. self.buffer = []
  173. self._init()
  174. self.init_count = 1
  175. def select_video(self,dmx_value):
  176. try:
  177. dmx_value = int(dmx_value/10)
  178. if self._video_nr != dmx_value:
  179. self._video_nr = dmx_value
  180. if self._video_nr >= 25:
  181. self.fname = "cam_"
  182. self.init()
  183. elif self._video_nr < len(PLAYLIST):
  184. self.fname = str(PLAYLIST[self._video_nr])
  185. self.init()
  186. except Exception as e:
  187. print("Vopen.select_video()",dmx_value,e)
  188. def close_cap():
  189. print(dir(self.Rcap)) # = self.cv2.VideoCapture(self.fpath+self.fname)
  190. def _cam_cfg_new(self,fn):
  191. #fn = HOME+'/LibreLight/video_in.txt'
  192. txt = [
  193. "# Example",
  194. "d0 w640 h480 # PAL",
  195. "d0 w800 h600 # SVGA",
  196. "d0 w960 h540 # 540p",
  197. "d0 w1024 h768 # XGA <---",
  198. "d0 w1280 h720 # 720p",
  199. "d0 w1440 h900 # WSXGA+",
  200. "d0 w1600 h900 # WSXGA - Laptop 16:9",
  201. "d0 w1920 h1080 # HD 1080p",
  202. "",
  203. "# config",
  204. "d0 w1024 h768 # XGA",
  205. ]
  206. f = open(fn ,"w")
  207. f.write("\n".join(txt))
  208. f.close()
  209. def _cam_cfg(self):
  210. fn = HOME+'/LibreLight/video_in.txt'
  211. if not os.path.isfile(fn):
  212. self._cam_cfg_new(fn)
  213. f = open(fn ,"r")
  214. lines = f.readlines()
  215. f.close()
  216. cfg = ""
  217. for i in lines:
  218. if i.startswith("d"):
  219. cfg = i
  220. cfg = cfg.split()
  221. capture = scan_capture(name="MiraBox",serial="")
  222. if capture:
  223. print("capture",capture)
  224. try:
  225. cfg.append(capture[0][0])
  226. except Exception as e:
  227. print("except 2992",e)
  228. else:
  229. print("---- ERROR NO VIDEO CAPTURE DEV - FOUND ----")
  230. print("_cam_cfg:",len(cfg),[cfg])
  231. return cfg
  232. def _open_cam(self):
  233. # LIVE VIDEO INPUT UVC DEVICE
  234. cfg = self._cam_cfg()
  235. res = "HD"
  236. #res = "4K"
  237. #res = ""
  238. if res == "HD":
  239. # HD
  240. w = 1920
  241. h = 1080
  242. elif res == "4K":
  243. # 4K
  244. w = 3840
  245. h = 2160
  246. else:
  247. # LOW
  248. w = 1280
  249. h = 720
  250. d = 0 # VIDEO INPUT DEV
  251. for c in cfg:
  252. try:
  253. if c.startswith("d"):
  254. d = int(c[1:])
  255. if c.startswith("h"):
  256. h = int(c[1:])
  257. if c.startswith("w"):
  258. w = int(c[1:])
  259. if c.startswith("video"):
  260. d = int(c[-1])
  261. except Exception as e:
  262. print("Exception", "cam_cfg 878",e)
  263. df = "video{}".format(d)
  264. if self._stop:
  265. df += "_stop"
  266. elif os.path.exists("/dev/{}".format(df)):
  267. self.Rcap = self.cv2.VideoCapture(d)
  268. self.Rcap.set(self.cv2.CAP_PROP_FRAME_WIDTH, w)
  269. self.Rcap.set(self.cv2.CAP_PROP_FRAME_HEIGHT, h)
  270. else:
  271. #self.Rcap = None
  272. #self.end = 1
  273. df += "_error"
  274. if self.fname.startswith("cam_"):
  275. self.fname = "cam_{}".format(df)
  276. def _init(self):
  277. print(self)
  278. print("videoplayer.init()",self.fpath,self.fname)
  279. if not os.path.isfile(self.fpath+self.fname):
  280. print()
  281. print("video file does not exits !! >",self.fpath,self.fname)
  282. print()
  283. #exit()
  284. self.Rsuccess = 0
  285. if self.cv2:
  286. if self._stop:
  287. return
  288. if self.fname.startswith("cam_"):
  289. self._open_cam()
  290. else:
  291. self.Rcap = self.cv2.VideoCapture(self.fpath+self.fname, cv2.CAP_FFMPEG)
  292. #FFMPEG malloc(): unsorted double linked list corrupted ... Abgebrochen
  293. print("_init ?",self.Rcap)
  294. print("open ?" ,self.Rcap.isOpened())
  295. self.Rcap.read()
  296. #self.Rfvs = FileVideoStream(self.fpath+self.fname).start()
  297. self.Rsuccess = 1
  298. self._read()
  299. def _del(self):
  300. self._stop = 1
  301. self.buffer = [] #.append(self.img)
  302. self.Rcap.release()
  303. self.Rcap.retrieve()
  304. self.Rcap = None
  305. self.cap = None
  306. del self.Rcap #.release()
  307. gc.collect()
  308. def _read(self):
  309. success = self.Rsuccess
  310. ok = 0
  311. if success and self.fname:
  312. cap = self.Rcap
  313. #fvs = self.Rfvs
  314. _break = 0
  315. try:
  316. success, img = cap.read()
  317. if not success:
  318. self.Rcap.release()
  319. self.Rcap.retrieve()
  320. self.end = 1
  321. #j = json.dumps(self.buffer) #.append(img)
  322. #f = open("/tmp/buff")
  323. #f.write(j)
  324. #f.close()
  325. return
  326. if self.fps == 0:
  327. self.fps = cap.get(cv2.CAP_PROP_FPS)
  328. img = self.cv2.cvtColor(img, self.cv2.COLOR_BGR2RGB)
  329. if self.fname.startswith("cam_"):
  330. pass
  331. #img = self.rescale_frame2(img, 1200)
  332. else:
  333. img = self.rescale_frame2(img, 200)
  334. # store frame into buffer list
  335. if self.fname.startswith("cam_"):
  336. self.buffer = [ img ]
  337. else:
  338. self.buffer.append(img)
  339. ok = 1
  340. if len(self.buffer) % 100 == 0:
  341. _id = str(self.__repr__)[-5:-1]
  342. print(_id,"video read",self.dmx,len(self.buffer),self.fname,"fps",self.fps,self.dim)
  343. except Exception as e:
  344. print("Excetpion","_init",self,e,end="")
  345. self.success = 1
  346. return ok
  347. def read(self):
  348. pass
  349. def prev(self):
  350. self.pos -= 1
  351. if self.pos < 0:
  352. self.pos = len(self.buffer)-1
  353. if self.pos >= len(self.buffer):
  354. self.pos = len(self.buffer)-1
  355. self.im = self.buffer[int(self.pos)]
  356. def rotateImage(self,image, angle):
  357. if angle in [0,360]:
  358. pass#return image
  359. try:
  360. #print("EE",image.shape)
  361. #print("EE",dir(image))
  362. shape = list(image.shape[1::-1])
  363. bg_shape = shape[:]
  364. delta_x = 0 # + nach rechts
  365. delta_y = 0 # + nach unten
  366. r = []
  367. r.extend(list(range(0-3,0+3)))
  368. r.extend(list(range(180-3,180+3)))
  369. r.extend(list(range(360-3,360+3)))
  370. r2 = []
  371. r2.extend(list(range(90-3,90+3)))
  372. r2.extend(list(range(270-3,270+3)))
  373. if angle in r:
  374. pass
  375. elif 0:#angle in r2:
  376. bg_shape = bg_shape[::-1]
  377. delta_y = -65 #shape[1]/2 # # + nach unten
  378. delta_x = 30 #shape[1]/2 # # + nach unten
  379. delta_y -= bg_shape[1]/2 - 20
  380. delta_x = bg_shape[0]/2 - 10
  381. pass
  382. else:
  383. if shape[0] > shape[1]:
  384. # landscape video
  385. bg_shape = (shape[0],shape[0])
  386. delta_y = (shape[0]-shape[1])/2
  387. else:
  388. # portrait video
  389. bg_shape = (shape[1],shape[1])
  390. delta_x = (shape[1]-shape[0])/2
  391. center = (shape[0]/2+delta_x,shape[1]/2+delta_y)
  392. image = self.moveImage(image,x=delta_x,y=delta_y,shape=bg_shape)
  393. bg_shape = tuple(bg_shape) # exception ... if angle 0 no tuple !!!
  394. rot_mat = self.cv2.getRotationMatrix2D(center,angle,1.0)
  395. frame = self.cv2.warpAffine(image, rot_mat, bg_shape ) #,flags=self.cv2.INTER_LINEAR)
  396. return frame
  397. except Exception as e:
  398. raise(e)
  399. def moveImage(self,img,x=0,y=0,shape=None):
  400. # Creating a translation matrix
  401. np = numpy
  402. translation_matrix = np.float32([ [1,0,x], [0,1,y] ])
  403. if shape is None:
  404. num_cols,num_rows = img.shape[1::-1]
  405. else:
  406. num_cols,num_rows = shape #[1::-1]
  407. # Image translation
  408. frame = self.cv2.warpAffine(img, translation_matrix, (num_cols,num_rows))
  409. return frame
  410. def rescale_frame2(self,frame, width):
  411. height = int(frame.shape[0]/frame.shape[1] * width )
  412. dim = (width, height)
  413. return self.cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)
  414. def rescale_frame(self,frame, percent=75):
  415. width = int(frame.shape[1] * percent/ 100)
  416. height = int(frame.shape[0] * percent/ 100)
  417. dim = (width, height)
  418. return self.cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)
  419. def pause(self):
  420. #print("pause",self.t_last)
  421. t = time.time()
  422. self.t_delta = 0
  423. self.t_last = t
  424. def next_frame(self):
  425. #return 0
  426. if self._run and len(self.buffer) > 30:
  427. t = time.time()
  428. self.t_delta = t-self.t_last
  429. self.pos += self.t_delta*self.fps
  430. self.t_last = t
  431. else:
  432. self.pause()
  433. if self.restart_t > time.time()-0.5:
  434. # prevent flickerung by reset
  435. #self.pause()
  436. self.pos = 5
  437. pass
  438. # restart at the end
  439. if int(self.pos) >= len(self.buffer)-1:
  440. self.pos = 0
  441. self.img = None
  442. if self.buffer:
  443. self._img = self.buffer[int(self.pos)]
  444. self.img = self._img
  445. def next(self):
  446. try:
  447. self.next_frame()
  448. if self.img is None:
  449. return
  450. self.img = self.rescale_frame(self.img, percent=self.scale)
  451. # rotate frame BUG: x,y offset ??? !!!
  452. self.img = self.rotateImage(self.img, self.angle)
  453. self.shape = self.img.shape[1::-1]
  454. if len(self.buffer) % 100 == 0:
  455. _id = str(self.__repr__)[-5:-1]
  456. print("next",_id)
  457. # add DIMER to videplayer
  458. self.cv2.normalize(self.img, self.img, 0, self.dim, self.cv2.NORM_MINMAX)
  459. img = self.img
  460. if img is None:
  461. return
  462. self.im = None
  463. self.im = pygame.image.frombuffer(img.tobytes(), self.shape, "RGB")
  464. #self.next_frame()
  465. except AttributeError as e:
  466. time.sleep(.05)
  467. #if self.init_count % 100 == 0:
  468. print("except 776",e)
  469. #self.init()
  470. except Exception as e:
  471. print("except 756",e)
  472. print(traceback.format_exc())
  473. print(sys.exc_info()[2])
  474. print()
  475. def draw(self,wn=None):
  476. if self.dim <= 1:
  477. return
  478. if self.scale < 255*.05:
  479. self.scale = 255*0.05
  480. # draw video background box
  481. __xw = int(self.shape_x*self.scale/255)
  482. __yw = int(self.shape_y*self.scale/255)
  483. #xx ,yy = (370,235)
  484. xx = self.shape_x #= xx
  485. yy = self.shape_y #= yy
  486. try:
  487. xx ,yy = self.im.get_size()[:2]
  488. self.shape_x = xx
  489. self.shape_y = yy
  490. except AttributeError as e:
  491. pass
  492. xx = int(xx*self.scale/255)
  493. yy = int(yy*self.scale/255)
  494. __xw = int(xx) #*self.scale/255)
  495. __yw = int(yy) #*self.scale/255)
  496. yellow = [105,50,0]
  497. yellow[0] = int(yellow[0]*self.dim/255)
  498. yellow[1] = int(yellow[1]*self.dim/255)
  499. yellow[2] = int(yellow[2]*self.dim/255)
  500. if self.fname.startswith("cam_"):
  501. pass
  502. elif 1: #corner left up
  503. p1 = [self.x+2,self.y+2]
  504. p2 = [self.x+__xw-4,self.y+__yw-4]
  505. p3 = [self.x+__xw-4,self.y+2]
  506. p4 = [self.x+2,self.y+__yw-4]
  507. p1 = [self.x,self.y]
  508. p2 = [self.x+__xw,self.y+__yw]
  509. p3 = [self.x+__xw,self.y]
  510. p4 = [self.x,self.y+__yw]
  511. hx=int(xx/2)
  512. hy=int(yy/2)
  513. p1 = [self.x-hx,self.y-hy]
  514. p2 = [self.x+__xw-hx,self.y+__yw-hy]
  515. p3 = [self.x+__xw-hx,self.y-hy]
  516. p4 = [self.x-hx,self.y+__yw-hy]
  517. pygame.draw.rect(wn,yellow ,[p1[0] ,p1[1] ,__xw ,__yw])
  518. pygame.draw.rect(wn,[25,20,20],[p1[0]+1,p1[1]+1,__xw-2,__yw-2])
  519. pygame.draw.line(wn,yellow ,p1 ,p2)
  520. pygame.draw.line(wn,yellow ,p3 ,p4)
  521. if 0: #corner right down
  522. pygame.draw.rect(wn,yellow,[self.x-__xw,self.y-__yw,__xw,__yw])
  523. pygame.draw.rect(wn,[25,20,20],[self.x+1-__xw,self.y+1-__yw,__xw-2,__yw-2])
  524. pygame.draw.line(wn,yellow,[self.x+2-__xw,self.y+2-__yw],[self.x+__xw-4-__xw,self.y+__yw-4-__yw])
  525. pygame.draw.line(wn,yellow,[self.x+__xw-4-__xw,self.y+2-__yw],[self.x+2-__xw,self.y+__yw-4-__yw])
  526. pz = 0
  527. if self.success and wn and self.im: # is not None:
  528. xx ,yy = self.im.get_size()[:2]
  529. if self.fname.startswith("cam_"):
  530. wn.blit(self.im, (int(self.x-xx/2), int(self.y)))
  531. else:
  532. wn.blit(self.im, (int(self.x-xx/2), int(self.y-yy/2)))
  533. def overlay(self,wn=None,mode="x"):
  534. # overlay
  535. pygame.draw.rect(wn,[255,200,0],[5,MAIN_SIZE[1]-(self._id+1)*35,300,28])
  536. font15 = pygame.font.SysFont("freemonobold",17)
  537. pz = 0
  538. if self.end:
  539. rgb = [ 100,255,100]
  540. else:
  541. rgb = [255,100,0]
  542. pygame.draw.rect(wn,rgb,[220,MAIN_SIZE[1]-(self._id+1)*35,80,13])
  543. _line = "error no _line"
  544. _line ="FPS:{} F:{:05} von {:05} sec:{:0.02f} von {:0.02f}"
  545. if self.fps == 0: # check if div zerro
  546. _line = _line.format(self.fps,int(self.pos),len(self.buffer),(-1),pz )
  547. else:
  548. pz = (len(self.buffer)/self.fps)
  549. _line = _line.format(self.fps,int(self.pos),len(self.buffer),(self.pos/self.fps),pz )
  550. fr = font15.render(_line ,1, (0,0,0))
  551. wn.blit(fr,(10,MAIN_SIZE[1]-(self._id+1)*35))
  552. if self._run:
  553. mode = "run"
  554. else:
  555. mode = "pause"
  556. fr = font15.render(" {} {} >:{} ".format(self._id+1,self._video_nr,mode) ,1, (0,0,0))
  557. wn.blit(fr,(3,MAIN_SIZE[1]-(self._id+1)*35+15))
  558. fr = font15.render("{}".format(self.fname) ,1, (0,0,0))
  559. wn.blit(fr,(70,MAIN_SIZE[1]-(self._id+1)*35+15))
  560. Vopen = Vopen
  561. VIDEO = []
  562. videoplayer=[]
  563. cv2 = None
  564. FileVideoStream = None
  565. _vid = 0
  566. if type(options.videoplayer) is str:
  567. try:
  568. import cv2
  569. except Exception as e:
  570. print("Except Import:",e)
  571. try:
  572. # faster video reading ... ???
  573. from imutils.video import FileVideoStream
  574. except Exception as e:
  575. print("Except Import:",e)
  576. max_videoplayer = 4
  577. dmx_start = options.videoplayer.split(",")
  578. for cdmx in dmx_start:
  579. if len(videoplayer) > max_videoplayer:
  580. print("-- videoplayer max count {} !! break".format(max_videoplayer))
  581. break
  582. print("-- videoplayer dmx:",cdmx)
  583. try:
  584. cdmx = int(cdmx)
  585. videoplayer.append( Vopen(cdmx,_id=_vid) )
  586. _vid += 1
  587. _tmp = {"DMX":cdmx,"DIM":0,"PAN":127,"TILT":127
  588. ,"CONTROL":0,"SEC":10,"VIDEO":"3"
  589. ,"RED":255,"GREEN":255,"BLUE":255
  590. ,"_time":time.time()
  591. ,"_RUN":0,"_SEC":">{}<".format(cdmx)
  592. }
  593. VIDEO.append(_tmp)
  594. except Exception as e:
  595. print("EXCEPTION COUNTER INIT ",cdmx)
  596. def loop_videoplayer():
  597. while 1:
  598. _videoplayer = videoplayer[:]
  599. #print(".")
  600. ok = 0
  601. for i in _videoplayer: #.append( Vopen(cdmx,_id=_vid) )
  602. try:
  603. r = i._read() # read next frame from file
  604. if r:
  605. ok = 1
  606. except Exception as e:
  607. print("EXCEPTION loop_videoplayer ",e)
  608. if ok == 0:
  609. time.sleep(0.1)
  610. else:
  611. time.sleep(0.005)
  612. thread.start_new_thread(loop_videoplayer,())
  613. # ===== ======
  614. #self.fname = PLAYLIST[0]
  615. videoplayer2 = []
  616. def loop2_videoplayer():
  617. while 1:
  618. print()
  619. print()
  620. print()
  621. for i,v in enumerate(PLAYLIST):
  622. try:
  623. video1 = videoplayer2[i]
  624. video1.select_video(i)
  625. except:
  626. vi = Vopen(181,1)
  627. videoplayer2.append( vi )
  628. _videoplayer = videoplayer2[:]
  629. ok = 0
  630. j =0
  631. for i in _videoplayer: #.append( Vopen(cdmx,_id=_vid) )
  632. try:
  633. r = i._read() # read next frame from file
  634. if r:
  635. print(j,len(videoplayer2),i,len(i.buffer))
  636. ok = 1
  637. except Exception as e:
  638. print("EXCEPTION loop2_videoplayer ",e)
  639. time.sleep(0.002)
  640. j += 1
  641. p = 16
  642. block = [p,p]
  643. _x = 8
  644. _y = 8
  645. #HD = "0"
  646. if options.mode:
  647. try:
  648. HD = options.mode
  649. p,_x,_y = HD.split(",")
  650. _x = int(_x)
  651. _y = int(_y)
  652. p = int(p)
  653. block = [p,p]
  654. except Exception as e:
  655. print( "Exc",options.mode,e)
  656. # PARSE COMMANDLINE ARGUMENTS
  657. CFG_IN = {"name":"CFG_IN","x1":40,"y1":60,"x2":300,"y2":300 ,"w":300,"h":300}
  658. CFG_OUT = {"name":"CFG_OUT","x1":40,"y1":60,"x2":300,"y2":300 ,"w":300,"h":300,"on":0}
  659. CFG_OUT2 = {"name":"CFG_OUT2","x1":p*8+142,"y1":60,"x2":300,"y2":300 ,"w":300,"h":300,"on":0}
  660. CFG_BLOCK = {"name":"CFG_BLOCK","size":16,"h-split":2,"v-split":2,"h-count":8,"v-count":8}
  661. if _x < 1:
  662. _x = 1
  663. if _y < 1:
  664. _y = 1
  665. CFG_BLOCK["h-count"] = _x
  666. CFG_BLOCK["v-count"] = _y # int(8*8/_x) #/_x+0.5) #_y
  667. CFG_BLOCK["size"] = p
  668. print( [options.xsplit])
  669. print( [options.ysplit])
  670. try:
  671. if options.xsplit:
  672. CFG_BLOCK["h-split"] = int(options.xsplit)
  673. if options.ysplit:
  674. CFG_BLOCK["v-split"] = int(options.ysplit)
  675. except Exception as e:
  676. print( "Exc",options.mode,e)
  677. print("HD",CFG_BLOCK["h-split"],CFG_BLOCK["v-split"])
  678. print("xy",_x,_y)
  679. print("++++++++++++++++++", p,_x,_y)
  680. _x2 = _x
  681. try:
  682. if options.XX:
  683. pass#_x2 = int(options.XX)
  684. except Exception as e:
  685. print( "Exc",options.mode,e)
  686. print("_x2 , -X",_x2)
  687. # ===== GUI =========
  688. import pygame
  689. import pygame.gfxdraw
  690. import pygame.font
  691. clock = pygame.time.Clock()
  692. os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (200,164)
  693. if options.win_pos:
  694. if "," in options.win_pos:
  695. win_pos = options.win_pos.split(",")
  696. try:
  697. WIN_POS = '%i,%i' % (int(win_pos[0]),int(win_pos[1]) )
  698. os.environ['SDL_VIDEO_WINDOW_POS'] = WIN_POS
  699. except Excetpion as e:
  700. print("win_pos",win_pos,e)
  701. os.environ['SDL_VIDEO_CENTERED'] = '0'
  702. pg = pygame
  703. pygame.init()
  704. pygame.mixer.quit()
  705. f = pygame.font.get_fonts()
  706. for i in f:
  707. if "mono" in i.lower():
  708. print(i)
  709. font = pygame.font.SysFont("freemonobold",22)
  710. font10 = pygame.font.SysFont("freemonobold",10)
  711. font12 = pygame.font.SysFont("freemonobold",12)
  712. font12l = pygame.font.SysFont("freemono",12)
  713. font15 = pygame.font.SysFont("freemonobold",15)
  714. font40 = pygame.font.SysFont("freemonobold",40)
  715. font80 = pygame.font.SysFont("freemonobold",70)
  716. #font = pygame.font.SysFont(None,30)
  717. fr = font.render("hallo" ,1, (200,0,255))
  718. PIXEL_MAPPING = 0
  719. grid_file = "/tmp/vpu_grid_hd.csv"
  720. text_file = HOME+"/LibreLight/vpu_text_hd.csv"
  721. play_list = "/tmp/vpu_playlist_hd.csv"
  722. play_list = HOME+"/LibreLight/video/" #.format(path)
  723. pm_wy = 0
  724. if options.pixel_mapping:
  725. PIXEL_MAPPING = 1
  726. CFG_OUT["on"] = 1
  727. path = options.pixel_mapping
  728. path = path.replace("/","-")
  729. path = path.replace(".","-")
  730. path = path.replace("\"","-")
  731. path = path.replace("'","-")
  732. grid_file = HOME+"/LibreLight/vpu_grid_hd{}.csv".format(path)
  733. if options.dual_vpu:
  734. grid_file = HOME+"/LibreLight/vpu_grid_dual{}.csv".format(path)
  735. text_file = HOME+"/LibreLight/vpu_text_hd{}.csv".format(path)
  736. play_list = HOME+"/LibreLight/vpu_playlist_hd{}.csv".format(path)
  737. play_list = HOME+"/LibreLight/video/" #.format(path)
  738. #_x = 8
  739. #_y = 8
  740. if options.dual_vpu:
  741. CFG_OUT2["on"] = 1
  742. print(" ",[options.pixel_mapping],"grid_file",grid_file)
  743. #grid_file = HOME+"/LibreLight/vpu_grid_hd.csv"
  744. MAIN_SIZE=(600,500)
  745. try:
  746. if _x < 8 and PIXEL_MAPPING >= 1:
  747. wx = 30+30+block[0] * 8
  748. CFG_IN["w"] = CFG_BLOCK["size"] * 8
  749. else:
  750. wx = 30+30+block[0] * _x
  751. wy = 40+40+block[1] * _y
  752. if type(options.videoplayer) is str:
  753. wy += 150 # video playlist
  754. MAIN_SIZE=(wx,wy)
  755. if PIXEL_MAPPING >= 1:
  756. pm_wy = 11*p #+ p*3
  757. CFG_IN["y1"] += 11*p
  758. if options.dual_vpu:
  759. if wx < p*16+155:
  760. wx = p*16+155
  761. MAIN_SIZE=(wx,wy+pm_wy)
  762. except Exception as e:
  763. print("Exception:",e)
  764. CFG_IN["w"] = CFG_BLOCK["size"] * CFG_BLOCK["h-count"]
  765. CFG_IN["h"] = CFG_BLOCK["size"] * CFG_BLOCK["v-count"]
  766. CFG_OUT["w"] = CFG_BLOCK["size"] * 8
  767. CFG_OUT["h"] = CFG_BLOCK["size"] * 8
  768. CFG_OUT2["w"] = CFG_BLOCK["size"] * 8
  769. CFG_OUT2["h"] = CFG_BLOCK["size"] * 8
  770. def CFG_CALC_P(CFG):
  771. CFG["x2"] = CFG["x1"]+CFG["w"]
  772. CFG["y2"] = CFG["y1"]+CFG["h"]
  773. CFG["p1"] = [CFG["x1"] ,CFG["y1"]]
  774. CFG["p2"] = [CFG["x2"] ,CFG["y2"]]
  775. print("CFG",CFG)
  776. # ?
  777. #CFG_IN["w"] = int(CFG_BLOCK["v-count"]-2) * CFG_BLOCK["size"]
  778. CFG_CALC_P(CFG_IN)
  779. CFG_CALC_P(CFG_OUT)
  780. CFG_CALC_P(CFG_OUT2)
  781. print("CFG_BLOCK",CFG_BLOCK)
  782. print()
  783. print()
  784. print()
  785. print()
  786. print()
  787. window = pygame.display.set_mode(MAIN_SIZE)#,pg.RESIZABLE)#,32)#,pygame.FULLSCREEN) #x left->right ,y top-> bottom
  788. pg.display.set_caption('LibreLight VPU-{}'.format(options.title))
  789. class Fix():
  790. def __init__(self,_id,pos,block=[16,16],univ=0,dmx=0,ch=4):
  791. #print("Fix",_id)
  792. self._id = _id
  793. self.dmx = (_id-1) * ch +1 #dmx
  794. self.univ = univ
  795. self.ch = ch
  796. self.pos = pos
  797. self.rgb = [0,0,0]
  798. self.block = block #[10,10]
  799. self.x = pos[0]
  800. self.y = pos[1]
  801. self.strobo = time.time()
  802. self.bmp = 250
  803. self.sub_fix = []
  804. sub_block =[block[0]/CFG_BLOCK["h-split"],block[1]/CFG_BLOCK["v-split"]]
  805. if _id <= 0: #exit
  806. return
  807. spalte = (_id-1)%_y +1
  808. zeile = int((_id-1)/_x2) #+1
  809. add_row = _x*CFG_BLOCK["h-split"]*CFG_BLOCK["v-split"]
  810. #zeile 1
  811. sid = (_id-1)*2 + zeile*CFG_BLOCK["h-split"]*_x2
  812. sid = sid+1
  813. sub_pos= [pos[0]*block[0],pos[1]*block[1]]
  814. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  815. self.sub_fix.append(sub_fix)
  816. sid = sid+1
  817. sub_pos= [pos[0]*block[0]+block[0]/2,pos[1]*block[1]]
  818. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  819. self.sub_fix.append(sub_fix)
  820. #zeile 2
  821. sid = (_id-1)*2+1 + _x2*CFG_BLOCK["h-split"] + zeile*CFG_BLOCK["h-split"]*_x2 # int(add_row)
  822. sub_pos= [pos[0]*block[0],pos[1]*block[1]+block[1]/2]
  823. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  824. self.sub_fix.append(sub_fix)
  825. #sid = sid+1
  826. sid = sid+1
  827. sub_pos= [pos[0]*block[0]+block[0]/2,pos[1]*block[1]+block[1]/2]
  828. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  829. self.sub_fix.append(sub_fix)
  830. def calc(self,data):
  831. _rgb = [0,255,0]
  832. return _rgb
  833. def sub_calc(self,data):
  834. _rgb = [0,255,0]
  835. for sub_fix in self.sub_fix:
  836. sub_fix.block = self.block[:]
  837. _rgb = sub_fix.calc(data)
  838. return _rgb
  839. def POS(self,x=0,y=0,a=0,b=0):
  840. A = (self.pos[0])*self.block[0]
  841. B = (self.pos[1])*self.block[1]
  842. C = self.block[0]-a
  843. D = self.block[1]-b
  844. return [x+A,y+B,C,D]
  845. def subPOS(self,x=0,y=0,a=0,b=0):
  846. __out = []
  847. for sub_fix in self.sub_fix:
  848. __out.append( sub_fix.POS(x,y,a,b) )
  849. return __out
  850. class SubFix():
  851. def __init__(self,_id,pos,block=[16,16],univ=0,dmx=0,ch=4):
  852. #print("Fix",_id)
  853. self._id = _id
  854. self.dmx = (_id-1) * ch +1 #dmx
  855. self.univ = univ
  856. self.ch = ch
  857. self.pos = pos
  858. self.rgb = [0,0,40]
  859. self.block = block #[10,10]
  860. self.x = pos[0]
  861. self.y = pos[1]
  862. self.strobo = time.time()
  863. self.bmp = 250
  864. def calc(self,data):
  865. #return [130,30,20]
  866. dmx_sub = [30]*10
  867. dmx = self.dmx -1
  868. _dmx_sub = []
  869. if self.dmx >= 0:
  870. dmx = rDMX(self.univ,self.dmx)-1
  871. if dmx+self.ch < len(data):
  872. _dmx_sub = data[dmx:dmx+self.ch]
  873. if _dmx_sub:
  874. dmx_sub = _dmx_sub
  875. dim = dmx_sub[0]/255
  876. r = dmx_sub[1]*dim
  877. g = dmx_sub[2]*dim
  878. b = dmx_sub[3]*dim
  879. r = int(r)
  880. g = int(g)
  881. b = int(b)
  882. self.rgb = [r,g,b]
  883. return self.rgb
  884. def POS(self,x=0,y=0,a=0,b=0):
  885. A = (self.pos[0]) #+self.block[0]
  886. B = (self.pos[1]) #+self.block[1]
  887. C = self.block[0]-a
  888. D = self.block[1]-b
  889. if NR:
  890. C-=1
  891. D-=1
  892. return [int(x+A),int(y+B),int(C),int(D)]
  893. class POINTER():
  894. def __init__(self):
  895. self.pos = [0,0,0,0]
  896. self.on = 0
  897. self.rgb = [0,100,10]
  898. self._x = 0
  899. self._y = 0
  900. self.x = 0
  901. self.y = 0
  902. self.fix = Fix(0 ,[999,999],[16,16],0,0,0)
  903. def row_move(self,x,y):
  904. self._x = x
  905. self._y = y
  906. def move(self,pos):
  907. self.pos = pos
  908. self.on = 1
  909. def cross(self,x,y):
  910. self.x = x
  911. self.y = y
  912. def draw(self,x,y):
  913. pos = self.pos[:]
  914. #print("draw",x,y,pos)
  915. pos[0] += x
  916. pos[1] += y
  917. fix_x= self.fix.x
  918. fix_y= self.fix.y +y
  919. if self.on:
  920. pygame.draw.rect(window,self.rgb,pos)
  921. # mouse grid posision
  922. fr = font15.render("{}/{}".format(fix_x+1,fix_y) ,1, (200,200,200))
  923. _nr = fix_y * _x + fix_x +1
  924. fr = font15.render("{:02}".format(_nr ) ,1, (200,200,200))
  925. window.blit(fr,(pos[0]+2,pos[1]+2 ))
  926. window.blit(fr,(130,1))
  927. # fix pos
  928. #txt=str(pos) #"[0, 0, 0, 0]"
  929. #fr = font15.render(txt ,1, (200,200,200))
  930. #window.blit(fr,(10,1))
  931. # pointer
  932. fr = font15.render("X:{:03}".format(self._x) ,1, (200,200,200))
  933. window.blit(fr,(10,30))
  934. fr = font15.render("Y:{:03}".format(self._y) ,1, (200,200,200))
  935. window.blit(fr,(10,40))
  936. # crosshair
  937. self.rgb = [0,0,200]
  938. pygame.draw.line(window,self.rgb, (self.x-p,self.y) , (self.x-2,self.y),4 )
  939. pygame.draw.line(window,self.rgb, (self.x,self.y-p) , (self.x,self.y-2),4 )
  940. self.rgb = [0,200,0]
  941. pygame.draw.line(window,self.rgb, (self.x+2,self.y) , (self.x+p,self.y),4 )
  942. pygame.draw.line(window,self.rgb, (self.x,self.y+2) , (self.x,self.y+p),4 )
  943. self.rgb = [200,0,0]
  944. pointer = POINTER()
  945. NR = 0
  946. running = True
  947. def event():
  948. global NR,running
  949. for event in pygame.event.get():
  950. #print(event.dict)
  951. _button = None
  952. if "button" in event.dict:
  953. _button = event.dict["button"]
  954. _state = None
  955. if "state" in event.dict:
  956. _state = event.state
  957. _key = None
  958. if "key" in event.dict:
  959. _key = event.key
  960. _pos = None
  961. if "pos" in event.dict:
  962. _pos = event.pos
  963. _type = None
  964. if "type" in event.dict:
  965. _type = event.type
  966. _type = event.type
  967. _mod = None
  968. if "mod" in event.dict:
  969. _mod = event.mod
  970. if 0:
  971. print( " ")
  972. print( "{:.02f}".format( time.time() - START ))
  973. print("button -",_button,end="\t| ")
  974. #print("state -",_state)
  975. print("pos -",_pos)
  976. print("type -",_type, end="\t| ")
  977. print("key -",_key)
  978. print("mod -",_mod)
  979. try:
  980. if _type == 5:
  981. if _button == 1:
  982. NR += 1
  983. if NR > 1:
  984. NR = 0
  985. if _button == 3:
  986. NR -= 1
  987. if NR < 0:
  988. NR = 1
  989. if _pos:
  990. posA = _pos
  991. fix = find_pix(_pos[0]-40,_pos[1]-60+pm_wy)
  992. if fix:
  993. pos = fix.POS(40,60+pm_wy)
  994. rgb = [0,0,0]
  995. pointer.move(pos)
  996. pointer.fix = fix
  997. else:
  998. pointer.on = 0
  999. pointer.row_move(_pos[0],_pos[1])
  1000. pointer.cross(_pos[0],_pos[1])
  1001. if event.type == pygame.VIDEORESIZE:
  1002. window = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)
  1003. except Exception as e:
  1004. print(e)
  1005. if event.type==pygame.QUIT:
  1006. running=False
  1007. fps = 0
  1008. fps2 = 0
  1009. frame = 0
  1010. frame2 = 0
  1011. frame_t = time.time()
  1012. frame2_t = time.time()
  1013. IP = "yyy"
  1014. vplay1=0
  1015. vplay2=0
  1016. if options.videoplayer:
  1017. try:
  1018. t=options.videoplayer.split(",")
  1019. vplay1=t[0]
  1020. vplay2=t[1]
  1021. except:pass
  1022. def draw_overlay():
  1023. global fps,fps2
  1024. fr = font15.render("DMX-FPS: {}".format(fps) ,1, (200,0,255))
  1025. window.blit(fr,(10,2))
  1026. fr = font15.render("GUI-FPS : {}".format(fps2) ,1, (200,0,255))
  1027. window.blit(fr,(10,12))
  1028. #fr = font15.render("ip:{}".format(IP) ,1, (200,0,255))
  1029. #window.blit(fr,(100,2))
  1030. fr = font15.render("start-uni: {:}.xx dRGB".format(options.start_univ) ,1, (200,0,255))
  1031. window.blit(fr,(90,2))
  1032. fr = font15.render("a1_idim: 1.{:}".format(options.grid_a1_idim) ,1, (200,0,255))
  1033. window.blit(fr,(90,12))
  1034. fr = font15.render("a2_idim: 1.{:}".format(options.grid_a2_idim) ,1, (200,0,255))
  1035. window.blit(fr,(90,22))
  1036. fr = font15.render("gobo_ch1: 1.{:}".format(options.gobo_ch) ,1, (200,0,255))
  1037. window.blit(fr,(180,12))
  1038. fr = font15.render("gobo_ch2: 1.{:}".format(options.gobo_ch2) ,1, (200,0,255))
  1039. window.blit(fr,(180,22))
  1040. fr = font15.render("v-play1: 1.{:}".format(vplay1) ,1, (200,0,255))
  1041. window.blit(fr,(270,12))
  1042. fr = font15.render("v-play2: 1.{:}".format(vplay2) ,1, (200,0,255))
  1043. window.blit(fr,(270,22))
  1044. def draw_frame(window,rgb,p1,p2,offset=0):
  1045. o = offset
  1046. _p1 = [p1[0]-o,p1[1]-o]
  1047. _p2 = [p1[0]-o,p2[1]+o]
  1048. pygame.draw.line(window,rgb,_p1,_p2) # left
  1049. _p1 = [p2[0]+o,p2[1]+o]
  1050. pygame.draw.line(window,rgb,_p1,_p2) # bottom
  1051. _p2 = [p2[0]+o,p1[1]-o]
  1052. pygame.draw.line(window,rgb,_p1,_p2) # right
  1053. _p1 = [p1[0]-o,p1[1]-o]
  1054. pygame.draw.line(window,rgb,_p1,_p2) # top
  1055. def calc_fps():
  1056. global fps,frame,frame_t
  1057. t = time.time()
  1058. if frame_t+1 < t:
  1059. fps = frame #frame_t- t #frame
  1060. frame = 1
  1061. frame_t = time.time()
  1062. def calc_fps2():
  1063. global fps2,frame2,frame2_t
  1064. t = time.time()
  1065. if frame2_t+0.1 < t:
  1066. fps2 = frame2*10 #frame_t- t #frame
  1067. frame2 = 1
  1068. frame2_t = t #time.time()
  1069. TEXT_BLOCK = []
  1070. def _create_text_block():
  1071. print("======== CREATE NEW TEXT FILE !!",text_file)
  1072. f = open(text_file,"w")
  1073. for i in range(10):
  1074. f.write("TEXT {}\n".format(i+1))
  1075. f.close()
  1076. def open_text_block():
  1077. print("======== OPEN TEXT FILE !!",text_file)
  1078. _lines = []
  1079. try:
  1080. f = open(text_file,"r")
  1081. _lines = f.readlines()
  1082. f.close()
  1083. except FileNotFoundError as e:
  1084. print("TEXT",e)
  1085. _create_text_block()
  1086. if len(_lines) <= 0:
  1087. _create_text_block()
  1088. lines = []
  1089. for l in _lines:
  1090. #print(">> ",l.strip())
  1091. lines.append(l.strip())
  1092. if len(lines) <= 10:
  1093. for i in range(10-len(lines)):
  1094. lines.append("LINE ERROR")
  1095. return lines
  1096. TEXT_BLOCK = open_text_block()
  1097. TEXT_BLOCK_TIME = time.time()
  1098. # video playlist
  1099. PLAYLIST = []
  1100. def open_playlist():
  1101. print("======== OPEN PLAYLIST DIR !!",play_list)
  1102. if not os.path.isdir(play_list):
  1103. os.system("mkdir -p {}".format(play_list))
  1104. _lines = os.listdir(play_list)
  1105. _lines.sort()
  1106. lines = ['']*25
  1107. i=0
  1108. for l in _lines:
  1109. l = l.strip()
  1110. if "_" in l:
  1111. ll = l.split("_",1)
  1112. print(">> ",ll)
  1113. try:
  1114. lll = int(ll[0])
  1115. lines[lll] = l
  1116. except:pass
  1117. if len(lines) <= 10:
  1118. for i in range(10-len(lines)):
  1119. lines.append("")#"LINE ERROR")
  1120. return lines
  1121. PLAYLIST_TIME = time.time()
  1122. PLAYLIST = open_playlist()
  1123. # ===== GUI =========
  1124. def draw_circle(surface,color, pos, radius):
  1125. x,y=pos
  1126. pygame.gfxdraw.aacircle(surface, int(x), int(y), radius-1, color)
  1127. pygame.gfxdraw.filled_circle(surface, int(x), int(y), radius-1, color)
  1128. def rDMX(univ,dmx):
  1129. return univ*512+dmx
  1130. def generate_grid(mapping=0):
  1131. _log = []
  1132. #if PIXEL_MAPPING:
  1133. # log = open(grid_file,"w")
  1134. head = "i,univ,dmx,x,y,ch\n"
  1135. head = "i,univ,dmx,ch\n"
  1136. head = "univ,dmx,x,y,ch\n"
  1137. head = "nr,id,info\n"
  1138. print("csv:",head)
  1139. _log.append(head)
  1140. dmx = 1-1
  1141. ch = 4
  1142. y=0
  1143. x=0
  1144. for i in range((_y)*(_x)):
  1145. #if x > _x and i%_x == 0:
  1146. if x > 8 and i%8 == 0:
  1147. #print("--> -->")
  1148. x=0
  1149. y+=1
  1150. _univ = int(dmx/512)
  1151. _dmx = dmx - (_univ)*512
  1152. pos=[x,y]
  1153. line="{},{},x\n".format(i+1,i+1)
  1154. _log.append(line)
  1155. dmx += ch
  1156. x+=1
  1157. if mapping and PIXEL_MAPPING:
  1158. print("CREATE NEW PIXELMAP FILE !!",grid_file)
  1159. log = open(grid_file,"w")
  1160. log.writelines(_log)
  1161. log.close()
  1162. return _log[:] #GRID
  1163. def init_grid(mapping=0,_x=4,_y=4,_xp=0,_yp=0,start=0,name="init_gird"):
  1164. if mapping and PIXEL_MAPPING:
  1165. try:
  1166. log = open(grid_file,"r")
  1167. except:
  1168. generate_grid(mapping=mapping)
  1169. log = open(grid_file,"r")
  1170. lines = log.readlines()
  1171. else:
  1172. lines = generate_grid()
  1173. GRID = []
  1174. y=0
  1175. x=0
  1176. print("CSV header",[lines[0]],[PIXEL_MAPPING,"len:",len(lines),"start:",start,name])
  1177. for i,line in enumerate(lines[1+start:]): #exclude first line
  1178. #print("rcsv",[line])
  1179. line = line.strip()
  1180. line = line.split(",") # csv
  1181. if i >= _x and i%_x == 0:
  1182. x=0
  1183. y+=1
  1184. if y >= _y:
  1185. break
  1186. _id = int(line[1])
  1187. pos = [x+_xp,y+_yp]
  1188. f = Fix(_id,pos,block) #pos,univ,dmx,ch)
  1189. GRID.append(f)
  1190. x+=1
  1191. return GRID
  1192. def find_pix(x,y):
  1193. global GRID
  1194. for fix in GRID:
  1195. X = 0
  1196. Y = 0
  1197. pos = fix.POS()
  1198. if x > pos[0] and x < pos[0]+pos[2]:
  1199. X = 1
  1200. if y > pos[1] and y < pos[1]+pos[3]:
  1201. Y = 1
  1202. if X and Y:
  1203. return fix
  1204. GRID = []
  1205. GRID_A = []
  1206. NR = 0
  1207. START_UNIV=2
  1208. if options.start_univ:
  1209. try:
  1210. START_UNIV=int(options.start_univ)
  1211. except Exception as e:
  1212. print("Exception START UNIV",e)
  1213. gobo_ch=1
  1214. if options.gobo_ch:
  1215. try:
  1216. gobo_ch=int(options.gobo_ch)
  1217. except Exception as e:
  1218. print("Exception gobo_ch",e)
  1219. if gobo_ch <= 0:
  1220. gobo_ch = 1
  1221. def draw_box(pos1,pos2,color=[128,128,128],text=1):
  1222. color = [200,0,0,127]
  1223. if text:
  1224. fr = font15.render("A" ,1, (200,200,200))
  1225. window.blit(fr,pos1)
  1226. fr = font15.render("B" ,1, (200,200,200))
  1227. window.blit(fr,[pos2[0]-10,pos2[1]-10])
  1228. # h unten
  1229. _pos1 = [pos1[0],pos2[1]]
  1230. _pos2 = [pos2[0],pos2[1]]
  1231. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1232. color = [255,255,0,127]
  1233. # h rechts
  1234. _pos1 = [pos2[0],pos1[1]]
  1235. _pos2 = [pos2[0],pos2[1]]
  1236. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1237. color = [0,200,0,127]
  1238. # h links
  1239. _pos1 = [pos1[0],pos1[1]]
  1240. _pos2 = [pos1[0],pos2[1]]
  1241. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1242. color = [0,0,200,127]
  1243. # h oben
  1244. _pos1 = [pos1[0],pos1[1]]
  1245. _pos2 = [pos2[0],pos1[1]]
  1246. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1247. def grab(x=55,y=55,w=60,h=60):
  1248. crop = None
  1249. rect = pygame.Rect(x, y, w, h)
  1250. try:
  1251. sub = window.subsurface(rect)
  1252. crop = pygame.Surface((w,h))
  1253. crop.blit(sub, (0,0))
  1254. except ValueError as e:
  1255. pass
  1256. return crop
  1257. def reshape(GRID,GRID_OUT,_x,_y,name="GRID_Z"):
  1258. """reshape LED-WALL Block/Pixel mapping"""
  1259. if PIXEL_MAPPING <= 0:
  1260. return None
  1261. x = _x
  1262. y = _y
  1263. i = 0
  1264. counter = 0
  1265. z=0
  1266. x_min = 99999
  1267. x_max = 0
  1268. y_min = 99999
  1269. y_max = 0
  1270. pos= [0,0]
  1271. pos[0] = _x+p*3 #+10
  1272. pos[1] = p*8+65 #-10 #_y+p*8+80
  1273. tmp_font = pygame.font.SysFont("freemonobold",int(p*0.8))
  1274. fr = tmp_font.render("OUTPUT: "+str(name) ,1, (255,255,255))
  1275. fr_r = fr.get_rect(center=(int(wx/2),int(0+pm_wy-p*0-10)))
  1276. window.blit(fr,pos)
  1277. fr = tmp_font.render("INPUT" ,1, (255,255,255))
  1278. fr_r = fr.get_rect(center=(int(wx/2),int(60+pm_wy-p/2)))
  1279. window.blit(fr,fr_r)
  1280. j = 0
  1281. for fix in GRID_OUT:
  1282. if j >= 64:
  1283. break
  1284. j+=1
  1285. ii = i
  1286. pos = fix.POS(40,60)
  1287. rgb = fix.rgb
  1288. pos[0]+=_x
  1289. pos[1]+=_y
  1290. # green
  1291. pygame.draw.rect(window,[0,40,0],pos)
  1292. xposs = [] #None #pos[:]
  1293. for fix2 in GRID:
  1294. if fix._id == fix2._id:
  1295. _xposs = fix2.POS(40,60)
  1296. xposs.append( _xposs) #fix2.POS(40,60) )
  1297. # ToDo
  1298. for xpos in xposs:
  1299. sub = grab(xpos[0],xpos[1]+pm_wy,xpos[2],xpos[3])
  1300. if sub:
  1301. window.blit(sub, (pos[0]+z,pos[1]+z))
  1302. else:
  1303. # red
  1304. pygame.draw.rect(window,[40,0,0],pos) #[x+pos[0]+2+z,y+pos[1]+2+z-pm_wy,12,9])
  1305. if xpos[0] < x_min:
  1306. x_min = xpos[0]
  1307. if xpos[0] > x_max:
  1308. x_max += xpos[2]
  1309. if xpos[1] < y_min:
  1310. y_min = xpos[1]
  1311. if xpos[1] > x_max:
  1312. y_max += xpos[3]
  1313. # DRAW FIX NUMBER on TOP
  1314. apos = fix.POS(40,60)#+pm_wy)
  1315. apos[0]+=_x
  1316. apos[1]+=_y
  1317. argb = fix.rgb
  1318. # overwrite number overlay
  1319. if NR:
  1320. pygame.draw.rect(window,[20,20,20],[apos[0],apos[1],12,8])
  1321. fix_id = fix._id
  1322. if fix_id > 8*8:
  1323. fix_id -= 8*8
  1324. if NR:# == 2: # sub fix_nr
  1325. if fix_id != i+1:
  1326. fr = font12.render("{:02}".format(fix._id) ,1, (255,255,0))
  1327. window.blit(fr,(apos[0],apos[1]))
  1328. else:
  1329. fr = font12.render("{:02}".format(fix._id) ,1, (100,100,255))
  1330. window.blit(fr,(apos[0],apos[1]))
  1331. i += 1
  1332. #print("--#")
  1333. # frame box
  1334. pos1= [x+x_min,y+x_min]
  1335. pos2= [x_max+x_min,y_min+y_max]
  1336. class Timer():
  1337. def __init__(self,start=120):
  1338. self.start = start
  1339. self.timer = self.start
  1340. self.timer_t = time.time()
  1341. def reset(self):
  1342. self.timer = self.start
  1343. def get(self):
  1344. self.timer -= time.time()-self.timer_t
  1345. self.timer_t = time.time()
  1346. if self.timer <= 0:
  1347. self.reset()
  1348. return self.timer
  1349. def reload_grid():
  1350. print("==== reload_grid")
  1351. global GRID
  1352. global GRID_A
  1353. try:
  1354. GRID = init_grid(_x=_x,_y=_y,name="GRID") #init_gird()
  1355. GRID_A = init_grid(_x=8,_y=16,mapping=1,name="GRID_A") #init_gird()
  1356. except Exception as e:
  1357. print("Except: grid re init",e)
  1358. VPU_TEXT = []
  1359. def load_vpu_text(nr=0):
  1360. txt = "NONE"
  1361. if len(VPU_TEXT) > nr:
  1362. txt = VPU_TEXT[0]
  1363. return txt
  1364. grid_counter = time.time()
  1365. def draw_fix_nr(GRID):
  1366. global grid_counter
  1367. i=0
  1368. y=0
  1369. for fix in GRID:
  1370. pos = fix.POS(40,60+pm_wy)
  1371. rgb = fix.rgb
  1372. if NR:
  1373. pygame.draw.rect(window,[0,0,0],[pos[0]+2,pos[1]+2,17,9])
  1374. if fix._id%_x-1 == 0: # line break border
  1375. pygame.draw.line(window,[255,255,0],(pos[0],pos[1]+4),(pos[0],pos[1]+pos[3]-4),1)
  1376. pygame.draw.line(window,[255,255,0],(pos[0],pos[1]+int(pos[3]/2)),(pos[0]+int(pos[2]/2),int(pos[1]+pos[3]/2)),1)
  1377. if fix._id%_x == 0: # line break border
  1378. pygame.draw.line(window,[255,255,255],(pos[0]+pos[2]-1,pos[1]+4),(pos[0]+pos[2]-1,pos[1]+pos[3]-4),1)
  1379. pygame.draw.line(window,[255,255,255],(pos[0]+pos[2]-1,int(pos[1]+pos[3]/2)),(pos[0]+int(pos[2]/2-1),int(pos[1]+pos[3]/2)),1)
  1380. if grid_counter +5 < time.time():
  1381. grid_counter = time.time()
  1382. reload_grid()
  1383. if fix._id == 2 or i+1 == 2:
  1384. print("FIX.id,i",fix._id != i+1,end=" ")
  1385. print(fix._id, i+1)
  1386. if fix._id != i+1:
  1387. fr = font12.render("{:02}".format(fix._id) ,1, (255,255,0))
  1388. else:
  1389. fr = font12.render("{:02}".format(fix._id) ,1, (100,100,255))
  1390. window.blit(fr,(pos[0]+2,pos[1]+2))
  1391. i += 1
  1392. def draw_counter(COUNTER):
  1393. for count in COUNTER:
  1394. cpan = 0
  1395. ctilt = 0
  1396. cr=255
  1397. cg=255
  1398. cb=255
  1399. csize=10
  1400. cdim=0
  1401. k = "DIM"
  1402. if k in count:
  1403. cdim = int(count[k])
  1404. k = "RED"
  1405. if k in count:
  1406. cr = int(count[k])
  1407. k = "GREEN"
  1408. if k in count:
  1409. cg = int(count[k])
  1410. k = "BLUE"
  1411. if k in count:
  1412. cb = int(count[k])
  1413. k = "SIZE"
  1414. if k in count:
  1415. csize = int(count[k]) #/60*p)
  1416. if csize < 5:
  1417. csize = 5
  1418. k = "PAN"
  1419. if k in count:
  1420. cpan = int(count[k])/255*(block[0] *(_x))
  1421. #cpan = int(cpan * 1.2)
  1422. cpan = int(cpan)
  1423. k = "TILT"
  1424. if k in count:
  1425. ctilt = int(count[k])/255*(block[1] *(_y))
  1426. #ctilt = int(cpan * 1.2)
  1427. ctilt = int(ctilt)
  1428. ddim = cdim/255
  1429. if "DIM" in count and count["DIM"] > 0:
  1430. tmp_font = pygame.font.SysFont("freemonobold",int(p/100*csize))
  1431. rgb =(int(cr*ddim),int(cg*ddim),int(cb*ddim),cdim)
  1432. _sec = count["_SEC"]
  1433. try:
  1434. _sec = int(count["_SEC"])
  1435. except:
  1436. pass
  1437. if type(_sec) is int:
  1438. #print(_sec)
  1439. if _sec <= 60:
  1440. fr = tmp_font.render("{:0}".format(_sec) ,1, rgb)
  1441. else:
  1442. _xx = time.strftime("%M:%S",time.localtime(_sec))
  1443. #print("_xx",_xx)
  1444. fr = tmp_font.render("{}".format(_xx) ,1, rgb)
  1445. else:
  1446. fr = tmp_font.render("{}".format((count["_SEC"])) ,1, rgb)
  1447. fr_r = fr.get_rect(center=(40+cpan-(block[0]),60+ctilt+pm_wy))
  1448. pygame.draw.rect(window,[0,0,0],fr_r)
  1449. window.blit(fr,fr_r)
  1450. class FADE():
  1451. def __init__(self,sec):
  1452. self.start = time.time()
  1453. self.sec = sec
  1454. self.val
  1455. def next(self):
  1456. pass
  1457. def draw_video(VIDEO):
  1458. global videplayer
  1459. i = 0
  1460. # set DMX-VALUE to videoplayer Object
  1461. for count in VIDEO:
  1462. cpan = 0
  1463. ctilt = 0
  1464. cr=255
  1465. cg=255
  1466. cb=255
  1467. csize=10
  1468. cang=0
  1469. cdim=0
  1470. video1 = videoplayer[i]
  1471. k = "VIDEO"
  1472. if k in count:
  1473. #video1.select_video(count[k])
  1474. play_nr = int(count[k]/10)
  1475. if play_nr != video1._video_nr:
  1476. print( "+ + + + + + + + + + +", play_nr , video1._video_nr ,str(video1))
  1477. _vid = video1._id
  1478. _cdmx = video1.dmx
  1479. del_video1 = video1
  1480. video1 = Vopen(cdmx,_id=_vid)
  1481. video1.select_video(count[k])
  1482. videoplayer[i] = video1
  1483. del_video1._del()
  1484. del del_video1
  1485. k = "DIM"
  1486. if k in count:
  1487. cdim = int(count[k])
  1488. video1.dim = cdim
  1489. #if i == 0:
  1490. # print(i,cdim)
  1491. k = "ANG"
  1492. if k in count:
  1493. cang = int(count[k])
  1494. k = "SIZE"
  1495. if k in count:
  1496. csize = int(count[k]/16*p)
  1497. if csize < 5:
  1498. csize = 5
  1499. k = "PAN"
  1500. cpan_max = block[0] *(_x) #+block[0]
  1501. if k in count:
  1502. if 0:#video1.fname.startswith("cam_"):
  1503. cpan = int(count[k]) #/ 255*cpan_max
  1504. else:
  1505. cpan = int(count[k]) / 255*cpan_max
  1506. cpan = int(cpan)
  1507. k = "TILT"
  1508. ctilt_max = block[1] *(_y) #+block[1]
  1509. if k in count:
  1510. ctilt = int(count[k]) / 255*ctilt_max
  1511. ctilt = int(ctilt)
  1512. k = "_reset"
  1513. if k in count:
  1514. if count[k]:
  1515. count[k] = 0
  1516. video1.restart()
  1517. k = "_RUN"
  1518. if k in count:
  1519. video1._run = count[k]
  1520. video1.pos
  1521. video1.x=40+0+cpan
  1522. video1.y=60+0+pm_wy+ctilt
  1523. video1.scale = int((csize))
  1524. video1.angle = int(360-(cang))
  1525. if cdim:
  1526. video1.next()
  1527. i += 1
  1528. # draw
  1529. i=0
  1530. for count in VIDEO:
  1531. video1 = videoplayer[i]
  1532. video1.draw(window) #,x=0,y=0)
  1533. i+=1
  1534. i=0
  1535. for count in VIDEO:
  1536. video1 = videoplayer[i]
  1537. video1.overlay(window,"run")
  1538. i += 1
  1539. def counter_dmx(COUNTER,dataA):
  1540. for count in COUNTER:
  1541. cDMX=count["DMX"]-1
  1542. try:
  1543. count["DIM"] = dataA[cDMX]
  1544. count["PAN"] = dataA[cDMX+1]
  1545. count["TILT"] = dataA[cDMX+2]
  1546. count["CONTROL"] = dataA[cDMX+3]
  1547. if count["CONTROL"] >= 10 and count["CONTROL"] < 20:
  1548. count["_SEC"] = int(count["SEC"] - (time.time() - count["_time"]))
  1549. if count["CONTROL"] >= 20 and count["CONTROL"] < 30:
  1550. count["_RUN"] = 0
  1551. if count["CONTROL"] >= 30 and count["CONTROL"] < 40:
  1552. count["_RUN"] = 1
  1553. count["SIZE"] = dataA[cDMX+4]
  1554. count["SEC"] = dataA[cDMX+5]
  1555. if count["_RUN"]:
  1556. try:
  1557. count["_SEC"] = int(count["SEC"] - (time.time() - count["_time"]))
  1558. except Exception as e:
  1559. pass
  1560. if type(count["_SEC"]) is int:
  1561. if count["_SEC"] < 0:
  1562. count["_SEC"] = 0
  1563. for ti in range(10):
  1564. #print(ti,(ti+6)*10)
  1565. if count["CONTROL"] >= (ti+6)*10 and count["CONTROL"] < (ti+7)*10:
  1566. count["_SEC"] = "----" #text 1
  1567. try:
  1568. count["_SEC"] = TEXT_BLOCK[ti]
  1569. except Exception as e:
  1570. pass
  1571. if count["CONTROL"] >= 250 and count["CONTROL"] < 256:
  1572. count["_SEC"] = ">{}<".format(cDMX+1)
  1573. count["RED"] = dataA[cDMX+6]
  1574. count["GREEN"] = dataA[cDMX+7]
  1575. count["BLUE"] = dataA[cDMX+8]
  1576. except Exception as e:
  1577. print("EXC FUNC",e,count)
  1578. #print(count)
  1579. def video_dmx(VIDEO,dataA):
  1580. for count in VIDEO:
  1581. cDMX=count["DMX"]-1
  1582. try:
  1583. count["DIM"] = dataA[cDMX]
  1584. count["PAN"] = dataA[cDMX+1]
  1585. count["TILT"] = dataA[cDMX+2]
  1586. count["CONTROL"] = dataA[cDMX+3]
  1587. if count["CONTROL"] >= 10 and count["CONTROL"] < 20:
  1588. count["_reset"] = 1
  1589. if count["CONTROL"] >= 20 and count["CONTROL"] < 30:
  1590. if count["_RUN"] == 1:
  1591. print( "_RUN:0",count["DMX"]-1)
  1592. count["_RUN"] = 0
  1593. if count["CONTROL"] >= 30 and count["CONTROL"] < 40:
  1594. if count["_RUN"] == 0:
  1595. print( "_RUN:1",count["DMX"]-1)
  1596. count["_RUN"] = 1
  1597. count["SIZE"] = dataA[cDMX+4]#*2
  1598. count["SEC"] = dataA[cDMX+5]
  1599. #count["RED"] = dataA[cDMX+6]
  1600. count["VIDEO"] = dataA[cDMX+6]
  1601. #count["GREEN"] = dataA[cDMX+7]
  1602. count["ANG"] = (int(dataA[cDMX+7])/255*360)
  1603. #count["BLUE"] = dataA[cDMX+8]
  1604. except Exception as e:
  1605. print("VIDEOPLAYER EXCEPT FUNC",e,count)
  1606. #print(count)
  1607. def read_dmx_data(ip,ips):
  1608. ip = select_ip(ips,univ=START_UNIV)
  1609. IP = ip
  1610. data = read_dmx(ip)
  1611. ip = select_ip(ips,univ=START_UNIV+1)
  1612. data3 = read_dmx(ip)
  1613. data.extend(data3)
  1614. ip = select_ip(ips,univ=START_UNIV+2)
  1615. data3 = read_dmx(ip)
  1616. data.extend(data3)
  1617. ip = select_ip(ips,univ=START_UNIV+3)
  1618. data3 = read_dmx(ip)
  1619. data.extend(data3)
  1620. if options.dual_vpu:
  1621. ip = select_ip(ips,univ=START_UNIV+4)
  1622. data3 = read_dmx(ip)
  1623. data.extend(data3)
  1624. ip = select_ip(ips,univ=START_UNIV+5)
  1625. data3 = read_dmx(ip)
  1626. data.extend(data3)
  1627. return data
  1628. def draw_gobo(GRID,data):
  1629. i = 0
  1630. dmx = 1
  1631. h = 1
  1632. v = 1
  1633. for fix in GRID:
  1634. pos = fix.POS(40,60+pm_wy)
  1635. rgb = fix.rgb
  1636. if 1:
  1637. # draw row/col grid number
  1638. if fix.pos[0] == 0:
  1639. fr = font12.render("{}".format(fix.pos[1]+1) ,1, (200,200,200))
  1640. window.blit(fr,(10,pos[1]+3 ))
  1641. if fix.pos[1] == 0:
  1642. fr = font12.render("{}".format(fix.pos[0]+1) ,1, (200,200,200))
  1643. window.blit(fr,(pos[0]+2,35 ))
  1644. pygame.draw.rect(window,rgb,pos)
  1645. # DRAW SUB-FIXTURE
  1646. j = 0
  1647. for subfix in fix.sub_fix:#calc(data):
  1648. subfix.calc(data)
  1649. #fix = subfix
  1650. spos = subfix.POS(40,60+pm_wy)
  1651. srgb = subfix.rgb
  1652. #print(fix.dmx,rgb,pos)
  1653. #pygame.draw.circle(window,rgb,(pos[0]+int(pos[2]/2),pos[1]+int(pos[3]/2)),int(pos[3]/2))
  1654. #FUNC = 0
  1655. if FUNC > 10 and FUNC <= 20: # big dot
  1656. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1657. elif FUNC > 20 and FUNC <= 30:#small dot
  1658. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1659. elif FUNC > 30 and FUNC <= 40:#donut
  1660. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1661. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1662. elif FUNC > 40 and FUNC <= 50: # rec with hole
  1663. pygame.draw.rect(window,srgb,spos)
  1664. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1665. elif FUNC > 50 and FUNC <= 60: # rec with big hole
  1666. pygame.draw.rect(window,srgb,spos)
  1667. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1668. elif FUNC > 60 and FUNC <= 70: # rec with donat
  1669. pygame.draw.rect(window,srgb,spos)
  1670. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1671. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1672. elif FUNC > 70 and FUNC <= 80: # rec boarder
  1673. pygame.draw.rect(window,srgb,[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  1674. elif FUNC > 80 and FUNC <= 90: # rec big boarder
  1675. pygame.draw.rect(window,srgb,[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  1676. elif FUNC > 90 and FUNC <= 100: # rec thin line
  1677. pygame.draw.rect(window,srgb,spos)
  1678. pygame.draw.rect(window,[0,0,0],[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  1679. elif FUNC > 100 and FUNC <= 110: # rec big line
  1680. pygame.draw.rect(window,srgb,spos)
  1681. pygame.draw.rect(window,[0,0,0],[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  1682. elif FUNC > 110 and FUNC <= 120: # rec with dot
  1683. pygame.draw.rect(window,srgb,spos)
  1684. pygame.draw.rect(window,[0,0,0],[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  1685. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1686. elif FUNC > 120 and FUNC <= 130: # rec inline
  1687. pygame.draw.rect(window,srgb,[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  1688. pygame.draw.rect(window,[0,0,0],[spos[0]+3,spos[1]+3,spos[2]-6,spos[3]-6])
  1689. elif FUNC > 130 and FUNC <= 140: # 3 dot (heart)
  1690. draw_circle(window,srgb,(spos[0]+int(spos[2]/2)+2,spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1691. draw_circle(window,srgb,(spos[0]+int(spos[2]/2)-2,spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1692. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)+2),int(spos[3]/3.5))
  1693. else:
  1694. pygame.draw.rect(window,srgb,spos)
  1695. for subfix in fix.sub_fix:#calc(data):
  1696. subfix.calc(data)
  1697. #fix = subfix
  1698. spos = subfix.POS(40,60+pm_wy)
  1699. srgb = subfix.rgb
  1700. # draw row/col grid number
  1701. if subfix.pos[0] == 0:
  1702. fr = font12.render("{}".format(v ) ,1, (200,200,200))
  1703. window.blit(fr,(25,spos[1] ))
  1704. v += 1
  1705. if subfix.pos[1] == 0:
  1706. fr = font12.render("{}".format(1) ,1, (200,200,200))
  1707. fr = font12.render("{}".format(h ) ,1, (200,200,200))
  1708. h+=1
  1709. window.blit(fr,(spos[0],50 ))
  1710. if p >= 40:
  1711. if NR:
  1712. #fr = font15.render("{:02}".format(j+1) ,1, (0,200,255))
  1713. fr = font15.render("{:02}".format(subfix._id) ,1, (250,200,5))
  1714. window.blit(fr,(spos[0]+2,spos[1]+10))
  1715. j += 1
  1716. i += 1
  1717. def frame_area():
  1718. rgb = [255,255,0]
  1719. CFG = CFG_IN
  1720. p1 = CFG_IN["p1"]
  1721. p2 = CFG_IN["p2"]
  1722. draw_frame(window,rgb,p1,p2,offset=2)
  1723. rgb = [255,0,0]
  1724. #draw_frame(window,rgb,p1,p2,offset=4)
  1725. if CFG_OUT["on"]:
  1726. rgb = [255,0,0]
  1727. p1 = CFG_OUT["p1"]
  1728. p2 = CFG_OUT["p2"]
  1729. #pygame.draw.line(window,rgb,p1,p2)
  1730. draw_frame(window,rgb,p1,p2,offset=3)
  1731. if CFG_OUT2["on"]:
  1732. rgb = [255,0,0]
  1733. p1 = CFG_OUT2["p1"]
  1734. p2 = CFG_OUT2["p2"]
  1735. #pygame.draw.line(window,rgb,p1,p2)
  1736. draw_frame(window,rgb,p1,p2,offset=3)
  1737. ips=[]
  1738. dataA=[]
  1739. data=[]
  1740. def dmx_raw():
  1741. global frame
  1742. frame += 1
  1743. global ips,dataA,data
  1744. ips = read_index()
  1745. # ----
  1746. ip = select_ip(ips,univ=1) # univ 1 gobo
  1747. dataA = read_dmx(ip)
  1748. # ----
  1749. data = read_dmx_data(ip,ips)
  1750. if options.countdown:
  1751. counter_dmx(COUNTER,dataA)
  1752. if len(VIDEO) > 0:
  1753. video_dmx(VIDEO,dataA)
  1754. return ips,dataA,data
  1755. def dmx_loop():
  1756. while 1:
  1757. dmx_raw()
  1758. time.sleep(1/40) # fast high cpu
  1759. #time.sleep(1/25)
  1760. dmx_raw()
  1761. thread.start_new_thread(dmx_loop,())
  1762. t1 = Timer(143)
  1763. time.sleep(0.33)
  1764. t2 = Timer(11)
  1765. count_tilt = 0
  1766. def main():
  1767. global IP
  1768. global GRID
  1769. global GRID_A
  1770. global FUNC
  1771. global count_tilt
  1772. global TEXT_BLOCK
  1773. global TEXT_BLOCK_TIME
  1774. global PLAYLIST
  1775. global PLAYLIST_TIME
  1776. global dataA
  1777. global frame2
  1778. reload_grid()
  1779. s=time.time()
  1780. r = ""
  1781. IP = "xx"
  1782. while running:
  1783. if TEXT_BLOCK_TIME+5 < time.time():
  1784. TEXT_BLOCK = open_text_block()
  1785. TEXT_BLOCK_TIME = time.time()
  1786. if PLAYLIST_TIME+6 < time.time():
  1787. PLAYLIST = open_playlist()
  1788. PLAYLIST_TIME = time.time()
  1789. pygame.display.flip()
  1790. event()
  1791. window.fill((10,0,30))
  1792. calc_fps()
  1793. calc_fps2()
  1794. draw_overlay()
  1795. # GRID loop
  1796. try:
  1797. FUNC = dataA[gobo_ch-1]
  1798. FUNC2 = FUNC
  1799. except Exception as e:
  1800. print("EXC FUNC",e)
  1801. draw_gobo(GRID,data)
  1802. if VIDEO:
  1803. draw_video(VIDEO)
  1804. if options.countdown:
  1805. draw_counter(COUNTER)
  1806. pointer.draw(0,pm_wy) #wy
  1807. pygame.draw.rect(window,[5,5,5],[0,60,MAIN_SIZE[0],p*9]) # background
  1808. #pygame.draw.rect(window,[5,5,5],[0,p*8+p*10+120,MAIN_SIZE[0],500]) # background
  1809. spos = [0,0,0,0]
  1810. if PIXEL_MAPPING >= 1:
  1811. try:
  1812. GRID_X = GRID_A[8*8:]
  1813. xx = p*8
  1814. reshape(GRID,GRID_X,xx+102,-xx,name="GRID_A2") #start pos
  1815. except Exception as e:
  1816. print("Exception 23123",e)
  1817. reshape(GRID,GRID_A,0,0,name="GRID_A1") #start pos
  1818. else:
  1819. reshape(GRID,GRID_A,spos[0]+spos[2]+20,10) #start pos
  1820. # DRAW FIX NR
  1821. draw_fix_nr(GRID)
  1822. frame_area()
  1823. # GRID_A1 DIM
  1824. if options.grid_a1_idim:
  1825. grid_dim_ch = int(options.grid_a1_idim)
  1826. grid_dim_v = dataA[grid_dim_ch-1]
  1827. dim=255
  1828. try:
  1829. dim = int(grid_dim_v)
  1830. if dim > 255:
  1831. dim = 255
  1832. if dim <=0:
  1833. dim = 0
  1834. except:pass
  1835. dim_raw = dim*-1
  1836. #dim = 255-dim
  1837. xx = p*8
  1838. s = pygame.Surface((p*8,p*8)) # the size of your rect
  1839. s.set_alpha(dim) # alpha level
  1840. s.fill((0,0,0)) # this fills the entire surface
  1841. window.blit(s, (40,60))#(xx,-xx)) # (0,0) are the top-left coordinates
  1842. #tmp_font = pygame.font.SysFont("freemonobold",int(p*0.8))
  1843. fr = font15.render("inv-dim: "+str(dim_raw) ,1, (255,255,255))
  1844. fr_r = fr.get_rect(center=(int(wx/2),int(0+pm_wy-p*0-10)))
  1845. window.blit(fr,(48,p*9+60))
  1846. # GRID_A2 DIM
  1847. if options.grid_a2_idim:
  1848. grid_dim_ch = int(options.grid_a2_idim)
  1849. grid_dim_v = dataA[grid_dim_ch-1]
  1850. dim=255
  1851. try:
  1852. dim = int(grid_dim_v)
  1853. if dim > 255:
  1854. dim = 255
  1855. if dim <=0:
  1856. dim = 0
  1857. except:pass
  1858. dim_raw = dim*-1
  1859. #dim = 255-dim
  1860. xx = p*8
  1861. s = pygame.Surface((p*8,p*8)) # the size of your rect
  1862. s.set_alpha(dim) # alpha level
  1863. s.fill((0,0,0)) # this fills the entire surface
  1864. window.blit(s, (xx*2+14,60))#(xx,-xx)) # (0,0) are the top-left coordinates
  1865. fr = font15.render("inv-dim: "+str(dim_raw) ,1, (255,255,255))
  1866. fr_r = fr.get_rect(center=(int(wx/2),int(0+pm_wy-p*0-10)))
  1867. window.blit(fr,(p*16+22,p*9+60))
  1868. pygame.display.flip()
  1869. clock.tick(25)
  1870. if 'SDL_VIDEO_WINDOW_POS' in os.environ:
  1871. del os.environ['SDL_VIDEO_WINDOW_POS'] #= '%i,%i' % (200,164)
  1872. frame2 += 1
  1873. if __name__ == "__main__":
  1874. main()