vpu_live.py 60 KB

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