vpu_live.py 61 KB

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