vpu_live.py 18 KB

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