vpu3_live.py 29 KB

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