vpu_live.py 62 KB

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