vpu_live4.py 77 KB

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