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