vpu_live.py 45 KB

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