_console.py 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  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. #thread.start_new_thread
  54. #import lib.motion as motion
  55. #idmx = [0]*512 # incremental dmx
  56. dmx = [0]*512 # absolute dmx data
  57. gcolor = 1
  58. def cprint(*text,color="blue",space=" ",end="\n"):
  59. color = color.lower()
  60. #return 0 #disable print dbg
  61. if not gcolor:
  62. print(text)
  63. return 0
  64. if color == "green":
  65. txt = '\033[92m'
  66. elif color == "red":
  67. txt = '\033[0;31m\033[1m'
  68. elif color == "yellow":
  69. txt = '\033[93m\033[1m'
  70. elif color == "cyan":
  71. txt = '\033[96m'
  72. else:
  73. txt = '\033[94m'
  74. for t in text:
  75. txt += str(t ) +" "
  76. #HEADER = '\033[95m'
  77. #OKBLUE = '\033[94m'
  78. #OKCYAN = '\033[96m'
  79. #OKGREEN = '\033[92m'
  80. #WARNING = '\033[93m'
  81. #FAIL = '\033[91m'
  82. #ENDC = '\033[0m'
  83. #BOLD = '\033[1m'
  84. #UNDERLINE = '\033[4m'
  85. txt += '\033[0m'
  86. print(txt,end=end)
  87. #return txt
  88. def artnet_loop():
  89. #artnet = ANN.ArtNetNode(to="127.0.0.1",port=6555,univ=12)
  90. #artnet = ANN.ArtNetNode(to="127.0.0.1",port=6555,univ=0)
  91. artnet = ANN.ArtNetNode(to="10.10.10.255",univ=0)
  92. #artnet = ANN.ArtNetNode(to="2.0.0.255",univ=0)
  93. #artnet = ANN.ArtNetNode(to="10.10.10.255",univ=1)
  94. #dmx[205] = 255 #205 BLUE
  95. artnet.dmx= dmx #[0]*512
  96. artnet.send()
  97. while 1:
  98. #artnet._test_frame()
  99. artnet.next()
  100. time.sleep(0.001)
  101. class CLOCK():
  102. def __init__(self):
  103. self.__time = 0
  104. self.__start = time.time() # only for debugging
  105. self.__tick = 0.01 # incremental timer drift's on highe cpu load ?
  106. def time(self):
  107. return self.__time
  108. def get_drift(self):
  109. run_time = time.time() - self.__start
  110. tick_time = self.__time # * self.__tick
  111. print( "runtime:{:0.2f} tick_timer:{:0.2f} drift:{:0.2f}".format(run_time,tick_time,run_time-tick_time))
  112. def loop(self):
  113. while 1:
  114. self.__time +=self.__tick
  115. #if int(self.__time*100)/10. % 10 == 0:# self.__time % 2 == 0:
  116. # print( self.get_drift())
  117. #print(self.__time)
  118. #for i in range(10):
  119. time.sleep(self.__tick)
  120. class CLOCK_REAL():
  121. def __init__(self):
  122. self.__time = 0
  123. self.__start = time.time() # only for debugging
  124. self.__tick = 0.001 # incremental timer drift's on highe cpu load ?
  125. def time(self):
  126. self.__time = time.time()
  127. return self.__time
  128. def get_drift(self):
  129. run_time = time.time() - self.__start
  130. tick_time = self.__time # * self.__tick
  131. print( "runtime:{:0.2f} tick_timer:{:0.2f} drift:{:0.2f}".format(run_time,tick_time,run_time-tick_time))
  132. def loop(self):
  133. pass
  134. #clock = CLOCK()
  135. clock = CLOCK_REAL()
  136. if __run_main:
  137. thread.start_new_thread(clock.loop,())
  138. class Fade():
  139. def __init__(self,start,target,ftime,clock,delay=0):
  140. #print("init Fade ",start,target,ftime,clock)
  141. if delay < 0:
  142. delay = 0.0001
  143. if ftime <= 0:
  144. ftime = 0.0001
  145. clock += delay
  146. self.__delay = delay
  147. self.__clock = clock
  148. self.__clock_curr = clock
  149. self.__ftime = ftime
  150. self.__start = start
  151. self.__last = start
  152. self.__target = target
  153. self.abs = 0
  154. self.run = 1
  155. self.end = 0
  156. self.off = 0
  157. #print("INIT", str(self) )
  158. def __str__(self):
  159. return self.__repr__()
  160. def __repr__(self):
  161. return "<FADE Next:{:0.2f} from:{:0.2f} to:{:0.2f} ft:{:0.2f} Clock:{:0.2f} run:{} delay:{:0.2f}>".format(
  162. self.__last, self.__start,self.__target,self.__ftime,self.__clock_curr,self.run,self.__delay )
  163. def next(self,clock=None):
  164. if self.__ftime <= 0 and self.__delay <= 0:
  165. self.__last = self.__target
  166. self.end = 1
  167. self.run = 0
  168. if type(clock) is float or type(clock) is int:#not None:
  169. self.__clock_curr = clock
  170. if self.__target > self.__start:
  171. if self.__last >= self.__target:
  172. self.run = 0
  173. self.end = 1
  174. return self.__target
  175. else:
  176. if self.__last <= self.__target:
  177. self.run = 0
  178. self.end = 1
  179. return self.__target
  180. current = (self.__clock - self.__clock_curr) / self.__ftime
  181. length = self.__start - self.__target
  182. self.__last = self.__start+ length*current
  183. self.run = 1
  184. return self.__last
  185. def ctl(self,cmd="",value=None): # if x-fade cmd="%" value=50
  186. pass
  187. class _MASTER():
  188. def __init__(self,name="None"):
  189. self.__data = {}
  190. self.name = name
  191. def val(self,name,value=None):
  192. _value = 100 #%
  193. name = str(name)
  194. if name not in self.__data:
  195. self.__data[name] = 100
  196. _value = self.__data[name]
  197. if value is not None:
  198. if _value != value:
  199. print(self.name,"CHANGE MASTER:",name,"from:",_value,"to:",value)
  200. self.__data[name] = value
  201. _value = self.__data[name]
  202. return _value /100.
  203. exec_size_master = _MASTER("EXEC-SIZE")
  204. exec_speed_master = _MASTER("EXEC-SPEED")
  205. exec_offset_master = _MASTER("EXEC-OFFSET")
  206. size_master = _MASTER("SIZE")
  207. speed_master = _MASTER("SPEED")
  208. exe_master = []
  209. exe_master.append({"SIZE":100,"SPEED":100,"id":12,"link-ids":[2]})
  210. class MASTER_FX():
  211. def __init__(self):
  212. self.__data = []
  213. self.__ok = []
  214. self.i=0
  215. self.old_offsets = []
  216. self.offsets = []
  217. self.count = -1
  218. self.init = 10
  219. def add(self,fx):
  220. if fx not in self.__data:
  221. self.__data.append(fx)
  222. info = fx._get_info()
  223. offset = 0
  224. if "offset" in info:
  225. offset = info["offset"]
  226. self.old_offsets.append(offset)
  227. self.offsets.append(offset)
  228. if "xtype" in info:
  229. if info["xtype"] == "rnd":
  230. self._shuffle()
  231. def _shuffle(self):
  232. random.seed(1000)
  233. random.shuffle(self.old_offsets)
  234. def _init(self):
  235. self._shuffle()
  236. for i,v in enumerate(self.old_offsets):
  237. offset = self.old_offsets[i]
  238. self.offsets[i] = offset
  239. self.init = 0
  240. def next(self,child):
  241. i = self.__data.index(child)
  242. offset = self.old_offsets[i]
  243. self.offsets[i] = offset
  244. return offset
  245. def get(self,child,count):
  246. offset = 0
  247. if child not in self.__data:
  248. return offset
  249. if self.init:
  250. self._init()
  251. idx = self.__data.index(child)
  252. if (self.count != count and idx == 0 ) or self.init == 0:
  253. self.init = 1
  254. self._shuffle()
  255. self.count=count
  256. idx = self.__data.index(child)
  257. offset = self.offsets[idx]
  258. return offset
  259. class FX():
  260. 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):
  261. self.__xtype=xtype
  262. self.__size = size
  263. self.__start = start
  264. self.__master_id = master_id
  265. if width > 200:
  266. width = 200
  267. if width <= 0:
  268. width = 1
  269. self.__fade_in_master = 0
  270. self.__width = width
  271. self.__invert = invert
  272. self.__base = base
  273. self.__speed = speed
  274. self.__offset = offset
  275. self.__clock = clock
  276. self.__clock_curr = clock
  277. self.__clock_delta = 0
  278. self.__clock_old = self.__clock_curr
  279. self.out = 0
  280. self.old_v = -1
  281. self.run = 1
  282. self.count = -1
  283. self.abs = 0 # ABSOLUT
  284. self.__angel = self.__clock_curr*360%360
  285. if master is None:
  286. cprint(master, "MASTER_FX ERR",master,color="red")
  287. self.__master = MASTER_FX()
  288. self.__master.add(self)
  289. else:
  290. self.__master = master
  291. self.__master.add(self)
  292. if self.__xtype == "rnd":
  293. self.__offset = self.__master.get(self,-2)
  294. self.__offset = self.__master.next(self)#,count)
  295. self._exec_id = None
  296. self.next()
  297. def exec_id(self,_id=None):
  298. if type(_id) is not type(None):
  299. self._exec_id = str(_id)
  300. return self._exec_id
  301. def _get_info(self):
  302. return {"offset":self.__offset,"xtype":self.__xtype}
  303. def __str__(self):
  304. return self.__repr__()
  305. def __repr__(self):
  306. ABS = "INC"
  307. if self.abs:
  308. ABS = "ABS"
  309. return "<FX Next:{:0.2f} xtype:{} Size:{:0.2f} Speed:{:0.2f} ang:{:0.2f} base:{} Clock:{:0.2f} run:{} EXEC:{} :{}>".format(
  310. self.next(), self.__xtype, self.__size, self.__speed, self.__angel
  311. , self.__base, self.__clock_curr, self.run, self._exec_id,ABS )
  312. def _calc_fx(self,v,t,size,base):
  313. base = 0
  314. if self.__base == "-": # sub
  315. if self.__invert:
  316. v = 1-v
  317. size *=-1
  318. v *=-1
  319. elif self.__base == "+": # sub
  320. if self.__invert:
  321. v = v-1
  322. else:
  323. v = (t%1-0.5)
  324. return v
  325. def next(self,clock=None):
  326. if type(clock) is float or type(clock) is int:#not None:
  327. self.__clock_curr = clock
  328. d = (self.__clock_curr - self.__clock_old)
  329. m1 = ( speed_master.val(self.__master_id)) # global speed-master
  330. m2 = ( exec_speed_master.val(self._exec_id)) # exec start by 0
  331. shift = 0
  332. m = (m1 * m2) -1
  333. shift += d * m
  334. self.__clock_delta += shift
  335. self.__clock_old = self.__clock_curr
  336. t = self.__clock_curr
  337. t += self.__clock_delta
  338. t *= self.__speed / 60
  339. offset2 = self.__offset
  340. offset2 *= exec_offset_master.val(self._exec_id)
  341. t += offset2 / 100
  342. t += self.__start / 1024 #255
  343. tw = t%1
  344. count = t//1
  345. t = t * (100/self.__width)
  346. if tw > self.__width/100:
  347. t = 1
  348. self.__angel = t%1*360
  349. t = t%1
  350. rad = math.radians(self.__angel)
  351. self.abs = 0
  352. v=0
  353. out = 0
  354. base = 0
  355. size = self.__size
  356. if self.__base == "+": # add
  357. base = size/2
  358. elif self.__base == "-": # sub
  359. base = size/2*-1
  360. if self.__xtype == "sinus":
  361. v = math.sin( rad )
  362. v/=2
  363. elif self.__xtype == "cosinus":
  364. v = math.cos( rad )
  365. if self.__base == "+": # add
  366. size *= -1
  367. v/=2
  368. elif self.__xtype == "rnd":
  369. #base = 0
  370. if self.__angel > 90 and self.__angel <=270:
  371. v=1
  372. else:
  373. v=0
  374. #if count != self.count and v: # % 2 == 0:#!= self.count:
  375. # #self.__offset = random.randint(0,1024)# /1024
  376. # self.__master._shuffle()
  377. if count != self.count and v == 0: # and v: # % 2 == 0:#!= self.count:
  378. self.__master.next(self)#,count)
  379. #self.__master.next(self)#,count)
  380. self.__offset = self.__master.get(self,count)
  381. base = 0
  382. if self.__base == "-": # sub
  383. if self.__invert:
  384. v = 1-v
  385. #base = -size
  386. size *=-1
  387. v *=-1
  388. elif self.__base == "+": # sub
  389. if self.__invert:
  390. v = v-1
  391. else:
  392. v = (t%1-0.5)
  393. elif self.__xtype == "on":
  394. #base = 0
  395. if self.__angel > 90 and self.__angel <=270:
  396. v=1
  397. else:
  398. v=0
  399. base = 0
  400. if self.__base == "-": # sub
  401. if self.__invert:
  402. v = 1-v
  403. #base = -size
  404. size *=-1
  405. v *=-1
  406. elif self.__base == "+": # sub
  407. if self.__invert:
  408. v = v-1
  409. else:
  410. v = (t%1-0.5)
  411. elif self.__xtype == "ramp" or self.__xtype == "bump":
  412. v = (t%1)
  413. base = 0
  414. if self.__base == "-": # sub
  415. if self.__invert:
  416. v = 1-v
  417. #base = -size
  418. size *=-1
  419. v *=-1
  420. elif self.__base == "+": # sub
  421. if self.__invert:
  422. v = v-1
  423. else:
  424. v = (t%1-0.5)
  425. elif self.__xtype == "ramp2" or self.__xtype == "bump2":
  426. v = (t%1)
  427. v = 1-v
  428. if v == 1:
  429. v=0
  430. base = 0
  431. if self.__base == "-": # sub
  432. if self.__invert:
  433. v = 1-v
  434. #base = -size
  435. size *=-1
  436. v *=-1
  437. elif self.__base == "+": # sub
  438. if self.__invert:
  439. v = v-1
  440. else:
  441. v = (t%1-0.5)
  442. elif self.__xtype == "static":
  443. self.abs = 1
  444. base = size #100
  445. v=0
  446. size=0
  447. elif self.__xtype == "fade":
  448. x = t * 2
  449. if x > 1:
  450. x = 2-x
  451. x -= 0.5
  452. v = x*2
  453. #base /= 2
  454. #base *=2
  455. if self.__base == "+": # add
  456. pass#base /= 2
  457. else:
  458. v *= -1
  459. v/=2
  460. if self.__invert:
  461. v *=-1
  462. #if self.__fade_in_master < 255:
  463. # self.__fade_in_master += v*size
  464. out = v *size +base
  465. self.out = out
  466. self.count = count
  467. out = out * size_master.val(self.__master_id) # master
  468. out = out * exec_size_master.val(self._exec_id) # master
  469. #* (self.__fade_in_master /255.)
  470. return out
  471. class DMXCH(object):
  472. def __init__(self,dmx=-1):
  473. self._base_value = 0
  474. self._fade = None
  475. self._fx = [None,None] # None
  476. self._fx_value = 0
  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. fx_value = self._fx_value
  532. self._flash_fx = None
  533. self._flash_fx_value = 0
  534. else:
  535. self._flash_fx = FX(xtype=xtype,size=size,speed=speed,invert=invert
  536. ,width=width,start=start,offset=offset,base=base
  537. ,clock=clock,master=master,master_id=0)
  538. self._flash_fx.exec_id(self._exec_ids[3])
  539. self.next(clock)
  540. #print("init flash_fx",self)
  541. def fx_ctl(self,cmd=""): #start,stop,off
  542. pass
  543. def __str__(self):
  544. return self.__repr__()
  545. def exec_ids(self,_id=None):
  546. #if type(_id) is not type(None):
  547. # #self._exec_id = _id
  548. # #print("set exec_id",_id)
  549. return self._exec_ids
  550. def __repr__(self):
  551. v = self._last_val
  552. if type(self._last_val) in [int,float]:
  553. v = round(self._last_val,2)
  554. return "<BUFFER DMX:{} FINE:{} VAL:{} EXEC:{}\n fd:{}\n fx:{}\n fx_flash:{}\n".format(
  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. Bdmx.append( DMXCH(i) )
  611. #print(type(dmx[i]))
  612. _id = 1
  613. def split_cmd(data):
  614. if "cmd" in data:
  615. cmd = data["cmd"]
  616. #print("cmd",cmd)
  617. if "," in cmd:
  618. cmds = cmd.split(",")
  619. else:
  620. cmds = [cmd]
  621. return cmds
  622. class VDMX():
  623. """functional implementation as class for namespace encapsulation
  624. """
  625. def __init__(self):
  626. self.data = OrderedDict()
  627. self.data[4] = {"DMX":[21,22,23],"VALUE":255, "LIMIT":255} #,"DMXCH":DMXCH("V4")}
  628. for k,v in self.data.items():
  629. pass
  630. #dmxch = v["DMXCH"]
  631. #dmxch.fade(10,0)
  632. #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):
  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 by_dmx(self,clock,dmx):
  648. #print("master of dmx:",dmx)
  649. val=0
  650. flag = 0
  651. data = self._list_by_dmx(dmx)
  652. for i,row in data.items():
  653. if "DMXCH" not in row:
  654. row["DMXCH"] = DMXCH("V{}".format(i))
  655. row["DMXCH"].fade(255,0)
  656. v = row["DMXCH"].next(clock)
  657. #row["DMXCH"].fade(200,20)
  658. if v >= val:
  659. val = v
  660. flag = 1
  661. out = 1.
  662. if val > 255:
  663. val = 255
  664. if flag:
  665. out = val/255.
  666. else:
  667. out = 1.
  668. return out
  669. vdmx = VDMX()
  670. class HTP_MASTER():
  671. """functional implementation as class for namespace encapsulation
  672. """
  673. def __init__(self):
  674. self.data = OrderedDict()
  675. #self.data[1] = {"DMX":[1,2,3],"VALUE":80, "LIMIT":255}
  676. #self.data[2] = {"DMX":[12,13,22],"VALUE":70, "LIMIT":255}
  677. #self.data[3] = {"DMX":[22,23,24],"VALUE":99, "LIMIT":255}
  678. self.data[4] = {"DMX":[22,23,24],"VALUE":99, "LIMIT":255,"DMXCH":DMXCH(4)}
  679. def _list_by_dmx(self,_dmx=0):
  680. data = OrderedDict()
  681. for i,link in self.data.items(): # enumerate(self.data):
  682. if _dmx in link["DMX"]:
  683. #print( "_list_by_dmx",i,link)
  684. data[i] = link
  685. return data
  686. def dmx_by_id(self,_id=0):
  687. #print("dmx by master-id:",_id)
  688. if _id in self.data:
  689. for i,link in self.data[_id].items():
  690. #print("dmx_by_id", i,link)
  691. return (i,link)
  692. return 0,{}
  693. def val_by_dmx(self,dmx=0):
  694. #print("master of dmx:",dmx)
  695. val=0
  696. flag = 0
  697. data = self._list_by_dmx(dmx)
  698. for i,link in data.items():
  699. #print("master_by_dmx", i,link)
  700. if link["VALUE"] > val:
  701. #print("master_by_dmx", i,link)
  702. val = link["VALUE"]
  703. flag=1
  704. out = 1.
  705. if flag:
  706. out = val/255.
  707. return out
  708. htp_master = HTP_MASTER()
  709. #htp_master.data[_id] = {"DMX":[1,2,3],"VALUE":80, "LIMIT":255,"DMXCH":DMXCH()}
  710. class Main():
  711. def __init__(self):
  712. self.artnet = {}
  713. self.fx = {} # key is dmx address
  714. self.lock = thread.allocate_lock()
  715. def loop(self):
  716. xx = [0]*512
  717. ii = 0
  718. old_univ = -1
  719. xx = [0]*512
  720. for ii,dmxch in enumerate(Bdmx):
  721. i = ii%512
  722. univ = ii//512
  723. if str(univ) not in self.artnet:
  724. print("add uiv",univ)
  725. self.artnet[str(univ)] = ANN.ArtNetNode(to="10.10.10.255",univ=univ)
  726. if univ != old_univ:
  727. old_univ = univ
  728. try:
  729. artnet.next()
  730. except:pass
  731. artnet = self.artnet[str(univ)]
  732. artnet.dmx = [0]*512
  733. fps_start = time.time()
  734. fps = 0
  735. dbg= 0#1
  736. while 1:
  737. self.lock.acquire_lock()
  738. start = time.time()
  739. _t=0
  740. t = clock.time()
  741. ii = 0
  742. old_univ = -1
  743. xx = [0]*512
  744. for ii,dmxch in enumerate(Bdmx):
  745. i = ii%512
  746. univ = ii//512
  747. s_univ = str(univ)
  748. if s_univ not in self.artnet:
  749. print("add uiv",univ)
  750. self.artnet[s_univ] = ANN.ArtNetNode(to="10.10.10.255",univ=univ)
  751. if dbg:
  752. end = time.time()
  753. print(" t",_t,ii,round(end-start,4))
  754. start = time.time()
  755. _t+=1
  756. old_univ = -1
  757. xx = [0]*512
  758. for ii,dmxch in enumerate(Bdmx):
  759. i = ii%512
  760. univ = ii//512
  761. if univ != old_univ:
  762. old_univ = univ
  763. artnet = self.artnet[str(univ)]
  764. xx = artnet.dmx
  765. v = dmxch.next(t)
  766. xx[i] = int(v)
  767. if dbg:
  768. end = time.time()
  769. print(" t",_t,ii,round(end-start,4))
  770. start = time.time()
  771. _t+=1
  772. old_univ = -1
  773. xx = [0]*512
  774. for ii,dmxch in enumerate(Bdmx): #fine loop
  775. dmx_fine = dmxch._dmx_fine
  776. if dmx_fine > 0:
  777. i = ii%512
  778. univ = ii//512
  779. if univ != old_univ:
  780. artnet = self.artnet[str(univ)]
  781. xx = artnet.dmx# = xx
  782. v = dmxch.next(t)
  783. vv = vdmx.by_dmx(clock=i,dmx=ii+1)
  784. try:
  785. v = v*vv # disable v-master
  786. except Exception as e:
  787. cprint("Exception v*vv",[v,vv],e)
  788. continue
  789. vf = int(v%1*255)
  790. dmx_fine = dmx_fine%512
  791. try:
  792. if v >= 255:
  793. xx[dmx_fine-1] = 255
  794. elif v < 0:
  795. xx[dmx_fine-1] = 0
  796. else:
  797. xx[dmx_fine-1] = int(v%1*255)
  798. except Exception as e:
  799. print("E dmx_fine",e,dmx_fine)
  800. if dbg:
  801. end = time.time()
  802. print(" t",_t,ii,round(end-start,4))
  803. print()
  804. start = time.time()
  805. _t+=1
  806. self.lock.release_lock()
  807. for k,artnet in self.artnet.items():
  808. artnet.next()
  809. fps += 1
  810. stop_fps = 50
  811. time.sleep(1/30)
  812. if fps >= stop_fps:
  813. fps_t = time.time()
  814. FPS = round(stop_fps/(fps_t-fps_start),2)
  815. print("{:0.0f} core/fps".format(FPS) )
  816. fps = 0
  817. fps_start = time.time()
  818. main = Main()
  819. if __run_main:
  820. thread.start_new_thread(main.loop,())
  821. def _init_action(row):#Bdmx,out,DMX):
  822. Admx = row["DMXCH"]
  823. line_sub_count = 0
  824. if row["fx"]:
  825. x = row["fx"]
  826. Admx.fx(xtype=x["xtype"]
  827. ,size=x["size"]
  828. ,speed=x["speed"]
  829. ,invert=x["invert"]
  830. ,width=x["width"]
  831. ,start=x["start"]
  832. ,offset=x["offset"]
  833. ,base=x["base"]
  834. ,clock=x["clock"]
  835. ,master=x["master"])
  836. line_sub_count += 1
  837. if row["flash_fx"]:
  838. x = row["flash_fx"]
  839. Admx.flash_fx(xtype=x["xtype"]
  840. ,size=x["size"]
  841. ,speed=x["speed"]
  842. ,invert=x["invert"]
  843. ,width=x["width"]
  844. ,start=x["start"]
  845. ,offset=x["offset"]
  846. ,base=x["base"]
  847. ,clock=x["clock"]
  848. ,master=x["master"])
  849. line_sub_count += 1
  850. if row["flash"]:
  851. x = row["flash"]
  852. Admx.flash(target=x["target"]
  853. ,ftime=x["ftime"]
  854. ,clock=x["clock"]
  855. ,delay=x["delay"])
  856. line_sub_count += 1
  857. if row["fade"]:
  858. x = row["fade"]
  859. Admx.fade(target=x["target"]
  860. ,ftime=x["ftime"]
  861. ,clock=x["clock"]
  862. ,delay=x["delay"])
  863. line_sub_count += 1
  864. return line_sub_count
  865. def set_dmx_fine_ch(Admx,dmx_fine_nr):
  866. try:
  867. if int(dmx_fine_nr) > 0:
  868. Admx._dmx_fine = int(dmx_fine_nr)
  869. except Exception as e:
  870. cprint(x,color="red")
  871. cprint("except 3455",e,color="red")
  872. def _parse_fx2(x,fx2,clock):
  873. if type(fx2) is dict and fx2:
  874. pass
  875. else:
  876. return
  877. xtype="fade"
  878. size = 10
  879. speed = 10
  880. start = 0
  881. offset= 0
  882. width=100
  883. invert=0
  884. base = "-"
  885. if "TYPE" in fx2:
  886. xtype = fx2["TYPE"]
  887. if "SIZE" in fx2:
  888. size = fx2["SIZE"]
  889. if "SPEED" in fx2:
  890. speed = fx2["SPEED"]
  891. if "OFFSET" in fx2:
  892. offset = fx2["OFFSET"]
  893. if "BASE" in fx2:
  894. base = fx2["BASE"]
  895. if "INVERT" in fx2:
  896. invert = fx2["INVERT"]
  897. if "WIDTH" in fx2:
  898. width = fx2["WIDTH"]
  899. if "off" == x["VALUE"]: #fix fx flash off
  900. xtype= "off"
  901. if "alloff" == xtype.lower():
  902. for dmxch in Bdmx:
  903. if dmxch is not None:
  904. dmxch.flash_fx(xtype="off",clock=clock)
  905. dmxch.fx(xtype="off",clock=clock)
  906. for j in V_MASTER:
  907. dmxch = V_MASTER[j]
  908. if j is not None:
  909. dmxch.flash_fx(xtype="off",clock=clock)
  910. dmxch.fx(xtype="off",clock=clock)
  911. xout = {"xtype":xtype,"size":size,"speed":speed
  912. ,"invert":invert,"width":width,"start":start
  913. ,"offset":offset,"base":base,"clock":clock,"master":0
  914. }
  915. #,"offset":offset,"base":base,"clock":c,"master":master_fx
  916. mode="fx"
  917. if "FLASH" in x:
  918. mode = "flash_fx"
  919. return [mode,xout]
  920. def _parse_master(x):
  921. print("CMD:",x)
  922. if "EXEC-SPEED-MASTER" == x["CMD"]:
  923. exec_speed_master.val(x["NR"],x["VALUE"])
  924. if "EXEC-SIZE-MASTER" == x["CMD"]:
  925. exec_size_master.val(x["NR"],x["VALUE"])
  926. if "EXEC-OFFSET-MASTER" == x["CMD"]:
  927. exec_offset_master.val(x["NR"],x["VALUE"])
  928. if "SPEED-MASTER" == x["CMD"]:
  929. speed_master.val(x["NR"],x["VALUE"])
  930. if "SIZE-MASTER" == x["CMD"]:
  931. size_master.val(x["NR"],x["VALUE"])
  932. def _parse_cmds(cmds,clock=0):
  933. out = {}
  934. for cmd in cmds:
  935. try:
  936. _parse_cmd(out,cmd,clock=clock)
  937. except Exception as e:
  938. cprint("EXCEPTION JCB",e,color="red")
  939. cprint("----",str(cmds)[:150],"...",color="red")
  940. cprint("Error on line {}".format(sys.exc_info()[-1].tb_lineno),color="red")
  941. raise e
  942. return out
  943. def _parse_cmd(out,cmd,clock=0):
  944. master_fx = MASTER_FX()
  945. Admx = DMXCH() #dummy
  946. x=cmd
  947. c=clock
  948. _fix_id=0
  949. _attr = ""
  950. if "CMD" in x:
  951. _parse_master(x)
  952. return #continue
  953. if "DMX" not in x:
  954. return #continue
  955. DMX = int(x["DMX"])
  956. if "VALUE" not in x:
  957. return #continue
  958. _val = x["VALUE"]
  959. _inc = 0
  960. _fix_id = 0
  961. _clock = 0
  962. exec_id = None
  963. if "INC" in x:
  964. _inc = x["INC"]
  965. if "FIX" in x:
  966. _fix_id = x["FIX"]
  967. if "clock" in x:
  968. _clock=x["clock"]
  969. if "ATTR" in x:
  970. _attr = x["ATTR"]
  971. if DMX <= 0: # VIRTUAL
  972. DMX = "FIX"+str(_fix_id)
  973. ok = 0
  974. if "ATTR" in x:
  975. _attr = x["ATTR"]
  976. if "DIM" == x["ATTR"]:
  977. if _fix_id not in V_MASTER:
  978. V_MASTER[_fix_id] = DMXCH()
  979. #print("_val",_val)
  980. #V_MASTER[_fix_id].fade(_val,ftime=0,clock=0,delay=0)
  981. #print(" V-MASTER",_fix_id,_val,_inc)
  982. ok = 1
  983. if _fix_id in V_MASTER:
  984. Admx = V_MASTER[_fix_id]
  985. if not ok:
  986. return #continue
  987. else:
  988. if DMX < len(Bdmx):
  989. Admx = Bdmx[DMX-1]
  990. else:
  991. print("DMX ADDRESS too BIG",DMX)
  992. return #continue
  993. if "DMX-FINE" in x and DMX > 0:
  994. set_dmx_fine_ch(Admx, x["DMX-FINE"])
  995. if "EXEC" in x:
  996. exec_id = x["EXEC"]
  997. if "ATTR" in x:
  998. _attr = x["ATTR"]
  999. if "FIX" in x:
  1000. _fix_id = x["FIX"]
  1001. fx=""
  1002. fx2={}
  1003. ftime=0
  1004. delay=0
  1005. if "FX" in x:
  1006. fx = x["FX"]
  1007. if "FX2" in x:
  1008. fx2 = x["FX2"]
  1009. if "FADE" in x:
  1010. ftime = x["FADE"]
  1011. if "DELAY" in x:
  1012. delay = x["DELAY"]
  1013. #print("DO",[exec_id],x)
  1014. # ids = [401,402,304,103]
  1015. # exec-id, exec-fx-id, flush-id, flush-fx-id
  1016. if _val != "off":
  1017. if "FLASH" in x:
  1018. ids = Admx.exec_ids()
  1019. if type(_val) is int:
  1020. ids[2] = exec_id
  1021. if fx2:
  1022. ids[3] = exec_id
  1023. #print(" ",[ids, exec_id],"FL")
  1024. else: # GO or ON
  1025. ids = Admx.exec_ids()
  1026. if type(_val) is int:
  1027. ids[0] = exec_id
  1028. if fx2:
  1029. ids[1] = exec_id
  1030. #print(" ",[ids, exec_id],"GO")
  1031. if _val == "off":
  1032. # flash bug if lot's of virtual dimmer are RELEASED-FROM-FLASH ("FLASH off")
  1033. # the V-MASTER FLASH ist HANGING ... no ENCODER change possible !!! ???
  1034. if 0: # "FLASH" in x:
  1035. ids = Admx.exec_ids()
  1036. stop = 0
  1037. #print(" ",[ids, exec_id])
  1038. if ids[2] != exec_id:
  1039. stop = 1
  1040. else:
  1041. ids[2] = None
  1042. if fx2:
  1043. if ids[3] != exec_id:
  1044. stop = 1
  1045. else:
  1046. ids[3] = None
  1047. stop = 0
  1048. if stop:
  1049. # this FLASH cmd OFF/RELEASE is not valid anymore
  1050. return #continue
  1051. #aprint("OK")
  1052. #ids = Admx.exec_ids()
  1053. #print("OK ",[ids, exec_id])
  1054. #Bdmx[DMX].exec_id(exec_id)
  1055. out[DMX] = {"flash":{},"fade":{},"fx":{},"flash_fx":{},"fix_id":_fix_id,"attr":_attr,"DMXCH":Admx}
  1056. if _val is not None:
  1057. Xval = {"target":_val,"ftime":ftime, "clock":c,"delay":delay,"DMXCH":Admx}
  1058. mode = "fade"
  1059. if "FLASH" in x:
  1060. mode = "flash"
  1061. out[DMX][mode] = Xval
  1062. # FX PARSE
  1063. _fx2 = _parse_fx2(x,fx2,clock=c)
  1064. if _fx2:
  1065. _key=_fx2[0] # fx, fx_flash
  1066. Xval=_fx2[1] # dict
  1067. Xval["master"] = master_fx
  1068. out[DMX][_key] = Xval
  1069. elif type(fx) is str and fx:
  1070. # old fx like sinus:200:12:244
  1071. ccm = str(DMX+1)+":"+fx
  1072. print("fx",ccm)
  1073. if "FLASH" in x:
  1074. pass #CB({"cmd":"fxf"+ccm})
  1075. else:
  1076. pass #CB({"cmd":"fx"+ccm})
  1077. def calc_jitter(jdatas,t_start):
  1078. for cmds in jdatas:
  1079. for line in cmds:
  1080. if "time" in line:
  1081. jt_start = line["time"]
  1082. latenz = round(t_start-jt_start,4)
  1083. if latenz > 0.5:
  1084. cprint("jitter 0.5 >",latenz,color="red")
  1085. break
  1086. def process_flash(jdatas):
  1087. for cmds in jdatas:
  1088. for line in cmds: # run first
  1089. if "FLASH" in line:
  1090. cprint("FLUSH",end=" ",color="CYAN")
  1091. if "VALUE" in line:
  1092. if line["VALUE"] == "off":
  1093. cprint("OFF",end=" ",color="red")
  1094. else:
  1095. cprint("ON",end=" ",color="green")
  1096. print("")
  1097. else:
  1098. cprint("FADE",color="CYAN")
  1099. break
  1100. def parse_json(data):
  1101. jdatas = []
  1102. l2 = 0
  1103. print("INPUT JCB =>",len(data),":",l2)
  1104. for line in data:
  1105. data2 = json.loads(line)
  1106. l2 += len(data2)
  1107. jdatas.append(data2) #["CMD"])
  1108. jdatas2 = []
  1109. for cmds in jdatas:
  1110. if not cmds:
  1111. continue
  1112. jdatas2.append(cmds)
  1113. return jdatas2
  1114. import hashlib
  1115. JCB_GLOB_BUF = {}
  1116. def JCB(data,sock=None): #json client input
  1117. t_start = time.time()
  1118. s = time.time()
  1119. e = time.time()
  1120. ct = int(e*100)/100
  1121. print()
  1122. msg = "{} JCB START: {:0.02f} sizeof:{}"
  1123. msg = msg.format(ct,e-s,sys.getsizeof(data) )
  1124. print(msg)
  1125. jdatas = parse_json(data)
  1126. c = clock.time()
  1127. c = float(c)
  1128. ftime = 0
  1129. delay = 0
  1130. out = {}
  1131. line=""
  1132. process_flash(jdatas)
  1133. calc_jitter(jdatas,t_start)
  1134. for cmds in jdatas:
  1135. master_fx = MASTER_FX()
  1136. out = _parse_cmds(cmds,clock=c)
  1137. line_count = 0
  1138. line_count_sub = 0
  1139. line_size = 0
  1140. attr_count = {}
  1141. attr_count2 = 0
  1142. if not out:
  1143. return
  1144. try:
  1145. main.lock.acquire_lock()
  1146. for _id in out:
  1147. row = out[_id]
  1148. #print(" ",_id,[str(row)])
  1149. line_size += sys.getsizeof(row)
  1150. #print("_id",_id)
  1151. Admx = row["DMXCH"]
  1152. #print("Admx",Admx)
  1153. if "attr" in row:
  1154. if row["attr"] not in attr_count:
  1155. attr_count[row["attr"]] = 0
  1156. attr_count[row["attr"]] += 1
  1157. attr_count2 +=1
  1158. if row["fix_id"]:
  1159. _fix_id = row["fix_id"]
  1160. Admx._fix_id = _fix_id
  1161. if "attr" in row:
  1162. if row["attr"] in ["RED","GREEN","BLUE","WHITE","AMBER"]: #CYAN,MAGENTA,YELLOW
  1163. Admx._v_master_id = _fix_id
  1164. #print("SET V_MASTER",row)
  1165. line_count_sub += _init_action(row)
  1166. line_count += 1
  1167. e = time.time()
  1168. print(" sub-JCB TIME:","{:0.02f}".format(e-s),int(e*100)/100)
  1169. #time.sleep(1/30)
  1170. except Exception as e:
  1171. cprint("EXCEPTION JCB",e,color="red")
  1172. cprint("----",str(cmds)[:150],"...",color="red")
  1173. cprint("Error on line {}".format(sys.exc_info()[-1].tb_lineno),color="red")
  1174. raise e
  1175. finally:
  1176. main.lock.release_lock()
  1177. #cprint(" ","{:0.04} sec.".format(time.time()-t_start),color="yellow")
  1178. print("attr_count:",attr_count)
  1179. #print(line_count,line_size)
  1180. e = time.time()
  1181. ct = int(e*100)/100
  1182. msg = "{} JCB: END {:0.02f} sizeof:{} fix-count:{} attr-count:{}"
  1183. msg = msg.format(ct,e-s,line_size,line_count,line_count_sub )
  1184. print(msg)
  1185. time.sleep(1/60)
  1186. if __run_main:
  1187. s = chat.Server(cb=JCB)
  1188. while 1:
  1189. s.poll()
  1190. time.sleep(0.001)