vpu_live.py 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  1. import math
  2. import random
  3. import time
  4. import os
  5. from optparse import OptionParser
  6. ...
  7. parser = OptionParser()
  8. parser.add_option("-m", "--mode", dest="mode",
  9. help="pixel mode pix,x,y --mode 40,10,8") #, metavar="FILE")
  10. parser.add_option("-X", "--XX", dest="XX", #default=1,
  11. help="x-split") #, metavar="FILE")
  12. parser.add_option("-x", "--xx", dest="xsplit", #default=1,
  13. help="x-split") #, metavar="FILE")
  14. parser.add_option("-y", "--yy", dest="ysplit",#default=1,
  15. help="y-split") #, metavar="FILE")
  16. parser.add_option("", "--start-univ", dest="start_univ",#default=1,
  17. help="set start-univers default=2") #, metavar="FILE")
  18. parser.add_option("", "--gobo-ch", dest="gobo_ch",#default=1,
  19. help="gobo ch univ on 1") #, metavar="FILE")
  20. #os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (200,164)
  21. parser.add_option("", "--win-pos", dest="win_pos",default="200,164",
  22. help="SDL_VIDEO_WINDOW_POS --win-pos=200,164") #, metavar="FILE")
  23. parser.add_option("", "--pixel-mapping", dest="pixel_mapping",default=0,
  24. help="pixel_mapping file/on --pixel-mapping=_x") #, metavar="FILE")
  25. parser.add_option("", "--countdown", dest="countdown",#default=1,
  26. help="enable countdown") #, metavar="FILE")
  27. #parser.add_option("-f", "--file", dest="filename",
  28. # help="write report to FILE", metavar="FILE")
  29. #parser.add_option("-q", "--quiet",
  30. # action="store_false", dest="verbose", default=True,
  31. # help="don't print status messages to stdout")
  32. (options, args) = parser.parse_args()
  33. START = time.time()
  34. # ===== ARTNET DMX =========
  35. import memcache
  36. mc = memcache.Client(['127.0.0.1:11211'], debug=0)
  37. def read_index():
  38. ips=mc.get("index")#cmd)
  39. if ips is None:
  40. ips = {}
  41. #for k,v in ips.items():
  42. # print(k,v)
  43. return ips
  44. def select_ip(ips, univ=2): # artnet univ
  45. _univ = ":{}".format(univ)
  46. for ip in ips: #high priority
  47. if "2.0.0" in ip and _univ in ip:
  48. return ip
  49. for ip in ips:
  50. if "ltp-out" in ip and _univ in ip:
  51. return ip
  52. FUNC = 0
  53. COUNTER = []
  54. if options.countdown:
  55. cdmx_start = options.countdown.split(",")
  56. for cdmx in cdmx_start:
  57. try:
  58. cdmx = int(cdmx)
  59. 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)})
  60. except Exception as e:
  61. print("EXCEPTION COUNTER INIT ",cdmx)
  62. def read_dmx(ip):
  63. global frame
  64. r = ""
  65. if ip:
  66. #t = int(math.sin(time.time() - s)*10)
  67. r = mc.get(ip) #"2.0.0.13:2")
  68. frame += 1
  69. rr = [0]*512
  70. for i,v in enumerate(r):
  71. try: #cleanup ltp-out to int
  72. v = int(v)
  73. rr[i] = v
  74. except:pass
  75. r = rr
  76. if not r:
  77. c = 0
  78. #time.sleep(0.1)
  79. r = [0] *512
  80. for i in range(12*8+1):
  81. dmx = i*4
  82. #print(dmx)
  83. r[dmx:dmx+4] = [255,10,10,40]
  84. return r
  85. # ===== ARTNET DMX =========
  86. p = 16
  87. block = [p,p]
  88. _x = 8
  89. _y = 8
  90. #HD = "0"
  91. if options.mode:
  92. try:
  93. HD = options.mode
  94. p,_x,_y = HD.split(",")
  95. _x = int(_x)
  96. _y = int(_y)
  97. p = int(p)
  98. block = [p,p]
  99. except Exception as e:
  100. print( "Exc",options.mode,e)
  101. HD_x = 2
  102. HD_y = 2
  103. print( [options.xsplit])
  104. print( [options.ysplit])
  105. try:
  106. if options.xsplit:
  107. HD_x = int(options.xsplit)
  108. if options.ysplit:
  109. HD_y = int(options.ysplit)
  110. except Exception as e:
  111. print( "Exc",options.mode,e)
  112. print("HD",HD_x,HD_y)
  113. print("xy",_x,_y)
  114. print("++++++++++++++++++", p,_x,_y)
  115. _x2 = _x
  116. try:
  117. if options.XX:
  118. pass#_x2 = int(options.XX)
  119. except Exception as e:
  120. print( "Exc",options.mode,e)
  121. print("_x2 , -X",_x2)
  122. # ===== GUI =========
  123. import pygame
  124. import pygame.gfxdraw
  125. import pygame.font
  126. os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (200,164)
  127. if options.win_pos:
  128. if "," in options.win_pos:
  129. win_pos = options.win_pos.split(",")
  130. try:
  131. WIN_POS = '%i,%i' % (int(win_pos[0]),int(win_pos[1]) )
  132. os.environ['SDL_VIDEO_WINDOW_POS'] = WIN_POS
  133. except Excetpion as e:
  134. print("win_pos",win_pos,e)
  135. os.environ['SDL_VIDEO_CENTERED'] = '0'
  136. pg = pygame
  137. pygame.init()
  138. pygame.mixer.quit()
  139. f = pygame.font.get_fonts()
  140. for i in f:
  141. if "mono" in i.lower():
  142. print(i)
  143. font = pygame.font.SysFont("freemonobold",22)
  144. font10 = pygame.font.SysFont("freemonobold",10)
  145. font12 = pygame.font.SysFont("freemonobold",12)
  146. font15 = pygame.font.SysFont("freemonobold",15)
  147. font40 = pygame.font.SysFont("freemonobold",40)
  148. font80 = pygame.font.SysFont("freemonobold",70)
  149. #font = pygame.font.SysFont(None,30)
  150. fr = font.render("hallo" ,1, (200,0,255))
  151. PIXEL_MAPPING = 0
  152. grid_file = "/tmp/vpu_grid_hd.csv"
  153. pm_wy = 0
  154. if options.pixel_mapping:
  155. PIXEL_MAPPING = 1
  156. path = options.pixel_mapping
  157. path = path.replace("/","-")
  158. path = path.replace(".","-")
  159. path = path.replace("\"","-")
  160. path = path.replace("'","-")
  161. grid_file = "/home/user/LibreLight/vpu_grid_hd{}.csv".format(path)
  162. #_x = 8
  163. #_y = 8
  164. print(" ",[options.pixel_mapping],"grid_file",grid_file)
  165. #grid_file = "/home/user/LibreLight/vpu_grid_hd.csv"
  166. main_size=(600,500)
  167. try:
  168. if _x < 8 and PIXEL_MAPPING >= 1:
  169. wx = 60+block[0] * 8
  170. else:
  171. wx = 60+block[0] * _x
  172. wy = 80+block[1] * _y
  173. main_size=(wx,wy)
  174. if PIXEL_MAPPING >= 1:
  175. pm_wy = 120+block[0] * 8
  176. main_size=(wx,wy+pm_wy)
  177. except Exception as e:
  178. print("Exception:",e)
  179. #main_size=(280,200)
  180. main_size = (main_size[0],main_size[1])
  181. window = pygame.display.set_mode(main_size,pg.RESIZABLE)#,32)#,pygame.FULLSCREEN) #x left->right ,y top-> bottom
  182. pg.display.set_caption('LibreLight VPU-SCREEN')
  183. class Fix():
  184. def __init__(self,_id,pos,block=[16,16],univ=0,dmx=0,ch=4):
  185. #print("Fix",_id)
  186. self._id = _id
  187. self.dmx = (_id-1) * ch +1 #dmx
  188. self.univ = univ
  189. self.ch = ch
  190. self.pos = pos
  191. self.rgb = [0,0,0]
  192. self.block = block #[10,10]
  193. self.x = pos[0]
  194. self.y = pos[1]
  195. self.strobo = time.time()
  196. self.bmp = 250
  197. self.sub_fix = []
  198. sub_block =[block[0]/HD_x,block[1]/HD_y]
  199. if _id <= 0: #exit
  200. return
  201. spalte = (_id-1)%_y +1
  202. zeile = int((_id-1)/_x2) #+1
  203. #zeile = zeile*_x*HD_x*HD_y
  204. add_row = _x*HD_x*HD_y
  205. #zeile 1
  206. sid = (_id-1)*2 + zeile*HD_x*_x2
  207. #for i in range(1,HD_x):
  208. sid = sid+1
  209. #sid = zeile
  210. sub_pos= [pos[0]*block[0],pos[1]*block[1]]
  211. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  212. self.sub_fix.append(sub_fix)
  213. sid = sid+1
  214. #sid = zeile
  215. sub_pos= [pos[0]*block[0]+block[0]/2,pos[1]*block[1]]
  216. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  217. self.sub_fix.append(sub_fix)
  218. #zeile 2
  219. sid = (_id-1)*2+1 + _x2*HD_x + zeile*HD_x*_x2 # int(add_row)
  220. #sid = sid+1
  221. #sid = HD_x
  222. sub_pos= [pos[0]*block[0],pos[1]*block[1]+block[1]/2]
  223. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  224. self.sub_fix.append(sub_fix)
  225. #sid = sid+1
  226. sid = sid+1
  227. sub_pos= [pos[0]*block[0]+block[0]/2,pos[1]*block[1]+block[1]/2]
  228. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  229. self.sub_fix.append(sub_fix)
  230. def calc(self,data):
  231. _rgb = [0,255,0]
  232. return _rgb
  233. def sub_calc(self,data):
  234. _rgb = [0,255,0]
  235. for sub_fix in self.sub_fix:
  236. sub_fix.block = self.block[:]
  237. _rgb = sub_fix.calc(data)
  238. return _rgb
  239. def POS(self,x=0,y=0,a=0,b=0):
  240. A = (self.pos[0])*self.block[0]
  241. B = (self.pos[1])*self.block[1]
  242. C = self.block[0]-a
  243. D = self.block[1]-b
  244. return [x+A,y+B,C,D]
  245. def subPOS(self,x=0,y=0,a=0,b=0):
  246. __out = []
  247. for sub_fix in self.sub_fix:
  248. __out.append( sub_fix.POS(x,y,a,b) )
  249. return __out
  250. class SubFix():
  251. def __init__(self,_id,pos,block=[16,16],univ=0,dmx=0,ch=4):
  252. #print("Fix",_id)
  253. self._id = _id
  254. self.dmx = (_id-1) * ch +1 #dmx
  255. self.univ = univ
  256. self.ch = ch
  257. self.pos = pos
  258. self.rgb = [0,0,40]
  259. self.block = block #[10,10]
  260. self.x = pos[0]
  261. self.y = pos[1]
  262. self.strobo = time.time()
  263. self.bmp = 250
  264. def calc(self,data):
  265. #return [130,30,20]
  266. dmx_sub = [30]*10
  267. #print(dmx_sub)
  268. dmx = self.dmx -1
  269. _dmx_sub = []
  270. if self.dmx >= 0:
  271. dmx = rDMX(self.univ,self.dmx)-1
  272. if dmx+self.ch < len(data):
  273. _dmx_sub = data[dmx:dmx+self.ch]
  274. if _dmx_sub:
  275. dmx_sub = _dmx_sub
  276. #print(dmx_sub)
  277. dim = dmx_sub[0]/255
  278. #print("dmx",dmx,dmx_sub)
  279. r = dmx_sub[1]*dim
  280. g = dmx_sub[2]*dim
  281. b = dmx_sub[3]*dim
  282. r = int(r)
  283. g = int(g)
  284. b = int(b)
  285. self.rgb = [r,g,b]
  286. return self.rgb
  287. def POS(self,x=0,y=0,a=0,b=0):
  288. A = (self.pos[0]) #+self.block[0]
  289. B = (self.pos[1]) #+self.block[1]
  290. C = self.block[0]-a
  291. D = self.block[1]-b
  292. if NR:
  293. C-=1
  294. D-=1
  295. return [int(x+A),int(y+B),int(C),int(D)]
  296. class POINTER():
  297. def __init__(self):
  298. self.pos = [0,0,0,0]
  299. self.on = 0
  300. self.rgb = [0,100,10]
  301. self._x = 0
  302. self._y = 0
  303. self.x = 0
  304. self.y = 0
  305. self.fix = Fix(0 ,[999,999],[16,16],0,0,0)
  306. def row_move(self,x,y):
  307. self._x = x
  308. self._y = y
  309. def move(self,pos):
  310. self.pos = pos
  311. self.on = 1
  312. def cross(self,x,y):
  313. self.x = x
  314. self.y = y
  315. def draw(self,x,y):
  316. pos = self.pos[:]
  317. #print("draw",x,y,pos)
  318. pos[0] += x
  319. pos[1] += y
  320. fix_x= self.fix.x
  321. fix_y= self.fix.y +y
  322. #print("draw",x,y,pos)
  323. if self.on:
  324. pygame.draw.rect(window,self.rgb,pos)
  325. #pygame.draw.line(window,self.rgb, (pos[0],pos[1]) , (pos[0]+100,pos[1]) )
  326. # mouse grid posision
  327. fr = font15.render("{}/{}".format(fix_x+1,fix_y) ,1, (200,200,200))
  328. _nr = fix_y * _x + fix_x +1
  329. #fr = font15.render("{:02} {}/{}".format(_nr, fix_x+1,fix_y+1 ) ,1, (200,200,200))
  330. fr = font15.render("{:02}".format(_nr ) ,1, (200,200,200))
  331. window.blit(fr,(pos[0]+2,pos[1]+2 ))
  332. window.blit(fr,(130,1))
  333. # fix pos
  334. txt=str(pos) #"[0, 0, 0, 0]"
  335. fr = font15.render(txt ,1, (200,200,200))
  336. #window.blit(fr,(pos[0]+2,pos[1]+2 ))
  337. window.blit(fr,(10,1))
  338. # univers
  339. #fr = font15.render("{:02}:{:03}".format(fix.univ,fix.dmx) ,1, (200,200,200))
  340. #window.blit(fr,(300,10))
  341. # pointer
  342. fr = font15.render("X:{:03}".format(self._x) ,1, (200,200,200))
  343. window.blit(fr,(10,30))
  344. fr = font15.render("Y:{:03}".format(self._y) ,1, (200,200,200))
  345. window.blit(fr,(10,40))
  346. # crosshair
  347. self.rgb = [0,0,200]
  348. pygame.draw.line(window,self.rgb, (self.x-p,self.y) , (self.x-2,self.y),4 )
  349. pygame.draw.line(window,self.rgb, (self.x,self.y-p) , (self.x,self.y-2),4 )
  350. self.rgb = [0,200,0]
  351. pygame.draw.line(window,self.rgb, (self.x+2,self.y) , (self.x+p,self.y),4 )
  352. pygame.draw.line(window,self.rgb, (self.x,self.y+2) , (self.x,self.y+p),4 )
  353. self.rgb = [200,0,0]
  354. pointer = POINTER()
  355. NR = 0
  356. running = True
  357. def event():
  358. global NR,running
  359. for event in pygame.event.get():
  360. #print(event.dict)
  361. _button = None
  362. if "button" in event.dict:
  363. _button = event.dict["button"]
  364. _state = None
  365. if "state" in event.dict:
  366. _state = event.state
  367. _key = None
  368. if "key" in event.dict:
  369. _key = event.key
  370. _pos = None
  371. if "pos" in event.dict:
  372. _pos = event.pos
  373. _type = None
  374. if "type" in event.dict:
  375. _type = event.type
  376. _type = event.type
  377. _mod = None
  378. if "mod" in event.dict:
  379. _mod = event.mod
  380. if 0:
  381. print( " ")
  382. print( "{:.02f}".format( time.time() - START ))
  383. print("button -",_button,end="\t| ")
  384. #print("state -",_state)
  385. print("pos -",_pos)
  386. print("type -",_type, end="\t| ")
  387. print("key -",_key)
  388. print("mod -",_mod)
  389. try:
  390. if _type == 5:
  391. if _button == 1:
  392. NR += 1
  393. if NR > 1:
  394. NR = 0
  395. if _button == 3:
  396. NR -= 1
  397. if NR < 0:
  398. NR = 1
  399. if _pos:
  400. posA = _pos
  401. fix = find_pix(_pos[0]-40,_pos[1]-60+pm_wy)
  402. if fix:
  403. pos = fix.POS(40,60+pm_wy)
  404. rgb = [0,0,0]
  405. pointer.move(pos)
  406. pointer.fix = fix
  407. else:
  408. pointer.on = 0
  409. pointer.row_move(_pos[0],_pos[1])
  410. pointer.cross(_pos[0],_pos[1])
  411. if event.type == pygame.VIDEORESIZE:
  412. window = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)
  413. except Exception as e:
  414. print(e)
  415. if event.type==pygame.QUIT:
  416. running=False
  417. fps = 0
  418. frame = 0
  419. frame_t = time.time()
  420. IP = "yyy"
  421. def draw_overlay():
  422. global fps
  423. fr = font.render("FPS:{}".format(fps) ,1, (200,0,255))
  424. window.blit(fr,(10,10))
  425. fr = font.render("ip:{}".format(IP) ,1, (200,0,255))
  426. window.blit(fr,(80,10))
  427. def calc_fps():
  428. global fps,frame,frame_t
  429. t = time.time()
  430. if frame_t+1 < t:
  431. fps = frame #frame_t- t #frame
  432. frame = 1
  433. frame_t = time.time()
  434. # ===== GUI =========
  435. #def draw_circle(surface, x, y, radius, color):
  436. def draw_circle(surface,color, pos, radius):
  437. x,y=pos
  438. pygame.gfxdraw.aacircle(surface, int(x), int(y), radius-1, color)
  439. pygame.gfxdraw.filled_circle(surface, int(x), int(y), radius-1, color)
  440. def rDMX(univ,dmx):
  441. return univ*512+dmx
  442. def generate_grid(mapping=0):
  443. _log = []
  444. #if PIXEL_MAPPING:
  445. # log = open(grid_file,"w")
  446. head = "i,univ,dmx,x,y,ch\n"
  447. head = "i,univ,dmx,ch\n"
  448. head = "univ,dmx,x,y,ch\n"
  449. head = "nr,id,info\n"
  450. print("csv:",head)
  451. #if PIXEL_MAPPING:
  452. # log.write(head)
  453. _log.append(head)
  454. dmx = 1-1
  455. ch = 4
  456. y=0
  457. x=0
  458. #for i in range((_y)*(_x)):
  459. for i in range((8)*(8)):
  460. #if x > _x and i%_x == 0:
  461. if x > 8 and i%8 == 0:
  462. print("--> -->")
  463. x=0
  464. y+=1
  465. _univ = int(dmx/512)
  466. _dmx = dmx - (_univ)*512
  467. pos=[x,y]
  468. line="{},{},{},{},{},{}\n".format(i+1,_univ,_dmx+1,pos[0],pos[1],ch)
  469. line="{},{},{},{},{}\n".format(_univ,_dmx+1,x,y,ch)
  470. line="{},{},x\n".format(i+1,i+1)
  471. #print("wcsv:",[line])
  472. #if PIXEL_MAPPING:
  473. # log.write(line)
  474. _log.append(line)
  475. dmx += ch
  476. x+=1
  477. if mapping and PIXEL_MAPPING:
  478. print("CREATE NEW PIXELMAP FILE !!",grid_file)
  479. log = open(grid_file,"w")
  480. log.writelines(_log)
  481. log.close()
  482. return _log[:] #GRID
  483. def init_grid(mapping=0,_x=4,_y=4):
  484. if mapping and PIXEL_MAPPING:
  485. try:
  486. log = open(grid_file,"r")
  487. except:
  488. generate_grid(mapping=mapping)
  489. log = open(grid_file,"r")
  490. lines = log.readlines()
  491. else:
  492. lines = generate_grid()
  493. GRID = []
  494. y=0
  495. x=0
  496. print("CSV header",[lines[0]],[PIXEL_MAPPING])
  497. for i,line in enumerate(lines[1:]): #exclude first line
  498. #print("rcsv",[line])
  499. line = line.strip()
  500. line = line.split(",") # csv
  501. if i >= _x and i%_x == 0:
  502. x=0
  503. y+=1
  504. if y >= _y:
  505. break
  506. #i = int(line[0])
  507. _id = int(line[1])
  508. #univ = int(line[0])
  509. #dmx = int(line[1])
  510. #x = int(line[3])
  511. #y = int(line[4])
  512. #ch = int(line[4])
  513. pos = [x,y]
  514. f = Fix(_id,pos,block) #pos,univ,dmx,ch)
  515. #f.x = x
  516. #f.y = y
  517. #f.block = block
  518. GRID.append(f)
  519. x+=1
  520. #print("y, _y",y,_y)
  521. return GRID
  522. def find_pix(x,y):
  523. global GRID
  524. for fix in GRID:
  525. X = 0
  526. Y = 0
  527. pos = fix.POS()
  528. #pos = fix.POS()
  529. #pos = fix.POS(40,60-pm_wy)
  530. #pos = fix.POS(x,y+pm_wy)#40,60)
  531. if x > pos[0] and x < pos[0]+pos[2]:
  532. X = 1
  533. if y > pos[1] and y < pos[1]+pos[3]:
  534. Y = 1
  535. if X and Y:
  536. #print(pos,x,y)
  537. #print("find",X,Y)
  538. return fix
  539. GRID = []
  540. _GRID = []
  541. _GRID = init_grid(_x=8,_y=8,mapping=1) #init_gird()
  542. NR = 0
  543. START_UNIV=2
  544. if options.start_univ:
  545. try:
  546. START_UNIV=int(options.start_univ)
  547. except Exception as e:
  548. print("Exception START UNIV",e)
  549. gobo_ch=1
  550. if options.gobo_ch:
  551. try:
  552. gobo_ch=int(options.gobo_ch)
  553. except Exception as e:
  554. print("Exception gobo_ch",e)
  555. if gobo_ch <= 0:
  556. gobo_ch = 1
  557. def draw_box(pos1,pos2,color=[128,128,128],text=1):
  558. color = [200,0,0,127]
  559. if text:
  560. fr = font15.render("A" ,1, (200,200,200))
  561. window.blit(fr,pos1)
  562. fr = font15.render("B" ,1, (200,200,200))
  563. window.blit(fr,[pos2[0]-10,pos2[1]-10])
  564. # h unten
  565. _pos1 = [pos1[0],pos2[1]]
  566. _pos2 = [pos2[0],pos2[1]]
  567. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  568. color = [255,255,0,127]
  569. # h rechts
  570. _pos1 = [pos2[0],pos1[1]]
  571. _pos2 = [pos2[0],pos2[1]]
  572. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  573. color = [0,200,0,127]
  574. # h links
  575. _pos1 = [pos1[0],pos1[1]]
  576. _pos2 = [pos1[0],pos2[1]]
  577. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  578. color = [0,0,200,127]
  579. # h oben
  580. _pos1 = [pos1[0],pos1[1]]
  581. _pos2 = [pos2[0],pos1[1]]
  582. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  583. def grab(x=55,y=55,w=60,h=60):
  584. # usage
  585. # sub = grab()
  586. # window.blit(sub, (500,10))
  587. crop = None
  588. rect = pygame.Rect(x, y, w, h)
  589. try:
  590. sub = window.subsurface(rect)
  591. #pixArray = pygame.PixelArray(screen)
  592. crop = pygame.Surface((w,h))
  593. crop.blit(sub, (0,0))
  594. except ValueError as e:
  595. pass#print("exception",e,"line715")
  596. #except Exception as e:
  597. # print("exception",e,"line715")
  598. #print(rect)
  599. return crop
  600. def reshape(x,y):
  601. if PIXEL_MAPPING <= 0:
  602. return None
  603. global GRID
  604. global _GRID
  605. i = 0
  606. counter = 0
  607. z=0
  608. x_min = 99999
  609. x_max = 0
  610. y_min = 99999
  611. y_max = 0
  612. fr = font.render("OUTPUT MAP".format(t1.get()) ,1, (255,255,255))
  613. fr_r = fr.get_rect(center=(x+int(wx/2),y+pm_wy-5))
  614. #window.blit(fr,(x+int(wx/2),y+pm_wy))
  615. window.blit(fr,fr_r)
  616. fr = font.render("↑ ↑ ↑".format(t1.get()) ,1, (255,255,255))
  617. fr_r = fr.get_rect(center=(x+int(wx/2),y+pm_wy+15))
  618. #window.blit(fr,(x+int(wx/2),y+pm_wy))
  619. window.blit(fr,fr_r)
  620. fr = font.render("INPUT".format(t1.get()) ,1, (255,255,255))
  621. fr_r = fr.get_rect(center=(x+int(wx/2),y+pm_wy+35))
  622. #window.blit(fr,(x+int(wx/2),y+pm_wy))
  623. window.blit(fr,fr_r)
  624. j = 0
  625. for fix in _GRID:
  626. if j >= 8*8: # max output size
  627. break
  628. #if j >= _x*_y: # max input size
  629. # break
  630. j+=1
  631. ii = i
  632. #z= i # helping border offset
  633. pos = fix.POS(40,60)
  634. rgb = fix.rgb
  635. # green
  636. pygame.draw.rect(window,[0,40,0],pos)
  637. xposs = [] #None #pos[:]
  638. for fix2 in GRID:
  639. if fix._id == fix2._id:
  640. xposs.append( fix2.POS(40,60) )
  641. for xpos in xposs:
  642. sub = grab(xpos[0],xpos[1]+pm_wy,xpos[2],xpos[3])
  643. if sub:
  644. if 1:#j <= _x*_y: # max input size
  645. window.blit(sub, (x+pos[0]+z,y+pos[1]+z))
  646. else:
  647. # red
  648. pygame.draw.rect(window,[40,0,0],pos) #[x+pos[0]+2+z,y+pos[1]+2+z-pm_wy,12,9])
  649. if xpos[0] < x_min:
  650. x_min = xpos[0]
  651. if xpos[0] > x_max:
  652. x_max += xpos[2]
  653. if xpos[1] < y_min:
  654. y_min = xpos[1]
  655. if xpos[1] > x_max:
  656. y_max += xpos[3]
  657. # DRAW FIX NUMBER on TOP
  658. #apos = pos
  659. #argb = rgb
  660. apos = fix.POS(40,60+pm_wy)
  661. argb = fix.rgb
  662. # overwrite number overlay
  663. if NR:
  664. #pygame.draw.rect(window,[30,40,0],apos)
  665. pygame.draw.rect(window,[20,40,0],[x+apos[0]+2+z,y+apos[1]+2+z-pm_wy,12,9])
  666. if NR:# == 2:
  667. if fix._id != i+1:
  668. fr = font15.render("{:02}".format(fix._id) ,1, (255,255,0))
  669. window.blit(fr,(x+apos[0]+2+z,y+apos[1]+2+z-pm_wy))
  670. #print(fix._id,xposs,pos)
  671. else:
  672. fr = font15.render("{:02}".format(fix._id) ,1, (100,100,255))
  673. window.blit(fr,(x+apos[0]+2+z,y+apos[1]+2+z-pm_wy))
  674. i += 1
  675. #print("--#")
  676. # frame box
  677. #pygame.draw.box(window,[100,0,0],[x+x_min,y+x_min,x_max+x_min,y_min+y_max])
  678. pos1= [x+x_min,y+x_min]
  679. pos2= [x_max+x_min,y_min+y_max]
  680. #draw_box(pos1,pos2,text=0)
  681. class Timer():
  682. def __init__(self,start=120):
  683. self.start = start
  684. self.timer = self.start
  685. self.timer_t = time.time()
  686. def reset(self):
  687. self.timer = self.start
  688. def get(self):
  689. self.timer -= time.time()-self.timer_t
  690. self.timer_t = time.time()
  691. if self.timer <= 0:
  692. self.reset()
  693. return self.timer
  694. def reload_grid():
  695. global GRID
  696. global _GRID
  697. try:
  698. GRID = init_grid(_x=_x,_y=_y) #init_gird()
  699. _GRID = init_grid(_x=8,_y=8,mapping=1) #init_gird()
  700. except Exception as e:
  701. print("Except: grid re init",e)
  702. VPU_TEXT = []
  703. def load_vpu_text(nr=0):
  704. txt = "NONE"
  705. if not VPU_TEXT:
  706. pass #create VPU_TEXT_FILE
  707. if len(VPU_TEXT) > nr:
  708. txt = VPU_TEXT[0]
  709. return txt
  710. t1 = Timer(143)
  711. time.sleep(0.33)
  712. t2 = Timer(11)
  713. count_tilt = 0
  714. def main():
  715. global IP
  716. global GRID
  717. global _GRID
  718. global FUNC
  719. global count_tilt
  720. counter = time.time()
  721. GRID = init_grid(_x=_x,_y=_y) #init_gird()
  722. #GRID = init_grid(_x=8,_y=8) #init_gird()
  723. print("GRID LEN:",len(GRID))
  724. s=time.time()
  725. print("run")
  726. r = ""
  727. IP = "xx"
  728. while running:
  729. #reload_grid()
  730. pygame.display.flip()
  731. event()
  732. window.fill((10,0,20))
  733. calc_fps()
  734. draw_overlay()
  735. ips = read_index()
  736. # ----
  737. ip = select_ip(ips,univ=1) # univ 1 gobo
  738. dataA = read_dmx(ip)
  739. # ----
  740. ip = select_ip(ips,univ=START_UNIV)
  741. IP = ip
  742. data = read_dmx(ip)
  743. ip = select_ip(ips,univ=START_UNIV+1)
  744. data3 = read_dmx(ip)
  745. data.extend(data3)
  746. ip = select_ip(ips,univ=START_UNIV+2)
  747. data3 = read_dmx(ip)
  748. data.extend(data3)
  749. ip = select_ip(ips,univ=START_UNIV+4)
  750. data3 = read_dmx(ip)
  751. data.extend(data3)
  752. #ip = select_ip(ips,univ=START_UNIV+5)
  753. #data3 = read_dmx(ip)
  754. #data.extend(data3)
  755. if options.countdown:
  756. for count in COUNTER:
  757. cDMX=count["DMX"]-1
  758. try:
  759. count["DIM"] = dataA[cDMX]
  760. count["PAN"] = dataA[cDMX+1]
  761. count["TILT"] = dataA[cDMX+2]
  762. count["CONTROL"] = dataA[cDMX+3]
  763. if count["CONTROL"] >= 10 and count["CONTROL"] < 20:
  764. count["_time"] = int(time.time()*10)/10
  765. count["_SEC"] = int(count["SEC"] - (time.time() - count["_time"]))
  766. if count["CONTROL"] >= 20 and count["CONTROL"] < 30:
  767. count["_RUN"] = 0
  768. if count["CONTROL"] >= 30 and count["CONTROL"] < 40:
  769. count["_RUN"] = 1
  770. count["SIZE"] = dataA[cDMX+4]
  771. count["SEC"] = dataA[cDMX+5]
  772. if count["_RUN"]:
  773. try:
  774. count["_SEC"] = int(count["SEC"] - (time.time() - count["_time"]))
  775. except Exception as e:
  776. pass
  777. if type(count["_SEC"]) is int:
  778. if count["_SEC"] < 0:
  779. count["_SEC"] = 0
  780. if count["CONTROL"] >= 60 and count["CONTROL"] < 70:
  781. count["_SEC"] = "HSN" #text 1
  782. if count["CONTROL"] >= 70 and count["CONTROL"] < 80:
  783. count["_SEC"] = "LOS" #text 2
  784. if count["CONTROL"] >= 80 and count["CONTROL"] < 90:
  785. count["_SEC"] = "GO" #text 3
  786. if count["CONTROL"] >= 90 and count["CONTROL"] < 100:
  787. count["_SEC"] = "PARTY" #text 4
  788. if count["CONTROL"] >= 250 and count["CONTROL"] < 256:
  789. count["_SEC"] = ">{}<".format(cDMX+1)
  790. count["RED"] = dataA[cDMX+6]
  791. count["GREEN"] = dataA[cDMX+7]
  792. count["BLUE"] = dataA[cDMX+8]
  793. except Exception as e:
  794. print("EXC FUNC",e,count)
  795. #print(count)
  796. # GRID loop
  797. try:
  798. ddd = 1023 #univ 3 512
  799. #FUNC = data[ddd]
  800. FUNC2 = dataA[gobo_ch-1]
  801. FUNC = FUNC2
  802. #print("FUNC", FUNC )#:ddd+512])
  803. #FUNC = 15
  804. except Exception as e:
  805. print("EXC FUNC",e)
  806. i = 0
  807. dmx = 1
  808. h = 1
  809. v = 1
  810. for fix in GRID:
  811. pos = fix.POS(40,60+pm_wy)
  812. rgb = fix.rgb
  813. if 1:
  814. # draw row/col grid number
  815. if fix.pos[0] == 0:
  816. fr = font12.render("{}".format(fix.pos[1]+1) ,1, (200,200,200))
  817. window.blit(fr,(10,pos[1]+3 ))
  818. if fix.pos[1] == 0:
  819. fr = font12.render("{}".format(fix.pos[0]+1) ,1, (200,200,200))
  820. window.blit(fr,(pos[0]+2,35 ))
  821. pygame.draw.rect(window,rgb,pos)
  822. # DRAW SUB-FIXTURE
  823. j = 0
  824. for subfix in fix.sub_fix:#calc(data):
  825. subfix.calc(data)
  826. #fix = subfix
  827. spos = subfix.POS(40,60+pm_wy)
  828. srgb = subfix.rgb
  829. #print(fix.dmx,rgb,pos)
  830. #pygame.draw.circle(window,rgb,(pos[0]+int(pos[2]/2),pos[1]+int(pos[3]/2)),int(pos[3]/2))
  831. #FUNC = 0
  832. if FUNC > 10 and FUNC <= 20: # big dot
  833. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  834. elif FUNC > 20 and FUNC <= 30:#small dot
  835. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  836. elif FUNC > 30 and FUNC <= 40:#donut
  837. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  838. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  839. elif FUNC > 40 and FUNC <= 50: # rec with hole
  840. pygame.draw.rect(window,srgb,spos)
  841. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  842. elif FUNC > 50 and FUNC <= 60: # rec with big hole
  843. pygame.draw.rect(window,srgb,spos)
  844. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  845. elif FUNC > 60 and FUNC <= 70: # rec with donat
  846. pygame.draw.rect(window,srgb,spos)
  847. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  848. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  849. elif FUNC > 70 and FUNC <= 80: # rec boarder
  850. pygame.draw.rect(window,srgb,[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  851. elif FUNC > 80 and FUNC <= 90: # rec big boarder
  852. pygame.draw.rect(window,srgb,[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  853. elif FUNC > 90 and FUNC <= 100: # rec thin line
  854. pygame.draw.rect(window,srgb,spos)
  855. pygame.draw.rect(window,[0,0,0],[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  856. elif FUNC > 100 and FUNC <= 110: # rec big line
  857. pygame.draw.rect(window,srgb,spos)
  858. pygame.draw.rect(window,[0,0,0],[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  859. elif FUNC > 110 and FUNC <= 120: # rec with dot
  860. pygame.draw.rect(window,srgb,spos)
  861. pygame.draw.rect(window,[0,0,0],[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  862. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  863. elif FUNC > 120 and FUNC <= 130: # rec inline
  864. pygame.draw.rect(window,srgb,[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  865. pygame.draw.rect(window,[0,0,0],[spos[0]+3,spos[1]+3,spos[2]-6,spos[3]-6])
  866. elif FUNC > 130 and FUNC <= 140: # 3 dot (heart)
  867. draw_circle(window,srgb,(spos[0]+int(spos[2]/2)+2,spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  868. draw_circle(window,srgb,(spos[0]+int(spos[2]/2)-2,spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  869. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)+2),int(spos[3]/3.5))
  870. else:
  871. pygame.draw.rect(window,srgb,spos)
  872. for subfix in fix.sub_fix:#calc(data):
  873. subfix.calc(data)
  874. #fix = subfix
  875. spos = subfix.POS(40,60+pm_wy)
  876. srgb = subfix.rgb
  877. # draw row/col grid number
  878. if subfix.pos[0] == 0:
  879. fr = font12.render("{}".format(v ) ,1, (200,200,200))
  880. window.blit(fr,(25,spos[1] ))
  881. v += 1
  882. if subfix.pos[1] == 0:
  883. fr = font12.render("{}".format(1) ,1, (200,200,200))
  884. fr = font12.render("{}".format(h ) ,1, (200,200,200))
  885. h+=1
  886. window.blit(fr,(spos[0],50 ))
  887. if p >= 40:
  888. if NR:
  889. #fr = font15.render("{:02}".format(j+1) ,1, (0,200,255))
  890. fr = font15.render("{:02}".format(subfix._id) ,1, (250,200,5))
  891. window.blit(fr,(spos[0]+2,spos[1]+10))
  892. j += 1
  893. i += 1
  894. # DRAW FIX NUMBER on TOP
  895. i=0
  896. y=0
  897. for fix in GRID:
  898. pos = fix.POS(40,60+pm_wy)
  899. rgb = fix.rgb
  900. if NR:
  901. pygame.draw.rect(window,[0,0,0],[pos[0]+2,pos[1]+2,12,9])
  902. if fix._id%_x-1 == 0: # line break border
  903. pygame.draw.line(window,[255,255,0],(pos[0],pos[1]+4),(pos[0],pos[1]+pos[3]-4),1)
  904. 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)
  905. if fix._id%_x == 0: # line break border
  906. 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)
  907. 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)
  908. if counter +5 < time.time():
  909. counter = time.time()
  910. reload_grid()
  911. if fix._id != i+1:
  912. fr = font15.render("{:02}".format(fix._id) ,1, (255,255,0))
  913. else:
  914. fr = font15.render("{:02}".format(fix._id) ,1, (100,100,255))
  915. window.blit(fr,(pos[0]+2,pos[1]+2))
  916. i += 1
  917. pointer.draw(0,pm_wy) #wy
  918. #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})
  919. if options.countdown:
  920. for count in COUNTER:
  921. cpan = 0
  922. ctilt = 0
  923. cr=255
  924. cg=255
  925. cb=255
  926. csize=10
  927. cdim=0
  928. k = "DIM"
  929. if k in count:
  930. cdim = int(count[k])
  931. k = "RED"
  932. if k in count:
  933. cr = int(count[k])
  934. k = "GREEN"
  935. if k in count:
  936. cg = int(count[k])
  937. k = "BLUE"
  938. if k in count:
  939. cb = int(count[k])
  940. k = "SIZE"
  941. if k in count:
  942. csize = int(count[k])
  943. if csize < 5:
  944. csize = 5
  945. k = "PAN"
  946. if k in count:
  947. cpan = int(count[k])/255*(block[0] *(_x))
  948. cpan = int(cpan)
  949. k = "TILT"
  950. if k in count:
  951. ctilt = int(count[k])/255*(block[1] *(_y))
  952. ctilt = int(ctilt)
  953. ddim = cdim/255
  954. if "DIM" in count and count["DIM"] > 0:
  955. tmp_font = pygame.font.SysFont("freemonobold",int(block[0]/100*csize))
  956. rgb =(int(cr*ddim),int(cg*ddim),int(cb*ddim),cdim)
  957. _sec = count["_SEC"]
  958. try:
  959. _sec = int(count["_SEC"])
  960. except:
  961. pass
  962. if type(_sec) is int:
  963. #print(_sec)
  964. if _sec <= 60:
  965. fr = tmp_font.render("{:0}".format(_sec) ,1, rgb)
  966. else:
  967. _xx = time.strftime("%M:%S",time.localtime(_sec))
  968. #print("_xx",_xx)
  969. fr = tmp_font.render("{}".format(_xx) ,1, rgb)
  970. else:
  971. fr = tmp_font.render("{}".format((count["_SEC"])) ,1, rgb)
  972. fr_r = fr.get_rect(center=(60+cpan-(block[0]),60+ctilt+pm_wy))
  973. pygame.draw.rect(window,[0,0,0],fr_r)
  974. window.blit(fr,fr_r)
  975. if PIXEL_MAPPING >= 1:
  976. reshape(0,0) #start pos
  977. else:
  978. reshape(spos[0]+spos[2]+20,10) #start pos
  979. pygame.display.flip()
  980. pg.time.wait(60)
  981. if __name__ == "__main__":
  982. main()