Files
@ 7772cc48e016
Branch filter:
Location: light9/light9/effecteval/effectloop.py
7772cc48e016
11.2 KiB
text/x-python
reformat all python
Ignore-this: 1135b78893f8b3d31badddda7f45678f
Ignore-this: 1135b78893f8b3d31badddda7f45678f
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | from __future__ import division
import time, json, logging, traceback
import numpy
import serial
from twisted.internet import reactor, threads
from twisted.internet.defer import inlineCallbacks, returnValue, succeed
from twisted.internet.error import TimeoutError
from rdflib import URIRef, Literal
import cyclone.httpclient
from light9.namespaces import L9, RDF, RDFS
from light9.effecteval.effect import EffectNode
from light9 import Effects
from light9 import networking
from light9 import Submaster
from light9 import dmxclient
from light9 import prof
log = logging.getLogger('effectloop')
class EffectLoop(object):
"""maintains a collection of the current EffectNodes, gets time from
music player, sends dmx"""
def __init__(self, graph, stats):
self.graph, self.stats = graph, stats
self.currentSong = None
self.currentEffects = [
] # EffectNodes for the current song plus the submaster ones
self.lastLogTime = 0
self.lastLogMsg = ""
self.lastErrorLog = 0
self.graph.addHandler(self.setEffects)
self.period = 1 / 30
self.coastSecs = .3 # main reason to keep this low is to notice play/pause
self.songTimeFetch = 0
self.songIsPlaying = False
self.songTimeFromRequest = 0
self.requestTime = 0 # unix sec for when we fetched songTime
self.initOutput()
def initOutput(self):
pass
def startLoop(self):
log.info("startLoop")
self.lastSendLevelsTime = 0
reactor.callLater(self.period, self.sendLevels)
reactor.callLater(self.period, self.updateTimeFromMusic)
def setEffects(self):
self.currentEffects = []
log.info('setEffects currentSong=%s', self.currentSong)
if self.currentSong is None:
return
for effectUri in self.graph.objects(self.currentSong, L9['effect']):
self.currentEffects.append(EffectNode(self.graph, effectUri))
for sub in self.graph.subjects(RDF.type, L9['Submaster']):
for effectUri in self.graph.objects(sub, L9['drivesEffect']):
self.currentEffects.append(EffectNode(self.graph, effectUri))
log.info('now we have %s effects', len(self.currentEffects))
@inlineCallbacks
def getSongTime(self):
now = time.time()
old = now - self.requestTime
if old > self.coastSecs:
try:
response = json.loads((yield cyclone.httpclient.fetch(
networking.musicPlayer.path('time'), timeout=.5)).body)
except TimeoutError:
log.warning("TimeoutError: using stale time from %.1f ago", old)
else:
self.requestTime = now
self.currentPlaying = response['playing']
self.songTimeFromRequest = response['t']
returnValue((response['t'], (response['song'] and
URIRef(response['song']))))
estimated = self.songTimeFromRequest
if self.currentSong is not None and self.currentPlaying:
estimated += now - self.requestTime
returnValue((estimated, self.currentSong))
@inlineCallbacks
def updateTimeFromMusic(self):
t1 = time.time()
with self.stats.getMusic.time():
self.songTime, song = yield self.getSongTime()
self.songTimeFetch = time.time()
if song != self.currentSong:
self.currentSong = song
# this may be piling on the handlers
self.graph.addHandler(self.setEffects)
elapsed = time.time() - t1
reactor.callLater(max(0, self.period - elapsed),
self.updateTimeFromMusic)
def estimatedSongTime(self):
now = time.time()
t = self.songTime
if self.currentPlaying:
t += max(0, now - self.songTimeFetch)
return t
@inlineCallbacks
def sendLevels(self):
t1 = time.time()
log.debug("time since last call: %.1f ms" %
(1000 * (t1 - self.lastSendLevelsTime)))
self.lastSendLevelsTime = t1
try:
with self.stats.sendLevels.time():
if self.currentSong is not None:
log.debug('allEffectOutputs')
with self.stats.evals.time():
outputs = self.allEffectOutputs(
self.estimatedSongTime())
log.debug('combineOutputs')
combined = self.combineOutputs(outputs)
self.logLevels(t1, combined)
log.debug('sendOutput')
with self.stats.sendOutput.time():
yield self.sendOutput(combined)
elapsed = time.time() - t1
dt = max(0, self.period - elapsed)
except Exception:
self.stats.errors += 1
traceback.print_exc()
dt = .5
reactor.callLater(dt, self.sendLevels)
def combineOutputs(self, outputs):
"""pick usable effect outputs and reduce them into one for sendOutput"""
outputs = [x for x in outputs if isinstance(x, Submaster.Submaster)]
out = Submaster.sub_maxes(*outputs)
return out
@inlineCallbacks
def sendOutput(self, combined):
dmx = combined.get_dmx_list()
yield dmxclient.outputlevels(dmx, twisted=True)
def allEffectOutputs(self, songTime):
outputs = []
for e in self.currentEffects:
try:
out = e.eval(songTime)
if isinstance(out, (list, tuple)):
outputs.extend(out)
else:
outputs.append(out)
except Exception as exc:
now = time.time()
if now > self.lastErrorLog + 5:
if hasattr(exc, 'expr'):
log.exception('in expression %r', exc.expr)
log.error("effect %s: %s" % (e.uri, exc))
self.lastErrorLog = now
log.debug('eval %s effects, got %s outputs', len(self.currentEffects),
len(outputs))
return outputs
def logLevels(self, now, out):
# this would look nice on the top of the effecteval web pages too
if log.isEnabledFor(logging.DEBUG):
log.debug(self.logMessage(out))
else:
if now > self.lastLogTime + 5:
msg = self.logMessage(out)
if msg != self.lastLogMsg:
log.info(msg)
self.lastLogMsg = msg
self.lastLogTime = now
def logMessage(self, out):
return ("send dmx: {%s}" % ", ".join(
"%r: %.3g" % (str(k), v) for k, v in out.get_levels().items()))
Z = numpy.zeros((50, 3), dtype=numpy.float16)
class ControlBoard(object):
def __init__(
self,
dev='/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A7027NYX-if00-port0'
):
log.info('opening %s', dev)
self._dev = serial.Serial(dev, baudrate=115200)
def _8bitMessage(self, floatArray):
px255 = (numpy.clip(floatArray, 0, 1) * 255).astype(numpy.uint8)
return px255.reshape((-1,)).tostring()
def setStrip(self, which, pixels):
"""
which: 0 or 1 to pick the strip
pixels: (50, 3) array of 0..1 floats
"""
command = {0: '\x00', 1: '\x01'}[which]
if pixels.shape != (50, 3):
raise ValueError("pixels was %s" % pixels.shape)
self._dev.write('\x60' + command + self._8bitMessage(pixels))
self._dev.flush()
def setUv(self, which, level):
"""
which: 0 or 1
level: 0 to 1
"""
command = {0: '\x02', 1: '\x03'}[which]
self._dev.write('\x60' + command +
chr(int(max(0, min(1, level)) * 255)))
self._dev.flush()
def setRgb(self, color):
"""
color: (1, 3) array of 0..1 floats
"""
if color.shape != (1, 3):
raise ValueError("color was %s" % color.shape)
self._dev.write('\x60\x04' + self._8bitMessage(color))
self._dev.flush()
class LedLoop(EffectLoop):
def initOutput(self):
self.board = ControlBoard()
self.lastSent = {} # what's in arduino's memory
def combineOutputs(self, outputs):
combined = {
'L': Z,
'R': Z,
'blacklight0': 0,
'blacklight1': 0,
'W': numpy.zeros((1, 3), dtype=numpy.float16)
}
for out in outputs:
log.debug('combine output %r', out)
# workaround- somehow these subs that drive fx aren't
# sending their fx during playback (KC only), so we react
# to the sub itself
if isinstance(out, Submaster.Submaster) and '*' in out.name:
level = float(out.name.split('*')[1])
n = out.name.split('*')[0]
if n == 'widered':
out = Effects.Strip.solid('LRW', (1, 0, 0)) * level
if n == 'widegreen':
out = Effects.Strip.solid('LRW', (0, 1, 0)) * level
if n == 'wideblue':
out = Effects.Strip.solid('LRW', (0, 0, 1)) * level
if n == 'whiteled':
out = Effects.Strip.solid('LRW', (1, .7, .7)) * level
if n == 'blacklight':
out = Effects.Blacklight(level) # missing blues!
if isinstance(out, Effects.Blacklight):
# no picking yet
#key = 'blacklight%s' % out.which
for key in ['blacklight0', 'blacklight1']:
combined[key] = max(combined[key], out)
elif isinstance(out, Effects.Strip):
pixels = numpy.array(out.pixels, dtype=numpy.float16)
for w in out.which:
combined[w] = numpy.maximum(
combined[w], pixels[:1, :] if w == 'W' else pixels)
return combined
@inlineCallbacks
def sendOutput(self, combined):
for meth, selectArgs, value in [
('setStrip', (0,), combined['L']),
('setStrip', (1,), combined['R']),
('setUv', (0,), combined['blacklight0']),
('setUv', (1,), combined['blacklight1']),
('setRgb', (), combined['W']),
]:
key = (meth, selectArgs)
compValue = value.tolist() if isinstance(value,
numpy.ndarray) else value
if self.lastSent.get(key) == compValue:
continue
log.debug('value changed: %s(%s %s)', meth, selectArgs, value)
getattr(self.board, meth)(*(selectArgs + (value,)))
self.lastSent[key] = compValue
yield succeed(None) # there was an attempt at an async send
def logMessage(self, out):
return str([(w, p.tolist() if isinstance(p, numpy.ndarray) else p)
for w, p in out.items()])
def makeEffectLoop(graph, stats, outputWhere):
if outputWhere == 'dmx':
return EffectLoop(graph, stats)
elif outputWhere == 'leds':
return LedLoop(graph, stats)
else:
raise NotImplementedError("unknown output system %r" % outputWhere)
|