_console.py 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384
  1. #! /usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. This file is part of LibreLight.
  5. LibreLight is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, version 2 of the License.
  8. LibreLight is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with LibreLight. If not, see <http://www.gnu.org/licenses/>.
  14. (c) 2012 micha@librelight.de
  15. """
  16. import sys
  17. import time
  18. import json
  19. import zlib
  20. rnd_id = ""
  21. rnd_id += " Beta 22.02 "
  22. import subprocess
  23. import tool.git as git
  24. rnd_id += git.get_all()
  25. import lib.binfs as binfs
  26. binfs.fname="/tmp/vmaster.bin"
  27. #binfs.data =[0]*1024
  28. #binfs.patch(self._v_master_id,vm)
  29. binfs.debug = 0
  30. binfs.loop()
  31. if "__file__" in dir():
  32. sys.stdout.write("\x1b]2;"+str(__file__)+" "+rnd_id+"\x07") # terminal title
  33. else:
  34. sys.stdout.write("\x1b]2;"+str("__file__")+" "+rnd_if+"\x07") # terminal title
  35. __run_main = 0
  36. if __name__ == "__main__":
  37. __run_main = 1
  38. else:
  39. import __main__
  40. print(dir())
  41. if "unittest" not in dir(__main__):
  42. __run_main = 1
  43. import time
  44. import socket
  45. import struct
  46. import sys
  47. import random
  48. import math
  49. from collections import OrderedDict
  50. import lib.zchat as chat
  51. import lib.ArtNetNode as ANN
  52. import _thread as thread
  53. #idmx = [0]*512 # incremental dmx
  54. dmx = [0]*512 # absolute dmx data
  55. gcolor = 1
  56. def cprint(*text,color="blue",space=" ",end="\n"):
  57. color = color.lower()
  58. #return 0 #disable print dbg
  59. if not gcolor:
  60. print(text)
  61. return 0
  62. if color == "green":
  63. txt = '\033[92m'
  64. elif color == "red":
  65. txt = '\033[0;31m\033[1m'
  66. elif color == "yellow":
  67. txt = '\033[93m\033[1m'
  68. elif color == "cyan":
  69. txt = '\033[96m'
  70. else:
  71. txt = '\033[94m'
  72. for t in text:
  73. txt += str(t ) +" "
  74. #HEADER = '\033[95m'
  75. #OKBLUE = '\033[94m'
  76. #OKCYAN = '\033[96m'
  77. #OKGREEN = '\033[92m'
  78. #WARNING = '\033[93m'
  79. #FAIL = '\033[91m'
  80. #ENDC = '\033[0m'
  81. #BOLD = '\033[1m'
  82. #UNDERLINE = '\033[4m'
  83. txt += '\033[0m'
  84. print(txt,end=end)
  85. #return txt
  86. def artnet_loop():
  87. #artnet = ANN.ArtNetNode(to="127.0.0.1",port=6555,univ=12)
  88. #artnet = ANN.ArtNetNode(to="127.0.0.1",port=6555,univ=0)
  89. artnet = ANN.ArtNetNode(to="10.10.10.255",univ=0)
  90. #artnet = ANN.ArtNetNode(to="2.0.0.255",univ=0)
  91. #artnet = ANN.ArtNetNode(to="10.10.10.255",univ=1)
  92. #dmx[205] = 255 #205 BLUE
  93. artnet.dmx= dmx #[0]*512
  94. artnet.send()
  95. while 1:
  96. #artnet._test_frame()
  97. artnet.next()
  98. time.sleep(0.001)
  99. class CLOCK():
  100. def __init__(self):
  101. self.__time = 0
  102. self.__start = time.time() # only for debugging
  103. self.__tick = 0.01 # incremental timer drift's on highe cpu load ?
  104. def time(self):
  105. return self.__time
  106. def get_drift(self):
  107. run_time = time.time() - self.__start
  108. tick_time = self.__time # * self.__tick
  109. print( "runtime:{:0.2f} tick_timer:{:0.2f} drift:{:0.2f}".format(run_time,tick_time,run_time-tick_time))
  110. def loop(self):
  111. while 1:
  112. self.__time +=self.__tick
  113. #if int(self.__time*100)/10. % 10 == 0:# self.__time % 2 == 0:
  114. # print( self.get_drift())
  115. #print(self.__time)
  116. #for i in range(10):
  117. time.sleep(self.__tick)
  118. class CLOCK_REAL():
  119. def __init__(self):
  120. self.__time = 0
  121. self.__start = time.time() # only for debugging
  122. self.__tick = 0.001 # incremental timer drift's on highe cpu load ?
  123. def time(self):
  124. self.__time = time.time()
  125. return self.__time
  126. def get_drift(self):
  127. run_time = time.time() - self.__start
  128. tick_time = self.__time # * self.__tick
  129. print( "runtime:{:0.2f} tick_timer:{:0.2f} drift:{:0.2f}".format(run_time,tick_time,run_time-tick_time))
  130. def loop(self):
  131. pass
  132. #clock = CLOCK()
  133. clock = CLOCK_REAL()
  134. if __run_main:
  135. thread.start_new_thread(clock.loop,())
  136. class Fade():
  137. def __init__(self,start,target,ftime,clock,delay=0):
  138. #print("init Fade ",start,target,ftime,clock)
  139. if delay < 0:
  140. delay = 0.0001
  141. if ftime <= 0:
  142. ftime = 0.0001
  143. clock += delay
  144. self.__delay = delay
  145. self.__clock = clock
  146. self.__clock_curr = clock
  147. self.__ftime = ftime
  148. self.__start = start
  149. self.__last = start
  150. self.__target = target
  151. self.abs = 0
  152. self.run = 1
  153. self.end = 0
  154. self.off = 0
  155. #print("INIT", str(self) )
  156. def __str__(self):
  157. return self.__repr__()
  158. def __repr__(self):
  159. return "<FADE Next:{:0.2f} from:{:0.2f} to:{:0.2f} ft:{:0.2f} Clock:{:0.2f} run:{} delay:{:0.2f}>".format(
  160. self.__last, self.__start,self.__target,self.__ftime,self.__clock_curr,self.run,self.__delay )
  161. def next(self,clock=None):
  162. if self.__ftime <= 0 and self.__delay <= 0:
  163. self.__last = self.__target
  164. self.end = 1
  165. self.run = 0
  166. if type(clock) is float or type(clock) is int:#not None:
  167. self.__clock_curr = clock
  168. if self.__target > self.__start:
  169. if self.__last >= self.__target:
  170. self.run = 0
  171. self.end = 1
  172. return self.__target
  173. else:
  174. if self.__last <= self.__target:
  175. self.run = 0
  176. self.end = 1
  177. return self.__target
  178. current = (self.__clock - self.__clock_curr) / self.__ftime
  179. length = self.__start - self.__target
  180. self.__last = self.__start+ length*current
  181. self.run = 1
  182. return self.__last
  183. def ctl(self,cmd="",value=None): # if x-fade cmd="%" value=50
  184. pass
  185. class _MASTER():
  186. def __init__(self,name="None"):
  187. self.__data = {}
  188. self.name = name
  189. def val(self,name,value=None):
  190. _value = 100 #%
  191. name = str(name)
  192. if name not in self.__data:
  193. self.__data[name] = 100
  194. _value = self.__data[name]
  195. if value is not None:
  196. if _value != value:
  197. print(self.name,"CHANGE MASTER:",name,"from:",_value,"to:",value)
  198. self.__data[name] = value
  199. _value = self.__data[name]
  200. return _value /100.
  201. exec_size_master = _MASTER("EXEC-SIZE")
  202. exec_speed_master = _MASTER("EXEC-SPEED")
  203. exec_offset_master = _MASTER("EXEC-OFFSET")
  204. size_master = _MASTER("SIZE")
  205. speed_master = _MASTER("SPEED")
  206. exe_master = []
  207. exe_master.append({"SIZE":100,"SPEED":100,"id":12,"link-ids":[2]})
  208. class MASTER_FX():
  209. def __init__(self):
  210. self.__data = []
  211. self.__ok = []
  212. self.i=0
  213. self.old_offsets = []
  214. self.offsets = []
  215. self.count = -1
  216. self.init = 10
  217. def add(self,fx):
  218. if fx not in self.__data:
  219. self.__data.append(fx)
  220. info = fx._get_info()
  221. offset = 0
  222. if "offset" in info:
  223. offset = info["offset"]
  224. self.old_offsets.append(offset)
  225. self.offsets.append(offset)
  226. if "xtype" in info:
  227. if info["xtype"] == "rnd":
  228. self._shuffle()
  229. def _shuffle(self):
  230. random.seed(1000)
  231. random.shuffle(self.old_offsets)
  232. def _init(self):
  233. self._shuffle()
  234. for i,_ in enumerate(self.old_offsets):
  235. offset = self.old_offsets[i]
  236. self.offsets[i] = offset
  237. self.init = 0
  238. def next(self,child):
  239. i = self.__data.index(child)
  240. offset = self.old_offsets[i]
  241. self.offsets[i] = offset
  242. return offset
  243. def get(self,child,count):
  244. offset = 0
  245. if child not in self.__data:
  246. return offset
  247. if self.init:
  248. self._init()
  249. idx = self.__data.index(child)
  250. if (self.count != count and idx == 0 ) or self.init == 0:
  251. self.init = 1
  252. self._shuffle()
  253. self.count=count
  254. idx = self.__data.index(child)
  255. offset = self.offsets[idx]
  256. return offset
  257. class FX():
  258. def __init__(self,xtype="sinus",size=10,speed=10,invert=0,width=100,start=0,offset=0,base="",clock=0,master=None,master_id=0):
  259. self.__xtype=xtype
  260. self.__size = size
  261. self.__start = start
  262. self.__master_id = master_id
  263. if width > 200:
  264. width = 200
  265. if width <= 0:
  266. width = 1
  267. self.__fade_in_master = 0
  268. self.__width = width
  269. self.__invert = invert
  270. self.__base = base
  271. self.__speed = speed
  272. self.__offset = offset
  273. self.__clock = clock
  274. self.__clock_curr = clock
  275. self.__clock_delta = 0
  276. self.__clock_old = self.__clock_curr
  277. self.out = 0
  278. self.old_v = -1
  279. self.run = 1
  280. self.count = -1
  281. self.abs = 0 # ABSOLUT
  282. self.__angel = self.__clock_curr*360%360
  283. if master is None:
  284. cprint(master, "MASTER_FX ERR",master,color="red")
  285. self.__master = MASTER_FX()
  286. self.__master.add(self)
  287. else:
  288. self.__master = master
  289. self.__master.add(self)
  290. if self.__xtype == "rnd":
  291. self.__offset = self.__master.get(self,-2)
  292. self.__offset = self.__master.next(self)#,count)
  293. self._exec_id = None
  294. self.next()
  295. def exec_id(self,_id=None):
  296. if type(_id) is not type(None):
  297. self._exec_id = str(_id)
  298. return self._exec_id
  299. def _get_info(self):
  300. return {"offset":self.__offset,"xtype":self.__xtype}
  301. def __str__(self):
  302. return self.__repr__()
  303. def __repr__(self):
  304. ABS = "INC"
  305. if self.abs:
  306. ABS = "ABS"
  307. return "<FX Next:{:0.2f} xtype:{} Size:{:0.2f} Speed:{:0.2f} ang:{:0.2f} base:{} Clock:{:0.2f} run:{} EXEC:{} :{}>".format(
  308. self.next(), self.__xtype, self.__size, self.__speed, self.__angel
  309. , self.__base, self.__clock_curr, self.run, self._exec_id,ABS )
  310. def _calc_fx(self,v,t,size,base):
  311. base = 0
  312. if self.__base == "-": # sub
  313. if self.__invert:
  314. v = 1-v
  315. size *=-1
  316. v *=-1
  317. elif self.__base == "+": # sub
  318. if self.__invert:
  319. v = v-1
  320. else:
  321. v = (t%1-0.5)
  322. return v
  323. def next(self,clock=None):
  324. if type(clock) is float or type(clock) is int:#not None:
  325. self.__clock_curr = clock
  326. d = (self.__clock_curr - self.__clock_old)
  327. m1 = ( speed_master.val(self.__master_id)) # global speed-master
  328. m2 = ( exec_speed_master.val(self._exec_id)) # exec start by 0
  329. shift = 0
  330. m = (m1 * m2) -1
  331. shift += d * m
  332. self.__clock_delta += shift
  333. self.__clock_old = self.__clock_curr
  334. t = self.__clock_curr
  335. t += self.__clock_delta
  336. t *= self.__speed / 60
  337. offset2 = self.__offset
  338. offset2 *= exec_offset_master.val(self._exec_id)
  339. t += offset2 / 100
  340. t += self.__start / 200 # 1024 #255
  341. tw = t%1
  342. count = t//1
  343. t = t * (100/self.__width)
  344. if tw > self.__width/100:
  345. t = 1
  346. self.__angel = t%1*360
  347. t = t%1
  348. rad = math.radians(self.__angel)
  349. self.abs = 0
  350. v=0
  351. out = 0
  352. base = 0
  353. size = self.__size
  354. if self.__base == "+": # add
  355. base = size/2
  356. elif self.__base == "-": # sub
  357. base = size/2*-1
  358. if self.__xtype == "sinus":
  359. v = math.sin( rad )
  360. v/=2
  361. elif self.__xtype == "cosinus":
  362. v = math.cos( rad )
  363. if self.__base == "+": # add
  364. size *= -1
  365. v/=2
  366. elif self.__xtype == "rnd":
  367. #base = 0
  368. if self.__angel > 90 and self.__angel <=270:
  369. v=1
  370. else:
  371. v=0
  372. #if count != self.count and v: # % 2 == 0:#!= self.count:
  373. # #self.__offset = random.randint(0,1024)# /1024
  374. # self.__master._shuffle()
  375. if count != self.count and v == 0: # and v: # % 2 == 0:#!= self.count:
  376. self.__master.next(self)#,count)
  377. #self.__master.next(self)#,count)
  378. self.__offset = self.__master.get(self,count)
  379. base = 0
  380. if self.__base == "-": # sub
  381. if self.__invert:
  382. v = 1-v
  383. #base = -size
  384. size *=-1
  385. v *=-1
  386. elif self.__base == "+": # sub
  387. if self.__invert:
  388. v = v-1
  389. else:
  390. v = (t%1-0.5)
  391. elif self.__xtype == "on":
  392. #base = 0
  393. #if self.__angel > 90 and self.__angel <=270:
  394. if self.__angel > 0 and self.__angel <=360:
  395. v=1
  396. else:
  397. v=0
  398. base = 0
  399. if self.__base == "-": # sub
  400. if self.__invert:
  401. v = 1-v
  402. #base = -size
  403. size *=-1
  404. v *=-1
  405. elif self.__base == "+": # sub
  406. if self.__invert:
  407. v = v-1
  408. else:
  409. v = (t%1-0.5)
  410. elif self.__xtype == "ramp" or self.__xtype == "bump":
  411. v = (t%1)
  412. base = 0
  413. if self.__base == "-": # sub
  414. if self.__invert:
  415. v = 1-v
  416. #base = -size
  417. size *=-1
  418. v *=-1
  419. elif self.__base == "+": # sub
  420. if self.__invert:
  421. v = v-1
  422. else:
  423. v = (t%1-0.5)
  424. elif self.__xtype == "ramp2" or self.__xtype == "bump2":
  425. v = (t%1)
  426. v = 1-v
  427. if v == 1:
  428. v=0
  429. base = 0
  430. if self.__base == "-": # sub
  431. if self.__invert:
  432. v = 1-v
  433. #base = -size
  434. size *=-1
  435. v *=-1
  436. elif self.__base == "+": # sub
  437. if self.__invert:
  438. v = v-1
  439. else:
  440. v = (t%1-0.5)
  441. elif self.__xtype == "static":
  442. self.abs = 1
  443. base = size #100
  444. v=0
  445. size=0
  446. elif self.__xtype == "fade":
  447. x = t * 2
  448. if x > 1:
  449. x = 2-x
  450. x -= 0.5
  451. v = x*2
  452. #base /= 2
  453. #base *=2
  454. if self.__base == "+": # add
  455. pass#base /= 2
  456. else:
  457. v *= -1
  458. v/=2
  459. if self.__invert:
  460. v *=-1
  461. #if self.__fade_in_master < 255:
  462. # self.__fade_in_master += v*size
  463. out = v *size +base
  464. self.out = out
  465. self.count = count
  466. out = out * size_master.val(self.__master_id) # master
  467. out = out * exec_size_master.val(self._exec_id) # master
  468. #* (self.__fade_in_master /255.)
  469. return out
  470. class DMXCH(object):
  471. def __init__(self,dmx=-1):
  472. self._base_value = 0
  473. self._fade = None
  474. self._fx = [None,None] # None
  475. self._fx_value = 0
  476. self.name = "<name>"
  477. self._dmx = dmx
  478. self._dmx_fine = 0
  479. self._fix_id = 0
  480. self._v_master_id=0
  481. self._last_val_raw=0
  482. self._flash = None
  483. self._flash_fx = None
  484. self._flash_fx_value = 0
  485. self._last_val = None
  486. self._exec_ids = [None,None,None,None] # go, go-fx, flash, flash-fx
  487. def fade(self,target,ftime=0,clock=0,delay=0):
  488. if target != self._base_value:
  489. try:
  490. target = float(target)
  491. self._fade = Fade(self._base_value,target,ftime=ftime,clock=clock,delay=delay)
  492. except Exception as e:
  493. print( "Except:fade",e,target,ftime,clock)
  494. self.next(clock)
  495. def fx(self,xtype="sinus",size=40,speed=40,invert=0,width=100,start=0,offset=0,base="", clock=0,master=None):
  496. self._fx[0] = self._fx[1]
  497. if str(xtype).lower() == "off":
  498. fx_value = self._fx_value
  499. if fx_value != 0:
  500. #cprint("???????______ FX OFF AS FADE",fx_value,0,255)
  501. if self._fx[1].abs:
  502. self._fx[1] = Fade(self._last_val_raw,0,ftime=0.5,clock=clock)
  503. else:
  504. self._fx[1] = Fade(fx_value,0,ftime=0.5,clock=clock)
  505. else:
  506. self._fx[1] = None
  507. self._fx_value = 0
  508. else:
  509. self._fx[1] = FX(xtype=xtype,size=size,speed=speed,invert=invert
  510. ,width=width,start=start,offset=offset,base=base
  511. ,clock=clock,master=master,master_id=1)
  512. self._fx[1].exec_id(self._exec_ids[1])
  513. self.next(clock)
  514. def flash(self,target,ftime=0,clock=0,delay=0):
  515. if str(target).lower() == "off":
  516. if self._flash:
  517. cur_val = self._flash.next()
  518. cur_tar = self._base_value
  519. self._flash = Fade(cur_val,cur_tar,ftime=ftime,clock=clock)
  520. self._flash.off = 1
  521. else:
  522. try:
  523. target = float(target)
  524. self._flash = Fade(self._last_val,target,ftime=ftime,clock=clock,delay=delay)
  525. self._flash = Fade(self._last_val,target,ftime=0,clock=clock,delay=delay)
  526. except Exception as e:
  527. print( "Except:flash",target,ftime,clock,__name__,e,)
  528. self.next(clock)
  529. def flash_fx(self,xtype="sinus",size=40,speed=40,invert=0,width=100,start=0,offset=0,base="",clock=0,master=None):
  530. if str(xtype).lower() == "off":
  531. self._flash_fx = None
  532. self._flash_fx_value = 0
  533. else:
  534. self._flash_fx = FX(xtype=xtype,size=size,speed=speed,invert=invert
  535. ,width=width,start=start,offset=offset,base=base
  536. ,clock=clock,master=master,master_id=0)
  537. self._flash_fx.exec_id(self._exec_ids[3])
  538. self.next(clock)
  539. #print("init flash_fx",self)
  540. def fx_ctl(self,cmd=""): #start,stop,off
  541. pass
  542. def __str__(self):
  543. return self.__repr__()
  544. def exec_ids(self,_id=None):
  545. #if type(_id) is not type(None):
  546. # #self._exec_id = _id
  547. # #print("set exec_id",_id)
  548. return self._exec_ids
  549. def __repr__(self):
  550. v = self._last_val
  551. if type(self._last_val) in [int,float]:
  552. v = round(self._last_val,2)
  553. return "<DMXCH {} DMX:{} FINE:{} VAL:{} EXEC:{}\n fd:{}\n fx:{}\n fx_flash:{}\n".format(
  554. self.name,
  555. self._dmx,self._dmx_fine
  556. ,v,str(self._exec_ids)
  557. ,self._fade
  558. ,self._fx
  559. ,self._flash_fx)
  560. def fade_ctl(self,cmd=""): #start,stop,backw,fwd,bounce
  561. pass
  562. def next(self,clock=0):
  563. try:
  564. self._next(clock)
  565. except Exception as e:
  566. cprint("Exception DMXCH.next()" ,e)
  567. out = self._last_val
  568. return out
  569. def _next(self,clock=0):
  570. value = self._base_value
  571. if self._last_val is None:
  572. self._last_val = value
  573. fx_value = self._fx_value
  574. fx_abs = 0
  575. if self._flash is not None:
  576. value = self._flash.next(clock)
  577. value = self._flash.next(clock) #flicker bug ?!
  578. if self._flash.end == 1 and self._flash.off == 1:
  579. self._flash = None
  580. fx_value = 0
  581. elif self._fade is not None: # is Fade: # is Fade:
  582. self._base_value = self._fade.next(clock)
  583. self._base_value = self._fade.next(clock) #flicker bug ?!
  584. value = self._base_value
  585. if self._flash_fx is not None:# is FX:
  586. fx_value = self._flash_fx.next(clock)
  587. fx_abs = self._flash_fx.abs
  588. else:
  589. self._fx_value = 0
  590. if self._fx[-1] is not None and self._flash is None:# is FX:
  591. self._fx_value += self._fx[-1].next(clock)
  592. fx_abs = self._fx[-1].abs
  593. fx_value = self._fx_value
  594. if fx_abs == 1:
  595. self._last_val = fx_value
  596. else:
  597. self._last_val = value + fx_value
  598. self._last_val_raw = self._last_val
  599. if self._v_master_id in V_MASTER:
  600. vm = V_MASTER[self._v_master_id].next(clock)
  601. #binfs.patch(self._v_master_id,int(vm))
  602. binfs.buffer(self._v_master_id,int(vm))
  603. vm = vm/256
  604. self._last_val *= vm
  605. out = self._last_val
  606. return out
  607. V_MASTER = {} #DMXCH
  608. Bdmx = []
  609. for i in range(512*7+1):
  610. xDMX = DMXCH(i)
  611. xDMX.name = "543"+str(i)
  612. Bdmx.append(xDMX)
  613. #print(type(dmx[i]))
  614. _id = 1
  615. def split_cmd(data):
  616. if "cmd" in data:
  617. cmd = data["cmd"]
  618. #print("cmd",cmd)
  619. if "," in cmd:
  620. cmds = cmd.split(",")
  621. else:
  622. cmds = [cmd]
  623. return cmds
  624. class VDMX():
  625. """functional implementation as class for namespace encapsulation
  626. """
  627. def __init__(self):
  628. self.data = OrderedDict()
  629. self.data[4] = {"DMX":[21,22,23],"VALUE":255, "LIMIT":255} #,"DMXCH":DMXCH("V4")}
  630. def _list_by_dmx(self,_dmx=0):
  631. data = OrderedDict()
  632. for i,link in self.data.items(): # enumerate(self.data):
  633. if _dmx in link["DMX"]:
  634. #print( "_list_by_dmx",i,link)
  635. data[i] = link
  636. return data
  637. def dmx_by_id(self,_id=0):
  638. #print("dmx by master-id:",_id)
  639. if _id in self.data:
  640. for i,link in self.data[_id].items():
  641. #print("dmx_by_id", i,link)
  642. return (i,link)
  643. return 0,{}
  644. def by_dmx(self,clock,dmx):
  645. #print("master of dmx:",dmx)
  646. val=0
  647. flag = 0
  648. data = self._list_by_dmx(dmx)
  649. for i,row in data.items():
  650. if "DMXCH" not in row:
  651. row["DMXCH"] = DMXCH("V{}".format(i))
  652. row["DMXCH"].fade(255,0)
  653. v = row["DMXCH"].next(clock)
  654. #row["DMXCH"].fade(200,20)
  655. if v >= val:
  656. val = v
  657. flag = 1
  658. out = 1.
  659. if val > 255:
  660. val = 255
  661. if flag:
  662. out = val/255.
  663. else:
  664. out = 1.
  665. return out
  666. vdmx = VDMX()
  667. class HTP_MASTER():
  668. """functional implementation as class for namespace encapsulation
  669. """
  670. def __init__(self):
  671. self.data = OrderedDict()
  672. #self.data[1] = {"DMX":[1,2,3],"VALUE":80, "LIMIT":255}
  673. #self.data[2] = {"DMX":[12,13,22],"VALUE":70, "LIMIT":255}
  674. #self.data[3] = {"DMX":[22,23,24],"VALUE":99, "LIMIT":255}
  675. self.data[4] = {"DMX":[22,23,24],"VALUE":99, "LIMIT":255,"DMXCH":DMXCH(4)}
  676. def _list_by_dmx(self,_dmx=0):
  677. data = OrderedDict()
  678. for i,link in self.data.items(): # enumerate(self.data):
  679. if _dmx in link["DMX"]:
  680. #print( "_list_by_dmx",i,link)
  681. data[i] = link
  682. return data
  683. def dmx_by_id(self,_id=0):
  684. #print("dmx by master-id:",_id)
  685. if _id in self.data:
  686. for i,link in self.data[_id].items():
  687. #print("dmx_by_id", i,link)
  688. return (i,link)
  689. return 0,{}
  690. def val_by_dmx(self,dmx=0):
  691. #print("master of dmx:",dmx)
  692. val=0
  693. flag = 0
  694. data = self._list_by_dmx(dmx)
  695. for _,link in data.items():
  696. #print("master_by_dmx", i,link)
  697. if link["VALUE"] > val:
  698. #print("master_by_dmx", i,link)
  699. val = link["VALUE"]
  700. flag=1
  701. out = 1.
  702. if flag:
  703. out = val/255.
  704. return out
  705. htp_master = HTP_MASTER()
  706. #htp_master.data[_id] = {"DMX":[1,2,3],"VALUE":80, "LIMIT":255,"DMXCH":DMXCH()}
  707. class Core():
  708. def __init__(self):
  709. self.artnet = {}
  710. self.fx = {} # key is dmx address
  711. self.lock = thread.allocate_lock()
  712. def _fine_loop(self,Bdmx,t):
  713. old_univ = -1
  714. xx = [0]*512
  715. for ii,dmxch in enumerate(Bdmx): #fine loop
  716. dmx_fine = dmxch._dmx_fine
  717. if dmx_fine > 0:
  718. i = ii%512
  719. univ = ii//512
  720. if univ != old_univ:
  721. artnet = self.artnet[str(univ)]
  722. xx = artnet.dmx# = xx
  723. v = dmxch.next(t)
  724. vv = vdmx.by_dmx(clock=i,dmx=ii+1)
  725. try:
  726. v = v*vv # disable v-master
  727. except Exception as e:
  728. cprint("Exception v*vv",[v,vv],e)
  729. continue
  730. dmx_fine = dmx_fine%512
  731. try:
  732. if v >= 255:
  733. xx[dmx_fine-1] = 255
  734. elif v < 0:
  735. xx[dmx_fine-1] = 0
  736. else:
  737. xx[dmx_fine-1] = int(v%1*255)
  738. except Exception as e:
  739. print("E dmx_fine",e,dmx_fine)
  740. def _coarse_loop(self,Bdmx,t):
  741. old_univ = -1
  742. xx = [0]*512
  743. for ii,dmxch in enumerate(Bdmx):
  744. i = ii%512
  745. univ = ii//512
  746. if univ != old_univ:
  747. old_univ = univ
  748. artnet = self.artnet[str(univ)]
  749. xx = artnet.dmx
  750. v = dmxch.next(t)
  751. xx[i] = int(v)
  752. def artnete_init(self,Bdmx):
  753. """Init artnet if not exist's
  754. """
  755. for ii,_ in enumerate(Bdmx):
  756. univ = ii//512
  757. if str(univ) not in self.artnet:
  758. print("add uiv",univ)
  759. self.artnet[str(univ)] = ANN.ArtNetNode(to="10.10.10.255",univ=univ)
  760. def calc_fps(self,stop_fps=50):
  761. self.fps += 1
  762. if self.fps >= stop_fps:
  763. fps_t = time.time()
  764. FPS = round(stop_fps/(fps_t-self.fps_start),2)
  765. print("{:0.0f} core/fps".format(FPS) )
  766. self.fps = 0
  767. self.fps_start = time.time()
  768. def loop(self):
  769. """ core artnet calculation loop
  770. """
  771. self.artnete_init(Bdmx)
  772. self.fps_start = time.time()
  773. self.fps = 0
  774. while 1:
  775. self.lock.acquire_lock()
  776. self.artnete_init(Bdmx)
  777. t = clock.time()
  778. self._coarse_loop(Bdmx,t)
  779. self._fine_loop(Bdmx,t)
  780. self.lock.release_lock()
  781. for _,artnet in self.artnet.items():
  782. artnet.next()
  783. time.sleep(1/30)
  784. self.calc_fps()
  785. core = Core()
  786. if __run_main:
  787. thread.start_new_thread(core.loop,())
  788. def _init_action(row):#Bdmx,out,DMX):
  789. Admx = row["DMXCH"]
  790. line_sub_count = 0
  791. if row["fx"]:
  792. x = row["fx"]
  793. Admx.fx(xtype=x["xtype"]
  794. ,size=x["size"]
  795. ,speed=x["speed"]
  796. ,invert=x["invert"]
  797. ,width=x["width"]
  798. ,start=x["start"]
  799. ,offset=x["offset"]
  800. ,base=x["base"]
  801. ,clock=x["clock"]
  802. ,master=x["master"])
  803. line_sub_count += 1
  804. if row["flash_fx"]:
  805. x = row["flash_fx"]
  806. Admx.flash_fx(xtype=x["xtype"]
  807. ,size=x["size"]
  808. ,speed=x["speed"]
  809. ,invert=x["invert"]
  810. ,width=x["width"]
  811. ,start=x["start"]
  812. ,offset=x["offset"]
  813. ,base=x["base"]
  814. ,clock=x["clock"]
  815. ,master=x["master"])
  816. line_sub_count += 1
  817. if row["flash"]:
  818. x = row["flash"]
  819. Admx.flash(target=x["target"]
  820. ,ftime=x["ftime"]
  821. ,clock=x["clock"]
  822. ,delay=x["delay"])
  823. line_sub_count += 1
  824. if row["fade"]:
  825. x = row["fade"]
  826. Admx.fade(target=x["target"]
  827. ,ftime=x["ftime"]
  828. ,clock=x["clock"]
  829. ,delay=x["delay"])
  830. line_sub_count += 1
  831. return line_sub_count
  832. def set_dmx_fine_ch(Admx,dmx_fine_nr):
  833. try:
  834. if int(dmx_fine_nr) > 0:
  835. Admx._dmx_fine = int(dmx_fine_nr)
  836. except Exception as e:
  837. cprint(x,color="red")
  838. cprint("except 3455",e,color="red")
  839. def _parse_fx2(x,fx2,clock):
  840. if type(fx2) is dict and fx2:
  841. pass
  842. else:
  843. return
  844. xtype="fade"
  845. size = 10
  846. speed = 10
  847. start = 0
  848. offset= 0
  849. width=100
  850. invert=0
  851. base = "-"
  852. if "TYPE" in fx2:
  853. xtype = fx2["TYPE"]
  854. if "SIZE" in fx2:
  855. size = fx2["SIZE"]
  856. if "SPEED" in fx2:
  857. speed = fx2["SPEED"]
  858. if "OFFSET" in fx2:
  859. offset = fx2["OFFSET"]
  860. if "BASE" in fx2:
  861. base = fx2["BASE"]
  862. if "INVERT" in fx2:
  863. invert = fx2["INVERT"]
  864. if "WIDTH" in fx2:
  865. width = fx2["WIDTH"]
  866. if "START" in fx2:
  867. start = fx2["START"]
  868. if "off" == x["VALUE"]: #fix fx flash off
  869. xtype= "off"
  870. if "alloff" == xtype.lower():
  871. for dmxch in Bdmx:
  872. if dmxch is not None:
  873. dmxch.flash_fx(xtype="off",clock=clock)
  874. dmxch.fx(xtype="off",clock=clock)
  875. for j in V_MASTER:
  876. dmxch = V_MASTER[j]
  877. if j is not None:
  878. dmxch.flash_fx(xtype="off",clock=clock)
  879. dmxch.fx(xtype="off",clock=clock)
  880. xout = {"xtype":xtype,"size":size,"speed":speed
  881. ,"invert":invert,"width":width,"start":start
  882. ,"offset":offset,"base":base,"clock":clock,"master":0
  883. }
  884. #,"offset":offset,"base":base,"clock":c,"master":master_fx
  885. mode="fx"
  886. if "FLASH" in x:
  887. mode = "flash_fx"
  888. return [mode,xout]
  889. def _parse_master(x):
  890. print("CMD:",x)
  891. if "EXEC-SPEED-MASTER" == x["CMD"]:
  892. exec_speed_master.val(x["NR"],x["VALUE"])
  893. if "EXEC-SIZE-MASTER" == x["CMD"]:
  894. exec_size_master.val(x["NR"],x["VALUE"])
  895. if "EXEC-OFFSET-MASTER" == x["CMD"]:
  896. exec_offset_master.val(x["NR"],x["VALUE"])
  897. if "SPEED-MASTER" == x["CMD"]:
  898. speed_master.val(x["NR"],x["VALUE"])
  899. if "SIZE-MASTER" == x["CMD"]:
  900. size_master.val(x["NR"],x["VALUE"])
  901. def _check_vmaster(DMX,attr):
  902. if DMX <= 0 and "DIM" == attr:
  903. return True
  904. def _get_vmaster(_fix_id,attr):
  905. if _fix_id not in V_MASTER:
  906. print(" VMASTER init",_fix_id)
  907. V_MASTER[_fix_id] = DMXCH()
  908. V_MASTER[_fix_id].name = "vmaster_"+str(_fix_id)
  909. Admx = V_MASTER[_fix_id]
  910. return Admx
  911. def _get_dmx(DMX,Bdmx):
  912. if DMX < len(Bdmx):
  913. Admx = Bdmx[DMX-1]
  914. else:
  915. Admx = DMXCH() #dummy
  916. Admx.name = "get-dmx-dummy"
  917. print("DMX ADDRESS too BIG",DMX)
  918. return Admx
  919. def _parse_cmds(cmds,clock=0):
  920. out = {}
  921. for cmd in cmds:
  922. try:
  923. _parse_cmd(out,cmd,clock=clock)
  924. except Exception as e:
  925. cprint("EXCEPTION JCB",e,color="red")
  926. cprint("----",str(cmds)[:150],"...",color="red")
  927. cprint("Error on line {}".format(sys.exc_info()[-1].tb_lineno),color="red")
  928. raise e
  929. return out
  930. def _check_cmd(cmd):
  931. err=0
  932. if not isinstance(cmd,dict):
  933. cprint("_check_cmd() ... cmd is not dict !",type(cmd),color="red")
  934. err=1
  935. #if "DMX" not in cmd:
  936. # cprint("_check_cmd() ... 'DMX' not in x!",color="red")
  937. # #err=1
  938. if "VALUE" not in cmd:
  939. cprint("_check_cmd() ... 'VALUE' not in x!",color="red")
  940. err=1
  941. if err:
  942. return
  943. return True
  944. def _parse_cmd(out,cmd,clock=0):
  945. if not isinstance(out,dict):
  946. cprint("_parse_cmd() ... out !",type(out),color="red")
  947. return
  948. if not _check_cmd(cmd):
  949. return
  950. if "CMD" in cmd:
  951. _parse_master(cmd)
  952. return
  953. master_fx = MASTER_FX()
  954. c=clock
  955. DMX = cmd.get("DMX",0)
  956. DMX = int(DMX)
  957. exec_id = cmd.get("EXEC",None)
  958. _fix_id = cmd.get("FIX",0)
  959. _attr = cmd.get("ATTR","")
  960. _clock = cmd.get("clock",0)
  961. _val = cmd.get("VALUE","")
  962. _inc = cmd.get("INC",0)
  963. fx = cmd.get("FX","")
  964. fx2 = cmd.get("FX2",{})
  965. ftime = cmd.get("FADE",0)
  966. delay = cmd.get("DELAY",0)
  967. #print("cmd",cmd)
  968. Admx = _get_dmx(DMX,Bdmx)
  969. if _check_vmaster(DMX,_attr):
  970. Admx = _get_vmaster(_fix_id,_attr)
  971. DMX = "FIX"+str(_fix_id)
  972. DMX_FINE = cmd.get("DMX-FINE",-1)
  973. if DMX_FINE > 0:
  974. set_dmx_fine_ch(Admx, DMX_FINE)
  975. ids = Admx.exec_ids()
  976. if _val != "off":
  977. if "FLASH" in cmd:
  978. if type(_val) is int:
  979. ids[2] = exec_id
  980. if fx2:
  981. ids[3] = exec_id
  982. #print(" ",[ids, exec_id],"FL")
  983. else: # GO or ON
  984. if type(_val) is int:
  985. ids[0] = exec_id
  986. if fx2:
  987. ids[1] = exec_id
  988. #print(" ",[ids, exec_id],"GO")
  989. if _val == "off":
  990. # flash bug if lot's of virtual dimmer are RELEASED-FROM-FLASH ("FLASH off")
  991. # the V-MASTER FLASH ist HANGING ... no ENCODER change possible !!! ???
  992. if 0: # "FLASH" in cmd:
  993. stop = 0
  994. #print(" ",[ids, exec_id])
  995. if ids[2] != exec_id:
  996. stop = 1
  997. else:
  998. ids[2] = None
  999. if fx2:
  1000. if ids[3] != exec_id:
  1001. stop = 1
  1002. else:
  1003. ids[3] = None
  1004. stop = 0
  1005. if stop:
  1006. # this FLASH cmd OFF/RELEASE is not valid anymore
  1007. return #continue
  1008. out[DMX] = {"flash":{},"fade":{},"fx":{},"flash_fx":{},"fix_id":_fix_id,"attr":_attr,"DMXCH":Admx}
  1009. if _val is not None:
  1010. Xval = {"target":_val,"ftime":ftime, "clock":c,"delay":delay,"DMXCH":Admx}
  1011. mode = "fade"
  1012. if "FLASH" in cmd:
  1013. mode = "flash"
  1014. out[DMX][mode] = Xval
  1015. # FX PARSE
  1016. _fx2 = _parse_fx2(cmd,fx2,clock=c)
  1017. if _fx2:
  1018. _key=_fx2[0] # fx, fx_flash
  1019. Xval=_fx2[1] # dict
  1020. Xval["master"] = master_fx
  1021. out[DMX][_key] = Xval
  1022. elif type(fx) is str and fx:
  1023. # old fx like sinus:200:12:244
  1024. ccm = str(DMX+1)+":"+fx
  1025. print("fx",ccm)
  1026. if "FLASH" in cmd:
  1027. pass #CB({"cmd":"fxf"+ccm})
  1028. else:
  1029. pass #CB({"cmd":"fx"+ccm})
  1030. def calc_jitter(jdatas,t_start):
  1031. for cmds in jdatas:
  1032. for line in cmds:
  1033. if "time" in line:
  1034. jt_start = line["time"]
  1035. latenz = round(t_start-jt_start,4)
  1036. if latenz > 0.5:
  1037. cprint("jitter 0.5 >",latenz,color="red")
  1038. break
  1039. def process_flash(jdatas):
  1040. for cmds in jdatas:
  1041. for line in cmds: # run first
  1042. if "FLASH" in line:
  1043. cprint("FLUSH",end=" ",color="CYAN")
  1044. if "VALUE" in line:
  1045. if line["VALUE"] == "off":
  1046. cprint("OFF",end=" ",color="red")
  1047. else:
  1048. cprint("ON",end=" ",color="green")
  1049. print("")
  1050. else:
  1051. cprint("FADE",color="CYAN")
  1052. break
  1053. def parse_json(data):
  1054. jdatas = []
  1055. l2 = 0
  1056. print("INPUT JCB =>",len(data),":",l2)
  1057. for line in data:
  1058. data2 = json.loads(line)
  1059. l2 += len(data2)
  1060. jdatas.append(data2) #["CMD"])
  1061. jdatas2 = []
  1062. for cmds in jdatas:
  1063. if not cmds:
  1064. continue
  1065. jdatas2.append(cmds)
  1066. return jdatas2
  1067. import hashlib
  1068. JCB_GLOB_BUF = {}
  1069. def JCB(data,sock=None): #json client input
  1070. t_start = time.time()
  1071. s = time.time()
  1072. e = time.time()
  1073. ct = int(e*100)/100
  1074. msg = "{} JCB START: {:0.02f} sizeof:{}"
  1075. msg = msg.format(ct,e-s,sys.getsizeof(data) )
  1076. jdatas = parse_json(data)
  1077. c = clock.time()
  1078. c = float(c)
  1079. out = {}
  1080. process_flash(jdatas)
  1081. calc_jitter(jdatas,t_start)
  1082. for cmds in jdatas:
  1083. #master_fx = MASTER_FX()
  1084. out = _parse_cmds(cmds,clock=c)
  1085. line_count = 0
  1086. line_count_sub = 0
  1087. line_size = 0
  1088. attr_count = {}
  1089. attr_count2 = 0
  1090. if not out:
  1091. return
  1092. try:
  1093. core.lock.acquire_lock()
  1094. for _id in out:
  1095. row = out[_id]
  1096. #print(" ",_id,[str(row)])
  1097. line_size += sys.getsizeof(row)
  1098. #print("_id",_id)
  1099. Admx = row["DMXCH"]
  1100. #print("Admx",Admx)
  1101. if "attr" in row:
  1102. if row["attr"] not in attr_count:
  1103. attr_count[row["attr"]] = 0
  1104. attr_count[row["attr"]] += 1
  1105. attr_count2 +=1
  1106. if row["fix_id"]:
  1107. _fix_id = row["fix_id"]
  1108. Admx._fix_id = _fix_id
  1109. if "attr" in row:
  1110. if row["attr"] in ["RED","GREEN","BLUE","WHITE","AMBER"]: #CYAN,MAGENTA,YELLOW
  1111. Admx._v_master_id = _fix_id
  1112. #print("SET V_MASTER",row)
  1113. line_count_sub += _init_action(row)
  1114. line_count += 1
  1115. e = time.time()
  1116. print(" sub-JCB TIME:","{:0.02f}".format(e-s),int(e*100)/100)
  1117. #time.sleep(1/30)
  1118. except Exception as e:
  1119. cprint("EXCEPTION JCB",e,color="red")
  1120. cprint("----",str(cmds)[:150],"...",color="red")
  1121. cprint("Error on line {}".format(sys.exc_info()[-1].tb_lineno),color="red")
  1122. raise e
  1123. finally:
  1124. core.lock.release_lock()
  1125. #cprint(" ","{:0.04} sec.".format(time.time()-t_start),color="yellow")
  1126. print("attr_count:",attr_count)
  1127. #print(line_count,line_size)
  1128. e = time.time()
  1129. ct = int(e*100)/100
  1130. msg = "{} JCB: END {:0.02f} sizeof:{} fix-count:{} attr-count:{}"
  1131. msg = msg.format(ct,e-s,line_size,line_count,line_count_sub )
  1132. print(msg)
  1133. time.sleep(1/60)
  1134. def jcb_loop():
  1135. s = chat.Server(cb=JCB)
  1136. while 1:
  1137. s.poll()
  1138. time.sleep(0.001)
  1139. if __run_main:
  1140. jcb_loop()