vpu_live.py 63 KB

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