vpu_live.py 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709
  1. import math
  2. import random
  3. import time
  4. import os
  5. import _thread as thread
  6. from optparse import OptionParser
  7. ...
  8. parser = OptionParser()
  9. parser.add_option("-m", "--mode", dest="mode",
  10. help="pixel mode pix,x,y --mode 40,10,8") #, metavar="FILE")
  11. parser.add_option("-X", "--XX", dest="XX", #default=1,
  12. help="x-split") #, metavar="FILE")
  13. parser.add_option("-x", "--xx", dest="xsplit", #default=1,
  14. help="x-split") #, metavar="FILE")
  15. parser.add_option("-y", "--yy", dest="ysplit",#default=1,
  16. help="y-split") #, metavar="FILE")
  17. parser.add_option("", "--start-univ", dest="start_univ",#default=1,
  18. help="set start-univers default=2") #, metavar="FILE")
  19. parser.add_option("", "--gobo-ch", dest="gobo_ch",#default=1,
  20. help="gobo ch univ on 1") #, metavar="FILE")
  21. #os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (200,164)
  22. parser.add_option("", "--win-pos", dest="win_pos",default="200,164",
  23. help="SDL_VIDEO_WINDOW_POS --win-pos=200,164") #, metavar="FILE")
  24. parser.add_option("", "--pixel-mapping", dest="pixel_mapping",default=0,
  25. help="pixel_mapping file/on --pixel-mapping=_x") #, metavar="FILE")
  26. parser.add_option("", "--countdown", dest="countdown",#default=1,
  27. help="enable countdown") #, metavar="FILE")
  28. parser.add_option("", "--videoplayer", dest="videoplayer",#default=1,
  29. help="enable videoplayer") #, metavar="FILE")
  30. #parser.add_option("-f", "--file", dest="filename",
  31. # help="write report to FILE", metavar="FILE")
  32. #parser.add_option("-q", "--quiet",
  33. # action="store_false", dest="verbose", default=True,
  34. # help="don't print status messages to stdout")
  35. (options, args) = parser.parse_args()
  36. for o in dir(options):
  37. if "_" in o:
  38. continue
  39. print(o,options.__getattribute__(o))
  40. START = time.time()
  41. # ===== ARTNET DMX =========
  42. import memcache
  43. mc = memcache.Client(['127.0.0.1:11211'], debug=0)
  44. def read_index():
  45. ips=mc.get("index")#cmd)
  46. if ips is None:
  47. ips = {}
  48. #for k,v in ips.items():
  49. # print(k,v)
  50. return ips
  51. def select_ip(ips, univ=2): # artnet univ
  52. _univ = ":{}".format(univ)
  53. for ip in ips: #high priority
  54. if "2.0.0" in ip and _univ in ip:
  55. return ip
  56. for ip in ips:
  57. if "ltp-out" in ip and _univ in ip:
  58. return ip
  59. FUNC = 0
  60. COUNTER = []
  61. if options.countdown:
  62. cdmx_start = options.countdown.split(",")
  63. for cdmx in cdmx_start:
  64. try:
  65. cdmx = int(cdmx)
  66. COUNTER.append({"DMX":cdmx,"DIM":0,"PAN":127,"TILT":127,"CONTROL":0,"SEC":10,"RED":255,"GREEN":255,"BLUE":255,"_time":time.time(),"_RUN":0,"_SEC":">{}<".format(cdmx)})
  67. except Exception as e:
  68. print("EXCEPTION COUNTER INIT ",cdmx)
  69. def read_dmx(ip):
  70. global frame
  71. r = ""
  72. if ip:
  73. #t = int(math.sin(time.time() - s)*10)
  74. r = mc.get(ip) #"2.0.0.13:2")
  75. frame += 1
  76. rr = [0]*512
  77. for i,v in enumerate(r):
  78. try: #cleanup ltp-out to int
  79. v = int(v)
  80. rr[i] = v
  81. except:pass
  82. r = rr
  83. if not r:
  84. c = 0
  85. #time.sleep(0.1)
  86. r = [0] *512
  87. for i in range(12*8+1):
  88. dmx = i*4
  89. #print(dmx)
  90. r[dmx:dmx+4] = [255,10,10,40]
  91. return r
  92. # ===== ARTNET DMX =========
  93. PLAYLIST = []
  94. try:
  95. PLAYLIST = open_playlist()
  96. except:pass
  97. class Vopen():
  98. def __init__(self,dmx=None):
  99. global PLAYLIST
  100. self.fpath = '/home/user/Downloads/'
  101. self.fpath = '/home/user/LibreLight/video/'
  102. self.fname = 'bbb_sunflower_480x320.mp4'
  103. #self.fname = 'no-video.mp4'
  104. try:
  105. self.fname = PLAYLIST[0]
  106. except Exception as e:
  107. print("Exception set video from PLAYLIST 5543",e)
  108. self.time = 0
  109. self.t_delta = 0
  110. self.t_last = time.time()
  111. self.fps = 0
  112. self.scale = 50 #%
  113. self.dmx=dmx
  114. self.dim = 0
  115. self.x = 0
  116. self.y = 0
  117. self.init_count = 0
  118. self.cap = None
  119. self.shape = [200,200]
  120. self.img = None
  121. self.success = 1
  122. self.cv2 = None
  123. self._run = 0
  124. try:
  125. global cv2
  126. self.cv2 = cv2
  127. except:
  128. pass
  129. self.im = None
  130. self.pos = 0
  131. self.buffer = []
  132. self.init()
  133. def init(self):
  134. #self._init()
  135. thread.start_new_thread(self._init,())
  136. self.init_count = 1
  137. def _init(self):
  138. print(self)
  139. print("videoplayer.init()",self.fpath,self.fname)
  140. if not os.path.isfile(self.fpath+self.fname):
  141. print()
  142. print("video file does not exits !! >",self.fpath,self.fname)
  143. print()
  144. exit()
  145. if self.cv2:
  146. cap = self.cv2.VideoCapture(self.fpath+self.fname)
  147. self.cap = cap
  148. success = 1
  149. while success:
  150. try:
  151. success, self.img = self.cap.read()
  152. if self.fps == 0:
  153. self.fps = self.cap.get(cv2.CAP_PROP_FPS)
  154. #print("fps",self.fps)
  155. #self.img = self.cv2.cvtColor(self.img, self.cv2.COLOR_BGR2RGB)
  156. self.img = self.cv2.cvtColor(self.img, self.cv2.COLOR_BGR2RGB)
  157. self.img = self.rescale_frame2(self.img, 100)
  158. #ret, self.img = self.cv2.threshold(self.img, 100, 130, self.cv2.THRESH_BINARY) # treshold
  159. #self.img = self.cv2.Canny(self.img, 100, 200) # kanten
  160. #M = cv2.getPerspectiveTransform(Punkte_A, Punkte_B)
  161. #warped = cv2.warpPerspective(Bild, m, (420,594))
  162. #self.cv2.normalize(self.img, self.img, 0, self.dim, self.cv2.NORM_MINMAX)
  163. self.buffer.append(self.img)
  164. if len(self.buffer) % 100 == 0:
  165. _id = str(self.__repr__)[-5:-1]
  166. print(_id,"video read",self.dmx,len(self.buffer),self.fname,"fps",self.fps,self.dim)
  167. time.sleep(0.2)
  168. time.sleep(0.005)
  169. except Exception as e:
  170. print("Excetpion","_init",self,e)
  171. self.pos = 0
  172. self.img = self.buffer[int(self.pos)]
  173. self.success = 1
  174. def read(self):
  175. #print(self,"read()")
  176. if len(self.buffer) <= 0:
  177. return
  178. try:
  179. if self.pos >= len(self.buffer):
  180. self.pos = 0 #len(self.buffer)-1
  181. self.img = self.buffer[int(self.pos)]
  182. #self.img = self.cv2.cvtColor(self.img, self.cv2.COLOR_BGR2RGB)
  183. self.img = self.rescale_frame(self.img, percent=self.scale)
  184. if self._run:
  185. t = time.time()
  186. self.t_delta = t-self.t_last
  187. self.t_last = t
  188. self.pos += self.t_delta*self.fps
  189. else:
  190. t = time.time()
  191. self.t_delta = 0
  192. self.t_last = t
  193. if self.pos >= len(self.buffer):
  194. self.pos = 0 #len(self.buffer)-1
  195. #print("video.read",self.pos)
  196. self.shape = self.img.shape[1::-1]
  197. except Exception as e:
  198. print("exception 432",e,len(self.buffer),self.fname)
  199. #self.init()
  200. def prev(self):
  201. self.pos -= 1
  202. if self.pos < 0:
  203. self.pos = len(self.buffer)-1
  204. if self.pos >= len(self.buffer):
  205. self.pos = len(self.buffer)-1
  206. self.im = self.buffer[int(self.pos)]
  207. def rescale_frame2(self,frame, width):
  208. height = int(frame.shape[0]/frame.shape[1] * width )
  209. dim = (width, height)
  210. #print("rescale_frame2",dim)
  211. #sys.exit()
  212. return self.cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)
  213. def rescale_frame(self,frame, percent=75):
  214. width = int(frame.shape[1] * percent/ 100)
  215. height = int(frame.shape[0] * percent/ 100)
  216. dim = (width, height)
  217. return self.cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)
  218. def next(self):
  219. #print(self,"next",time.time())
  220. #self.success = 0
  221. self.read()
  222. try:
  223. if len(self.buffer) % 100 == 0:
  224. #print("video pos",self.pos)
  225. _id = str(self.__repr__)[-5:-1]
  226. print(_id,"video pos",self.dmx,self.pos,len(self.buffer),self.fname)
  227. # add DIMER to videplayer
  228. self.cv2.normalize(self.img, self.img, 0, self.dim, self.cv2.NORM_MINMAX)
  229. img = self.img #self.rescale_frame(self.img, percent=30)
  230. if img is None:
  231. return
  232. self.im = pygame.image.frombuffer(img.tobytes(), self.shape, "RGB")
  233. #print(type(self.im))
  234. #self.buffer.append(self.im)
  235. #self.pos = int(self.time * self.fps)
  236. if self._run:
  237. t = time.time()
  238. self.t_delta = t-self.t_last
  239. self.t_last = t
  240. self.pos += self.t_delta*self.fps
  241. else:
  242. t = time.time()
  243. self.t_delta = 0
  244. self.t_last = t
  245. #if self._run:
  246. # self.pos += 2
  247. if self.pos > len(self.buffer):
  248. self.pos = 0
  249. # wn.blit(im, (self.x, self.y))
  250. #self.success = 1
  251. except AttributeError as e:
  252. time.sleep(.05)
  253. #if self.init_count % 100 == 0:
  254. print("except 776",e)
  255. #self.init()
  256. except Exception as e:
  257. print("except 756",e)
  258. def draw(self,wn=None):
  259. if self.success and wn and self.im: # is not None:
  260. wn.blit(self.im, (self.x, self.y))
  261. def overlay(self,wn=None,mode="x"):
  262. # overlay
  263. img_shape = [100,100]
  264. if type(self.img) is list:
  265. img_shape = self.img.shape
  266. pygame.draw.rect(wn,[255,200,0],[5+self.x-3,4+self.y-1+img_shape[0],140,20])
  267. font15 = pygame.font.SysFont("freemonobold",17)
  268. fr = font15.render(">:{}".format(mode) ,1, (0,0,0))
  269. wn.blit(fr,(3+self.x,4+self.y+img_shape[0]))
  270. fr = font15.render("FRAME:{}".format(self.pos) ,1, (0,0,0))
  271. wn.blit(fr,(45+self.x,4+self.y+img_shape[0]))
  272. VIDEO = []
  273. videoplayer=[]
  274. cv2 = None
  275. if type(options.videoplayer) is str:
  276. try:
  277. import cv2
  278. except:pass
  279. max_videoplayer = 2
  280. dmx_start = options.videoplayer.split(",")
  281. for cdmx in dmx_start:
  282. if len(videoplayer) > max_videoplayer:
  283. print("-- videoplayer max count {} !! break".format(max_videoplayer))
  284. break
  285. print("-- videoplayer dmx:",cdmx)
  286. try:
  287. cdmx = int(cdmx)
  288. videoplayer.append( Vopen(cdmx) )
  289. VIDEO.append({"DMX":cdmx,"DIM":0,"PAN":127,"TILT":127,"CONTROL":0,"SEC":10,"RED":255,"GREEN":255,"BLUE":255,"_time":time.time(),"_RUN":0,"_SEC":">{}<".format(cdmx)})
  290. except Exception as e:
  291. print("EXCEPTION COUNTER INIT ",cdmx)
  292. # ===== ======
  293. p = 16
  294. block = [p,p]
  295. _x = 8
  296. _y = 8
  297. #HD = "0"
  298. if options.mode:
  299. try:
  300. HD = options.mode
  301. p,_x,_y = HD.split(",")
  302. _x = int(_x)
  303. _y = int(_y)
  304. p = int(p)
  305. block = [p,p]
  306. except Exception as e:
  307. print( "Exc",options.mode,e)
  308. HD_x = 2
  309. HD_y = 2
  310. print( [options.xsplit])
  311. print( [options.ysplit])
  312. try:
  313. if options.xsplit:
  314. HD_x = int(options.xsplit)
  315. if options.ysplit:
  316. HD_y = int(options.ysplit)
  317. except Exception as e:
  318. print( "Exc",options.mode,e)
  319. print("HD",HD_x,HD_y)
  320. print("xy",_x,_y)
  321. print("++++++++++++++++++", p,_x,_y)
  322. _x2 = _x
  323. try:
  324. if options.XX:
  325. pass#_x2 = int(options.XX)
  326. except Exception as e:
  327. print( "Exc",options.mode,e)
  328. print("_x2 , -X",_x2)
  329. # ===== GUI =========
  330. import pygame
  331. import pygame.gfxdraw
  332. import pygame.font
  333. clock = pygame.time.Clock()
  334. os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (200,164)
  335. if options.win_pos:
  336. if "," in options.win_pos:
  337. win_pos = options.win_pos.split(",")
  338. try:
  339. WIN_POS = '%i,%i' % (int(win_pos[0]),int(win_pos[1]) )
  340. os.environ['SDL_VIDEO_WINDOW_POS'] = WIN_POS
  341. except Excetpion as e:
  342. print("win_pos",win_pos,e)
  343. os.environ['SDL_VIDEO_CENTERED'] = '0'
  344. pg = pygame
  345. pygame.init()
  346. pygame.mixer.quit()
  347. f = pygame.font.get_fonts()
  348. for i in f:
  349. if "mono" in i.lower():
  350. print(i)
  351. font = pygame.font.SysFont("freemonobold",22)
  352. font10 = pygame.font.SysFont("freemonobold",10)
  353. font12 = pygame.font.SysFont("freemonobold",12)
  354. font15 = pygame.font.SysFont("freemonobold",15)
  355. font40 = pygame.font.SysFont("freemonobold",40)
  356. font80 = pygame.font.SysFont("freemonobold",70)
  357. #font = pygame.font.SysFont(None,30)
  358. fr = font.render("hallo" ,1, (200,0,255))
  359. PIXEL_MAPPING = 0
  360. grid_file = "/tmp/vpu_grid_hd.csv"
  361. text_file = "/home/user/LibreLight/vpu_text_hd.csv"
  362. play_list = "/tmp/vpu_playlist_hd.csv"
  363. pm_wy = 0
  364. if options.pixel_mapping:
  365. PIXEL_MAPPING = 1
  366. path = options.pixel_mapping
  367. path = path.replace("/","-")
  368. path = path.replace(".","-")
  369. path = path.replace("\"","-")
  370. path = path.replace("'","-")
  371. grid_file = "/home/user/LibreLight/vpu_grid_hd{}.csv".format(path)
  372. text_file = "/home/user/LibreLight/vpu_text_hd{}.csv".format(path)
  373. play_list = "/home/user/LibreLight/vpu_playlist_hd{}.csv".format(path)
  374. #_x = 8
  375. #_y = 8
  376. print(" ",[options.pixel_mapping],"grid_file",grid_file)
  377. #grid_file = "/home/user/LibreLight/vpu_grid_hd.csv"
  378. main_size=(600,500)
  379. try:
  380. if _x < 8 and PIXEL_MAPPING >= 1:
  381. wx = 60+block[0] * 8
  382. else:
  383. wx = 60+block[0] * _x
  384. wy = 80+block[1] * _y
  385. main_size=(wx,wy)
  386. if PIXEL_MAPPING >= 1:
  387. pm_wy = 120+block[0] * 8
  388. main_size=(wx,wy+pm_wy)
  389. except Exception as e:
  390. print("Exception:",e)
  391. #main_size=(280,200)
  392. main_size = (main_size[0],main_size[1])
  393. window = pygame.display.set_mode(main_size,pg.RESIZABLE)#,32)#,pygame.FULLSCREEN) #x left->right ,y top-> bottom
  394. pg.display.set_caption('LibreLight VPU-SCREEN')
  395. class Fix():
  396. def __init__(self,_id,pos,block=[16,16],univ=0,dmx=0,ch=4):
  397. #print("Fix",_id)
  398. self._id = _id
  399. self.dmx = (_id-1) * ch +1 #dmx
  400. self.univ = univ
  401. self.ch = ch
  402. self.pos = pos
  403. self.rgb = [0,0,0]
  404. self.block = block #[10,10]
  405. self.x = pos[0]
  406. self.y = pos[1]
  407. self.strobo = time.time()
  408. self.bmp = 250
  409. self.sub_fix = []
  410. sub_block =[block[0]/HD_x,block[1]/HD_y]
  411. if _id <= 0: #exit
  412. return
  413. spalte = (_id-1)%_y +1
  414. zeile = int((_id-1)/_x2) #+1
  415. #zeile = zeile*_x*HD_x*HD_y
  416. add_row = _x*HD_x*HD_y
  417. #zeile 1
  418. sid = (_id-1)*2 + zeile*HD_x*_x2
  419. #for i in range(1,HD_x):
  420. sid = sid+1
  421. #sid = zeile
  422. sub_pos= [pos[0]*block[0],pos[1]*block[1]]
  423. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  424. self.sub_fix.append(sub_fix)
  425. sid = sid+1
  426. #sid = zeile
  427. sub_pos= [pos[0]*block[0]+block[0]/2,pos[1]*block[1]]
  428. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  429. self.sub_fix.append(sub_fix)
  430. #zeile 2
  431. sid = (_id-1)*2+1 + _x2*HD_x + zeile*HD_x*_x2 # int(add_row)
  432. #sid = sid+1
  433. #sid = HD_x
  434. sub_pos= [pos[0]*block[0],pos[1]*block[1]+block[1]/2]
  435. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  436. self.sub_fix.append(sub_fix)
  437. #sid = sid+1
  438. sid = sid+1
  439. sub_pos= [pos[0]*block[0]+block[0]/2,pos[1]*block[1]+block[1]/2]
  440. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  441. self.sub_fix.append(sub_fix)
  442. def calc(self,data):
  443. _rgb = [0,255,0]
  444. return _rgb
  445. def sub_calc(self,data):
  446. _rgb = [0,255,0]
  447. for sub_fix in self.sub_fix:
  448. sub_fix.block = self.block[:]
  449. _rgb = sub_fix.calc(data)
  450. return _rgb
  451. def POS(self,x=0,y=0,a=0,b=0):
  452. A = (self.pos[0])*self.block[0]
  453. B = (self.pos[1])*self.block[1]
  454. C = self.block[0]-a
  455. D = self.block[1]-b
  456. return [x+A,y+B,C,D]
  457. def subPOS(self,x=0,y=0,a=0,b=0):
  458. __out = []
  459. for sub_fix in self.sub_fix:
  460. __out.append( sub_fix.POS(x,y,a,b) )
  461. return __out
  462. class SubFix():
  463. def __init__(self,_id,pos,block=[16,16],univ=0,dmx=0,ch=4):
  464. #print("Fix",_id)
  465. self._id = _id
  466. self.dmx = (_id-1) * ch +1 #dmx
  467. self.univ = univ
  468. self.ch = ch
  469. self.pos = pos
  470. self.rgb = [0,0,40]
  471. self.block = block #[10,10]
  472. self.x = pos[0]
  473. self.y = pos[1]
  474. self.strobo = time.time()
  475. self.bmp = 250
  476. def calc(self,data):
  477. #return [130,30,20]
  478. dmx_sub = [30]*10
  479. #print(dmx_sub)
  480. dmx = self.dmx -1
  481. _dmx_sub = []
  482. if self.dmx >= 0:
  483. dmx = rDMX(self.univ,self.dmx)-1
  484. if dmx+self.ch < len(data):
  485. _dmx_sub = data[dmx:dmx+self.ch]
  486. if _dmx_sub:
  487. dmx_sub = _dmx_sub
  488. #print(dmx_sub)
  489. dim = dmx_sub[0]/255
  490. #print("dmx",dmx,dmx_sub)
  491. r = dmx_sub[1]*dim
  492. g = dmx_sub[2]*dim
  493. b = dmx_sub[3]*dim
  494. r = int(r)
  495. g = int(g)
  496. b = int(b)
  497. self.rgb = [r,g,b]
  498. return self.rgb
  499. def POS(self,x=0,y=0,a=0,b=0):
  500. A = (self.pos[0]) #+self.block[0]
  501. B = (self.pos[1]) #+self.block[1]
  502. C = self.block[0]-a
  503. D = self.block[1]-b
  504. if NR:
  505. C-=1
  506. D-=1
  507. return [int(x+A),int(y+B),int(C),int(D)]
  508. class POINTER():
  509. def __init__(self):
  510. self.pos = [0,0,0,0]
  511. self.on = 0
  512. self.rgb = [0,100,10]
  513. self._x = 0
  514. self._y = 0
  515. self.x = 0
  516. self.y = 0
  517. self.fix = Fix(0 ,[999,999],[16,16],0,0,0)
  518. def row_move(self,x,y):
  519. self._x = x
  520. self._y = y
  521. def move(self,pos):
  522. self.pos = pos
  523. self.on = 1
  524. def cross(self,x,y):
  525. self.x = x
  526. self.y = y
  527. def draw(self,x,y):
  528. pos = self.pos[:]
  529. #print("draw",x,y,pos)
  530. pos[0] += x
  531. pos[1] += y
  532. fix_x= self.fix.x
  533. fix_y= self.fix.y +y
  534. #print("draw",x,y,pos)
  535. if self.on:
  536. pygame.draw.rect(window,self.rgb,pos)
  537. #pygame.draw.line(window,self.rgb, (pos[0],pos[1]) , (pos[0]+100,pos[1]) )
  538. # mouse grid posision
  539. fr = font15.render("{}/{}".format(fix_x+1,fix_y) ,1, (200,200,200))
  540. _nr = fix_y * _x + fix_x +1
  541. #fr = font15.render("{:02} {}/{}".format(_nr, fix_x+1,fix_y+1 ) ,1, (200,200,200))
  542. fr = font15.render("{:02}".format(_nr ) ,1, (200,200,200))
  543. window.blit(fr,(pos[0]+2,pos[1]+2 ))
  544. window.blit(fr,(130,1))
  545. # fix pos
  546. txt=str(pos) #"[0, 0, 0, 0]"
  547. fr = font15.render(txt ,1, (200,200,200))
  548. #window.blit(fr,(pos[0]+2,pos[1]+2 ))
  549. window.blit(fr,(10,1))
  550. # univers
  551. #fr = font15.render("{:02}:{:03}".format(fix.univ,fix.dmx) ,1, (200,200,200))
  552. #window.blit(fr,(300,10))
  553. # pointer
  554. fr = font15.render("X:{:03}".format(self._x) ,1, (200,200,200))
  555. window.blit(fr,(10,30))
  556. fr = font15.render("Y:{:03}".format(self._y) ,1, (200,200,200))
  557. window.blit(fr,(10,40))
  558. # crosshair
  559. self.rgb = [0,0,200]
  560. pygame.draw.line(window,self.rgb, (self.x-p,self.y) , (self.x-2,self.y),4 )
  561. pygame.draw.line(window,self.rgb, (self.x,self.y-p) , (self.x,self.y-2),4 )
  562. self.rgb = [0,200,0]
  563. pygame.draw.line(window,self.rgb, (self.x+2,self.y) , (self.x+p,self.y),4 )
  564. pygame.draw.line(window,self.rgb, (self.x,self.y+2) , (self.x,self.y+p),4 )
  565. self.rgb = [200,0,0]
  566. pointer = POINTER()
  567. NR = 0
  568. running = True
  569. def event():
  570. global NR,running
  571. for event in pygame.event.get():
  572. #print(event.dict)
  573. _button = None
  574. if "button" in event.dict:
  575. _button = event.dict["button"]
  576. _state = None
  577. if "state" in event.dict:
  578. _state = event.state
  579. _key = None
  580. if "key" in event.dict:
  581. _key = event.key
  582. _pos = None
  583. if "pos" in event.dict:
  584. _pos = event.pos
  585. _type = None
  586. if "type" in event.dict:
  587. _type = event.type
  588. _type = event.type
  589. _mod = None
  590. if "mod" in event.dict:
  591. _mod = event.mod
  592. if 0:
  593. print( " ")
  594. print( "{:.02f}".format( time.time() - START ))
  595. print("button -",_button,end="\t| ")
  596. #print("state -",_state)
  597. print("pos -",_pos)
  598. print("type -",_type, end="\t| ")
  599. print("key -",_key)
  600. print("mod -",_mod)
  601. try:
  602. if _type == 5:
  603. if _button == 1:
  604. NR += 1
  605. if NR > 1:
  606. NR = 0
  607. if _button == 3:
  608. NR -= 1
  609. if NR < 0:
  610. NR = 1
  611. if _pos:
  612. posA = _pos
  613. fix = find_pix(_pos[0]-40,_pos[1]-60+pm_wy)
  614. if fix:
  615. pos = fix.POS(40,60+pm_wy)
  616. rgb = [0,0,0]
  617. pointer.move(pos)
  618. pointer.fix = fix
  619. else:
  620. pointer.on = 0
  621. pointer.row_move(_pos[0],_pos[1])
  622. pointer.cross(_pos[0],_pos[1])
  623. if event.type == pygame.VIDEORESIZE:
  624. window = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)
  625. except Exception as e:
  626. print(e)
  627. if event.type==pygame.QUIT:
  628. running=False
  629. fps = 0
  630. frame = 0
  631. frame_t = time.time()
  632. IP = "yyy"
  633. def draw_overlay():
  634. global fps
  635. fr = font.render("FPS:{}".format(fps) ,1, (200,0,255))
  636. window.blit(fr,(10,10))
  637. fr = font.render("ip:{}".format(IP) ,1, (200,0,255))
  638. window.blit(fr,(80,10))
  639. def calc_fps():
  640. global fps,frame,frame_t
  641. t = time.time()
  642. if frame_t+1 < t:
  643. fps = frame #frame_t- t #frame
  644. frame = 1
  645. frame_t = time.time()
  646. TEXT_BLOCK = []
  647. def _create_text_block():
  648. print("======== CREATE NEW TEXT FILE !!",text_file)
  649. f = open(text_file,"w")
  650. for i in range(10):
  651. f.write("TEXT {}\n".format(i+1))
  652. f.close()
  653. def open_text_block():
  654. print("======== OPEN TEXT FILE !!",text_file)
  655. _lines = []
  656. try:
  657. f = open(text_file,"r")
  658. _lines = f.readlines()
  659. f.close()
  660. except FileNotFoundError as e:
  661. print("TEXT",e)
  662. _create_text_block()
  663. if len(_lines) <= 0:
  664. _create_text_block()
  665. lines = []
  666. for l in _lines:
  667. #print(">> ",l.strip())
  668. lines.append(l.strip())
  669. if len(lines) <= 10:
  670. for i in range(10-len(lines)):
  671. lines.append("LINE ERROR")
  672. return lines
  673. TEXT_BLOCK = open_text_block()
  674. TEXT_BLOCK_TIME = time.time()
  675. # video playlist
  676. PLAYLIST = []
  677. def _create_playlist():
  678. print("======== CREATE NEW PLAYLIST FILE !!",play_list)
  679. os.system("mkdir -p /home/user/LibreLight/video")
  680. f = open(play_list,"w")
  681. f.write("bbb_sunflower_480x320.mp4\n")
  682. for i in range(10-1):
  683. f.write("Video-file {}\n".format(i+1+1))
  684. f.close()
  685. def open_playlist():
  686. print("======== OPEN PLAYLIST FILE !!",play_list)
  687. _lines = []
  688. try:
  689. f = open(play_list,"r")
  690. _lines = f.readlines()
  691. f.close()
  692. except FileNotFoundError as e:
  693. print("TEXT",e)
  694. _create_playlist()
  695. if len(_lines) <= 0:
  696. _create_playlist()
  697. lines = []
  698. for l in _lines:
  699. print(">> ",l.strip())
  700. lines.append(l.strip())
  701. if len(lines) <= 10:
  702. for i in range(10-len(lines)):
  703. lines.append("LINE ERROR")
  704. return lines
  705. PLAYLIST_TIME = time.time()
  706. PLAYLIST = open_playlist()
  707. # ===== GUI =========
  708. #def draw_circle(surface, x, y, radius, color):
  709. def draw_circle(surface,color, pos, radius):
  710. x,y=pos
  711. pygame.gfxdraw.aacircle(surface, int(x), int(y), radius-1, color)
  712. pygame.gfxdraw.filled_circle(surface, int(x), int(y), radius-1, color)
  713. def rDMX(univ,dmx):
  714. return univ*512+dmx
  715. def generate_grid(mapping=0):
  716. _log = []
  717. #if PIXEL_MAPPING:
  718. # log = open(grid_file,"w")
  719. head = "i,univ,dmx,x,y,ch\n"
  720. head = "i,univ,dmx,ch\n"
  721. head = "univ,dmx,x,y,ch\n"
  722. head = "nr,id,info\n"
  723. print("csv:",head)
  724. #if PIXEL_MAPPING:
  725. # log.write(head)
  726. _log.append(head)
  727. dmx = 1-1
  728. ch = 4
  729. y=0
  730. x=0
  731. #for i in range((_y)*(_x)):
  732. for i in range((8)*(8)):
  733. #if x > _x and i%_x == 0:
  734. if x > 8 and i%8 == 0:
  735. #print("--> -->")
  736. x=0
  737. y+=1
  738. _univ = int(dmx/512)
  739. _dmx = dmx - (_univ)*512
  740. pos=[x,y]
  741. line="{},{},{},{},{},{}\n".format(i+1,_univ,_dmx+1,pos[0],pos[1],ch)
  742. line="{},{},{},{},{}\n".format(_univ,_dmx+1,x,y,ch)
  743. line="{},{},x\n".format(i+1,i+1)
  744. #print("wcsv:",[line])
  745. #if PIXEL_MAPPING:
  746. # log.write(line)
  747. _log.append(line)
  748. dmx += ch
  749. x+=1
  750. if mapping and PIXEL_MAPPING:
  751. print("CREATE NEW PIXELMAP FILE !!",grid_file)
  752. log = open(grid_file,"w")
  753. log.writelines(_log)
  754. log.close()
  755. return _log[:] #GRID
  756. def init_grid(mapping=0,_x=4,_y=4):
  757. if mapping and PIXEL_MAPPING:
  758. try:
  759. log = open(grid_file,"r")
  760. except:
  761. generate_grid(mapping=mapping)
  762. log = open(grid_file,"r")
  763. lines = log.readlines()
  764. else:
  765. lines = generate_grid()
  766. GRID = []
  767. y=0
  768. x=0
  769. print("CSV header",[lines[0]],[PIXEL_MAPPING])
  770. for i,line in enumerate(lines[1:]): #exclude first line
  771. #print("rcsv",[line])
  772. line = line.strip()
  773. line = line.split(",") # csv
  774. if i >= _x and i%_x == 0:
  775. x=0
  776. y+=1
  777. if y >= _y:
  778. break
  779. #i = int(line[0])
  780. _id = int(line[1])
  781. #univ = int(line[0])
  782. #dmx = int(line[1])
  783. #x = int(line[3])
  784. #y = int(line[4])
  785. #ch = int(line[4])
  786. pos = [x,y]
  787. f = Fix(_id,pos,block) #pos,univ,dmx,ch)
  788. #f.x = x
  789. #f.y = y
  790. #f.block = block
  791. GRID.append(f)
  792. x+=1
  793. #print("y, _y",y,_y)
  794. return GRID
  795. def find_pix(x,y):
  796. global GRID
  797. for fix in GRID:
  798. X = 0
  799. Y = 0
  800. pos = fix.POS()
  801. #pos = fix.POS()
  802. #pos = fix.POS(40,60-pm_wy)
  803. #pos = fix.POS(x,y+pm_wy)#40,60)
  804. if x > pos[0] and x < pos[0]+pos[2]:
  805. X = 1
  806. if y > pos[1] and y < pos[1]+pos[3]:
  807. Y = 1
  808. if X and Y:
  809. #print(pos,x,y)
  810. #print("find",X,Y)
  811. return fix
  812. GRID = []
  813. _GRID = []
  814. _GRID = init_grid(_x=8,_y=8,mapping=1) #init_gird()
  815. NR = 0
  816. START_UNIV=2
  817. if options.start_univ:
  818. try:
  819. START_UNIV=int(options.start_univ)
  820. except Exception as e:
  821. print("Exception START UNIV",e)
  822. gobo_ch=1
  823. if options.gobo_ch:
  824. try:
  825. gobo_ch=int(options.gobo_ch)
  826. except Exception as e:
  827. print("Exception gobo_ch",e)
  828. if gobo_ch <= 0:
  829. gobo_ch = 1
  830. def draw_box(pos1,pos2,color=[128,128,128],text=1):
  831. color = [200,0,0,127]
  832. if text:
  833. fr = font15.render("A" ,1, (200,200,200))
  834. window.blit(fr,pos1)
  835. fr = font15.render("B" ,1, (200,200,200))
  836. window.blit(fr,[pos2[0]-10,pos2[1]-10])
  837. # h unten
  838. _pos1 = [pos1[0],pos2[1]]
  839. _pos2 = [pos2[0],pos2[1]]
  840. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  841. color = [255,255,0,127]
  842. # h rechts
  843. _pos1 = [pos2[0],pos1[1]]
  844. _pos2 = [pos2[0],pos2[1]]
  845. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  846. color = [0,200,0,127]
  847. # h links
  848. _pos1 = [pos1[0],pos1[1]]
  849. _pos2 = [pos1[0],pos2[1]]
  850. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  851. color = [0,0,200,127]
  852. # h oben
  853. _pos1 = [pos1[0],pos1[1]]
  854. _pos2 = [pos2[0],pos1[1]]
  855. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  856. def grab(x=55,y=55,w=60,h=60):
  857. # usage
  858. # sub = grab()
  859. # window.blit(sub, (500,10))
  860. crop = None
  861. rect = pygame.Rect(x, y, w, h)
  862. try:
  863. sub = window.subsurface(rect)
  864. #pixArray = pygame.PixelArray(screen)
  865. crop = pygame.Surface((w,h))
  866. crop.blit(sub, (0,0))
  867. except ValueError as e:
  868. pass#print("exception",e,"line715")
  869. #except Exception as e:
  870. # print("exception",e,"line715")
  871. #print(rect)
  872. return crop
  873. def reshape(_x,_y):
  874. """reshape LED-WALL Block/Pixel mapping"""
  875. if PIXEL_MAPPING <= 0:
  876. return None
  877. global GRID
  878. global _GRID
  879. x = _x
  880. y = _y
  881. i = 0
  882. counter = 0
  883. z=0
  884. x_min = 99999
  885. x_max = 0
  886. y_min = 99999
  887. y_max = 0
  888. fr = font.render("OUTPUT MAP".format(t1.get()) ,1, (255,255,255))
  889. fr_r = fr.get_rect(center=(x+int(wx/2),y+pm_wy-5))
  890. #window.blit(fr,(x+int(wx/2),y+pm_wy))
  891. window.blit(fr,fr_r)
  892. fr = font.render("↑ ↑ ↑".format(t1.get()) ,1, (255,255,255))
  893. fr_r = fr.get_rect(center=(x+int(wx/2),y+pm_wy+15))
  894. #window.blit(fr,(x+int(wx/2),y+pm_wy))
  895. window.blit(fr,fr_r)
  896. fr = font.render("INPUT".format(t1.get()) ,1, (255,255,255))
  897. fr_r = fr.get_rect(center=(x+int(wx/2),y+pm_wy+35))
  898. #window.blit(fr,(x+int(wx/2),y+pm_wy))
  899. window.blit(fr,fr_r)
  900. j = 0
  901. for fix in _GRID:
  902. if j >= 8*8: # max output size
  903. break
  904. #if j >= _x*_y: # max input size
  905. # break
  906. j+=1
  907. ii = i
  908. #z= i # helping border offset
  909. pos = fix.POS(40,60)
  910. rgb = fix.rgb
  911. # green
  912. pygame.draw.rect(window,[0,40,0],pos)
  913. xposs = [] #None #pos[:]
  914. for fix2 in GRID:
  915. if fix._id == fix2._id:
  916. xposs.append( fix2.POS(40,60) )
  917. for xpos in xposs:
  918. sub = grab(xpos[0],xpos[1]+pm_wy,xpos[2],xpos[3])
  919. if sub:
  920. if 1:#j <= _x*_y: # max input size
  921. window.blit(sub, (x+pos[0]+z,y+pos[1]+z))
  922. else:
  923. # red
  924. pygame.draw.rect(window,[40,0,0],pos) #[x+pos[0]+2+z,y+pos[1]+2+z-pm_wy,12,9])
  925. if xpos[0] < x_min:
  926. x_min = xpos[0]
  927. if xpos[0] > x_max:
  928. x_max += xpos[2]
  929. if xpos[1] < y_min:
  930. y_min = xpos[1]
  931. if xpos[1] > x_max:
  932. y_max += xpos[3]
  933. # DRAW FIX NUMBER on TOP
  934. #apos = pos
  935. #argb = rgb
  936. apos = fix.POS(40,60+pm_wy)
  937. argb = fix.rgb
  938. # overwrite number overlay
  939. if NR:
  940. #pygame.draw.rect(window,[30,40,0],apos)
  941. pygame.draw.rect(window,[20,40,0],[x+apos[0]+2+z,y+apos[1]+2+z-pm_wy,12,9])
  942. if NR:# == 2:
  943. if fix._id != i+1:
  944. fr = font15.render("{:02}".format(fix._id) ,1, (255,255,0))
  945. window.blit(fr,(x+apos[0]+2+z,y+apos[1]+2+z-pm_wy))
  946. #print(fix._id,xposs,pos)
  947. else:
  948. fr = font15.render("{:02}".format(fix._id) ,1, (100,100,255))
  949. window.blit(fr,(x+apos[0]+2+z,y+apos[1]+2+z-pm_wy))
  950. i += 1
  951. #print("--#")
  952. # frame box
  953. #pygame.draw.box(window,[100,0,0],[x+x_min,y+x_min,x_max+x_min,y_min+y_max])
  954. pos1= [x+x_min,y+x_min]
  955. pos2= [x_max+x_min,y_min+y_max]
  956. #draw_box(pos1,pos2,text=0)
  957. class Timer():
  958. def __init__(self,start=120):
  959. self.start = start
  960. self.timer = self.start
  961. self.timer_t = time.time()
  962. def reset(self):
  963. self.timer = self.start
  964. def get(self):
  965. self.timer -= time.time()-self.timer_t
  966. self.timer_t = time.time()
  967. if self.timer <= 0:
  968. self.reset()
  969. return self.timer
  970. def reload_grid():
  971. print("==== reload_grid")
  972. global GRID
  973. global _GRID
  974. try:
  975. GRID = init_grid(_x=_x,_y=_y) #init_gird()
  976. _GRID = init_grid(_x=8,_y=8,mapping=1) #init_gird()
  977. except Exception as e:
  978. print("Except: grid re init",e)
  979. VPU_TEXT = []
  980. def load_vpu_text(nr=0):
  981. txt = "NONE"
  982. if not VPU_TEXT:
  983. pass #create VPU_TEXT_FILE
  984. if len(VPU_TEXT) > nr:
  985. txt = VPU_TEXT[0]
  986. return txt
  987. grid_counter = time.time()
  988. def draw_fix_nr(GRID):
  989. global grid_counter
  990. i=0
  991. y=0
  992. for fix in GRID:
  993. pos = fix.POS(40,60+pm_wy)
  994. rgb = fix.rgb
  995. if NR:
  996. pygame.draw.rect(window,[0,0,0],[pos[0]+2,pos[1]+2,12,9])
  997. if fix._id%_x-1 == 0: # line break border
  998. pygame.draw.line(window,[255,255,0],(pos[0],pos[1]+4),(pos[0],pos[1]+pos[3]-4),1)
  999. pygame.draw.line(window,[255,255,0],(pos[0],pos[1]+int(pos[3]/2)),(pos[0]+int(pos[2]/2),int(pos[1]+pos[3]/2)),1)
  1000. if fix._id%_x == 0: # line break border
  1001. pygame.draw.line(window,[255,255,255],(pos[0]+pos[2]-1,pos[1]+4),(pos[0]+pos[2]-1,pos[1]+pos[3]-4),1)
  1002. pygame.draw.line(window,[255,255,255],(pos[0]+pos[2]-1,int(pos[1]+pos[3]/2)),(pos[0]+int(pos[2]/2-1),int(pos[1]+pos[3]/2)),1)
  1003. if grid_counter +5 < time.time():
  1004. grid_counter = time.time()
  1005. reload_grid()
  1006. if fix._id != i+1:
  1007. fr = font15.render("{:02}".format(fix._id) ,1, (255,255,0))
  1008. else:
  1009. fr = font15.render("{:02}".format(fix._id) ,1, (100,100,255))
  1010. window.blit(fr,(pos[0]+2,pos[1]+2))
  1011. i += 1
  1012. def draw_counter(COUNTER):
  1013. for count in COUNTER:
  1014. cpan = 0
  1015. ctilt = 0
  1016. cr=255
  1017. cg=255
  1018. cb=255
  1019. csize=10
  1020. cdim=0
  1021. k = "DIM"
  1022. if k in count:
  1023. cdim = int(count[k])
  1024. k = "RED"
  1025. if k in count:
  1026. cr = int(count[k])
  1027. k = "GREEN"
  1028. if k in count:
  1029. cg = int(count[k])
  1030. k = "BLUE"
  1031. if k in count:
  1032. cb = int(count[k])
  1033. k = "SIZE"
  1034. if k in count:
  1035. csize = int(count[k])
  1036. if csize < 5:
  1037. csize = 5
  1038. k = "PAN"
  1039. if k in count:
  1040. cpan = int(count[k])/255*(block[0] *(_x))
  1041. cpan = int(cpan)
  1042. k = "TILT"
  1043. if k in count:
  1044. ctilt = int(count[k])/255*(block[1] *(_y))
  1045. ctilt = int(ctilt)
  1046. ddim = cdim/255
  1047. if "DIM" in count and count["DIM"] > 0:
  1048. tmp_font = pygame.font.SysFont("freemonobold",int(block[0]/100*csize))
  1049. rgb =(int(cr*ddim),int(cg*ddim),int(cb*ddim),cdim)
  1050. _sec = count["_SEC"]
  1051. try:
  1052. _sec = int(count["_SEC"])
  1053. except:
  1054. pass
  1055. if type(_sec) is int:
  1056. #print(_sec)
  1057. if _sec <= 60:
  1058. fr = tmp_font.render("{:0}".format(_sec) ,1, rgb)
  1059. else:
  1060. _xx = time.strftime("%M:%S",time.localtime(_sec))
  1061. #print("_xx",_xx)
  1062. fr = tmp_font.render("{}".format(_xx) ,1, rgb)
  1063. else:
  1064. fr = tmp_font.render("{}".format((count["_SEC"])) ,1, rgb)
  1065. fr_r = fr.get_rect(center=(60+cpan-(block[0]),60+ctilt+pm_wy))
  1066. pygame.draw.rect(window,[0,0,0],fr_r)
  1067. window.blit(fr,fr_r)
  1068. def draw_video(VIDEO):
  1069. global videplayer
  1070. i = 0
  1071. for count in VIDEO:
  1072. cpan = 0
  1073. ctilt = 0
  1074. cr=255
  1075. cg=255
  1076. cb=255
  1077. csize=10
  1078. cdim=0
  1079. video1 = videoplayer[i]
  1080. k = "DIM"
  1081. if k in count:
  1082. cdim = int(count[k])
  1083. video1.dim = cdim
  1084. #if i == 0:
  1085. # print(i,cdim)
  1086. k = "SIZE"
  1087. if k in count:
  1088. csize = int(count[k])
  1089. if csize < 5:
  1090. csize = 5
  1091. k = "PAN"
  1092. if k in count:
  1093. cpan = int(count[k])/255*(block[0] *(_x))
  1094. cpan = int(cpan)
  1095. k = "TILT"
  1096. if k in count:
  1097. ctilt = int(count[k])/255*(block[1] *(_y))
  1098. ctilt = int(ctilt)
  1099. k = "_reset"
  1100. if k in count:
  1101. if count[k]:
  1102. count[k] = 0
  1103. video1.pos = 0
  1104. k = "_RUN"
  1105. if k in count:
  1106. video1._run = count[k]
  1107. video1.pos
  1108. video1.x=40+cpan
  1109. video1.y=60+pm_wy+ctilt
  1110. video1.scale = int((csize))
  1111. if cdim:
  1112. video1.next()
  1113. #video1.prev()
  1114. video1.draw(window) #,x=0,y=0)
  1115. # overlay
  1116. video1.overlay(window,"run")
  1117. i += 1
  1118. def counter_dmx(COUNTER,dataA):
  1119. for count in COUNTER:
  1120. cDMX=count["DMX"]-1
  1121. try:
  1122. count["DIM"] = dataA[cDMX]
  1123. count["PAN"] = dataA[cDMX+1]
  1124. count["TILT"] = dataA[cDMX+2]
  1125. count["CONTROL"] = dataA[cDMX+3]
  1126. if count["CONTROL"] >= 10 and count["CONTROL"] < 20:
  1127. count["_SEC"] = int(count["SEC"] - (time.time() - count["_time"]))
  1128. if count["CONTROL"] >= 20 and count["CONTROL"] < 30:
  1129. count["_RUN"] = 0
  1130. if count["CONTROL"] >= 30 and count["CONTROL"] < 40:
  1131. count["_RUN"] = 1
  1132. count["SIZE"] = dataA[cDMX+4]
  1133. count["SEC"] = dataA[cDMX+5]
  1134. if count["_RUN"]:
  1135. try:
  1136. count["_SEC"] = int(count["SEC"] - (time.time() - count["_time"]))
  1137. except Exception as e:
  1138. pass
  1139. if type(count["_SEC"]) is int:
  1140. if count["_SEC"] < 0:
  1141. count["_SEC"] = 0
  1142. for ti in range(10):
  1143. #print(ti,(ti+6)*10)
  1144. if count["CONTROL"] >= (ti+6)*10 and count["CONTROL"] < (ti+7)*10:
  1145. count["_SEC"] = "----" #text 1
  1146. try:
  1147. count["_SEC"] = TEXT_BLOCK[ti]
  1148. except Exception as e:
  1149. pass
  1150. if count["CONTROL"] >= 250 and count["CONTROL"] < 256:
  1151. count["_SEC"] = ">{}<".format(cDMX+1)
  1152. count["RED"] = dataA[cDMX+6]
  1153. count["GREEN"] = dataA[cDMX+7]
  1154. count["BLUE"] = dataA[cDMX+8]
  1155. except Exception as e:
  1156. print("EXC FUNC",e,count)
  1157. #print(count)
  1158. def video_dmx(VIDEO,dataA):
  1159. for count in VIDEO:
  1160. cDMX=count["DMX"]-1
  1161. try:
  1162. count["DIM"] = dataA[cDMX]
  1163. count["PAN"] = dataA[cDMX+1]
  1164. count["TILT"] = dataA[cDMX+2]
  1165. count["CONTROL"] = dataA[cDMX+3]
  1166. if count["CONTROL"] >= 10 and count["CONTROL"] < 20:
  1167. count["_reset"] = 1
  1168. if count["CONTROL"] >= 20 and count["CONTROL"] < 30:
  1169. if count["_RUN"] == 1:
  1170. print( "_RUN:0",count["DMX"]-1)
  1171. count["_RUN"] = 0
  1172. if count["CONTROL"] >= 30 and count["CONTROL"] < 40:
  1173. if count["_RUN"] == 0:
  1174. print( "_RUN:1",count["DMX"]-1)
  1175. count["_RUN"] = 1
  1176. count["SIZE"] = dataA[cDMX+4]
  1177. count["SEC"] = dataA[cDMX+5]
  1178. count["RED"] = dataA[cDMX+6]
  1179. count["GREEN"] = dataA[cDMX+7]
  1180. count["BLUE"] = dataA[cDMX+8]
  1181. except Exception as e:
  1182. print("VIDEOPLAYER EXCEPT FUNC",e,count)
  1183. #print(count)
  1184. def read_dmx_data(ip,ips):
  1185. ip = select_ip(ips,univ=START_UNIV)
  1186. IP = ip
  1187. data = read_dmx(ip)
  1188. ip = select_ip(ips,univ=START_UNIV+1)
  1189. data3 = read_dmx(ip)
  1190. data.extend(data3)
  1191. ip = select_ip(ips,univ=START_UNIV+2)
  1192. data3 = read_dmx(ip)
  1193. data.extend(data3)
  1194. ip = select_ip(ips,univ=START_UNIV+4)
  1195. data3 = read_dmx(ip)
  1196. data.extend(data3)
  1197. #ip = select_ip(ips,univ=START_UNIV+5)
  1198. #data3 = read_dmx(ip)
  1199. #data.extend(data3)
  1200. return data
  1201. def draw_gobo(GRID,data):
  1202. i = 0
  1203. dmx = 1
  1204. h = 1
  1205. v = 1
  1206. for fix in GRID:
  1207. pos = fix.POS(40,60+pm_wy)
  1208. rgb = fix.rgb
  1209. if 1:
  1210. # draw row/col grid number
  1211. if fix.pos[0] == 0:
  1212. fr = font12.render("{}".format(fix.pos[1]+1) ,1, (200,200,200))
  1213. window.blit(fr,(10,pos[1]+3 ))
  1214. if fix.pos[1] == 0:
  1215. fr = font12.render("{}".format(fix.pos[0]+1) ,1, (200,200,200))
  1216. window.blit(fr,(pos[0]+2,35 ))
  1217. pygame.draw.rect(window,rgb,pos)
  1218. # DRAW SUB-FIXTURE
  1219. j = 0
  1220. for subfix in fix.sub_fix:#calc(data):
  1221. subfix.calc(data)
  1222. #fix = subfix
  1223. spos = subfix.POS(40,60+pm_wy)
  1224. srgb = subfix.rgb
  1225. #print(fix.dmx,rgb,pos)
  1226. #pygame.draw.circle(window,rgb,(pos[0]+int(pos[2]/2),pos[1]+int(pos[3]/2)),int(pos[3]/2))
  1227. #FUNC = 0
  1228. if FUNC > 10 and FUNC <= 20: # big dot
  1229. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1230. elif FUNC > 20 and FUNC <= 30:#small dot
  1231. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1232. elif FUNC > 30 and FUNC <= 40:#donut
  1233. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1234. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1235. elif FUNC > 40 and FUNC <= 50: # rec with hole
  1236. pygame.draw.rect(window,srgb,spos)
  1237. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1238. elif FUNC > 50 and FUNC <= 60: # rec with big hole
  1239. pygame.draw.rect(window,srgb,spos)
  1240. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1241. elif FUNC > 60 and FUNC <= 70: # rec with donat
  1242. pygame.draw.rect(window,srgb,spos)
  1243. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1244. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1245. elif FUNC > 70 and FUNC <= 80: # rec boarder
  1246. pygame.draw.rect(window,srgb,[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  1247. elif FUNC > 80 and FUNC <= 90: # rec big boarder
  1248. pygame.draw.rect(window,srgb,[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  1249. elif FUNC > 90 and FUNC <= 100: # rec thin line
  1250. pygame.draw.rect(window,srgb,spos)
  1251. pygame.draw.rect(window,[0,0,0],[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  1252. elif FUNC > 100 and FUNC <= 110: # rec big line
  1253. pygame.draw.rect(window,srgb,spos)
  1254. pygame.draw.rect(window,[0,0,0],[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  1255. elif FUNC > 110 and FUNC <= 120: # rec with dot
  1256. pygame.draw.rect(window,srgb,spos)
  1257. pygame.draw.rect(window,[0,0,0],[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  1258. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1259. elif FUNC > 120 and FUNC <= 130: # rec inline
  1260. pygame.draw.rect(window,srgb,[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  1261. pygame.draw.rect(window,[0,0,0],[spos[0]+3,spos[1]+3,spos[2]-6,spos[3]-6])
  1262. elif FUNC > 130 and FUNC <= 140: # 3 dot (heart)
  1263. draw_circle(window,srgb,(spos[0]+int(spos[2]/2)+2,spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1264. draw_circle(window,srgb,(spos[0]+int(spos[2]/2)-2,spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1265. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)+2),int(spos[3]/3.5))
  1266. else:
  1267. pygame.draw.rect(window,srgb,spos)
  1268. for subfix in fix.sub_fix:#calc(data):
  1269. subfix.calc(data)
  1270. #fix = subfix
  1271. spos = subfix.POS(40,60+pm_wy)
  1272. srgb = subfix.rgb
  1273. # draw row/col grid number
  1274. if subfix.pos[0] == 0:
  1275. fr = font12.render("{}".format(v ) ,1, (200,200,200))
  1276. window.blit(fr,(25,spos[1] ))
  1277. v += 1
  1278. if subfix.pos[1] == 0:
  1279. fr = font12.render("{}".format(1) ,1, (200,200,200))
  1280. fr = font12.render("{}".format(h ) ,1, (200,200,200))
  1281. h+=1
  1282. window.blit(fr,(spos[0],50 ))
  1283. if p >= 40:
  1284. if NR:
  1285. #fr = font15.render("{:02}".format(j+1) ,1, (0,200,255))
  1286. fr = font15.render("{:02}".format(subfix._id) ,1, (250,200,5))
  1287. window.blit(fr,(spos[0]+2,spos[1]+10))
  1288. j += 1
  1289. i += 1
  1290. ips=[]
  1291. dataA=[]
  1292. data=[]
  1293. def dmx_raw():
  1294. global ips,dataA,data
  1295. ips = read_index()
  1296. # ----
  1297. ip = select_ip(ips,univ=1) # univ 1 gobo
  1298. dataA = read_dmx(ip)
  1299. # ----
  1300. data = read_dmx_data(ip,ips)
  1301. if options.countdown:
  1302. counter_dmx(COUNTER,dataA)
  1303. if len(VIDEO) > 0:
  1304. video_dmx(VIDEO,dataA)
  1305. return ips,dataA,data
  1306. def dmx_loop():
  1307. while 1:
  1308. dmx_raw()
  1309. time.sleep(0.1)
  1310. dmx_raw()
  1311. thread.start_new_thread(dmx_loop,())
  1312. t1 = Timer(143)
  1313. time.sleep(0.33)
  1314. t2 = Timer(11)
  1315. count_tilt = 0
  1316. def main():
  1317. global IP
  1318. global GRID
  1319. global _GRID
  1320. global FUNC
  1321. global count_tilt
  1322. global TEXT_BLOCK
  1323. global TEXT_BLOCK_TIME
  1324. global PLAYLIST
  1325. global PLAYLIST_TIME
  1326. global dataA
  1327. GRID = init_grid(_x=_x,_y=_y) #init_gird()
  1328. #GRID = init_grid(_x=8,_y=8) #init_gird()
  1329. print("GRID LEN:",len(GRID))
  1330. #thread.start_new_thread( video_dmx_loop,(VIDEO,dataA) )
  1331. s=time.time()
  1332. print("run")
  1333. r = ""
  1334. IP = "xx"
  1335. while running:
  1336. if TEXT_BLOCK_TIME+5 < time.time():
  1337. TEXT_BLOCK = open_text_block()
  1338. TEXT_BLOCK_TIME = time.time()
  1339. if PLAYLIST_TIME+6 < time.time():
  1340. PLAYLIST = open_playlist()
  1341. PLAYLIST_TIME = time.time()
  1342. pygame.display.flip()
  1343. event()
  1344. window.fill((10,0,20))
  1345. calc_fps()
  1346. draw_overlay()
  1347. # GRID loop
  1348. try:
  1349. ddd = 1023 #univ 3 512
  1350. #FUNC = data[ddd]
  1351. FUNC2 = dataA[gobo_ch-1]
  1352. FUNC = FUNC2
  1353. #print("FUNC", FUNC )#:ddd+512])
  1354. #FUNC = 15
  1355. except Exception as e:
  1356. print("EXC FUNC",e)
  1357. draw_gobo(GRID,data)
  1358. # DRAW FIX NUMBER on TOP
  1359. draw_fix_nr(GRID)
  1360. #COUNTER.append({"DMX":31,"DIM":0,"PAN":127,"TILT":127,"CONTROL":0,"SEC":10,"RED":255,"GREEN":255,"BLUE":255,"_time":time.time(),"_RUN":0,"_SEC":0})
  1361. if options.countdown:
  1362. draw_counter(COUNTER)
  1363. if VIDEO:
  1364. draw_video(VIDEO)
  1365. pointer.draw(0,pm_wy) #wy
  1366. spos = [0,0,0,0]
  1367. if PIXEL_MAPPING >= 1:
  1368. reshape(0,0) #start pos
  1369. else:
  1370. reshape(spos[0]+spos[2]+20,10) #start pos
  1371. #reshape(spos[0]+spos[2]+20,10) #start pos
  1372. pygame.display.flip()
  1373. #pg.time.wait(15)
  1374. clock.tick(10)
  1375. if 'SDL_VIDEO_WINDOW_POS' in os.environ:
  1376. del os.environ['SDL_VIDEO_WINDOW_POS'] #= '%i,%i' % (200,164)
  1377. #if 'SDL_VIDEO_CENTERED' in os.environ['SDL_VIDEO_CENTERED']:
  1378. # del os.environ['SDL_VIDEO_CENTERED'] #= '0'
  1379. if __name__ == "__main__":
  1380. main()