vpu_live4.py 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925
  1. #!/usr/bin/python3
  2. import math
  3. import random
  4. import time
  5. import os
  6. import sys
  7. import traceback
  8. import gc
  9. sys.path.insert(0,"/opt/LibreLight/Xdesk/")
  10. from lib.cprint import cprint
  11. HOME = os.getenv('HOME')
  12. import _thread as thread
  13. from optparse import OptionParser
  14. ...
  15. parser = OptionParser()
  16. parser.add_option("-m", "--mode", dest="mode",
  17. help="pixel mode pix,x,y --mode 40,10,8") #, metavar="FILE")
  18. parser.add_option("-X", "--XX", dest="XX", #default=1,
  19. help="x-split") #, metavar="FILE")
  20. parser.add_option("-x", "--xx", dest="xsplit", #default=1,
  21. help="x-split") #, metavar="FILE")
  22. parser.add_option("-y", "--yy", dest="ysplit",#default=1,
  23. help="y-split") #, metavar="FILE")
  24. parser.add_option("", "--start-univ", dest="start_univ",default=2,
  25. help="set start-univers default=2") #, metavar="FILE")
  26. parser.add_option("", "--gobo-ch", dest="gobo_ch",default=0,
  27. help="gobo ch univ on 1") #, metavar="FILE")
  28. parser.add_option("", "--gobo-ch2", dest="gobo_ch2",default=0,
  29. help="gobo ch univ on 1") #, metavar="FILE")
  30. #os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (200,164)
  31. parser.add_option("", "--win-pos", dest="win_pos",default="200,164",
  32. help="SDL_VIDEO_WINDOW_POS --win-pos=200,164") #, metavar="FILE")
  33. parser.add_option("", "--pixel-mapping", dest="pixel_mapping",default=0,
  34. help="pixel_mapping file/on --pixel-mapping=_x") #, metavar="FILE")
  35. parser.add_option("", "--dual-vpu", dest="dual_vpu",default=0,
  36. help=" --dual-vpu=1/0") #, metavar="FILE")
  37. parser.add_option("", "--grid-a1-idim", dest="grid_a1_idim",default=0,
  38. help=" grid_a1_idim") #, metavar="FILE")
  39. parser.add_option("", "--grid-a2-idim", dest="grid_a2_idim",default=0,
  40. help=" grid_a2_idim") #, metavar="FILE")
  41. parser.add_option("", "--countdown", dest="countdown",#default=1,
  42. help="enable countdown") #, metavar="FILE")
  43. parser.add_option("", "--videoplayer", dest="videoplayer",#default=1,
  44. help="enable videoplayer") #, metavar="FILE")
  45. parser.add_option("", "--title", dest="title",default="SCREEN",
  46. help="set title") #, metavar="FILE")
  47. (options, args) = parser.parse_args()
  48. import numpy
  49. QUIT = 0
  50. for o in dir(options):
  51. if "_" in o:
  52. continue
  53. print(o,options.__getattribute__(o))
  54. START = time.time()
  55. # ===== ARTNET DMX =========
  56. import memcache
  57. mc = memcache.Client(['127.0.0.1:11211'], debug=0)
  58. def read_index():
  59. ips=mc.get("index")#cmd)
  60. if ips is None:
  61. ips = {}
  62. return ips
  63. def select_ip(ips, univ=2): # artnet univ
  64. _univ = ":{}".format(univ)
  65. for ip in ips: #high priority
  66. if "2.0.0" in ip and _univ in ip:
  67. return ip
  68. for ip in ips:
  69. if "ltp-out" in ip and _univ in ip:
  70. return ip
  71. INPUT_START_POS_X = 40
  72. INPUT_START_POS_Y = 236 #320
  73. FUNC = 0
  74. COUNTER = []
  75. if options.countdown:
  76. cdmx_start = options.countdown.split(",")
  77. for cdmx in cdmx_start:
  78. try:
  79. cdmx = int(cdmx)
  80. _tmp={"DMX":cdmx,"DIM":0,"PAN":127,"TILT":127
  81. ,"CONTROL":0,"SEC":10
  82. ,"RED":255,"GREEN":255,"BLUE":255
  83. ,"_time":time.time(),"_RUN":0,"_pause":0,"_start":0
  84. ,"_SEC":">{}<".format(cdmx)
  85. }
  86. COUNTER.append(_tmp)
  87. except Exception as e:
  88. cprint("EXCEPTION COUNTER INIT ",cdmx,color="red")
  89. def read_dmx(ip):
  90. r = ""
  91. if ip:
  92. #t = int(math.sin(time.time() - s)*10)
  93. r = mc.get(ip) #"2.0.0.13:2")
  94. rr = [0]*512
  95. for i,v in enumerate(r):
  96. try: #cleanup ltp-out to int
  97. v = int(v)
  98. rr[i] = v
  99. except:pass
  100. r = rr
  101. if not r:
  102. c = 0
  103. #time.sleep(0.1)
  104. r = [0] *512
  105. for i in range(12*8+1):
  106. dmx = i*4
  107. #print(dmx)
  108. r[dmx:dmx+4] = [255,10,10,40]
  109. return r
  110. # ===== ARTNET DMX =========
  111. MEDIALIST = []
  112. try:
  113. MEDIALIST = open_medialist()
  114. except:pass
  115. #import json
  116. #import pickle
  117. from tool.video_capture import scan_capture #as scan_capture
  118. def _cam_cfg_new(fn):
  119. #fn = HOME+'/LibreLight/video_in.txt'
  120. txt = [
  121. "# Example",
  122. "d0 w640 h480 # PAL",
  123. "d0 w800 h600 # SVGA",
  124. "d0 w960 h540 # 540p",
  125. "d0 w1024 h768 # XGA <---",
  126. "d0 w1280 h720 # 720p",
  127. "d0 w1440 h900 # WSXGA+",
  128. "d0 w1600 h900 # WSXGA - Laptop 16:9",
  129. "d0 w1920 h1080 # HD 1080p",
  130. "",
  131. "# config",
  132. "d0 w1024 h768 # XGA",
  133. ]
  134. f = open(fn ,"w")
  135. f.write("\n".join(txt))
  136. f.close()
  137. def VideoMeta():
  138. out = {}
  139. out["run"] = 1
  140. out["x"] = 0
  141. out["y"] = 0
  142. out["ang"] = 0
  143. out["scale"] = 0
  144. out["fname"] = "xxx.mp4"
  145. out["fpath"] = "xxx.mp4"
  146. class VideoPlayer(): # concept / mokup
  147. def __init__():
  148. self.data = VideoMeta()
  149. def play(self):
  150. pass
  151. def pause(self):
  152. pass
  153. def reset(self):
  154. pass
  155. def select(self):
  156. pass
  157. def draw(self):
  158. pass
  159. def _rescale_frame_by_width(frame, width):
  160. #print(type(frame)) #np array
  161. height = int(frame.shape[0]/frame.shape[1] * width )
  162. dim = (width, height)
  163. #return cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)
  164. return cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
  165. def _rescale_frame_by_procent(frame, percent=75):
  166. #print(type(frame))
  167. width = int(frame.shape[1] * percent/ 100)
  168. height = int(frame.shape[0] * percent/ 100)
  169. dim = (width, height)
  170. return cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
  171. def _moveImage(img,x=0,y=0,shape=None):
  172. #print(type(img)) #np.array
  173. # Creating a translation matrix
  174. np = numpy
  175. translation_matrix = np.float32([ [1,0,x], [0,1,y] ])
  176. if shape is None:
  177. num_cols,num_rows = img.shape[1::-1]
  178. else:
  179. num_cols,num_rows = shape #[1::-1]
  180. # Image translation
  181. frame = cv2.warpAffine(img, translation_matrix, (num_cols,num_rows))
  182. return frame
  183. def _rotateImage(image, angle):
  184. #print(type(image)) # nm.array
  185. if angle in [0,360]:
  186. pass#return image
  187. try:
  188. #print("EE",image.shape)
  189. #print("EE",dir(image))
  190. shape = list(image.shape[1::-1])
  191. bg_shape = shape[:]
  192. delta_x = 0 # + nach rechts
  193. delta_y = 0 # + nach unten
  194. r = []
  195. r.extend(list(range(0-3,0+3)))
  196. r.extend(list(range(180-3,180+3)))
  197. r.extend(list(range(360-3,360+3)))
  198. r2 = []
  199. r2.extend(list(range(90-3,90+3)))
  200. r2.extend(list(range(270-3,270+3)))
  201. if angle in r:
  202. pass
  203. elif 0:#angle in r2:
  204. bg_shape = bg_shape[::-1]
  205. delta_y = -65 #shape[1]/2 # # + nach unten
  206. delta_x = 30 #shape[1]/2 # # + nach unten
  207. delta_y -= bg_shape[1]/2 - 20
  208. delta_x = bg_shape[0]/2 - 10
  209. pass
  210. else:
  211. if shape[0] > shape[1]:
  212. # landscape video
  213. bg_shape = (shape[0],shape[0])
  214. delta_y = (shape[0]-shape[1])/2
  215. else:
  216. # portrait video
  217. bg_shape = (shape[1],shape[1])
  218. delta_x = (shape[1]-shape[0])/2
  219. center = (shape[0]/2+delta_x,shape[1]/2+delta_y)
  220. image = _moveImage(image,x=delta_x,y=delta_y,shape=bg_shape)
  221. bg_shape = tuple(bg_shape) # exception ... if angle 0 no tuple !!!
  222. rot_mat = cv2.getRotationMatrix2D(center,angle,1.0)
  223. frame = cv2.warpAffine(image, rot_mat, bg_shape ) #,flags=cv2.INTER_LINEAR)
  224. return frame
  225. except Exception as e:
  226. raise(e)
  227. def _cam_cfg():
  228. fn = HOME+'/LibreLight/video_in.txt'
  229. if not os.path.isfile(fn):
  230. _cam_cfg_new(fn)
  231. f = open(fn ,"r")
  232. lines = f.readlines()
  233. f.close()
  234. cfg = ""
  235. for i in lines:
  236. if i.startswith("d"):
  237. cfg = i
  238. cfg = cfg.split()
  239. capture = scan_capture(name="MiraBox",serial="")
  240. #capture = scan_capture(name="Integrated",serial="")
  241. if capture:
  242. print("capture",capture)
  243. try:
  244. cfg.append(capture[0][0])
  245. except Exception as e:
  246. cprint("except 2992",e,color="red")
  247. else:
  248. cprint("---- ERROR NO VIDEO CAPTURE DEV - FOUND ----",color="red")
  249. print("_cam_cfg:",len(cfg),[cfg])
  250. return cfg
  251. def _open_cam(obj):
  252. # LIVE VIDEO INPUT UVC DEVICE
  253. cfg = _cam_cfg()
  254. print("III",cfg)
  255. res = "HD"
  256. #res = "4K"
  257. #res = ""
  258. if res == "HD":
  259. # HD
  260. w = 1920
  261. h = 1080
  262. elif res == "4K":
  263. # 4K
  264. w = 3840
  265. h = 2160
  266. else:
  267. # LOW
  268. w = 1280
  269. h = 720
  270. d = 0 # VIDEO INPUT DEV
  271. for c in cfg:
  272. try:
  273. if c.startswith("d"):
  274. d = int(c[1:])
  275. if c.startswith("h"):
  276. h = int(c[1:])
  277. if c.startswith("w"):
  278. w = int(c[1:])
  279. if c.startswith("video"):
  280. d = int(c[-1])
  281. except Exception as e:
  282. cprint("Exception", "cam_cfg 878",e,color="red")
  283. df = "video{}".format(d)
  284. if obj._stop:
  285. df += "_stop"
  286. elif os.path.exists("/dev/{}".format(df)):
  287. obj.Rcap = cv2.VideoCapture(d)
  288. obj.Rcap.set(cv2.CAP_PROP_FRAME_WIDTH, w)
  289. obj.Rcap.set(cv2.CAP_PROP_FRAME_HEIGHT, h)
  290. else:
  291. #obj.Rcap = None
  292. #obj.end = 1
  293. df += "_error"
  294. if obj.fname.startswith("cam_"):
  295. obj.fname = "cam_{}".format(df)
  296. class PixelMedia():
  297. def __init__(self,dmx=None,_id=None):
  298. global MEDIALIST
  299. self._id = _id
  300. self.fpath = HOME+'/Downloads/'
  301. self.fpath = HOME+'/LibreLight/video/'
  302. self.fname = '' #'bbb_sunflower_480x320.mp4'
  303. #self.fname = 'no-video.mp4'
  304. try:
  305. self.fname = MEDIALIST[0]
  306. except Exception as e:
  307. cprint("Exception set video from MEDIALIST 5543:",e,color="red")
  308. self.img_meta = {}
  309. self.restart_t = time.time()
  310. self.fps = 0
  311. self.scale = 50 #%
  312. self.angle = 0 #%
  313. self.dmx=dmx
  314. if self._id >= 250:
  315. self.fname = "cam_"
  316. self.dim = 0
  317. self.x = 0
  318. self.y = 0
  319. self.init_count = 0
  320. self.cap = None
  321. self.shape = [200,200]
  322. self.img = None
  323. self.success = 1
  324. self._run = 0
  325. self.end = 0
  326. self._media_nr = 0
  327. self._stop = 0 # fix buffer-overflow ... ?? opencv
  328. self.shape_x = 370
  329. self.shape_y = 235
  330. self.init()
  331. def __repr__(self):
  332. sizeof = int(sys.getsizeof(self.buffer)/8)
  333. return "< id:{} buf:{} run:{} fps:{} scale:{} name:{} {}kb>".format(
  334. id(self),len(self.buffer),self._run,self.fps,self.scale
  335. ,self.fname,sizeof
  336. )
  337. def restart(self):
  338. print(self,"reset()")
  339. self.pos = 0
  340. self.restart_t = time.time()
  341. def init(self):
  342. print("---- ---- PixelMedia.init()",[self.fname,self._media_nr])
  343. print(MEDIALIST)
  344. self.time = 0
  345. self.t_delta = 0
  346. self.t_last = time.time()
  347. self.im = None
  348. self.pos = 0
  349. self.buffer = []
  350. self._init()
  351. self.init_count = 1
  352. def select_video(self,dmx_value):
  353. try:
  354. dmx_value = int(dmx_value/10)
  355. if self._media_nr != dmx_value:
  356. self._media_nr = dmx_value
  357. if self._media_nr >= 25:
  358. self.fname = "cam_"
  359. self.init()
  360. elif self._media_nr < len(MEDIALIST):
  361. self.fname = str(MEDIALIST[self._media_nr])
  362. self.init()
  363. #finally:pass
  364. except Exception as e:
  365. cprint("PixelMedia.select_video()",dmx_value,e,color="red")
  366. def _init(self):
  367. print(self)
  368. print("videoplayer.init()",self.fpath,self.fname)
  369. if not os.path.isfile(self.fpath+self.fname):
  370. print()
  371. cprint("video file does not exits !! >",self.fpath,self.fname,color="red")
  372. print()
  373. #exit()
  374. self.Rsuccess = 0
  375. if not cv2:
  376. return
  377. if self._stop:
  378. return
  379. if self.fname.endswith(".jpg"):
  380. self.img_meta = create_img_meta()
  381. self.img_meta["_fpath"] = self.fpath
  382. self.img_meta["fname"] = self.fname
  383. img_open(self.img_meta)
  384. self.buffer = [1,2,3]
  385. return # STOP
  386. if self.fname.startswith("cam_"):
  387. _open_cam(obj=self)
  388. self.img_meta = {}
  389. else:
  390. self.Rcap = cv2.VideoCapture(self.fpath+self.fname, cv2.CAP_FFMPEG)
  391. #FFMPEG malloc(): unsorted double linked list corrupted ... Abgebrochen
  392. self.img_meta = {}
  393. print("_init ?",self.Rcap)
  394. print("open ?" ,self.Rcap.isOpened())
  395. self.Rcap.read()
  396. #self.Rfvs = FileVideoStream(self.fpath+self.fname).start()
  397. self.Rsuccess = 1
  398. self.read()
  399. def _del(self):
  400. if self.img_meta:
  401. return
  402. self._stop = 1
  403. self.buffer = [] #.append(self.img)
  404. self.Rcap.release()
  405. self.Rcap.retrieve()
  406. self.Rcap = None
  407. self.cap = None
  408. del self.Rcap #.release()
  409. gc.collect()
  410. def read(self):
  411. if self.img_meta:
  412. return
  413. success = self.Rsuccess
  414. ok = 0
  415. if success and self.fname:
  416. cap = self.Rcap
  417. #fvs = self.Rfvs
  418. _break = 0
  419. try:
  420. success, img = cap.read()
  421. if not success:
  422. self.Rcap.release()
  423. self.Rcap.retrieve()
  424. self.end = 1
  425. #j = json.dumps(self.buffer) #.append(img)
  426. #f = open("/tmp/buff")
  427. #f.write(j)
  428. #f.close()
  429. return
  430. if self.fps == 0:
  431. self.fps = cap.get(cv2.CAP_PROP_FPS)
  432. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  433. if self.fname.startswith("cam_"):
  434. pass
  435. #img = _rescale_frame_by_width(img, 1200)
  436. else:
  437. img = _rescale_frame_by_width(img, 200)
  438. #img = _rescale_frame_by_width(img, 1200)
  439. # store frame into buffer list
  440. if self.fname.startswith("cam_"):
  441. self.buffer = [ img ]
  442. else:
  443. self.buffer.append(img)
  444. ok = 1
  445. if len(self.buffer) % 100 == 0:
  446. _id = str(self.__repr__)[-5:-1]
  447. print(_id,"video read",self.dmx,len(self.buffer),self.fname,"fps",self.fps,self.dim)
  448. except Exception as e:
  449. cprint("Exception","_init",self,e,end="",color="red")
  450. self.success = 1
  451. return ok
  452. def prev(self):
  453. self.pos -= 1
  454. if self.pos < 0:
  455. self.pos = len(self.buffer)-1
  456. if self.pos >= len(self.buffer):
  457. self.pos = len(self.buffer)-1
  458. self.im = self.buffer[int(self.pos)]
  459. def _rotateImage(self,image, angle):
  460. if self.img_meta:
  461. return
  462. return _rotateImage(image, angle)
  463. def pause(self):
  464. if self.img_meta:
  465. return
  466. #print("pause",self.t_last)
  467. t = time.time()
  468. self.t_delta = 0
  469. self.t_last = t
  470. def next_frame(self):
  471. if self.img_meta:
  472. return
  473. #return 0
  474. if self._run and len(self.buffer) > 30:
  475. t = time.time()
  476. self.t_delta = t-self.t_last
  477. self.pos += self.t_delta*self.fps
  478. self.t_last = t
  479. else:
  480. self.pause()
  481. if self.restart_t > time.time()-0.5:
  482. # prevent flickerung by reset
  483. #self.pause()
  484. self.pos = 5
  485. pass
  486. # restart at the end
  487. if int(self.pos) >= len(self.buffer)-1:
  488. self.pos = 0
  489. self.img = None
  490. if self.buffer:
  491. self._img = self.buffer[int(self.pos)]
  492. self.img = self._img
  493. def check_cv2_check(self):
  494. if not cv2:
  495. cprint("cv2 not importet ! no VIDEO support !",color="red")
  496. return 0
  497. return 1
  498. def next(self):
  499. if self.img_meta:
  500. self.next_frame()
  501. return
  502. try:
  503. self.next_frame()
  504. if self.img is None:
  505. return
  506. self.img = _rescale_frame_by_procent(self.img, percent=self.scale)
  507. # rotate frame BUG: x,y offset ??? !!!
  508. self.img = self._rotateImage(self.img, self.angle)
  509. self.shape = self.img.shape[1::-1]
  510. if len(self.buffer) % 100 == 0:
  511. _id = str(self.__repr__)[-5:-1]
  512. print("next",_id)
  513. # add DIMER to videplayer
  514. cv2.normalize(self.img, self.img, 0, self.dim, cv2.NORM_MINMAX)
  515. img = self.img
  516. if img is None:
  517. return
  518. self.im = None
  519. self.im = pygame.image.frombuffer(img.tobytes(), self.shape, "RGB")
  520. #self.next_frame()
  521. except AttributeError as e:
  522. time.sleep(.05)
  523. #if self.init_count % 100 == 0:
  524. cprint("except 776",e,color="red")
  525. #self.init()
  526. except Exception as e:
  527. cprint("except 756",e,color="red")
  528. cprint(traceback.format_exc(),color="red")
  529. cprint(sys.exc_info()[2],color="red")
  530. cprint()
  531. def draw(self,wn=None):
  532. if self.img_meta:
  533. self.img_meta["pos"] = [self.x,self.y]
  534. self.img_meta["scale"] = self.scale #= 50 #%
  535. self.img_meta["dim"] = self.dim #255
  536. self.img_meta["ang"] = self.angle # 0
  537. img_draw_raw(self.img_meta)
  538. return
  539. if self.dim <= 1:
  540. return
  541. if self.scale < 255*.05:
  542. self.scale = 255*0.05
  543. # draw video background box
  544. __xw = int(self.shape_x*self.scale/255)
  545. __yw = int(self.shape_y*self.scale/255)
  546. #xx ,yy = (370,235)
  547. xx = self.shape_x #= xx
  548. yy = self.shape_y #= yy
  549. try:
  550. xx ,yy = self.im.get_size()[:2]
  551. self.shape_x = xx
  552. self.shape_y = yy
  553. except AttributeError as e:
  554. pass
  555. xx = int(xx*self.scale/255)
  556. yy = int(yy*self.scale/255)
  557. __xw = int(xx) #*self.scale/255)
  558. __yw = int(yy) #*self.scale/255)
  559. yellow = [105,50,0]
  560. yellow[0] = int(yellow[0]*self.dim/255)
  561. yellow[1] = int(yellow[1]*self.dim/255)
  562. yellow[2] = int(yellow[2]*self.dim/255)
  563. if self.fname.startswith("cam_"):
  564. pass
  565. elif 1: #corner left up
  566. p1 = [self.x+2,self.y+2]
  567. p2 = [self.x+__xw-4,self.y+__yw-4]
  568. p3 = [self.x+__xw-4,self.y+2]
  569. p4 = [self.x+2,self.y+__yw-4]
  570. p1 = [self.x,self.y]
  571. p2 = [self.x+__xw,self.y+__yw]
  572. p3 = [self.x+__xw,self.y]
  573. p4 = [self.x,self.y+__yw]
  574. hx=int(xx/2)
  575. hy=int(yy/2)
  576. p1 = [self.x-hx,self.y-hy]
  577. p2 = [self.x+__xw-hx,self.y+__yw-hy]
  578. p3 = [self.x+__xw-hx,self.y-hy]
  579. p4 = [self.x-hx,self.y+__yw-hy]
  580. pygame.draw.rect(wn,yellow ,[p1[0] ,p1[1] ,__xw ,__yw])
  581. pygame.draw.rect(wn,[25,20,20],[p1[0]+1,p1[1]+1,__xw-2,__yw-2])
  582. pygame.draw.line(wn,yellow ,p1 ,p2)
  583. pygame.draw.line(wn,yellow ,p3 ,p4)
  584. if 0: #corner right down
  585. pygame.draw.rect(wn,yellow,[self.x-__xw,self.y-__yw,__xw,__yw])
  586. pygame.draw.rect(wn,[25,20,20],[self.x+1-__xw,self.y+1-__yw,__xw-2,__yw-2])
  587. pygame.draw.line(wn,yellow,[self.x+2-__xw,self.y+2-__yw],[self.x+__xw-4-__xw,self.y+__yw-4-__yw])
  588. pygame.draw.line(wn,yellow,[self.x+__xw-4-__xw,self.y+2-__yw],[self.x+2-__xw,self.y+__yw-4-__yw])
  589. pz = 0
  590. if self.success and wn and self.im: # is not None:
  591. xx ,yy = self.im.get_size()[:2]
  592. if self.fname.startswith("cam_"):
  593. #wn.blit(self.im, (int(self.x-xx/2), int(self.y))) # corner center-top
  594. #wn.blit(self.im, (int(self.x-xx/2), int(self.y-yy))) # corner center-bottom
  595. wn.blit(self.im, (int(self.x-xx/2), int(self.y-yy))) # corner center-bottom
  596. else:
  597. wn.blit(self.im, (int(self.x-xx/2), int(self.y-yy/2)))
  598. def get_meta(self):
  599. out = {}
  600. mode="x"
  601. if self._run:
  602. mode = "run"
  603. else:
  604. mode = "pause"
  605. out["frames"] = len(self.buffer)
  606. out["mode"] = mode
  607. out["fname"] = self.fname
  608. out["scale"] =self.scale
  609. out["fps"] = int(self.fps)
  610. out["pos"] = self.pos
  611. out["dim"] = self.dim
  612. out["run"] = self._run
  613. out["cur"] = 0
  614. if self.fps > 0: # check if div zerro
  615. out["cur"] = (self.pos/self.fps)
  616. out["x"] = self.x
  617. out["y"] = self.y
  618. out["id"] = self._id
  619. out["nr"] = self._media_nr
  620. out["pz"] = 0
  621. return out
  622. def _overlay_text(self):
  623. meta = self.get_meta()
  624. keys = ["fps","pos","frames","cur","pz","x","y","scale","dim"]
  625. _line = create_format_line(meta,keys)
  626. keys= ["id","nr","mode","fname"]
  627. _line2 = create_format_line(meta,keys)
  628. return [_line,_line2]
  629. def create_format_line(meta,keys):
  630. _line = []
  631. for k in keys:
  632. v=meta.get(k,"-")
  633. _line.append(str(k))
  634. _line.append(":")
  635. if type(v) is float:
  636. _line.append("{:0.02f}".format(v))
  637. else:
  638. _line.append(str(v))
  639. _line.append(" ")
  640. _line = "".join(_line)
  641. return _line
  642. PixelMedia = PixelMedia
  643. VIDEO = []
  644. videoplayer=[]
  645. cv2 = None
  646. FileVideoStream = None
  647. _vid = 0
  648. if type(options.videoplayer) is str:
  649. try:
  650. import cv2
  651. except Exception as e:
  652. cprint("Except Import:",e,color="red")
  653. try:
  654. # faster video reading ... ???
  655. from imutils.video import FileVideoStream
  656. except Exception as e:
  657. cprint("Except Import:",e,color="red")
  658. max_videoplayer = 4
  659. dmx_start = options.videoplayer.split(",")
  660. for cdmx in dmx_start:
  661. if len(videoplayer) > max_videoplayer:
  662. print("-- videoplayer max count {} !! break".format(max_videoplayer))
  663. break
  664. print("-- videoplayer dmx:",cdmx)
  665. try:
  666. cdmx = int(cdmx)
  667. videoplayer.append( PixelMedia(cdmx,_id=_vid) )
  668. _vid += 1
  669. _tmp = {"DMX":cdmx,"DIM":0,"PAN":127,"TILT":127
  670. ,"CONTROL":0,"SEC":10,"VIDEO":"3"
  671. ,"RED":255,"GREEN":255,"BLUE":255
  672. ,"_time":time.time()
  673. ,"_RUN":0,"_SEC":">{}<".format(cdmx)
  674. }
  675. VIDEO.append(_tmp)
  676. except Exception as e:
  677. cprint("EXCEPTION COUNTER INIT ",cdmx,color="red")
  678. def loop_videoplayer():
  679. while 1:
  680. _videoplayer = videoplayer[:]
  681. #print(".")
  682. ok = 0
  683. for i in _videoplayer: #.append( PixelMedia(cdmx,_id=_vid) )
  684. try:
  685. r = i.read() # read next frame from file
  686. if r:
  687. ok = 1
  688. except Exception as e:
  689. cprint("EXCEPTION loop_videoplayer ",e,color="red")
  690. if ok == 0:
  691. time.sleep(0.1)
  692. else:
  693. time.sleep(0.005)
  694. if QUIT:
  695. break
  696. thread.start_new_thread(loop_videoplayer,())
  697. # ===== ======
  698. #self.fname = MEDIALIST[0]
  699. videoplayer2 = []
  700. def loop2_videoplayer():
  701. while 1:
  702. print()
  703. print()
  704. print()
  705. for i,v in enumerate(MEDIALIST):
  706. try:
  707. video1 = videoplayer2[i]
  708. video1.select_video(i)
  709. except:
  710. vi = PixelMedia(181,1)
  711. videoplayer2.append( vi )
  712. _videoplayer = videoplayer2[:]
  713. ok = 0
  714. j =0
  715. for i in _videoplayer: #.append( PixelMedia(cdmx,_id=_vid) )
  716. try:
  717. r = i.read() # read next frame from file
  718. if r:
  719. print(j,len(videoplayer2),i,len(i.buffer))
  720. ok = 1
  721. except Exception as e:
  722. cprint("EXCEPTION loop2_videoplayer ",e,color="red")
  723. time.sleep(0.002)
  724. j += 1
  725. p = 16
  726. block = [p,p]
  727. _x = 8
  728. _y = 8
  729. #HD = "0"
  730. if options.mode:
  731. try:
  732. HD = options.mode
  733. p,_x,_y = HD.split(",")
  734. _x = int(_x)
  735. _y = int(_y)
  736. p = int(p)
  737. block = [p,p]
  738. except Exception as e:
  739. cprint( "Exc",options.mode,e,color="red")
  740. # PARSE COMMANDLINE ARGUMENTS
  741. CFG_IN = {"name":"CFG_IN","x1":40,"y1":60,"x2":300,"y2":300 ,"w":300,"h":300}
  742. CFG_OUT = {"name":"CFG_OUT","x1":40,"y1":60,"x2":300,"y2":300 ,"w":300,"h":300,"on":0}
  743. #CFG_OUT2 = {"name":"CFG_OUT2","x1":p*8+142,"y1":60,"x2":300,"y2":300 ,"w":300,"h":300,"on":0}
  744. CFG_OUT2 = {"name":"CFG_OUT2","x1":p*8+(16*2)+8,"y1":60,"x2":300,"y2":300 ,"w":300,"h":300,"on":0}
  745. CFG_BLOCK = {"name":"CFG_BLOCK","size":16,"h-split":2,"v-split":2,"h-count":8,"v-count":8}
  746. if _x < 1:
  747. _x = 1
  748. if _y < 1:
  749. _y = 1
  750. CFG_BLOCK["h-count"] = _x
  751. CFG_BLOCK["v-count"] = _y # int(8*8/_x) #/_x+0.5) #_y
  752. CFG_BLOCK["size"] = p
  753. print( [options.xsplit])
  754. print( [options.ysplit])
  755. try:
  756. if options.xsplit:
  757. CFG_BLOCK["h-split"] = int(options.xsplit)
  758. if options.ysplit:
  759. CFG_BLOCK["v-split"] = int(options.ysplit)
  760. except Exception as e:
  761. cprint( "Exc",options.mode,e,color="red")
  762. print("HD",CFG_BLOCK["h-split"],CFG_BLOCK["v-split"])
  763. print("xy",_x,_y)
  764. print("++++++++++++++++++", p,_x,_y)
  765. _x2 = _x
  766. try:
  767. if options.XX:
  768. pass#_x2 = int(options.XX)
  769. except Exception as e:
  770. cprint( "Exc",options.mode,e,color="red")
  771. print("_x2 , -X",_x2)
  772. # ===== GUI =========
  773. import pygame
  774. import pygame.gfxdraw
  775. import pygame.font
  776. clock = pygame.time.Clock()
  777. os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (200,164)
  778. if options.win_pos:
  779. if "," in options.win_pos:
  780. win_pos = options.win_pos.split(",")
  781. try:
  782. WIN_POS = '%i,%i' % (int(win_pos[0]),int(win_pos[1]) )
  783. os.environ['SDL_VIDEO_WINDOW_POS'] = WIN_POS
  784. except Excetpion as e:
  785. cprint("win_pos",win_pos,e,color="red")
  786. os.environ['SDL_VIDEO_CENTERED'] = '0'
  787. pg = pygame
  788. pygame.init()
  789. pygame.mixer.quit()
  790. f = pygame.font.get_fonts()
  791. for i in f:
  792. if "mono" in i.lower():
  793. print(i)
  794. font = pygame.font.SysFont("freemonobold",22)
  795. font10 = pygame.font.SysFont("freemonobold",10)
  796. font12 = pygame.font.SysFont("freemonobold",12)
  797. font12l = pygame.font.SysFont("freemono",12)
  798. font15 = pygame.font.SysFont("freemonobold",15)
  799. font40 = pygame.font.SysFont("freemonobold",40)
  800. font80 = pygame.font.SysFont("freemonobold",70)
  801. #font = pygame.font.SysFont(None,30)
  802. fr = font.render("hallo" ,1, (200,0,255))
  803. def clean_path(path):
  804. _path = path[:]
  805. _path = _path.replace("/","-")
  806. _path = _path.replace(".","-")
  807. _path = _path.replace("\"","-")
  808. _path = _path.replace("'","-")
  809. return _path
  810. PIXEL_MAPPING = 0
  811. vpu_wall_conf_file = HOME+"/LibreLight/vpu_wall_conf.csv"
  812. grid_file = "/tmp/vpu_grid_hd.csv"
  813. text_file = HOME+"/LibreLight/vpu_text_hd.csv"
  814. media_list = "/tmp/vpu_medialist_hd.csv"
  815. media_list = HOME+"/LibreLight/video/" #.format(path)
  816. pm_wy = 0
  817. if options.pixel_mapping:
  818. PIXEL_MAPPING = 1
  819. CFG_OUT["on"] = 1
  820. path = options.pixel_mapping
  821. path = clean_path(path)
  822. grid_file = HOME+"/LibreLight/vpu_grid_hd{}.csv".format(path)
  823. if options.dual_vpu:
  824. grid_file = HOME+"/LibreLight/vpu_grid_dual{}.csv".format(path)
  825. text_file = HOME+"/LibreLight/vpu_text_hd{}.csv".format(path)
  826. media_list = HOME+"/LibreLight/vpu_medialist_hd{}.csv".format(path)
  827. media_list = HOME+"/LibreLight/video/" #.format(path)
  828. #_x = 8
  829. #_y = 8
  830. if options.dual_vpu:
  831. CFG_OUT2["on"] = 1
  832. print(" ",[options.pixel_mapping],"grid_file",grid_file)
  833. #grid_file = HOME+"/LibreLight/vpu_grid_hd.csv"
  834. MAIN_SIZE=(600,500)
  835. try:
  836. if _x < 8 and PIXEL_MAPPING >= 1:
  837. wx = 30+30+block[0] * 8
  838. CFG_IN["w"] = CFG_BLOCK["size"] * 8
  839. else:
  840. wx = 30+30+block[0] * _x
  841. wy = 40+40+block[1] * _y
  842. if type(options.videoplayer) is str:
  843. wy += 150 # video medialist
  844. MAIN_SIZE=(wx,wy)
  845. if PIXEL_MAPPING >= 1:
  846. pm_wy = 11*p #+ p*3
  847. CFG_IN["y1"] += 11*p
  848. if options.dual_vpu:
  849. if wx < p*16+155:
  850. wx = p*16+155
  851. MAIN_SIZE=(wx,wy+pm_wy)
  852. except Exception as e:
  853. cprint("Exception:",e,color="red")
  854. CFG_IN["w"] = CFG_BLOCK["size"] * CFG_BLOCK["h-count"]
  855. CFG_IN["h"] = CFG_BLOCK["size"] * CFG_BLOCK["v-count"]
  856. CFG_OUT["w"] = CFG_BLOCK["size"] * 8
  857. CFG_OUT["h"] = CFG_BLOCK["size"] * 8
  858. CFG_OUT2["w"] = CFG_BLOCK["size"] * 8
  859. CFG_OUT2["h"] = CFG_BLOCK["size"] * 8
  860. def CFG_CALC_P(CFG):
  861. CFG["x2"] = CFG["x1"]+CFG["w"]
  862. CFG["y2"] = CFG["y1"]+CFG["h"]
  863. CFG["p1"] = [CFG["x1"] ,CFG["y1"]]
  864. CFG["p2"] = [CFG["x2"] ,CFG["y2"]]
  865. print("CFG",CFG)
  866. # ?
  867. #CFG_IN["w"] = int(CFG_BLOCK["v-count"]-2) * CFG_BLOCK["size"]
  868. CFG_CALC_P(CFG_IN)
  869. CFG_CALC_P(CFG_OUT)
  870. CFG_CALC_P(CFG_OUT2)
  871. print("CFG_BLOCK",CFG_BLOCK)
  872. print()
  873. print()
  874. print()
  875. print()
  876. print()
  877. window = pygame.display.set_mode(MAIN_SIZE,pg.RESIZABLE)#,32)#,pygame.FULLSCREEN) #x left->right ,y top-> bottom
  878. pg.display.set_caption('LibreLight VPU-{}'.format(options.title))
  879. class Fix():
  880. def __init__(self,_id,pos,block=[16,16],univ=0,dmx=0,ch=4):
  881. #print("Fix",_id)
  882. self._id = _id
  883. self.dmx = (_id-1) * ch +1 #dmx
  884. self.univ = univ
  885. self.ch = ch
  886. self.pos = pos
  887. self.rgb = [0,0,0]
  888. self.block = block #[10,10]
  889. self.x = pos[0]
  890. self.y = pos[1]
  891. self._x = 0
  892. self._y = 0
  893. self.strobo = time.time()
  894. self.bmp = 250
  895. self.sub_fix = []
  896. sub_block =[block[0]/CFG_BLOCK["h-split"],block[1]/CFG_BLOCK["v-split"]]
  897. if _id <= 0: #exit
  898. return
  899. spalte = (_id-1)%_y +1
  900. zeile = int((_id-1)/_x2) #+1
  901. add_row = _x*CFG_BLOCK["h-split"]*CFG_BLOCK["v-split"]
  902. #zeile 1
  903. sid = (_id-1)*2 + zeile*CFG_BLOCK["h-split"]*_x2
  904. sid = sid+1
  905. sub_pos= [pos[0]*block[0],pos[1]*block[1]]
  906. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  907. self.sub_fix.append(sub_fix)
  908. sid = sid+1
  909. sub_pos= [pos[0]*block[0]+block[0]/2,pos[1]*block[1]]
  910. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  911. self.sub_fix.append(sub_fix)
  912. #zeile 2
  913. sid = (_id-1)*2+1 + _x2*CFG_BLOCK["h-split"] + zeile*CFG_BLOCK["h-split"]*_x2 # int(add_row)
  914. sub_pos= [pos[0]*block[0],pos[1]*block[1]+block[1]/2]
  915. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  916. self.sub_fix.append(sub_fix)
  917. #sid = sid+1
  918. sid = sid+1
  919. sub_pos= [pos[0]*block[0]+block[0]/2,pos[1]*block[1]+block[1]/2]
  920. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  921. self.sub_fix.append(sub_fix)
  922. def __repr__(self):
  923. return "<Fix: {} {} {} {}>".format(id(self),self.x,self.y,self.dmx)
  924. def calc(self,data):
  925. _rgb = [0,255,0]
  926. return _rgb
  927. def sub_calc(self,data):
  928. _rgb = [0,255,0]
  929. for sub_fix in self.sub_fix:
  930. sub_fix.block = self.block[:]
  931. _rgb = sub_fix.dmx_calc(data)
  932. return _rgb
  933. def _calc(self,a,b):
  934. A = (self.pos[0])*self.block[0]
  935. B = (self.pos[1])*self.block[1]
  936. C = self.block[0]-a
  937. D = self.block[1]-b
  938. return A,B,C,D
  939. def POS(self,x=None,y=None,a=0,b=0):
  940. if type(x) is int:
  941. self._x=x
  942. if type(y) is int:
  943. self._y=y
  944. A,B,C,D = self._calc(a,b)
  945. return [self._x+A,self._y+B,C,D]
  946. def XXXsubPOS(self,x=0,y=0,a=0,b=0):
  947. __out = []
  948. for sub_fix in self.sub_fix:
  949. __out.append( sub_fix.POS(x,y,a,b) )
  950. return __out
  951. class SubFix():
  952. def __init__(self,_id,pos,block=[16,16],univ=0,dmx=0,ch=4):
  953. #print("Fix",_id)
  954. self._id = _id
  955. self.dmx = (_id-1) * ch +1 #dmx
  956. self.univ = univ
  957. self.ch = ch
  958. self.pos = pos
  959. self.rgb = [0,0,40]
  960. self.block = block #[10,10]
  961. self.x = pos[0]
  962. self.y = pos[1]
  963. self._x = 0
  964. self._y = 0
  965. self.strobo = time.time()
  966. self.bmp = 250
  967. def dmx_calc(self,data):
  968. #return [130,30,20]
  969. dmx_sub = [30]*10
  970. dmx = self.dmx -1
  971. _dmx_sub = []
  972. if self.dmx >= 0:
  973. dmx = rDMX(self.univ,self.dmx)-1
  974. if dmx+self.ch < len(data):
  975. _dmx_sub = data[dmx:dmx+self.ch]
  976. if _dmx_sub:
  977. dmx_sub = _dmx_sub
  978. dim = dmx_sub[0]/255
  979. r = dmx_sub[1]*dim
  980. g = dmx_sub[2]*dim
  981. b = dmx_sub[3]*dim
  982. r = int(r)
  983. g = int(g)
  984. b = int(b)
  985. self.rgb = [r,g,b]
  986. return self.rgb
  987. def _calc(self,a,b):
  988. A = (self.pos[0]) #+self.block[0]
  989. B = (self.pos[1]) #+self.block[1]
  990. C = self.block[0]-a
  991. D = self.block[1]-b
  992. return A,B,C,D
  993. def POS(self,x=None,y=None,a=0,b=0):
  994. if type(x) is int:
  995. self._x=x
  996. if type(y) is int:
  997. self._y=y
  998. A,B,C,D = self._calc(a,b)
  999. #if NR:
  1000. # C-=1
  1001. # D-=1
  1002. return [int(self._x+A),int(self._y+B),int(C),int(D)]
  1003. class POINTER():
  1004. def __init__(self):
  1005. self.pos = [0,0,0,0]
  1006. self.on = 0
  1007. self.rgb = [0,100,10]
  1008. self._x = 0
  1009. self._y = 0
  1010. self.x = 0
  1011. self.y = 0
  1012. self.fix = Fix(0 ,[999,999],[16,16],0,0,0)
  1013. def row_move(self,x,y):
  1014. self._x = x
  1015. self._y = y
  1016. def move(self,pos):
  1017. self.pos = pos
  1018. self.on = 1
  1019. def cross(self,x,y):
  1020. self.x = x
  1021. self.y = y
  1022. def draw(self,x,y):
  1023. pos = self.pos[:]
  1024. #print("draw",x,y,pos)
  1025. pos[0] += x
  1026. pos[1] += y
  1027. fix_x= self.fix.x
  1028. fix_y= self.fix.y +y
  1029. if 0:# self.on:
  1030. pygame.draw.rect(window,self.rgb,pos)
  1031. # mouse grid posision
  1032. fr = font15.render("{}/{}".format(fix_x+1,fix_y) ,1, (200,200,200))
  1033. _nr = fix_y * _x + fix_x +1
  1034. fr = font15.render("{:02}".format(_nr ) ,1, (200,200,200))
  1035. window.blit(fr,(pos[0]+2,pos[1]+2 ))
  1036. window.blit(fr,(130,1))
  1037. if 1:# lupe / looking-glass / lens
  1038. __t = 60
  1039. rgb=[255,0,0]
  1040. pos=[400,400,160,160]
  1041. draw_box_frame(window,rgb,pos,offset=1)
  1042. r_sub = grab(x=self._x-__t/2,y=self._y-__t/2,w=__t,h=__t)
  1043. if r_sub:
  1044. r_sub = pygame.transform.scale(r_sub,(160,160))
  1045. if r_sub: # target
  1046. window.blit(r_sub, (400,400))
  1047. #pos=[400+40,400+40,80,80]
  1048. pos=[400+80,400+80,80,80]
  1049. draw_box_frame(window,rgb,pos,offset=3)
  1050. draw_box_frame(window,rgb,pos,offset=2)
  1051. draw_box_frame(window,rgb,pos,offset=0)
  1052. draw_box_frame(window,rgb,pos,offset=1)
  1053. # fix pos
  1054. #txt=str(pos) #"[0, 0, 0, 0]"
  1055. #fr = font15.render(txt ,1, (200,200,200))
  1056. #window.blit(fr,(10,1))
  1057. # pointer
  1058. fr = font15.render("X:{:03}".format(self._x) ,1, (200,200,200))
  1059. window.blit(fr,(10,30))
  1060. fr = font15.render("Y:{:03}".format(self._y) ,1, (200,200,200))
  1061. window.blit(fr,(10,40))
  1062. if 1: #self._x > 30 and self._y > 230:
  1063. # pointer
  1064. _x = self._x-INPUT_START_POS_X
  1065. _y = self._y-INPUT_START_POS_Y
  1066. rgb = [200,200,200]
  1067. if _x < 0:
  1068. rgb = [200,0,0]
  1069. fr = font80.render("X:{:03}".format(_x) ,1, rgb)
  1070. window.blit(fr,(200,400))
  1071. rgb = [200,200,200]
  1072. if _y < 0:
  1073. rgb = [200,0,0]
  1074. fr = font80.render("Y:{:03}".format(_y) ,1, rgb)
  1075. window.blit(fr,(200,450))
  1076. p=110
  1077. # crosshair
  1078. self.rgb = [0,0,200]
  1079. pygame.draw.line(window,self.rgb, (self.x-p,self.y) , (self.x-2,self.y),2 )
  1080. pygame.draw.line(window,self.rgb, (self.x,self.y-p) , (self.x,self.y-2),2 )
  1081. self.rgb = [0,200,0]
  1082. pygame.draw.line(window,self.rgb, (self.x+2,self.y) , (self.x+p,self.y),2 )
  1083. pygame.draw.line(window,self.rgb, (self.x,self.y+2) , (self.x,self.y+p),2 )
  1084. self.rgb = [200,0,0]
  1085. pointer = POINTER()
  1086. NR = 0
  1087. running = True
  1088. def event():
  1089. global NR,running
  1090. for event in pygame.event.get():
  1091. #print(event.dict)
  1092. _button = None
  1093. if "button" in event.dict:
  1094. _button = event.dict["button"]
  1095. _state = None
  1096. if "state" in event.dict:
  1097. _state = event.state
  1098. _key = None
  1099. if "key" in event.dict:
  1100. _key = event.key
  1101. _pos = None
  1102. if "pos" in event.dict:
  1103. _pos = event.pos
  1104. _type = None
  1105. if "type" in event.dict:
  1106. _type = event.type
  1107. _type = event.type
  1108. _mod = None
  1109. if "mod" in event.dict:
  1110. _mod = event.mod
  1111. if 0:
  1112. print( " ")
  1113. print( "{:.02f}".format( time.time() - START ))
  1114. print("button -",_button,end="\t| ")
  1115. #print("state -",_state)
  1116. print("pos -",_pos)
  1117. print("type -",_type, end="\t| ")
  1118. print("key -",_key)
  1119. print("mod -",_mod)
  1120. try:
  1121. if _type in [5,1025]:
  1122. if _button == 1:
  1123. NR += 1
  1124. if NR > 1:
  1125. NR = 0
  1126. if _button == 3:
  1127. NR -= 1
  1128. if NR < 0:
  1129. NR = 1
  1130. if _pos:
  1131. posA = _pos
  1132. fix = find_pix(_pos[0]-40,_pos[1]-60+pm_wy)
  1133. if fix:
  1134. #pos = fix.POS(40,60+pm_wy)
  1135. pos = fix.POS() #(CFG_IN["x1"],CFG_IN["y1"])#0,0) #+pm_wy)
  1136. rgb = [0,0,0]
  1137. pointer.move(pos)
  1138. pointer.fix = fix
  1139. else:
  1140. pointer.on = 0
  1141. pointer.row_move(_pos[0],_pos[1])
  1142. pointer.cross(_pos[0],_pos[1])
  1143. if event.type == pygame.VIDEORESIZE:
  1144. window = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)
  1145. except Exception as e:
  1146. cprint(e,color="red")
  1147. if event.type==pygame.QUIT:
  1148. global QUIT
  1149. QUIT = 1
  1150. cprint("QUIT",color="red")
  1151. running=False
  1152. #time.sleep(1)
  1153. #sys.exit()
  1154. fps = 0
  1155. fps2 = 0
  1156. frame = 0
  1157. frame2 = 0
  1158. frame_t = time.time()
  1159. frame2_t = time.time()
  1160. IP = "yyy"
  1161. vplay1=0
  1162. vplay2=0
  1163. if options.videoplayer:
  1164. try:
  1165. t=options.videoplayer.split(",")
  1166. vplay1=t[0]
  1167. vplay2=t[1]
  1168. except:pass
  1169. def output_mask(): # oben
  1170. RGB = [0,0,50]
  1171. #RGB = [120,0,0] # test
  1172. start = 0
  1173. # oben
  1174. pygame.draw.rect(window,RGB,[0,start,MAIN_SIZE[0]+200,57]) # background
  1175. # unten
  1176. pygame.draw.rect(window,RGB,[0,start+192,MAIN_SIZE[0]+200,35]) # background
  1177. # links
  1178. pygame.draw.rect(window,RGB,[0,start+0,35,235]) # background
  1179. def input_mask(): # unten
  1180. RGB = [0,0,50]
  1181. #RGB = [120,0,110] # test
  1182. start = 227
  1183. # oben
  1184. pygame.draw.rect(window,RGB,[0,start,MAIN_SIZE[0]+200,7]) # background
  1185. # links
  1186. pygame.draw.rect(window,RGB,[0,start,35,200]) # background
  1187. # unten
  1188. pygame.draw.rect(window,RGB,[0,start+173,MAIN_SIZE[0]+200,198]) # background
  1189. def draw_main_overlay():
  1190. global fps,fps2
  1191. fr = font15.render("DMX-FPS: {}".format(fps) ,1, (200,0,255))
  1192. window.blit(fr,(10,2))
  1193. fr = font15.render("GUI-FPS : {}".format(fps2) ,1, (200,0,255))
  1194. window.blit(fr,(10,12))
  1195. #fr = font15.render("ip:{}".format(IP) ,1, (200,0,255))
  1196. #window.blit(fr,(100,2))
  1197. fr = font15.render("start-uni: {:}.xx dRGB".format(START_UNIV) ,1, (200,0,255))
  1198. window.blit(fr,(90,2))
  1199. fr = font15.render("a1_idim: {}.{:}".format(START_UNIV_A,options.grid_a1_idim) ,1, (200,0,255))
  1200. window.blit(fr,(90,12))
  1201. fr = font15.render("a2_idim: {}.{:}".format(START_UNIV_A,options.grid_a2_idim) ,1, (200,0,255))
  1202. window.blit(fr,(90,22))
  1203. fr = font15.render("gobo_ch1: {}.{:}".format(START_UNIV_A,options.gobo_ch) ,1, (200,0,255))
  1204. window.blit(fr,(180,12))
  1205. fr = font15.render("gobo_ch2: {}.{:}".format(START_UNIV_A,options.gobo_ch2) ,1, (200,0,255))
  1206. window.blit(fr,(180,22))
  1207. fr = font15.render("v-play1: {}.{:}".format(START_UNIV_A,vplay1) ,1, (200,0,255))
  1208. window.blit(fr,(270,12))
  1209. fr = font15.render("v-play2: {}.{:}".format(START_UNIV_A,vplay2) ,1, (200,0,255))
  1210. window.blit(fr,(270,22))
  1211. try:
  1212. global pointer
  1213. # pointer
  1214. fr = font15.render("X:{:03}".format(pointer._x) ,1, (200,200,200))
  1215. window.blit(fr,(10,30))
  1216. fr = font15.render("Y:{:03}".format(pointer._y) ,1, (200,200,200))
  1217. window.blit(fr,(10,40))
  1218. except Exception as e:
  1219. print("Overlay err:",e)
  1220. def draw_box_frame(window,rgb,pos,offset=0):
  1221. p1 = [pos[0],pos[1]]
  1222. p2 = [pos[0]+pos[2],pos[1]+pos[3]]
  1223. o = offset
  1224. _p1 = [p1[0]-o,p1[1]-o]
  1225. _p2 = [p1[0]-o,p2[1]+o]
  1226. pygame.draw.line(window,rgb,_p1,_p2) # left
  1227. _p1 = [p2[0]+o,p2[1]+o]
  1228. pygame.draw.line(window,rgb,_p1,_p2) # bottom
  1229. _p2 = [p2[0]+o,p1[1]-o]
  1230. pygame.draw.line(window,rgb,_p1,_p2) # right
  1231. _p1 = [p1[0]-o,p1[1]-o]
  1232. pygame.draw.line(window,rgb,_p1,_p2) # top
  1233. def draw_frame(window,rgb,p1,p2,offset=0):
  1234. o = offset
  1235. _p1 = [p1[0]-o,p1[1]-o]
  1236. _p2 = [p1[0]-o,p2[1]+o]
  1237. #pygame.draw.line(window,rgb,_p1,_p2) # left
  1238. _p1 = [p2[0]+o,p2[1]+o]
  1239. pygame.draw.line(window,rgb,_p1,_p2) # bottom
  1240. _p2 = [p2[0]+o,p1[1]-o]
  1241. #pygame.draw.line(window,rgb,_p1,_p2) # right
  1242. _p1 = [p1[0]-o,p1[1]-o]
  1243. pygame.draw.line(window,rgb,_p1,_p2) # top
  1244. def calc_fps():
  1245. global fps,frame,frame_t
  1246. t = time.time()
  1247. if frame_t+1 < t:
  1248. fps = frame #frame_t- t #frame
  1249. frame = 1
  1250. frame_t = time.time()
  1251. def calc_fps2():
  1252. global fps2,frame2,frame2_t
  1253. t = time.time()
  1254. if frame2_t+0.1 < t:
  1255. fps2 = frame2*10 #frame_t- t #frame
  1256. frame2 = 1
  1257. frame2_t = t #time.time()
  1258. TEXT_BLOCK = []
  1259. def _create_text_block():
  1260. print("======== CREATE NEW TEXT FILE !!",text_file)
  1261. f = open(text_file,"w")
  1262. for i in range(10):
  1263. f.write("TEXT {}\n".format(i+1))
  1264. f.close()
  1265. def open_text_block():
  1266. print("======== OPEN TEXT FILE !!",text_file)
  1267. _lines = []
  1268. try:
  1269. f = open(text_file,"r")
  1270. _lines = f.readlines()
  1271. f.close()
  1272. except FileNotFoundError as e:
  1273. print("TEXT",e)
  1274. _create_text_block()
  1275. if len(_lines) <= 0:
  1276. _create_text_block()
  1277. lines = []
  1278. for l in _lines:
  1279. #print(">> ",l.strip())
  1280. lines.append(l.strip())
  1281. if len(lines) <= 10:
  1282. for i in range(10-len(lines)):
  1283. lines.append("LINE ERROR")
  1284. return lines
  1285. TEXT_BLOCK = open_text_block()
  1286. TEXT_BLOCK_TIME = time.time()
  1287. # video medialist
  1288. MEDIALIST = []
  1289. def open_medialist():
  1290. print("======== OPEN MEDIALIST DIR !!",media_list)
  1291. if not os.path.isdir(media_list):
  1292. os.system("mkdir -p {}".format(media_list))
  1293. _lines = os.listdir(media_list)
  1294. _lines.sort()
  1295. lines = ['']*25
  1296. i=0
  1297. for l in _lines:
  1298. l = l.strip()
  1299. if "_" in l:
  1300. ll = l.split("_",1)
  1301. print(">> ",ll)
  1302. try:
  1303. lll = int(ll[0])
  1304. lines[lll] = l
  1305. except:pass
  1306. if len(lines) <= 10:
  1307. for i in range(10-len(lines)):
  1308. lines.append("")#"LINE ERROR")
  1309. return lines
  1310. MEDIALIST_TIME = time.time()
  1311. MEDIALIST = open_medialist()
  1312. # ===== GUI =========
  1313. def draw_circle(surface,color, pos, radius):
  1314. x,y=pos
  1315. pygame.gfxdraw.aacircle(surface, int(x), int(y), radius-1, color)
  1316. pygame.gfxdraw.filled_circle(surface, int(x), int(y), radius-1, color)
  1317. def rDMX(univ,dmx):
  1318. return univ*512+dmx
  1319. def generate_grid(mapping=0):
  1320. _log = []
  1321. #if PIXEL_MAPPING:
  1322. # log = open(grid_file,"w")
  1323. head = "i,univ,dmx,x,y,ch\n"
  1324. head = "i,univ,dmx,ch\n"
  1325. head = "univ,dmx,x,y,ch\n"
  1326. head = "nr,id,info\n"
  1327. print("csv:",head)
  1328. _log.append(head)
  1329. dmx = 1-1
  1330. ch = 4
  1331. y=0
  1332. x=0
  1333. for i in range((_y)*(_x)):
  1334. #if x > _x and i%_x == 0:
  1335. if x > 8 and i%8 == 0:
  1336. #print("--> -->")
  1337. x=0
  1338. y+=1
  1339. _univ = int(dmx/512)
  1340. _dmx = dmx - (_univ)*512
  1341. pos=[x,y]
  1342. line="{},{},x\n".format(i+1,i+1)
  1343. _log.append(line)
  1344. dmx += ch
  1345. x+=1
  1346. if mapping and PIXEL_MAPPING:
  1347. print("CREATE NEW PIXELMAP FILE !!",grid_file)
  1348. log = open(grid_file,"w")
  1349. log.writelines(_log)
  1350. log.close()
  1351. return _log[:] #GRID
  1352. def init_grid(mapping=0,_x=4,_y=4,_xp=0,_yp=0,start=0,name="init_gird"):
  1353. if mapping and PIXEL_MAPPING:
  1354. try:
  1355. log = open(grid_file,"r")
  1356. except:
  1357. generate_grid(mapping=mapping)
  1358. log = open(grid_file,"r")
  1359. lines = log.readlines()
  1360. else:
  1361. lines = generate_grid()
  1362. GRID = []
  1363. y=0
  1364. x=0
  1365. print("CSV header",[lines[0]],[PIXEL_MAPPING,"len:",len(lines),"start:",start,name])
  1366. for i,line in enumerate(lines[1+start:]): #exclude first line
  1367. #print("rcsv",[line])
  1368. line = line.strip()
  1369. line = line.split(",") # csv
  1370. if i >= _x and i%_x == 0:
  1371. x=0
  1372. y+=1
  1373. if y >= _y:
  1374. break
  1375. _id = int(line[1])
  1376. pos = [x+_xp,y+_yp]
  1377. f = Fix(_id,pos,block) #pos,univ,dmx,ch)
  1378. GRID.append(f)
  1379. x+=1
  1380. return GRID
  1381. def find_pix(x,y):
  1382. global GRID
  1383. for fix in GRID:
  1384. X = 0
  1385. Y = 0
  1386. pos = fix.POS()
  1387. if x > pos[0] and x < pos[0]+pos[2]:
  1388. X = 1
  1389. if y > pos[1] and y < pos[1]+pos[3]:
  1390. Y = 1
  1391. if X and Y:
  1392. return fix
  1393. GRID = []
  1394. GRID_A = []
  1395. NR = 0
  1396. START_UNIV_A=1
  1397. START_UNIV=2
  1398. if options.start_univ:
  1399. try:
  1400. START_UNIV=int(options.start_univ)
  1401. except Exception as e:
  1402. print("Exception START UNIV",e)
  1403. gobo_ch=1
  1404. if options.gobo_ch:
  1405. try:
  1406. gobo_ch=int(options.gobo_ch)
  1407. except Exception as e:
  1408. print("Exception gobo_ch",e)
  1409. if gobo_ch <= 0:
  1410. gobo_ch = 1
  1411. def draw_box(pos1,pos2,color=[128,128,128],text=1):
  1412. color = [200,0,0,127]
  1413. if text:
  1414. fr = font15.render("A" ,1, (200,200,200))
  1415. window.blit(fr,pos1)
  1416. fr = font15.render("B" ,1, (200,200,200))
  1417. window.blit(fr,[pos2[0]-10,pos2[1]-10])
  1418. # h unten
  1419. _pos1 = [pos1[0],pos2[1]]
  1420. _pos2 = [pos2[0],pos2[1]]
  1421. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1422. color = [255,255,0,127]
  1423. # h rechts
  1424. _pos1 = [pos2[0],pos1[1]]
  1425. _pos2 = [pos2[0],pos2[1]]
  1426. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1427. color = [0,200,0,127]
  1428. # h links
  1429. _pos1 = [pos1[0],pos1[1]]
  1430. _pos2 = [pos1[0],pos2[1]]
  1431. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1432. color = [0,0,200,127]
  1433. # h oben
  1434. _pos1 = [pos1[0],pos1[1]]
  1435. _pos2 = [pos2[0],pos1[1]]
  1436. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1437. def grab(x=55,y=55,w=60,h=60):
  1438. crop = None
  1439. rect = pygame.Rect(x, y, w, h)
  1440. try:
  1441. sub = window.subsurface(rect)
  1442. crop = pygame.Surface((w,h))
  1443. crop.blit(sub, (0,0))
  1444. except ValueError as e:
  1445. pass
  1446. return crop
  1447. vpu_wall_txt="""---,quelle,,---,ziel,---,---
  1448. out,x,y,ang,row,col,info
  1449. Wall1,0,0,0,1,1,
  1450. Wall1,0,0,0,2,2,
  1451. Wall1,0,0,0,3,3,
  1452. Wall1,0,0,0,4,4,
  1453. Wall1,0,0,0,5,5,
  1454. Wall1,0,0,0,6,6,
  1455. Wall1,0,0,0,7,7,
  1456. ,,,,,,
  1457. Wall2,0,0,0,4,1,----
  1458. Wall2,0,0,0,5,2,
  1459. Wall2,4,4,0,6,3,
  1460. Wall2,0,0,0,7,4,
  1461. Wall2,0,0,0,8,5,
  1462. Wall2,0,0,0,9,6,
  1463. ,,,,,,
  1464. ,,,,,,
  1465. Wall3,64,48,0,12,1,----
  1466. Wall3,64,64,0,12,2,
  1467. Wall3,64,80,0,12,3,
  1468. Wall3,64,96,0,12,4,
  1469. Wall3,64,112,0,12,5,
  1470. Wall3,64,128,0,12,6,"""
  1471. def create_vpu_wall_conf():
  1472. f=open(vpu_wall_conf_file,"w")
  1473. f.write(vpu_wall_txt)
  1474. f.close()
  1475. print("CREATE VPU CONFIG !")
  1476. wall_data = []
  1477. _last_wall_config_time = time.time()
  1478. def read_wall_pix(init=0):
  1479. global _last_wall_config_time
  1480. global wall_data, NR
  1481. grid = []
  1482. #if not NR and init==0:
  1483. # return
  1484. ok = 0
  1485. if init:
  1486. ok = 1
  1487. if NR:
  1488. if time.time()-_last_wall_config_time > 3:
  1489. ok = 1
  1490. else:
  1491. if time.time()-_last_wall_config_time > 10:
  1492. ok = 1
  1493. if not ok:
  1494. return
  1495. _last_wall_config_time = time.time()
  1496. if not os.path.isfile(vpu_wall_conf_file):
  1497. create_vpu_wall_conf()
  1498. f=open(vpu_wall_conf_file,"r")
  1499. lines = f.readlines()
  1500. for i,line in enumerate(lines):
  1501. line = line.strip()
  1502. if not line:
  1503. continue
  1504. line = line.split(",")
  1505. if not line[0]:
  1506. continue
  1507. #print(i,line)
  1508. if len(line) >= 3:
  1509. _l = []
  1510. for i in line:
  1511. try:
  1512. _l.append(int(i))
  1513. except Exception as e:
  1514. _l.append(i)
  1515. grid.append(_l)
  1516. for i,v in enumerate(grid):
  1517. print(i,v)
  1518. wall_data = grid
  1519. read_wall_pix(init=1)
  1520. def reshape2(GRID,GRID_OUT,_x,_y,name="GRID_Z"):
  1521. _w = 16
  1522. _h = 16
  1523. from_x_min = INPUT_START_POS_X
  1524. from_y_min = INPUT_START_POS_Y
  1525. target_x = 40 # min
  1526. target_y = 60 # min
  1527. ang=0
  1528. #read_block_pix()
  1529. read_wall_pix()
  1530. _wall_data = wall_data[:]
  1531. for i,_wall in enumerate(_wall_data):
  1532. #print(_wall)
  1533. if len(_wall) <=6:
  1534. continue
  1535. try:
  1536. if type(_wall[1]) is str:
  1537. continue
  1538. if type(_wall[2]) is str:
  1539. continue
  1540. if type(_wall[3]) is str:
  1541. continue
  1542. if type(_wall[4]) is str:
  1543. continue
  1544. if type(_wall[5]) is str:
  1545. continue
  1546. from_x=from_x_min+_wall[1]#*_w
  1547. from_y=from_y_min+_wall[2]#*_h
  1548. _a= _wall[3]
  1549. ang=0
  1550. if _a < 0:
  1551. ang=0
  1552. elif _a > 0 and _a <= 90:
  1553. ang = 90
  1554. elif _a > 90 and _a <= 180:
  1555. ang = 180
  1556. elif _a > 180 and _a <= 270:
  1557. ang = 270
  1558. target_x=40+(_wall[4]-1)*_w
  1559. target_y=60+(_wall[5]-1)*_h
  1560. sub = grab(from_x,from_y,_w,_h)
  1561. #draw_box_frame(window,[0,40,0],[from_x+x,from_y+y,_w,_h])
  1562. if sub:
  1563. sub = pygame.transform.rotate(sub,ang)
  1564. window.blit(sub,(target_x,target_y))
  1565. except Exception as e:
  1566. print(" ",_wall)
  1567. print("WALL err:",e)
  1568. def reshape(GRID,GRID_OUT,_x,_y,name="GRID_Z"):
  1569. """reshape LED-WALL Block/Pixel mapping"""
  1570. if PIXEL_MAPPING <= 0:
  1571. return None
  1572. x = _x
  1573. y = _y
  1574. i = 0
  1575. counter = 0
  1576. z=0
  1577. x_min = 99999
  1578. x_max = 0
  1579. y_min = 99999
  1580. y_max = 0
  1581. pos= [0,0]
  1582. pos[0] = _x+p*3 #+10
  1583. pos[1] = p*8+65 #-10 #_y+p*8+80
  1584. tmp_font = pygame.font.SysFont("freemonobold",int(p*0.8))
  1585. #fr = tmp_font.render("OUTPUT: "+str(name) ,1, (255,255,255))
  1586. #fr_r = fr.get_rect(center=(int(wx/2),int(0+pm_wy-p*0-10)))
  1587. #window.blit(fr,pos)
  1588. fr = tmp_font.render("INPUT" ,1, (255,255,255))
  1589. fr_r = fr.get_rect(center=(int(wx/2),int(60+pm_wy-p/2)))
  1590. window.blit(fr,fr_r)
  1591. j = 0
  1592. for fix in GRID_OUT:
  1593. if j >= 64:
  1594. break
  1595. j+=1
  1596. ii = i
  1597. #pos = fix.POS(40,60)
  1598. pos = fix.POS() #(0,0)
  1599. rgb = fix.rgb
  1600. pos[0]+=_x
  1601. pos[1]+=_y
  1602. # green
  1603. pygame.draw.rect(window,[0,40,0],pos)
  1604. xposs = [] #None #pos[:]
  1605. for fix2 in GRID:
  1606. if fix._id == fix2._id:
  1607. #_xposs = fix2.POS(40,60)
  1608. #_xposs = fix2.POS(0,0)#_x,_y)
  1609. _xposs = fix2.POS() #(CFG_IN["x1"],CFG_IN["y1"])
  1610. xposs.append( _xposs) #fix2.POS(40,60) )
  1611. # ToDo
  1612. #sub = grab(10,10,60,60)
  1613. #sub = pygame.transform.rotate(sub,90)
  1614. #wn.blit(sub, (500,10))
  1615. for xpos in xposs:
  1616. #sub = grab(xpos[0],xpos[1]+pm_wy,xpos[2],xpos[3])
  1617. sub = grab(xpos[0],xpos[1],xpos[2],xpos[3])
  1618. if NR:
  1619. pygame.draw.rect(window,[255,0,255],xpos,1) # frame on grab area
  1620. if sub:
  1621. window.blit(sub, (pos[0]+z,pos[1]+z))
  1622. else:
  1623. # red
  1624. pygame.draw.rect(window,[40,0,0],pos) #[x+pos[0]+2+z,y+pos[1]+2+z-pm_wy,12,9])
  1625. if xpos[0] < x_min:
  1626. x_min = xpos[0]
  1627. if xpos[0] > x_max:
  1628. x_max += xpos[2]
  1629. if xpos[1] < y_min:
  1630. y_min = xpos[1]
  1631. if xpos[1] > x_max:
  1632. y_max += xpos[3]
  1633. # DRAW FIX NUMBER on TOP
  1634. #apos = fix.POS(40,60)#+pm_wy)
  1635. apos = fix.POS() #0,0)#+pm_wy)
  1636. apos[0]+=_x
  1637. apos[1]+=_y
  1638. argb = fix.rgb
  1639. # overwrite number overlay
  1640. if NR:
  1641. pygame.draw.rect(window,[20,20,20],[apos[0],apos[1],12,8])
  1642. fix_id = fix._id
  1643. if fix_id > 8*8:
  1644. fix_id -= 8*8
  1645. if NR:# == 2: # sub fix_nr
  1646. if fix_id != i+1:
  1647. fr = font12.render("{:02}".format(fix._id) ,1, (255,255,0))
  1648. window.blit(fr,(apos[0],apos[1]))
  1649. else:
  1650. fr = font12.render("{:02}".format(fix._id) ,1, (100,100,255))
  1651. window.blit(fr,(apos[0],apos[1]))
  1652. i += 1
  1653. #print("--#")
  1654. # frame box
  1655. pos1= [x+x_min,y+x_min]
  1656. pos2= [x_max+x_min,y_min+y_max]
  1657. class Timer():
  1658. def __init__(self,start=120):
  1659. self.start = start
  1660. self.timer = self.start
  1661. self.timer_t = time.time()
  1662. def reset(self):
  1663. self.timer = self.start
  1664. def get(self):
  1665. self.timer -= time.time()-self.timer_t
  1666. self.timer_t = time.time()
  1667. if self.timer <= 0:
  1668. self.reset()
  1669. return self.timer
  1670. def reload_grid():
  1671. print("==== reload_grid")
  1672. global GRID
  1673. global GRID_A
  1674. try:
  1675. GRID = init_grid(_x=_x,_y=_y,name="GRID") #init_gird()
  1676. GRID_A = init_grid(_x=8,_y=16,mapping=1,name="GRID_A") #init_gird()
  1677. except Exception as e:
  1678. print("Except: grid re init",e)
  1679. VPU_TEXT = []
  1680. def load_vpu_text(nr=0):
  1681. txt = "NONE"
  1682. if len(VPU_TEXT) > nr:
  1683. txt = VPU_TEXT[0]
  1684. return txt
  1685. import string
  1686. def draw_kachel_nr(): #GRID,_x=0,_y=0):
  1687. if not NR:
  1688. return
  1689. n = 0
  1690. _max_row = 40
  1691. _y=0
  1692. for i in range(8): # ROW / ZEILEN -> ->
  1693. _x=0
  1694. for j in range(30): # COLUMS / SPALTEN | |
  1695. pygame.draw.rect(window,[10,20,10],[40+_x+2,60+_y+2,14,9]) #background
  1696. _j=j #string.ascii_uppercase[j]
  1697. fr = font12.render("{}:{}".format(_j+1,i+1) ,1, (200,200,55))
  1698. window.blit(fr,(40+_x+2,60+_y+2))
  1699. #fr = font12.render("{}".format(_j) ,1, (200,200,55))
  1700. #window.blit(fr,(40+_x+2,60+_y+2))
  1701. #fr = font12.render(":{}".format(i+1) ,1, (200,200,55))
  1702. #window.blit(fr,(40+_x+2+6,60+_y+2))
  1703. _x+=16 # pix
  1704. _y +=16 # pix
  1705. grid_counter = time.time()
  1706. def draw_counter(COUNTER):
  1707. for count in COUNTER:
  1708. cpan = 0
  1709. ctilt = 0
  1710. cr=255
  1711. cg=255
  1712. cb=255
  1713. csize=10
  1714. cdim=0
  1715. k = "DIM"
  1716. if k in count:
  1717. cdim = int(count[k])
  1718. k = "RED"
  1719. if k in count:
  1720. cr = int(count[k])
  1721. k = "GREEN"
  1722. if k in count:
  1723. cg = int(count[k])
  1724. k = "BLUE"
  1725. if k in count:
  1726. cb = int(count[k])
  1727. k = "SIZE"
  1728. if k in count:
  1729. csize = int(count[k]) #/60*p)
  1730. if csize < 5:
  1731. csize = 5
  1732. k = "PAN"
  1733. if k in count:
  1734. cpan = int(count[k])/255*(block[0] *(_x))
  1735. #cpan = int(cpan * 1.2)
  1736. cpan = int(cpan)
  1737. k = "TILT"
  1738. if k in count:
  1739. ctilt = int(count[k])/255*(block[1] *(_y))
  1740. #ctilt = int(cpan * 1.2)
  1741. ctilt = int(ctilt)
  1742. ddim = cdim/255
  1743. if "DIM" in count and count["DIM"] > 0:
  1744. tmp_font = pygame.font.SysFont("freemonobold",int(p/100*csize))
  1745. rgb =(int(cr*ddim),int(cg*ddim),int(cb*ddim),cdim)
  1746. _sec = count["_SEC"]
  1747. try:
  1748. _sec = int(count["_SEC"])
  1749. except:
  1750. pass
  1751. if type(_sec) is int:
  1752. #print(_sec)
  1753. if _sec <= 60:
  1754. fr = tmp_font.render("{:0}".format(_sec) ,1, rgb)
  1755. else:
  1756. _xx = time.strftime("%M:%S",time.localtime(_sec))
  1757. #print("_xx",_xx)
  1758. fr = tmp_font.render("{}".format(_xx) ,1, rgb)
  1759. else:
  1760. fr = tmp_font.render("{}".format((count["_SEC"])) ,1, rgb)
  1761. fr_r = fr.get_rect(center=(40+cpan-(block[0]),60+ctilt+pm_wy))
  1762. pygame.draw.rect(window,[0,0,0],fr_r)
  1763. window.blit(fr,fr_r)
  1764. class FADE():
  1765. def __init__(self,sec):
  1766. self.start = time.time()
  1767. self.sec = sec
  1768. self.val
  1769. def next(self):
  1770. pass
  1771. def video_overlay_draw(wn=None,lines=["line1","line2"],i=0):
  1772. # overlay
  1773. pygame.draw.rect(wn,[255,200,0],[5,MAIN_SIZE[1]-(i+1)*35,380,28])
  1774. font15 = pygame.font.SysFont("freemonobold",17)
  1775. rgb = [255,100,0]
  1776. pygame.draw.rect(wn,rgb,[5,MAIN_SIZE[1]-(i+1)*35,200,13])
  1777. _line = lines[0]
  1778. fr = font15.render(_line ,1, (0,0,0))
  1779. wn.blit(fr,(10,MAIN_SIZE[1]-(i+1)*35))
  1780. _line = lines[1]
  1781. fr = font15.render(_line ,1, (0,0,0))
  1782. wn.blit(fr,(10,MAIN_SIZE[1]-(i+1)*35+15))
  1783. #_line = lines[2]
  1784. #fr = font15.render(_line ,1, (0,0,0))
  1785. #wn.blit(fr,(75,MAIN_SIZE[1]-(i+1)*35+15))
  1786. def draw_all_video_overlay(VIDEO):
  1787. global videplayer
  1788. i=0
  1789. for count in VIDEO:
  1790. video1 = videoplayer[i]
  1791. #video1.overlay_draw(window,"run") # old
  1792. lines = video1._overlay_text()
  1793. video_overlay_draw(wn=window,lines=lines,i=i)
  1794. i += 1
  1795. def del_all_video():
  1796. global videplayer
  1797. i = 0
  1798. print()
  1799. cprint("del_all_video")
  1800. for video in videoplayer:
  1801. cprint("- del video",video)
  1802. video._del()
  1803. def draw_all_video(VIDEO):
  1804. global videplayer
  1805. i = 0
  1806. # set DMX-VALUE to videoplayer Object
  1807. for count in VIDEO:
  1808. cpan = 0
  1809. ctilt = 0
  1810. cr=255
  1811. cg=255
  1812. cb=255
  1813. csize=10
  1814. cang=0
  1815. cdim=0
  1816. video1 = videoplayer[i]
  1817. k = "VIDEO"
  1818. if k in count:
  1819. #video1.select_video(count[k])
  1820. play_nr = int(count[k]/10)
  1821. if play_nr != video1._media_nr:
  1822. print( "+ + + + + + + + + + +", play_nr , video1._media_nr ,str(video1))
  1823. _vid = video1._id
  1824. _cdmx = video1.dmx
  1825. del_video1 = video1
  1826. video1 = PixelMedia(cdmx,_id=_vid)
  1827. video1.select_video(count[k])
  1828. videoplayer[i] = video1
  1829. del_video1._del()
  1830. del del_video1
  1831. k = "DIM"
  1832. if k in count:
  1833. cdim = int(count[k])
  1834. video1.dim = cdim
  1835. #if i == 0:
  1836. # print(i,cdim)
  1837. k = "ANG"
  1838. if k in count:
  1839. cang = int(count[k])
  1840. k = "SIZE"
  1841. if k in count:
  1842. csize = int(count[k]/16*p)
  1843. if csize < 5:
  1844. csize = 5
  1845. k = "PAN"
  1846. cpan_max = block[0] *(_x) #+block[0]
  1847. if k in count:
  1848. if 0:#video1.fname.startswith("cam_"):
  1849. cpan = int(count[k]) #/ 255*cpan_max
  1850. else:
  1851. cpan = int(count[k]) / 255*cpan_max
  1852. cpan = int(cpan)
  1853. k = "TILT"
  1854. ctilt_max = block[1] *(_y) #+block[1]
  1855. if k in count:
  1856. ctilt = int(count[k]) / 255*ctilt_max
  1857. ctilt = int(ctilt)
  1858. k = "_reset"
  1859. if k in count:
  1860. if count[k]:
  1861. count[k] = 0
  1862. video1.restart()
  1863. k = "_RUN"
  1864. if k in count:
  1865. video1._run = count[k]
  1866. video1.pos
  1867. #video1.x=40+0+cpan
  1868. #video1.y=60+0+pm_wy+ctilt
  1869. video1.x=CFG_IN["x1"]+cpan
  1870. video1.y=CFG_IN["y1"]+ctilt
  1871. video1.scale = int((csize))
  1872. video1.angle = int(360-(cang))
  1873. if cdim:
  1874. video1.next()
  1875. i += 1
  1876. # draw
  1877. i=0
  1878. for count in VIDEO:
  1879. video1 = videoplayer[i]
  1880. video1.draw(window) #,x=0,y=0)
  1881. i+=1
  1882. def counter_dmx(COUNTER,dataA):
  1883. for count in COUNTER:
  1884. cDMX=count["DMX"]-1
  1885. try:
  1886. count["DIM"] = dataA[cDMX]
  1887. count["PAN"] = dataA[cDMX+1]
  1888. count["TILT"] = dataA[cDMX+2]
  1889. count["CONTROL"] = dataA[cDMX+3]
  1890. count["SIZE"] = dataA[cDMX+4]
  1891. count["SEC"] = dataA[cDMX+5]
  1892. count["RED"] = dataA[cDMX+6]
  1893. count["GREEN"] = dataA[cDMX+7]
  1894. count["BLUE"] = dataA[cDMX+8]
  1895. if count["CONTROL"] >= 10 and count["CONTROL"] < 20: # RESET COUNTER
  1896. count["_RUN"] = 0
  1897. count["_pause"] = 0
  1898. count["_time"] = time.time()
  1899. count["_start"] = int(count["SEC"])
  1900. count["_SEC"] = int(count["_start"])
  1901. if count["CONTROL"] >= 20 and count["CONTROL"] < 30: # PAUSE COUNTER
  1902. count["_RUN"] = 0
  1903. if count["CONTROL"] >= 30 and count["CONTROL"] < 40: # PLAY COUNTER
  1904. count["_RUN"] = 1
  1905. if count["_RUN"]:
  1906. try:
  1907. if count["_pause"] > 0:
  1908. count["_time"] += time.time()-count["_pause"]
  1909. count["_pause"] = 0
  1910. count["_SEC"] = int(count["_start"] - (time.time() - count["_time"]))
  1911. except Exception as e:
  1912. pass
  1913. else:
  1914. if count["_pause"] == 0:
  1915. count["_pause"] = time.time()
  1916. if type(count["_SEC"]) is int:
  1917. if count["_SEC"] < 0:
  1918. count["_SEC"] = 0
  1919. for ti in range(10):
  1920. #print(ti,(ti+6)*10)
  1921. if count["CONTROL"] >= (ti+6)*10 and count["CONTROL"] < (ti+7)*10:
  1922. count["_SEC"] = "----" #text 1
  1923. try:
  1924. count["_SEC"] = TEXT_BLOCK[ti]
  1925. except Exception as e:
  1926. pass
  1927. if count["CONTROL"] >= 250 and count["CONTROL"] < 256:
  1928. count["_SEC"] = "DMX:{}-{}".format(cDMX+1,cDMX+8+1)
  1929. except Exception as e:
  1930. print("EXC FUNC",e,count)
  1931. #print(count)
  1932. def video_dmx(VIDEO,dataA):
  1933. for count in VIDEO:
  1934. cDMX=count["DMX"]-1
  1935. try:
  1936. count["DIM"] = dataA[cDMX]
  1937. count["PAN"] = dataA[cDMX+1]
  1938. count["TILT"] = dataA[cDMX+2]
  1939. count["CONTROL"] = dataA[cDMX+3]
  1940. if count["CONTROL"] >= 10 and count["CONTROL"] < 20:
  1941. count["_reset"] = 1
  1942. if count["CONTROL"] >= 20 and count["CONTROL"] < 30:
  1943. if count["_RUN"] == 1:
  1944. print( "_RUN:0",count["DMX"]-1)
  1945. count["_RUN"] = 0
  1946. if count["CONTROL"] >= 30 and count["CONTROL"] < 40:
  1947. if count["_RUN"] == 0:
  1948. print( "_RUN:1",count["DMX"]-1)
  1949. count["_RUN"] = 1
  1950. count["SIZE"] = dataA[cDMX+4]#*2
  1951. count["SEC"] = dataA[cDMX+5]
  1952. #count["RED"] = dataA[cDMX+6]
  1953. count["VIDEO"] = dataA[cDMX+6]
  1954. #count["GREEN"] = dataA[cDMX+7]
  1955. count["ANG"] = (int(dataA[cDMX+7])/255*360)
  1956. #count["BLUE"] = dataA[cDMX+8]
  1957. except Exception as e:
  1958. print("VIDEOPLAYER EXCEPT FUNC",e,count)
  1959. #print(count)
  1960. def read_dmx_data(ip,ips):
  1961. ip = select_ip(ips,univ=START_UNIV)
  1962. IP = ip
  1963. data = read_dmx(ip)
  1964. ip = select_ip(ips,univ=START_UNIV+1)
  1965. data3 = read_dmx(ip)
  1966. data.extend(data3)
  1967. ip = select_ip(ips,univ=START_UNIV+2)
  1968. data3 = read_dmx(ip)
  1969. data.extend(data3)
  1970. ip = select_ip(ips,univ=START_UNIV+3)
  1971. data3 = read_dmx(ip)
  1972. data.extend(data3)
  1973. if options.dual_vpu:
  1974. ip = select_ip(ips,univ=START_UNIV+4)
  1975. data3 = read_dmx(ip)
  1976. data.extend(data3)
  1977. ip = select_ip(ips,univ=START_UNIV+5)
  1978. data3 = read_dmx(ip)
  1979. data.extend(data3)
  1980. return data
  1981. def set_pos(GRID,_x=0,_y=0):
  1982. for fix in GRID:
  1983. pos = fix.POS(_x,_y)#40,60+pm_wy)
  1984. for subfix in fix.sub_fix:
  1985. #subfix.dmc_calc(data)
  1986. spos = subfix.POS(_x,_y)
  1987. def draw_gobo(GRID,data,_x=0,_y=0):
  1988. i = 0
  1989. dmx = 1
  1990. h = 1
  1991. v = 1
  1992. for fix in GRID:
  1993. rgb = fix.rgb
  1994. pos = fix.POS() #CFG_IN["x1"],CFG_IN["y1"])
  1995. pygame.draw.rect(window,rgb,pos)
  1996. # DRAW SUB-FIXTURE
  1997. j = 0
  1998. for subfix in fix.sub_fix:#calc(data):
  1999. subfix.dmx_calc(data)
  2000. spos = subfix.POS() #_x,_y)#+pm_wy)
  2001. srgb = subfix.rgb
  2002. if FUNC > 10 and FUNC <= 20: # big dot
  2003. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  2004. elif FUNC > 20 and FUNC <= 30:#small dot
  2005. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  2006. elif FUNC > 30 and FUNC <= 40:#donut
  2007. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  2008. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  2009. elif FUNC > 40 and FUNC <= 50: # rec with hole
  2010. pygame.draw.rect(window,srgb,spos)
  2011. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  2012. elif FUNC > 50 and FUNC <= 60: # rec with big hole
  2013. pygame.draw.rect(window,srgb,spos)
  2014. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  2015. elif FUNC > 60 and FUNC <= 70: # rec with donat
  2016. pygame.draw.rect(window,srgb,spos)
  2017. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  2018. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  2019. elif FUNC > 70 and FUNC <= 80: # rec boarder
  2020. pygame.draw.rect(window,srgb,[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  2021. elif FUNC > 80 and FUNC <= 90: # rec big boarder
  2022. pygame.draw.rect(window,srgb,[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  2023. elif FUNC > 90 and FUNC <= 100: # rec thin line
  2024. pygame.draw.rect(window,srgb,spos)
  2025. pygame.draw.rect(window,[0,0,0],[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  2026. elif FUNC > 100 and FUNC <= 110: # rec big line
  2027. pygame.draw.rect(window,srgb,spos)
  2028. pygame.draw.rect(window,[0,0,0],[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  2029. elif FUNC > 110 and FUNC <= 120: # rec with dot
  2030. pygame.draw.rect(window,srgb,spos)
  2031. pygame.draw.rect(window,[0,0,0],[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  2032. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  2033. elif FUNC > 120 and FUNC <= 130: # rec inline
  2034. pygame.draw.rect(window,srgb,[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  2035. pygame.draw.rect(window,[0,0,0],[spos[0]+3,spos[1]+3,spos[2]-6,spos[3]-6])
  2036. elif FUNC > 130 and FUNC <= 140: # 3 dot (heart)
  2037. draw_circle(window,srgb,(spos[0]+int(spos[2]/2)+2,spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  2038. draw_circle(window,srgb,(spos[0]+int(spos[2]/2)-2,spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  2039. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)+2),int(spos[3]/3.5))
  2040. else:
  2041. pygame.draw.rect(window,srgb,spos)
  2042. i += 1
  2043. def grid_label(GRID,_x=0,_y=0):
  2044. for fix in GRID:
  2045. #pos = fix.POS(_x,_y)
  2046. pos = fix.POS() #CFG_IN["x1"],CFG_IN["y1"])
  2047. rgb = fix.rgb
  2048. # draw row/col grid number
  2049. if pos[0] == 0:
  2050. fr = font12.render("{}".format(pos[1]+1) ,1, (200,200,200))
  2051. window.blit(fr,(10,pos[1]+3 ))
  2052. if pos[1] == 0:
  2053. fr = font12.render("{}".format(pos[0]+1) ,1, (200,200,200))
  2054. window.blit(fr,(pos[0]+2,35 ))
  2055. #pygame.draw.rect(window,rgb,pos)
  2056. def grid_sub_label(GRID,_x=0,_y=0):
  2057. h = 1
  2058. v = 1
  2059. for fix in GRID:
  2060. j = 0
  2061. #pos = fix.POS(_x,_y)
  2062. pos = fix.POS() #CFG_IN["x1"],CFG_IN["y1"])
  2063. for subfix in fix.sub_fix:
  2064. subfix.dmx_calc(data)
  2065. spos = subfix.POS() #(_x,_y)
  2066. srgb = subfix.rgb
  2067. # draw row/col grid number
  2068. if subfix.pos[0] == 0:
  2069. fr = font12.render("{}".format(v ) ,1, (200,200,200))
  2070. window.blit(fr,(25,spos[1] ))
  2071. v += 1
  2072. if subfix.pos[1] == 0:
  2073. fr = font12.render("{}".format(1) ,1, (200,200,200))
  2074. fr = font12.render("{}".format(h ) ,1, (200,200,200))
  2075. h+=1
  2076. window.blit(fr,(spos[0],50 ))
  2077. if p >= 40:
  2078. if NR:
  2079. fr = font15.render("{:02}".format(subfix._id) ,1, (250,200,5))
  2080. window.blit(fr,(spos[0]+2,spos[1]+10))
  2081. j += 1
  2082. def frame_area():
  2083. rgb = [255,255,0]
  2084. CFG = CFG_IN
  2085. p1 = CFG_IN["p1"]
  2086. p2 = CFG_IN["p2"]
  2087. draw_frame(window,rgb,p1,p2,offset=2)
  2088. rgb = [255,0,0]
  2089. #draw_frame(window,rgb,p1,p2,offset=4)
  2090. if CFG_OUT["on"]:
  2091. rgb = [255,0,0]
  2092. p1 = CFG_OUT["p1"]
  2093. p2 = CFG_OUT["p2"]
  2094. #pygame.draw.line(window,rgb,p1,p2)
  2095. draw_frame(window,rgb,p1,p2,offset=3)
  2096. if CFG_OUT2["on"]:
  2097. rgb = [255,0,0]
  2098. p1 = CFG_OUT2["p1"]
  2099. p2 = CFG_OUT2["p2"]
  2100. #pygame.draw.line(window,rgb,p1,p2)
  2101. draw_frame(window,rgb,p1,p2,offset=3)
  2102. ips=[]
  2103. dataA=[]
  2104. data=[]
  2105. def dmx_raw():
  2106. global frame
  2107. frame += 1
  2108. global ips,dataA,data
  2109. ips = read_index()
  2110. # ----
  2111. ip = select_ip(ips,univ=1) # univ 1 gobo
  2112. dataA = read_dmx(ip)
  2113. # ----
  2114. data = read_dmx_data(ip,ips)
  2115. if options.countdown:
  2116. counter_dmx(COUNTER,dataA)
  2117. if len(VIDEO) > 0:
  2118. video_dmx(VIDEO,dataA)
  2119. return ips,dataA,data
  2120. def dmx_loop():
  2121. while 1:
  2122. dmx_raw()
  2123. time.sleep(1/40) # fast high cpu
  2124. #time.sleep(1/25)
  2125. if QUIT:
  2126. break
  2127. dmx_raw()
  2128. thread.start_new_thread(dmx_loop,())
  2129. def draw_output_label(CFG,value,dmx="0.0",name="GRID_XX"):
  2130. dim_raw = value
  2131. pygame.draw.rect(window,[5,5,155],[CFG["x1"]-5,CFG["y2"]+4,130,35]) # background
  2132. # GRID_A1_DIM_LABEL
  2133. fr = font15.render("Output:"+name,1, (255,255,255))
  2134. fr_r = fr.get_rect(center=(int(wx/2),int(0+pm_wy-p*0-10)))
  2135. window.blit(fr,(CFG["x1"],CFG["y2"]+5))
  2136. fr = font15.render("inv-dim: "+str(dim_raw) ,1, (255,255,255))
  2137. fr_r = fr.get_rect(center=(int(wx/2),int(0+pm_wy-p*0-10)))
  2138. window.blit(fr,(CFG["x1"]+60,CFG["y2"]+15))
  2139. fr = font15.render("dmx: {}.{}".format(START_UNIV_A,dmx) ,1, (255,255,255))
  2140. window.blit(fr,(CFG["x1"],CFG["y2"]+15))
  2141. def GRID_DIM(dim,x,y):
  2142. try:
  2143. dim = int(grid_dim_v)
  2144. if dim > 255:
  2145. dim = 255
  2146. if dim <=0:
  2147. dim = 0
  2148. except:pass
  2149. dim_raw = dim*-1
  2150. s = pygame.Surface((p*8,p*8)) # the size of your rect
  2151. s.set_alpha(dim) # alpha level
  2152. s.fill((0,0,0)) # this fills the entire surface
  2153. window.blit(s, (x,y))
  2154. def GRID_A1_DIM():
  2155. # GRID_A1 DIM
  2156. if options.grid_a1_idim:
  2157. CFG=CFG_OUT
  2158. grid_dim_ch = int(options.grid_a1_idim)
  2159. grid_dim_v = dataA[grid_dim_ch-1]
  2160. dim=255
  2161. dim=grid_dim_v
  2162. #dim=120
  2163. GRID_DIM(dim,x=CFG["x1"],y=CFG["y2"]-p*8)
  2164. a=options.grid_a1_idim
  2165. draw_output_label(CFG,dim,a,name="GRID_A1_DIM")
  2166. def GRID_A2_DIM():
  2167. # GRID_A2 DIM
  2168. if options.grid_a2_idim:
  2169. CFG=CFG_OUT2
  2170. grid_dim_ch = int(options.grid_a2_idim)
  2171. grid_dim_v = dataA[grid_dim_ch-1]
  2172. dim=255
  2173. try:
  2174. dim = int(grid_dim_v)
  2175. if dim > 255:
  2176. dim = 255
  2177. if dim <=0:
  2178. dim = 0
  2179. except:pass
  2180. dim_raw = dim*-1
  2181. s = pygame.Surface((p*8,p*8)) # the size of your rect
  2182. s.set_alpha(dim) # alpha level
  2183. s.fill((0,0,0)) # this fills the entire surface
  2184. window.blit(s, (CFG["x1"],CFG["y2"]-p*8))
  2185. a=options.grid_a2_idim
  2186. draw_output_label(CFG,dim_raw,a,name="GRID_A2_DIM")
  2187. def cvImageToSurface(cvImage):
  2188. if cvImage.dtype.name == 'uint16':
  2189. cvImage = (cvImage / 256).astype('uint8')
  2190. size = cvImage.shape[1::-1]
  2191. if len(cvImage.shape) == 2:
  2192. cvImage = np.repeat(cvImage.reshape(size[1], size[0], 1), 3, axis = 2)
  2193. format = 'RGB'
  2194. else:
  2195. format = 'RGBA' if cvImage.shape[2] == 4 else 'RGB'
  2196. cvImage[:, :, [0, 2]] = cvImage[:, :, [2, 0]]
  2197. surface = pygame.image.frombuffer(cvImage.flatten(), size, format)
  2198. return surface.convert_alpha() if format == 'RGBA' else surface.convert()
  2199. def create_img_meta():
  2200. out = {}
  2201. #40 235
  2202. out["pos"] = (0,0) # x,y
  2203. out["ipos"] = (0,0) # x,y
  2204. out["scale"] = 50
  2205. out["dim"] = 100
  2206. out["ang"] = 0
  2207. out["_fpath"] = '/opt/LibreLight/Xdesk/icon/'
  2208. out["fname"] = "picker.png"
  2209. out["raw_img"] = ""
  2210. out["img"] = ""
  2211. out["err"] = []
  2212. return out
  2213. def img_open(img_meta):
  2214. fname = img_meta["_fpath"] +"/"+ img_meta["fname"] #'/opt/LibreLight/Xdesk/icon/picker.png'
  2215. im2 = cv2.imread(fname)
  2216. width = int(im2.shape[1])
  2217. height = int(im2.shape[0])
  2218. w_max = 480
  2219. h_max = 320
  2220. #w_max = 600
  2221. #h_max = 400
  2222. if width > w_max:
  2223. h = int(height*(w_max/width))
  2224. w = w_max
  2225. im2 = cv2.resize(im2, (w,h), interpolation =cv2.INTER_LINEAR)
  2226. if height > h_max:
  2227. w = int(width*(h_max/height))
  2228. h = h_max
  2229. if w < w_max:
  2230. im2 = cv2.resize(im2, (w,h), interpolation =cv2.INTER_LINEAR)
  2231. img_meta["raw_img"] = im2
  2232. img_meta["img"] = im2
  2233. return im2
  2234. def img_jitter(img_meta):
  2235. r1=random.randint(0,3)
  2236. r2=random.randint(0,3)
  2237. img_meta["ipos"] = [r1,r2]
  2238. def img_rescale_width(img_meta, _width):
  2239. frame = img_meta["raw_img"]
  2240. width = int(frame.shape[1])
  2241. height = int(frame.shape[0])
  2242. dim = (width, height)
  2243. if 1: #width > _width:
  2244. h = int(height*( width/_width))
  2245. dim = (_width, h)
  2246. im = cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
  2247. else:
  2248. im = cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
  2249. return im
  2250. def img_rescale(img_meta, percent=75):
  2251. frame = img_meta["raw_img"]
  2252. width = int(frame.shape[1] * percent/ 100)
  2253. height = int(frame.shape[0] * percent/ 100)
  2254. dim = (width, height)
  2255. return cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
  2256. def img_draw_inp(img_meta):
  2257. #img_jitter(img_meta)
  2258. pos = img_meta["pos"]
  2259. ipos = img_meta["ipos"]
  2260. im2 = img_meta["img"]
  2261. im2 = img_rescale(img_meta, percent=img_meta["scale"])
  2262. im2 = cvImageToSurface(im2)
  2263. w2 = int(im2.get_width()/2)
  2264. h2 = int(im2.get_height()/2)
  2265. window.blit(im2, (INPUT_START_POS_X-w2+pos[0]+ipos[0], INPUT_START_POS_Y-h2+pos[1]+ipos[1]))
  2266. def img_draw_raw(img_meta):
  2267. #img_jitter(img_meta)
  2268. pos = img_meta["pos"]
  2269. ipos = img_meta["ipos"]
  2270. im2 = img_meta["img"]
  2271. #def img_rescale_width(img_meta, _width):
  2272. im2 = img_rescale(img_meta, percent=img_meta["scale"])
  2273. im2 = cvImageToSurface(im2)
  2274. w2 = int(im2.get_width()/2)
  2275. h2 = int(im2.get_height()/2)
  2276. window.blit(im2, (pos[0]+ipos[0]-w2, pos[1]+ipos[1]-h2))
  2277. img_meta_a = create_img_meta()
  2278. img_open(img_meta_a)
  2279. t1 = Timer(143)
  2280. time.sleep(0.33)
  2281. t2 = Timer(11)
  2282. count_tilt = 0
  2283. def main():
  2284. global IP
  2285. global GRID
  2286. global GRID_A
  2287. global FUNC
  2288. global count_tilt
  2289. global TEXT_BLOCK
  2290. global TEXT_BLOCK_TIME
  2291. global MEDIALIST
  2292. global MEDIALIST_TIME
  2293. global dataA
  2294. global frame2
  2295. global runnung
  2296. reload_grid()
  2297. s=time.time()
  2298. r = ""
  2299. IP = "xx"
  2300. while running:
  2301. if TEXT_BLOCK_TIME+5 < time.time():
  2302. TEXT_BLOCK = open_text_block()
  2303. TEXT_BLOCK_TIME = time.time()
  2304. if MEDIALIST_TIME+6 < time.time():
  2305. MEDIALIST = open_medialist()
  2306. MEDIALIST_TIME = time.time()
  2307. pygame.display.flip()
  2308. set_pos(GRID,_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2309. #set_pos(GRID,600,600)#_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2310. event()
  2311. window.fill((10,0,30))
  2312. calc_fps()
  2313. calc_fps2()
  2314. #draw_overlay()
  2315. # GRID loop
  2316. try:
  2317. FUNC = dataA[gobo_ch-1]
  2318. FUNC2 = FUNC
  2319. except Exception as e:
  2320. print("EXC FUNC",e)
  2321. #if int(time.time()) % 2: # BLINK
  2322. #set_gobo_pos(GRID,_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2323. draw_gobo(GRID,data) #,_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2324. #if int(time.time()) % 2: # BLINK
  2325. grid_label(GRID,_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2326. #grid_sub_label(GRID) #,_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2327. if VIDEO:
  2328. draw_all_video(VIDEO)
  2329. #draw_all_video_overlay(VIDEO)
  2330. #img_draw(img_meta_a)
  2331. if options.countdown:
  2332. draw_counter(COUNTER)
  2333. #pygame.draw.rect(window,[5,5,5],[0,60,MAIN_SIZE[0],p*9]) # background
  2334. pointer.draw(0,pm_wy) #wy
  2335. spos = [0,0,0,0]
  2336. if PIXEL_MAPPING >= 1:
  2337. #try:
  2338. if options.dual_vpu:
  2339. GRID_X = GRID_A[8*8:]
  2340. xx = p*8
  2341. #reshape(GRID,GRID_X,CFG_OUT2["x1"],CFG_OUT2["y1"]-p*8,name="GRID_A2") #start pos
  2342. #except Exception as e:
  2343. # print("Exception 23123",e)
  2344. reshape2(GRID,GRID_A,CFG_OUT["x1"],CFG_OUT["y1"],name="GRID_A1") #start pos
  2345. #reshape(GRID,GRID_A,CFG_OUT["x1"],CFG_OUT["y1"],name="GRID_A1") #start pos
  2346. else:
  2347. pass #reshape(GRID,GRID_A,spos[0]+spos[2]+20,10) #start pos
  2348. frame_area()
  2349. # OUTPUT -> MAPING
  2350. draw_kachel_nr()
  2351. output_mask() # mask background
  2352. input_mask() # mask background
  2353. GRID_A1_DIM() # overlay
  2354. GRID_A2_DIM() # overlay
  2355. if VIDEO:
  2356. draw_all_video_overlay(VIDEO)
  2357. draw_main_overlay()
  2358. pointer.draw(0,pm_wy) #wy
  2359. pygame.display.flip()
  2360. clock.tick(25)
  2361. if 'SDL_VIDEO_WINDOW_POS' in os.environ:
  2362. del os.environ['SDL_VIDEO_WINDOW_POS'] #= '%i,%i' % (200,164)
  2363. frame2 += 1
  2364. if __name__ == "__main__":
  2365. try:
  2366. main()
  2367. finally:
  2368. del_all_video()