vpu_live.py 67 KB

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