vpu_live.py 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171
  1. import math
  2. import random
  3. import time
  4. import os
  5. import sys
  6. import gc
  7. import _thread as thread
  8. from optparse import OptionParser
  9. ...
  10. parser = OptionParser()
  11. parser.add_option("-m", "--mode", dest="mode",
  12. help="pixel mode pix,x,y --mode 40,10,8") #, metavar="FILE")
  13. parser.add_option("-X", "--XX", dest="XX", #default=1,
  14. help="x-split") #, metavar="FILE")
  15. parser.add_option("-x", "--xx", dest="xsplit", #default=1,
  16. help="x-split") #, metavar="FILE")
  17. parser.add_option("-y", "--yy", dest="ysplit",#default=1,
  18. help="y-split") #, metavar="FILE")
  19. parser.add_option("", "--start-univ", dest="start_univ",#default=1,
  20. help="set start-univers default=2") #, metavar="FILE")
  21. parser.add_option("", "--gobo-ch", dest="gobo_ch",#default=1,
  22. help="gobo ch univ on 1") #, metavar="FILE")
  23. #os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (200,164)
  24. parser.add_option("", "--win-pos", dest="win_pos",default="200,164",
  25. help="SDL_VIDEO_WINDOW_POS --win-pos=200,164") #, metavar="FILE")
  26. parser.add_option("", "--pixel-mapping", dest="pixel_mapping",default=0,
  27. help="pixel_mapping file/on --pixel-mapping=_x") #, metavar="FILE")
  28. parser.add_option("", "--countdown", dest="countdown",#default=1,
  29. help="enable countdown") #, metavar="FILE")
  30. parser.add_option("", "--videoplayer", dest="videoplayer",#default=1,
  31. help="enable videoplayer") #, metavar="FILE")
  32. parser.add_option("", "--title", dest="title",default="SCREEN",
  33. help="set title") #, metavar="FILE")
  34. #parser.add_option("-f", "--file", dest="filename",
  35. # help="write report to FILE", metavar="FILE")
  36. #parser.add_option("-q", "--quiet",
  37. # action="store_false", dest="verbose", default=True,
  38. # help="don't print status messages to stdout")
  39. (options, args) = parser.parse_args()
  40. import numpy
  41. for o in dir(options):
  42. if "_" in o:
  43. continue
  44. print(o,options.__getattribute__(o))
  45. START = time.time()
  46. # ===== ARTNET DMX =========
  47. import memcache
  48. mc = memcache.Client(['127.0.0.1:11211'], debug=0)
  49. def read_index():
  50. ips=mc.get("index")#cmd)
  51. if ips is None:
  52. ips = {}
  53. #for k,v in ips.items():
  54. # print(k,v)
  55. return ips
  56. def select_ip(ips, univ=2): # artnet univ
  57. _univ = ":{}".format(univ)
  58. for ip in ips: #high priority
  59. if "2.0.0" in ip and _univ in ip:
  60. return ip
  61. for ip in ips:
  62. if "ltp-out" in ip and _univ in ip:
  63. return ip
  64. FUNC = 0
  65. COUNTER = []
  66. if options.countdown:
  67. cdmx_start = options.countdown.split(",")
  68. for cdmx in cdmx_start:
  69. try:
  70. cdmx = int(cdmx)
  71. 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)})
  72. except Exception as e:
  73. print("EXCEPTION COUNTER INIT ",cdmx)
  74. def read_dmx(ip):
  75. r = ""
  76. if ip:
  77. #t = int(math.sin(time.time() - s)*10)
  78. r = mc.get(ip) #"2.0.0.13:2")
  79. rr = [0]*512
  80. for i,v in enumerate(r):
  81. try: #cleanup ltp-out to int
  82. v = int(v)
  83. rr[i] = v
  84. except:pass
  85. r = rr
  86. if not r:
  87. c = 0
  88. #time.sleep(0.1)
  89. r = [0] *512
  90. for i in range(12*8+1):
  91. dmx = i*4
  92. #print(dmx)
  93. r[dmx:dmx+4] = [255,10,10,40]
  94. return r
  95. # ===== ARTNET DMX =========
  96. PLAYLIST = []
  97. try:
  98. PLAYLIST = open_playlist()
  99. except:pass
  100. #import json
  101. #import pickle
  102. class Vopen():
  103. def __init__(self,dmx=None,_id=None):
  104. global PLAYLIST
  105. self._id = _id
  106. self.fpath = '/home/user/Downloads/'
  107. self.fpath = '/home/user/LibreLight/video/'
  108. self.fname = '' #'bbb_sunflower_480x320.mp4'
  109. #self.fname = 'no-video.mp4'
  110. try:
  111. self.fname = PLAYLIST[0]
  112. except Exception as e:
  113. print("Exception set video from PLAYLIST 5543:",e)
  114. self.restart_t = time.time()
  115. self.fps = 0
  116. self.scale = 50 #%
  117. self.angle = 0 #%
  118. self.dmx=dmx
  119. self.dim = 0
  120. self.x = 0
  121. self.y = 0
  122. self.init_count = 0
  123. self.cap = None
  124. self.shape = [200,200]
  125. self.img = None
  126. self.success = 1
  127. self.cv2 = None
  128. self._run = 0
  129. self.end = 0
  130. self._video_nr = 0
  131. self.shape_x = 370
  132. self.shape_y = 235
  133. try:
  134. global cv2
  135. self.cv2 = cv2
  136. except:
  137. pass
  138. self.init()
  139. def __repr__(self):
  140. sizeof = int(sys.getsizeof(self.buffer)/8)
  141. return "< id:{} buf:{} run:{} fps:{} scale:{} name:{} {}kb>".format(
  142. id(self),len(self.buffer),self._run,self.fps,self.scale
  143. ,self.fname,sizeof
  144. )
  145. def restart(self):
  146. print(self,"reset()")
  147. self.pos = 0
  148. self.restart_t = time.time()
  149. def init(self):
  150. print("---- ---- Vopen.init()",[self.fname,self._video_nr])
  151. print(PLAYLIST)
  152. self.time = 0
  153. self.t_delta = 0
  154. self.t_last = time.time()
  155. self.im = None
  156. self.pos = 0
  157. self.buffer = []
  158. self._init()
  159. self.init_count = 1
  160. def select_video(self,dmx_value):
  161. try:
  162. dmx_value = int(dmx_value/10)
  163. if self._video_nr != dmx_value:
  164. self._video_nr = dmx_value
  165. if self._video_nr < len(PLAYLIST):
  166. self.fname = str(PLAYLIST[self._video_nr])
  167. self.init()
  168. except Exception as e:
  169. print("Vopen.select_video()",dmx_value,e)
  170. def close_cap():
  171. print(dir(self.Rcap)) # = self.cv2.VideoCapture(self.fpath+self.fname)
  172. def _init(self):
  173. print(self)
  174. print("videoplayer.init()",self.fpath,self.fname)
  175. if not os.path.isfile(self.fpath+self.fname):
  176. print()
  177. print("video file does not exits !! >",self.fpath,self.fname)
  178. print()
  179. #exit()
  180. self.Rsuccess = 0
  181. if self.cv2:
  182. #self.Rcap = self.cv2.VideoCapture(self.fpath+self.fname)
  183. #self.Rcap = self.cv2.VideoCapture(self.fpath+self.fname, cv2.CAP_GSTREAMER)
  184. #GSTREAMER Assertion fctx->async_lock failed at libavcodec/pthread_frame.c:175
  185. self.Rcap = self.cv2.VideoCapture(self.fpath+self.fname, cv2.CAP_FFMPEG)
  186. #FFMPEG malloc(): unsorted double linked list corrupted ... Abgebrochen
  187. self.Rcap.read()
  188. #self.Rfvs = FileVideoStream(self.fpath+self.fname).start()
  189. self.Rsuccess = 1
  190. self._read()
  191. def _del(self):
  192. #self.Rcap = self.cv2.VideoCapture(self.fpath+self.fname)
  193. #for i in dir(self.Rcap):
  194. # print(i)
  195. #print()
  196. #for i in dir(self.cv2):
  197. # print(i)
  198. #time.sleep(0.01)
  199. self.buffer = [] #.append(self.img)
  200. self.Rcap.release()
  201. self.Rcap.retrieve()
  202. self.Rcap = None
  203. self.cap = None
  204. del self.Rcap #.release()
  205. #time.sleep(4)
  206. #sys.exit()
  207. #gc.collect()
  208. #import gc
  209. gc.collect()
  210. def _read(self):
  211. success = self.Rsuccess
  212. ok = 0
  213. if success and self.fname:
  214. cap = self.Rcap
  215. #fvs = self.Rfvs
  216. _break = 0
  217. try:
  218. success, img = cap.read()
  219. #self.img = fvs.read()
  220. if not success:
  221. self.Rcap.release()
  222. self.Rcap.retrieve()
  223. self.end = 1
  224. #j = json.dumps(self.buffer) #.append(img)
  225. #f = open("/tmp/buff")
  226. #f.write(j)
  227. #f.close()
  228. return
  229. if self.fps == 0:
  230. self.fps = cap.get(cv2.CAP_PROP_FPS)
  231. img = self.cv2.cvtColor(img, self.cv2.COLOR_BGR2RGB)
  232. img = self.rescale_frame2(img, 200)
  233. #ret, img = self.cv2.threshold(img, 100, 130, self.cv2.THRESH_BINARY) # treshold
  234. #self.img = self.cv2.Canny(self.img, 100, 200) # kanten
  235. #M = cv2.getPerspectiveTransform(Punkte_A, Punkte_B)
  236. #warped = cv2.warpPerspective(Bild, m, (420,594))
  237. #self.cv2.normalize(self.img, self.img, 0, self.dim, self.cv2.NORM_MINMAX)
  238. # store frame into buffer list
  239. self.buffer.append(img)
  240. ok = 1
  241. if len(self.buffer) % 100 == 0:
  242. _id = str(self.__repr__)[-5:-1]
  243. print(_id,"video read",self.dmx,len(self.buffer),self.fname,"fps",self.fps,self.dim)
  244. except Exception as e:
  245. print("Excetpion","_init",self,e,end="")
  246. self.success = 1
  247. return ok
  248. def read(self):
  249. pass
  250. def prev(self):
  251. self.pos -= 1
  252. if self.pos < 0:
  253. self.pos = len(self.buffer)-1
  254. if self.pos >= len(self.buffer):
  255. self.pos = len(self.buffer)-1
  256. self.im = self.buffer[int(self.pos)]
  257. def rotateImage(self,image, angle):
  258. try:
  259. print("EE",image.shape)
  260. shape = list(image.shape[1::-1])
  261. center = ((shape[0])/2,(shape[1])/2)
  262. #center = [(shape[0])/2,(shape[1])/2+35]
  263. rot_mat = self.cv2.getRotationMatrix2D(center,angle,1.0)
  264. if angle in [0,90,180,360]:
  265. pass
  266. else:
  267. if shape[0] > shape[1]:
  268. shape = (shape[0],shape[0])
  269. else:
  270. shape = (shape[1],shape[1])
  271. frame = self.cv2.warpAffine(image, rot_mat, shape) #,flags=self.cv2.INTER_LINEAR)
  272. return frame
  273. except Exception as e:
  274. raise(e)
  275. def moveImage(self,img,x=0,y=0):
  276. # Creating a translation matrix
  277. np = numpy
  278. translation_matrix = np.float32([ [1,0,x], [0,1,y] ])
  279. num_cols,num_rows = img.shape[1::-1]
  280. # Image translation
  281. frame = self.cv2.warpAffine(img, translation_matrix, (num_cols,num_rows))
  282. return frame
  283. def rescale_frame2(self,frame, width):
  284. height = int(frame.shape[0]/frame.shape[1] * width )
  285. dim = (width, height)
  286. return self.cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)
  287. def rescale_frame(self,frame, percent=75):
  288. width = int(frame.shape[1] * percent/ 100)
  289. height = int(frame.shape[0] * percent/ 100)
  290. dim = (width, height)
  291. return self.cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)
  292. def pause(self):
  293. #print("pause",self.t_last)
  294. t = time.time()
  295. self.t_delta = 0
  296. self.t_last = t
  297. def next_frame(self):
  298. #return 0
  299. if self._run and len(self.buffer) > 30:
  300. t = time.time()
  301. self.t_delta = t-self.t_last
  302. self.pos += self.t_delta*self.fps
  303. self.t_last = t
  304. else:
  305. self.pause()
  306. if self.restart_t > time.time()-0.5:
  307. # prevent flickerung by reset
  308. #self.pause()
  309. self.pos = 5
  310. pass
  311. # restart at the end
  312. if self.pos >= len(self.buffer):
  313. self.pos = 0
  314. self.img = None
  315. self._img = self.buffer[int(self.pos)]
  316. self.img = self._img
  317. def next(self):
  318. try:
  319. self.next_frame()
  320. #self.img = self.cv2.cvtColor(self.img, self.cv2.COLOR_BGR2RGB)
  321. self.img = self.rescale_frame(self.img, percent=self.scale)
  322. # rotate frame BUG: x,y offset ??? !!!
  323. self.img = self.rotateImage(self.img, self.angle)
  324. self.shape = self.img.shape[1::-1]
  325. if len(self.buffer) % 100 == 0:
  326. _id = str(self.__repr__)[-5:-1]
  327. print("next",_id)
  328. # add DIMER to videplayer
  329. self.cv2.normalize(self.img, self.img, 0, self.dim, self.cv2.NORM_MINMAX)
  330. img = self.img
  331. if img is None:
  332. return
  333. self.im = None
  334. self.im = pygame.image.frombuffer(img.tobytes(), self.shape, "RGB")
  335. #self.next_frame()
  336. except AttributeError as e:
  337. time.sleep(.05)
  338. #if self.init_count % 100 == 0:
  339. print("except 776",e)
  340. #self.init()
  341. except Exception as e:
  342. print("except 756",e)
  343. def draw(self,wn=None):
  344. if self.dim <= 1:
  345. return
  346. if self.scale < 255*.05:
  347. self.scale = 255*0.05
  348. # draw video background box
  349. __xw = int(self.shape_x*self.scale/255)
  350. __yw = int(self.shape_y*self.scale/255)
  351. #xx ,yy = (370,235)
  352. xx = self.shape_x #= xx
  353. yy = self.shape_y #= yy
  354. try:
  355. xx ,yy = self.im.get_size()[:2]
  356. self.shape_x = xx
  357. self.shape_y = yy
  358. except AttributeError as e:
  359. pass
  360. xx = int(xx*self.scale/255)
  361. yy = int(yy*self.scale/255)
  362. __xw = int(xx) #*self.scale/255)
  363. __yw = int(yy) #*self.scale/255)
  364. yellow = [105,50,0]
  365. yellow[0] = int(yellow[0]*self.dim/255)
  366. yellow[1] = int(yellow[1]*self.dim/255)
  367. yellow[2] = int(yellow[2]*self.dim/255)
  368. #print(yellow)
  369. if 1: #corner left up
  370. p1 = [self.x+2,self.y+2]
  371. p2 = [self.x+__xw-4,self.y+__yw-4]
  372. p3 = [self.x+__xw-4,self.y+2]
  373. p4 = [self.x+2,self.y+__yw-4]
  374. p1 = [self.x,self.y]
  375. p2 = [self.x+__xw,self.y+__yw]
  376. p3 = [self.x+__xw,self.y]
  377. p4 = [self.x,self.y+__yw]
  378. hx=int(xx/2)
  379. hy=int(yy/2)
  380. #print(hx,hy,xx,yy) #,ratio)
  381. #print("-")
  382. #print(sys.getsizeof(self.buffer))
  383. #print(sys.getsizeof(self))
  384. #print(sys.getsizeof(self.Rcap))
  385. #print(dir(self.Rcap))
  386. p1 = [self.x-hx,self.y-hy]
  387. p2 = [self.x+__xw-hx,self.y+__yw-hy]
  388. p3 = [self.x+__xw-hx,self.y-hy]
  389. p4 = [self.x-hx,self.y+__yw-hy]
  390. pygame.draw.rect(wn,yellow ,[p1[0] ,p1[1] ,__xw ,__yw])
  391. pygame.draw.rect(wn,[25,20,20],[p1[0]+1,p1[1]+1,__xw-2,__yw-2])
  392. pygame.draw.line(wn,yellow ,p1 ,p2)
  393. pygame.draw.line(wn,yellow ,p3 ,p4)
  394. if 0: #corner left up
  395. pygame.draw.rect(wn,yellow,[self.x,self.y,__xw,__yw])
  396. pygame.draw.rect(wn,[25,20,20],[self.x+1,self.y+1,__xw-2,__yw-2])
  397. pygame.draw.line(wn,yellow,[self.x+2,self.y+2],[self.x+__xw-4,self.y+__yw-4])
  398. pygame.draw.line(wn,yellow,[self.x+__xw-4,self.y+2],[self.x+2,self.y+__yw-4])
  399. if 0: #corner right down
  400. pygame.draw.rect(wn,yellow,[self.x-__xw,self.y-__yw,__xw,__yw])
  401. pygame.draw.rect(wn,[25,20,20],[self.x+1-__xw,self.y+1-__yw,__xw-2,__yw-2])
  402. pygame.draw.line(wn,yellow,[self.x+2-__xw,self.y+2-__yw],[self.x+__xw-4-__xw,self.y+__yw-4-__yw])
  403. pygame.draw.line(wn,yellow,[self.x+__xw-4-__xw,self.y+2-__yw],[self.x+2-__xw,self.y+__yw-4-__yw])
  404. #pygame.draw.line(wn,yellow,[self.x,self.y],[self.x,self.y+300])
  405. #pygame.draw.line(wn,yellow,[self.x,self.y],[self.x+300,self.y])
  406. pz = 0
  407. #txt = "FPS:{} F:{:05} von {:05} sec:{:0.02f} von {:0.02f}"
  408. #txt = txt.format(self.fps,int(self.pos),len(self.buffer),(-1),pz )
  409. #if self.end:
  410. # fr = font15.render(txt,1, (0,255,0))
  411. #else:
  412. # fr = font15.render(txt,1, (255,0,0))
  413. #wn.blit(fr,(10,MAIN_SIZE[1]-(self._id+1)*35))
  414. if self.success and wn and self.im: # is not None:
  415. #wn.blit(self.im, (int(self.x), int(self.y)))
  416. #wn.blit(self.im, (int(self.x-__xw), int(self.y-__yw)))
  417. #xx ,yy = self._img.shape[:2]
  418. xx ,yy = self.im.get_size()[:2]
  419. #print(xx,yy)
  420. #wn.blit(self.im, (int(self.x+xx/2), int(self.y+yy/2)))
  421. wn.blit(self.im, (int(self.x-xx/2), int(self.y-yy/2)))
  422. def overlay(self,wn=None,mode="x"):
  423. # overlay
  424. pygame.draw.rect(wn,[255,200,0],[5,MAIN_SIZE[1]-(self._id+1)*35,300,28])
  425. font15 = pygame.font.SysFont("freemonobold",17)
  426. pz = 0
  427. if self.end:
  428. rgb = [ 100,255,100]
  429. else:
  430. rgb = [255,100,0]
  431. pygame.draw.rect(wn,rgb,[220,MAIN_SIZE[1]-(self._id+1)*35,80,13])
  432. _line = "error no _line"
  433. _line ="FPS:{} F:{:05} von {:05} sec:{:0.02f} von {:0.02f}"
  434. if self.fps == 0: # check if div zerro
  435. _line = _line.format(self.fps,int(self.pos),len(self.buffer),(-1),pz )
  436. else:
  437. pz = (len(self.buffer)/self.fps)
  438. _line = _line.format(self.fps,int(self.pos),len(self.buffer),(self.pos/self.fps),pz )
  439. fr = font15.render(_line ,1, (0,0,0))
  440. wn.blit(fr,(10,MAIN_SIZE[1]-(self._id+1)*35))
  441. if self._run:
  442. mode = "run"
  443. else:
  444. mode = "pause"
  445. fr = font15.render(" {} {} >:{} ".format(self._id+1,self._video_nr,mode) ,1, (0,0,0))
  446. wn.blit(fr,(3,MAIN_SIZE[1]-(self._id+1)*35+15))
  447. fr = font15.render("{}".format(self.fname) ,1, (0,0,0))
  448. wn.blit(fr,(70,MAIN_SIZE[1]-(self._id+1)*35+15))
  449. Vopen = Vopen
  450. VIDEO = []
  451. videoplayer=[]
  452. cv2 = None
  453. FileVideoStream = None
  454. _vid = 0
  455. if type(options.videoplayer) is str:
  456. try:
  457. import cv2
  458. except Exception as e:
  459. print("Except Import:",e)
  460. try:
  461. # faster video reading ... ???
  462. from imutils.video import FileVideoStream
  463. except Exception as e:
  464. print("Except Import:",e)
  465. max_videoplayer = 4
  466. dmx_start = options.videoplayer.split(",")
  467. for cdmx in dmx_start:
  468. if len(videoplayer) > max_videoplayer:
  469. print("-- videoplayer max count {} !! break".format(max_videoplayer))
  470. break
  471. print("-- videoplayer dmx:",cdmx)
  472. try:
  473. cdmx = int(cdmx)
  474. videoplayer.append( Vopen(cdmx,_id=_vid) )
  475. _vid += 1
  476. VIDEO.append({"DMX":cdmx,"DIM":0,"PAN":127,"TILT":127,"CONTROL":0,"SEC":10,"VIDEO":"3","RED":255,"GREEN":255,"BLUE":255,"_time":time.time(),"_RUN":0,"_SEC":">{}<".format(cdmx)})
  477. except Exception as e:
  478. print("EXCEPTION COUNTER INIT ",cdmx)
  479. def loop_videoplayer():
  480. while 1:
  481. _videoplayer = videoplayer[:]
  482. #print(".")
  483. ok = 0
  484. for i in _videoplayer: #.append( Vopen(cdmx,_id=_vid) )
  485. try:
  486. r = i._read() # read next frame from file
  487. if r:
  488. ok = 1
  489. except Exception as e:
  490. print("EXCEPTION loop_videoplayer ",e)
  491. #time.sleep(0.002)
  492. #time.sleep(1/120)
  493. if ok == 0:
  494. time.sleep(0.1)
  495. else:
  496. time.sleep(0.005)
  497. thread.start_new_thread(loop_videoplayer,())
  498. # ===== ======
  499. #self.fname = PLAYLIST[0]
  500. videoplayer2 = []
  501. def loop2_videoplayer():
  502. while 1:
  503. print()
  504. print()
  505. print()
  506. for i,v in enumerate(PLAYLIST):
  507. try:
  508. video1 = videoplayer2[i]
  509. video1.select_video(i)
  510. except:
  511. vi = Vopen(181,1)
  512. videoplayer2.append( vi )
  513. _videoplayer = videoplayer2[:]
  514. #print(".")
  515. ok = 0
  516. j =0
  517. for i in _videoplayer: #.append( Vopen(cdmx,_id=_vid) )
  518. try:
  519. r = i._read() # read next frame from file
  520. #print(j,len(videoplayer2),i,len(i.buffer))
  521. if r:
  522. print(j,len(videoplayer2),i,len(i.buffer))
  523. ok = 1
  524. except Exception as e:
  525. print("EXCEPTION loop2_videoplayer ",e)
  526. time.sleep(0.002)
  527. j += 1
  528. if ok == 0:
  529. pass#time.sleep(0.01)
  530. else:
  531. pass#time.sleep(0.005)
  532. #thread.start_new_thread(loop2_videoplayer,())
  533. # PARSE COMMANDLINE ARGUMENTS
  534. CFG_IN = {"name":"CFG_IN","x1":40,"y1":60,"x2":300,"y2":300 ,"w":300,"h":300}
  535. CFG_OUT = {"name":"CFG_OUT","x1":40,"y1":60,"x2":300,"y2":300 ,"w":300,"h":300,"on":0}
  536. CFG_BLOCK = {"name":"CFG_BLOCK","size":16,"h-split":2,"v-split":2,"h-count":8,"v-count":8}
  537. p = 16
  538. block = [p,p]
  539. _x = 8
  540. _y = 8
  541. #HD = "0"
  542. if options.mode:
  543. try:
  544. HD = options.mode
  545. p,_x,_y = HD.split(",")
  546. _x = int(_x)
  547. _y = int(_y)
  548. p = int(p)
  549. block = [p,p]
  550. except Exception as e:
  551. print( "Exc",options.mode,e)
  552. if _x < 1:
  553. _x = 1
  554. if _y < 1:
  555. _y = 1
  556. CFG_BLOCK["h-count"] = _x
  557. CFG_BLOCK["v-count"] = _y # int(8*8/_x) #/_x+0.5) #_y
  558. CFG_BLOCK["size"] = p
  559. print( [options.xsplit])
  560. print( [options.ysplit])
  561. try:
  562. if options.xsplit:
  563. CFG_BLOCK["h-split"] = int(options.xsplit)
  564. if options.ysplit:
  565. CFG_BLOCK["v-split"] = int(options.ysplit)
  566. except Exception as e:
  567. print( "Exc",options.mode,e)
  568. print("HD",CFG_BLOCK["h-split"],CFG_BLOCK["v-split"])
  569. print("xy",_x,_y)
  570. print("++++++++++++++++++", p,_x,_y)
  571. _x2 = _x
  572. try:
  573. if options.XX:
  574. pass#_x2 = int(options.XX)
  575. except Exception as e:
  576. print( "Exc",options.mode,e)
  577. print("_x2 , -X",_x2)
  578. # ===== GUI =========
  579. import pygame
  580. import pygame.gfxdraw
  581. import pygame.font
  582. clock = pygame.time.Clock()
  583. os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (200,164)
  584. if options.win_pos:
  585. if "," in options.win_pos:
  586. win_pos = options.win_pos.split(",")
  587. try:
  588. WIN_POS = '%i,%i' % (int(win_pos[0]),int(win_pos[1]) )
  589. os.environ['SDL_VIDEO_WINDOW_POS'] = WIN_POS
  590. except Excetpion as e:
  591. print("win_pos",win_pos,e)
  592. os.environ['SDL_VIDEO_CENTERED'] = '0'
  593. pg = pygame
  594. pygame.init()
  595. pygame.mixer.quit()
  596. f = pygame.font.get_fonts()
  597. for i in f:
  598. if "mono" in i.lower():
  599. print(i)
  600. font = pygame.font.SysFont("freemonobold",22)
  601. font10 = pygame.font.SysFont("freemonobold",10)
  602. font12 = pygame.font.SysFont("freemonobold",12)
  603. font15 = pygame.font.SysFont("freemonobold",15)
  604. font40 = pygame.font.SysFont("freemonobold",40)
  605. font80 = pygame.font.SysFont("freemonobold",70)
  606. #font = pygame.font.SysFont(None,30)
  607. fr = font.render("hallo" ,1, (200,0,255))
  608. PIXEL_MAPPING = 0
  609. grid_file = "/tmp/vpu_grid_hd.csv"
  610. text_file = "/home/user/LibreLight/vpu_text_hd.csv"
  611. play_list = "/tmp/vpu_playlist_hd.csv"
  612. play_list = "/home/user/LibreLight/video/" #.format(path)
  613. pm_wy = 0
  614. if options.pixel_mapping:
  615. PIXEL_MAPPING = 1
  616. CFG_OUT["on"] = 1
  617. path = options.pixel_mapping
  618. path = path.replace("/","-")
  619. path = path.replace(".","-")
  620. path = path.replace("\"","-")
  621. path = path.replace("'","-")
  622. grid_file = "/home/user/LibreLight/vpu_grid_hd{}.csv".format(path)
  623. text_file = "/home/user/LibreLight/vpu_text_hd{}.csv".format(path)
  624. play_list = "/home/user/LibreLight/vpu_playlist_hd{}.csv".format(path)
  625. play_list = "/home/user/LibreLight/video/" #.format(path)
  626. #_x = 8
  627. #_y = 8
  628. print(" ",[options.pixel_mapping],"grid_file",grid_file)
  629. #grid_file = "/home/user/LibreLight/vpu_grid_hd.csv"
  630. MAIN_SIZE=(600,500)
  631. try:
  632. if _x < 8 and PIXEL_MAPPING >= 1:
  633. wx = 30+30+block[0] * 8
  634. CFG_IN["w"] = CFG_BLOCK["size"] * 8
  635. else:
  636. wx = 30+30+block[0] * _x
  637. wy = 40+40+block[1] * _y
  638. if type(options.videoplayer) is str:
  639. wy += 150 # video playlist
  640. MAIN_SIZE=(wx,wy)
  641. if PIXEL_MAPPING >= 1:
  642. pm_wy = 11*p #+ p*3
  643. CFG_IN["y1"] += 11*p
  644. MAIN_SIZE=(wx,wy+pm_wy)
  645. except Exception as e:
  646. print("Exception:",e)
  647. CFG_IN["w"] = CFG_BLOCK["size"] * CFG_BLOCK["h-count"]
  648. CFG_IN["h"] = CFG_BLOCK["size"] * CFG_BLOCK["v-count"]
  649. CFG_OUT["w"] = CFG_BLOCK["size"] * 8
  650. CFG_OUT["h"] = CFG_BLOCK["size"] * 8
  651. def CFG_CALC_P(CFG):
  652. CFG["x2"] = CFG["x1"]+CFG["w"]
  653. CFG["y2"] = CFG["y1"]+CFG["h"]
  654. CFG["p1"] = [CFG["x1"] ,CFG["y1"]]
  655. CFG["p2"] = [CFG["x2"] ,CFG["y2"]]
  656. print("CFG",CFG)
  657. # ?
  658. #CFG_IN["w"] = int(CFG_BLOCK["v-count"]-2) * CFG_BLOCK["size"]
  659. CFG_CALC_P(CFG_IN)
  660. CFG_CALC_P(CFG_OUT)
  661. print("CFG_BLOCK",CFG_BLOCK)
  662. print()
  663. print()
  664. print()
  665. print()
  666. print()
  667. window = pygame.display.set_mode(MAIN_SIZE,pg.RESIZABLE)#,32)#,pygame.FULLSCREEN) #x left->right ,y top-> bottom
  668. #if options.title:
  669. pg.display.set_caption('LibreLight VPU-{}'.format(options.title))
  670. #else:
  671. # pg.display.set_caption('LibreLight VPU-SCREEN')
  672. class Fix():
  673. def __init__(self,_id,pos,block=[16,16],univ=0,dmx=0,ch=4):
  674. #print("Fix",_id)
  675. self._id = _id
  676. self.dmx = (_id-1) * ch +1 #dmx
  677. self.univ = univ
  678. self.ch = ch
  679. self.pos = pos
  680. self.rgb = [0,0,0]
  681. self.block = block #[10,10]
  682. self.x = pos[0]
  683. self.y = pos[1]
  684. self.strobo = time.time()
  685. self.bmp = 250
  686. self.sub_fix = []
  687. sub_block =[block[0]/CFG_BLOCK["h-split"],block[1]/CFG_BLOCK["v-split"]]
  688. if _id <= 0: #exit
  689. return
  690. spalte = (_id-1)%_y +1
  691. zeile = int((_id-1)/_x2) #+1
  692. #zeile = zeile*_x*CFG_BLOCK["h-split"]*CFG_BLOCK["v-split"]
  693. add_row = _x*CFG_BLOCK["h-split"]*CFG_BLOCK["v-split"]
  694. #zeile 1
  695. sid = (_id-1)*2 + zeile*CFG_BLOCK["h-split"]*_x2
  696. #for i in range(1,CFG_BLOCK["h-split"]):
  697. sid = sid+1
  698. #sid = zeile
  699. sub_pos= [pos[0]*block[0],pos[1]*block[1]]
  700. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  701. self.sub_fix.append(sub_fix)
  702. sid = sid+1
  703. #sid = zeile
  704. sub_pos= [pos[0]*block[0]+block[0]/2,pos[1]*block[1]]
  705. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  706. self.sub_fix.append(sub_fix)
  707. #zeile 2
  708. sid = (_id-1)*2+1 + _x2*CFG_BLOCK["h-split"] + zeile*CFG_BLOCK["h-split"]*_x2 # int(add_row)
  709. #sid = sid+1
  710. #sid = CFG_BLOCK["h-split"]
  711. sub_pos= [pos[0]*block[0],pos[1]*block[1]+block[1]/2]
  712. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  713. self.sub_fix.append(sub_fix)
  714. #sid = sid+1
  715. sid = sid+1
  716. sub_pos= [pos[0]*block[0]+block[0]/2,pos[1]*block[1]+block[1]/2]
  717. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  718. self.sub_fix.append(sub_fix)
  719. def calc(self,data):
  720. _rgb = [0,255,0]
  721. return _rgb
  722. def sub_calc(self,data):
  723. _rgb = [0,255,0]
  724. for sub_fix in self.sub_fix:
  725. sub_fix.block = self.block[:]
  726. _rgb = sub_fix.calc(data)
  727. return _rgb
  728. def POS(self,x=0,y=0,a=0,b=0):
  729. A = (self.pos[0])*self.block[0]
  730. B = (self.pos[1])*self.block[1]
  731. C = self.block[0]-a
  732. D = self.block[1]-b
  733. return [x+A,y+B,C,D]
  734. def subPOS(self,x=0,y=0,a=0,b=0):
  735. __out = []
  736. for sub_fix in self.sub_fix:
  737. __out.append( sub_fix.POS(x,y,a,b) )
  738. return __out
  739. class SubFix():
  740. def __init__(self,_id,pos,block=[16,16],univ=0,dmx=0,ch=4):
  741. #print("Fix",_id)
  742. self._id = _id
  743. self.dmx = (_id-1) * ch +1 #dmx
  744. self.univ = univ
  745. self.ch = ch
  746. self.pos = pos
  747. self.rgb = [0,0,40]
  748. self.block = block #[10,10]
  749. self.x = pos[0]
  750. self.y = pos[1]
  751. self.strobo = time.time()
  752. self.bmp = 250
  753. def calc(self,data):
  754. #return [130,30,20]
  755. dmx_sub = [30]*10
  756. #print(dmx_sub)
  757. dmx = self.dmx -1
  758. _dmx_sub = []
  759. if self.dmx >= 0:
  760. dmx = rDMX(self.univ,self.dmx)-1
  761. if dmx+self.ch < len(data):
  762. _dmx_sub = data[dmx:dmx+self.ch]
  763. if _dmx_sub:
  764. dmx_sub = _dmx_sub
  765. #print(dmx_sub)
  766. dim = dmx_sub[0]/255
  767. #print("dmx",dmx,dmx_sub)
  768. r = dmx_sub[1]*dim
  769. g = dmx_sub[2]*dim
  770. b = dmx_sub[3]*dim
  771. r = int(r)
  772. g = int(g)
  773. b = int(b)
  774. self.rgb = [r,g,b]
  775. return self.rgb
  776. def POS(self,x=0,y=0,a=0,b=0):
  777. A = (self.pos[0]) #+self.block[0]
  778. B = (self.pos[1]) #+self.block[1]
  779. C = self.block[0]-a
  780. D = self.block[1]-b
  781. if NR:
  782. C-=1
  783. D-=1
  784. return [int(x+A),int(y+B),int(C),int(D)]
  785. class POINTER():
  786. def __init__(self):
  787. self.pos = [0,0,0,0]
  788. self.on = 0
  789. self.rgb = [0,100,10]
  790. self._x = 0
  791. self._y = 0
  792. self.x = 0
  793. self.y = 0
  794. self.fix = Fix(0 ,[999,999],[16,16],0,0,0)
  795. def row_move(self,x,y):
  796. self._x = x
  797. self._y = y
  798. def move(self,pos):
  799. self.pos = pos
  800. self.on = 1
  801. def cross(self,x,y):
  802. self.x = x
  803. self.y = y
  804. def draw(self,x,y):
  805. pos = self.pos[:]
  806. #print("draw",x,y,pos)
  807. pos[0] += x
  808. pos[1] += y
  809. fix_x= self.fix.x
  810. fix_y= self.fix.y +y
  811. #print("draw",x,y,pos)
  812. if self.on:
  813. pygame.draw.rect(window,self.rgb,pos)
  814. #pygame.draw.line(window,self.rgb, (pos[0],pos[1]) , (pos[0]+100,pos[1]) )
  815. # mouse grid posision
  816. fr = font15.render("{}/{}".format(fix_x+1,fix_y) ,1, (200,200,200))
  817. _nr = fix_y * _x + fix_x +1
  818. #fr = font15.render("{:02} {}/{}".format(_nr, fix_x+1,fix_y+1 ) ,1, (200,200,200))
  819. fr = font15.render("{:02}".format(_nr ) ,1, (200,200,200))
  820. window.blit(fr,(pos[0]+2,pos[1]+2 ))
  821. window.blit(fr,(130,1))
  822. # fix pos
  823. txt=str(pos) #"[0, 0, 0, 0]"
  824. fr = font15.render(txt ,1, (200,200,200))
  825. #window.blit(fr,(pos[0]+2,pos[1]+2 ))
  826. window.blit(fr,(10,1))
  827. # univers
  828. #fr = font15.render("{:02}:{:03}".format(fix.univ,fix.dmx) ,1, (200,200,200))
  829. #window.blit(fr,(300,10))
  830. # pointer
  831. fr = font15.render("X:{:03}".format(self._x) ,1, (200,200,200))
  832. window.blit(fr,(10,30))
  833. fr = font15.render("Y:{:03}".format(self._y) ,1, (200,200,200))
  834. window.blit(fr,(10,40))
  835. # crosshair
  836. self.rgb = [0,0,200]
  837. pygame.draw.line(window,self.rgb, (self.x-p,self.y) , (self.x-2,self.y),4 )
  838. pygame.draw.line(window,self.rgb, (self.x,self.y-p) , (self.x,self.y-2),4 )
  839. self.rgb = [0,200,0]
  840. pygame.draw.line(window,self.rgb, (self.x+2,self.y) , (self.x+p,self.y),4 )
  841. pygame.draw.line(window,self.rgb, (self.x,self.y+2) , (self.x,self.y+p),4 )
  842. self.rgb = [200,0,0]
  843. pointer = POINTER()
  844. NR = 0
  845. running = True
  846. def event():
  847. global NR,running
  848. for event in pygame.event.get():
  849. #print(event.dict)
  850. _button = None
  851. if "button" in event.dict:
  852. _button = event.dict["button"]
  853. _state = None
  854. if "state" in event.dict:
  855. _state = event.state
  856. _key = None
  857. if "key" in event.dict:
  858. _key = event.key
  859. _pos = None
  860. if "pos" in event.dict:
  861. _pos = event.pos
  862. _type = None
  863. if "type" in event.dict:
  864. _type = event.type
  865. _type = event.type
  866. _mod = None
  867. if "mod" in event.dict:
  868. _mod = event.mod
  869. if 0:
  870. print( " ")
  871. print( "{:.02f}".format( time.time() - START ))
  872. print("button -",_button,end="\t| ")
  873. #print("state -",_state)
  874. print("pos -",_pos)
  875. print("type -",_type, end="\t| ")
  876. print("key -",_key)
  877. print("mod -",_mod)
  878. try:
  879. if _type == 5:
  880. if _button == 1:
  881. NR += 1
  882. if NR > 1:
  883. NR = 0
  884. if _button == 3:
  885. NR -= 1
  886. if NR < 0:
  887. NR = 1
  888. if _pos:
  889. posA = _pos
  890. fix = find_pix(_pos[0]-40,_pos[1]-60+pm_wy)
  891. if fix:
  892. pos = fix.POS(40,60+pm_wy)
  893. rgb = [0,0,0]
  894. pointer.move(pos)
  895. pointer.fix = fix
  896. else:
  897. pointer.on = 0
  898. pointer.row_move(_pos[0],_pos[1])
  899. pointer.cross(_pos[0],_pos[1])
  900. if event.type == pygame.VIDEORESIZE:
  901. window = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)
  902. except Exception as e:
  903. print(e)
  904. if event.type==pygame.QUIT:
  905. running=False
  906. fps = 0
  907. fps2 = 0
  908. frame = 0
  909. frame2 = 0
  910. frame_t = time.time()
  911. frame2_t = time.time()
  912. IP = "yyy"
  913. def draw_overlay():
  914. global fps,fps2
  915. fr = font15.render("DMX-FPS:{}".format(fps) ,1, (200,0,255))
  916. window.blit(fr,(10,10))
  917. fr = font15.render("GUI-FPS :{}".format(fps2) ,1, (200,0,255))
  918. window.blit(fr,(10,20))
  919. fr = font.render("ip:{}".format(IP) ,1, (200,0,255))
  920. window.blit(fr,(90,10))
  921. def draw_frame(window,rgb,p1,p2,offset=0):
  922. o = offset
  923. _p1 = [p1[0]-o,p1[1]-o]
  924. _p2 = [p1[0]-o,p2[1]+o]
  925. pygame.draw.line(window,rgb,_p1,_p2) # left
  926. _p1 = [p2[0]+o,p2[1]+o]
  927. pygame.draw.line(window,rgb,_p1,_p2) # bottom
  928. _p2 = [p2[0]+o,p1[1]-o]
  929. pygame.draw.line(window,rgb,_p1,_p2) # right
  930. _p1 = [p1[0]-o,p1[1]-o]
  931. pygame.draw.line(window,rgb,_p1,_p2) # top
  932. def calc_fps():
  933. global fps,frame,frame_t
  934. t = time.time()
  935. if frame_t+1 < t:
  936. fps = frame #frame_t- t #frame
  937. frame = 1
  938. frame_t = time.time()
  939. def calc_fps2():
  940. global fps2,frame2,frame2_t
  941. t = time.time()
  942. if frame2_t+0.1 < t:
  943. fps2 = frame2*10 #frame_t- t #frame
  944. frame2 = 1
  945. frame2_t = t #time.time()
  946. TEXT_BLOCK = []
  947. def _create_text_block():
  948. print("======== CREATE NEW TEXT FILE !!",text_file)
  949. f = open(text_file,"w")
  950. for i in range(10):
  951. f.write("TEXT {}\n".format(i+1))
  952. f.close()
  953. def open_text_block():
  954. print("======== OPEN TEXT FILE !!",text_file)
  955. _lines = []
  956. try:
  957. f = open(text_file,"r")
  958. _lines = f.readlines()
  959. f.close()
  960. except FileNotFoundError as e:
  961. print("TEXT",e)
  962. _create_text_block()
  963. if len(_lines) <= 0:
  964. _create_text_block()
  965. lines = []
  966. for l in _lines:
  967. #print(">> ",l.strip())
  968. lines.append(l.strip())
  969. if len(lines) <= 10:
  970. for i in range(10-len(lines)):
  971. lines.append("LINE ERROR")
  972. return lines
  973. TEXT_BLOCK = open_text_block()
  974. TEXT_BLOCK_TIME = time.time()
  975. # video playlist
  976. PLAYLIST = []
  977. def open_playlist():
  978. print("======== OPEN PLAYLIST DIR !!",play_list)
  979. if not os.path.isdir(play_list):
  980. os.system("mkdir -p {}".format(play_list))
  981. _lines = os.listdir(play_list)
  982. _lines.sort()
  983. lines = ['']*25 # first is empty
  984. i=0
  985. for l in _lines:
  986. #print(">> ",l.strip(),len(lines))
  987. l = l.strip()
  988. if "_" in l:
  989. ll = l.split("_",1)
  990. print(">> ",ll)
  991. #ll = int(ll)
  992. try:
  993. lll = int(ll[0])
  994. #lines.append(l.strip())
  995. lines[lll] = l
  996. except:pass
  997. if len(lines) <= 10:
  998. for i in range(10-len(lines)):
  999. lines.append("")#"LINE ERROR")
  1000. return lines
  1001. PLAYLIST_TIME = time.time()
  1002. PLAYLIST = open_playlist()
  1003. # ===== GUI =========
  1004. #def draw_circle(surface, x, y, radius, color):
  1005. def draw_circle(surface,color, pos, radius):
  1006. x,y=pos
  1007. pygame.gfxdraw.aacircle(surface, int(x), int(y), radius-1, color)
  1008. pygame.gfxdraw.filled_circle(surface, int(x), int(y), radius-1, color)
  1009. def rDMX(univ,dmx):
  1010. return univ*512+dmx
  1011. def generate_grid(mapping=0):
  1012. _log = []
  1013. #if PIXEL_MAPPING:
  1014. # log = open(grid_file,"w")
  1015. head = "i,univ,dmx,x,y,ch\n"
  1016. head = "i,univ,dmx,ch\n"
  1017. head = "univ,dmx,x,y,ch\n"
  1018. head = "nr,id,info\n"
  1019. print("csv:",head)
  1020. #if PIXEL_MAPPING:
  1021. # log.write(head)
  1022. _log.append(head)
  1023. dmx = 1-1
  1024. ch = 4
  1025. y=0
  1026. x=0
  1027. #for i in range((_y)*(_x)):
  1028. for i in range((8)*(8)):
  1029. #if x > _x and i%_x == 0:
  1030. if x > 8 and i%8 == 0:
  1031. #print("--> -->")
  1032. x=0
  1033. y+=1
  1034. _univ = int(dmx/512)
  1035. _dmx = dmx - (_univ)*512
  1036. pos=[x,y]
  1037. line="{},{},{},{},{},{}\n".format(i+1,_univ,_dmx+1,pos[0],pos[1],ch)
  1038. line="{},{},{},{},{}\n".format(_univ,_dmx+1,x,y,ch)
  1039. line="{},{},x\n".format(i+1,i+1)
  1040. #print("wcsv:",[line])
  1041. #if PIXEL_MAPPING:
  1042. # log.write(line)
  1043. _log.append(line)
  1044. dmx += ch
  1045. x+=1
  1046. if mapping and PIXEL_MAPPING:
  1047. print("CREATE NEW PIXELMAP FILE !!",grid_file)
  1048. log = open(grid_file,"w")
  1049. log.writelines(_log)
  1050. log.close()
  1051. return _log[:] #GRID
  1052. def init_grid(mapping=0,_x=4,_y=4):
  1053. if mapping and PIXEL_MAPPING:
  1054. try:
  1055. log = open(grid_file,"r")
  1056. except:
  1057. generate_grid(mapping=mapping)
  1058. log = open(grid_file,"r")
  1059. lines = log.readlines()
  1060. else:
  1061. lines = generate_grid()
  1062. GRID = []
  1063. y=0
  1064. x=0
  1065. print("CSV header",[lines[0]],[PIXEL_MAPPING])
  1066. for i,line in enumerate(lines[1:]): #exclude first line
  1067. #print("rcsv",[line])
  1068. line = line.strip()
  1069. line = line.split(",") # csv
  1070. if i >= _x and i%_x == 0:
  1071. x=0
  1072. y+=1
  1073. if y >= _y:
  1074. break
  1075. #i = int(line[0])
  1076. _id = int(line[1])
  1077. #univ = int(line[0])
  1078. #dmx = int(line[1])
  1079. #x = int(line[3])
  1080. #y = int(line[4])
  1081. #ch = int(line[4])
  1082. pos = [x,y]
  1083. f = Fix(_id,pos,block) #pos,univ,dmx,ch)
  1084. #f.x = x
  1085. #f.y = y
  1086. #f.block = block
  1087. GRID.append(f)
  1088. x+=1
  1089. #print("y, _y",y,_y)
  1090. return GRID
  1091. def find_pix(x,y):
  1092. global GRID
  1093. for fix in GRID:
  1094. X = 0
  1095. Y = 0
  1096. pos = fix.POS()
  1097. #pos = fix.POS()
  1098. #pos = fix.POS(40,60-pm_wy)
  1099. #pos = fix.POS(x,y+pm_wy)#40,60)
  1100. if x > pos[0] and x < pos[0]+pos[2]:
  1101. X = 1
  1102. if y > pos[1] and y < pos[1]+pos[3]:
  1103. Y = 1
  1104. if X and Y:
  1105. #print(pos,x,y)
  1106. #print("find",X,Y)
  1107. return fix
  1108. GRID = []
  1109. _GRID = []
  1110. _GRID = init_grid(_x=8,_y=8,mapping=1) #init_gird()
  1111. NR = 0
  1112. START_UNIV=2
  1113. if options.start_univ:
  1114. try:
  1115. START_UNIV=int(options.start_univ)
  1116. except Exception as e:
  1117. print("Exception START UNIV",e)
  1118. gobo_ch=1
  1119. if options.gobo_ch:
  1120. try:
  1121. gobo_ch=int(options.gobo_ch)
  1122. except Exception as e:
  1123. print("Exception gobo_ch",e)
  1124. if gobo_ch <= 0:
  1125. gobo_ch = 1
  1126. def draw_box(pos1,pos2,color=[128,128,128],text=1):
  1127. color = [200,0,0,127]
  1128. if text:
  1129. fr = font15.render("A" ,1, (200,200,200))
  1130. window.blit(fr,pos1)
  1131. fr = font15.render("B" ,1, (200,200,200))
  1132. window.blit(fr,[pos2[0]-10,pos2[1]-10])
  1133. # h unten
  1134. _pos1 = [pos1[0],pos2[1]]
  1135. _pos2 = [pos2[0],pos2[1]]
  1136. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1137. color = [255,255,0,127]
  1138. # h rechts
  1139. _pos1 = [pos2[0],pos1[1]]
  1140. _pos2 = [pos2[0],pos2[1]]
  1141. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1142. color = [0,200,0,127]
  1143. # h links
  1144. _pos1 = [pos1[0],pos1[1]]
  1145. _pos2 = [pos1[0],pos2[1]]
  1146. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1147. color = [0,0,200,127]
  1148. # h oben
  1149. _pos1 = [pos1[0],pos1[1]]
  1150. _pos2 = [pos2[0],pos1[1]]
  1151. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1152. def grab(x=55,y=55,w=60,h=60):
  1153. # usage
  1154. # sub = grab()
  1155. # window.blit(sub, (500,10))
  1156. crop = None
  1157. rect = pygame.Rect(x, y, w, h)
  1158. try:
  1159. sub = window.subsurface(rect)
  1160. #pixArray = pygame.PixelArray(screen)
  1161. crop = pygame.Surface((w,h))
  1162. crop.blit(sub, (0,0))
  1163. except ValueError as e:
  1164. pass#print("exception",e,"line715")
  1165. #except Exception as e:
  1166. # print("exception",e,"line715")
  1167. #print(rect)
  1168. return crop
  1169. def reshape(_x,_y):
  1170. """reshape LED-WALL Block/Pixel mapping"""
  1171. if PIXEL_MAPPING <= 0:
  1172. return None
  1173. global GRID
  1174. global _GRID
  1175. x = _x
  1176. y = _y
  1177. i = 0
  1178. counter = 0
  1179. z=0
  1180. x_min = 99999
  1181. x_max = 0
  1182. y_min = 99999
  1183. y_max = 0
  1184. # black background for -> output MAP
  1185. pygame.draw.rect(window,[0,0,20],[0,60,600,pm_wy-1])
  1186. pygame.draw.rect(window,[0,0,20],[wx,wy-80,400,pm_wy+10])
  1187. tmp_font = pygame.font.SysFont("freemonobold",int(p*0.8))
  1188. fr = tmp_font.render("OUTPUT" ,1, (255,255,255))
  1189. fr_r = fr.get_rect(center=(x+int(wx/2),int(60+pm_wy-p*1.5)))
  1190. window.blit(fr,fr_r)
  1191. fr = tmp_font.render("↑ ↑ MAP ↑ ↑" ,1, (255,255,255))
  1192. fr_r = fr.get_rect(center=(x+int(wx/2),int(60+pm_wy-p)))
  1193. window.blit(fr,fr_r)
  1194. fr = tmp_font.render("INPUT" ,1, (255,255,255))
  1195. fr_r = fr.get_rect(center=(x+int(wx/2),int(60+pm_wy-p/2)))
  1196. window.blit(fr,fr_r)
  1197. j = 0
  1198. for fix in _GRID:
  1199. if j >= 8*8: # max output size
  1200. break
  1201. j+=1
  1202. ii = i
  1203. #z= i # helping border offset
  1204. pos = fix.POS(40,60)
  1205. rgb = fix.rgb
  1206. # green
  1207. pygame.draw.rect(window,[0,40,0],pos)
  1208. xposs = [] #None #pos[:]
  1209. for fix2 in GRID:
  1210. if fix._id == fix2._id:
  1211. xposs.append( fix2.POS(40,60) )
  1212. for xpos in xposs:
  1213. sub = grab(xpos[0],xpos[1]+pm_wy,xpos[2],xpos[3])
  1214. if sub:
  1215. if 1:#j <= _x*_y: # max input size
  1216. window.blit(sub, (x+pos[0]+z,y+pos[1]+z))
  1217. else:
  1218. # red
  1219. pygame.draw.rect(window,[40,0,0],pos) #[x+pos[0]+2+z,y+pos[1]+2+z-pm_wy,12,9])
  1220. if xpos[0] < x_min:
  1221. x_min = xpos[0]
  1222. if xpos[0] > x_max:
  1223. x_max += xpos[2]
  1224. if xpos[1] < y_min:
  1225. y_min = xpos[1]
  1226. if xpos[1] > x_max:
  1227. y_max += xpos[3]
  1228. # DRAW FIX NUMBER on TOP
  1229. #apos = pos
  1230. #argb = rgb
  1231. apos = fix.POS(40,60+pm_wy)
  1232. argb = fix.rgb
  1233. # overwrite number overlay
  1234. if NR:
  1235. #pygame.draw.rect(window,[30,40,0],apos)
  1236. pygame.draw.rect(window,[20,40,0],[x+apos[0]+2+z,y+apos[1]+2+z-pm_wy,12,9])
  1237. if NR:# == 2:
  1238. if fix._id != i+1:
  1239. fr = font15.render("{:02}".format(fix._id) ,1, (255,255,0))
  1240. window.blit(fr,(x+apos[0]+2+z,y+apos[1]+2+z-pm_wy))
  1241. #print(fix._id,xposs,pos)
  1242. else:
  1243. fr = font15.render("{:02}".format(fix._id) ,1, (100,100,255))
  1244. window.blit(fr,(x+apos[0]+2+z,y+apos[1]+2+z-pm_wy))
  1245. i += 1
  1246. #print("--#")
  1247. # frame box
  1248. #pygame.draw.box(window,[100,0,0],[x+x_min,y+x_min,x_max+x_min,y_min+y_max])
  1249. pos1= [x+x_min,y+x_min]
  1250. pos2= [x_max+x_min,y_min+y_max]
  1251. #draw_box(pos1,pos2,text=0)
  1252. class Timer():
  1253. def __init__(self,start=120):
  1254. self.start = start
  1255. self.timer = self.start
  1256. self.timer_t = time.time()
  1257. def reset(self):
  1258. self.timer = self.start
  1259. def get(self):
  1260. self.timer -= time.time()-self.timer_t
  1261. self.timer_t = time.time()
  1262. if self.timer <= 0:
  1263. self.reset()
  1264. return self.timer
  1265. def reload_grid():
  1266. print("==== reload_grid")
  1267. global GRID
  1268. global _GRID
  1269. try:
  1270. GRID = init_grid(_x=_x,_y=_y) #init_gird()
  1271. _GRID = init_grid(_x=8,_y=8,mapping=1) #init_gird()
  1272. except Exception as e:
  1273. print("Except: grid re init",e)
  1274. VPU_TEXT = []
  1275. def load_vpu_text(nr=0):
  1276. txt = "NONE"
  1277. if not VPU_TEXT:
  1278. pass #create VPU_TEXT_FILE
  1279. if len(VPU_TEXT) > nr:
  1280. txt = VPU_TEXT[0]
  1281. return txt
  1282. grid_counter = time.time()
  1283. def draw_fix_nr(GRID):
  1284. global grid_counter
  1285. i=0
  1286. y=0
  1287. for fix in GRID:
  1288. pos = fix.POS(40,60+pm_wy)
  1289. rgb = fix.rgb
  1290. if NR:
  1291. pygame.draw.rect(window,[0,0,0],[pos[0]+2,pos[1]+2,12,9])
  1292. if fix._id%_x-1 == 0: # line break border
  1293. pygame.draw.line(window,[255,255,0],(pos[0],pos[1]+4),(pos[0],pos[1]+pos[3]-4),1)
  1294. 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)
  1295. if fix._id%_x == 0: # line break border
  1296. 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)
  1297. 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)
  1298. if grid_counter +5 < time.time():
  1299. grid_counter = time.time()
  1300. reload_grid()
  1301. if fix._id != i+1:
  1302. fr = font15.render("{:02}".format(fix._id) ,1, (255,255,0))
  1303. else:
  1304. fr = font15.render("{:02}".format(fix._id) ,1, (100,100,255))
  1305. window.blit(fr,(pos[0]+2,pos[1]+2))
  1306. i += 1
  1307. def draw_counter(COUNTER):
  1308. for count in COUNTER:
  1309. cpan = 0
  1310. ctilt = 0
  1311. cr=255
  1312. cg=255
  1313. cb=255
  1314. csize=10
  1315. cdim=0
  1316. k = "DIM"
  1317. if k in count:
  1318. cdim = int(count[k])
  1319. k = "RED"
  1320. if k in count:
  1321. cr = int(count[k])
  1322. k = "GREEN"
  1323. if k in count:
  1324. cg = int(count[k])
  1325. k = "BLUE"
  1326. if k in count:
  1327. cb = int(count[k])
  1328. k = "SIZE"
  1329. if k in count:
  1330. csize = int(count[k]) #/60*p)
  1331. if csize < 5:
  1332. csize = 5
  1333. k = "PAN"
  1334. if k in count:
  1335. cpan = int(count[k])/255*(block[0] *(_x))
  1336. #cpan = int(cpan * 1.2)
  1337. cpan = int(cpan)
  1338. k = "TILT"
  1339. if k in count:
  1340. ctilt = int(count[k])/255*(block[1] *(_y))
  1341. #ctilt = int(cpan * 1.2)
  1342. ctilt = int(ctilt)
  1343. ddim = cdim/255
  1344. if "DIM" in count and count["DIM"] > 0:
  1345. tmp_font = pygame.font.SysFont("freemonobold",int(p/100*csize))
  1346. rgb =(int(cr*ddim),int(cg*ddim),int(cb*ddim),cdim)
  1347. _sec = count["_SEC"]
  1348. try:
  1349. _sec = int(count["_SEC"])
  1350. except:
  1351. pass
  1352. if type(_sec) is int:
  1353. #print(_sec)
  1354. if _sec <= 60:
  1355. fr = tmp_font.render("{:0}".format(_sec) ,1, rgb)
  1356. else:
  1357. _xx = time.strftime("%M:%S",time.localtime(_sec))
  1358. #print("_xx",_xx)
  1359. fr = tmp_font.render("{}".format(_xx) ,1, rgb)
  1360. else:
  1361. fr = tmp_font.render("{}".format((count["_SEC"])) ,1, rgb)
  1362. fr_r = fr.get_rect(center=(40+cpan-(block[0]),60+ctilt+pm_wy))
  1363. pygame.draw.rect(window,[0,0,0],fr_r)
  1364. window.blit(fr,fr_r)
  1365. class FADE():
  1366. def __init__(self,sec):
  1367. self.start = time.time()
  1368. self.sec = sec
  1369. self.val
  1370. def next(self):
  1371. pass
  1372. def draw_video(VIDEO):
  1373. global videplayer
  1374. i = 0
  1375. # set DMX-VALUE to videoplayer Object
  1376. for count in VIDEO:
  1377. cpan = 0
  1378. ctilt = 0
  1379. cr=255
  1380. cg=255
  1381. cb=255
  1382. csize=10
  1383. cang=0
  1384. cdim=0
  1385. video1 = videoplayer[i]
  1386. k = "VIDEO"
  1387. if k in count:
  1388. #video1.select_video(count[k])
  1389. play_nr = int(count[k]/10)
  1390. if play_nr != video1._video_nr:
  1391. print( "+ + + + + + + + + + +", play_nr , video1._video_nr ,str(video1))
  1392. _vid = video1._id
  1393. _cdmx = video1.dmx
  1394. del_video1 = video1
  1395. video1 = Vopen(cdmx,_id=_vid)
  1396. video1.select_video(count[k])
  1397. videoplayer[i] = video1
  1398. del_video1._del()
  1399. del del_video1
  1400. k = "DIM"
  1401. if k in count:
  1402. cdim = int(count[k])
  1403. video1.dim = cdim
  1404. #if i == 0:
  1405. # print(i,cdim)
  1406. k = "ANG"
  1407. if k in count:
  1408. cang = int(count[k])
  1409. k = "SIZE"
  1410. if k in count:
  1411. csize = int(count[k]/16*p)
  1412. if csize < 5:
  1413. csize = 5
  1414. k = "PAN"
  1415. cpan_max = block[0] *(_x) #+block[0]
  1416. if k in count:
  1417. cpan = int(count[k]) / 255*cpan_max
  1418. cpan = int(cpan)
  1419. k = "TILT"
  1420. ctilt_max = block[1] *(_y) #+block[1]
  1421. if k in count:
  1422. ctilt = int(count[k]) / 255*ctilt_max
  1423. ctilt = int(ctilt)
  1424. k = "_reset"
  1425. if k in count:
  1426. if count[k]:
  1427. count[k] = 0
  1428. video1.restart()
  1429. k = "_RUN"
  1430. if k in count:
  1431. video1._run = count[k]
  1432. video1.pos
  1433. video1.x=40+0+cpan
  1434. video1.y=60+0+pm_wy+ctilt
  1435. video1.scale = int((csize))
  1436. video1.angle = int(360-(cang))
  1437. if cdim:
  1438. video1.next()
  1439. i += 1
  1440. # draw
  1441. i=0
  1442. for count in VIDEO:
  1443. video1 = videoplayer[i]
  1444. video1.draw(window) #,x=0,y=0)
  1445. i+=1
  1446. i=0
  1447. for count in VIDEO:
  1448. video1 = videoplayer[i]
  1449. video1.overlay(window,"run")
  1450. i += 1
  1451. def counter_dmx(COUNTER,dataA):
  1452. for count in COUNTER:
  1453. cDMX=count["DMX"]-1
  1454. try:
  1455. count["DIM"] = dataA[cDMX]
  1456. count["PAN"] = dataA[cDMX+1]
  1457. count["TILT"] = dataA[cDMX+2]
  1458. count["CONTROL"] = dataA[cDMX+3]
  1459. if count["CONTROL"] >= 10 and count["CONTROL"] < 20:
  1460. count["_SEC"] = int(count["SEC"] - (time.time() - count["_time"]))
  1461. if count["CONTROL"] >= 20 and count["CONTROL"] < 30:
  1462. count["_RUN"] = 0
  1463. if count["CONTROL"] >= 30 and count["CONTROL"] < 40:
  1464. count["_RUN"] = 1
  1465. count["SIZE"] = dataA[cDMX+4]
  1466. count["SEC"] = dataA[cDMX+5]
  1467. if count["_RUN"]:
  1468. try:
  1469. count["_SEC"] = int(count["SEC"] - (time.time() - count["_time"]))
  1470. except Exception as e:
  1471. pass
  1472. if type(count["_SEC"]) is int:
  1473. if count["_SEC"] < 0:
  1474. count["_SEC"] = 0
  1475. for ti in range(10):
  1476. #print(ti,(ti+6)*10)
  1477. if count["CONTROL"] >= (ti+6)*10 and count["CONTROL"] < (ti+7)*10:
  1478. count["_SEC"] = "----" #text 1
  1479. try:
  1480. count["_SEC"] = TEXT_BLOCK[ti]
  1481. except Exception as e:
  1482. pass
  1483. if count["CONTROL"] >= 250 and count["CONTROL"] < 256:
  1484. count["_SEC"] = ">{}<".format(cDMX+1)
  1485. count["RED"] = dataA[cDMX+6]
  1486. count["GREEN"] = dataA[cDMX+7]
  1487. count["BLUE"] = dataA[cDMX+8]
  1488. except Exception as e:
  1489. print("EXC FUNC",e,count)
  1490. #print(count)
  1491. def video_dmx(VIDEO,dataA):
  1492. for count in VIDEO:
  1493. cDMX=count["DMX"]-1
  1494. try:
  1495. count["DIM"] = dataA[cDMX]
  1496. count["PAN"] = dataA[cDMX+1]
  1497. count["TILT"] = dataA[cDMX+2]
  1498. count["CONTROL"] = dataA[cDMX+3]
  1499. if count["CONTROL"] >= 10 and count["CONTROL"] < 20:
  1500. count["_reset"] = 1
  1501. if count["CONTROL"] >= 20 and count["CONTROL"] < 30:
  1502. if count["_RUN"] == 1:
  1503. print( "_RUN:0",count["DMX"]-1)
  1504. count["_RUN"] = 0
  1505. if count["CONTROL"] >= 30 and count["CONTROL"] < 40:
  1506. if count["_RUN"] == 0:
  1507. print( "_RUN:1",count["DMX"]-1)
  1508. count["_RUN"] = 1
  1509. count["SIZE"] = dataA[cDMX+4]#*2
  1510. count["SEC"] = dataA[cDMX+5]
  1511. #count["RED"] = dataA[cDMX+6]
  1512. count["VIDEO"] = dataA[cDMX+6]
  1513. #count["GREEN"] = dataA[cDMX+7]
  1514. count["ANG"] = (int(dataA[cDMX+7])/255*360)
  1515. #count["BLUE"] = dataA[cDMX+8]
  1516. except Exception as e:
  1517. print("VIDEOPLAYER EXCEPT FUNC",e,count)
  1518. #print(count)
  1519. def read_dmx_data(ip,ips):
  1520. ip = select_ip(ips,univ=START_UNIV)
  1521. IP = ip
  1522. data = read_dmx(ip)
  1523. ip = select_ip(ips,univ=START_UNIV+1)
  1524. data3 = read_dmx(ip)
  1525. data.extend(data3)
  1526. ip = select_ip(ips,univ=START_UNIV+2)
  1527. data3 = read_dmx(ip)
  1528. data.extend(data3)
  1529. ip = select_ip(ips,univ=START_UNIV+4)
  1530. data3 = read_dmx(ip)
  1531. data.extend(data3)
  1532. #ip = select_ip(ips,univ=START_UNIV+5)
  1533. #data3 = read_dmx(ip)
  1534. #data.extend(data3)
  1535. return data
  1536. def draw_gobo(GRID,data):
  1537. i = 0
  1538. dmx = 1
  1539. h = 1
  1540. v = 1
  1541. for fix in GRID:
  1542. pos = fix.POS(40,60+pm_wy)
  1543. rgb = fix.rgb
  1544. if 1:
  1545. # draw row/col grid number
  1546. if fix.pos[0] == 0:
  1547. fr = font12.render("{}".format(fix.pos[1]+1) ,1, (200,200,200))
  1548. window.blit(fr,(10,pos[1]+3 ))
  1549. if fix.pos[1] == 0:
  1550. fr = font12.render("{}".format(fix.pos[0]+1) ,1, (200,200,200))
  1551. window.blit(fr,(pos[0]+2,35 ))
  1552. pygame.draw.rect(window,rgb,pos)
  1553. # DRAW SUB-FIXTURE
  1554. j = 0
  1555. for subfix in fix.sub_fix:#calc(data):
  1556. subfix.calc(data)
  1557. #fix = subfix
  1558. spos = subfix.POS(40,60+pm_wy)
  1559. srgb = subfix.rgb
  1560. #print(fix.dmx,rgb,pos)
  1561. #pygame.draw.circle(window,rgb,(pos[0]+int(pos[2]/2),pos[1]+int(pos[3]/2)),int(pos[3]/2))
  1562. #FUNC = 0
  1563. if FUNC > 10 and FUNC <= 20: # big dot
  1564. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1565. elif FUNC > 20 and FUNC <= 30:#small dot
  1566. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1567. elif FUNC > 30 and FUNC <= 40:#donut
  1568. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1569. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1570. elif FUNC > 40 and FUNC <= 50: # rec with hole
  1571. pygame.draw.rect(window,srgb,spos)
  1572. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1573. elif FUNC > 50 and FUNC <= 60: # rec with big hole
  1574. pygame.draw.rect(window,srgb,spos)
  1575. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1576. elif FUNC > 60 and FUNC <= 70: # rec with donat
  1577. pygame.draw.rect(window,srgb,spos)
  1578. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1579. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1580. elif FUNC > 70 and FUNC <= 80: # rec boarder
  1581. pygame.draw.rect(window,srgb,[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  1582. elif FUNC > 80 and FUNC <= 90: # rec big boarder
  1583. pygame.draw.rect(window,srgb,[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  1584. elif FUNC > 90 and FUNC <= 100: # rec thin line
  1585. pygame.draw.rect(window,srgb,spos)
  1586. pygame.draw.rect(window,[0,0,0],[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  1587. elif FUNC > 100 and FUNC <= 110: # rec big line
  1588. pygame.draw.rect(window,srgb,spos)
  1589. pygame.draw.rect(window,[0,0,0],[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  1590. elif FUNC > 110 and FUNC <= 120: # rec with dot
  1591. pygame.draw.rect(window,srgb,spos)
  1592. pygame.draw.rect(window,[0,0,0],[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  1593. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1594. elif FUNC > 120 and FUNC <= 130: # rec inline
  1595. pygame.draw.rect(window,srgb,[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  1596. pygame.draw.rect(window,[0,0,0],[spos[0]+3,spos[1]+3,spos[2]-6,spos[3]-6])
  1597. elif FUNC > 130 and FUNC <= 140: # 3 dot (heart)
  1598. draw_circle(window,srgb,(spos[0]+int(spos[2]/2)+2,spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1599. draw_circle(window,srgb,(spos[0]+int(spos[2]/2)-2,spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1600. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)+2),int(spos[3]/3.5))
  1601. else:
  1602. pygame.draw.rect(window,srgb,spos)
  1603. for subfix in fix.sub_fix:#calc(data):
  1604. subfix.calc(data)
  1605. #fix = subfix
  1606. spos = subfix.POS(40,60+pm_wy)
  1607. srgb = subfix.rgb
  1608. # draw row/col grid number
  1609. if subfix.pos[0] == 0:
  1610. fr = font12.render("{}".format(v ) ,1, (200,200,200))
  1611. window.blit(fr,(25,spos[1] ))
  1612. v += 1
  1613. if subfix.pos[1] == 0:
  1614. fr = font12.render("{}".format(1) ,1, (200,200,200))
  1615. fr = font12.render("{}".format(h ) ,1, (200,200,200))
  1616. h+=1
  1617. window.blit(fr,(spos[0],50 ))
  1618. if p >= 40:
  1619. if NR:
  1620. #fr = font15.render("{:02}".format(j+1) ,1, (0,200,255))
  1621. fr = font15.render("{:02}".format(subfix._id) ,1, (250,200,5))
  1622. window.blit(fr,(spos[0]+2,spos[1]+10))
  1623. j += 1
  1624. i += 1
  1625. def frame_area():
  1626. rgb = [255,255,0]
  1627. CFG = CFG_IN
  1628. p1 = CFG_IN["p1"]
  1629. p2 = CFG_IN["p2"]
  1630. draw_frame(window,rgb,p1,p2,offset=2)
  1631. rgb = [255,0,0]
  1632. #draw_frame(window,rgb,p1,p2,offset=4)
  1633. if CFG_OUT["on"]:
  1634. rgb = [255,0,0]
  1635. p1 = CFG_OUT["p1"]
  1636. p2 = CFG_OUT["p2"]
  1637. #pygame.draw.line(window,rgb,p1,p2)
  1638. draw_frame(window,rgb,p1,p2,offset=3)
  1639. ips=[]
  1640. dataA=[]
  1641. data=[]
  1642. def dmx_raw():
  1643. global frame
  1644. frame += 1
  1645. global ips,dataA,data
  1646. ips = read_index()
  1647. # ----
  1648. ip = select_ip(ips,univ=1) # univ 1 gobo
  1649. dataA = read_dmx(ip)
  1650. # ----
  1651. data = read_dmx_data(ip,ips)
  1652. if options.countdown:
  1653. counter_dmx(COUNTER,dataA)
  1654. if len(VIDEO) > 0:
  1655. video_dmx(VIDEO,dataA)
  1656. return ips,dataA,data
  1657. def dmx_loop():
  1658. while 1:
  1659. dmx_raw()
  1660. time.sleep(1/40) # fast high cpu
  1661. #time.sleep(1/25)
  1662. dmx_raw()
  1663. thread.start_new_thread(dmx_loop,())
  1664. t1 = Timer(143)
  1665. time.sleep(0.33)
  1666. t2 = Timer(11)
  1667. count_tilt = 0
  1668. def main():
  1669. global IP
  1670. global GRID
  1671. global _GRID
  1672. global FUNC
  1673. global count_tilt
  1674. global TEXT_BLOCK
  1675. global TEXT_BLOCK_TIME
  1676. global PLAYLIST
  1677. global PLAYLIST_TIME
  1678. global dataA
  1679. global frame2
  1680. GRID = init_grid(_x=_x,_y=_y) #init_gird()
  1681. #GRID = init_grid(_x=8,_y=8) #init_gird()
  1682. print("GRID LEN:",len(GRID))
  1683. s=time.time()
  1684. print("run")
  1685. r = ""
  1686. IP = "xx"
  1687. while running:
  1688. if TEXT_BLOCK_TIME+5 < time.time():
  1689. TEXT_BLOCK = open_text_block()
  1690. TEXT_BLOCK_TIME = time.time()
  1691. if PLAYLIST_TIME+6 < time.time():
  1692. PLAYLIST = open_playlist()
  1693. PLAYLIST_TIME = time.time()
  1694. pygame.display.flip()
  1695. event()
  1696. window.fill((10,0,30))
  1697. calc_fps()
  1698. calc_fps2()
  1699. draw_overlay()
  1700. # GRID loop
  1701. try:
  1702. ddd = 1023 #univ 3 512
  1703. #FUNC = data[ddd]
  1704. FUNC2 = dataA[gobo_ch-1]
  1705. FUNC = FUNC2
  1706. #print("FUNC", FUNC )#:ddd+512])
  1707. #FUNC = 15
  1708. except Exception as e:
  1709. print("EXC FUNC",e)
  1710. draw_gobo(GRID,data)
  1711. # DRAW FIX NUMBER on TOP
  1712. draw_fix_nr(GRID)
  1713. #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})
  1714. if VIDEO:
  1715. draw_video(VIDEO)
  1716. if options.countdown:
  1717. draw_counter(COUNTER)
  1718. pointer.draw(0,pm_wy) #wy
  1719. spos = [0,0,0,0]
  1720. if PIXEL_MAPPING >= 1:
  1721. reshape(0,0) #start pos
  1722. else:
  1723. reshape(spos[0]+spos[2]+20,10) #start pos
  1724. #reshape(spos[0]+spos[2]+20,10) #start pos
  1725. frame_area()
  1726. pygame.display.flip()
  1727. #pg.time.wait(55)
  1728. #clock.tick(120) # fast hight cpu
  1729. clock.tick(25)
  1730. #time.sleep(1/120)
  1731. if 'SDL_VIDEO_WINDOW_POS' in os.environ:
  1732. del os.environ['SDL_VIDEO_WINDOW_POS'] #= '%i,%i' % (200,164)
  1733. #if 'SDL_VIDEO_CENTERED' in os.environ['SDL_VIDEO_CENTERED']:
  1734. # del os.environ['SDL_VIDEO_CENTERED'] #= '0'
  1735. frame2 += 1
  1736. if __name__ == "__main__":
  1737. main()