fix.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. #!/usr/bin/python3
  2. import time
  3. boot = time.time()
  4. import random
  5. import os
  6. import sys
  7. #sys.path.insert(0,os.path.realpath(os.getcwd() + '/..'))
  8. sys.path.insert(0,"/opt/LibreLight/Xdesk/")
  9. print(sys.path)
  10. print()
  11. import pathlib
  12. _file_path=pathlib.Path(__file__)
  13. print("file:",_file_path)
  14. import tool.movewin as movewin
  15. pids = movewin.search_process(_file_path)
  16. CAPTION = 'LibreLight SDL-FIXTURE '
  17. if len(pids) >= 2:
  18. search = CAPTION[:]
  19. _ids = movewin.winfo(search)
  20. for _id in _ids:
  21. c3 = movewin.activate(_id)
  22. os.system(c3)
  23. sys.exit()
  24. # ===== GUI =========
  25. import pygame
  26. import pygame.gfxdraw
  27. import pygame.font
  28. pg = pygame
  29. main_size=(850,460)
  30. window = pygame.display.set_mode(main_size,pg.RESIZABLE,32)
  31. pg = pygame
  32. pygame.init()
  33. pygame.mixer.quit()
  34. clock = pygame.time.Clock()
  35. icon = pygame.image.load('icon/scribble.png')
  36. pygame.display.set_icon(icon)
  37. import tool.movewin as movewin
  38. import tool.sdl_elm as sdl_elm
  39. #CAPTION = 'LibreLight DMX '
  40. CAPTION += ':{}'.format(random.randint(100,999))
  41. import tool.git as git
  42. CAPTION += git.get_all()
  43. _id = movewin.winfo(CAPTION)
  44. c1 = movewin.movewin(_id,main_size[0],main_size[1]) #800,500)
  45. os.system(c1)
  46. c1 = movewin.activate(_id)
  47. os.system(c1)
  48. pg.display.set_caption(CAPTION)
  49. font0 = pygame.font.SysFont("freesans",10)
  50. font0b = pygame.font.SysFont("freesansbold",10)
  51. font = pygame.font.SysFont("freemonobold",22)
  52. font10 = pygame.font.SysFont("freemonobold",10)
  53. font12 = pygame.font.SysFont("freemonobold",12)
  54. font15 = pygame.font.SysFont("freemonobold",15)
  55. font16 = pygame.font.SysFont("freemonobold",16)
  56. font22 = pygame.font.SysFont("FreeSans",22)
  57. #font = pygame.font.SysFont(None,30)
  58. fr = font.render("hallo" ,1, (200,0,255))
  59. mouse_down = 0
  60. mouse_pos1 = [0,0]
  61. mouse_pos2 = [0,0]
  62. mouse_grab = []
  63. print(int((time.time()-boot)*10),"loop...")
  64. fps_t = time.time()
  65. fps = 0
  66. fps_old = 0
  67. import memcache
  68. mc = memcache.Client(['127.0.0.1:11211'], debug=0)
  69. mc.set("some_key", "Some value")
  70. value = mc.get("some_key")
  71. mc.set("another_key", 3)
  72. mc.delete("another_key")
  73. import time
  74. import json
  75. data = {}
  76. start = time.time()
  77. delta = start
  78. #for i in dir(mc):
  79. # print(i)#,[i.__doc__])
  80. # print()
  81. #for i in mc.get_stats():
  82. # print("keys",i)
  83. #fps_btn = []
  84. #fps_btn_press = []
  85. #i += 1
  86. #bx = sdl_elm.Button(window,pos=[30,r,190,60])
  87. #bx.text = "FIX:{}\n<val>\nx".format(i+1)
  88. #bx.font0 = pygame.font.SysFont("freesans-bold",20)
  89. #bx.btn1.type = "flash"
  90. #fps_btn.append(bx)
  91. #r+=bx.get_rect()[3]
  92. def read_dmx():
  93. dmx = {} #univ
  94. iii = 0
  95. y=mc.get("index")
  96. key=y.keys()
  97. key = list(key)
  98. key.sort()
  99. for k in key:
  100. if k.startswith("ltp-out"):
  101. #v = y[k]
  102. u = k.split(":")[-1]
  103. x=mc.get(k)
  104. dmx[u] = x
  105. return dmx
  106. def read_fix(dmx):
  107. y=mc.get("fix")#cmd)
  108. key=y.keys()
  109. key = list(key)
  110. key.sort()
  111. for k in key:#y.items():
  112. v = y[k]
  113. #print(k,v)
  114. x=mc.get(k)
  115. dmx_start = 0
  116. if "DMX" in v:
  117. dmx_start = v["DMX"]
  118. univ_start = 0
  119. if "UNIVERS" in v:
  120. univ_start = v["UNIVERS"]
  121. if "ATTRIBUT" in v: # and 10:
  122. ATTR = v["ATTRIBUT"]
  123. for k2 in ATTR:
  124. k2_ATTR = ATTR[k2]
  125. #print(ATTR) #[k2_ATTR]) #["VALUE2"] = -2
  126. k2_ATTR["VALUE2"] = -2
  127. #if k2.endswith("-FINE"):
  128. # continue
  129. if k2.startswith("_"):
  130. continue
  131. k3 = k+"-"+k2
  132. dmx_nr = 0
  133. if "NR" in k2_ATTR:
  134. if k2_ATTR["NR"] >= 1:
  135. dmx_nr = k2_ATTR["NR"]+1
  136. val2 = ""
  137. if "VALUE" in k2_ATTR:
  138. val2 = k2_ATTR["VALUE"]
  139. dmx_val=-1
  140. dmx_x=-1
  141. if dmx_nr > 0 and dmx_start > 0:
  142. try:
  143. dmx_x = dmx_start-1+dmx_nr-1
  144. dmx_val = dmx[str(univ_start)][dmx_x-1]
  145. except:pass
  146. if type(dmx_val) in [int,float]:
  147. k2_ATTR["VALUE2"] = dmx_val
  148. else:
  149. k2_ATTR["VALUE2"] = 0
  150. return y
  151. def add_dmx(data,dmx):
  152. pass
  153. table={}
  154. table_grid={}
  155. btn1_press = [] #["10.10.10.13:0"]
  156. y=[]
  157. bx_font0 = pygame.font.SysFont("freesans-bold",20)
  158. while 1:
  159. fps +=1
  160. t = time.time()
  161. if t-fps_t >= 1:
  162. #print("FPS:",fps)
  163. fps_old = fps
  164. fps=0
  165. fps_t =t
  166. pygame.display.flip()
  167. pos = [160,10,70,60]
  168. rgb = (0xdd,0xdd,0xdd,0)
  169. rgb = (0xaa,0xaa,0xaa,0)
  170. window.fill((5,5,5))
  171. pygame.draw.rect(window,(0,0,0),[0,0,main_size[0],main_size[1]])
  172. fr = font22.render("FIXTURE DATA (READONLY!)" ,1, (200,200,200))
  173. window.blit(fr,(20,10 ))
  174. fr = font22.render("DEMO / TEST - MODE ! " ,1, (200,200,200))
  175. #window.blit(fr,(10,30 ))
  176. pos = [160,110,70+80,20]
  177. #pygame.draw.rect(window,rgb,pos)
  178. t=(time.time()-start)
  179. if t > 15:
  180. start = time.time()
  181. b= 80-int(t*10)
  182. pos = [160,110,70+(b),20]
  183. rgb = (0x00,0xff,0xff,0)
  184. dmx = read_dmx()
  185. rgb = (0xff,0,0xaa,0)
  186. data = read_fix(dmx)
  187. #data = add_dmx(data,dmx)
  188. i = 0
  189. r=40
  190. if 1:
  191. ch = 141
  192. send = 0
  193. #cmd="stats items"
  194. #if not y:
  195. y=data #mc.get("fix")#cmd)
  196. if y:
  197. iii = 0
  198. key=y.keys()
  199. key = list(key)
  200. key2 = []
  201. for k in key:
  202. try:
  203. key2.append(int(k))
  204. except:
  205. pass
  206. key2.sort()
  207. key = key2 #.sort()
  208. if len(btn1_press) == 0:
  209. btn1_press = [key[0]]
  210. rgb = (0x00,0,0xff,0)
  211. k2 = btn1_press[-1]
  212. fr = font22.render("SRC:"+str(k2) ,1, rgb) #(200,200,200))
  213. window.blit(fr,(400,1))
  214. fr = font22.render("FPS:"+str(fps_old) ,1, rgb) #(200,200,200))
  215. window.blit(fr,(600,1))
  216. for k in key:#y.items():
  217. k = str(k)
  218. v = y[k]
  219. attr_count = 0
  220. if "ATTRIBUT" in v:
  221. for ATTR in v["ATTRIBUT"]:
  222. if ATTR.startswith("_"):
  223. continue
  224. if ATTR.endswith("-FINE"):
  225. continue
  226. if ATTR == "DIM":
  227. continue
  228. attr_count += 1
  229. if attr_count <= 0:
  230. continue
  231. #print(k,v)
  232. x=mc.get(k)
  233. cccount = 0
  234. txt = str([k,v,ch,"=",cccount]) #x[ch-1]])
  235. rgb = (0xaa,0xaa,0xaa,0)
  236. i += 1
  237. if k not in table:
  238. bx = sdl_elm.Button(window,pos=[20,r,50,20])
  239. bx.btn1.color_on = [255,255,0]
  240. table[k] = bx
  241. bxc = sdl_elm.Button(window,pos=[-11,r,5,20])
  242. bxc.btn1.color_on = [255,255,0]
  243. table[k+"_color"] = bxc
  244. bx = table[k]
  245. bx.data = v
  246. bxc.data = {}
  247. active = 0
  248. if "ATTRIBUT" in v:
  249. if "_ACTIVE" in v["ATTRIBUT"]:
  250. if "ACTIVE" in v["ATTRIBUT"]["_ACTIVE"]:
  251. if v["ATTRIBUT"]["_ACTIVE"]["ACTIVE"] >=1:
  252. active = 1
  253. bx.btn1.val.set(active)
  254. bx.text = "ID:"+ k #str(txt) #+"\n<val>\n" #.format(i+1)
  255. bx.font0 = bx_font0 #pygame.font.SysFont("freesans-bold",20)
  256. bx.btn1.bg_on = [0,255,255]
  257. bx.btn1.type = "toggle"
  258. bx.pos = [10,r,70,20]
  259. if "ATTRIBUT" in v:
  260. bcv_r = 0
  261. bcv_g = 0
  262. bcv_b = 0
  263. if "RED" in v["ATTRIBUT"]:
  264. bcv_r = v["ATTRIBUT"]["RED"]["VALUE2"]
  265. if "GREEN" in v["ATTRIBUT"]:
  266. bcv_g = v["ATTRIBUT"]["GREEN"]["VALUE2"]
  267. if "BLUE" in v["ATTRIBUT"]:
  268. bcv_b = v["ATTRIBUT"]["BLUE"]["VALUE2"]
  269. print( [bcv_r,bcv_g,bcv_b])
  270. if bcv_r > 255:
  271. bcv_r=255
  272. if bcv_g > 255:
  273. bcv_g=255
  274. if bcv_b > 255:
  275. bcv_b=255
  276. bxc.btn1.color = [bcv_r,bcv_g,bcv_b]
  277. bxc.btn1.color_on = [bcv_r,bcv_g,bcv_b]
  278. bxc.pos = [85,r,20,20]
  279. bxc.text = ""
  280. bx.draw()
  281. bxc.draw()
  282. r_buf=bx.get_rect()[3]+2
  283. iii += 35
  284. rr = 0
  285. if "ATTRIBUT" in v: # and 10:
  286. ATTR = v["ATTRIBUT"]
  287. for k2 in ATTR:
  288. k2_ATTR = ATTR[k2]
  289. if k2.endswith("-FINE"):
  290. continue
  291. if k2.startswith("_"):
  292. continue
  293. k3 = k+"-"+k2
  294. val2 = ""
  295. if "VALUE" in k2_ATTR:
  296. val2 = k2_ATTR["VALUE"]
  297. virt = 1
  298. if "NR" in k2_ATTR:
  299. if k2_ATTR["NR"] > 0:
  300. virt = 0
  301. dmx_val = -3
  302. if "VALUE2"in k2_ATTR:
  303. dmx_val = k2_ATTR["VALUE2"]
  304. if k3 not in table_grid:
  305. bx = sdl_elm.Button(window,pos=[300,rr,60,20])
  306. bx.btn1.color_on = [255,255,0]
  307. table_grid[k3] = bx
  308. if "ACTIVE" in k2_ATTR:
  309. if k2_ATTR["ACTIVE"] >=1:
  310. table_grid[k3].btn1.val.set(1)
  311. else:
  312. table_grid[k3].btn1.val.set(0)
  313. bx = table_grid[k3]
  314. bx.data = k2_ATTR
  315. try:val = v
  316. except:pass
  317. bx.text = k2 +" "+str(val2)+" "+str(dmx_val)
  318. bx.font0 = bx_font0
  319. if k2 == "RED":
  320. bx.btn4.color_on = [255,0,0]
  321. elif k2 == "GREEN":
  322. bx.btn4.color_on = [0,255,0]
  323. elif k2 == "BLUE":
  324. bx.btn4.color_on = [0,0,255]
  325. if virt == 1:
  326. bx.btn3.color= [0,0,0]
  327. bx.btn1.type = "toggle"
  328. if type(dmx_val) == int:
  329. bx.btn4.val.set(dmx_val) # "toggle"
  330. bx.pos = [100+rr,r,120,20]
  331. bx.draw()
  332. rr+=bx.get_rect()[2]+2
  333. if rr > 1000:
  334. break
  335. r += r_buf
  336. if r > 800:
  337. break
  338. if 0:#kill:
  339. #time.sleep(.13)
  340. last_k = ""
  341. for k in table:
  342. t = table[k]
  343. if t.btn1.val.get():
  344. if k not in btn1_press:
  345. btn1_press.append(k)
  346. #print(btn1_press)
  347. if len(btn1_press) > 1:
  348. for k in table:
  349. table[k].btn1.val.set(0)
  350. k = btn1_press[-1]
  351. if k in table:
  352. btn1_press = [k]
  353. try:
  354. k = btn1_press[-1]
  355. table[k].btn1.val.set(1)
  356. except:pass
  357. resize_changed = 0
  358. for event in pygame.event.get():
  359. if "scancode" in event.dict:
  360. if event.scancode == 9:
  361. for k in table:
  362. t = table[k]
  363. #t.btn2.clean()
  364. t.btn1.clean()
  365. for k in table_grid:
  366. t = table_grid[k]
  367. #t.btn2.clean()
  368. t.btn1.clean()
  369. print("event",event)
  370. if event.type == pygame.QUIT:
  371. pygame.quit()
  372. sys.exit(0)
  373. elif event.type == pygame.VIDEORESIZE:
  374. scrsize = event.size
  375. width = event.w
  376. hight = event.h
  377. resize_changed = True
  378. for t in table:
  379. #print(t)
  380. table[t].event(event)
  381. if table[t].btn3.get():
  382. data = table[t].data
  383. print("FIX:",data)
  384. for t in table_grid:
  385. #print(t)
  386. table_grid[t].event(event)
  387. if table_grid[t].btn3.get():
  388. data = table_grid[t].data
  389. print(" ATTR:",data)
  390. if "pos" in event.dict:
  391. if "button" in event.dict:
  392. if event.type == 5:#press
  393. mouse_down = 1
  394. mouse_pos1 = [event.pos[0],event.pos[1]]
  395. if event.type == 6:#release
  396. mouse_down = 0
  397. for btn in mouse_grab:
  398. #btn.btn2.val.set(1)
  399. btn.btn1.val.set(1)
  400. mouse_grab = []
  401. mouse_pos2 = [event.pos[0],event.pos[1]]
  402. if mouse_down:
  403. d1 = mouse_pos1[0]-mouse_pos2[0]
  404. d2 = mouse_pos1[1]-mouse_pos2[1]
  405. pix = 23
  406. #print(d1,d2)
  407. if ( d1 > pix or d1 < -pix) or ( d2 >pix or d2 < -pix):
  408. sdl_elm.draw_mouse_box(window,mouse_pos1,mouse_pos2)
  409. for k in table:
  410. t = table[k]
  411. pos = t.get_rect()
  412. mpos = [mouse_pos1[0],mouse_pos1[1],mouse_pos2[0],mouse_pos2[1]]
  413. if sdl_elm.check_area2(pos,mpos):
  414. t._set_mouse_focus(1)
  415. mouse_grab.append(t)
  416. else:
  417. t._set_mouse_focus(0)
  418. for k3 in table_grid:
  419. t = table_grid[k3]
  420. pos = t.get_rect()
  421. mpos = [mouse_pos1[0],mouse_pos1[1],mouse_pos2[0],mouse_pos2[1]]
  422. if sdl_elm.check_area2(pos,mpos):
  423. t._set_mouse_focus(1)
  424. mouse_grab.append(t)
  425. else:
  426. t._set_mouse_focus(0)
  427. if resize_changed:# = True
  428. screen = pygame.display.set_mode(scrsize,pg.RESIZABLE)
  429. clock.tick(10)