console.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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] != 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. def __str__(self):
  102. return self.__repr__()
  103. def __repr__(self):
  104. return " Fade Next:{:0.2f} Start:{:0.2f} Target:{:0.2f} Clock:{:0.2f} ".format(
  105. self.next(), self.__start,self.__target,self.__clock_curr )
  106. def next(self,clock=None):
  107. if self.__time <= 0:
  108. self.__last = self.__target
  109. if type(clock) is float or type(clock) is int:#not None:
  110. self.__clock_curr = clock
  111. if self.__target > self.__start:
  112. if self.__last >= self.__target:
  113. return self.__target
  114. else:
  115. if self.__last <= self.__target:
  116. return self.__target
  117. current = (self.__clock - self.__clock_curr) / self.__time
  118. length = self.__start - self.__target
  119. self.__last = self.__start+ length*current
  120. return self.__last
  121. def ctl(self,cmd="",value=None): # if x-fade cmd="%" value=50
  122. # start,stop,fwd,bwd,revers
  123. pass
  124. class FX():
  125. def __init__(self,type="sinus",size=10,speed=10,offset=0):
  126. pass
  127. def next(self):
  128. pass
  129. class DMXCH(object):
  130. def __init__(self):
  131. self._value = 1
  132. self._fade = None
  133. self._fx = None
  134. def fade(self,target,time=0,clock=0):
  135. if target != self._value:
  136. self._fade = Fade(self._value,target,time=time,clock=clock)
  137. def fx(self,type="sinus",size=20,speed=20,offset=0):
  138. pass
  139. def fx_ctl(self,cmd=""):#start,stop,off
  140. pass
  141. def __str__(self):
  142. return self.__repr__()
  143. def __repr__(self):
  144. try:
  145. return " DMXCH {:0.2f} {:0.2f}".format( self._value,self._fade)
  146. except:
  147. return " DMXCH {:0.2f} {}".format( self._value,self._fade)
  148. def fade_ctl(self,cmd=""):#start,stop,backw,fwd,bounce
  149. pass
  150. def next(self,clock=0):
  151. if type(self._fade) is Fade:# is Fade:
  152. self._value = self._fade.next(clock)
  153. return self._value
  154. Bdmx = []
  155. for i in range(512):
  156. Bdmx.append( DMXCH() )
  157. #print(type(dmx[i]))
  158. def split_cmd(data):
  159. if "cmd" in data:
  160. cmd = data["cmd"]
  161. print("cmd",cmd)
  162. if "," in cmd:
  163. cmds = cmd.split(",")
  164. else:
  165. cmds = [cmd]
  166. return cmds
  167. def CB(data):
  168. print("CB",data)
  169. cmds = split_cmd(data)
  170. t = clock.time()
  171. for xcmd in cmds:
  172. if xcmd.startswith("d"):
  173. xxcmd=xcmd[1:].split(":")
  174. print("DMX:",xxcmd)
  175. l = xxcmd
  176. try:
  177. k=int(l[0])-1
  178. v=int(l[1])
  179. if v > 255:
  180. v = 255
  181. if len(Bdmx) > k:
  182. print( Bdmx[k])
  183. #if dmx[k] is int:
  184. #dmx[k] = v
  185. Bdmx[k].fade(target=v,clock=t ,time=2)#clock.time())
  186. except Exception as e:
  187. print("EXCEPTION IN DMX",e)
  188. chat.cmd(CB) # server listener
  189. input("END")