vpu_live.py 61 KB

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