vpu_live.py 66 KB

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