_console.py 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  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@uxsrv.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. rnd_id += subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode('ascii').strip()
  24. if "__file__" in dir():
  25. sys.stdout.write("\x1b]2;"+str(__file__)+" "+rnd_id+"\x07") # terminal title
  26. else:
  27. sys.stdout.write("\x1b]2;"+str("__file__")+" "+rnd_if+"\x07") # terminal title
  28. __run_main = 0
  29. if __name__ == "__main__":
  30. __run_main = 1
  31. else:
  32. import __main__
  33. print(dir())
  34. if "unittest" not in dir(__main__):
  35. __run_main = 1
  36. import time
  37. import socket
  38. import struct
  39. import sys
  40. import random
  41. import math
  42. from collections import OrderedDict
  43. import lib.zchat as chat
  44. import lib.ArtNetNode as ANN
  45. import _thread as thread
  46. #thread.start_new_thread
  47. import lib.motion as motion
  48. #idmx = [0]*512 # incremental dmx
  49. dmx = [0]*512 # absolute dmx data
  50. gcolor = 1
  51. def cprint(*text,color="blue",space=" ",end="\n"):
  52. #return 0 #disable print dbg
  53. if not gcolor:
  54. print(text)
  55. return 0
  56. if color == "green":
  57. txt = '\033[92m'
  58. elif color == "red":
  59. txt = '\033[0;31m\033[1m'
  60. elif color == "yellow":
  61. txt = '\033[93m\033[1m'
  62. elif color == "cyan":
  63. txt = '\033[96m'
  64. else:
  65. txt = '\033[94m'
  66. for t in text:
  67. txt += str(t ) +" "
  68. #HEADER = '\033[95m'
  69. #OKBLUE = '\033[94m'
  70. #OKCYAN = '\033[96m'
  71. #OKGREEN = '\033[92m'
  72. #WARNING = '\033[93m'
  73. #FAIL = '\033[91m'
  74. #ENDC = '\033[0m'
  75. #BOLD = '\033[1m'
  76. #UNDERLINE = '\033[4m'
  77. txt += '\033[0m'
  78. print(txt,end=end)
  79. #return txt
  80. def artnet_loop():
  81. #artnet = ANN.ArtNetNode(to="127.0.0.1",port=6555,univ=12)
  82. #artnet = ANN.ArtNetNode(to="127.0.0.1",port=6555,univ=0)
  83. artnet = ANN.ArtNetNode(to="10.10.10.255",univ=0)
  84. #artnet = ANN.ArtNetNode(to="2.0.0.255",univ=0)
  85. #artnet = ANN.ArtNetNode(to="10.10.10.255",univ=1)
  86. #dmx[205] = 255 #205 BLUE
  87. artnet.dmx= dmx #[0]*512
  88. artnet.send()
  89. while 1:
  90. #artnet._test_frame()
  91. artnet.next()
  92. time.sleep(0.01)
  93. class CLOCK():
  94. def __init__(self):
  95. self.__time = 0
  96. self.__start = time.time() # only for debugging
  97. self.__tick = 0.01 # incremental timer drift's on highe cpu load ?
  98. def time(self):
  99. return self.__time
  100. def get_drift(self):
  101. run_time = time.time() - self.__start
  102. tick_time = self.__time # * self.__tick
  103. print( "runtime:{:0.2f} tick_timer:{:0.2f} drift:{:0.2f}".format(run_time,tick_time,run_time-tick_time))
  104. def loop(self):
  105. while 1:
  106. self.__time +=self.__tick
  107. #if int(self.__time*100)/10. % 10 == 0:# self.__time % 2 == 0:
  108. # print( self.get_drift())
  109. #print(self.__time)
  110. #for i in range(10):
  111. time.sleep(self.__tick)
  112. class CLOCK_REAL():
  113. def __init__(self):
  114. self.__time = 0
  115. self.__start = time.time() # only for debugging
  116. self.__tick = 0.01 # incremental timer drift's on highe cpu load ?
  117. def time(self):
  118. self.__time = time.time()
  119. return self.__time
  120. def get_drift(self):
  121. run_time = time.time() - self.__start
  122. tick_time = self.__time # * self.__tick
  123. print( "runtime:{:0.2f} tick_timer:{:0.2f} drift:{:0.2f}".format(run_time,tick_time,run_time-tick_time))
  124. def loop(self):
  125. pass
  126. #clock = CLOCK()
  127. clock = CLOCK_REAL()
  128. if __run_main:
  129. thread.start_new_thread(clock.loop,())
  130. class Fade():
  131. def __init__(self,start,target,ftime,clock,delay=0):
  132. #print("init Fade ",start,target,ftime,clock)
  133. if delay < 0:
  134. delay = 0.0001
  135. if ftime <= 0:
  136. ftime = 0.0001
  137. clock += delay
  138. self.__delay = delay
  139. self.__clock = clock
  140. self.__clock_curr = clock
  141. self.__ftime = ftime
  142. self.__start = start
  143. self.__last = start
  144. self.__target = target
  145. self.run = 1
  146. #print("INIT", str(self) )
  147. def __str__(self):
  148. return self.__repr__()
  149. def __repr__(self):
  150. return "<FADE Next:{:0.2f} from:{:0.2f} to:{:0.2f} ft:{:0.2f} Clock:{:0.2f} run:{} delay:{:0.2f}>".format(
  151. self.__last, self.__start,self.__target,self.__ftime,self.__clock_curr,self.run,self.__delay )
  152. def next(self,clock=None):
  153. if self.__ftime <= 0 and self.__delay <= 0:
  154. self.__last = self.__target
  155. self.run = 0
  156. if type(clock) is float or type(clock) is int:#not None:
  157. self.__clock_curr = clock
  158. if self.__target > self.__start:
  159. if self.__last >= self.__target:
  160. self.run = 0
  161. return self.__target
  162. else:
  163. if self.__last <= self.__target:
  164. self.run = 0
  165. return self.__target
  166. current = (self.__clock - self.__clock_curr) / self.__ftime
  167. length = self.__start - self.__target
  168. self.__last = self.__start+ length*current
  169. #if self.__last < 0:
  170. # self.__last = 0
  171. #if self.__last > 255:
  172. # self.__last = 255
  173. self.run = 1
  174. return self.__last
  175. def ctl(self,cmd="",value=None): # if x-fade cmd="%" value=50
  176. # start,stop,fwd,bwd,revers
  177. pass
  178. class _MASTER():
  179. def __init__(self,name="None"):
  180. self.__data = {}
  181. self.name = name
  182. def val(self,name,value=None):
  183. _value = 100 #%
  184. name = str(name)
  185. if name not in self.__data:
  186. self.__data[name] = 100
  187. _value = self.__data[name]
  188. if value is not None:
  189. if _value != value:
  190. print(self.name,"CHANGE MASTER:",name,"from:",_value,"to:",value)
  191. self.__data[name] = value
  192. _value = self.__data[name]
  193. return _value /100.
  194. exec_size_master = _MASTER("EXEC-SIZE")
  195. exec_speed_master = _MASTER("EXEC-SPEED")
  196. exec_offset_master = _MASTER("EXEC-OFFSET")
  197. size_master = _MASTER("SIZE")
  198. speed_master = _MASTER("SPEED")
  199. exe_master = []
  200. exe_master.append({"SIZE":100,"SPEED":100,"id":12,"link-ids":[2]})
  201. class MASTER_FX():
  202. def __init__(self):
  203. #cprint(self,"MASTER_FX INIT !",color="green")
  204. self.__data = []
  205. self.__ok = []
  206. self.i=0
  207. self.old_offsets = []
  208. self.offsets = []
  209. self.count = -1
  210. self.init = 10
  211. def add(self,fx):
  212. if fx not in self.__data:
  213. #cprint(self,"ADD TO MASTER !",color="green")
  214. self.__data.append(fx)
  215. info = fx._get_info()
  216. #cprint(self,"ADD" ,info,color="green")
  217. offset = 0
  218. if "offset" in info:
  219. offset = info["offset"]
  220. self.old_offsets.append(offset)
  221. self.offsets.append(offset)
  222. if "xtype" in info:
  223. if info["xtype"] == "rnd":
  224. self._shuffle()
  225. #self.init += 1
  226. def _shuffle(self):
  227. #cprint(self,"REORDER RANDOM !",color="green")
  228. #self.init = 0
  229. #cprint(self.old_offsets)
  230. random.shuffle(self.old_offsets)
  231. #cprint(self.old_offsets)
  232. def _init(self):
  233. self._shuffle()
  234. #self.offsets = []
  235. for i,v in enumerate(self.old_offsets):
  236. offset = self.old_offsets[i]
  237. self.offsets[i] = offset
  238. self.init = 0
  239. def next(self,child):
  240. i = self.__data.index(child)
  241. offset = self.old_offsets[i]
  242. self.offsets[i] = offset
  243. return offset
  244. #for i,v in enumerate(self.old_offsets):
  245. # offset = self.old_offsets[i]
  246. # self.offsets[i] = offset
  247. def get(self,child,count):
  248. offset = 0
  249. if child not in self.__data:
  250. return offset
  251. if self.init:
  252. self._init()
  253. idx = self.__data.index(child)
  254. if (self.count != count and idx == 0 ) or self.init == 0:
  255. self.init = 1
  256. self._shuffle()
  257. #print( count)
  258. self.count=count
  259. idx = self.__data.index(child)
  260. offset = self.offsets[idx]
  261. return offset
  262. class FX():
  263. 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):
  264. self.__xtype=xtype
  265. self.__size = size
  266. self.__start = start
  267. self.__master_id = master_id
  268. if width > 200:
  269. width = 200
  270. if width <= 0:
  271. width = 1
  272. self.__fade_in_master = 0
  273. self.__width = width
  274. self.__invert = invert
  275. self.__base = base
  276. self.__speed = speed
  277. self.__offset = offset
  278. self.__clock = clock
  279. self.__clock_curr = clock
  280. self.__clock_delta = 0
  281. self.__clock_old = self.__clock_curr
  282. self.out = 0
  283. self.old_v = -1
  284. self.run = 1
  285. self.count = -1
  286. self.__angel = self.__clock_curr*360%360
  287. if master is None:
  288. cprint(master, "MASTER_FX ERR",master,color="red")
  289. self.__master = MASTER_FX()
  290. self.__master.add(self)
  291. else:
  292. #cprint( "MASTER_FX OK",master,color="red")
  293. self.__master = master
  294. self.__master.add(self)
  295. if self.__xtype == "rnd":
  296. self.__offset = self.__master.get(self,-2)
  297. self.__offset = self.__master.next(self)#,count)
  298. self._exec_id = None
  299. self.next()
  300. #print("init FX",self)
  301. def exec_id(self,_id=None):
  302. if type(_id) is not type(None):
  303. self._exec_id = str(_id)
  304. return self._exec_id
  305. def _get_info(self):
  306. #print("self.__offset",self.__offset)
  307. return {"offset":self.__offset,"xtype":self.__xtype}
  308. #return self.next(),self.__xtype, self.__size,self.__speed,self.__angel, self.__base,self.__clock_curr,self.run
  309. def __str__(self):
  310. return self.__repr__()
  311. def __repr__(self):
  312. return "<FX Next:{:0.2f} xtype:{} Size:{:0.2f} Speed:{:0.2f} ang:{:0.2f} base:{} Clock:{:0.2f} run:{} EXEC:{}>".format(
  313. self.next(),self.__xtype, self.__size,self.__speed,self.__angel, self.__base,self.__clock_curr,self.run,self._exec_id )
  314. def next(self,clock=None):
  315. if type(clock) is float or type(clock) is int:#not None:
  316. self.__clock_curr = clock
  317. d = (self.__clock_curr - self.__clock_old)
  318. #print()
  319. #print("A",d)
  320. m1 = ( speed_master.val(self.__master_id)) # global speed-master
  321. #print("B {:0.4}".format(m1))
  322. m2 = ( exec_speed_master.val(self._exec_id)) # exec start by 0
  323. #print("C {:0.4}".format(m2))
  324. shift = 0
  325. m = (m1 * m2) -1
  326. shift += d * m
  327. #print("D",shift)
  328. self.__clock_delta += shift
  329. #print(self.__clock_delta )
  330. self.__clock_old = self.__clock_curr
  331. t = self.__clock_curr
  332. t += self.__clock_delta
  333. t *= self.__speed / 60
  334. offset2 = self.__offset
  335. offset2 *= exec_offset_master.val(self._exec_id)
  336. #t += self.__offset / 100 #255 #1024 #255
  337. t += offset2 / 100
  338. t += self.__start / 1024 #255
  339. #t = t*speed_master.val(self.__master_id)
  340. tw = t%1
  341. count = t//1
  342. t = t * (100/self.__width)
  343. if tw > self.__width/100:
  344. t = 1
  345. self.__angel = t%1*360
  346. t = t%1
  347. rad = math.radians(self.__angel)
  348. v=0
  349. out = 0
  350. base = 0
  351. size = self.__size
  352. if self.__base == "+": # add
  353. base = size/2
  354. elif self.__base == "-": # sub
  355. base = size/2*-1
  356. if self.__xtype == "sinus":
  357. v = math.sin( rad )
  358. v/=2
  359. elif self.__xtype == "cosinus":
  360. v = math.cos( rad )
  361. if self.__base == "+": # add
  362. size *= -1
  363. v/=2
  364. elif self.__xtype == "rnd":
  365. #base = 0
  366. if self.__angel > 90 and self.__angel <=270:
  367. v=1
  368. else:
  369. v=0
  370. #if count != self.count and v: # % 2 == 0:#!= self.count:
  371. # #self.__offset = random.randint(0,1024)# /1024
  372. # self.__master._shuffle()
  373. if count != self.count and v == 0: # and v: # % 2 == 0:#!= self.count:
  374. self.__master.next(self)#,count)
  375. #self.__master.next(self)#,count)
  376. self.__offset = self.__master.get(self,count)
  377. base = 0
  378. if self.__base == "-": # sub
  379. if self.__invert:
  380. v = 1-v
  381. #base = -size
  382. size *=-1
  383. v *=-1
  384. elif self.__base == "+": # sub
  385. if self.__invert:
  386. v = v-1
  387. else:
  388. v = (t%1-0.5)
  389. elif self.__xtype == "on":
  390. #base = 0
  391. if self.__angel > 90 and self.__angel <=270:
  392. v=1
  393. else:
  394. v=0
  395. base = 0
  396. if self.__base == "-": # sub
  397. if self.__invert:
  398. v = 1-v
  399. #base = -size
  400. size *=-1
  401. v *=-1
  402. elif self.__base == "+": # sub
  403. if self.__invert:
  404. v = v-1
  405. else:
  406. v = (t%1-0.5)
  407. elif self.__xtype == "ramp" or self.__xtype == "ramp":
  408. v = (t%1)
  409. base = 0
  410. if self.__base == "-": # sub
  411. if self.__invert:
  412. v = 1-v
  413. #base = -size
  414. size *=-1
  415. v *=-1
  416. elif self.__base == "+": # sub
  417. if self.__invert:
  418. v = v-1
  419. else:
  420. v = (t%1-0.5)
  421. elif self.__xtype == "ramp2" or self.__xtype == "bump2":
  422. v = (t%1)
  423. v = 1-v
  424. if v == 1:
  425. v=0
  426. base = 0
  427. if self.__base == "-": # sub
  428. if self.__invert:
  429. v = 1-v
  430. #base = -size
  431. size *=-1
  432. v *=-1
  433. elif self.__base == "+": # sub
  434. if self.__invert:
  435. v = v-1
  436. else:
  437. v = (t%1-0.5)
  438. elif self.__xtype == "fade":
  439. x = t * 2
  440. if x > 1:
  441. x = 2-x
  442. x -= 0.5
  443. v = x*2
  444. #base /= 2
  445. #base *=2
  446. if self.__base == "+": # add
  447. pass#base /= 2
  448. else:
  449. v *= -1
  450. v/=2
  451. if self.__invert:
  452. v *=-1
  453. #if self.__fade_in_master < 255:
  454. # self.__fade_in_master += v*size
  455. out = v *size +base
  456. self.out = out
  457. self.count = count
  458. out = out * size_master.val(self.__master_id) # master
  459. out = out * exec_size_master.val(self._exec_id) # master
  460. #* (self.__fade_in_master /255.)
  461. return out
  462. class DMXCH(object):
  463. def __init__(self,dmx=-1):
  464. self._base_value = 0
  465. self._fade = None
  466. self._fx = [None,None] # None
  467. self._fx_value = 0
  468. self._dmx = dmx
  469. self._dmx_fine = 0
  470. self._flash = None
  471. self._flash_fx = None
  472. self._flash_fx_value = 0
  473. self._last_val = None
  474. self._exec_id = None
  475. #self.next(clock)
  476. #print("init",self)
  477. def fade(self,target,ftime=0,clock=0,delay=0):
  478. if target != self._base_value:
  479. try:
  480. target = float(target)
  481. self._fade = Fade(self._base_value,target,ftime=ftime,clock=clock,delay=delay)
  482. #self._fade.next()
  483. #self._fade.next()
  484. except Exception as e:
  485. print( "Except:fade",e,target,ftime,clock)
  486. self.next(clock)
  487. #print("init fade",self)
  488. def fx(self,xtype="sinus",size=40,speed=40,invert=0,width=100,start=0,offset=0,base="", clock=0,master=None):
  489. print([self,xtype,size,speed,start,offset,base, clock])
  490. self._fx[0] = self._fx[1]
  491. if str(xtype).lower() == "off":
  492. fx_value = self._fx_value
  493. if fx_value != 0:
  494. cprint("???????______ FX OFF AS FADE",fx_value,0,255)
  495. self._fx[1] = Fade(fx_value,0,ftime=0.5,clock=clock)
  496. else:
  497. self._fx[1] = None
  498. self._fx_value = 0
  499. else:
  500. self._fx[1] = FX(xtype=xtype,size=size,speed=speed,invert=invert,width=width,start=start,offset=offset,base=base,clock=clock,master=master,master_id=1)
  501. self._fx[1].exec_id(self._exec_id)
  502. self.next(clock)
  503. print("init fx",self)
  504. def flash(self,target,ftime=0,clock=0,delay=0):
  505. if str(target).lower() == "off":
  506. self._flash = None
  507. else:#elif target != self._base_value:
  508. try:
  509. target = float(target)
  510. self._flash = Fade(self._last_val,target,ftime=ftime,clock=clock,delay=delay)
  511. except Exception as e:
  512. print( "Except:flash",target,ftime,clock,__name__,e,)
  513. self.next(clock)
  514. #print("init flush",self)
  515. def flash_fx(self,xtype="sinus",size=40,speed=40,invert=0,width=100,start=0,offset=0,base="",clock=0,master=None):
  516. if str(xtype).lower() == "off":
  517. fx_value = self._fx_value
  518. self._flash_fx = None
  519. self._flash_fx_value = 0
  520. else:
  521. self._flash_fx = FX(xtype=xtype,size=size,speed=speed,invert=invert,width=width,start=start,offset=offset,base=base,clock=clock,master=master,master_id=0)
  522. self._flash_fx.exec_id(self._exec_id)
  523. self.next(clock)
  524. #print("init flash_fx",self)
  525. def fx_ctl(self,cmd=""): #start,stop,off
  526. pass
  527. def __str__(self):
  528. return self.__repr__()
  529. def exec_id(self,_id=None):
  530. if type(id) is not type(None):
  531. self._exec_id = _id
  532. return self._exec_id
  533. def __repr__(self):
  534. return "<BUFFER {} {} v:{:0.2f} EXEC:{}> fx:[{}] fd:{}".format(self._dmx,self._dmx_fine, self._last_val,self._exec_id,self._fx,self._fade)
  535. def fade_ctl(self,cmd=""): #start,stop,backw,fwd,bounce
  536. pass
  537. def next(self,clock=0):
  538. value = self._base_value
  539. if self._last_val is None:
  540. self._last_val = value
  541. fx_value = self._fx_value
  542. if self._flash is not None:
  543. value = self._flash.next(clock)
  544. #flicker bug ?!
  545. value = self._flash.next(clock)
  546. fx_value = 0
  547. elif self._fade is not None:#is Fade:# is Fade:
  548. self._base_value = self._fade.next(clock)
  549. self._base_value = self._fade.next(clock) #flicker bug ?!
  550. value = self._base_value
  551. if self._flash_fx is not None:# is FX:
  552. fx_value = self._flash_fx.next(clock)
  553. else:
  554. self._fx_value = 0
  555. if self._fx[-1] is not None and self._flash is None:# is FX:
  556. self._fx_value += self._fx[-1].next(clock)
  557. fx_value = self._fx_value
  558. self._last_val = value + fx_value
  559. #out = self._last_val * htp_master.master_by_dmx(self._dmx)
  560. out = self._last_val
  561. return out
  562. Bdmx = []
  563. for i in range(512*5+1):
  564. Bdmx.append( DMXCH(i) )
  565. #print(type(dmx[i]))
  566. _id = 1
  567. def split_cmd(data):
  568. if "cmd" in data:
  569. cmd = data["cmd"]
  570. #print("cmd",cmd)
  571. if "," in cmd:
  572. cmds = cmd.split(",")
  573. else:
  574. cmds = [cmd]
  575. return cmds
  576. class VDMX():
  577. """functional implementation as class for namespace encapsulation
  578. """
  579. def __init__(self):
  580. self.data = OrderedDict()
  581. self.data[4] = {"DMX":[21,22,23],"VALUE":255, "LIMIT":255} #,"DMXCH":DMXCH("V4")}
  582. for k,v in self.data.items():
  583. pass
  584. #dmxch = v["DMXCH"]
  585. #dmxch.fade(10,0)
  586. #dmxch.fx(size=200,speed=200,base="-") #self,xtype="sinus",size=40,speed=40,invert=0,width=100,start=0,offset=0,base="", clock=0,master=None):
  587. def _list_by_dmx(self,_dmx=0):
  588. data = OrderedDict()
  589. for i,link in self.data.items(): # enumerate(self.data):
  590. if _dmx in link["DMX"]:
  591. #print( "_list_by_dmx",i,link)
  592. data[i] = link
  593. return data
  594. def dmx_by_id(self,_id=0):
  595. #print("dmx by master-id:",_id)
  596. if _id in self.data:
  597. for i,link in self.data[_id].items():
  598. #print("dmx_by_id", i,link)
  599. return (i,link)
  600. return 0,{}
  601. def by_dmx(self,clock,dmx):
  602. #print("master of dmx:",dmx)
  603. val=0
  604. flag = 0
  605. data = self._list_by_dmx(dmx)
  606. for i,row in data.items():
  607. if "DMXCH" not in row:
  608. row["DMXCH"] = DMXCH("V{}".format(i))
  609. row["DMXCH"].fade(255,0)
  610. v = row["DMXCH"].next(clock)
  611. #row["DMXCH"].fade(200,20)
  612. if v >= val:
  613. val = v
  614. flag = 1
  615. out = 1.
  616. if val > 255:
  617. val = 255
  618. if flag:
  619. out = val/255.
  620. else:
  621. out = 1.
  622. return out
  623. vdmx = VDMX()
  624. class HTP_MASTER():
  625. """functional implementation as class for namespace encapsulation
  626. """
  627. def __init__(self):
  628. self.data = OrderedDict()
  629. #self.data[1] = {"DMX":[1,2,3],"VALUE":80, "LIMIT":255}
  630. #self.data[2] = {"DMX":[12,13,22],"VALUE":70, "LIMIT":255}
  631. #self.data[3] = {"DMX":[22,23,24],"VALUE":99, "LIMIT":255}
  632. self.data[4] = {"DMX":[22,23,24],"VALUE":99, "LIMIT":255,"DMXCH":DMXCH(4)}
  633. def _list_by_dmx(self,_dmx=0):
  634. data = OrderedDict()
  635. for i,link in self.data.items(): # enumerate(self.data):
  636. if _dmx in link["DMX"]:
  637. #print( "_list_by_dmx",i,link)
  638. data[i] = link
  639. return data
  640. def dmx_by_id(self,_id=0):
  641. #print("dmx by master-id:",_id)
  642. if _id in self.data:
  643. for i,link in self.data[_id].items():
  644. #print("dmx_by_id", i,link)
  645. return (i,link)
  646. return 0,{}
  647. def val_by_dmx(self,dmx=0):
  648. #print("master of dmx:",dmx)
  649. val=0
  650. flag = 0
  651. data = self._list_by_dmx(dmx)
  652. for i,link in data.items():
  653. #print("master_by_dmx", i,link)
  654. if link["VALUE"] > val:
  655. #print("master_by_dmx", i,link)
  656. val = link["VALUE"]
  657. flag=1
  658. out = 1.
  659. if flag:
  660. out = val/255.
  661. return out
  662. htp_master = HTP_MASTER()
  663. #htp_master.data[_id] = {"DMX":[1,2,3],"VALUE":80, "LIMIT":255,"DMXCH":DMXCH()}
  664. class Main():
  665. def __init__(self):
  666. #artnet = ANN.ArtNetNode(to="127.0.0.1",port=6555,univ=12)
  667. #artnet = ANN.ArtNetNode(to="127.0.0.1",port=6555,univ=0)
  668. #artnet = ANN.ArtNetNode(to="2.0.0.255",univ=0)
  669. #artnet = ANN.ArtNetNode(to="10.10.10.255",univ=1)
  670. self.artnet = {}
  671. #self.artnet["0"] = ANN.ArtNetNode(to="10.10.10.255",univ=0)
  672. #self.artnet["0"].dmx[512-1] = 10
  673. #self.artnet["1"] = ANN.ArtNetNode(to="10.10.10.255",univ=1)
  674. #self.artnet["1"].dmx[512-1] = 11
  675. self.fx = {} # key is dmx address
  676. def loop(self):
  677. #dmx[205] = 255 #205 BLUE
  678. #self.artnet.send()
  679. xx = [0]*512
  680. #artnet = self.artnet["0"]
  681. #artnet.dmx = xx# [:] #dmx #[0]*512
  682. ii = 0
  683. old_univ = -1
  684. xx = [0]*512
  685. for ii,dmxch in enumerate(Bdmx):
  686. i = ii%512
  687. univ = ii//512
  688. if str(univ) not in self.artnet:
  689. print("add uiv",univ)
  690. self.artnet[str(univ)] = ANN.ArtNetNode(to="10.10.10.255",univ=univ)
  691. #self.artnet[str(univ)].dmx[512-1] = 100+univ
  692. if univ != old_univ:
  693. old_univ = univ
  694. #print("UNIV",ii/512)
  695. try:
  696. artnet.next()
  697. except:pass
  698. artnet = self.artnet[str(univ)]
  699. artnet.dmx = [0]*512
  700. while 1:
  701. t = clock.time()
  702. ii = 0
  703. old_univ = -1
  704. xx = [0]*512
  705. for ii,dmxch in enumerate(Bdmx):
  706. i = ii%512
  707. univ = ii//512
  708. if str(univ) not in self.artnet:
  709. print("add uiv",univ)
  710. self.artnet[str(univ)] = ANN.ArtNetNode(to="10.10.10.255",univ=univ)
  711. #self.artnet[str(univ)].dmx[512-1] = 100+univ
  712. if univ != old_univ:
  713. old_univ = univ
  714. #print("UNIV",ii/512)
  715. try:
  716. artnet.next()
  717. except:pass
  718. artnet = self.artnet[str(univ)]
  719. #artnet.dmx = xx
  720. old_univ = -1
  721. xx = [0]*512
  722. for ii,dmxch in enumerate(Bdmx):
  723. i = ii%512
  724. univ = ii//512
  725. if univ != old_univ:
  726. old_univ = univ
  727. artnet = self.artnet[str(univ)]
  728. xx = artnet.dmx
  729. v = dmxch.next(t)
  730. vv = vdmx.by_dmx(clock=i,dmx=ii+1)
  731. v = v*vv # disable v-master
  732. xx[i] = int(v)
  733. old_univ = -1
  734. xx = [0]*512
  735. for ii,dmxch in enumerate(Bdmx): #fine loop
  736. i = ii%512
  737. univ = ii//512
  738. if univ != old_univ:
  739. artnet = self.artnet[str(univ)]
  740. xx = artnet.dmx# = xx
  741. v = dmxch.next(t)
  742. vv = vdmx.by_dmx(clock=i,dmx=ii+1)
  743. v = v*vv # disable v-master
  744. #xx[i] = int(v)
  745. dmx_fine = dmxch._dmx_fine
  746. if dmx_fine > 0:
  747. vf = int(v%1*255)
  748. #print(dmx_fine,end=" ")
  749. dmx_fine = dmx_fine%512
  750. #print(dmx_fine,end=" ")
  751. #print(int(v),end=" ")
  752. #print(vf,end=" ")
  753. #print()
  754. #univ = ii//512
  755. try:
  756. #xx[dmx_fine+1] = 9# int(v%1*255)
  757. if v >= 255:
  758. xx[dmx_fine-1] = 255
  759. elif v < 0:
  760. xx[dmx_fine-1] = 0
  761. else:
  762. xx[dmx_fine-1] = int(v%1*255)
  763. except Exception as e:
  764. print("E dmx_fine",e,dmx_fine)
  765. try:
  766. artnet.next()
  767. except:pass
  768. time.sleep(0.01)
  769. main = Main()
  770. if __run_main:
  771. #thread.start_new_thread(artnet_loop,())
  772. thread.start_new_thread(main.loop,())
  773. def JCB(data): #json client input
  774. t_start = time.time()
  775. #jdatas = data["cmd"].split("\x00")
  776. jdatas = [data["cmd"]]
  777. c = clock.time()
  778. c = float(c)
  779. print("JCB",round(c,2))
  780. ftime = 0
  781. delay = 0
  782. for j in jdatas:
  783. master_fx = MASTER_FX()
  784. if not j:
  785. continue
  786. try:
  787. cprint("JCB::")#,j)
  788. jdata = j #jdatas[j]
  789. jtxt = jdata
  790. #jtxt = zlib.decompress(jtxt) #jtxt.decode())
  791. jtxt = str(jtxt,"UTF-8")
  792. cmds = json.loads(jtxt)
  793. for x in cmds:
  794. #cprint(int(clock.time()*1000)/1000,end=" ",color="yellow")#time.time())
  795. #cprint("json", x,type(x),color="yellow")#,cmds[x])
  796. if "CMD" in x:
  797. print("CMD:",x)
  798. if "EXEC-SPEED-MASTER" == x["CMD"]:
  799. exec_speed_master.val(x["NR"],x["VALUE"])
  800. if "EXEC-SIZE-MASTER" == x["CMD"]:
  801. exec_size_master.val(x["NR"],x["VALUE"])
  802. if "EXEC-OFFSET-MASTER" == x["CMD"]:
  803. exec_offset_master.val(x["NR"],x["VALUE"])
  804. if "SPEED-MASTER" == x["CMD"]:
  805. speed_master.val(x["NR"],x["VALUE"])
  806. if x["NR"] == 2:
  807. pass
  808. #vdmx.data[4]["DMXCH"].fade(x["VALUE"],3)#,clock=clock.time())
  809. if x["NR"] == 3:
  810. pass
  811. #vdmx.data[4]["DMXCH"].fx(size=255,speed=x["VALUE"],base="-",offset=0)#xtype=xtype,size=size,speed=speed,invert=invert,width=width,start=start,offset=offset,base=base,clock=c,master=master_fx)
  812. if "SIZE-MASTER" == x["CMD"]:
  813. size_master.val(x["NR"],x["VALUE"])
  814. else:
  815. if "DMX" in x:
  816. DMX = int(x["DMX"])
  817. else:continue
  818. if DMX > 0:
  819. DMX -=1
  820. else:continue
  821. exec_id = None
  822. if "EXEC" in x:
  823. exec_id = x["EXEC"]
  824. if "VALUE" in x:# and x["VALUE"] is not None:
  825. v = x["VALUE"]
  826. else:continue
  827. if "FX" in x:# and x["VALUE"] is not None:
  828. fx = x["FX"]
  829. else:fx=""
  830. if "FX2" in x:# and x["VALUE"] is not None:
  831. fx2 = x["FX2"]
  832. else:fx2={}
  833. if "FADE" in x:
  834. ftime = x["FADE"]
  835. else:ftime=0
  836. if "DELAY" in x:
  837. delay = x["DELAY"]
  838. else:delay=0
  839. if "DMX-FINE" in x:
  840. Bdmx[DMX]._dmx_fine = int(x["DMX-FINE"])
  841. #cprint("DMX-FINE",Bdmx[DMX],color="blue")
  842. if len(Bdmx) < DMX:
  843. continue
  844. Bdmx[DMX].exec_id(exec_id)
  845. if v is not None:
  846. if "FLASH" in x:
  847. #print("FLASH")
  848. Bdmx[DMX].flash(target=v,ftime=ftime, clock=c,delay=delay)
  849. else:
  850. #print("FADE")
  851. Bdmx[DMX].fade(target=v,ftime=ftime, clock=c,delay=delay)
  852. if type(fx2) is dict and fx2:
  853. #cprint("FX2",DMX,fx2,color="green")
  854. xtype="fade"
  855. size = 10
  856. speed = 10
  857. start = 0
  858. offset= 0
  859. width=100
  860. invert=0
  861. base = "-"
  862. if "TYPE" in fx2:
  863. xtype = fx2["TYPE"]
  864. if "SIZE" in fx2:
  865. size = fx2["SIZE"]
  866. if "SPEED" in fx2:
  867. speed = fx2["SPEED"]
  868. if "OFFSET" in fx2:
  869. offset = fx2["OFFSET"]
  870. if "BASE" in fx2:
  871. base = fx2["BASE"]
  872. if "INVERT" in fx2:
  873. invert = fx2["INVERT"]
  874. if "WIDTH" in fx2:
  875. width = fx2["WIDTH"]
  876. if "off" == x["VALUE"]: #fix fx flash off
  877. xtype= "off"
  878. if "alloff" == xtype.lower():
  879. for i in Bdmx:
  880. if i is not None:
  881. i.flash_fx(xtype="off",clock=c)
  882. i.fx(xtype="off",clock=c)
  883. if "FLASH" in x:
  884. Bdmx[DMX].flash_fx(xtype=xtype,size=size,speed=speed,invert=invert,width=width,start=start,offset=offset,base=base,clock=c,master=master_fx)
  885. else:
  886. Bdmx[DMX].fx(xtype=xtype,size=size,speed=speed,invert=invert,width=width,start=start,offset=offset,base=base,clock=c,master=master_fx)
  887. elif type(fx) is str and fx: # old fx like sinus:200:12:244
  888. ccm = str(DMX+1)+":"+fx
  889. print("fx",ccm)
  890. if "FLASH" in x:
  891. CB({"cmd":"fxf"+ccm})
  892. else:
  893. CB({"cmd":"fx"+ccm})
  894. cprint("{:0.04} sec.".format(time.time()-t_start),color="yellow")
  895. cprint("{:0.04} t.".format(time.time()),color="yellow")
  896. except Exception as e:
  897. cprint("EXCEPTION JCB",e,color="red")
  898. cprint("----",str(jdata)[:150],"...",color="red")
  899. cprint("Error on line {}".format(sys.exc_info()[-1].tb_lineno),color="red")
  900. cprint()
  901. cprint("{:0.04} sec.".format(time.time()-t_start),color="yellow")
  902. cprint("{:0.04} t.".format(time.time()),color="yellow")
  903. def CB(data): # raw/text client input
  904. #print("CB",data)
  905. cmds = split_cmd(data)
  906. c = clock.time()
  907. c = float(c)
  908. ftime = 0
  909. delay = 0
  910. for xcmd in cmds:
  911. if xcmd:
  912. cprint("CB",xcmd,end=" ")
  913. pass
  914. else:
  915. continue
  916. if xcmd.startswith("fxf"):
  917. xxcmd=xcmd[3:].split(":")
  918. #print("fxf:",xxcmd)
  919. if "alloff" == xxcmd[1].lower():
  920. for i in Bdmx:
  921. if i is not None:
  922. i.flash_fx(xtype="off",clock=c)
  923. l = xxcmd
  924. try:
  925. xtype=""
  926. size=40
  927. speed=100
  928. start=0
  929. offset=0
  930. base=""
  931. k=int(l[0])-1
  932. xtype=l[1]
  933. if len(l) >= 3:
  934. try:size=int(l[2])
  935. except:pass
  936. if len(l) >= 4:
  937. try:speed=int(l[3])
  938. except:pass
  939. if len(l) >= 5:
  940. try:start=int(l[4])
  941. except:pass
  942. if len(l) >= 6:
  943. try:offset=int(l[5])
  944. except:pass
  945. if len(l) >= 7:
  946. try:base=l[6]
  947. except:pass
  948. if len(Bdmx) > k:
  949. #Bdmx[k].fade(target=v,ftime=t, clock=c)
  950. Bdmx[k].flash_fx(xtype=xtype,size=size,speed=speed,start=start,offset=offset,base=base,clock=c)
  951. except Exception as e:
  952. print("EXCEPTION IN FX",e)
  953. print("Error on line {}".format(sys.exc_info()[-1].tb_lineno))
  954. elif xcmd.startswith("fx"):
  955. xxcmd=xcmd[2:].split(":")
  956. print("DMX:",xxcmd)
  957. if len(xxcmd) < 2:
  958. print("xxcmd err",xxcmd,xcmd)
  959. continue
  960. if "alloff" == xxcmd[1].lower():
  961. for i in Bdmx:
  962. i.fx(xtype="off",clock=c)
  963. l = xxcmd
  964. try:
  965. xtype=""
  966. size=40
  967. speed=100
  968. start=0
  969. offset=0
  970. base=""
  971. k=int(l[0])-1
  972. xtype=l[1]
  973. if len(l) >= 3:
  974. try:size=int(l[2])
  975. except:pass
  976. if len(l) >= 4:
  977. try:speed=int(l[3])
  978. except:pass
  979. if len(l) >= 5:
  980. try:start=int(l[4])
  981. except:pass
  982. if len(l) >= 6:
  983. try:offset=int(l[5])
  984. except:pass
  985. if len(l) >= 7:
  986. try:base=l[6]
  987. except:pass
  988. if len(Bdmx) > k:
  989. #Bdmx[k].fade(target=v,ftime=t, clock=c)
  990. Bdmx[k].fx(xtype=xtype,size=size,speed=speed,start=start,offset=offset,base=base,clock=c)
  991. except Exception as e:
  992. print("EXCEPTION IN FX",xcmd,e)
  993. print("Error on line {}".format(sys.exc_info()[-1].tb_lineno))
  994. if __run_main:
  995. #jchat = chat.CMD(CB,port=50001) # server listener
  996. #thread.start_new_thread(jchat.poll,())
  997. chat.cmd(JCB) # server listener
  998. #chat.cmd(JCB,port=50001) # server listener
  999. #input("END")