vpu_live.py 16 KB

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