vpu_live.py 17 KB

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