vpu_live.py 64 KB

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