vpu_live.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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") #, metavar="FILE")
  10. parser.add_option("-x", "--xx", dest="xsplit", default=1,
  11. help="x-split") #, metavar="FILE")
  12. parser.add_option("-y", "--yy", dest="ysplit",default=1,
  13. help="y-split") #, metavar="FILE")
  14. #parser.add_option("-f", "--file", dest="filename",
  15. # help="write report to FILE", metavar="FILE")
  16. #parser.add_option("-q", "--quiet",
  17. # action="store_false", dest="verbose", default=True,
  18. # help="don't print status messages to stdout")
  19. (options, args) = parser.parse_args()
  20. START = time.time()
  21. # ===== ARTNET DMX =========
  22. import memcache
  23. mc = memcache.Client(['127.0.0.1:11211'], debug=0)
  24. def read_index():
  25. ips=mc.get("index")#cmd)
  26. if ips is None:
  27. ips = {}
  28. #for k,v in ips.items():
  29. # print(k,v)
  30. return ips
  31. def select_ip(ips, univ=2): # artnet univ
  32. _univ = ":{}".format(univ)
  33. for ip in ips: #high priority
  34. if "2.0.0" in ip and _univ in ip:
  35. return ip
  36. for ip in ips:
  37. if "ltp-out" in ip and _univ in ip:
  38. return ip
  39. def read_dmx(ip):
  40. global frame
  41. r = ""
  42. if ip:
  43. #t = int(math.sin(time.time() - s)*10)
  44. r = mc.get(ip) #"2.0.0.13:2")
  45. frame += 1
  46. rr = [0]*512
  47. for i,v in enumerate(r):
  48. try: #cleanup ltp-out to int
  49. v = int(v)
  50. rr[i] = v
  51. except:pass
  52. r = rr
  53. if not r:
  54. c = 0
  55. #time.sleep(0.1)
  56. r = [0] *512
  57. for i in range(12*8+1):
  58. dmx = i*4
  59. #print(dmx)
  60. r[dmx:dmx+4] = [255,10,10,40]
  61. return r
  62. # ===== ARTNET DMX =========
  63. p = 16
  64. block = [p,p]
  65. _x = 12
  66. _y = 5
  67. #HD = "0"
  68. if options.mode:
  69. try:
  70. HD = options.mode
  71. p,_x,_y = HD.split(",")
  72. _x = int(_x)
  73. _y = int(_y)
  74. p = int(p)
  75. block = [p,p]
  76. except Exception as e:
  77. print( "Exc",options.mode,e)
  78. HD_x = 2
  79. HD_y = 2
  80. if options.mode:
  81. try:
  82. if options.xsplit:
  83. HD_x = int(options.xsplit)
  84. if options.ysplit:
  85. HD_y = int(options.ysplit)
  86. except Exception as e:
  87. print( "Exc",options.mode,e)
  88. #print(HD_x,HD_y)
  89. #print(_x,_y)
  90. print("++++++++++++++++++", p,_x,_y)
  91. # ===== GUI =========
  92. import pygame
  93. import pygame.gfxdraw
  94. import pygame.font
  95. os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (200,184)
  96. os.environ['SDL_VIDEO_CENTERED'] = '0'
  97. pg = pygame
  98. pygame.init()
  99. pygame.mixer.quit()
  100. f = pygame.font.get_fonts()
  101. for i in f:
  102. if "mono" in i.lower():
  103. print(i)
  104. font = pygame.font.SysFont("freemonobold",22)
  105. font10 = pygame.font.SysFont("freemonobold",10)
  106. font12 = pygame.font.SysFont("freemonobold",12)
  107. font15 = pygame.font.SysFont("freemonobold",15)
  108. #font = pygame.font.SysFont(None,30)
  109. fr = font.render("hallo" ,1, (200,0,255))
  110. main_size=(600,500)
  111. try:
  112. wx = 100+block[0] * _x
  113. wy = 100+block[1] * _y
  114. main_size=(wx,wy)
  115. except Exception as e:
  116. print("Exception:",e)
  117. #main_size=(280,200)
  118. window = pygame.display.set_mode(main_size,pg.RESIZABLE)#,32)#,pygame.FULLSCREEN) #x left->right ,y top-> bottom
  119. pg.display.set_caption('LibreLight LED-SCREEN')
  120. class Fix():
  121. def __init__(self,_id,pos,block=[16,16],univ=0,dmx=0,ch=4):
  122. #print("Fix",_id)
  123. self._id = _id
  124. self.dmx = (_id-1) * ch +1 #dmx
  125. self.univ = univ
  126. self.ch = ch
  127. self.pos = pos
  128. self.rgb = [0,0,140]
  129. self.block = block #[10,10]
  130. self.x = pos[0]
  131. self.y = pos[1]
  132. self.strobo = time.time()
  133. self.bmp = 250
  134. self.sub_fix = []
  135. sub_block =[block[0]/HD_x,block[1]/HD_y]
  136. spalte = (_id-1)%_y +1
  137. zeile = int((_id-1)/_x) #+1
  138. #zeile = zeile*_x*HD_x*HD_y
  139. add_row = _x*HD_x*HD_y
  140. #zeile 1
  141. sid = (_id-1)*2 + zeile*HD_x*_x
  142. #for i in range(1,HD_x):
  143. sid = sid+1
  144. sub_pos= [pos[0]*block[0],pos[1]*block[1]]
  145. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  146. self.sub_fix.append(sub_fix)
  147. sid = sid+1
  148. sub_pos= [pos[0]*block[0]+block[0]/2,pos[1]*block[1]]
  149. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  150. self.sub_fix.append(sub_fix)
  151. #zeile 2
  152. sid = (_id-1)*2+1 + _x*HD_x + zeile*HD_x*_x # int(add_row)
  153. #sid = HD_x
  154. sub_pos= [pos[0]*block[0],pos[1]*block[1]+block[1]/2]
  155. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  156. self.sub_fix.append(sub_fix)
  157. sid = sid+1
  158. sub_pos= [pos[0]*block[0]+block[0]/2,pos[1]*block[1]+block[1]/2]
  159. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  160. self.sub_fix.append(sub_fix)
  161. def calc(self,data):
  162. _rgb = [0,255,0]
  163. return _rgb
  164. def sub_calc(self,data):
  165. _rgb = [0,255,0]
  166. for sub_fix in self.sub_fix:
  167. sub_fix.block = self.block[:]
  168. _rgb = sub_fix.calc(data)
  169. return _rgb
  170. def POS(self,x=0,y=0,a=0,b=0):
  171. A = (self.pos[0])*self.block[0]
  172. B = (self.pos[1])*self.block[1]
  173. C = self.block[0]-a
  174. D = self.block[1]-b
  175. return [x+A,y+B,C,D]
  176. def subPOS(self,x=0,y=0,a=0,b=0):
  177. __out = []
  178. for sub_fix in self.sub_fix:
  179. __out.append( sub_fix.POS(x,y,a,b) )
  180. return __out
  181. class SubFix():
  182. def __init__(self,_id,pos,block=[16,16],univ=0,dmx=0,ch=4):
  183. #print("Fix",_id)
  184. self._id = _id
  185. self.dmx = (_id-1) * ch +1 #dmx
  186. self.univ = univ
  187. self.ch = ch
  188. self.pos = pos
  189. self.rgb = [0,0,40]
  190. self.block = block #[10,10]
  191. self.x = pos[0]
  192. self.y = pos[1]
  193. self.strobo = time.time()
  194. self.bmp = 250
  195. def calc(self,data):
  196. #return [130,30,20]
  197. dmx_sub = [30]*10
  198. #print(dmx_sub)
  199. dmx = self.dmx -1
  200. _dmx_sub = []
  201. if self.dmx >= 0:
  202. dmx = rDMX(self.univ,self.dmx)-1
  203. if dmx+self.ch < len(data):
  204. _dmx_sub = data[dmx:dmx+self.ch]
  205. if _dmx_sub:
  206. dmx_sub = _dmx_sub
  207. #print(dmx_sub)
  208. dim = dmx_sub[0]/255
  209. #print("dmx",dmx,dmx_sub)
  210. r = dmx_sub[1]*dim
  211. g = dmx_sub[2]*dim
  212. b = dmx_sub[3]*dim
  213. r = int(r)
  214. g = int(g)
  215. b = int(b)
  216. self.rgb = [r,g,b]
  217. return self.rgb
  218. def POS(self,x=0,y=0,a=0,b=0):
  219. A = (self.pos[0]) #+self.block[0]
  220. B = (self.pos[1]) #+self.block[1]
  221. C = self.block[0]-a
  222. D = self.block[1]-b
  223. if NR:
  224. C-=1
  225. D-=1
  226. return [x+A,y+B,C,D]
  227. class POINTER():
  228. def __init__(self):
  229. self.pos = [0,0,0,0]
  230. self.on = 0
  231. self.rgb = [0,100,10]
  232. self._x = 0
  233. self._y = 0
  234. self.x = 0
  235. self.y = 0
  236. self.fix = Fix(0 ,[999,999],[16,16],0,0,0)
  237. def row_move(self,x,y):
  238. self._x = x
  239. self._y = y
  240. def move(self,pos):
  241. self.pos = pos
  242. self.on = 1
  243. def cross(self,x,y):
  244. self.x = x
  245. self.y = y
  246. def draw(self):
  247. if self.on:
  248. pygame.draw.rect(window,self.rgb,self.pos)
  249. #pygame.draw.line(window,self.rgb, (self.pos[0],self.pos[1]) , (self.pos[0]+100,self.pos[1]) )
  250. # mouse grid posision
  251. fr = font15.render("{}/{}".format(self.fix.x+1,self.fix.y) ,1, (200,200,200))
  252. _nr = self.fix.y * _y + self.fix.x+1
  253. fr = font15.render("{:02} {}/{}".format(_nr, self.fix.x+1,self.fix.y+1 ) ,1, (200,200,200))
  254. window.blit(fr,(self.pos[0]+2,self.pos[1]+2 ))
  255. window.blit(fr,(200,25))
  256. # fix pos
  257. txt=str(self.pos)
  258. fr = font15.render(txt ,1, (200,200,200))
  259. #window.blit(fr,(self.pos[0]+2,self.pos[1]+2 ))
  260. window.blit(fr,(200,10))
  261. # univers
  262. #fr = font15.render("{:02}:{:03}".format(self.fix.univ,self.fix.dmx) ,1, (200,200,200))
  263. #window.blit(fr,(300,10))
  264. # pointer
  265. fr = font15.render("X:{:03}".format(self._x) ,1, (200,200,200))
  266. window.blit(fr,(10,30))
  267. fr = font15.render("Y:{:03}".format(self._y) ,1, (200,200,200))
  268. window.blit(fr,(10,40))
  269. # crosshair
  270. self.rgb = [0,0,200]
  271. pygame.draw.line(window,self.rgb, (self.x-p,self.y) , (self.x-2,self.y) )
  272. pygame.draw.line(window,self.rgb, (self.x,self.y-p) , (self.x,self.y-2) )
  273. self.rgb = [0,200,0]
  274. pygame.draw.line(window,self.rgb, (self.x+2,self.y) , (self.x+p,self.y) )
  275. pygame.draw.line(window,self.rgb, (self.x,self.y+2) , (self.x,self.y+p) )
  276. self.rgb = [200,0,0]
  277. pointer = POINTER()
  278. NR = 0
  279. running = True
  280. def event():
  281. global NR,running
  282. for event in pygame.event.get():
  283. #print(event.dict)
  284. _button = None
  285. if "button" in event.dict:
  286. _button = event.dict["button"]
  287. _state = None
  288. if "state" in event.dict:
  289. _state = event.state
  290. _key = None
  291. if "key" in event.dict:
  292. _key = event.key
  293. _pos = None
  294. if "pos" in event.dict:
  295. _pos = event.pos
  296. _type = None
  297. if "type" in event.dict:
  298. _type = event.type
  299. _type = event.type
  300. _mod = None
  301. if "mod" in event.dict:
  302. _mod = event.mod
  303. print( " ")
  304. print( "{:.02f}".format( time.time() - START ))
  305. print("button -",_button,end="\t| ")
  306. #print("state -",_state)
  307. print("pos -",_pos)
  308. print("type -",_type, end="\t| ")
  309. print("key -",_key)
  310. print("mod -",_mod)
  311. try:
  312. if _type == 5:
  313. if _button == 1:
  314. NR += 1
  315. if NR > 2:
  316. NR = 0
  317. if _button == 3:
  318. NR -= 1
  319. if NR < 0:
  320. NR = 2
  321. if _pos:
  322. posA = _pos
  323. fix = find_pix(_pos[0]-40,_pos[1]-60)
  324. if fix:
  325. pos = fix.POS(40,60)
  326. rgb = [0,0,0]
  327. pointer.move(pos)
  328. pointer.fix = fix
  329. else:
  330. pointer.on = 0
  331. pointer.row_move(_pos[0],_pos[1])
  332. pointer.cross(_pos[0],_pos[1])
  333. except Exception as e:
  334. print(e)
  335. if event.type==pygame.QUIT:
  336. running=False
  337. fps = 0
  338. frame = 0
  339. frame_t = time.time()
  340. IP = "yyy"
  341. def draw_overlay():
  342. global fps
  343. fr = font.render("FPS:{}".format(fps) ,1, (200,0,255))
  344. window.blit(fr,(10,10))
  345. fr = font.render("ip:{}".format(IP) ,1, (200,0,255))
  346. window.blit(fr,(80,10))
  347. def calc_fps():
  348. global fps,frame,frame_t
  349. t = time.time()
  350. if frame_t+1 < t:
  351. fps = frame #frame_t- t #frame
  352. frame = 1
  353. frame_t = time.time()
  354. # ===== GUI =========
  355. #def draw_circle(surface, x, y, radius, color):
  356. def draw_circle(surface,color, pos, radius):
  357. x,y=pos
  358. pygame.gfxdraw.aacircle(surface, int(x), int(y), radius-1, color)
  359. pygame.gfxdraw.filled_circle(surface, int(x), int(y), radius-1, color)
  360. def rDMX(univ,dmx):
  361. return univ*512+dmx
  362. grid_file = "/tmp/vpu_grid.csv"
  363. grid_file = "/home/user/LibreLight/vpu_grid_hd.csv"
  364. def generate_grid():
  365. log = open(grid_file,"w")
  366. head = "i,univ,dmx,x,y,ch\n"
  367. head = "i,univ,dmx,ch\n"
  368. head = "univ,dmx,x,y,ch\n"
  369. head = "nr,id,info\n"
  370. print("csv:",head)
  371. log.write(head)
  372. dmx = 1-1
  373. ch = 4
  374. y=0
  375. x=0
  376. for i in range((_y)*(_x)):
  377. if x > _x and i%_x == 0:
  378. print("--> -->")
  379. x=0
  380. y+=1
  381. _univ = int(dmx/512)
  382. _dmx = dmx - (_univ)*512
  383. pos=[x,y]
  384. line="{},{},{},{},{},{}\n".format(i+1,_univ,_dmx+1,pos[0],pos[1],ch)
  385. line="{},{},{},{},{}\n".format(_univ,_dmx+1,x,y,ch)
  386. line="{},{},x\n".format(i+1,i+1)
  387. print("wcsv:",[line])
  388. log.write(line)
  389. dmx += ch
  390. x+=1
  391. log.close()
  392. return GRID
  393. def init_grid():
  394. try:
  395. log = open(grid_file,"r")
  396. except:
  397. generate_grid()
  398. log = open(grid_file,"r")
  399. lines = log.readlines()
  400. GRID = []
  401. y=0
  402. x=0
  403. print("CSV header",[lines[0]])
  404. for i,line in enumerate(lines[1:]): #exclude first line
  405. #print("rcsv",[line])
  406. line = line.strip()
  407. line = line.split(",") # csv
  408. if i >= _x and i%_x == 0:
  409. x=0
  410. y+=1
  411. if y > _y:
  412. break
  413. #i = int(line[0])
  414. _id = int(line[1])
  415. #univ = int(line[0])
  416. #dmx = int(line[1])
  417. #x = int(line[3])
  418. #y = int(line[4])
  419. #ch = int(line[4])
  420. pos = [x,y]
  421. f = Fix(_id,pos,block) #pos,univ,dmx,ch)
  422. #f.x = x
  423. #f.y = y
  424. #f.block = block
  425. GRID.append(f)
  426. x+=1
  427. #print("y, _y",y,_y)
  428. return GRID
  429. def find_pix(x,y):
  430. global GRID
  431. for fix in GRID:
  432. X = 0
  433. Y = 0
  434. pos = fix.POS()
  435. if x > pos[0] and x < pos[0]+pos[2]:
  436. X = 1
  437. if y > pos[1] and y < pos[1]+pos[3]:
  438. Y = 1
  439. if X and Y:
  440. print(pos,x,y)
  441. print("find",X,Y)
  442. return fix
  443. GRID = []
  444. NR = 0
  445. START_UNIV=2
  446. def main():
  447. global IP,GRID
  448. counter = time.time()
  449. GRID = init_grid() #init_gird()
  450. print("GRID LEN:",len(GRID))
  451. s=time.time()
  452. print("run")
  453. r = ""
  454. IP = "xx"
  455. while running:
  456. event()
  457. pygame.display.flip()
  458. window.fill((0,0,0))
  459. calc_fps()
  460. draw_overlay()
  461. ips = read_index()
  462. ip = select_ip(ips,univ=START_UNIV)
  463. IP = ip
  464. #print("IP",ip)
  465. data = read_dmx(ip)
  466. ip = select_ip(ips,univ=START_UNIV+1)
  467. data3 = read_dmx(ip)
  468. data.extend(data3)
  469. ip = select_ip(ips,univ=START_UNIV+2)
  470. data3 = read_dmx(ip)
  471. data.extend(data3)
  472. #ip = select_ip(ips,univ=START_UNIV+4)
  473. #data3 = read_dmx(ip)
  474. #data.extend(data3)
  475. # GRID loop
  476. i = 0
  477. dmx = 1
  478. h = 1
  479. v = 1
  480. for fix in GRID:
  481. pos = fix.POS(40,60)
  482. rgb = fix.rgb
  483. if 1:
  484. # draw row/col grid number
  485. if fix.pos[0] == 0:
  486. fr = font12.render("{}".format(fix.pos[1]+1) ,1, (200,200,200))
  487. window.blit(fr,(10,pos[1]+3 ))
  488. if fix.pos[1] == 0:
  489. fr = font12.render("{}".format(fix.pos[0]+1) ,1, (200,200,200))
  490. window.blit(fr,(pos[0]+2,35 ))
  491. pygame.draw.rect(window,rgb,pos)
  492. # DRAW SUB-FIXTURE
  493. j = 0
  494. for subfix in fix.sub_fix:#calc(data):
  495. subfix.calc(data)
  496. #fix = subfix
  497. spos = subfix.POS(40,60)
  498. srgb = subfix.rgb
  499. #print(fix.dmx,rgb,pos)
  500. pygame.draw.rect(window,srgb,spos)
  501. #pygame.draw.circle(window,rgb,(pos[0]+int(pos[2]/2),pos[1]+int(pos[3]/2)),int(pos[3]/2))
  502. #draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  503. # draw row/col grid number
  504. if subfix.pos[0] == 0:
  505. fr = font12.render("{}".format(v ) ,1, (200,200,200))
  506. window.blit(fr,(25,spos[1] ))
  507. v += 1
  508. if subfix.pos[1] == 0:
  509. fr = font12.render("{}".format(1) ,1, (200,200,200))
  510. fr = font12.render("{}".format(h ) ,1, (200,200,200))
  511. h+=1
  512. window.blit(fr,(spos[0],50 ))
  513. if NR:
  514. #fr = font15.render("{:02}".format(j+1) ,1, (0,200,255))
  515. fr = font15.render("{:02}".format(subfix._id) ,1, (250,200,5))
  516. window.blit(fr,(spos[0]+2,spos[1]+10))
  517. j += 1
  518. i += 1
  519. pointer.draw()
  520. # DRAW FIX NUMBER on TOP
  521. i=0
  522. for fix in GRID:
  523. pos = fix.POS(40,60)
  524. rgb = fix.rgb
  525. if NR:
  526. pygame.draw.rect(window,[0,0,0],[pos[0]+2,pos[1]+2,12,9])
  527. if NR == 1:
  528. fr = font15.render("{:02}".format(i+1) ,1, (200,0,255))
  529. window.blit(fr,(pos[0]+2,pos[1]+2))
  530. elif NR == 2:
  531. if counter +5 < time.time():
  532. counter = time.time()
  533. try:
  534. GRID = init_grid() #init_gird()
  535. except Exception as e:
  536. print("Except: grid re init",e)
  537. if fix._id != i+1:
  538. fr = font15.render("{:02}".format(fix._id) ,1, (255,255,0))
  539. else:
  540. fr = font15.render("{:02}".format(fix._id) ,1, (100,100,255))
  541. window.blit(fr,(pos[0]+2,pos[1]+2))
  542. i += 1
  543. pygame.display.flip()
  544. pg.time.wait(10)
  545. if __name__ == "__main__":
  546. main()