vpu_live4.py 80 KB

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