console.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #! /usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. This file is part of grandPA.
  5. grandPA 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, either version 2 of the License, or
  8. (at your option) any later version.
  9. grandPA is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with grandPA. If not, see <http://www.gnu.org/licenses/>.
  15. (c) 2012 micha.rathfelder@gmail.com
  16. """
  17. import time
  18. import socket
  19. import struct
  20. import random
  21. from collections import OrderedDict
  22. import lib.chat as chat
  23. import lib.ArtNetNode as ANN
  24. import _thread as thread
  25. #thread.start_new_thread
  26. import lib.motion as motion
  27. #idmx = [0]*512 # incremental dmx
  28. dmx = [0]*512 # absolute dmx data
  29. def artnet_loop():
  30. #artnet = ANN.ArtNetNode(to="127.0.0.1",port=6555,univ=12)
  31. #artnet = ANN.ArtNetNode(to="127.0.0.1",port=6555,univ=0)
  32. artnet = ANN.ArtNetNode(to="10.10.10.255",univ=0)
  33. #artnet = ANN.ArtNetNode(to="2.0.0.255",univ=0)
  34. #artnet = ANN.ArtNetNode(to="10.10.10.255",univ=1)
  35. dmx[205] = 255 #205 BLUE
  36. artnet.dmx= dmx #[0]*512
  37. artnet.send()
  38. while 1:
  39. #artnet._test_frame()
  40. artnet.next()
  41. time.sleep(0.01)
  42. class Main():
  43. def __init__(self):
  44. #artnet = ANN.ArtNetNode(to="127.0.0.1",port=6555,univ=12)
  45. #artnet = ANN.ArtNetNode(to="127.0.0.1",port=6555,univ=0)
  46. #artnet = ANN.ArtNetNode(to="2.0.0.255",univ=0)
  47. #artnet = ANN.ArtNetNode(to="10.10.10.255",univ=1)
  48. self.artnet = ANN.ArtNetNode(to="10.10.10.255",univ=0)
  49. self.fx = {} # key is dmx address
  50. def loop(self):
  51. #dmx[205] = 255 #205 BLUE
  52. self.artnet.send()
  53. xx = [0]*512
  54. self.artnet.dmx = xx# [:] #dmx #[0]*512
  55. while 1:
  56. t = clock.time()
  57. for i,dmxch in enumerate(Bdmx):
  58. v = dmxch.next(t)
  59. if i == 0:
  60. if xx[i]+1 < v or xx[i]-1 > v :
  61. #print("----v",x[i],v,t)
  62. print("i:{:0.2f} xx:{:0.2f} v:{:0.2f} {:0.2f}----v {}".format(i,xx[i],v,t+100,dmxch))
  63. xx[i] = int(v)
  64. #artnet._test_frame()
  65. self.artnet.next()
  66. #self.artnet.send()
  67. time.sleep(0.01)
  68. main = Main()
  69. #thread.start_new_thread(artnet_loop,())
  70. thread.start_new_thread(main.loop,())
  71. class CLOCK():
  72. def __init__(self):
  73. self.__time = 0
  74. self.__start = time.time() # only for debugging
  75. self.__tick = 0.01 # incremental timer drift's on highe cpu load ?
  76. def time(self):
  77. return self.__time
  78. def get_drift(self):
  79. run_time = time.time() - self.__start
  80. tick_time = self.__time # * self.__tick
  81. print( "runtime:{:0.2f} tick_timer:{:0.2f} drift:{:0.2f}".format(run_time,tick_time,run_time-tick_time))
  82. def loop(self):
  83. while 1:
  84. self.__time +=self.__tick
  85. #if int(self.__time*100)/10. % 10 == 0:# self.__time % 2 == 0:
  86. # print( self.get_drift())
  87. #print(self.__time)
  88. #for i in range(10):
  89. time.sleep(self.__tick)
  90. clock = CLOCK()
  91. thread.start_new_thread(clock.loop,())
  92. class Fade():
  93. def __init__(self,start,target,time,clock):
  94. #print("init Fade ",start,target,time,clock)
  95. self.__clock = clock
  96. self.__clock_curr = clock
  97. self.__time = time
  98. self.__start = start
  99. self.__last = start
  100. self.__target = target
  101. print("INIT", str(self) )
  102. def __str__(self):
  103. return self.__repr__()
  104. def __repr__(self):
  105. return "<Fade Next:{:0.2f} Start:{:0.2f} Target:{:0.2f} Clock:{:0.2f}>".format(
  106. self.next(), self.__start,self.__target,self.__clock_curr )
  107. def next(self,clock=None):
  108. if self.__time <= 0:
  109. self.__last = self.__target
  110. if type(clock) is float or type(clock) is int:#not None:
  111. self.__clock_curr = clock
  112. if self.__target > self.__start:
  113. if self.__last >= self.__target:
  114. return self.__target
  115. else:
  116. if self.__last <= self.__target:
  117. return self.__target
  118. current = (self.__clock - self.__clock_curr) / self.__time
  119. length = self.__start - self.__target
  120. self.__last = self.__start+ length*current
  121. return self.__last
  122. def ctl(self,cmd="",value=None): # if x-fade cmd="%" value=50
  123. # start,stop,fwd,bwd,revers
  124. pass
  125. class FX():
  126. def __init__(self,type="sinus",size=10,speed=10,offset=0):
  127. pass
  128. def next(self):
  129. pass
  130. class DMXCH(object):
  131. def __init__(self):
  132. self._value = 1
  133. self._fade = None
  134. self._fx = None
  135. def fade(self,target,time=0,clock=0):
  136. if target != self._value:
  137. self._fade = Fade(self._value,target,time=time,clock=clock)
  138. def fx(self,type="sinus",size=20,speed=20,offset=0):
  139. pass
  140. def fx_ctl(self,cmd=""):#start,stop,off
  141. pass
  142. def __str__(self):
  143. return self.__repr__()
  144. def __repr__(self):
  145. try:
  146. return " DMXCH {:0.2f} {:0.2f}".format( self._value,self._fade)
  147. except:
  148. return " DMXCH {:0.2f} {}".format( self._value,self._fade)
  149. def fade_ctl(self,cmd=""):#start,stop,backw,fwd,bounce
  150. pass
  151. def next(self,clock=0):
  152. if type(self._fade) is Fade:# is Fade:
  153. self._value = self._fade.next(clock)
  154. return self._value
  155. Bdmx = []
  156. for i in range(512):
  157. Bdmx.append( DMXCH() )
  158. #print(type(dmx[i]))
  159. def split_cmd(data):
  160. if "cmd" in data:
  161. cmd = data["cmd"]
  162. #print("cmd",cmd)
  163. if "," in cmd:
  164. cmds = cmd.split(",")
  165. else:
  166. cmds = [cmd]
  167. return cmds
  168. def CB(data):
  169. print("CB",data)
  170. cmds = split_cmd(data)
  171. c = clock.time()
  172. for xcmd in cmds:
  173. if xcmd.startswith("d"):
  174. xxcmd=xcmd[1:].split(":")
  175. #print("DMX:",xxcmd)
  176. l = xxcmd
  177. t = 2
  178. try:
  179. k=int(l[0])-1
  180. v=float(l[1])
  181. if len(l) >= 3:
  182. t=float(l[2])
  183. if v > 255:
  184. v = 255
  185. if len(Bdmx) > k:
  186. Bdmx[k].fade(target=v,time=t, clock=c)
  187. except Exception as e:
  188. print("EXCEPTION IN DMX",e)
  189. chat.cmd(CB) # server listener
  190. input("END")