vpu_live.py 62 KB

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