vpu_live.py 36 KB

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