vpu_live4.py 81 KB

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