vpu_live.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  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. t1 = Timer(143)
  703. time.sleep(0.33)
  704. t2 = Timer(11)
  705. count_tilt = 0
  706. def main():
  707. global IP
  708. global GRID
  709. global _GRID
  710. global FUNC
  711. global count_tilt
  712. counter = time.time()
  713. GRID = init_grid(_x=_x,_y=_y) #init_gird()
  714. #GRID = init_grid(_x=8,_y=8) #init_gird()
  715. print("GRID LEN:",len(GRID))
  716. s=time.time()
  717. print("run")
  718. r = ""
  719. IP = "xx"
  720. while running:
  721. #reload_grid()
  722. pygame.display.flip()
  723. event()
  724. window.fill((10,0,20))
  725. calc_fps()
  726. draw_overlay()
  727. ips = read_index()
  728. # ----
  729. ip = select_ip(ips,univ=1) # univ 1 gobo
  730. dataA = read_dmx(ip)
  731. # ----
  732. ip = select_ip(ips,univ=START_UNIV)
  733. IP = ip
  734. data = read_dmx(ip)
  735. ip = select_ip(ips,univ=START_UNIV+1)
  736. data3 = read_dmx(ip)
  737. data.extend(data3)
  738. ip = select_ip(ips,univ=START_UNIV+2)
  739. data3 = read_dmx(ip)
  740. data.extend(data3)
  741. ip = select_ip(ips,univ=START_UNIV+4)
  742. data3 = read_dmx(ip)
  743. data.extend(data3)
  744. #ip = select_ip(ips,univ=START_UNIV+5)
  745. #data3 = read_dmx(ip)
  746. #data.extend(data3)
  747. if options.countdown:
  748. for count in COUNTER:
  749. cDMX=count["DMX"]-1
  750. try:
  751. count["DIM"] = dataA[cDMX]
  752. count["PAN"] = dataA[cDMX+1]
  753. count["TILT"] = dataA[cDMX+2]
  754. count["CONTROL"] = dataA[cDMX+3]
  755. if count["CONTROL"] >= 10 and count["CONTROL"] < 20:
  756. count["_time"] = int(time.time()*10)/10
  757. count["_SEC"] = int(count["SEC"] - (time.time() - count["_time"]))
  758. if count["CONTROL"] >= 20 and count["CONTROL"] < 30:
  759. count["_RUN"] = 0
  760. if count["CONTROL"] >= 30 and count["CONTROL"] < 40:
  761. count["_RUN"] = 1
  762. count["SIZE"] = dataA[cDMX+4]
  763. count["SEC"] = dataA[cDMX+5]
  764. if count["_RUN"]:
  765. try:
  766. count["_SEC"] = int(count["SEC"] - (time.time() - count["_time"]))
  767. except Exception as e:
  768. pass
  769. if type(count["_SEC"]) is int:
  770. if count["_SEC"] < 0:
  771. count["_SEC"] = 0
  772. if count["CONTROL"] >= 60 and count["CONTROL"] < 70:
  773. count["_SEC"] = "HSN"
  774. if count["CONTROL"] >= 70 and count["CONTROL"] < 80:
  775. count["_SEC"] = "LOS"
  776. if count["CONTROL"] >= 80 and count["CONTROL"] < 90:
  777. count["_SEC"] = "GO"
  778. if count["CONTROL"] >= 90 and count["CONTROL"] < 100:
  779. count["_SEC"] = "PARTY"
  780. if count["CONTROL"] >= 250 and count["CONTROL"] < 256:
  781. count["_SEC"] = ">{}<".format(cDMX+1)
  782. count["RED"] = dataA[cDMX+6]
  783. count["GREEN"] = dataA[cDMX+7]
  784. count["BLUE"] = dataA[cDMX+8]
  785. except Exception as e:
  786. print("EXC FUNC",e,count)
  787. #print(count)
  788. # GRID loop
  789. try:
  790. ddd = 1023 #univ 3 512
  791. #FUNC = data[ddd]
  792. FUNC2 = dataA[gobo_ch-1]
  793. FUNC = FUNC2
  794. #print("FUNC", FUNC )#:ddd+512])
  795. #FUNC = 15
  796. except Exception as e:
  797. print("EXC FUNC",e)
  798. i = 0
  799. dmx = 1
  800. h = 1
  801. v = 1
  802. for fix in GRID:
  803. pos = fix.POS(40,60+pm_wy)
  804. rgb = fix.rgb
  805. if 1:
  806. # draw row/col grid number
  807. if fix.pos[0] == 0:
  808. fr = font12.render("{}".format(fix.pos[1]+1) ,1, (200,200,200))
  809. window.blit(fr,(10,pos[1]+3 ))
  810. if fix.pos[1] == 0:
  811. fr = font12.render("{}".format(fix.pos[0]+1) ,1, (200,200,200))
  812. window.blit(fr,(pos[0]+2,35 ))
  813. pygame.draw.rect(window,rgb,pos)
  814. # DRAW SUB-FIXTURE
  815. j = 0
  816. for subfix in fix.sub_fix:#calc(data):
  817. subfix.calc(data)
  818. #fix = subfix
  819. spos = subfix.POS(40,60+pm_wy)
  820. srgb = subfix.rgb
  821. #print(fix.dmx,rgb,pos)
  822. #pygame.draw.circle(window,rgb,(pos[0]+int(pos[2]/2),pos[1]+int(pos[3]/2)),int(pos[3]/2))
  823. #FUNC = 0
  824. if FUNC > 10 and FUNC <= 20: # big dot
  825. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  826. elif FUNC > 20 and FUNC <= 30:#small dot
  827. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  828. elif FUNC > 30 and FUNC <= 40:#donut
  829. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  830. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  831. elif FUNC > 40 and FUNC <= 50: # rec with hole
  832. pygame.draw.rect(window,srgb,spos)
  833. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  834. elif FUNC > 50 and FUNC <= 60: # rec with big hole
  835. pygame.draw.rect(window,srgb,spos)
  836. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  837. elif FUNC > 60 and FUNC <= 70: # rec with donat
  838. pygame.draw.rect(window,srgb,spos)
  839. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  840. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  841. elif FUNC > 70 and FUNC <= 80: # rec boarder
  842. pygame.draw.rect(window,srgb,[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  843. elif FUNC > 80 and FUNC <= 90: # rec big boarder
  844. pygame.draw.rect(window,srgb,[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  845. elif FUNC > 90 and FUNC <= 100: # rec thin line
  846. pygame.draw.rect(window,srgb,spos)
  847. pygame.draw.rect(window,[0,0,0],[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  848. elif FUNC > 100 and FUNC <= 110: # rec big line
  849. pygame.draw.rect(window,srgb,spos)
  850. pygame.draw.rect(window,[0,0,0],[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  851. elif FUNC > 110 and FUNC <= 120: # rec with dot
  852. pygame.draw.rect(window,srgb,spos)
  853. pygame.draw.rect(window,[0,0,0],[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  854. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  855. elif FUNC > 120 and FUNC <= 130: # rec inline
  856. pygame.draw.rect(window,srgb,[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  857. pygame.draw.rect(window,[0,0,0],[spos[0]+3,spos[1]+3,spos[2]-6,spos[3]-6])
  858. elif FUNC > 130 and FUNC <= 140: # 3 dot (heart)
  859. draw_circle(window,srgb,(spos[0]+int(spos[2]/2)+2,spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  860. draw_circle(window,srgb,(spos[0]+int(spos[2]/2)-2,spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  861. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)+2),int(spos[3]/3.5))
  862. else:
  863. pygame.draw.rect(window,srgb,spos)
  864. for subfix in fix.sub_fix:#calc(data):
  865. subfix.calc(data)
  866. #fix = subfix
  867. spos = subfix.POS(40,60+pm_wy)
  868. srgb = subfix.rgb
  869. # draw row/col grid number
  870. if subfix.pos[0] == 0:
  871. fr = font12.render("{}".format(v ) ,1, (200,200,200))
  872. window.blit(fr,(25,spos[1] ))
  873. v += 1
  874. if subfix.pos[1] == 0:
  875. fr = font12.render("{}".format(1) ,1, (200,200,200))
  876. fr = font12.render("{}".format(h ) ,1, (200,200,200))
  877. h+=1
  878. window.blit(fr,(spos[0],50 ))
  879. if p >= 40:
  880. if NR:
  881. #fr = font15.render("{:02}".format(j+1) ,1, (0,200,255))
  882. fr = font15.render("{:02}".format(subfix._id) ,1, (250,200,5))
  883. window.blit(fr,(spos[0]+2,spos[1]+10))
  884. j += 1
  885. i += 1
  886. # DRAW FIX NUMBER on TOP
  887. i=0
  888. y=0
  889. for fix in GRID:
  890. pos = fix.POS(40,60+pm_wy)
  891. rgb = fix.rgb
  892. if NR:
  893. pygame.draw.rect(window,[0,0,0],[pos[0]+2,pos[1]+2,12,9])
  894. if fix._id%_x-1 == 0: # line break border
  895. pygame.draw.line(window,[255,255,0],(pos[0],pos[1]+4),(pos[0],pos[1]+pos[3]-4),1)
  896. 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)
  897. if fix._id%_x == 0: # line break border
  898. 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)
  899. 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)
  900. if counter +5 < time.time():
  901. counter = time.time()
  902. reload_grid()
  903. if fix._id != i+1:
  904. fr = font15.render("{:02}".format(fix._id) ,1, (255,255,0))
  905. else:
  906. fr = font15.render("{:02}".format(fix._id) ,1, (100,100,255))
  907. window.blit(fr,(pos[0]+2,pos[1]+2))
  908. i += 1
  909. pointer.draw(0,pm_wy) #wy
  910. #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})
  911. if options.countdown:
  912. for count in COUNTER:
  913. cpan = 0
  914. ctilt = 0
  915. cr=255
  916. cg=255
  917. cb=255
  918. csize=10
  919. cdim=0
  920. k = "DIM"
  921. if k in count:
  922. cdim = int(count[k])
  923. k = "RED"
  924. if k in count:
  925. cr = int(count[k])
  926. k = "GREEN"
  927. if k in count:
  928. cg = int(count[k])
  929. k = "BLUE"
  930. if k in count:
  931. cb = int(count[k])
  932. k = "SIZE"
  933. if k in count:
  934. csize = int(count[k])
  935. if csize < 5:
  936. csize = 5
  937. k = "PAN"
  938. if k in count:
  939. cpan = int(count[k])/255*(block[0] *_x)
  940. cpan = int(cpan)
  941. k = "TILT"
  942. if k in count:
  943. ctilt = int(count[k])/255*(block[1] *_y)
  944. ctilt = int(ctilt)
  945. ddim = cdim/255
  946. if "DIM" in count and count["DIM"] > 0:
  947. tmp_font = pygame.font.SysFont("freemonobold",int(block[0]/100*csize))
  948. rgb =(int(cr*ddim),int(cg*ddim),int(cb*ddim),cdim)
  949. try:
  950. fr = tmp_font.render("{:0}".format(int(count["_SEC"])) ,1, rgb)
  951. except:
  952. fr = tmp_font.render("{}".format((count["_SEC"])) ,1, rgb)
  953. fr_r = fr.get_rect(center=(60+cpan,60+ctilt+pm_wy))
  954. pygame.draw.rect(window,[0,0,0],fr_r)
  955. window.blit(fr,fr_r)
  956. if PIXEL_MAPPING >= 1:
  957. reshape(0,0) #start pos
  958. else:
  959. reshape(spos[0]+spos[2]+20,10) #start pos
  960. pygame.display.flip()
  961. pg.time.wait(60)
  962. if __name__ == "__main__":
  963. main()