vpu_live4.py 80 KB

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