vpu_live.py 62 KB

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