vpu_live.py 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384
  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=2,
  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))) # corner center-top
  531. #wn.blit(self.im, (int(self.x-xx/2), int(self.y-yy))) # corner center-bottom
  532. wn.blit(self.im, (int(self.x-xx/2), int(self.y-yy))) # corner center-bottom
  533. else:
  534. wn.blit(self.im, (int(self.x-xx/2), int(self.y-yy/2)))
  535. def overlay(self,wn=None,mode="x"):
  536. # overlay
  537. pygame.draw.rect(wn,[255,200,0],[5,MAIN_SIZE[1]-(self._id+1)*35,300,28])
  538. font15 = pygame.font.SysFont("freemonobold",17)
  539. pz = 0
  540. if self.end:
  541. rgb = [ 100,255,100]
  542. else:
  543. rgb = [255,100,0]
  544. pygame.draw.rect(wn,rgb,[220,MAIN_SIZE[1]-(self._id+1)*35,80,13])
  545. _line = "error no _line"
  546. _line ="FPS:{} F:{:05} von {:05} sec:{:0.02f} von {:0.02f}"
  547. if self.fps == 0: # check if div zerro
  548. _line = _line.format(self.fps,int(self.pos),len(self.buffer),(-1),pz )
  549. else:
  550. pz = (len(self.buffer)/self.fps)
  551. _line = _line.format(self.fps,int(self.pos),len(self.buffer),(self.pos/self.fps),pz )
  552. fr = font15.render(_line ,1, (0,0,0))
  553. wn.blit(fr,(10,MAIN_SIZE[1]-(self._id+1)*35))
  554. if self._run:
  555. mode = "run"
  556. else:
  557. mode = "pause"
  558. fr = font15.render(" {} {} >:{} ".format(self._id+1,self._video_nr,mode) ,1, (0,0,0))
  559. wn.blit(fr,(3,MAIN_SIZE[1]-(self._id+1)*35+15))
  560. fr = font15.render("{}".format(self.fname) ,1, (0,0,0))
  561. wn.blit(fr,(70,MAIN_SIZE[1]-(self._id+1)*35+15))
  562. Vopen = Vopen
  563. VIDEO = []
  564. videoplayer=[]
  565. cv2 = None
  566. FileVideoStream = None
  567. _vid = 0
  568. if type(options.videoplayer) is str:
  569. try:
  570. import cv2
  571. except Exception as e:
  572. print("Except Import:",e)
  573. try:
  574. # faster video reading ... ???
  575. from imutils.video import FileVideoStream
  576. except Exception as e:
  577. print("Except Import:",e)
  578. max_videoplayer = 4
  579. dmx_start = options.videoplayer.split(",")
  580. for cdmx in dmx_start:
  581. if len(videoplayer) > max_videoplayer:
  582. print("-- videoplayer max count {} !! break".format(max_videoplayer))
  583. break
  584. print("-- videoplayer dmx:",cdmx)
  585. try:
  586. cdmx = int(cdmx)
  587. videoplayer.append( Vopen(cdmx,_id=_vid) )
  588. _vid += 1
  589. _tmp = {"DMX":cdmx,"DIM":0,"PAN":127,"TILT":127
  590. ,"CONTROL":0,"SEC":10,"VIDEO":"3"
  591. ,"RED":255,"GREEN":255,"BLUE":255
  592. ,"_time":time.time()
  593. ,"_RUN":0,"_SEC":">{}<".format(cdmx)
  594. }
  595. VIDEO.append(_tmp)
  596. except Exception as e:
  597. print("EXCEPTION COUNTER INIT ",cdmx)
  598. def loop_videoplayer():
  599. while 1:
  600. _videoplayer = videoplayer[:]
  601. #print(".")
  602. ok = 0
  603. for i in _videoplayer: #.append( Vopen(cdmx,_id=_vid) )
  604. try:
  605. r = i._read() # read next frame from file
  606. if r:
  607. ok = 1
  608. except Exception as e:
  609. print("EXCEPTION loop_videoplayer ",e)
  610. if ok == 0:
  611. time.sleep(0.1)
  612. else:
  613. time.sleep(0.005)
  614. thread.start_new_thread(loop_videoplayer,())
  615. # ===== ======
  616. #self.fname = PLAYLIST[0]
  617. videoplayer2 = []
  618. def loop2_videoplayer():
  619. while 1:
  620. print()
  621. print()
  622. print()
  623. for i,v in enumerate(PLAYLIST):
  624. try:
  625. video1 = videoplayer2[i]
  626. video1.select_video(i)
  627. except:
  628. vi = Vopen(181,1)
  629. videoplayer2.append( vi )
  630. _videoplayer = videoplayer2[:]
  631. ok = 0
  632. j =0
  633. for i in _videoplayer: #.append( Vopen(cdmx,_id=_vid) )
  634. try:
  635. r = i._read() # read next frame from file
  636. if r:
  637. print(j,len(videoplayer2),i,len(i.buffer))
  638. ok = 1
  639. except Exception as e:
  640. print("EXCEPTION loop2_videoplayer ",e)
  641. time.sleep(0.002)
  642. j += 1
  643. p = 16
  644. block = [p,p]
  645. _x = 8
  646. _y = 8
  647. #HD = "0"
  648. if options.mode:
  649. try:
  650. HD = options.mode
  651. p,_x,_y = HD.split(",")
  652. _x = int(_x)
  653. _y = int(_y)
  654. p = int(p)
  655. block = [p,p]
  656. except Exception as e:
  657. print( "Exc",options.mode,e)
  658. # PARSE COMMANDLINE ARGUMENTS
  659. CFG_IN = {"name":"CFG_IN","x1":40,"y1":60,"x2":300,"y2":300 ,"w":300,"h":300}
  660. CFG_OUT = {"name":"CFG_OUT","x1":40,"y1":60,"x2":300,"y2":300 ,"w":300,"h":300,"on":0}
  661. #CFG_OUT2 = {"name":"CFG_OUT2","x1":p*8+142,"y1":60,"x2":300,"y2":300 ,"w":300,"h":300,"on":0}
  662. CFG_OUT2 = {"name":"CFG_OUT2","x1":p*8+(16*2)+8,"y1":60,"x2":300,"y2":300 ,"w":300,"h":300,"on":0}
  663. CFG_BLOCK = {"name":"CFG_BLOCK","size":16,"h-split":2,"v-split":2,"h-count":8,"v-count":8}
  664. if _x < 1:
  665. _x = 1
  666. if _y < 1:
  667. _y = 1
  668. CFG_BLOCK["h-count"] = _x
  669. CFG_BLOCK["v-count"] = _y # int(8*8/_x) #/_x+0.5) #_y
  670. CFG_BLOCK["size"] = p
  671. print( [options.xsplit])
  672. print( [options.ysplit])
  673. try:
  674. if options.xsplit:
  675. CFG_BLOCK["h-split"] = int(options.xsplit)
  676. if options.ysplit:
  677. CFG_BLOCK["v-split"] = int(options.ysplit)
  678. except Exception as e:
  679. print( "Exc",options.mode,e)
  680. print("HD",CFG_BLOCK["h-split"],CFG_BLOCK["v-split"])
  681. print("xy",_x,_y)
  682. print("++++++++++++++++++", p,_x,_y)
  683. _x2 = _x
  684. try:
  685. if options.XX:
  686. pass#_x2 = int(options.XX)
  687. except Exception as e:
  688. print( "Exc",options.mode,e)
  689. print("_x2 , -X",_x2)
  690. # ===== GUI =========
  691. import pygame
  692. import pygame.gfxdraw
  693. import pygame.font
  694. clock = pygame.time.Clock()
  695. os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (200,164)
  696. if options.win_pos:
  697. if "," in options.win_pos:
  698. win_pos = options.win_pos.split(",")
  699. try:
  700. WIN_POS = '%i,%i' % (int(win_pos[0]),int(win_pos[1]) )
  701. os.environ['SDL_VIDEO_WINDOW_POS'] = WIN_POS
  702. except Excetpion as e:
  703. print("win_pos",win_pos,e)
  704. os.environ['SDL_VIDEO_CENTERED'] = '0'
  705. pg = pygame
  706. pygame.init()
  707. pygame.mixer.quit()
  708. f = pygame.font.get_fonts()
  709. for i in f:
  710. if "mono" in i.lower():
  711. print(i)
  712. font = pygame.font.SysFont("freemonobold",22)
  713. font10 = pygame.font.SysFont("freemonobold",10)
  714. font12 = pygame.font.SysFont("freemonobold",12)
  715. font12l = pygame.font.SysFont("freemono",12)
  716. font15 = pygame.font.SysFont("freemonobold",15)
  717. font40 = pygame.font.SysFont("freemonobold",40)
  718. font80 = pygame.font.SysFont("freemonobold",70)
  719. #font = pygame.font.SysFont(None,30)
  720. fr = font.render("hallo" ,1, (200,0,255))
  721. def clean_path(path):
  722. _path = path[:]
  723. _path = _path.replace("/","-")
  724. _path = _path.replace(".","-")
  725. _path = _path.replace("\"","-")
  726. _path = _path.replace("'","-")
  727. return _path
  728. PIXEL_MAPPING = 0
  729. grid_file = "/tmp/vpu_grid_hd.csv"
  730. text_file = HOME+"/LibreLight/vpu_text_hd.csv"
  731. play_list = "/tmp/vpu_playlist_hd.csv"
  732. play_list = HOME+"/LibreLight/video/" #.format(path)
  733. pm_wy = 0
  734. if options.pixel_mapping:
  735. PIXEL_MAPPING = 1
  736. CFG_OUT["on"] = 1
  737. path = options.pixel_mapping
  738. path = clean_path(path)
  739. grid_file = HOME+"/LibreLight/vpu_grid_hd{}.csv".format(path)
  740. if options.dual_vpu:
  741. grid_file = HOME+"/LibreLight/vpu_grid_dual{}.csv".format(path)
  742. text_file = HOME+"/LibreLight/vpu_text_hd{}.csv".format(path)
  743. play_list = HOME+"/LibreLight/vpu_playlist_hd{}.csv".format(path)
  744. play_list = HOME+"/LibreLight/video/" #.format(path)
  745. #_x = 8
  746. #_y = 8
  747. if options.dual_vpu:
  748. CFG_OUT2["on"] = 1
  749. print(" ",[options.pixel_mapping],"grid_file",grid_file)
  750. #grid_file = HOME+"/LibreLight/vpu_grid_hd.csv"
  751. MAIN_SIZE=(600,500)
  752. try:
  753. if _x < 8 and PIXEL_MAPPING >= 1:
  754. wx = 30+30+block[0] * 8
  755. CFG_IN["w"] = CFG_BLOCK["size"] * 8
  756. else:
  757. wx = 30+30+block[0] * _x
  758. wy = 40+40+block[1] * _y
  759. if type(options.videoplayer) is str:
  760. wy += 150 # video playlist
  761. MAIN_SIZE=(wx,wy)
  762. if PIXEL_MAPPING >= 1:
  763. pm_wy = 11*p #+ p*3
  764. CFG_IN["y1"] += 11*p
  765. if options.dual_vpu:
  766. if wx < p*16+155:
  767. wx = p*16+155
  768. MAIN_SIZE=(wx,wy+pm_wy)
  769. except Exception as e:
  770. print("Exception:",e)
  771. CFG_IN["w"] = CFG_BLOCK["size"] * CFG_BLOCK["h-count"]
  772. CFG_IN["h"] = CFG_BLOCK["size"] * CFG_BLOCK["v-count"]
  773. CFG_OUT["w"] = CFG_BLOCK["size"] * 8
  774. CFG_OUT["h"] = CFG_BLOCK["size"] * 8
  775. CFG_OUT2["w"] = CFG_BLOCK["size"] * 8
  776. CFG_OUT2["h"] = CFG_BLOCK["size"] * 8
  777. def CFG_CALC_P(CFG):
  778. CFG["x2"] = CFG["x1"]+CFG["w"]
  779. CFG["y2"] = CFG["y1"]+CFG["h"]
  780. CFG["p1"] = [CFG["x1"] ,CFG["y1"]]
  781. CFG["p2"] = [CFG["x2"] ,CFG["y2"]]
  782. print("CFG",CFG)
  783. # ?
  784. #CFG_IN["w"] = int(CFG_BLOCK["v-count"]-2) * CFG_BLOCK["size"]
  785. CFG_CALC_P(CFG_IN)
  786. CFG_CALC_P(CFG_OUT)
  787. CFG_CALC_P(CFG_OUT2)
  788. print("CFG_BLOCK",CFG_BLOCK)
  789. print()
  790. print()
  791. print()
  792. print()
  793. print()
  794. window = pygame.display.set_mode(MAIN_SIZE,pg.RESIZABLE)#,32)#,pygame.FULLSCREEN) #x left->right ,y top-> bottom
  795. pg.display.set_caption('LibreLight VPU-{}'.format(options.title))
  796. class Fix():
  797. def __init__(self,_id,pos,block=[16,16],univ=0,dmx=0,ch=4):
  798. #print("Fix",_id)
  799. self._id = _id
  800. self.dmx = (_id-1) * ch +1 #dmx
  801. self.univ = univ
  802. self.ch = ch
  803. self.pos = pos
  804. self.rgb = [0,0,0]
  805. self.block = block #[10,10]
  806. self.x = pos[0]
  807. self.y = pos[1]
  808. self.strobo = time.time()
  809. self.bmp = 250
  810. self.sub_fix = []
  811. sub_block =[block[0]/CFG_BLOCK["h-split"],block[1]/CFG_BLOCK["v-split"]]
  812. if _id <= 0: #exit
  813. return
  814. spalte = (_id-1)%_y +1
  815. zeile = int((_id-1)/_x2) #+1
  816. add_row = _x*CFG_BLOCK["h-split"]*CFG_BLOCK["v-split"]
  817. #zeile 1
  818. sid = (_id-1)*2 + zeile*CFG_BLOCK["h-split"]*_x2
  819. sid = sid+1
  820. sub_pos= [pos[0]*block[0],pos[1]*block[1]]
  821. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  822. self.sub_fix.append(sub_fix)
  823. sid = sid+1
  824. sub_pos= [pos[0]*block[0]+block[0]/2,pos[1]*block[1]]
  825. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  826. self.sub_fix.append(sub_fix)
  827. #zeile 2
  828. sid = (_id-1)*2+1 + _x2*CFG_BLOCK["h-split"] + zeile*CFG_BLOCK["h-split"]*_x2 # int(add_row)
  829. sub_pos= [pos[0]*block[0],pos[1]*block[1]+block[1]/2]
  830. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  831. self.sub_fix.append(sub_fix)
  832. #sid = sid+1
  833. sid = sid+1
  834. sub_pos= [pos[0]*block[0]+block[0]/2,pos[1]*block[1]+block[1]/2]
  835. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  836. self.sub_fix.append(sub_fix)
  837. def calc(self,data):
  838. _rgb = [0,255,0]
  839. return _rgb
  840. def sub_calc(self,data):
  841. _rgb = [0,255,0]
  842. for sub_fix in self.sub_fix:
  843. sub_fix.block = self.block[:]
  844. _rgb = sub_fix.calc(data)
  845. return _rgb
  846. def POS(self,x=0,y=0,a=0,b=0):
  847. A = (self.pos[0])*self.block[0]
  848. B = (self.pos[1])*self.block[1]
  849. C = self.block[0]-a
  850. D = self.block[1]-b
  851. return [x+A,y+B,C,D]
  852. def subPOS(self,x=0,y=0,a=0,b=0):
  853. __out = []
  854. for sub_fix in self.sub_fix:
  855. __out.append( sub_fix.POS(x,y,a,b) )
  856. return __out
  857. class SubFix():
  858. def __init__(self,_id,pos,block=[16,16],univ=0,dmx=0,ch=4):
  859. #print("Fix",_id)
  860. self._id = _id
  861. self.dmx = (_id-1) * ch +1 #dmx
  862. self.univ = univ
  863. self.ch = ch
  864. self.pos = pos
  865. self.rgb = [0,0,40]
  866. self.block = block #[10,10]
  867. self.x = pos[0]
  868. self.y = pos[1]
  869. self.strobo = time.time()
  870. self.bmp = 250
  871. def calc(self,data):
  872. #return [130,30,20]
  873. dmx_sub = [30]*10
  874. dmx = self.dmx -1
  875. _dmx_sub = []
  876. if self.dmx >= 0:
  877. dmx = rDMX(self.univ,self.dmx)-1
  878. if dmx+self.ch < len(data):
  879. _dmx_sub = data[dmx:dmx+self.ch]
  880. if _dmx_sub:
  881. dmx_sub = _dmx_sub
  882. dim = dmx_sub[0]/255
  883. r = dmx_sub[1]*dim
  884. g = dmx_sub[2]*dim
  885. b = dmx_sub[3]*dim
  886. r = int(r)
  887. g = int(g)
  888. b = int(b)
  889. self.rgb = [r,g,b]
  890. return self.rgb
  891. def POS(self,x=0,y=0,a=0,b=0):
  892. A = (self.pos[0]) #+self.block[0]
  893. B = (self.pos[1]) #+self.block[1]
  894. C = self.block[0]-a
  895. D = self.block[1]-b
  896. if NR:
  897. C-=1
  898. D-=1
  899. return [int(x+A),int(y+B),int(C),int(D)]
  900. class POINTER():
  901. def __init__(self):
  902. self.pos = [0,0,0,0]
  903. self.on = 0
  904. self.rgb = [0,100,10]
  905. self._x = 0
  906. self._y = 0
  907. self.x = 0
  908. self.y = 0
  909. self.fix = Fix(0 ,[999,999],[16,16],0,0,0)
  910. def row_move(self,x,y):
  911. self._x = x
  912. self._y = y
  913. def move(self,pos):
  914. self.pos = pos
  915. self.on = 1
  916. def cross(self,x,y):
  917. self.x = x
  918. self.y = y
  919. def draw(self,x,y):
  920. pos = self.pos[:]
  921. #print("draw",x,y,pos)
  922. pos[0] += x
  923. pos[1] += y
  924. fix_x= self.fix.x
  925. fix_y= self.fix.y +y
  926. if self.on:
  927. pygame.draw.rect(window,self.rgb,pos)
  928. # mouse grid posision
  929. fr = font15.render("{}/{}".format(fix_x+1,fix_y) ,1, (200,200,200))
  930. _nr = fix_y * _x + fix_x +1
  931. fr = font15.render("{:02}".format(_nr ) ,1, (200,200,200))
  932. window.blit(fr,(pos[0]+2,pos[1]+2 ))
  933. window.blit(fr,(130,1))
  934. # fix pos
  935. #txt=str(pos) #"[0, 0, 0, 0]"
  936. #fr = font15.render(txt ,1, (200,200,200))
  937. #window.blit(fr,(10,1))
  938. # pointer
  939. fr = font15.render("X:{:03}".format(self._x) ,1, (200,200,200))
  940. window.blit(fr,(10,30))
  941. fr = font15.render("Y:{:03}".format(self._y) ,1, (200,200,200))
  942. window.blit(fr,(10,40))
  943. # crosshair
  944. self.rgb = [0,0,200]
  945. pygame.draw.line(window,self.rgb, (self.x-p,self.y) , (self.x-2,self.y),4 )
  946. pygame.draw.line(window,self.rgb, (self.x,self.y-p) , (self.x,self.y-2),4 )
  947. self.rgb = [0,200,0]
  948. pygame.draw.line(window,self.rgb, (self.x+2,self.y) , (self.x+p,self.y),4 )
  949. pygame.draw.line(window,self.rgb, (self.x,self.y+2) , (self.x,self.y+p),4 )
  950. self.rgb = [200,0,0]
  951. pointer = POINTER()
  952. NR = 0
  953. running = True
  954. def event():
  955. global NR,running
  956. for event in pygame.event.get():
  957. #print(event.dict)
  958. _button = None
  959. if "button" in event.dict:
  960. _button = event.dict["button"]
  961. _state = None
  962. if "state" in event.dict:
  963. _state = event.state
  964. _key = None
  965. if "key" in event.dict:
  966. _key = event.key
  967. _pos = None
  968. if "pos" in event.dict:
  969. _pos = event.pos
  970. _type = None
  971. if "type" in event.dict:
  972. _type = event.type
  973. _type = event.type
  974. _mod = None
  975. if "mod" in event.dict:
  976. _mod = event.mod
  977. if 0:
  978. print( " ")
  979. print( "{:.02f}".format( time.time() - START ))
  980. print("button -",_button,end="\t| ")
  981. #print("state -",_state)
  982. print("pos -",_pos)
  983. print("type -",_type, end="\t| ")
  984. print("key -",_key)
  985. print("mod -",_mod)
  986. try:
  987. if _type == 5:
  988. if _button == 1:
  989. NR += 1
  990. if NR > 1:
  991. NR = 0
  992. if _button == 3:
  993. NR -= 1
  994. if NR < 0:
  995. NR = 1
  996. if _pos:
  997. posA = _pos
  998. fix = find_pix(_pos[0]-40,_pos[1]-60+pm_wy)
  999. if fix:
  1000. #pos = fix.POS(40,60+pm_wy)
  1001. pos = fix.POS(CFG_IN["x1"],CFG_IN["y1"])#0,0) #+pm_wy)
  1002. rgb = [0,0,0]
  1003. pointer.move(pos)
  1004. pointer.fix = fix
  1005. else:
  1006. pointer.on = 0
  1007. pointer.row_move(_pos[0],_pos[1])
  1008. pointer.cross(_pos[0],_pos[1])
  1009. if event.type == pygame.VIDEORESIZE:
  1010. window = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)
  1011. except Exception as e:
  1012. print(e)
  1013. if event.type==pygame.QUIT:
  1014. print("QUIT")
  1015. running=False
  1016. #time.sleep(1)
  1017. sys.exit()
  1018. fps = 0
  1019. fps2 = 0
  1020. frame = 0
  1021. frame2 = 0
  1022. frame_t = time.time()
  1023. frame2_t = time.time()
  1024. IP = "yyy"
  1025. vplay1=0
  1026. vplay2=0
  1027. if options.videoplayer:
  1028. try:
  1029. t=options.videoplayer.split(",")
  1030. vplay1=t[0]
  1031. vplay2=t[1]
  1032. except:pass
  1033. def draw_overlay():
  1034. global fps,fps2
  1035. fr = font15.render("DMX-FPS: {}".format(fps) ,1, (200,0,255))
  1036. window.blit(fr,(10,2))
  1037. fr = font15.render("GUI-FPS : {}".format(fps2) ,1, (200,0,255))
  1038. window.blit(fr,(10,12))
  1039. #fr = font15.render("ip:{}".format(IP) ,1, (200,0,255))
  1040. #window.blit(fr,(100,2))
  1041. fr = font15.render("start-uni: {:}.xx dRGB".format(options.start_univ) ,1, (200,0,255))
  1042. window.blit(fr,(90,2))
  1043. fr = font15.render("a1_idim: 1.{:}".format(options.grid_a1_idim) ,1, (200,0,255))
  1044. window.blit(fr,(90,12))
  1045. fr = font15.render("a2_idim: 1.{:}".format(options.grid_a2_idim) ,1, (200,0,255))
  1046. window.blit(fr,(90,22))
  1047. fr = font15.render("gobo_ch1: 1.{:}".format(options.gobo_ch) ,1, (200,0,255))
  1048. window.blit(fr,(180,12))
  1049. fr = font15.render("gobo_ch2: 1.{:}".format(options.gobo_ch2) ,1, (200,0,255))
  1050. window.blit(fr,(180,22))
  1051. fr = font15.render("v-play1: 1.{:}".format(vplay1) ,1, (200,0,255))
  1052. window.blit(fr,(270,12))
  1053. fr = font15.render("v-play2: 1.{:}".format(vplay2) ,1, (200,0,255))
  1054. window.blit(fr,(270,22))
  1055. def draw_frame(window,rgb,p1,p2,offset=0):
  1056. o = offset
  1057. _p1 = [p1[0]-o,p1[1]-o]
  1058. _p2 = [p1[0]-o,p2[1]+o]
  1059. #pygame.draw.line(window,rgb,_p1,_p2) # left
  1060. _p1 = [p2[0]+o,p2[1]+o]
  1061. pygame.draw.line(window,rgb,_p1,_p2) # bottom
  1062. _p2 = [p2[0]+o,p1[1]-o]
  1063. #pygame.draw.line(window,rgb,_p1,_p2) # right
  1064. _p1 = [p1[0]-o,p1[1]-o]
  1065. pygame.draw.line(window,rgb,_p1,_p2) # top
  1066. def calc_fps():
  1067. global fps,frame,frame_t
  1068. t = time.time()
  1069. if frame_t+1 < t:
  1070. fps = frame #frame_t- t #frame
  1071. frame = 1
  1072. frame_t = time.time()
  1073. def calc_fps2():
  1074. global fps2,frame2,frame2_t
  1075. t = time.time()
  1076. if frame2_t+0.1 < t:
  1077. fps2 = frame2*10 #frame_t- t #frame
  1078. frame2 = 1
  1079. frame2_t = t #time.time()
  1080. TEXT_BLOCK = []
  1081. def _create_text_block():
  1082. print("======== CREATE NEW TEXT FILE !!",text_file)
  1083. f = open(text_file,"w")
  1084. for i in range(10):
  1085. f.write("TEXT {}\n".format(i+1))
  1086. f.close()
  1087. def open_text_block():
  1088. print("======== OPEN TEXT FILE !!",text_file)
  1089. _lines = []
  1090. try:
  1091. f = open(text_file,"r")
  1092. _lines = f.readlines()
  1093. f.close()
  1094. except FileNotFoundError as e:
  1095. print("TEXT",e)
  1096. _create_text_block()
  1097. if len(_lines) <= 0:
  1098. _create_text_block()
  1099. lines = []
  1100. for l in _lines:
  1101. #print(">> ",l.strip())
  1102. lines.append(l.strip())
  1103. if len(lines) <= 10:
  1104. for i in range(10-len(lines)):
  1105. lines.append("LINE ERROR")
  1106. return lines
  1107. TEXT_BLOCK = open_text_block()
  1108. TEXT_BLOCK_TIME = time.time()
  1109. # video playlist
  1110. PLAYLIST = []
  1111. def open_playlist():
  1112. print("======== OPEN PLAYLIST DIR !!",play_list)
  1113. if not os.path.isdir(play_list):
  1114. os.system("mkdir -p {}".format(play_list))
  1115. _lines = os.listdir(play_list)
  1116. _lines.sort()
  1117. lines = ['']*25
  1118. i=0
  1119. for l in _lines:
  1120. l = l.strip()
  1121. if "_" in l:
  1122. ll = l.split("_",1)
  1123. print(">> ",ll)
  1124. try:
  1125. lll = int(ll[0])
  1126. lines[lll] = l
  1127. except:pass
  1128. if len(lines) <= 10:
  1129. for i in range(10-len(lines)):
  1130. lines.append("")#"LINE ERROR")
  1131. return lines
  1132. PLAYLIST_TIME = time.time()
  1133. PLAYLIST = open_playlist()
  1134. # ===== GUI =========
  1135. def draw_circle(surface,color, pos, radius):
  1136. x,y=pos
  1137. pygame.gfxdraw.aacircle(surface, int(x), int(y), radius-1, color)
  1138. pygame.gfxdraw.filled_circle(surface, int(x), int(y), radius-1, color)
  1139. def rDMX(univ,dmx):
  1140. return univ*512+dmx
  1141. def generate_grid(mapping=0):
  1142. _log = []
  1143. #if PIXEL_MAPPING:
  1144. # log = open(grid_file,"w")
  1145. head = "i,univ,dmx,x,y,ch\n"
  1146. head = "i,univ,dmx,ch\n"
  1147. head = "univ,dmx,x,y,ch\n"
  1148. head = "nr,id,info\n"
  1149. print("csv:",head)
  1150. _log.append(head)
  1151. dmx = 1-1
  1152. ch = 4
  1153. y=0
  1154. x=0
  1155. for i in range((_y)*(_x)):
  1156. #if x > _x and i%_x == 0:
  1157. if x > 8 and i%8 == 0:
  1158. #print("--> -->")
  1159. x=0
  1160. y+=1
  1161. _univ = int(dmx/512)
  1162. _dmx = dmx - (_univ)*512
  1163. pos=[x,y]
  1164. line="{},{},x\n".format(i+1,i+1)
  1165. _log.append(line)
  1166. dmx += ch
  1167. x+=1
  1168. if mapping and PIXEL_MAPPING:
  1169. print("CREATE NEW PIXELMAP FILE !!",grid_file)
  1170. log = open(grid_file,"w")
  1171. log.writelines(_log)
  1172. log.close()
  1173. return _log[:] #GRID
  1174. def init_grid(mapping=0,_x=4,_y=4,_xp=0,_yp=0,start=0,name="init_gird"):
  1175. if mapping and PIXEL_MAPPING:
  1176. try:
  1177. log = open(grid_file,"r")
  1178. except:
  1179. generate_grid(mapping=mapping)
  1180. log = open(grid_file,"r")
  1181. lines = log.readlines()
  1182. else:
  1183. lines = generate_grid()
  1184. GRID = []
  1185. y=0
  1186. x=0
  1187. print("CSV header",[lines[0]],[PIXEL_MAPPING,"len:",len(lines),"start:",start,name])
  1188. for i,line in enumerate(lines[1+start:]): #exclude first line
  1189. #print("rcsv",[line])
  1190. line = line.strip()
  1191. line = line.split(",") # csv
  1192. if i >= _x and i%_x == 0:
  1193. x=0
  1194. y+=1
  1195. if y >= _y:
  1196. break
  1197. _id = int(line[1])
  1198. pos = [x+_xp,y+_yp]
  1199. f = Fix(_id,pos,block) #pos,univ,dmx,ch)
  1200. GRID.append(f)
  1201. x+=1
  1202. return GRID
  1203. def find_pix(x,y):
  1204. global GRID
  1205. for fix in GRID:
  1206. X = 0
  1207. Y = 0
  1208. pos = fix.POS()
  1209. if x > pos[0] and x < pos[0]+pos[2]:
  1210. X = 1
  1211. if y > pos[1] and y < pos[1]+pos[3]:
  1212. Y = 1
  1213. if X and Y:
  1214. return fix
  1215. GRID = []
  1216. GRID_A = []
  1217. NR = 0
  1218. START_UNIV=2
  1219. if options.start_univ:
  1220. try:
  1221. START_UNIV=int(options.start_univ)
  1222. except Exception as e:
  1223. print("Exception START UNIV",e)
  1224. gobo_ch=1
  1225. if options.gobo_ch:
  1226. try:
  1227. gobo_ch=int(options.gobo_ch)
  1228. except Exception as e:
  1229. print("Exception gobo_ch",e)
  1230. if gobo_ch <= 0:
  1231. gobo_ch = 1
  1232. def draw_box(pos1,pos2,color=[128,128,128],text=1):
  1233. color = [200,0,0,127]
  1234. if text:
  1235. fr = font15.render("A" ,1, (200,200,200))
  1236. window.blit(fr,pos1)
  1237. fr = font15.render("B" ,1, (200,200,200))
  1238. window.blit(fr,[pos2[0]-10,pos2[1]-10])
  1239. # h unten
  1240. _pos1 = [pos1[0],pos2[1]]
  1241. _pos2 = [pos2[0],pos2[1]]
  1242. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1243. color = [255,255,0,127]
  1244. # h rechts
  1245. _pos1 = [pos2[0],pos1[1]]
  1246. _pos2 = [pos2[0],pos2[1]]
  1247. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1248. color = [0,200,0,127]
  1249. # h links
  1250. _pos1 = [pos1[0],pos1[1]]
  1251. _pos2 = [pos1[0],pos2[1]]
  1252. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1253. color = [0,0,200,127]
  1254. # h oben
  1255. _pos1 = [pos1[0],pos1[1]]
  1256. _pos2 = [pos2[0],pos1[1]]
  1257. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1258. def grab(x=55,y=55,w=60,h=60):
  1259. crop = None
  1260. rect = pygame.Rect(x, y, w, h)
  1261. try:
  1262. sub = window.subsurface(rect)
  1263. crop = pygame.Surface((w,h))
  1264. crop.blit(sub, (0,0))
  1265. except ValueError as e:
  1266. pass
  1267. return crop
  1268. def reshape(GRID,GRID_OUT,_x,_y,name="GRID_Z"):
  1269. """reshape LED-WALL Block/Pixel mapping"""
  1270. if PIXEL_MAPPING <= 0:
  1271. return None
  1272. x = _x
  1273. y = _y
  1274. i = 0
  1275. counter = 0
  1276. z=0
  1277. x_min = 99999
  1278. x_max = 0
  1279. y_min = 99999
  1280. y_max = 0
  1281. pos= [0,0]
  1282. pos[0] = _x+p*3 #+10
  1283. pos[1] = p*8+65 #-10 #_y+p*8+80
  1284. tmp_font = pygame.font.SysFont("freemonobold",int(p*0.8))
  1285. #fr = tmp_font.render("OUTPUT: "+str(name) ,1, (255,255,255))
  1286. #fr_r = fr.get_rect(center=(int(wx/2),int(0+pm_wy-p*0-10)))
  1287. #window.blit(fr,pos)
  1288. fr = tmp_font.render("INPUT" ,1, (255,255,255))
  1289. fr_r = fr.get_rect(center=(int(wx/2),int(60+pm_wy-p/2)))
  1290. window.blit(fr,fr_r)
  1291. j = 0
  1292. for fix in GRID_OUT:
  1293. if j >= 64:
  1294. break
  1295. j+=1
  1296. ii = i
  1297. #pos = fix.POS(40,60)
  1298. pos = fix.POS(0,0)
  1299. rgb = fix.rgb
  1300. pos[0]+=_x
  1301. pos[1]+=_y
  1302. # green
  1303. pygame.draw.rect(window,[0,40,0],pos)
  1304. xposs = [] #None #pos[:]
  1305. for fix2 in GRID:
  1306. if fix._id == fix2._id:
  1307. #_xposs = fix2.POS(40,60)
  1308. #_xposs = fix2.POS(0,0)#_x,_y)
  1309. _xposs = fix2.POS(CFG_IN["x1"],CFG_IN["y1"])
  1310. xposs.append( _xposs) #fix2.POS(40,60) )
  1311. # ToDo
  1312. for xpos in xposs:
  1313. #sub = grab(xpos[0],xpos[1]+pm_wy,xpos[2],xpos[3])
  1314. sub = grab(xpos[0],xpos[1],xpos[2],xpos[3])
  1315. if NR:
  1316. pygame.draw.rect(window,[255,0,255],xpos,1) # frame on grab area
  1317. if sub:
  1318. window.blit(sub, (pos[0]+z,pos[1]+z))
  1319. else:
  1320. # red
  1321. pygame.draw.rect(window,[40,0,0],pos) #[x+pos[0]+2+z,y+pos[1]+2+z-pm_wy,12,9])
  1322. if xpos[0] < x_min:
  1323. x_min = xpos[0]
  1324. if xpos[0] > x_max:
  1325. x_max += xpos[2]
  1326. if xpos[1] < y_min:
  1327. y_min = xpos[1]
  1328. if xpos[1] > x_max:
  1329. y_max += xpos[3]
  1330. # DRAW FIX NUMBER on TOP
  1331. #apos = fix.POS(40,60)#+pm_wy)
  1332. apos = fix.POS(0,0)#+pm_wy)
  1333. apos[0]+=_x
  1334. apos[1]+=_y
  1335. argb = fix.rgb
  1336. # overwrite number overlay
  1337. if NR:
  1338. pygame.draw.rect(window,[20,20,20],[apos[0],apos[1],12,8])
  1339. fix_id = fix._id
  1340. if fix_id > 8*8:
  1341. fix_id -= 8*8
  1342. if NR:# == 2: # sub fix_nr
  1343. if fix_id != i+1:
  1344. fr = font12.render("{:02}".format(fix._id) ,1, (255,255,0))
  1345. window.blit(fr,(apos[0],apos[1]))
  1346. else:
  1347. fr = font12.render("{:02}".format(fix._id) ,1, (100,100,255))
  1348. window.blit(fr,(apos[0],apos[1]))
  1349. i += 1
  1350. #print("--#")
  1351. # frame box
  1352. pos1= [x+x_min,y+x_min]
  1353. pos2= [x_max+x_min,y_min+y_max]
  1354. class Timer():
  1355. def __init__(self,start=120):
  1356. self.start = start
  1357. self.timer = self.start
  1358. self.timer_t = time.time()
  1359. def reset(self):
  1360. self.timer = self.start
  1361. def get(self):
  1362. self.timer -= time.time()-self.timer_t
  1363. self.timer_t = time.time()
  1364. if self.timer <= 0:
  1365. self.reset()
  1366. return self.timer
  1367. def reload_grid():
  1368. print("==== reload_grid")
  1369. global GRID
  1370. global GRID_A
  1371. try:
  1372. GRID = init_grid(_x=_x,_y=_y,name="GRID") #init_gird()
  1373. GRID_A = init_grid(_x=8,_y=16,mapping=1,name="GRID_A") #init_gird()
  1374. except Exception as e:
  1375. print("Except: grid re init",e)
  1376. VPU_TEXT = []
  1377. def load_vpu_text(nr=0):
  1378. txt = "NONE"
  1379. if len(VPU_TEXT) > nr:
  1380. txt = VPU_TEXT[0]
  1381. return txt
  1382. grid_counter = time.time()
  1383. def draw_fix_nr(GRID,_x=0,_y=0):
  1384. global grid_counter
  1385. i=0
  1386. y=0
  1387. for fix in GRID:
  1388. #pos = fix.POS(40,60+pm_wy)
  1389. pos = fix.POS(_x,_y)#+pm_wy)
  1390. rgb = fix.rgb
  1391. if NR:
  1392. pygame.draw.rect(window,[0,0,0],[pos[0]+2,pos[1]+2,17,9])
  1393. if fix._id%_x-1 == 0: # line break border
  1394. pygame.draw.line(window,[255,255,0],(pos[0],pos[1]+4),(pos[0],pos[1]+pos[3]-4),1)
  1395. 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)
  1396. if fix._id%_x == 0: # line break border
  1397. 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)
  1398. 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)
  1399. if grid_counter +5 < time.time():
  1400. grid_counter = time.time()
  1401. reload_grid()
  1402. if fix._id == 2 or i+1 == 2:
  1403. print("FIX.id,i",fix._id != i+1,end=" ")
  1404. print(fix._id, i+1)
  1405. if fix._id != i+1:
  1406. fr = font12.render("{:02}".format(fix._id) ,1, (255,255,0))
  1407. else:
  1408. fr = font12.render("{:02}".format(fix._id) ,1, (100,100,255))
  1409. window.blit(fr,(pos[0]+2,pos[1]+2))
  1410. i += 1
  1411. def draw_counter(COUNTER):
  1412. for count in COUNTER:
  1413. cpan = 0
  1414. ctilt = 0
  1415. cr=255
  1416. cg=255
  1417. cb=255
  1418. csize=10
  1419. cdim=0
  1420. k = "DIM"
  1421. if k in count:
  1422. cdim = int(count[k])
  1423. k = "RED"
  1424. if k in count:
  1425. cr = int(count[k])
  1426. k = "GREEN"
  1427. if k in count:
  1428. cg = int(count[k])
  1429. k = "BLUE"
  1430. if k in count:
  1431. cb = int(count[k])
  1432. k = "SIZE"
  1433. if k in count:
  1434. csize = int(count[k]) #/60*p)
  1435. if csize < 5:
  1436. csize = 5
  1437. k = "PAN"
  1438. if k in count:
  1439. cpan = int(count[k])/255*(block[0] *(_x))
  1440. #cpan = int(cpan * 1.2)
  1441. cpan = int(cpan)
  1442. k = "TILT"
  1443. if k in count:
  1444. ctilt = int(count[k])/255*(block[1] *(_y))
  1445. #ctilt = int(cpan * 1.2)
  1446. ctilt = int(ctilt)
  1447. ddim = cdim/255
  1448. if "DIM" in count and count["DIM"] > 0:
  1449. tmp_font = pygame.font.SysFont("freemonobold",int(p/100*csize))
  1450. rgb =(int(cr*ddim),int(cg*ddim),int(cb*ddim),cdim)
  1451. _sec = count["_SEC"]
  1452. try:
  1453. _sec = int(count["_SEC"])
  1454. except:
  1455. pass
  1456. if type(_sec) is int:
  1457. #print(_sec)
  1458. if _sec <= 60:
  1459. fr = tmp_font.render("{:0}".format(_sec) ,1, rgb)
  1460. else:
  1461. _xx = time.strftime("%M:%S",time.localtime(_sec))
  1462. #print("_xx",_xx)
  1463. fr = tmp_font.render("{}".format(_xx) ,1, rgb)
  1464. else:
  1465. fr = tmp_font.render("{}".format((count["_SEC"])) ,1, rgb)
  1466. fr_r = fr.get_rect(center=(40+cpan-(block[0]),60+ctilt+pm_wy))
  1467. pygame.draw.rect(window,[0,0,0],fr_r)
  1468. window.blit(fr,fr_r)
  1469. class FADE():
  1470. def __init__(self,sec):
  1471. self.start = time.time()
  1472. self.sec = sec
  1473. self.val
  1474. def next(self):
  1475. pass
  1476. def draw_video(VIDEO):
  1477. global videplayer
  1478. i = 0
  1479. # set DMX-VALUE to videoplayer Object
  1480. for count in VIDEO:
  1481. cpan = 0
  1482. ctilt = 0
  1483. cr=255
  1484. cg=255
  1485. cb=255
  1486. csize=10
  1487. cang=0
  1488. cdim=0
  1489. video1 = videoplayer[i]
  1490. k = "VIDEO"
  1491. if k in count:
  1492. #video1.select_video(count[k])
  1493. play_nr = int(count[k]/10)
  1494. if play_nr != video1._video_nr:
  1495. print( "+ + + + + + + + + + +", play_nr , video1._video_nr ,str(video1))
  1496. _vid = video1._id
  1497. _cdmx = video1.dmx
  1498. del_video1 = video1
  1499. video1 = Vopen(cdmx,_id=_vid)
  1500. video1.select_video(count[k])
  1501. videoplayer[i] = video1
  1502. del_video1._del()
  1503. del del_video1
  1504. k = "DIM"
  1505. if k in count:
  1506. cdim = int(count[k])
  1507. video1.dim = cdim
  1508. #if i == 0:
  1509. # print(i,cdim)
  1510. k = "ANG"
  1511. if k in count:
  1512. cang = int(count[k])
  1513. k = "SIZE"
  1514. if k in count:
  1515. csize = int(count[k]/16*p)
  1516. if csize < 5:
  1517. csize = 5
  1518. k = "PAN"
  1519. cpan_max = block[0] *(_x) #+block[0]
  1520. if k in count:
  1521. if 0:#video1.fname.startswith("cam_"):
  1522. cpan = int(count[k]) #/ 255*cpan_max
  1523. else:
  1524. cpan = int(count[k]) / 255*cpan_max
  1525. cpan = int(cpan)
  1526. k = "TILT"
  1527. ctilt_max = block[1] *(_y) #+block[1]
  1528. if k in count:
  1529. ctilt = int(count[k]) / 255*ctilt_max
  1530. ctilt = int(ctilt)
  1531. k = "_reset"
  1532. if k in count:
  1533. if count[k]:
  1534. count[k] = 0
  1535. video1.restart()
  1536. k = "_RUN"
  1537. if k in count:
  1538. video1._run = count[k]
  1539. video1.pos
  1540. #video1.x=40+0+cpan
  1541. #video1.y=60+0+pm_wy+ctilt
  1542. video1.x=CFG_IN["x1"]+cpan
  1543. video1.y=CFG_IN["y1"]+ctilt
  1544. video1.scale = int((csize))
  1545. video1.angle = int(360-(cang))
  1546. if cdim:
  1547. video1.next()
  1548. i += 1
  1549. # draw
  1550. i=0
  1551. for count in VIDEO:
  1552. video1 = videoplayer[i]
  1553. video1.draw(window) #,x=0,y=0)
  1554. i+=1
  1555. i=0
  1556. for count in VIDEO:
  1557. video1 = videoplayer[i]
  1558. video1.overlay(window,"run")
  1559. i += 1
  1560. def counter_dmx(COUNTER,dataA):
  1561. for count in COUNTER:
  1562. cDMX=count["DMX"]-1
  1563. try:
  1564. count["DIM"] = dataA[cDMX]
  1565. count["PAN"] = dataA[cDMX+1]
  1566. count["TILT"] = dataA[cDMX+2]
  1567. count["CONTROL"] = dataA[cDMX+3]
  1568. if count["CONTROL"] >= 10 and count["CONTROL"] < 20:
  1569. count["_SEC"] = int(count["SEC"] - (time.time() - count["_time"]))
  1570. if count["CONTROL"] >= 20 and count["CONTROL"] < 30:
  1571. count["_RUN"] = 0
  1572. if count["CONTROL"] >= 30 and count["CONTROL"] < 40:
  1573. count["_RUN"] = 1
  1574. count["SIZE"] = dataA[cDMX+4]
  1575. count["SEC"] = dataA[cDMX+5]
  1576. if count["_RUN"]:
  1577. try:
  1578. count["_SEC"] = int(count["SEC"] - (time.time() - count["_time"]))
  1579. except Exception as e:
  1580. pass
  1581. if type(count["_SEC"]) is int:
  1582. if count["_SEC"] < 0:
  1583. count["_SEC"] = 0
  1584. for ti in range(10):
  1585. #print(ti,(ti+6)*10)
  1586. if count["CONTROL"] >= (ti+6)*10 and count["CONTROL"] < (ti+7)*10:
  1587. count["_SEC"] = "----" #text 1
  1588. try:
  1589. count["_SEC"] = TEXT_BLOCK[ti]
  1590. except Exception as e:
  1591. pass
  1592. if count["CONTROL"] >= 250 and count["CONTROL"] < 256:
  1593. count["_SEC"] = ">{}<".format(cDMX+1)
  1594. count["RED"] = dataA[cDMX+6]
  1595. count["GREEN"] = dataA[cDMX+7]
  1596. count["BLUE"] = dataA[cDMX+8]
  1597. except Exception as e:
  1598. print("EXC FUNC",e,count)
  1599. #print(count)
  1600. def video_dmx(VIDEO,dataA):
  1601. for count in VIDEO:
  1602. cDMX=count["DMX"]-1
  1603. try:
  1604. count["DIM"] = dataA[cDMX]
  1605. count["PAN"] = dataA[cDMX+1]
  1606. count["TILT"] = dataA[cDMX+2]
  1607. count["CONTROL"] = dataA[cDMX+3]
  1608. if count["CONTROL"] >= 10 and count["CONTROL"] < 20:
  1609. count["_reset"] = 1
  1610. if count["CONTROL"] >= 20 and count["CONTROL"] < 30:
  1611. if count["_RUN"] == 1:
  1612. print( "_RUN:0",count["DMX"]-1)
  1613. count["_RUN"] = 0
  1614. if count["CONTROL"] >= 30 and count["CONTROL"] < 40:
  1615. if count["_RUN"] == 0:
  1616. print( "_RUN:1",count["DMX"]-1)
  1617. count["_RUN"] = 1
  1618. count["SIZE"] = dataA[cDMX+4]#*2
  1619. count["SEC"] = dataA[cDMX+5]
  1620. #count["RED"] = dataA[cDMX+6]
  1621. count["VIDEO"] = dataA[cDMX+6]
  1622. #count["GREEN"] = dataA[cDMX+7]
  1623. count["ANG"] = (int(dataA[cDMX+7])/255*360)
  1624. #count["BLUE"] = dataA[cDMX+8]
  1625. except Exception as e:
  1626. print("VIDEOPLAYER EXCEPT FUNC",e,count)
  1627. #print(count)
  1628. def read_dmx_data(ip,ips):
  1629. ip = select_ip(ips,univ=START_UNIV)
  1630. IP = ip
  1631. data = read_dmx(ip)
  1632. ip = select_ip(ips,univ=START_UNIV+1)
  1633. data3 = read_dmx(ip)
  1634. data.extend(data3)
  1635. ip = select_ip(ips,univ=START_UNIV+2)
  1636. data3 = read_dmx(ip)
  1637. data.extend(data3)
  1638. ip = select_ip(ips,univ=START_UNIV+3)
  1639. data3 = read_dmx(ip)
  1640. data.extend(data3)
  1641. if options.dual_vpu:
  1642. ip = select_ip(ips,univ=START_UNIV+4)
  1643. data3 = read_dmx(ip)
  1644. data.extend(data3)
  1645. ip = select_ip(ips,univ=START_UNIV+5)
  1646. data3 = read_dmx(ip)
  1647. data.extend(data3)
  1648. return data
  1649. def draw_gobo(GRID,data,_x=0,_y=0):
  1650. i = 0
  1651. dmx = 1
  1652. h = 1
  1653. v = 1
  1654. for fix in GRID:
  1655. #pos = fix.POS(40,60+pm_wy)
  1656. pos = fix.POS(_x,_y)#40,60+pm_wy)
  1657. rgb = fix.rgb
  1658. if 1:
  1659. # draw row/col grid number
  1660. if fix.pos[0] == 0:
  1661. fr = font12.render("{}".format(fix.pos[1]+1) ,1, (200,200,200))
  1662. window.blit(fr,(10,pos[1]+3 ))
  1663. if fix.pos[1] == 0:
  1664. fr = font12.render("{}".format(fix.pos[0]+1) ,1, (200,200,200))
  1665. window.blit(fr,(pos[0]+2,35 ))
  1666. pygame.draw.rect(window,rgb,pos)
  1667. # DRAW SUB-FIXTURE
  1668. j = 0
  1669. for subfix in fix.sub_fix:#calc(data):
  1670. subfix.calc(data)
  1671. #fix = subfix
  1672. #spos = subfix.POS(40,60+pm_wy)
  1673. spos = subfix.POS(_x,_y)#+pm_wy)
  1674. srgb = subfix.rgb
  1675. #print(fix.dmx,rgb,pos)
  1676. #pygame.draw.circle(window,rgb,(pos[0]+int(pos[2]/2),pos[1]+int(pos[3]/2)),int(pos[3]/2))
  1677. #FUNC = 0
  1678. if FUNC > 10 and FUNC <= 20: # big dot
  1679. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1680. elif FUNC > 20 and FUNC <= 30:#small dot
  1681. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1682. elif FUNC > 30 and FUNC <= 40:#donut
  1683. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1684. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1685. elif FUNC > 40 and FUNC <= 50: # rec with hole
  1686. pygame.draw.rect(window,srgb,spos)
  1687. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1688. elif FUNC > 50 and FUNC <= 60: # rec with big hole
  1689. pygame.draw.rect(window,srgb,spos)
  1690. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1691. elif FUNC > 60 and FUNC <= 70: # rec with donat
  1692. pygame.draw.rect(window,srgb,spos)
  1693. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1694. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1695. elif FUNC > 70 and FUNC <= 80: # rec boarder
  1696. pygame.draw.rect(window,srgb,[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  1697. elif FUNC > 80 and FUNC <= 90: # rec big boarder
  1698. pygame.draw.rect(window,srgb,[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  1699. elif FUNC > 90 and FUNC <= 100: # rec thin line
  1700. pygame.draw.rect(window,srgb,spos)
  1701. pygame.draw.rect(window,[0,0,0],[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  1702. elif FUNC > 100 and FUNC <= 110: # rec big line
  1703. pygame.draw.rect(window,srgb,spos)
  1704. pygame.draw.rect(window,[0,0,0],[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  1705. elif FUNC > 110 and FUNC <= 120: # rec with dot
  1706. pygame.draw.rect(window,srgb,spos)
  1707. pygame.draw.rect(window,[0,0,0],[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  1708. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1709. elif FUNC > 120 and FUNC <= 130: # rec inline
  1710. pygame.draw.rect(window,srgb,[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  1711. pygame.draw.rect(window,[0,0,0],[spos[0]+3,spos[1]+3,spos[2]-6,spos[3]-6])
  1712. elif FUNC > 130 and FUNC <= 140: # 3 dot (heart)
  1713. draw_circle(window,srgb,(spos[0]+int(spos[2]/2)+2,spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1714. draw_circle(window,srgb,(spos[0]+int(spos[2]/2)-2,spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1715. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)+2),int(spos[3]/3.5))
  1716. else:
  1717. pygame.draw.rect(window,srgb,spos)
  1718. for subfix in fix.sub_fix:#calc(data):
  1719. subfix.calc(data)
  1720. #fix = subfix
  1721. #spos = subfix.POS(40,60+pm_wy)
  1722. spos = subfix.POS(_x,_y)#+pm_wy)
  1723. srgb = subfix.rgb
  1724. # draw row/col grid number
  1725. if subfix.pos[0] == 0:
  1726. fr = font12.render("{}".format(v ) ,1, (200,200,200))
  1727. window.blit(fr,(25,spos[1] ))
  1728. v += 1
  1729. if subfix.pos[1] == 0:
  1730. fr = font12.render("{}".format(1) ,1, (200,200,200))
  1731. fr = font12.render("{}".format(h ) ,1, (200,200,200))
  1732. h+=1
  1733. window.blit(fr,(spos[0],50 ))
  1734. if p >= 40:
  1735. if NR:
  1736. #fr = font15.render("{:02}".format(j+1) ,1, (0,200,255))
  1737. fr = font15.render("{:02}".format(subfix._id) ,1, (250,200,5))
  1738. window.blit(fr,(spos[0]+2,spos[1]+10))
  1739. j += 1
  1740. i += 1
  1741. def frame_area():
  1742. rgb = [255,255,0]
  1743. CFG = CFG_IN
  1744. p1 = CFG_IN["p1"]
  1745. p2 = CFG_IN["p2"]
  1746. draw_frame(window,rgb,p1,p2,offset=2)
  1747. rgb = [255,0,0]
  1748. #draw_frame(window,rgb,p1,p2,offset=4)
  1749. if CFG_OUT["on"]:
  1750. rgb = [255,0,0]
  1751. p1 = CFG_OUT["p1"]
  1752. p2 = CFG_OUT["p2"]
  1753. #pygame.draw.line(window,rgb,p1,p2)
  1754. draw_frame(window,rgb,p1,p2,offset=3)
  1755. if CFG_OUT2["on"]:
  1756. rgb = [255,0,0]
  1757. p1 = CFG_OUT2["p1"]
  1758. p2 = CFG_OUT2["p2"]
  1759. #pygame.draw.line(window,rgb,p1,p2)
  1760. draw_frame(window,rgb,p1,p2,offset=3)
  1761. ips=[]
  1762. dataA=[]
  1763. data=[]
  1764. def dmx_raw():
  1765. global frame
  1766. frame += 1
  1767. global ips,dataA,data
  1768. ips = read_index()
  1769. # ----
  1770. ip = select_ip(ips,univ=1) # univ 1 gobo
  1771. dataA = read_dmx(ip)
  1772. # ----
  1773. data = read_dmx_data(ip,ips)
  1774. if options.countdown:
  1775. counter_dmx(COUNTER,dataA)
  1776. if len(VIDEO) > 0:
  1777. video_dmx(VIDEO,dataA)
  1778. return ips,dataA,data
  1779. def dmx_loop():
  1780. while 1:
  1781. dmx_raw()
  1782. time.sleep(1/40) # fast high cpu
  1783. #time.sleep(1/25)
  1784. dmx_raw()
  1785. thread.start_new_thread(dmx_loop,())
  1786. t1 = Timer(143)
  1787. time.sleep(0.33)
  1788. t2 = Timer(11)
  1789. count_tilt = 0
  1790. def main():
  1791. global IP
  1792. global GRID
  1793. global GRID_A
  1794. global FUNC
  1795. global count_tilt
  1796. global TEXT_BLOCK
  1797. global TEXT_BLOCK_TIME
  1798. global PLAYLIST
  1799. global PLAYLIST_TIME
  1800. global dataA
  1801. global frame2
  1802. global runnung
  1803. reload_grid()
  1804. s=time.time()
  1805. r = ""
  1806. IP = "xx"
  1807. while running:
  1808. if TEXT_BLOCK_TIME+5 < time.time():
  1809. TEXT_BLOCK = open_text_block()
  1810. TEXT_BLOCK_TIME = time.time()
  1811. if PLAYLIST_TIME+6 < time.time():
  1812. PLAYLIST = open_playlist()
  1813. PLAYLIST_TIME = time.time()
  1814. pygame.display.flip()
  1815. event()
  1816. window.fill((10,0,30))
  1817. calc_fps()
  1818. calc_fps2()
  1819. draw_overlay()
  1820. # GRID loop
  1821. try:
  1822. FUNC = dataA[gobo_ch-1]
  1823. FUNC2 = FUNC
  1824. except Exception as e:
  1825. print("EXC FUNC",e)
  1826. draw_gobo(GRID,data,_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  1827. if VIDEO:
  1828. draw_video(VIDEO)
  1829. if options.countdown:
  1830. draw_counter(COUNTER)
  1831. pointer.draw(0,pm_wy) #wy
  1832. pygame.draw.rect(window,[5,5,5],[0,60,MAIN_SIZE[0],p*9]) # background
  1833. spos = [0,0,0,0]
  1834. if PIXEL_MAPPING >= 1:
  1835. try:
  1836. GRID_X = GRID_A[8*8:]
  1837. xx = p*8
  1838. reshape(GRID,GRID_X,CFG_OUT2["x1"],CFG_OUT2["y1"]-p*8,name="GRID_A2") #start pos
  1839. except Exception as e:
  1840. print("Exception 23123",e)
  1841. reshape(GRID,GRID_A,CFG_OUT["x1"],CFG_OUT["y1"],name="GRID_A1") #start pos
  1842. else:
  1843. pass #reshape(GRID,GRID_A,spos[0]+spos[2]+20,10) #start pos
  1844. # DRAW FIX NR
  1845. draw_fix_nr(GRID,_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  1846. frame_area()
  1847. # OUTPUT -> MAPING
  1848. # GRID_A1 DIM
  1849. if options.grid_a1_idim:
  1850. CFG=CFG_OUT
  1851. grid_dim_ch = int(options.grid_a1_idim)
  1852. grid_dim_v = dataA[grid_dim_ch-1]
  1853. dim=255
  1854. try:
  1855. dim = int(grid_dim_v)
  1856. if dim > 255:
  1857. dim = 255
  1858. if dim <=0:
  1859. dim = 0
  1860. except:pass
  1861. dim_raw = dim*-1
  1862. s = pygame.Surface((p*8,p*8)) # the size of your rect
  1863. s.set_alpha(dim) # alpha level
  1864. s.fill((0,0,0)) # this fills the entire surface
  1865. window.blit(s, (CFG["x1"],CFG["y2"]-p*8))
  1866. fr = font15.render("Output:GRID_A1",1, (255,255,255))
  1867. fr_r = fr.get_rect(center=(int(wx/2),int(0+pm_wy-p*0-10)))
  1868. window.blit(fr,(CFG["x1"],CFG["y2"]+5))
  1869. fr = font15.render("inv-dim: "+str(dim_raw) ,1, (255,255,255))
  1870. fr_r = fr.get_rect(center=(int(wx/2),int(0+pm_wy-p*0-10)))
  1871. window.blit(fr,(CFG["x1"],CFG["y2"]+15))
  1872. # GRID_A2 DIM
  1873. if options.grid_a2_idim:
  1874. CFG=CFG_OUT2
  1875. grid_dim_ch = int(options.grid_a2_idim)
  1876. grid_dim_v = dataA[grid_dim_ch-1]
  1877. dim=255
  1878. try:
  1879. dim = int(grid_dim_v)
  1880. if dim > 255:
  1881. dim = 255
  1882. if dim <=0:
  1883. dim = 0
  1884. except:pass
  1885. dim_raw = dim*-1
  1886. s = pygame.Surface((p*8,p*8)) # the size of your rect
  1887. s.set_alpha(dim) # alpha level
  1888. s.fill((0,0,0)) # this fills the entire surface
  1889. window.blit(s, (CFG["x1"],CFG["y2"]-p*8))
  1890. fr = font15.render("Output:GRID_A2",1, (255,255,255))
  1891. fr_r = fr.get_rect(center=(int(wx/2),int(0+pm_wy-p*0-10)))
  1892. window.blit(fr,(CFG["x1"],CFG["y2"]+5))
  1893. fr = font15.render("inv-dim: "+str(dim_raw) ,1, (255,255,255))
  1894. fr_r = fr.get_rect(center=(int(wx/2),int(0+pm_wy-p*0-10)))
  1895. window.blit(fr,(CFG["x1"],CFG["y2"]+15))
  1896. pygame.display.flip()
  1897. clock.tick(25)
  1898. if 'SDL_VIDEO_WINDOW_POS' in os.environ:
  1899. del os.environ['SDL_VIDEO_WINDOW_POS'] #= '%i,%i' % (200,164)
  1900. frame2 += 1
  1901. if __name__ == "__main__":
  1902. main()