vpu_live.py 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690
  1. import math
  2. import random
  3. import time
  4. import os
  5. import _thread as thread
  6. from optparse import OptionParser
  7. ...
  8. parser = OptionParser()
  9. parser.add_option("-m", "--mode", dest="mode",
  10. help="pixel mode pix,x,y --mode 40,10,8") #, metavar="FILE")
  11. parser.add_option("-X", "--XX", dest="XX", #default=1,
  12. help="x-split") #, metavar="FILE")
  13. parser.add_option("-x", "--xx", dest="xsplit", #default=1,
  14. help="x-split") #, metavar="FILE")
  15. parser.add_option("-y", "--yy", dest="ysplit",#default=1,
  16. help="y-split") #, metavar="FILE")
  17. parser.add_option("", "--start-univ", dest="start_univ",#default=1,
  18. help="set start-univers default=2") #, metavar="FILE")
  19. parser.add_option("", "--gobo-ch", dest="gobo_ch",#default=1,
  20. help="gobo ch univ on 1") #, metavar="FILE")
  21. #os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (200,164)
  22. parser.add_option("", "--win-pos", dest="win_pos",default="200,164",
  23. help="SDL_VIDEO_WINDOW_POS --win-pos=200,164") #, metavar="FILE")
  24. parser.add_option("", "--pixel-mapping", dest="pixel_mapping",default=0,
  25. help="pixel_mapping file/on --pixel-mapping=_x") #, metavar="FILE")
  26. parser.add_option("", "--countdown", dest="countdown",#default=1,
  27. help="enable countdown") #, metavar="FILE")
  28. parser.add_option("", "--videoplayer", dest="videoplayer",#default=1,
  29. help="enable videoplayer") #, metavar="FILE")
  30. #parser.add_option("-f", "--file", dest="filename",
  31. # help="write report to FILE", metavar="FILE")
  32. #parser.add_option("-q", "--quiet",
  33. # action="store_false", dest="verbose", default=True,
  34. # help="don't print status messages to stdout")
  35. (options, args) = parser.parse_args()
  36. for o in dir(options):
  37. if "_" in o:
  38. continue
  39. print(o,options.__getattribute__(o))
  40. START = time.time()
  41. # ===== ARTNET DMX =========
  42. import memcache
  43. mc = memcache.Client(['127.0.0.1:11211'], debug=0)
  44. def read_index():
  45. ips=mc.get("index")#cmd)
  46. if ips is None:
  47. ips = {}
  48. #for k,v in ips.items():
  49. # print(k,v)
  50. return ips
  51. def select_ip(ips, univ=2): # artnet univ
  52. _univ = ":{}".format(univ)
  53. for ip in ips: #high priority
  54. if "2.0.0" in ip and _univ in ip:
  55. return ip
  56. for ip in ips:
  57. if "ltp-out" in ip and _univ in ip:
  58. return ip
  59. FUNC = 0
  60. COUNTER = []
  61. if options.countdown:
  62. cdmx_start = options.countdown.split(",")
  63. for cdmx in cdmx_start:
  64. try:
  65. cdmx = int(cdmx)
  66. COUNTER.append({"DMX":cdmx,"DIM":0,"PAN":127,"TILT":127,"CONTROL":0,"SEC":10,"RED":255,"GREEN":255,"BLUE":255,"_time":time.time(),"_RUN":0,"_SEC":">{}<".format(cdmx)})
  67. except Exception as e:
  68. print("EXCEPTION COUNTER INIT ",cdmx)
  69. def read_dmx(ip):
  70. global frame
  71. r = ""
  72. if ip:
  73. #t = int(math.sin(time.time() - s)*10)
  74. r = mc.get(ip) #"2.0.0.13:2")
  75. frame += 1
  76. rr = [0]*512
  77. for i,v in enumerate(r):
  78. try: #cleanup ltp-out to int
  79. v = int(v)
  80. rr[i] = v
  81. except:pass
  82. r = rr
  83. if not r:
  84. c = 0
  85. #time.sleep(0.1)
  86. r = [0] *512
  87. for i in range(12*8+1):
  88. dmx = i*4
  89. #print(dmx)
  90. r[dmx:dmx+4] = [255,10,10,40]
  91. return r
  92. # ===== ARTNET DMX =========
  93. PLAYLIST = []
  94. try:
  95. PLAYLIST = open_playlist()
  96. except:pass
  97. class Vopen():
  98. def __init__(self,dmx=None):
  99. global PLAYLIST
  100. self.fpath = '/home/user/Downloads/'
  101. self.fpath = '/home/user/LibreLight/video/'
  102. self.fname = 'bbb_sunflower_480x320.mp4'
  103. #self.fname = 'no-video.mp4'
  104. try:
  105. self.fname = PLAYLIST[0]
  106. except Exception as e:
  107. print("Exception set video from PLAYLIST 5543",e)
  108. self.time = 0
  109. self.t_delta = 0
  110. self.t_last = time.time()
  111. self.fps = 0
  112. self.scale = 50 #%
  113. self.dmx=dmx
  114. self.x = 0
  115. self.y = 0
  116. self.init_count = 0
  117. self.cap = None
  118. self.shape = [200,200]
  119. self.img = None
  120. self.success = 1
  121. self.cv2 = None
  122. self._run = 0
  123. try:
  124. global cv2
  125. self.cv2 = cv2
  126. except:
  127. pass
  128. self.im = None
  129. self.pos = 0
  130. self.buffer = []
  131. self.init()
  132. def init(self):
  133. #self._init()
  134. thread.start_new_thread(self._init,())
  135. self.init_count = 1
  136. def _init(self):
  137. print(self)
  138. print("videoplayer.init()",self.fpath,self.fname)
  139. if not os.path.isfile(self.fpath+self.fname):
  140. print()
  141. print("video file does not exits !! >",self.fpath,self.fname)
  142. print()
  143. exit()
  144. if self.cv2:
  145. cap = self.cv2.VideoCapture(self.fpath+self.fname)
  146. self.cap = cap
  147. success = 1
  148. while success:
  149. try:
  150. success, self.img = self.cap.read()
  151. if self.fps == 0:
  152. self.fps = self.cap.get(cv2.CAP_PROP_FPS)
  153. #print("fps",self.fps)
  154. #self.img = self.cv2.cvtColor(self.img, self.cv2.COLOR_BGR2RGB)
  155. self.img = self.cv2.cvtColor(self.img, self.cv2.COLOR_BGR2RGB)
  156. self.img = self.rescale_frame2(self.img, 100)
  157. self.buffer.append(self.img)
  158. if len(self.buffer) % 100 == 0:
  159. _id = str(self.__repr__)[-5:-1]
  160. print(_id,"video read",self.dmx,len(self.buffer),self.fname,"fps",self.fps)
  161. time.sleep(0.2)
  162. time.sleep(0.005)
  163. except Exception as e:
  164. print("Excetpion","_init",self,e)
  165. self.pos = 0
  166. self.img = self.buffer[int(self.pos)]
  167. self.success = 1
  168. def read(self):
  169. #print(self,"read()")
  170. if len(self.buffer) <= 0:
  171. return
  172. try:
  173. if self.pos >= len(self.buffer):
  174. self.pos = 0 #len(self.buffer)-1
  175. self.img = self.buffer[int(self.pos)]
  176. #self.img = self.cv2.cvtColor(self.img, self.cv2.COLOR_BGR2RGB)
  177. self.img = self.rescale_frame(self.img, percent=self.scale)
  178. if self._run:
  179. t = time.time()
  180. self.t_delta = t-self.t_last
  181. self.t_last = t
  182. self.pos += self.t_delta*self.fps
  183. else:
  184. t = time.time()
  185. self.t_delta = 0
  186. self.t_last = t
  187. if self.pos >= len(self.buffer):
  188. self.pos = 0 #len(self.buffer)-1
  189. #print("video.read",self.pos)
  190. self.shape = self.img.shape[1::-1]
  191. except Exception as e:
  192. print("exception 432",e,len(self.buffer),self.fname)
  193. #self.init()
  194. def prev(self):
  195. self.pos -= 1
  196. if self.pos < 0:
  197. self.pos = len(self.buffer)-1
  198. if self.pos >= len(self.buffer):
  199. self.pos = len(self.buffer)-1
  200. self.im = self.buffer[int(self.pos)]
  201. def rescale_frame2(self,frame, width):
  202. height = int(frame.shape[0]/frame.shape[1] * width )
  203. dim = (width, height)
  204. #print("rescale_frame2",dim)
  205. #sys.exit()
  206. return self.cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)
  207. def rescale_frame(self,frame, percent=75):
  208. width = int(frame.shape[1] * percent/ 100)
  209. height = int(frame.shape[0] * percent/ 100)
  210. dim = (width, height)
  211. return self.cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)
  212. def next(self):
  213. #print(self,"next",time.time())
  214. #self.success = 0
  215. self.read()
  216. try:
  217. if len(self.buffer) % 100 == 0:
  218. #print("video pos",self.pos)
  219. _id = str(self.__repr__)[-5:-1]
  220. print(_id,"video pos",self.dmx,self.pos,len(self.buffer),self.fname)
  221. img = self.img #self.rescale_frame(self.img, percent=30)
  222. if img is None:
  223. return
  224. self.im = pygame.image.frombuffer(img.tobytes(), self.shape, "RGB")
  225. #print(type(self.im))
  226. #self.buffer.append(self.im)
  227. #self.pos = int(self.time * self.fps)
  228. if self._run:
  229. t = time.time()
  230. self.t_delta = t-self.t_last
  231. self.t_last = t
  232. self.pos += self.t_delta*self.fps
  233. else:
  234. t = time.time()
  235. self.t_delta = 0
  236. self.t_last = t
  237. #if self._run:
  238. # self.pos += 2
  239. if self.pos > len(self.buffer):
  240. self.pos = 0
  241. # wn.blit(im, (self.x, self.y))
  242. #self.success = 1
  243. except AttributeError as e:
  244. time.sleep(.05)
  245. #if self.init_count % 100 == 0:
  246. print("except 776",e)
  247. #self.init()
  248. except Exception as e:
  249. print("except 756",e)
  250. def draw(self,wn=None):
  251. if self.success and wn and self.im: # is not None:
  252. wn.blit(self.im, (self.x, self.y))
  253. def overlay(self,wn=None,mode="x"):
  254. # overlay
  255. img_shape = [100,100]
  256. if type(self.img) is list:
  257. img_shape = self.img.shape
  258. pygame.draw.rect(wn,[255,200,0],[5+self.x-3,4+self.y-1+img_shape[0],140,20])
  259. font15 = pygame.font.SysFont("freemonobold",17)
  260. fr = font15.render(">:{}".format(mode) ,1, (0,0,0))
  261. wn.blit(fr,(3+self.x,4+self.y+img_shape[0]))
  262. fr = font15.render("FRAME:{}".format(self.pos) ,1, (0,0,0))
  263. wn.blit(fr,(45+self.x,4+self.y+img_shape[0]))
  264. VIDEO = []
  265. videoplayer=[]
  266. cv2 = None
  267. if type(options.videoplayer) is str:
  268. try:
  269. import cv2
  270. except:pass
  271. max_videoplayer = 2
  272. dmx_start = options.videoplayer.split(",")
  273. for cdmx in dmx_start:
  274. if len(videoplayer) > max_videoplayer:
  275. print("-- videoplayer max count {} !! break".format(max_videoplayer))
  276. break
  277. print("-- videoplayer dmx:",cdmx)
  278. try:
  279. cdmx = int(cdmx)
  280. videoplayer.append( Vopen(cdmx) )
  281. VIDEO.append({"DMX":cdmx,"DIM":0,"PAN":127,"TILT":127,"CONTROL":0,"SEC":10,"RED":255,"GREEN":255,"BLUE":255,"_time":time.time(),"_RUN":0,"_SEC":">{}<".format(cdmx)})
  282. except Exception as e:
  283. print("EXCEPTION COUNTER INIT ",cdmx)
  284. # ===== ======
  285. p = 16
  286. block = [p,p]
  287. _x = 8
  288. _y = 8
  289. #HD = "0"
  290. if options.mode:
  291. try:
  292. HD = options.mode
  293. p,_x,_y = HD.split(",")
  294. _x = int(_x)
  295. _y = int(_y)
  296. p = int(p)
  297. block = [p,p]
  298. except Exception as e:
  299. print( "Exc",options.mode,e)
  300. HD_x = 2
  301. HD_y = 2
  302. print( [options.xsplit])
  303. print( [options.ysplit])
  304. try:
  305. if options.xsplit:
  306. HD_x = int(options.xsplit)
  307. if options.ysplit:
  308. HD_y = int(options.ysplit)
  309. except Exception as e:
  310. print( "Exc",options.mode,e)
  311. print("HD",HD_x,HD_y)
  312. print("xy",_x,_y)
  313. print("++++++++++++++++++", p,_x,_y)
  314. _x2 = _x
  315. try:
  316. if options.XX:
  317. pass#_x2 = int(options.XX)
  318. except Exception as e:
  319. print( "Exc",options.mode,e)
  320. print("_x2 , -X",_x2)
  321. # ===== GUI =========
  322. import pygame
  323. import pygame.gfxdraw
  324. import pygame.font
  325. clock = pygame.time.Clock()
  326. os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (200,164)
  327. if options.win_pos:
  328. if "," in options.win_pos:
  329. win_pos = options.win_pos.split(",")
  330. try:
  331. WIN_POS = '%i,%i' % (int(win_pos[0]),int(win_pos[1]) )
  332. os.environ['SDL_VIDEO_WINDOW_POS'] = WIN_POS
  333. except Excetpion as e:
  334. print("win_pos",win_pos,e)
  335. os.environ['SDL_VIDEO_CENTERED'] = '0'
  336. pg = pygame
  337. pygame.init()
  338. pygame.mixer.quit()
  339. f = pygame.font.get_fonts()
  340. for i in f:
  341. if "mono" in i.lower():
  342. print(i)
  343. font = pygame.font.SysFont("freemonobold",22)
  344. font10 = pygame.font.SysFont("freemonobold",10)
  345. font12 = pygame.font.SysFont("freemonobold",12)
  346. font15 = pygame.font.SysFont("freemonobold",15)
  347. font40 = pygame.font.SysFont("freemonobold",40)
  348. font80 = pygame.font.SysFont("freemonobold",70)
  349. #font = pygame.font.SysFont(None,30)
  350. fr = font.render("hallo" ,1, (200,0,255))
  351. PIXEL_MAPPING = 0
  352. grid_file = "/tmp/vpu_grid_hd.csv"
  353. text_file = "/home/user/LibreLight/vpu_text_hd.csv"
  354. play_list = "/tmp/vpu_playlist_hd.csv"
  355. pm_wy = 0
  356. if options.pixel_mapping:
  357. PIXEL_MAPPING = 1
  358. path = options.pixel_mapping
  359. path = path.replace("/","-")
  360. path = path.replace(".","-")
  361. path = path.replace("\"","-")
  362. path = path.replace("'","-")
  363. grid_file = "/home/user/LibreLight/vpu_grid_hd{}.csv".format(path)
  364. text_file = "/home/user/LibreLight/vpu_text_hd{}.csv".format(path)
  365. play_list = "/home/user/LibreLight/vpu_playlist_hd{}.csv".format(path)
  366. #_x = 8
  367. #_y = 8
  368. print(" ",[options.pixel_mapping],"grid_file",grid_file)
  369. #grid_file = "/home/user/LibreLight/vpu_grid_hd.csv"
  370. main_size=(600,500)
  371. try:
  372. if _x < 8 and PIXEL_MAPPING >= 1:
  373. wx = 60+block[0] * 8
  374. else:
  375. wx = 60+block[0] * _x
  376. wy = 80+block[1] * _y
  377. main_size=(wx,wy)
  378. if PIXEL_MAPPING >= 1:
  379. pm_wy = 120+block[0] * 8
  380. main_size=(wx,wy+pm_wy)
  381. except Exception as e:
  382. print("Exception:",e)
  383. #main_size=(280,200)
  384. main_size = (main_size[0],main_size[1])
  385. window = pygame.display.set_mode(main_size,pg.RESIZABLE)#,32)#,pygame.FULLSCREEN) #x left->right ,y top-> bottom
  386. pg.display.set_caption('LibreLight VPU-SCREEN')
  387. class Fix():
  388. def __init__(self,_id,pos,block=[16,16],univ=0,dmx=0,ch=4):
  389. #print("Fix",_id)
  390. self._id = _id
  391. self.dmx = (_id-1) * ch +1 #dmx
  392. self.univ = univ
  393. self.ch = ch
  394. self.pos = pos
  395. self.rgb = [0,0,0]
  396. self.block = block #[10,10]
  397. self.x = pos[0]
  398. self.y = pos[1]
  399. self.strobo = time.time()
  400. self.bmp = 250
  401. self.sub_fix = []
  402. sub_block =[block[0]/HD_x,block[1]/HD_y]
  403. if _id <= 0: #exit
  404. return
  405. spalte = (_id-1)%_y +1
  406. zeile = int((_id-1)/_x2) #+1
  407. #zeile = zeile*_x*HD_x*HD_y
  408. add_row = _x*HD_x*HD_y
  409. #zeile 1
  410. sid = (_id-1)*2 + zeile*HD_x*_x2
  411. #for i in range(1,HD_x):
  412. sid = sid+1
  413. #sid = zeile
  414. sub_pos= [pos[0]*block[0],pos[1]*block[1]]
  415. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  416. self.sub_fix.append(sub_fix)
  417. sid = sid+1
  418. #sid = zeile
  419. sub_pos= [pos[0]*block[0]+block[0]/2,pos[1]*block[1]]
  420. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  421. self.sub_fix.append(sub_fix)
  422. #zeile 2
  423. sid = (_id-1)*2+1 + _x2*HD_x + zeile*HD_x*_x2 # int(add_row)
  424. #sid = sid+1
  425. #sid = HD_x
  426. sub_pos= [pos[0]*block[0],pos[1]*block[1]+block[1]/2]
  427. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  428. self.sub_fix.append(sub_fix)
  429. #sid = sid+1
  430. sid = sid+1
  431. sub_pos= [pos[0]*block[0]+block[0]/2,pos[1]*block[1]+block[1]/2]
  432. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  433. self.sub_fix.append(sub_fix)
  434. def calc(self,data):
  435. _rgb = [0,255,0]
  436. return _rgb
  437. def sub_calc(self,data):
  438. _rgb = [0,255,0]
  439. for sub_fix in self.sub_fix:
  440. sub_fix.block = self.block[:]
  441. _rgb = sub_fix.calc(data)
  442. return _rgb
  443. def POS(self,x=0,y=0,a=0,b=0):
  444. A = (self.pos[0])*self.block[0]
  445. B = (self.pos[1])*self.block[1]
  446. C = self.block[0]-a
  447. D = self.block[1]-b
  448. return [x+A,y+B,C,D]
  449. def subPOS(self,x=0,y=0,a=0,b=0):
  450. __out = []
  451. for sub_fix in self.sub_fix:
  452. __out.append( sub_fix.POS(x,y,a,b) )
  453. return __out
  454. class SubFix():
  455. def __init__(self,_id,pos,block=[16,16],univ=0,dmx=0,ch=4):
  456. #print("Fix",_id)
  457. self._id = _id
  458. self.dmx = (_id-1) * ch +1 #dmx
  459. self.univ = univ
  460. self.ch = ch
  461. self.pos = pos
  462. self.rgb = [0,0,40]
  463. self.block = block #[10,10]
  464. self.x = pos[0]
  465. self.y = pos[1]
  466. self.strobo = time.time()
  467. self.bmp = 250
  468. def calc(self,data):
  469. #return [130,30,20]
  470. dmx_sub = [30]*10
  471. #print(dmx_sub)
  472. dmx = self.dmx -1
  473. _dmx_sub = []
  474. if self.dmx >= 0:
  475. dmx = rDMX(self.univ,self.dmx)-1
  476. if dmx+self.ch < len(data):
  477. _dmx_sub = data[dmx:dmx+self.ch]
  478. if _dmx_sub:
  479. dmx_sub = _dmx_sub
  480. #print(dmx_sub)
  481. dim = dmx_sub[0]/255
  482. #print("dmx",dmx,dmx_sub)
  483. r = dmx_sub[1]*dim
  484. g = dmx_sub[2]*dim
  485. b = dmx_sub[3]*dim
  486. r = int(r)
  487. g = int(g)
  488. b = int(b)
  489. self.rgb = [r,g,b]
  490. return self.rgb
  491. def POS(self,x=0,y=0,a=0,b=0):
  492. A = (self.pos[0]) #+self.block[0]
  493. B = (self.pos[1]) #+self.block[1]
  494. C = self.block[0]-a
  495. D = self.block[1]-b
  496. if NR:
  497. C-=1
  498. D-=1
  499. return [int(x+A),int(y+B),int(C),int(D)]
  500. class POINTER():
  501. def __init__(self):
  502. self.pos = [0,0,0,0]
  503. self.on = 0
  504. self.rgb = [0,100,10]
  505. self._x = 0
  506. self._y = 0
  507. self.x = 0
  508. self.y = 0
  509. self.fix = Fix(0 ,[999,999],[16,16],0,0,0)
  510. def row_move(self,x,y):
  511. self._x = x
  512. self._y = y
  513. def move(self,pos):
  514. self.pos = pos
  515. self.on = 1
  516. def cross(self,x,y):
  517. self.x = x
  518. self.y = y
  519. def draw(self,x,y):
  520. pos = self.pos[:]
  521. #print("draw",x,y,pos)
  522. pos[0] += x
  523. pos[1] += y
  524. fix_x= self.fix.x
  525. fix_y= self.fix.y +y
  526. #print("draw",x,y,pos)
  527. if self.on:
  528. pygame.draw.rect(window,self.rgb,pos)
  529. #pygame.draw.line(window,self.rgb, (pos[0],pos[1]) , (pos[0]+100,pos[1]) )
  530. # mouse grid posision
  531. fr = font15.render("{}/{}".format(fix_x+1,fix_y) ,1, (200,200,200))
  532. _nr = fix_y * _x + fix_x +1
  533. #fr = font15.render("{:02} {}/{}".format(_nr, fix_x+1,fix_y+1 ) ,1, (200,200,200))
  534. fr = font15.render("{:02}".format(_nr ) ,1, (200,200,200))
  535. window.blit(fr,(pos[0]+2,pos[1]+2 ))
  536. window.blit(fr,(130,1))
  537. # fix pos
  538. txt=str(pos) #"[0, 0, 0, 0]"
  539. fr = font15.render(txt ,1, (200,200,200))
  540. #window.blit(fr,(pos[0]+2,pos[1]+2 ))
  541. window.blit(fr,(10,1))
  542. # univers
  543. #fr = font15.render("{:02}:{:03}".format(fix.univ,fix.dmx) ,1, (200,200,200))
  544. #window.blit(fr,(300,10))
  545. # pointer
  546. fr = font15.render("X:{:03}".format(self._x) ,1, (200,200,200))
  547. window.blit(fr,(10,30))
  548. fr = font15.render("Y:{:03}".format(self._y) ,1, (200,200,200))
  549. window.blit(fr,(10,40))
  550. # crosshair
  551. self.rgb = [0,0,200]
  552. pygame.draw.line(window,self.rgb, (self.x-p,self.y) , (self.x-2,self.y),4 )
  553. pygame.draw.line(window,self.rgb, (self.x,self.y-p) , (self.x,self.y-2),4 )
  554. self.rgb = [0,200,0]
  555. pygame.draw.line(window,self.rgb, (self.x+2,self.y) , (self.x+p,self.y),4 )
  556. pygame.draw.line(window,self.rgb, (self.x,self.y+2) , (self.x,self.y+p),4 )
  557. self.rgb = [200,0,0]
  558. pointer = POINTER()
  559. NR = 0
  560. running = True
  561. def event():
  562. global NR,running
  563. for event in pygame.event.get():
  564. #print(event.dict)
  565. _button = None
  566. if "button" in event.dict:
  567. _button = event.dict["button"]
  568. _state = None
  569. if "state" in event.dict:
  570. _state = event.state
  571. _key = None
  572. if "key" in event.dict:
  573. _key = event.key
  574. _pos = None
  575. if "pos" in event.dict:
  576. _pos = event.pos
  577. _type = None
  578. if "type" in event.dict:
  579. _type = event.type
  580. _type = event.type
  581. _mod = None
  582. if "mod" in event.dict:
  583. _mod = event.mod
  584. if 0:
  585. print( " ")
  586. print( "{:.02f}".format( time.time() - START ))
  587. print("button -",_button,end="\t| ")
  588. #print("state -",_state)
  589. print("pos -",_pos)
  590. print("type -",_type, end="\t| ")
  591. print("key -",_key)
  592. print("mod -",_mod)
  593. try:
  594. if _type == 5:
  595. if _button == 1:
  596. NR += 1
  597. if NR > 1:
  598. NR = 0
  599. if _button == 3:
  600. NR -= 1
  601. if NR < 0:
  602. NR = 1
  603. if _pos:
  604. posA = _pos
  605. fix = find_pix(_pos[0]-40,_pos[1]-60+pm_wy)
  606. if fix:
  607. pos = fix.POS(40,60+pm_wy)
  608. rgb = [0,0,0]
  609. pointer.move(pos)
  610. pointer.fix = fix
  611. else:
  612. pointer.on = 0
  613. pointer.row_move(_pos[0],_pos[1])
  614. pointer.cross(_pos[0],_pos[1])
  615. if event.type == pygame.VIDEORESIZE:
  616. window = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)
  617. except Exception as e:
  618. print(e)
  619. if event.type==pygame.QUIT:
  620. running=False
  621. fps = 0
  622. frame = 0
  623. frame_t = time.time()
  624. IP = "yyy"
  625. def draw_overlay():
  626. global fps
  627. fr = font.render("FPS:{}".format(fps) ,1, (200,0,255))
  628. window.blit(fr,(10,10))
  629. fr = font.render("ip:{}".format(IP) ,1, (200,0,255))
  630. window.blit(fr,(80,10))
  631. def calc_fps():
  632. global fps,frame,frame_t
  633. t = time.time()
  634. if frame_t+1 < t:
  635. fps = frame #frame_t- t #frame
  636. frame = 1
  637. frame_t = time.time()
  638. TEXT_BLOCK = []
  639. def _create_text_block():
  640. print("======== CREATE NEW TEXT FILE !!",text_file)
  641. f = open(text_file,"w")
  642. for i in range(10):
  643. f.write("TEXT {}\n".format(i+1))
  644. f.close()
  645. def open_text_block():
  646. print("======== OPEN TEXT FILE !!",text_file)
  647. _lines = []
  648. try:
  649. f = open(text_file,"r")
  650. _lines = f.readlines()
  651. f.close()
  652. except FileNotFoundError as e:
  653. print("TEXT",e)
  654. _create_text_block()
  655. if len(_lines) <= 0:
  656. _create_text_block()
  657. lines = []
  658. for l in _lines:
  659. #print(">> ",l.strip())
  660. lines.append(l.strip())
  661. if len(lines) <= 10:
  662. for i in range(10-len(lines)):
  663. lines.append("LINE ERROR")
  664. return lines
  665. TEXT_BLOCK = open_text_block()
  666. TEXT_BLOCK_TIME = time.time()
  667. # video playlist
  668. PLAYLIST = []
  669. def _create_playlist():
  670. print("======== CREATE NEW PLAYLIST FILE !!",play_list)
  671. os.system("mkdir -p /home/user/LibreLight/video")
  672. f = open(play_list,"w")
  673. f.write("bbb_sunflower_480x320.mp4\n")
  674. for i in range(10-1):
  675. f.write("Video-file {}\n".format(i+1+1))
  676. f.close()
  677. def open_playlist():
  678. print("======== OPEN PLAYLIST FILE !!",play_list)
  679. _lines = []
  680. try:
  681. f = open(play_list,"r")
  682. _lines = f.readlines()
  683. f.close()
  684. except FileNotFoundError as e:
  685. print("TEXT",e)
  686. _create_playlist()
  687. if len(_lines) <= 0:
  688. _create_playlist()
  689. lines = []
  690. for l in _lines:
  691. print(">> ",l.strip())
  692. lines.append(l.strip())
  693. if len(lines) <= 10:
  694. for i in range(10-len(lines)):
  695. lines.append("LINE ERROR")
  696. return lines
  697. PLAYLIST_TIME = time.time()
  698. PLAYLIST = open_playlist()
  699. # ===== GUI =========
  700. #def draw_circle(surface, x, y, radius, color):
  701. def draw_circle(surface,color, pos, radius):
  702. x,y=pos
  703. pygame.gfxdraw.aacircle(surface, int(x), int(y), radius-1, color)
  704. pygame.gfxdraw.filled_circle(surface, int(x), int(y), radius-1, color)
  705. def rDMX(univ,dmx):
  706. return univ*512+dmx
  707. def generate_grid(mapping=0):
  708. _log = []
  709. #if PIXEL_MAPPING:
  710. # log = open(grid_file,"w")
  711. head = "i,univ,dmx,x,y,ch\n"
  712. head = "i,univ,dmx,ch\n"
  713. head = "univ,dmx,x,y,ch\n"
  714. head = "nr,id,info\n"
  715. print("csv:",head)
  716. #if PIXEL_MAPPING:
  717. # log.write(head)
  718. _log.append(head)
  719. dmx = 1-1
  720. ch = 4
  721. y=0
  722. x=0
  723. #for i in range((_y)*(_x)):
  724. for i in range((8)*(8)):
  725. #if x > _x and i%_x == 0:
  726. if x > 8 and i%8 == 0:
  727. #print("--> -->")
  728. x=0
  729. y+=1
  730. _univ = int(dmx/512)
  731. _dmx = dmx - (_univ)*512
  732. pos=[x,y]
  733. line="{},{},{},{},{},{}\n".format(i+1,_univ,_dmx+1,pos[0],pos[1],ch)
  734. line="{},{},{},{},{}\n".format(_univ,_dmx+1,x,y,ch)
  735. line="{},{},x\n".format(i+1,i+1)
  736. #print("wcsv:",[line])
  737. #if PIXEL_MAPPING:
  738. # log.write(line)
  739. _log.append(line)
  740. dmx += ch
  741. x+=1
  742. if mapping and PIXEL_MAPPING:
  743. print("CREATE NEW PIXELMAP FILE !!",grid_file)
  744. log = open(grid_file,"w")
  745. log.writelines(_log)
  746. log.close()
  747. return _log[:] #GRID
  748. def init_grid(mapping=0,_x=4,_y=4):
  749. if mapping and PIXEL_MAPPING:
  750. try:
  751. log = open(grid_file,"r")
  752. except:
  753. generate_grid(mapping=mapping)
  754. log = open(grid_file,"r")
  755. lines = log.readlines()
  756. else:
  757. lines = generate_grid()
  758. GRID = []
  759. y=0
  760. x=0
  761. print("CSV header",[lines[0]],[PIXEL_MAPPING])
  762. for i,line in enumerate(lines[1:]): #exclude first line
  763. #print("rcsv",[line])
  764. line = line.strip()
  765. line = line.split(",") # csv
  766. if i >= _x and i%_x == 0:
  767. x=0
  768. y+=1
  769. if y >= _y:
  770. break
  771. #i = int(line[0])
  772. _id = int(line[1])
  773. #univ = int(line[0])
  774. #dmx = int(line[1])
  775. #x = int(line[3])
  776. #y = int(line[4])
  777. #ch = int(line[4])
  778. pos = [x,y]
  779. f = Fix(_id,pos,block) #pos,univ,dmx,ch)
  780. #f.x = x
  781. #f.y = y
  782. #f.block = block
  783. GRID.append(f)
  784. x+=1
  785. #print("y, _y",y,_y)
  786. return GRID
  787. def find_pix(x,y):
  788. global GRID
  789. for fix in GRID:
  790. X = 0
  791. Y = 0
  792. pos = fix.POS()
  793. #pos = fix.POS()
  794. #pos = fix.POS(40,60-pm_wy)
  795. #pos = fix.POS(x,y+pm_wy)#40,60)
  796. if x > pos[0] and x < pos[0]+pos[2]:
  797. X = 1
  798. if y > pos[1] and y < pos[1]+pos[3]:
  799. Y = 1
  800. if X and Y:
  801. #print(pos,x,y)
  802. #print("find",X,Y)
  803. return fix
  804. GRID = []
  805. _GRID = []
  806. _GRID = init_grid(_x=8,_y=8,mapping=1) #init_gird()
  807. NR = 0
  808. START_UNIV=2
  809. if options.start_univ:
  810. try:
  811. START_UNIV=int(options.start_univ)
  812. except Exception as e:
  813. print("Exception START UNIV",e)
  814. gobo_ch=1
  815. if options.gobo_ch:
  816. try:
  817. gobo_ch=int(options.gobo_ch)
  818. except Exception as e:
  819. print("Exception gobo_ch",e)
  820. if gobo_ch <= 0:
  821. gobo_ch = 1
  822. def draw_box(pos1,pos2,color=[128,128,128],text=1):
  823. color = [200,0,0,127]
  824. if text:
  825. fr = font15.render("A" ,1, (200,200,200))
  826. window.blit(fr,pos1)
  827. fr = font15.render("B" ,1, (200,200,200))
  828. window.blit(fr,[pos2[0]-10,pos2[1]-10])
  829. # h unten
  830. _pos1 = [pos1[0],pos2[1]]
  831. _pos2 = [pos2[0],pos2[1]]
  832. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  833. color = [255,255,0,127]
  834. # h rechts
  835. _pos1 = [pos2[0],pos1[1]]
  836. _pos2 = [pos2[0],pos2[1]]
  837. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  838. color = [0,200,0,127]
  839. # h links
  840. _pos1 = [pos1[0],pos1[1]]
  841. _pos2 = [pos1[0],pos2[1]]
  842. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  843. color = [0,0,200,127]
  844. # h oben
  845. _pos1 = [pos1[0],pos1[1]]
  846. _pos2 = [pos2[0],pos1[1]]
  847. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  848. def grab(x=55,y=55,w=60,h=60):
  849. # usage
  850. # sub = grab()
  851. # window.blit(sub, (500,10))
  852. crop = None
  853. rect = pygame.Rect(x, y, w, h)
  854. try:
  855. sub = window.subsurface(rect)
  856. #pixArray = pygame.PixelArray(screen)
  857. crop = pygame.Surface((w,h))
  858. crop.blit(sub, (0,0))
  859. except ValueError as e:
  860. pass#print("exception",e,"line715")
  861. #except Exception as e:
  862. # print("exception",e,"line715")
  863. #print(rect)
  864. return crop
  865. def reshape(_x,_y):
  866. if PIXEL_MAPPING <= 0:
  867. return None
  868. global GRID
  869. global _GRID
  870. x = _x
  871. y = _y
  872. i = 0
  873. counter = 0
  874. z=0
  875. x_min = 99999
  876. x_max = 0
  877. y_min = 99999
  878. y_max = 0
  879. fr = font.render("OUTPUT MAP".format(t1.get()) ,1, (255,255,255))
  880. fr_r = fr.get_rect(center=(x+int(wx/2),y+pm_wy-5))
  881. #window.blit(fr,(x+int(wx/2),y+pm_wy))
  882. window.blit(fr,fr_r)
  883. fr = font.render("↑ ↑ ↑".format(t1.get()) ,1, (255,255,255))
  884. fr_r = fr.get_rect(center=(x+int(wx/2),y+pm_wy+15))
  885. #window.blit(fr,(x+int(wx/2),y+pm_wy))
  886. window.blit(fr,fr_r)
  887. fr = font.render("INPUT".format(t1.get()) ,1, (255,255,255))
  888. fr_r = fr.get_rect(center=(x+int(wx/2),y+pm_wy+35))
  889. #window.blit(fr,(x+int(wx/2),y+pm_wy))
  890. window.blit(fr,fr_r)
  891. j = 0
  892. for fix in _GRID:
  893. if j >= 8*8: # max output size
  894. break
  895. #if j >= _x*_y: # max input size
  896. # break
  897. j+=1
  898. ii = i
  899. #z= i # helping border offset
  900. pos = fix.POS(40,60)
  901. rgb = fix.rgb
  902. # green
  903. pygame.draw.rect(window,[0,40,0],pos)
  904. xposs = [] #None #pos[:]
  905. for fix2 in GRID:
  906. if fix._id == fix2._id:
  907. xposs.append( fix2.POS(40,60) )
  908. for xpos in xposs:
  909. sub = grab(xpos[0],xpos[1]+pm_wy,xpos[2],xpos[3])
  910. if sub:
  911. if 1:#j <= _x*_y: # max input size
  912. window.blit(sub, (x+pos[0]+z,y+pos[1]+z))
  913. else:
  914. # red
  915. pygame.draw.rect(window,[40,0,0],pos) #[x+pos[0]+2+z,y+pos[1]+2+z-pm_wy,12,9])
  916. if xpos[0] < x_min:
  917. x_min = xpos[0]
  918. if xpos[0] > x_max:
  919. x_max += xpos[2]
  920. if xpos[1] < y_min:
  921. y_min = xpos[1]
  922. if xpos[1] > x_max:
  923. y_max += xpos[3]
  924. # DRAW FIX NUMBER on TOP
  925. #apos = pos
  926. #argb = rgb
  927. apos = fix.POS(40,60+pm_wy)
  928. argb = fix.rgb
  929. # overwrite number overlay
  930. if NR:
  931. #pygame.draw.rect(window,[30,40,0],apos)
  932. pygame.draw.rect(window,[20,40,0],[x+apos[0]+2+z,y+apos[1]+2+z-pm_wy,12,9])
  933. if NR:# == 2:
  934. if fix._id != i+1:
  935. fr = font15.render("{:02}".format(fix._id) ,1, (255,255,0))
  936. window.blit(fr,(x+apos[0]+2+z,y+apos[1]+2+z-pm_wy))
  937. #print(fix._id,xposs,pos)
  938. else:
  939. fr = font15.render("{:02}".format(fix._id) ,1, (100,100,255))
  940. window.blit(fr,(x+apos[0]+2+z,y+apos[1]+2+z-pm_wy))
  941. i += 1
  942. #print("--#")
  943. # frame box
  944. #pygame.draw.box(window,[100,0,0],[x+x_min,y+x_min,x_max+x_min,y_min+y_max])
  945. pos1= [x+x_min,y+x_min]
  946. pos2= [x_max+x_min,y_min+y_max]
  947. #draw_box(pos1,pos2,text=0)
  948. class Timer():
  949. def __init__(self,start=120):
  950. self.start = start
  951. self.timer = self.start
  952. self.timer_t = time.time()
  953. def reset(self):
  954. self.timer = self.start
  955. def get(self):
  956. self.timer -= time.time()-self.timer_t
  957. self.timer_t = time.time()
  958. if self.timer <= 0:
  959. self.reset()
  960. return self.timer
  961. def reload_grid():
  962. print("==== reload_grid")
  963. global GRID
  964. global _GRID
  965. try:
  966. GRID = init_grid(_x=_x,_y=_y) #init_gird()
  967. _GRID = init_grid(_x=8,_y=8,mapping=1) #init_gird()
  968. except Exception as e:
  969. print("Except: grid re init",e)
  970. VPU_TEXT = []
  971. def load_vpu_text(nr=0):
  972. txt = "NONE"
  973. if not VPU_TEXT:
  974. pass #create VPU_TEXT_FILE
  975. if len(VPU_TEXT) > nr:
  976. txt = VPU_TEXT[0]
  977. return txt
  978. grid_counter = time.time()
  979. def draw_fix_nr(GRID):
  980. global grid_counter
  981. i=0
  982. y=0
  983. for fix in GRID:
  984. pos = fix.POS(40,60+pm_wy)
  985. rgb = fix.rgb
  986. if NR:
  987. pygame.draw.rect(window,[0,0,0],[pos[0]+2,pos[1]+2,12,9])
  988. if fix._id%_x-1 == 0: # line break border
  989. pygame.draw.line(window,[255,255,0],(pos[0],pos[1]+4),(pos[0],pos[1]+pos[3]-4),1)
  990. 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)
  991. if fix._id%_x == 0: # line break border
  992. 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)
  993. 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)
  994. if grid_counter +5 < time.time():
  995. grid_counter = time.time()
  996. reload_grid()
  997. if fix._id != i+1:
  998. fr = font15.render("{:02}".format(fix._id) ,1, (255,255,0))
  999. else:
  1000. fr = font15.render("{:02}".format(fix._id) ,1, (100,100,255))
  1001. window.blit(fr,(pos[0]+2,pos[1]+2))
  1002. i += 1
  1003. def draw_counter(COUNTER):
  1004. for count in COUNTER:
  1005. cpan = 0
  1006. ctilt = 0
  1007. cr=255
  1008. cg=255
  1009. cb=255
  1010. csize=10
  1011. cdim=0
  1012. k = "DIM"
  1013. if k in count:
  1014. cdim = int(count[k])
  1015. k = "RED"
  1016. if k in count:
  1017. cr = int(count[k])
  1018. k = "GREEN"
  1019. if k in count:
  1020. cg = int(count[k])
  1021. k = "BLUE"
  1022. if k in count:
  1023. cb = int(count[k])
  1024. k = "SIZE"
  1025. if k in count:
  1026. csize = int(count[k])
  1027. if csize < 5:
  1028. csize = 5
  1029. k = "PAN"
  1030. if k in count:
  1031. cpan = int(count[k])/255*(block[0] *(_x))
  1032. cpan = int(cpan)
  1033. k = "TILT"
  1034. if k in count:
  1035. ctilt = int(count[k])/255*(block[1] *(_y))
  1036. ctilt = int(ctilt)
  1037. ddim = cdim/255
  1038. if "DIM" in count and count["DIM"] > 0:
  1039. tmp_font = pygame.font.SysFont("freemonobold",int(block[0]/100*csize))
  1040. rgb =(int(cr*ddim),int(cg*ddim),int(cb*ddim),cdim)
  1041. _sec = count["_SEC"]
  1042. try:
  1043. _sec = int(count["_SEC"])
  1044. except:
  1045. pass
  1046. if type(_sec) is int:
  1047. #print(_sec)
  1048. if _sec <= 60:
  1049. fr = tmp_font.render("{:0}".format(_sec) ,1, rgb)
  1050. else:
  1051. _xx = time.strftime("%M:%S",time.localtime(_sec))
  1052. #print("_xx",_xx)
  1053. fr = tmp_font.render("{}".format(_xx) ,1, rgb)
  1054. else:
  1055. fr = tmp_font.render("{}".format((count["_SEC"])) ,1, rgb)
  1056. fr_r = fr.get_rect(center=(60+cpan-(block[0]),60+ctilt+pm_wy))
  1057. pygame.draw.rect(window,[0,0,0],fr_r)
  1058. window.blit(fr,fr_r)
  1059. def draw_video(VIDEO):
  1060. global videplayer
  1061. i = 0
  1062. for count in VIDEO:
  1063. cpan = 0
  1064. ctilt = 0
  1065. cr=255
  1066. cg=255
  1067. cb=255
  1068. csize=10
  1069. cdim=0
  1070. k = "DIM"
  1071. if k in count:
  1072. cdim = int(count[k])
  1073. k = "SIZE"
  1074. if k in count:
  1075. csize = int(count[k])
  1076. if csize < 5:
  1077. csize = 5
  1078. k = "PAN"
  1079. if k in count:
  1080. cpan = int(count[k])/255*(block[0] *(_x))
  1081. cpan = int(cpan)
  1082. k = "TILT"
  1083. if k in count:
  1084. ctilt = int(count[k])/255*(block[1] *(_y))
  1085. ctilt = int(ctilt)
  1086. video1 = videoplayer[i]
  1087. k = "_reset"
  1088. if k in count:
  1089. if count[k]:
  1090. count[k] = 0
  1091. video1.pos = 0
  1092. k = "_RUN"
  1093. if k in count:
  1094. video1._run = count[k]
  1095. video1.pos
  1096. video1.x=40+cpan
  1097. video1.y=60+pm_wy+ctilt
  1098. video1.scale = int((csize))
  1099. if cdim:
  1100. video1.next()
  1101. #video1.prev()
  1102. video1.draw(window) #,x=0,y=0)
  1103. # overlay
  1104. video1.overlay(window,"run")
  1105. i += 1
  1106. def counter_dmx(COUNTER,dataA):
  1107. for count in COUNTER:
  1108. cDMX=count["DMX"]-1
  1109. try:
  1110. count["DIM"] = dataA[cDMX]
  1111. count["PAN"] = dataA[cDMX+1]
  1112. count["TILT"] = dataA[cDMX+2]
  1113. count["CONTROL"] = dataA[cDMX+3]
  1114. if count["CONTROL"] >= 10 and count["CONTROL"] < 20:
  1115. count["_SEC"] = int(count["SEC"] - (time.time() - count["_time"]))
  1116. if count["CONTROL"] >= 20 and count["CONTROL"] < 30:
  1117. count["_RUN"] = 0
  1118. if count["CONTROL"] >= 30 and count["CONTROL"] < 40:
  1119. count["_RUN"] = 1
  1120. count["SIZE"] = dataA[cDMX+4]
  1121. count["SEC"] = dataA[cDMX+5]
  1122. if count["_RUN"]:
  1123. try:
  1124. count["_SEC"] = int(count["SEC"] - (time.time() - count["_time"]))
  1125. except Exception as e:
  1126. pass
  1127. if type(count["_SEC"]) is int:
  1128. if count["_SEC"] < 0:
  1129. count["_SEC"] = 0
  1130. for ti in range(10):
  1131. #print(ti,(ti+6)*10)
  1132. if count["CONTROL"] >= (ti+6)*10 and count["CONTROL"] < (ti+7)*10:
  1133. count["_SEC"] = "----" #text 1
  1134. try:
  1135. count["_SEC"] = TEXT_BLOCK[ti]
  1136. except Exception as e:
  1137. pass
  1138. if count["CONTROL"] >= 250 and count["CONTROL"] < 256:
  1139. count["_SEC"] = ">{}<".format(cDMX+1)
  1140. count["RED"] = dataA[cDMX+6]
  1141. count["GREEN"] = dataA[cDMX+7]
  1142. count["BLUE"] = dataA[cDMX+8]
  1143. except Exception as e:
  1144. print("EXC FUNC",e,count)
  1145. #print(count)
  1146. def video_dmx(VIDEO,dataA):
  1147. for count in VIDEO:
  1148. cDMX=count["DMX"]-1
  1149. try:
  1150. count["DIM"] = dataA[cDMX]
  1151. count["PAN"] = dataA[cDMX+1]
  1152. count["TILT"] = dataA[cDMX+2]
  1153. count["CONTROL"] = dataA[cDMX+3]
  1154. if count["CONTROL"] >= 10 and count["CONTROL"] < 20:
  1155. count["_reset"] = 1
  1156. if count["CONTROL"] >= 20 and count["CONTROL"] < 30:
  1157. if count["_RUN"] == 1:
  1158. print( "_RUN:0",count["DMX"]-1)
  1159. count["_RUN"] = 0
  1160. if count["CONTROL"] >= 30 and count["CONTROL"] < 40:
  1161. if count["_RUN"] == 0:
  1162. print( "_RUN:1",count["DMX"]-1)
  1163. count["_RUN"] = 1
  1164. count["SIZE"] = dataA[cDMX+4]
  1165. count["SEC"] = dataA[cDMX+5]
  1166. count["RED"] = dataA[cDMX+6]
  1167. count["GREEN"] = dataA[cDMX+7]
  1168. count["BLUE"] = dataA[cDMX+8]
  1169. except Exception as e:
  1170. print("VIDEOPLAYER EXCEPT FUNC",e,count)
  1171. #print(count)
  1172. def read_dmx_data(ip,ips):
  1173. ip = select_ip(ips,univ=START_UNIV)
  1174. IP = ip
  1175. data = read_dmx(ip)
  1176. ip = select_ip(ips,univ=START_UNIV+1)
  1177. data3 = read_dmx(ip)
  1178. data.extend(data3)
  1179. ip = select_ip(ips,univ=START_UNIV+2)
  1180. data3 = read_dmx(ip)
  1181. data.extend(data3)
  1182. ip = select_ip(ips,univ=START_UNIV+4)
  1183. data3 = read_dmx(ip)
  1184. data.extend(data3)
  1185. #ip = select_ip(ips,univ=START_UNIV+5)
  1186. #data3 = read_dmx(ip)
  1187. #data.extend(data3)
  1188. return data
  1189. def draw_gobo(GRID,data):
  1190. i = 0
  1191. dmx = 1
  1192. h = 1
  1193. v = 1
  1194. for fix in GRID:
  1195. pos = fix.POS(40,60+pm_wy)
  1196. rgb = fix.rgb
  1197. if 1:
  1198. # draw row/col grid number
  1199. if fix.pos[0] == 0:
  1200. fr = font12.render("{}".format(fix.pos[1]+1) ,1, (200,200,200))
  1201. window.blit(fr,(10,pos[1]+3 ))
  1202. if fix.pos[1] == 0:
  1203. fr = font12.render("{}".format(fix.pos[0]+1) ,1, (200,200,200))
  1204. window.blit(fr,(pos[0]+2,35 ))
  1205. pygame.draw.rect(window,rgb,pos)
  1206. # DRAW SUB-FIXTURE
  1207. j = 0
  1208. for subfix in fix.sub_fix:#calc(data):
  1209. subfix.calc(data)
  1210. #fix = subfix
  1211. spos = subfix.POS(40,60+pm_wy)
  1212. srgb = subfix.rgb
  1213. #print(fix.dmx,rgb,pos)
  1214. #pygame.draw.circle(window,rgb,(pos[0]+int(pos[2]/2),pos[1]+int(pos[3]/2)),int(pos[3]/2))
  1215. #FUNC = 0
  1216. if FUNC > 10 and FUNC <= 20: # big dot
  1217. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1218. elif FUNC > 20 and FUNC <= 30:#small dot
  1219. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1220. elif FUNC > 30 and FUNC <= 40:#donut
  1221. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1222. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1223. elif FUNC > 40 and FUNC <= 50: # rec with hole
  1224. pygame.draw.rect(window,srgb,spos)
  1225. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1226. elif FUNC > 50 and FUNC <= 60: # rec with big hole
  1227. pygame.draw.rect(window,srgb,spos)
  1228. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1229. elif FUNC > 60 and FUNC <= 70: # rec with donat
  1230. pygame.draw.rect(window,srgb,spos)
  1231. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1232. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1233. elif FUNC > 70 and FUNC <= 80: # rec boarder
  1234. pygame.draw.rect(window,srgb,[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  1235. elif FUNC > 80 and FUNC <= 90: # rec big boarder
  1236. pygame.draw.rect(window,srgb,[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  1237. elif FUNC > 90 and FUNC <= 100: # rec thin line
  1238. pygame.draw.rect(window,srgb,spos)
  1239. pygame.draw.rect(window,[0,0,0],[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  1240. elif FUNC > 100 and FUNC <= 110: # rec big line
  1241. pygame.draw.rect(window,srgb,spos)
  1242. pygame.draw.rect(window,[0,0,0],[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  1243. elif FUNC > 110 and FUNC <= 120: # rec with dot
  1244. pygame.draw.rect(window,srgb,spos)
  1245. pygame.draw.rect(window,[0,0,0],[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  1246. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1247. elif FUNC > 120 and FUNC <= 130: # rec inline
  1248. pygame.draw.rect(window,srgb,[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  1249. pygame.draw.rect(window,[0,0,0],[spos[0]+3,spos[1]+3,spos[2]-6,spos[3]-6])
  1250. elif FUNC > 130 and FUNC <= 140: # 3 dot (heart)
  1251. draw_circle(window,srgb,(spos[0]+int(spos[2]/2)+2,spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1252. draw_circle(window,srgb,(spos[0]+int(spos[2]/2)-2,spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1253. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)+2),int(spos[3]/3.5))
  1254. else:
  1255. pygame.draw.rect(window,srgb,spos)
  1256. for subfix in fix.sub_fix:#calc(data):
  1257. subfix.calc(data)
  1258. #fix = subfix
  1259. spos = subfix.POS(40,60+pm_wy)
  1260. srgb = subfix.rgb
  1261. # draw row/col grid number
  1262. if subfix.pos[0] == 0:
  1263. fr = font12.render("{}".format(v ) ,1, (200,200,200))
  1264. window.blit(fr,(25,spos[1] ))
  1265. v += 1
  1266. if subfix.pos[1] == 0:
  1267. fr = font12.render("{}".format(1) ,1, (200,200,200))
  1268. fr = font12.render("{}".format(h ) ,1, (200,200,200))
  1269. h+=1
  1270. window.blit(fr,(spos[0],50 ))
  1271. if p >= 40:
  1272. if NR:
  1273. #fr = font15.render("{:02}".format(j+1) ,1, (0,200,255))
  1274. fr = font15.render("{:02}".format(subfix._id) ,1, (250,200,5))
  1275. window.blit(fr,(spos[0]+2,spos[1]+10))
  1276. j += 1
  1277. i += 1
  1278. ips=[]
  1279. dataA=[]
  1280. data=[]
  1281. def dmx_raw():
  1282. global ips,dataA,data
  1283. ips = read_index()
  1284. # ----
  1285. ip = select_ip(ips,univ=1) # univ 1 gobo
  1286. dataA = read_dmx(ip)
  1287. # ----
  1288. data = read_dmx_data(ip,ips)
  1289. if options.countdown:
  1290. counter_dmx(COUNTER,dataA)
  1291. if VIDEO:
  1292. video_dmx(VIDEO,dataA)
  1293. return ips,dataA,data
  1294. def dmx_loop():
  1295. while 1:
  1296. dmx_raw()
  1297. time.sleep(0.1)
  1298. dmx_raw()
  1299. thread.start_new_thread(dmx_loop,())
  1300. t1 = Timer(143)
  1301. time.sleep(0.33)
  1302. t2 = Timer(11)
  1303. count_tilt = 0
  1304. def main():
  1305. global IP
  1306. global GRID
  1307. global _GRID
  1308. global FUNC
  1309. global count_tilt
  1310. global TEXT_BLOCK
  1311. global TEXT_BLOCK_TIME
  1312. global PLAYLIST
  1313. global PLAYLIST_TIME
  1314. global dataA
  1315. GRID = init_grid(_x=_x,_y=_y) #init_gird()
  1316. #GRID = init_grid(_x=8,_y=8) #init_gird()
  1317. print("GRID LEN:",len(GRID))
  1318. #thread.start_new_thread( video_dmx_loop,(VIDEO,dataA) )
  1319. s=time.time()
  1320. print("run")
  1321. r = ""
  1322. IP = "xx"
  1323. while running:
  1324. if TEXT_BLOCK_TIME+5 < time.time():
  1325. TEXT_BLOCK = open_text_block()
  1326. TEXT_BLOCK_TIME = time.time()
  1327. if PLAYLIST_TIME+6 < time.time():
  1328. PLAYLIST = open_playlist()
  1329. PLAYLIST_TIME = time.time()
  1330. pygame.display.flip()
  1331. event()
  1332. window.fill((10,0,20))
  1333. calc_fps()
  1334. draw_overlay()
  1335. # GRID loop
  1336. try:
  1337. ddd = 1023 #univ 3 512
  1338. #FUNC = data[ddd]
  1339. FUNC2 = dataA[gobo_ch-1]
  1340. FUNC = FUNC2
  1341. #print("FUNC", FUNC )#:ddd+512])
  1342. #FUNC = 15
  1343. except Exception as e:
  1344. print("EXC FUNC",e)
  1345. draw_gobo(GRID,data)
  1346. # DRAW FIX NUMBER on TOP
  1347. draw_fix_nr(GRID)
  1348. #COUNTER.append({"DMX":31,"DIM":0,"PAN":127,"TILT":127,"CONTROL":0,"SEC":10,"RED":255,"GREEN":255,"BLUE":255,"_time":time.time(),"_RUN":0,"_SEC":0})
  1349. if options.countdown:
  1350. draw_counter(COUNTER)
  1351. if VIDEO:
  1352. draw_video(VIDEO)
  1353. pointer.draw(0,pm_wy) #wy
  1354. spos = [0,0,0,0]
  1355. if PIXEL_MAPPING >= 1:
  1356. reshape(0,0) #start pos
  1357. else:
  1358. reshape(spos[0]+spos[2]+20,10) #start pos
  1359. #reshape(spos[0]+spos[2]+20,10) #start pos
  1360. pygame.display.flip()
  1361. #pg.time.wait(15)
  1362. clock.tick(10)
  1363. if 'SDL_VIDEO_WINDOW_POS' in os.environ:
  1364. del os.environ['SDL_VIDEO_WINDOW_POS'] #= '%i,%i' % (200,164)
  1365. #if 'SDL_VIDEO_CENTERED' in os.environ['SDL_VIDEO_CENTERED']:
  1366. # del os.environ['SDL_VIDEO_CENTERED'] #= '0'
  1367. if __name__ == "__main__":
  1368. main()