vpu_live.py 64 KB

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