vpu_live4.py 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998
  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. rgb=[255,0,0]
  1096. pos=[400,400,160,160]
  1097. draw_box_frame(window,rgb,pos,offset=1)
  1098. r_sub = grab(x=self._x-__t/2,y=self._y-__t/2,w=__t,h=__t)
  1099. if r_sub:
  1100. r_sub = pygame.transform.scale(r_sub,(160,160))
  1101. if r_sub: # target
  1102. window.blit(r_sub, (400,400))
  1103. #pos=[400+40,400+40,80,80]
  1104. pos=[400+80,400+80,80,80]
  1105. draw_box_frame(window,rgb,pos,offset=3)
  1106. draw_box_frame(window,rgb,pos,offset=2)
  1107. draw_box_frame(window,rgb,pos,offset=0)
  1108. draw_box_frame(window,rgb,pos,offset=1)
  1109. # fix pos
  1110. #txt=str(pos) #"[0, 0, 0, 0]"
  1111. #fr = font15.render(txt ,1, (200,200,200))
  1112. #window.blit(fr,(10,1))
  1113. # pointer
  1114. fr = font15.render("X:{:03}".format(self._x) ,1, (200,200,200))
  1115. window.blit(fr,(10,30))
  1116. fr = font15.render("Y:{:03}".format(self._y) ,1, (200,200,200))
  1117. window.blit(fr,(10,40))
  1118. if 1: #self._x > 30 and self._y > 230:
  1119. # pointer
  1120. _x = self._x-INPUT_START_POS_X
  1121. _y = self._y-INPUT_START_POS_Y
  1122. rgb = [200,200,200]
  1123. if _x < 0:
  1124. rgb = [200,0,0]
  1125. fr = font80.render("X:{:03}".format(_x) ,1, rgb)
  1126. window.blit(fr,(200,400))
  1127. rgb = [200,200,200]
  1128. if _y < 0:
  1129. rgb = [200,0,0]
  1130. fr = font80.render("Y:{:03}".format(_y) ,1, rgb)
  1131. window.blit(fr,(200,450))
  1132. p=110
  1133. # crosshair
  1134. self.rgb = [0,0,200]
  1135. pygame.draw.line(window,self.rgb, (self.x-p,self.y) , (self.x-2,self.y),2 )
  1136. pygame.draw.line(window,self.rgb, (self.x,self.y-p) , (self.x,self.y-2),2 )
  1137. self.rgb = [0,200,0]
  1138. pygame.draw.line(window,self.rgb, (self.x+2,self.y) , (self.x+p,self.y),2 )
  1139. pygame.draw.line(window,self.rgb, (self.x,self.y+2) , (self.x,self.y+p),2 )
  1140. self.rgb = [200,0,0]
  1141. pointer = POINTER()
  1142. NR = 0
  1143. running = True
  1144. def event():
  1145. global NR,running
  1146. for event in pygame.event.get():
  1147. #print(event.dict)
  1148. _button = None
  1149. if "button" in event.dict:
  1150. _button = event.dict["button"]
  1151. _state = None
  1152. if "state" in event.dict:
  1153. _state = event.state
  1154. _key = None
  1155. if "key" in event.dict:
  1156. _key = event.key
  1157. _pos = None
  1158. if "pos" in event.dict:
  1159. _pos = event.pos
  1160. _type = None
  1161. if "type" in event.dict:
  1162. _type = event.type
  1163. _type = event.type
  1164. _mod = None
  1165. if "mod" in event.dict:
  1166. _mod = event.mod
  1167. if 0:
  1168. print( " ")
  1169. print( "{:.02f}".format( time.time() - START ))
  1170. print("button -",_button,end="\t| ")
  1171. #print("state -",_state)
  1172. print("pos -",_pos)
  1173. print("type -",_type, end="\t| ")
  1174. print("key -",_key)
  1175. print("mod -",_mod)
  1176. try:
  1177. if _type in [5,1025]:
  1178. if _button == 1:
  1179. NR += 1
  1180. if NR > 1:
  1181. NR = 0
  1182. if _button == 3:
  1183. NR -= 1
  1184. if NR < 0:
  1185. NR = 1
  1186. if _pos:
  1187. posA = _pos
  1188. fix = find_pix(_pos[0]-40,_pos[1]-60+pm_wy)
  1189. if fix:
  1190. #pos = fix.POS(40,60+pm_wy)
  1191. pos = fix.POS() #(CFG_IN["x1"],CFG_IN["y1"])#0,0) #+pm_wy)
  1192. rgb = [0,0,0]
  1193. pointer.move(pos)
  1194. pointer.fix = fix
  1195. else:
  1196. pointer.on = 0
  1197. pointer.row_move(_pos[0],_pos[1])
  1198. pointer.cross(_pos[0],_pos[1])
  1199. if event.type == pygame.VIDEORESIZE:
  1200. window = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)
  1201. except Exception as e:
  1202. cprint(e,color="red")
  1203. if event.type==pygame.QUIT:
  1204. global QUIT
  1205. QUIT = 1
  1206. cprint("QUIT",color="red")
  1207. running=False
  1208. #time.sleep(1)
  1209. #sys.exit()
  1210. fps = 0
  1211. fps2 = 0
  1212. frame = 0
  1213. frame2 = 0
  1214. frame_t = time.time()
  1215. frame2_t = time.time()
  1216. IP = "yyy"
  1217. vplay1=0
  1218. vplay2=0
  1219. if options.videoplayer:
  1220. try:
  1221. t=options.videoplayer.split(",")
  1222. vplay1=t[0]
  1223. vplay2=t[1]
  1224. except:pass
  1225. def output_mask(): # oben
  1226. RGB = [0,0,50]
  1227. #RGB = [120,0,0] # test
  1228. start = 0
  1229. # oben
  1230. pygame.draw.rect(window,RGB,[0,start,MAIN_SIZE[0]+200,57]) # background
  1231. # unten
  1232. pygame.draw.rect(window,RGB,[0,start+192,MAIN_SIZE[0]+200,35]) # background
  1233. # links
  1234. pygame.draw.rect(window,RGB,[0,start+0,35,235]) # background
  1235. def input_mask(): # unten
  1236. RGB = [0,0,50]
  1237. #RGB = [120,0,110] # test
  1238. start = 227
  1239. # oben
  1240. pygame.draw.rect(window,RGB,[0,start,MAIN_SIZE[0]+200,7]) # background
  1241. # links
  1242. pygame.draw.rect(window,RGB,[0,start,35,200]) # background
  1243. # unten
  1244. pygame.draw.rect(window,RGB,[0,start+173,MAIN_SIZE[0]+200,198]) # background
  1245. def draw_main_overlay():
  1246. global fps,fps2
  1247. fr = font15.render("DMX-FPS: {}".format(fps) ,1, (200,0,255))
  1248. window.blit(fr,(10,2))
  1249. fr = font15.render("GUI-FPS : {}".format(fps2) ,1, (200,0,255))
  1250. window.blit(fr,(10,12))
  1251. #fr = font15.render("ip:{}".format(IP) ,1, (200,0,255))
  1252. #window.blit(fr,(100,2))
  1253. fr = font15.render("start-uni: {:}.xx dRGB".format(START_UNIV) ,1, (200,0,255))
  1254. window.blit(fr,(90,2))
  1255. fr = font15.render("a1_idim: {}.{:}".format(START_UNIV_A,options.grid_a1_idim) ,1, (200,0,255))
  1256. window.blit(fr,(90,12))
  1257. fr = font15.render("a2_idim: {}.{:}".format(START_UNIV_A,options.grid_a2_idim) ,1, (200,0,255))
  1258. window.blit(fr,(90,22))
  1259. fr = font15.render("gobo_ch1: {}.{:}".format(START_UNIV_A,options.gobo_ch) ,1, (200,0,255))
  1260. window.blit(fr,(180,12))
  1261. fr = font15.render("gobo_ch2: {}.{:}".format(START_UNIV_A,options.gobo_ch2) ,1, (200,0,255))
  1262. window.blit(fr,(180,22))
  1263. fr = font15.render("v-play1: {}.{:}".format(START_UNIV_A,vplay1) ,1, (200,0,255))
  1264. window.blit(fr,(270,12))
  1265. fr = font15.render("v-play2: {}.{:}".format(START_UNIV_A,vplay2) ,1, (200,0,255))
  1266. window.blit(fr,(270,22))
  1267. try:
  1268. global pointer
  1269. # pointer
  1270. fr = font15.render("X:{:03}".format(pointer._x) ,1, (200,200,200))
  1271. window.blit(fr,(10,30))
  1272. fr = font15.render("Y:{:03}".format(pointer._y) ,1, (200,200,200))
  1273. window.blit(fr,(10,40))
  1274. except Exception as e:
  1275. print("Overlay err:",e)
  1276. def draw_box_frame(window,rgb,pos,offset=0):
  1277. p1 = [pos[0],pos[1]]
  1278. p2 = [pos[0]+pos[2],pos[1]+pos[3]]
  1279. o = offset
  1280. _p1 = [p1[0]-o,p1[1]-o]
  1281. _p2 = [p1[0]-o,p2[1]+o]
  1282. pygame.draw.line(window,rgb,_p1,_p2) # left
  1283. _p1 = [p2[0]+o,p2[1]+o]
  1284. pygame.draw.line(window,rgb,_p1,_p2) # bottom
  1285. _p2 = [p2[0]+o,p1[1]-o]
  1286. pygame.draw.line(window,rgb,_p1,_p2) # right
  1287. _p1 = [p1[0]-o,p1[1]-o]
  1288. pygame.draw.line(window,rgb,_p1,_p2) # top
  1289. def draw_frame(window,rgb,p1,p2,offset=0):
  1290. o = offset
  1291. _p1 = [p1[0]-o,p1[1]-o]
  1292. _p2 = [p1[0]-o,p2[1]+o]
  1293. #pygame.draw.line(window,rgb,_p1,_p2) # left
  1294. _p1 = [p2[0]+o,p2[1]+o]
  1295. pygame.draw.line(window,rgb,_p1,_p2) # bottom
  1296. _p2 = [p2[0]+o,p1[1]-o]
  1297. #pygame.draw.line(window,rgb,_p1,_p2) # right
  1298. _p1 = [p1[0]-o,p1[1]-o]
  1299. pygame.draw.line(window,rgb,_p1,_p2) # top
  1300. def calc_fps():
  1301. global fps,frame,frame_t
  1302. t = time.time()
  1303. if frame_t+1 < t:
  1304. fps = frame #frame_t- t #frame
  1305. frame = 1
  1306. frame_t = time.time()
  1307. def calc_fps2():
  1308. global fps2,frame2,frame2_t
  1309. t = time.time()
  1310. if frame2_t+0.1 < t:
  1311. fps2 = frame2*10 #frame_t- t #frame
  1312. frame2 = 1
  1313. frame2_t = t #time.time()
  1314. TEXT_BLOCK = []
  1315. def _create_text_block():
  1316. print("======== CREATE NEW TEXT FILE !!",text_file)
  1317. f = open(text_file,"w")
  1318. for i in range(10):
  1319. f.write("TEXT {}\n".format(i+1))
  1320. f.close()
  1321. def open_text_block():
  1322. print("======== OPEN TEXT FILE !!",text_file)
  1323. _lines = []
  1324. try:
  1325. f = open(text_file,"r")
  1326. _lines = f.readlines()
  1327. f.close()
  1328. except FileNotFoundError as e:
  1329. print("TEXT",e)
  1330. _create_text_block()
  1331. if len(_lines) <= 0:
  1332. _create_text_block()
  1333. lines = []
  1334. for l in _lines:
  1335. #print(">> ",l.strip())
  1336. lines.append(l.strip())
  1337. if len(lines) <= 10:
  1338. for i in range(10-len(lines)):
  1339. lines.append("LINE ERROR")
  1340. return lines
  1341. TEXT_BLOCK = open_text_block()
  1342. TEXT_BLOCK_TIME = time.time()
  1343. # video medialist
  1344. MEDIALIST = []
  1345. def open_medialist():
  1346. print("======== OPEN MEDIALIST DIR !!",media_list)
  1347. if not os.path.isdir(media_list):
  1348. os.system("mkdir -p {}".format(media_list))
  1349. _lines = os.listdir(media_list)
  1350. _lines.sort()
  1351. lines = ['']*25
  1352. i=0
  1353. for l in _lines:
  1354. l = l.strip()
  1355. if "_" in l:
  1356. ll = l.split("_",1)
  1357. print(">> ",ll)
  1358. try:
  1359. lll = int(ll[0])
  1360. lines[lll] = l
  1361. except:pass
  1362. if len(lines) <= 10:
  1363. for i in range(10-len(lines)):
  1364. lines.append("")#"LINE ERROR")
  1365. return lines
  1366. MEDIALIST_TIME = time.time()
  1367. MEDIALIST = open_medialist()
  1368. # ===== GUI =========
  1369. def draw_circle(surface,color, pos, radius):
  1370. x,y=pos
  1371. pygame.gfxdraw.aacircle(surface, int(x), int(y), radius-1, color)
  1372. pygame.gfxdraw.filled_circle(surface, int(x), int(y), radius-1, color)
  1373. def rDMX(univ,dmx):
  1374. return univ*512+dmx
  1375. def generate_grid(mapping=0):
  1376. _log = []
  1377. #if PIXEL_MAPPING:
  1378. # log = open(grid_file,"w")
  1379. head = "i,univ,dmx,x,y,ch\n"
  1380. head = "i,univ,dmx,ch\n"
  1381. head = "univ,dmx,x,y,ch\n"
  1382. head = "nr,id,info\n"
  1383. print("csv:",head)
  1384. _log.append(head)
  1385. dmx = 1-1
  1386. ch = 4
  1387. y=0
  1388. x=0
  1389. for i in range((_y)*(_x)):
  1390. #if x > _x and i%_x == 0:
  1391. if x > 8 and i%8 == 0:
  1392. #print("--> -->")
  1393. x=0
  1394. y+=1
  1395. _univ = int(dmx/512)
  1396. _dmx = dmx - (_univ)*512
  1397. pos=[x,y]
  1398. line="{},{},x\n".format(i+1,i+1)
  1399. _log.append(line)
  1400. dmx += ch
  1401. x+=1
  1402. if mapping and PIXEL_MAPPING:
  1403. print("CREATE NEW PIXELMAP FILE !!",grid_file)
  1404. log = open(grid_file,"w")
  1405. log.writelines(_log)
  1406. log.close()
  1407. return _log[:] #GRID
  1408. def init_grid(mapping=0,_x=4,_y=4,_xp=0,_yp=0,start=0,name="init_gird"):
  1409. if mapping and PIXEL_MAPPING:
  1410. try:
  1411. log = open(grid_file,"r")
  1412. except:
  1413. generate_grid(mapping=mapping)
  1414. log = open(grid_file,"r")
  1415. lines = log.readlines()
  1416. else:
  1417. lines = generate_grid()
  1418. GRID = []
  1419. y=0
  1420. x=0
  1421. print("CSV header",[lines[0]],[PIXEL_MAPPING,"len:",len(lines),"start:",start,name])
  1422. for i,line in enumerate(lines[1+start:]): #exclude first line
  1423. #print("rcsv",[line])
  1424. line = line.strip()
  1425. line = line.split(",") # csv
  1426. if i >= _x and i%_x == 0:
  1427. x=0
  1428. y+=1
  1429. if y >= _y:
  1430. break
  1431. _id = int(line[1])
  1432. pos = [x+_xp,y+_yp]
  1433. f = Fix(_id,pos,block) #pos,univ,dmx,ch)
  1434. GRID.append(f)
  1435. x+=1
  1436. return GRID
  1437. def find_pix(x,y):
  1438. global GRID
  1439. for fix in GRID:
  1440. X = 0
  1441. Y = 0
  1442. pos = fix.POS()
  1443. if x > pos[0] and x < pos[0]+pos[2]:
  1444. X = 1
  1445. if y > pos[1] and y < pos[1]+pos[3]:
  1446. Y = 1
  1447. if X and Y:
  1448. return fix
  1449. GRID = []
  1450. GRID_A = []
  1451. NR = 0
  1452. START_UNIV_A=1
  1453. START_UNIV=2
  1454. if options.start_univ:
  1455. try:
  1456. START_UNIV=int(options.start_univ)
  1457. except Exception as e:
  1458. print("Exception START UNIV",e)
  1459. gobo_ch=1
  1460. if options.gobo_ch:
  1461. try:
  1462. gobo_ch=int(options.gobo_ch)
  1463. except Exception as e:
  1464. print("Exception gobo_ch",e)
  1465. if gobo_ch <= 0:
  1466. gobo_ch = 1
  1467. def draw_box(pos1,pos2,color=[128,128,128],text=1):
  1468. color = [200,0,0,127]
  1469. if text:
  1470. fr = font15.render("A" ,1, (200,200,200))
  1471. window.blit(fr,pos1)
  1472. fr = font15.render("B" ,1, (200,200,200))
  1473. window.blit(fr,[pos2[0]-10,pos2[1]-10])
  1474. # h unten
  1475. _pos1 = [pos1[0],pos2[1]]
  1476. _pos2 = [pos2[0],pos2[1]]
  1477. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1478. color = [255,255,0,127]
  1479. # h rechts
  1480. _pos1 = [pos2[0],pos1[1]]
  1481. _pos2 = [pos2[0],pos2[1]]
  1482. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1483. color = [0,200,0,127]
  1484. # h links
  1485. _pos1 = [pos1[0],pos1[1]]
  1486. _pos2 = [pos1[0],pos2[1]]
  1487. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1488. color = [0,0,200,127]
  1489. # h oben
  1490. _pos1 = [pos1[0],pos1[1]]
  1491. _pos2 = [pos2[0],pos1[1]]
  1492. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1493. def grab(x=55,y=55,w=60,h=60):
  1494. crop = None
  1495. rect = pygame.Rect(x, y, w, h)
  1496. try:
  1497. sub = window.subsurface(rect)
  1498. crop = pygame.Surface((w,h))
  1499. crop.blit(sub, (0,0))
  1500. except ValueError as e:
  1501. pass
  1502. return crop
  1503. vpu_wall_txt="""---,quelle,,---,ziel,---,---
  1504. out,x,y,ang,row,col,info
  1505. Wall1,0,0,0,1,1,
  1506. Wall1,0,0,0,2,2,
  1507. Wall1,0,0,0,3,3,
  1508. Wall1,0,0,0,4,4,
  1509. Wall1,0,0,0,5,5,
  1510. Wall1,0,0,0,6,6,
  1511. Wall1,0,0,0,7,7,
  1512. ,,,,,,
  1513. Wall2,0,0,0,4,1,----
  1514. Wall2,0,0,0,5,2,
  1515. Wall2,4,4,0,6,3,
  1516. Wall2,0,0,0,7,4,
  1517. Wall2,0,0,0,8,5,
  1518. Wall2,0,0,0,9,6,
  1519. ,,,,,,
  1520. ,,,,,,
  1521. Wall3,64,48,0,12,1,----
  1522. Wall3,64,64,0,12,2,
  1523. Wall3,64,80,0,12,3,
  1524. Wall3,64,96,0,12,4,
  1525. Wall3,64,112,0,12,5,
  1526. Wall3,64,128,0,12,6,"""
  1527. def create_vpu_wall_conf():
  1528. f=open(vpu_wall_conf_file,"w")
  1529. f.write(vpu_wall_txt)
  1530. f.close()
  1531. print("CREATE VPU CONFIG !")
  1532. wall_data = []
  1533. _last_wall_config_time = time.time()
  1534. def read_wall_pix(init=0):
  1535. global _last_wall_config_time
  1536. global wall_data, NR
  1537. grid = []
  1538. #if not NR and init==0:
  1539. # return
  1540. ok = 0
  1541. if init:
  1542. ok = 1
  1543. if NR:
  1544. if time.time()-_last_wall_config_time > 3:
  1545. ok = 1
  1546. else:
  1547. if time.time()-_last_wall_config_time > 10:
  1548. ok = 1
  1549. if not ok:
  1550. return
  1551. _last_wall_config_time = time.time()
  1552. if not os.path.isfile(vpu_wall_conf_file):
  1553. create_vpu_wall_conf()
  1554. f=open(vpu_wall_conf_file,"r")
  1555. lines = f.readlines()
  1556. for i,line in enumerate(lines):
  1557. line = line.strip()
  1558. if not line:
  1559. continue
  1560. line = line.split(",")
  1561. if not line[0]:
  1562. continue
  1563. #print(i,line)
  1564. if len(line) >= 3:
  1565. _l = []
  1566. for i in line:
  1567. try:
  1568. _l.append(int(i))
  1569. except Exception as e:
  1570. _l.append(i)
  1571. grid.append(_l)
  1572. for i,v in enumerate(grid):
  1573. print(i,v)
  1574. wall_data = grid
  1575. read_wall_pix(init=1)
  1576. def reshape2(GRID,GRID_OUT,_x,_y,name="GRID_Z"):
  1577. _w = 16
  1578. _h = 16
  1579. from_x_min = INPUT_START_POS_X
  1580. from_y_min = INPUT_START_POS_Y
  1581. target_x = 40 # min
  1582. target_y = 60 # min
  1583. ang=0
  1584. #read_block_pix()
  1585. read_wall_pix()
  1586. _wall_data = wall_data[:]
  1587. for i,_wall in enumerate(_wall_data):
  1588. #print(_wall)
  1589. if len(_wall) <=6:
  1590. continue
  1591. try:
  1592. if type(_wall[1]) is str:
  1593. continue
  1594. if type(_wall[2]) is str:
  1595. continue
  1596. if type(_wall[3]) is str:
  1597. continue
  1598. if type(_wall[4]) is str:
  1599. continue
  1600. if type(_wall[5]) is str:
  1601. continue
  1602. from_x=from_x_min+_wall[1]#*_w
  1603. from_y=from_y_min+_wall[2]#*_h
  1604. _a= _wall[3]
  1605. ang=0
  1606. if _a < 0:
  1607. ang=0
  1608. elif _a > 0 and _a <= 90:
  1609. ang = 90
  1610. elif _a > 90 and _a <= 180:
  1611. ang = 180
  1612. elif _a > 180 and _a <= 270:
  1613. ang = 270
  1614. target_x=40+(_wall[4]-1)*_w
  1615. target_y=60+(_wall[5]-1)*_h
  1616. sub = grab(from_x,from_y,_w,_h)
  1617. #draw_box_frame(window,[0,40,0],[from_x+x,from_y+y,_w,_h])
  1618. if sub:
  1619. sub = pygame.transform.rotate(sub,ang)
  1620. window.blit(sub,(target_x,target_y))
  1621. except Exception as e:
  1622. cprint(" ",_wall,color="red")
  1623. cprint("WALL err:",e,color="red")
  1624. def reshape(GRID,GRID_OUT,_x,_y,name="GRID_Z"):
  1625. """reshape LED-WALL Block/Pixel mapping"""
  1626. if PIXEL_MAPPING <= 0:
  1627. return None
  1628. x = _x
  1629. y = _y
  1630. i = 0
  1631. counter = 0
  1632. z=0
  1633. x_min = 99999
  1634. x_max = 0
  1635. y_min = 99999
  1636. y_max = 0
  1637. pos= [0,0]
  1638. pos[0] = _x+p*3 #+10
  1639. pos[1] = p*8+65 #-10 #_y+p*8+80
  1640. tmp_font = pygame.font.SysFont("freemonobold",int(p*0.8))
  1641. #fr = tmp_font.render("OUTPUT: "+str(name) ,1, (255,255,255))
  1642. #fr_r = fr.get_rect(center=(int(wx/2),int(0+pm_wy-p*0-10)))
  1643. #window.blit(fr,pos)
  1644. fr = tmp_font.render("INPUT" ,1, (255,255,255))
  1645. fr_r = fr.get_rect(center=(int(wx/2),int(60+pm_wy-p/2)))
  1646. window.blit(fr,fr_r)
  1647. j = 0
  1648. for fix in GRID_OUT:
  1649. if j >= 64:
  1650. break
  1651. j+=1
  1652. ii = i
  1653. #pos = fix.POS(40,60)
  1654. pos = fix.POS() #(0,0)
  1655. rgb = fix.rgb
  1656. pos[0]+=_x
  1657. pos[1]+=_y
  1658. # green
  1659. pygame.draw.rect(window,[0,40,0],pos)
  1660. xposs = [] #None #pos[:]
  1661. for fix2 in GRID:
  1662. if fix._id == fix2._id:
  1663. #_xposs = fix2.POS(40,60)
  1664. #_xposs = fix2.POS(0,0)#_x,_y)
  1665. _xposs = fix2.POS() #(CFG_IN["x1"],CFG_IN["y1"])
  1666. xposs.append( _xposs) #fix2.POS(40,60) )
  1667. # ToDo
  1668. #sub = grab(10,10,60,60)
  1669. #sub = pygame.transform.rotate(sub,90)
  1670. #wn.blit(sub, (500,10))
  1671. for xpos in xposs:
  1672. #sub = grab(xpos[0],xpos[1]+pm_wy,xpos[2],xpos[3])
  1673. sub = grab(xpos[0],xpos[1],xpos[2],xpos[3])
  1674. if NR:
  1675. pygame.draw.rect(window,[255,0,255],xpos,1) # frame on grab area
  1676. if sub:
  1677. window.blit(sub, (pos[0]+z,pos[1]+z))
  1678. else:
  1679. # red
  1680. pygame.draw.rect(window,[40,0,0],pos) #[x+pos[0]+2+z,y+pos[1]+2+z-pm_wy,12,9])
  1681. if xpos[0] < x_min:
  1682. x_min = xpos[0]
  1683. if xpos[0] > x_max:
  1684. x_max += xpos[2]
  1685. if xpos[1] < y_min:
  1686. y_min = xpos[1]
  1687. if xpos[1] > x_max:
  1688. y_max += xpos[3]
  1689. # DRAW FIX NUMBER on TOP
  1690. #apos = fix.POS(40,60)#+pm_wy)
  1691. apos = fix.POS() #0,0)#+pm_wy)
  1692. apos[0]+=_x
  1693. apos[1]+=_y
  1694. argb = fix.rgb
  1695. # overwrite number overlay
  1696. if NR:
  1697. pygame.draw.rect(window,[20,20,20],[apos[0],apos[1],12,8])
  1698. fix_id = fix._id
  1699. if fix_id > 8*8:
  1700. fix_id -= 8*8
  1701. if NR:# == 2: # sub fix_nr
  1702. if fix_id != i+1:
  1703. fr = font12.render("{:02}".format(fix._id) ,1, (255,255,0))
  1704. window.blit(fr,(apos[0],apos[1]))
  1705. else:
  1706. fr = font12.render("{:02}".format(fix._id) ,1, (100,100,255))
  1707. window.blit(fr,(apos[0],apos[1]))
  1708. i += 1
  1709. #print("--#")
  1710. # frame box
  1711. pos1= [x+x_min,y+x_min]
  1712. pos2= [x_max+x_min,y_min+y_max]
  1713. class Timer():
  1714. def __init__(self,start=120):
  1715. self.start = start
  1716. self.timer = self.start
  1717. self.timer_t = time.time()
  1718. def reset(self):
  1719. self.timer = self.start
  1720. def get(self):
  1721. self.timer -= time.time()-self.timer_t
  1722. self.timer_t = time.time()
  1723. if self.timer <= 0:
  1724. self.reset()
  1725. return self.timer
  1726. def reload_grid():
  1727. print("==== reload_grid")
  1728. global GRID
  1729. global GRID_A
  1730. try:
  1731. GRID = init_grid(_x=_x,_y=_y,name="GRID") #init_gird()
  1732. GRID_A = init_grid(_x=8,_y=16,mapping=1,name="GRID_A") #init_gird()
  1733. except Exception as e:
  1734. cprint("Except: grid re init",e,color="red")
  1735. VPU_TEXT = []
  1736. def load_vpu_text(nr=0):
  1737. txt = "NONE"
  1738. if len(VPU_TEXT) > nr:
  1739. txt = VPU_TEXT[0]
  1740. return txt
  1741. import string
  1742. def draw_kachel_nr(): #GRID,_x=0,_y=0):
  1743. if not NR:
  1744. return
  1745. n = 0
  1746. _max_row = 40
  1747. _y=0
  1748. for i in range(8): # ROW / ZEILEN -> ->
  1749. _x=0
  1750. for j in range(30): # COLUMS / SPALTEN | |
  1751. pygame.draw.rect(window,[10,20,10],[40+_x+2,60+_y+2,14,9]) #background
  1752. _j=j #string.ascii_uppercase[j]
  1753. fr = font12.render("{}:{}".format(_j+1,i+1) ,1, (200,200,55))
  1754. window.blit(fr,(40+_x+2,60+_y+2))
  1755. #fr = font12.render("{}".format(_j) ,1, (200,200,55))
  1756. #window.blit(fr,(40+_x+2,60+_y+2))
  1757. #fr = font12.render(":{}".format(i+1) ,1, (200,200,55))
  1758. #window.blit(fr,(40+_x+2+6,60+_y+2))
  1759. _x+=16 # pix
  1760. _y +=16 # pix
  1761. grid_counter = time.time()
  1762. def draw_counter(COUNTER):
  1763. for count in COUNTER:
  1764. cpan = 0
  1765. ctilt = 0
  1766. cr=255
  1767. cg=255
  1768. cb=255
  1769. csize=10
  1770. cdim=0
  1771. k = "DIM"
  1772. if k in count:
  1773. cdim = int(count[k])
  1774. k = "RED"
  1775. if k in count:
  1776. cr = int(count[k])
  1777. k = "GREEN"
  1778. if k in count:
  1779. cg = int(count[k])
  1780. k = "BLUE"
  1781. if k in count:
  1782. cb = int(count[k])
  1783. k = "SIZE"
  1784. if k in count:
  1785. csize = int(count[k]) #/60*p)
  1786. if csize < 5:
  1787. csize = 5
  1788. k = "PAN"
  1789. if k in count:
  1790. cpan = int(count[k])/255*(block[0] *(_x))
  1791. #cpan = int(cpan * 1.2)
  1792. cpan = int(cpan)
  1793. k = "TILT"
  1794. if k in count:
  1795. ctilt = int(count[k])/255*(block[1] *(_y))
  1796. #ctilt = int(cpan * 1.2)
  1797. ctilt = int(ctilt)
  1798. ddim = cdim/255
  1799. if "DIM" in count and count["DIM"] > 0:
  1800. tmp_font = pygame.font.SysFont("freemonobold",int(p/100*csize))
  1801. rgb =(int(cr*ddim),int(cg*ddim),int(cb*ddim),cdim)
  1802. _sec = count["_SEC"]
  1803. try:
  1804. _sec = int(count["_SEC"])
  1805. except:
  1806. pass
  1807. if type(_sec) is int:
  1808. #print(_sec)
  1809. if _sec <= 60:
  1810. fr = tmp_font.render("{:0}".format(_sec) ,1, rgb)
  1811. else:
  1812. _xx = time.strftime("%M:%S",time.localtime(_sec))
  1813. #print("_xx",_xx)
  1814. fr = tmp_font.render("{}".format(_xx) ,1, rgb)
  1815. else:
  1816. fr = tmp_font.render("{}".format((count["_SEC"])) ,1, rgb)
  1817. fr_r = fr.get_rect(center=(40+cpan-(block[0]),60+ctilt+pm_wy))
  1818. pygame.draw.rect(window,[0,0,0],fr_r)
  1819. window.blit(fr,fr_r)
  1820. class FADE():
  1821. def __init__(self,sec):
  1822. self.start = time.time()
  1823. self.sec = sec
  1824. self.val
  1825. def next(self):
  1826. pass
  1827. def video_overlay_draw(wn=None,lines=["line1","line2"],i=0):
  1828. # overlay
  1829. pygame.draw.rect(wn,[255,200,0],[5,MAIN_SIZE[1]-(i+1)*35,380,28])
  1830. font15 = pygame.font.SysFont("freemonobold",17)
  1831. rgb = [255,100,0]
  1832. pygame.draw.rect(wn,rgb,[5,MAIN_SIZE[1]-(i+1)*35,200,13])
  1833. _line = lines[0]
  1834. fr = font15.render(_line ,1, (0,0,0))
  1835. wn.blit(fr,(10,MAIN_SIZE[1]-(i+1)*35))
  1836. _line = lines[1]
  1837. fr = font15.render(_line ,1, (0,0,0))
  1838. wn.blit(fr,(10,MAIN_SIZE[1]-(i+1)*35+15))
  1839. #_line = lines[2]
  1840. #fr = font15.render(_line ,1, (0,0,0))
  1841. #wn.blit(fr,(75,MAIN_SIZE[1]-(i+1)*35+15))
  1842. def draw_all_video_overlay(VIDEO):
  1843. global videplayer
  1844. i=0
  1845. for count in VIDEO:
  1846. video1 = videoplayer[i]
  1847. #video1.overlay_draw(window,"run") # old
  1848. lines = video1._overlay_text()
  1849. video_overlay_draw(wn=window,lines=lines,i=i)
  1850. i += 1
  1851. def del_all_video():
  1852. global videplayer
  1853. i = 0
  1854. print()
  1855. cprint("del_all_video")
  1856. for video in videoplayer:
  1857. cprint("- del video",video)
  1858. video._del()
  1859. def draw_all_video(VIDEO):
  1860. global videplayer
  1861. i = 0
  1862. # set DMX-VALUE to videoplayer Object
  1863. for count in VIDEO:
  1864. cpan = 0
  1865. ctilt = 0
  1866. cr=255
  1867. cg=255
  1868. cb=255
  1869. csize=10
  1870. cang=0
  1871. cdim=0
  1872. video1 = videoplayer[i]
  1873. k = "VIDEO"
  1874. if k in count:
  1875. #video1.select_video(count[k])
  1876. play_nr = int(count[k]/10)
  1877. if play_nr != video1._media_nr:
  1878. print( "+ + + + + + + + + + +", play_nr , video1._media_nr ,str(video1))
  1879. _vid = video1._id
  1880. _cdmx = video1.dmx
  1881. del_video1 = video1
  1882. video1 = PixelMedia(cdmx,_id=_vid)
  1883. video1.select_video(count[k])
  1884. videoplayer[i] = video1
  1885. del_video1._del()
  1886. del del_video1
  1887. k = "DIM"
  1888. if k in count:
  1889. cdim = int(count[k])
  1890. #video1.dim = cdim
  1891. #if i == 0:
  1892. # print(i,cdim)
  1893. k = "ANG"
  1894. if k in count:
  1895. cang = int(count[k])
  1896. k = "SIZE"
  1897. if k in count:
  1898. csize = int(count[k]/16*p)
  1899. if csize < 5:
  1900. csize = 5
  1901. k = "PAN"
  1902. cpan_max = block[0] *(_x) #+block[0]
  1903. if k in count:
  1904. cpan = int(count[k]) / 255*cpan_max
  1905. cpan = int(cpan)
  1906. k = "TILT"
  1907. ctilt_max = block[1] *(_y) #+block[1]
  1908. if k in count:
  1909. ctilt = int(count[k]) / 255*ctilt_max
  1910. ctilt = int(ctilt)
  1911. k = "_reset"
  1912. if k in count:
  1913. if count[k]:
  1914. count[k] = 0
  1915. video1.restart()
  1916. k = "_RUN"
  1917. if k in count:
  1918. video1._run = count[k]
  1919. #video1.pos
  1920. #video1.x=40+0+cpan
  1921. #video1.y=60+0+pm_wy+ctilt
  1922. x=CFG_IN["x1"]+cpan
  1923. y=CFG_IN["y1"]+ctilt
  1924. scale = int((csize))
  1925. angle = int(360-(cang))
  1926. video1.update_dmx(x,y,scale,angle,cdim)
  1927. if cdim:
  1928. video1.next()
  1929. i += 1
  1930. # draw
  1931. i=0
  1932. for count in VIDEO:
  1933. video1 = videoplayer[i]
  1934. video1.draw(window) #,x=0,y=0)
  1935. i+=1
  1936. def counter_dmx(COUNTER,dataA):
  1937. for count in COUNTER:
  1938. cDMX=count["DMX"]-1
  1939. try:
  1940. count["DIM"] = dataA[cDMX]
  1941. count["PAN"] = dataA[cDMX+1]
  1942. count["TILT"] = dataA[cDMX+2]
  1943. count["CONTROL"] = dataA[cDMX+3]
  1944. count["SIZE"] = dataA[cDMX+4]
  1945. count["SEC"] = dataA[cDMX+5]
  1946. count["RED"] = dataA[cDMX+6]
  1947. count["GREEN"] = dataA[cDMX+7]
  1948. count["BLUE"] = dataA[cDMX+8]
  1949. if count["CONTROL"] >= 10 and count["CONTROL"] < 20: # RESET COUNTER
  1950. count["_RUN"] = 0
  1951. count["_pause"] = 0
  1952. count["_time"] = time.time()
  1953. count["_start"] = int(count["SEC"])
  1954. count["_SEC"] = int(count["_start"])
  1955. if count["CONTROL"] >= 20 and count["CONTROL"] < 30: # PAUSE COUNTER
  1956. count["_RUN"] = 0
  1957. if count["CONTROL"] >= 30 and count["CONTROL"] < 40: # PLAY COUNTER
  1958. count["_RUN"] = 1
  1959. if count["_RUN"]:
  1960. try:
  1961. if count["_pause"] > 0:
  1962. count["_time"] += time.time()-count["_pause"]
  1963. count["_pause"] = 0
  1964. count["_SEC"] = int(count["_start"] - (time.time() - count["_time"]))
  1965. except Exception as e:
  1966. pass
  1967. else:
  1968. if count["_pause"] == 0:
  1969. count["_pause"] = time.time()
  1970. if type(count["_SEC"]) is int:
  1971. if count["_SEC"] < 0:
  1972. count["_SEC"] = 0
  1973. for ti in range(10):
  1974. #print(ti,(ti+6)*10)
  1975. if count["CONTROL"] >= (ti+6)*10 and count["CONTROL"] < (ti+7)*10:
  1976. count["_SEC"] = "----" #text 1
  1977. try:
  1978. count["_SEC"] = TEXT_BLOCK[ti]
  1979. except Exception as e:
  1980. pass
  1981. if count["CONTROL"] >= 250 and count["CONTROL"] < 256:
  1982. count["_SEC"] = "DMX:{}-{}".format(cDMX+1,cDMX+8+1)
  1983. except Exception as e:
  1984. cprint("EXC FUNC",e,count,color="red")
  1985. #print(count)
  1986. def video_dmx(VIDEO,dataA):
  1987. for count in VIDEO:
  1988. cDMX=count["DMX"]-1
  1989. try:
  1990. count["DIM"] = dataA[cDMX]
  1991. count["PAN"] = dataA[cDMX+1]
  1992. count["TILT"] = dataA[cDMX+2]
  1993. count["CONTROL"] = dataA[cDMX+3]
  1994. if count["CONTROL"] >= 10 and count["CONTROL"] < 20:
  1995. count["_reset"] = 1
  1996. if count["CONTROL"] >= 20 and count["CONTROL"] < 30:
  1997. if count["_RUN"] == 1:
  1998. print( "_RUN:0",count["DMX"]-1)
  1999. count["_RUN"] = 0
  2000. if count["CONTROL"] >= 30 and count["CONTROL"] < 40:
  2001. if count["_RUN"] == 0:
  2002. print( "_RUN:1",count["DMX"]-1)
  2003. count["_RUN"] = 1
  2004. count["SIZE"] = dataA[cDMX+4]#*2
  2005. count["SEC"] = dataA[cDMX+5]
  2006. #count["RED"] = dataA[cDMX+6]
  2007. count["VIDEO"] = dataA[cDMX+6]
  2008. #count["GREEN"] = dataA[cDMX+7]
  2009. count["ANG"] = (int(dataA[cDMX+7])/255*360)
  2010. #count["BLUE"] = dataA[cDMX+8]
  2011. except Exception as e:
  2012. cprint("VIDEOPLAYER EXCEPT FUNC",e,count,color="red")
  2013. #print(count)
  2014. def read_dmx_data(ip,ips):
  2015. ip = select_ip(ips,univ=START_UNIV)
  2016. IP = ip
  2017. data = read_dmx(ip)
  2018. ip = select_ip(ips,univ=START_UNIV+1)
  2019. data3 = read_dmx(ip)
  2020. data.extend(data3)
  2021. ip = select_ip(ips,univ=START_UNIV+2)
  2022. data3 = read_dmx(ip)
  2023. data.extend(data3)
  2024. ip = select_ip(ips,univ=START_UNIV+3)
  2025. data3 = read_dmx(ip)
  2026. data.extend(data3)
  2027. if options.dual_vpu:
  2028. ip = select_ip(ips,univ=START_UNIV+4)
  2029. data3 = read_dmx(ip)
  2030. data.extend(data3)
  2031. ip = select_ip(ips,univ=START_UNIV+5)
  2032. data3 = read_dmx(ip)
  2033. data.extend(data3)
  2034. return data
  2035. def set_pos(GRID,_x=0,_y=0):
  2036. for fix in GRID:
  2037. pos = fix.POS(_x,_y)#40,60+pm_wy)
  2038. for subfix in fix.sub_fix:
  2039. #subfix.dmc_calc(data)
  2040. spos = subfix.POS(_x,_y)
  2041. def draw_gobo(GRID,data,_x=0,_y=0):
  2042. i = 0
  2043. dmx = 1
  2044. h = 1
  2045. v = 1
  2046. for fix in GRID:
  2047. rgb = fix.rgb
  2048. pos = fix.POS() #CFG_IN["x1"],CFG_IN["y1"])
  2049. pygame.draw.rect(window,rgb,pos)
  2050. # DRAW SUB-FIXTURE
  2051. j = 0
  2052. for subfix in fix.sub_fix:#calc(data):
  2053. subfix.dmx_calc(data)
  2054. spos = subfix.POS() #_x,_y)#+pm_wy)
  2055. srgb = subfix.rgb
  2056. if FUNC > 10 and FUNC <= 20: # big dot
  2057. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  2058. elif FUNC > 20 and FUNC <= 30:#small dot
  2059. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  2060. elif FUNC > 30 and FUNC <= 40:#donut
  2061. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  2062. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  2063. elif FUNC > 40 and FUNC <= 50: # rec with hole
  2064. pygame.draw.rect(window,srgb,spos)
  2065. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  2066. elif FUNC > 50 and FUNC <= 60: # rec with big hole
  2067. pygame.draw.rect(window,srgb,spos)
  2068. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  2069. elif FUNC > 60 and FUNC <= 70: # rec with donat
  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]/2))
  2072. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  2073. elif FUNC > 70 and FUNC <= 80: # rec boarder
  2074. pygame.draw.rect(window,srgb,[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  2075. elif FUNC > 80 and FUNC <= 90: # rec big boarder
  2076. pygame.draw.rect(window,srgb,[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  2077. elif FUNC > 90 and FUNC <= 100: # rec thin line
  2078. pygame.draw.rect(window,srgb,spos)
  2079. pygame.draw.rect(window,[0,0,0],[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  2080. elif FUNC > 100 and FUNC <= 110: # rec big line
  2081. pygame.draw.rect(window,srgb,spos)
  2082. pygame.draw.rect(window,[0,0,0],[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  2083. elif FUNC > 110 and FUNC <= 120: # rec with dot
  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. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  2087. elif FUNC > 120 and FUNC <= 130: # rec inline
  2088. pygame.draw.rect(window,srgb,[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  2089. pygame.draw.rect(window,[0,0,0],[spos[0]+3,spos[1]+3,spos[2]-6,spos[3]-6])
  2090. elif FUNC > 130 and FUNC <= 140: # 3 dot (heart)
  2091. draw_circle(window,srgb,(spos[0]+int(spos[2]/2)+2,spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  2092. draw_circle(window,srgb,(spos[0]+int(spos[2]/2)-2,spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  2093. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)+2),int(spos[3]/3.5))
  2094. else:
  2095. pygame.draw.rect(window,srgb,spos)
  2096. i += 1
  2097. def grid_label(GRID,_x=0,_y=0):
  2098. for fix in GRID:
  2099. #pos = fix.POS(_x,_y)
  2100. pos = fix.POS() #CFG_IN["x1"],CFG_IN["y1"])
  2101. rgb = fix.rgb
  2102. # draw row/col grid number
  2103. if pos[0] == 0:
  2104. fr = font12.render("{}".format(pos[1]+1) ,1, (200,200,200))
  2105. window.blit(fr,(10,pos[1]+3 ))
  2106. if pos[1] == 0:
  2107. fr = font12.render("{}".format(pos[0]+1) ,1, (200,200,200))
  2108. window.blit(fr,(pos[0]+2,35 ))
  2109. #pygame.draw.rect(window,rgb,pos)
  2110. def grid_sub_label(GRID,_x=0,_y=0):
  2111. h = 1
  2112. v = 1
  2113. for fix in GRID:
  2114. j = 0
  2115. #pos = fix.POS(_x,_y)
  2116. pos = fix.POS() #CFG_IN["x1"],CFG_IN["y1"])
  2117. for subfix in fix.sub_fix:
  2118. subfix.dmx_calc(data)
  2119. spos = subfix.POS() #(_x,_y)
  2120. srgb = subfix.rgb
  2121. # draw row/col grid number
  2122. if subfix.pos[0] == 0:
  2123. fr = font12.render("{}".format(v ) ,1, (200,200,200))
  2124. window.blit(fr,(25,spos[1] ))
  2125. v += 1
  2126. if subfix.pos[1] == 0:
  2127. fr = font12.render("{}".format(1) ,1, (200,200,200))
  2128. fr = font12.render("{}".format(h ) ,1, (200,200,200))
  2129. h+=1
  2130. window.blit(fr,(spos[0],50 ))
  2131. if p >= 40:
  2132. if NR:
  2133. fr = font15.render("{:02}".format(subfix._id) ,1, (250,200,5))
  2134. window.blit(fr,(spos[0]+2,spos[1]+10))
  2135. j += 1
  2136. def frame_area():
  2137. rgb = [255,255,0]
  2138. CFG = CFG_IN
  2139. p1 = CFG_IN["p1"]
  2140. p2 = CFG_IN["p2"]
  2141. draw_frame(window,rgb,p1,p2,offset=2)
  2142. rgb = [255,0,0]
  2143. #draw_frame(window,rgb,p1,p2,offset=4)
  2144. if CFG_OUT["on"]:
  2145. rgb = [255,0,0]
  2146. p1 = CFG_OUT["p1"]
  2147. p2 = CFG_OUT["p2"]
  2148. #pygame.draw.line(window,rgb,p1,p2)
  2149. draw_frame(window,rgb,p1,p2,offset=3)
  2150. if CFG_OUT2["on"]:
  2151. rgb = [255,0,0]
  2152. p1 = CFG_OUT2["p1"]
  2153. p2 = CFG_OUT2["p2"]
  2154. #pygame.draw.line(window,rgb,p1,p2)
  2155. draw_frame(window,rgb,p1,p2,offset=3)
  2156. ips=[]
  2157. dataA=[]
  2158. data=[]
  2159. def dmx_raw():
  2160. global frame
  2161. frame += 1
  2162. global ips,dataA,data
  2163. ips = read_index()
  2164. # ----
  2165. ip = select_ip(ips,univ=1) # univ 1 gobo
  2166. dataA = read_dmx(ip)
  2167. # ----
  2168. data = read_dmx_data(ip,ips)
  2169. if options.countdown:
  2170. counter_dmx(COUNTER,dataA)
  2171. if len(VIDEO) > 0:
  2172. video_dmx(VIDEO,dataA)
  2173. return ips,dataA,data
  2174. def dmx_loop():
  2175. while 1:
  2176. dmx_raw()
  2177. time.sleep(1/40) # fast high cpu
  2178. #time.sleep(1/25)
  2179. if QUIT:
  2180. break
  2181. dmx_raw()
  2182. thread.start_new_thread(dmx_loop,())
  2183. def draw_output_label(CFG,value,dmx="0.0",name="GRID_XX"):
  2184. dim_raw = value
  2185. pygame.draw.rect(window,[5,5,155],[CFG["x1"]-5,CFG["y2"]+4,130,35]) # background
  2186. # GRID_A1_DIM_LABEL
  2187. fr = font15.render("Output:"+name,1, (255,255,255))
  2188. fr_r = fr.get_rect(center=(int(wx/2),int(0+pm_wy-p*0-10)))
  2189. window.blit(fr,(CFG["x1"],CFG["y2"]+5))
  2190. fr = font15.render("inv-dim: "+str(dim_raw) ,1, (255,255,255))
  2191. fr_r = fr.get_rect(center=(int(wx/2),int(0+pm_wy-p*0-10)))
  2192. window.blit(fr,(CFG["x1"]+60,CFG["y2"]+15))
  2193. fr = font15.render("dmx: {}.{}".format(START_UNIV_A,dmx) ,1, (255,255,255))
  2194. window.blit(fr,(CFG["x1"],CFG["y2"]+15))
  2195. def GRID_DIM(dim,x,y):
  2196. try:
  2197. dim = int(grid_dim_v)
  2198. if dim > 255:
  2199. dim = 255
  2200. if dim <=0:
  2201. dim = 0
  2202. except:pass
  2203. dim_raw = dim*-1
  2204. s = pygame.Surface((p*8,p*8)) # the size of your rect
  2205. s.set_alpha(dim) # alpha level
  2206. s.fill((0,0,0)) # this fills the entire surface
  2207. window.blit(s, (x,y))
  2208. def GRID_A1_DIM():
  2209. # GRID_A1 DIM
  2210. if options.grid_a1_idim:
  2211. CFG=CFG_OUT
  2212. grid_dim_ch = int(options.grid_a1_idim)
  2213. grid_dim_v = dataA[grid_dim_ch-1]
  2214. dim=255
  2215. dim=grid_dim_v
  2216. #dim=120
  2217. GRID_DIM(dim,x=CFG["x1"],y=CFG["y2"]-p*8)
  2218. a=options.grid_a1_idim
  2219. draw_output_label(CFG,dim,a,name="GRID_A1_DIM")
  2220. def GRID_A2_DIM():
  2221. # GRID_A2 DIM
  2222. if options.grid_a2_idim:
  2223. CFG=CFG_OUT2
  2224. grid_dim_ch = int(options.grid_a2_idim)
  2225. grid_dim_v = dataA[grid_dim_ch-1]
  2226. dim=255
  2227. try:
  2228. dim = int(grid_dim_v)
  2229. if dim > 255:
  2230. dim = 255
  2231. if dim <=0:
  2232. dim = 0
  2233. except:pass
  2234. dim_raw = dim*-1
  2235. s = pygame.Surface((p*8,p*8)) # the size of your rect
  2236. s.set_alpha(dim) # alpha level
  2237. s.fill((0,0,0)) # this fills the entire surface
  2238. window.blit(s, (CFG["x1"],CFG["y2"]-p*8))
  2239. a=options.grid_a2_idim
  2240. draw_output_label(CFG,dim_raw,a,name="GRID_A2_DIM")
  2241. def cvImageToSurface(cvImage):
  2242. if cvImage.dtype.name == 'uint16':
  2243. cvImage = (cvImage / 256).astype('uint8')
  2244. size = cvImage.shape[1::-1]
  2245. if len(cvImage.shape) == 2:
  2246. cvImage = np.repeat(cvImage.reshape(size[1], size[0], 1), 3, axis = 2)
  2247. format = 'RGB'
  2248. else:
  2249. format = 'RGBA' if cvImage.shape[2] == 4 else 'RGB'
  2250. cvImage[:, :, [0, 2]] = cvImage[:, :, [2, 0]]
  2251. surface = pygame.image.frombuffer(cvImage.flatten(), size, format)
  2252. return surface.convert_alpha() if format == 'RGBA' else surface.convert()
  2253. def create_img_meta():
  2254. out = {}
  2255. #40 235
  2256. out["pos"] = (0,0) # x,y
  2257. out["ipos"] = (0,0) # x,y
  2258. out["scale"] = 50
  2259. out["dim"] = 100
  2260. out["ang"] = 0
  2261. out["_fpath"] = '/opt/LibreLight/Xdesk/icon/'
  2262. out["fname"] = "picker.png"
  2263. out["raw_img"] = ""
  2264. out["img"] = ""
  2265. out["err"] = []
  2266. return out
  2267. def img_open(img_meta):
  2268. cprint("img_open",img_meta["fname"],"-------")
  2269. fname = img_meta["_fpath"] +"/"+ img_meta["fname"] #'/opt/LibreLight/Xdesk/icon/picker.png'
  2270. im2 = cv2.imread(fname)
  2271. width = int(im2.shape[1])
  2272. height = int(im2.shape[0])
  2273. w_max = 480
  2274. h_max = 320
  2275. #w_max = 600
  2276. #h_max = 400
  2277. if width > w_max:
  2278. h = int(height*(w_max/width))
  2279. w = w_max
  2280. im2 = cv2.resize(im2, (w,h), interpolation =cv2.INTER_LINEAR)
  2281. if height > h_max:
  2282. w = int(width*(h_max/height))
  2283. h = h_max
  2284. if w < w_max:
  2285. im2 = cv2.resize(im2, (w,h), interpolation =cv2.INTER_LINEAR)
  2286. img_meta["raw_img"] = im2
  2287. img_meta["img"] = im2
  2288. return im2
  2289. def img_jitter(img_meta):
  2290. r1=random.randint(0,3)
  2291. r2=random.randint(0,3)
  2292. img_meta["ipos"] = [r1,r2]
  2293. def img_rescale_width(img_meta, _width):
  2294. frame = img_meta["raw_img"]
  2295. width = int(frame.shape[1])
  2296. height = int(frame.shape[0])
  2297. dim = (width, height)
  2298. if 1: #width > _width:
  2299. h = int(height*( width/_width))
  2300. dim = (_width, h)
  2301. im = cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
  2302. else:
  2303. im = cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
  2304. return im
  2305. def img_rescale(img, percent=75):
  2306. frame = img
  2307. #frame = img_meta["raw_img"]
  2308. width = int(frame.shape[1] * percent/ 100)
  2309. height = int(frame.shape[0] * percent/ 100)
  2310. dim = (width, height)
  2311. return cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
  2312. def img_draw_raw(img_meta):
  2313. #img_jitter(img_meta)
  2314. pos = img_meta["pos"]
  2315. ipos = img_meta["ipos"]
  2316. im2 = img_meta["img"]
  2317. dim = img_meta["dim"]
  2318. im2 = cvImageToSurface(im2)
  2319. w2 = int(im2.get_width()/2)
  2320. h2 = int(im2.get_height()/2)
  2321. x=pos[0]+ipos[0]
  2322. y=pos[1]+ipos[1]
  2323. window.blit(im2, (x-w2,y-h2))
  2324. img_meta_a = create_img_meta()
  2325. img_open(img_meta_a)
  2326. t1 = Timer(143)
  2327. time.sleep(0.33)
  2328. t2 = Timer(11)
  2329. count_tilt = 0
  2330. def main():
  2331. global IP
  2332. global GRID
  2333. global GRID_A
  2334. global FUNC
  2335. global count_tilt
  2336. global TEXT_BLOCK
  2337. global TEXT_BLOCK_TIME
  2338. global MEDIALIST
  2339. global MEDIALIST_TIME
  2340. global dataA
  2341. global frame2
  2342. global runnung
  2343. reload_grid()
  2344. s=time.time()
  2345. r = ""
  2346. IP = "xx"
  2347. while running:
  2348. if TEXT_BLOCK_TIME+5 < time.time():
  2349. TEXT_BLOCK = open_text_block()
  2350. TEXT_BLOCK_TIME = time.time()
  2351. if MEDIALIST_TIME+6 < time.time():
  2352. MEDIALIST = open_medialist()
  2353. MEDIALIST_TIME = time.time()
  2354. pygame.display.flip()
  2355. set_pos(GRID,_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2356. #set_pos(GRID,600,600)#_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2357. event()
  2358. window.fill((10,0,30))
  2359. calc_fps()
  2360. calc_fps2()
  2361. #draw_overlay()
  2362. # GRID loop
  2363. try:
  2364. FUNC = dataA[gobo_ch-1]
  2365. FUNC2 = FUNC
  2366. except Exception as e:
  2367. print("EXC FUNC",e)
  2368. #if int(time.time()) % 2: # BLINK
  2369. #set_gobo_pos(GRID,_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2370. draw_gobo(GRID,data) #,_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2371. #if int(time.time()) % 2: # BLINK
  2372. grid_label(GRID,_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2373. #grid_sub_label(GRID) #,_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2374. if VIDEO:
  2375. draw_all_video(VIDEO)
  2376. #draw_all_video_overlay(VIDEO)
  2377. #img_draw(img_meta_a)
  2378. if options.countdown:
  2379. draw_counter(COUNTER)
  2380. # output background mask
  2381. pygame.draw.rect(window,[0,0,0],[35,57,MAIN_SIZE[0]+200-35,135]) # background
  2382. #pygame.draw.rect(window,[5,5,5],[0,60,MAIN_SIZE[0],p*9]) # background
  2383. pointer.draw(0,pm_wy) #wy
  2384. spos = [0,0,0,0]
  2385. if PIXEL_MAPPING >= 1:
  2386. #try:
  2387. if options.dual_vpu:
  2388. GRID_X = GRID_A[8*8:]
  2389. xx = p*8
  2390. #reshape(GRID,GRID_X,CFG_OUT2["x1"],CFG_OUT2["y1"]-p*8,name="GRID_A2") #start pos
  2391. #except Exception as e:
  2392. # print("Exception 23123",e)
  2393. reshape2(GRID,GRID_A,CFG_OUT["x1"],CFG_OUT["y1"],name="GRID_A1") #start pos
  2394. #reshape(GRID,GRID_A,CFG_OUT["x1"],CFG_OUT["y1"],name="GRID_A1") #start pos
  2395. else:
  2396. pass #reshape(GRID,GRID_A,spos[0]+spos[2]+20,10) #start pos
  2397. frame_area()
  2398. # OUTPUT -> MAPING
  2399. draw_kachel_nr()
  2400. input_mask() # mask background
  2401. output_mask() # mask background
  2402. GRID_A1_DIM() # overlay
  2403. GRID_A2_DIM() # overlay
  2404. if VIDEO:
  2405. draw_all_video_overlay(VIDEO)
  2406. draw_main_overlay()
  2407. pointer.draw(0,pm_wy) #wy
  2408. pygame.display.flip()
  2409. clock.tick(25)
  2410. if 'SDL_VIDEO_WINDOW_POS' in os.environ:
  2411. del os.environ['SDL_VIDEO_WINDOW_POS'] #= '%i,%i' % (200,164)
  2412. frame2 += 1
  2413. if __name__ == "__main__":
  2414. try:
  2415. main()
  2416. finally:
  2417. del_all_video()