vpu_live.py 17 KB

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