_console.py 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  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("DMXCH.fx",[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. #print("set exec_id",_id)
  533. return self._exec_id
  534. def __repr__(self):
  535. return "<BUFFER {} {} v:{:0.2f} EXEC:{}> fx:[{}] fd:{}".format(self._dmx,self._dmx_fine, self._last_val,self._exec_id,self._fx,self._fade)
  536. def fade_ctl(self,cmd=""): #start,stop,backw,fwd,bounce
  537. pass
  538. def next(self,clock=0):
  539. try:
  540. self._next(clock)
  541. except Exception as e:
  542. cprint("Exception DMXCH.next()" ,e)
  543. out = self._last_val
  544. return out
  545. def _next(self,clock=0):
  546. value = self._base_value
  547. if self._last_val is None:
  548. self._last_val = value
  549. fx_value = self._fx_value
  550. if self._flash is not None:
  551. value = self._flash.next(clock)
  552. #flicker bug ?!
  553. value = self._flash.next(clock)
  554. fx_value = 0
  555. elif self._fade is not None:#is Fade:# is Fade:
  556. self._base_value = self._fade.next(clock)
  557. self._base_value = self._fade.next(clock) #flicker bug ?!
  558. value = self._base_value
  559. if self._flash_fx is not None:# is FX:
  560. fx_value = self._flash_fx.next(clock)
  561. else:
  562. self._fx_value = 0
  563. if self._fx[-1] is not None and self._flash is None:# is FX:
  564. self._fx_value += self._fx[-1].next(clock)
  565. fx_value = self._fx_value
  566. self._last_val = value + fx_value
  567. #out = self._last_val * htp_master.master_by_dmx(self._dmx)
  568. out = self._last_val
  569. return out
  570. Bdmx = []
  571. for i in range(512*5+1):
  572. Bdmx.append( DMXCH(i) )
  573. #print(type(dmx[i]))
  574. _id = 1
  575. def split_cmd(data):
  576. if "cmd" in data:
  577. cmd = data["cmd"]
  578. #print("cmd",cmd)
  579. if "," in cmd:
  580. cmds = cmd.split(",")
  581. else:
  582. cmds = [cmd]
  583. return cmds
  584. class VDMX():
  585. """functional implementation as class for namespace encapsulation
  586. """
  587. def __init__(self):
  588. self.data = OrderedDict()
  589. self.data[4] = {"DMX":[21,22,23],"VALUE":255, "LIMIT":255} #,"DMXCH":DMXCH("V4")}
  590. for k,v in self.data.items():
  591. pass
  592. #dmxch = v["DMXCH"]
  593. #dmxch.fade(10,0)
  594. #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):
  595. def _list_by_dmx(self,_dmx=0):
  596. data = OrderedDict()
  597. for i,link in self.data.items(): # enumerate(self.data):
  598. if _dmx in link["DMX"]:
  599. #print( "_list_by_dmx",i,link)
  600. data[i] = link
  601. return data
  602. def dmx_by_id(self,_id=0):
  603. #print("dmx by master-id:",_id)
  604. if _id in self.data:
  605. for i,link in self.data[_id].items():
  606. #print("dmx_by_id", i,link)
  607. return (i,link)
  608. return 0,{}
  609. def by_dmx(self,clock,dmx):
  610. #print("master of dmx:",dmx)
  611. val=0
  612. flag = 0
  613. data = self._list_by_dmx(dmx)
  614. for i,row in data.items():
  615. if "DMXCH" not in row:
  616. row["DMXCH"] = DMXCH("V{}".format(i))
  617. row["DMXCH"].fade(255,0)
  618. v = row["DMXCH"].next(clock)
  619. #row["DMXCH"].fade(200,20)
  620. if v >= val:
  621. val = v
  622. flag = 1
  623. out = 1.
  624. if val > 255:
  625. val = 255
  626. if flag:
  627. out = val/255.
  628. else:
  629. out = 1.
  630. return out
  631. vdmx = VDMX()
  632. class HTP_MASTER():
  633. """functional implementation as class for namespace encapsulation
  634. """
  635. def __init__(self):
  636. self.data = OrderedDict()
  637. #self.data[1] = {"DMX":[1,2,3],"VALUE":80, "LIMIT":255}
  638. #self.data[2] = {"DMX":[12,13,22],"VALUE":70, "LIMIT":255}
  639. #self.data[3] = {"DMX":[22,23,24],"VALUE":99, "LIMIT":255}
  640. self.data[4] = {"DMX":[22,23,24],"VALUE":99, "LIMIT":255,"DMXCH":DMXCH(4)}
  641. def _list_by_dmx(self,_dmx=0):
  642. data = OrderedDict()
  643. for i,link in self.data.items(): # enumerate(self.data):
  644. if _dmx in link["DMX"]:
  645. #print( "_list_by_dmx",i,link)
  646. data[i] = link
  647. return data
  648. def dmx_by_id(self,_id=0):
  649. #print("dmx by master-id:",_id)
  650. if _id in self.data:
  651. for i,link in self.data[_id].items():
  652. #print("dmx_by_id", i,link)
  653. return (i,link)
  654. return 0,{}
  655. def val_by_dmx(self,dmx=0):
  656. #print("master of dmx:",dmx)
  657. val=0
  658. flag = 0
  659. data = self._list_by_dmx(dmx)
  660. for i,link in data.items():
  661. #print("master_by_dmx", i,link)
  662. if link["VALUE"] > val:
  663. #print("master_by_dmx", i,link)
  664. val = link["VALUE"]
  665. flag=1
  666. out = 1.
  667. if flag:
  668. out = val/255.
  669. return out
  670. htp_master = HTP_MASTER()
  671. #htp_master.data[_id] = {"DMX":[1,2,3],"VALUE":80, "LIMIT":255,"DMXCH":DMXCH()}
  672. class Main():
  673. def __init__(self):
  674. #artnet = ANN.ArtNetNode(to="127.0.0.1",port=6555,univ=12)
  675. #artnet = ANN.ArtNetNode(to="127.0.0.1",port=6555,univ=0)
  676. #artnet = ANN.ArtNetNode(to="2.0.0.255",univ=0)
  677. #artnet = ANN.ArtNetNode(to="10.10.10.255",univ=1)
  678. self.artnet = {}
  679. #self.artnet["0"] = ANN.ArtNetNode(to="10.10.10.255",univ=0)
  680. #self.artnet["0"].dmx[512-1] = 10
  681. #self.artnet["1"] = ANN.ArtNetNode(to="10.10.10.255",univ=1)
  682. #self.artnet["1"].dmx[512-1] = 11
  683. self.fx = {} # key is dmx address
  684. self.lock = thread.allocate_lock()
  685. def loop(self):
  686. #dmx[205] = 255 #205 BLUE
  687. #self.artnet.send()
  688. xx = [0]*512
  689. #artnet = self.artnet["0"]
  690. #artnet.dmx = xx# [:] #dmx #[0]*512
  691. ii = 0
  692. old_univ = -1
  693. xx = [0]*512
  694. for ii,dmxch in enumerate(Bdmx):
  695. i = ii%512
  696. univ = ii//512
  697. if str(univ) not in self.artnet:
  698. print("add uiv",univ)
  699. self.artnet[str(univ)] = ANN.ArtNetNode(to="10.10.10.255",univ=univ)
  700. #self.artnet[str(univ)].dmx[512-1] = 100+univ
  701. if univ != old_univ:
  702. old_univ = univ
  703. #print("UNIV",ii/512)
  704. try:
  705. artnet.next()
  706. except:pass
  707. artnet = self.artnet[str(univ)]
  708. artnet.dmx = [0]*512
  709. while 1:
  710. self.lock.acquire_lock()
  711. t = clock.time()
  712. ii = 0
  713. old_univ = -1
  714. xx = [0]*512
  715. for ii,dmxch in enumerate(Bdmx):
  716. i = ii%512
  717. univ = ii//512
  718. if str(univ) not in self.artnet:
  719. print("add uiv",univ)
  720. self.artnet[str(univ)] = ANN.ArtNetNode(to="10.10.10.255",univ=univ)
  721. #self.artnet[str(univ)].dmx[512-1] = 100+univ
  722. if univ != old_univ:
  723. old_univ = univ
  724. #print("UNIV",ii/512)
  725. try:
  726. artnet.next()
  727. except:pass
  728. artnet = self.artnet[str(univ)]
  729. #artnet.dmx = xx
  730. old_univ = -1
  731. xx = [0]*512
  732. for ii,dmxch in enumerate(Bdmx):
  733. i = ii%512
  734. univ = ii//512
  735. if univ != old_univ:
  736. old_univ = univ
  737. artnet = self.artnet[str(univ)]
  738. xx = artnet.dmx
  739. v = dmxch.next(t)
  740. vv = vdmx.by_dmx(clock=i,dmx=ii+1)
  741. try:
  742. v = v*vv # disable v-master
  743. except Exception as e:
  744. cprint("Exception v*vv",[v,vv],e)
  745. continue
  746. xx[i] = int(v)
  747. old_univ = -1
  748. xx = [0]*512
  749. for ii,dmxch in enumerate(Bdmx): #fine loop
  750. i = ii%512
  751. univ = ii//512
  752. if univ != old_univ:
  753. artnet = self.artnet[str(univ)]
  754. xx = artnet.dmx# = xx
  755. v = dmxch.next(t)
  756. vv = vdmx.by_dmx(clock=i,dmx=ii+1)
  757. try:
  758. v = v*vv # disable v-master
  759. except Exception as e:
  760. cprint("Exception v*vv",[v,vv],e)
  761. continue
  762. #xx[i] = int(v)
  763. dmx_fine = dmxch._dmx_fine
  764. if dmx_fine > 0:
  765. vf = int(v%1*255)
  766. #print(dmx_fine,end=" ")
  767. dmx_fine = dmx_fine%512
  768. #print(dmx_fine,end=" ")
  769. #print(int(v),end=" ")
  770. #print(vf,end=" ")
  771. #print()
  772. #univ = ii//512
  773. try:
  774. #xx[dmx_fine+1] = 9# int(v%1*255)
  775. if v >= 255:
  776. xx[dmx_fine-1] = 255
  777. elif v < 0:
  778. xx[dmx_fine-1] = 0
  779. else:
  780. xx[dmx_fine-1] = int(v%1*255)
  781. except Exception as e:
  782. print("E dmx_fine",e,dmx_fine)
  783. try:
  784. artnet.next()
  785. except:pass
  786. self.lock.release_lock()
  787. #self.lock.acquire_lock()
  788. time.sleep(0.01)
  789. main = Main()
  790. if __run_main:
  791. #thread.start_new_thread(artnet_loop,())
  792. thread.start_new_thread(main.loop,())
  793. def JCB(data): #json client input
  794. t_start = time.time()
  795. #jdatas = data["cmd"].split("\x00")
  796. jdatas = [data["cmd"]]
  797. c = clock.time()
  798. c = float(c)
  799. #print("JCB",round(c,2))
  800. ftime = 0
  801. delay = 0
  802. for j in jdatas:
  803. master_fx = MASTER_FX()
  804. if not j:
  805. continue
  806. try:
  807. #cprint("JCB::")#,j)
  808. jdata = j #jdatas[j]
  809. jtxt = jdata
  810. #jtxt = zlib.decompress(jtxt) #jtxt.decode())
  811. jtxt = str(jtxt,"UTF-8")
  812. cmds = json.loads(jtxt)
  813. out = {}
  814. for x in cmds:
  815. #cprint(int(clock.time()*1000)/1000,end=" ",color="yellow")#time.time())
  816. #cprint("json", x,type(x),color="yellow")#,cmds[x])
  817. if "CMD" in x:
  818. print("CMD:",x)
  819. if "EXEC-SPEED-MASTER" == x["CMD"]:
  820. exec_speed_master.val(x["NR"],x["VALUE"])
  821. if "EXEC-SIZE-MASTER" == x["CMD"]:
  822. exec_size_master.val(x["NR"],x["VALUE"])
  823. if "EXEC-OFFSET-MASTER" == x["CMD"]:
  824. exec_offset_master.val(x["NR"],x["VALUE"])
  825. if "SPEED-MASTER" == x["CMD"]:
  826. speed_master.val(x["NR"],x["VALUE"])
  827. if x["NR"] == 2:
  828. pass
  829. #vdmx.data[4]["DMXCH"].fade(x["VALUE"],3)#,clock=clock.time())
  830. if x["NR"] == 3:
  831. pass
  832. #vdmx.data[4]["DMXCH"].fx(size=255,speed=x["VALUE"],base="-",offset=0)
  833. #xtype=xtype,size=size,speed=speed,invert=invert,width=width,start=start,offset=offset,base=base,clock=c,master=master_fx)
  834. if "SIZE-MASTER" == x["CMD"]:
  835. size_master.val(x["NR"],x["VALUE"])
  836. else:
  837. if "DMX" in x:
  838. DMX = int(x["DMX"])
  839. else:continue
  840. if DMX > 0:
  841. DMX -=1
  842. else:continue
  843. exec_id = None
  844. if "EXEC" in x:
  845. exec_id = x["EXEC"]
  846. if "VALUE" in x:# and x["VALUE"] is not None:
  847. v = x["VALUE"]
  848. else:continue
  849. if "FX" in x:# and x["VALUE"] is not None:
  850. fx = x["FX"]
  851. else:fx=""
  852. if "FX2" in x:# and x["VALUE"] is not None:
  853. fx2 = x["FX2"]
  854. else:fx2={}
  855. if "FADE" in x:
  856. ftime = x["FADE"]
  857. else:ftime=0
  858. if "DELAY" in x:
  859. delay = x["DELAY"]
  860. else:delay=0
  861. if "DMX-FINE" in x:
  862. Bdmx[DMX]._dmx_fine = int(x["DMX-FINE"])
  863. #cprint("DMX-FINE",Bdmx[DMX],color="blue")
  864. if len(Bdmx) < DMX:
  865. continue
  866. if "FLASH" in x and v == "off" and Bdmx[DMX].exec_id() != exec_id:
  867. #print("STOP",[exec_id,v],Bdmx[DMX].exec_id() )
  868. continue # stop
  869. Bdmx[DMX].exec_id(exec_id)
  870. out[DMX] = {"flash":{},"fade":{},"fx":{},"flash_fx":{}}
  871. if v is not None:
  872. if "FLASH" in x:
  873. #print("FLASH")
  874. #Bdmx[DMX].flash(target=v,ftime=ftime, clock=c,delay=delay)
  875. out[DMX]["flash"] = {"target":v,"ftime":ftime, "clock":c,"delay":delay}
  876. else:
  877. #print("FADE")
  878. #Bdmx[DMX].fade(target=v,ftime=ftime, clock=c,delay=delay)
  879. out[DMX]["fade"] = {"target":v,"ftime":ftime, "clock":c,"delay":delay}
  880. if type(fx2) is dict and fx2:
  881. #cprint("FX2",DMX,fx2,color="green")
  882. xtype="fade"
  883. size = 10
  884. speed = 10
  885. start = 0
  886. offset= 0
  887. width=100
  888. invert=0
  889. base = "-"
  890. if "TYPE" in fx2:
  891. xtype = fx2["TYPE"]
  892. if "SIZE" in fx2:
  893. size = fx2["SIZE"]
  894. if "SPEED" in fx2:
  895. speed = fx2["SPEED"]
  896. if "OFFSET" in fx2:
  897. offset = fx2["OFFSET"]
  898. if "BASE" in fx2:
  899. base = fx2["BASE"]
  900. if "INVERT" in fx2:
  901. invert = fx2["INVERT"]
  902. if "WIDTH" in fx2:
  903. width = fx2["WIDTH"]
  904. if "off" == x["VALUE"]: #fix fx flash off
  905. xtype= "off"
  906. if "alloff" == xtype.lower():
  907. for i in Bdmx:
  908. if i is not None:
  909. i.flash_fx(xtype="off",clock=c)
  910. i.fx(xtype="off",clock=c)
  911. if "FLASH" in x:
  912. #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)
  913. out[DMX]["flash_fx"] = {"xtype":xtype,"size":size,"speed":speed,"invert":invert,"width":width,"start":start,"offset":offset,"base":base,"clock":c,"master":master_fx}
  914. else:
  915. #Bdmx[DMX].fx(xtype=xtype,size=size,speed=speed,invert=invert,width=width,start=start,offset=offset,base=base,clock=c,master=master_fx)
  916. out[DMX]["fx"] = {"xtype":xtype,"size":size,"speed":speed,"invert":invert,"width":width,"start":start,"offset":offset,"base":base,"clock":c,"master":master_fx}
  917. elif type(fx) is str and fx: # old fx like sinus:200:12:244
  918. ccm = str(DMX+1)+":"+fx
  919. print("fx",ccm)
  920. if "FLASH" in x:
  921. CB({"cmd":"fxf"+ccm})
  922. else:
  923. CB({"cmd":"fx"+ccm})
  924. #print("END",[exec_id,v],Bdmx[DMX].exec_id() )
  925. #print("END",[Bdmx[DMX] ])
  926. # ------- ----------------------------------------------------
  927. try: # second loop to sync-start all dmxch's
  928. main.lock.acquire_lock()
  929. # main.lock.release_lock()
  930. for DMX in out:
  931. line = out[DMX]
  932. if out[DMX]["fx"]:
  933. x = out[DMX]["fx"]
  934. Bdmx[DMX].fx(xtype=x["xtype"]
  935. ,size=x["size"]
  936. ,speed=x["speed"]
  937. ,invert=x["invert"]
  938. ,width=x["width"]
  939. ,start=x["start"]
  940. ,offset=x["offset"]
  941. ,base=x["base"]
  942. ,clock=x["clock"]
  943. ,master=x["master"])
  944. if out[DMX]["flash_fx"]:
  945. x = out[DMX]["flash_fx"]
  946. Bdmx[DMX].flash_fx(xtype=x["xtype"]
  947. ,size=x["size"]
  948. ,speed=x["speed"]
  949. ,invert=x["invert"]
  950. ,width=x["width"]
  951. ,start=x["start"]
  952. ,offset=x["offset"]
  953. ,base=x["base"]
  954. ,clock=x["clock"]
  955. ,master=x["master"])
  956. if out[DMX]["flash"]:
  957. x = out[DMX]["flash"]
  958. Bdmx[DMX].flash(target=x["target"]
  959. ,ftime=x["ftime"]
  960. ,clock=x["clock"]
  961. ,delay=x["delay"])
  962. if out[DMX]["fade"]:
  963. x = out[DMX]["fade"]
  964. Bdmx[DMX].fade(target=x["target"]
  965. ,ftime=x["ftime"]
  966. ,clock=x["clock"]
  967. ,delay=x["delay"])
  968. finally:
  969. main.lock.release_lock()
  970. #cprint("{:0.04} sec.".format(time.time()-t_start),color="yellow")
  971. #cprint("{:0.04} t.".format(time.time()),color="yellow")
  972. except Exception as e:
  973. cprint("EXCEPTION JCB",e,color="red")
  974. cprint("----",str(jdata)[:150],"...",color="red")
  975. cprint("Error on line {}".format(sys.exc_info()[-1].tb_lineno),color="red")
  976. raise e
  977. #cprint()
  978. #cprint("{:0.04} sec.".format(time.time()-t_start),color="yellow")
  979. #cprint("{:0.04} t.".format(time.time()),color="yellow")
  980. def CB(data): # raw/text client input
  981. #print("CB",data)
  982. cmds = split_cmd(data)
  983. c = clock.time()
  984. c = float(c)
  985. ftime = 0
  986. delay = 0
  987. for xcmd in cmds:
  988. if xcmd:
  989. cprint("CB",xcmd,end=" ")
  990. pass
  991. else:
  992. continue
  993. if xcmd.startswith("fxf"):
  994. xxcmd=xcmd[3:].split(":")
  995. #print("fxf:",xxcmd)
  996. if "alloff" == xxcmd[1].lower():
  997. for i in Bdmx:
  998. if i is not None:
  999. i.flash_fx(xtype="off",clock=c)
  1000. l = xxcmd
  1001. try:
  1002. xtype=""
  1003. size=40
  1004. speed=100
  1005. start=0
  1006. offset=0
  1007. base=""
  1008. k=int(l[0])-1
  1009. xtype=l[1]
  1010. if len(l) >= 3:
  1011. try:size=int(l[2])
  1012. except:pass
  1013. if len(l) >= 4:
  1014. try:speed=int(l[3])
  1015. except:pass
  1016. if len(l) >= 5:
  1017. try:start=int(l[4])
  1018. except:pass
  1019. if len(l) >= 6:
  1020. try:offset=int(l[5])
  1021. except:pass
  1022. if len(l) >= 7:
  1023. try:base=l[6]
  1024. except:pass
  1025. if len(Bdmx) > k:
  1026. #Bdmx[k].fade(target=v,ftime=t, clock=c)
  1027. Bdmx[k].flash_fx(xtype=xtype,size=size,speed=speed,start=start,offset=offset,base=base,clock=c)
  1028. except Exception as e:
  1029. print("EXCEPTION IN FX",e)
  1030. print("Error on line {}".format(sys.exc_info()[-1].tb_lineno))
  1031. elif xcmd.startswith("fx"):
  1032. xxcmd=xcmd[2:].split(":")
  1033. print("DMX:",xxcmd)
  1034. if len(xxcmd) < 2:
  1035. print("xxcmd err",xxcmd,xcmd)
  1036. continue
  1037. if "alloff" == xxcmd[1].lower():
  1038. for i in Bdmx:
  1039. i.fx(xtype="off",clock=c)
  1040. l = xxcmd
  1041. try:
  1042. xtype=""
  1043. size=40
  1044. speed=100
  1045. start=0
  1046. offset=0
  1047. base=""
  1048. k=int(l[0])-1
  1049. xtype=l[1]
  1050. if len(l) >= 3:
  1051. try:size=int(l[2])
  1052. except:pass
  1053. if len(l) >= 4:
  1054. try:speed=int(l[3])
  1055. except:pass
  1056. if len(l) >= 5:
  1057. try:start=int(l[4])
  1058. except:pass
  1059. if len(l) >= 6:
  1060. try:offset=int(l[5])
  1061. except:pass
  1062. if len(l) >= 7:
  1063. try:base=l[6]
  1064. except:pass
  1065. if len(Bdmx) > k:
  1066. #Bdmx[k].fade(target=v,ftime=t, clock=c)
  1067. Bdmx[k].fx(xtype=xtype,size=size,speed=speed,start=start,offset=offset,base=base,clock=c)
  1068. except Exception as e:
  1069. print("EXCEPTION IN FX",xcmd,e)
  1070. print("Error on line {}".format(sys.exc_info()[-1].tb_lineno))
  1071. if __run_main:
  1072. #jchat = chat.CMD(CB,port=50001) # server listener
  1073. #thread.start_new_thread(jchat.poll,())
  1074. chat.cmd(JCB) # server listener
  1075. #chat.cmd(JCB,port=50001) # server listener
  1076. #input("END")