vpu_live.py 65 KB

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