changeset 1252:fccb6ca6516d

currentSubLevel lets subs drive effects Ignore-this: 13c5c1743c456386b8aca36a189d905b
author drewp@bigasterisk.com
date Sat, 13 Jun 2015 06:09:24 +0000
parents b174f95a3b8d
children 7817e1ef0ff0
files light9/effecteval/effect.py light9/effecteval/effectloop.py
diffstat 2 files changed, 37 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/light9/effecteval/effect.py	Sat Jun 13 04:33:52 2015 +0000
+++ b/light9/effecteval/effect.py	Sat Jun 13 06:09:24 2015 +0000
@@ -127,6 +127,32 @@
             inNames.discard(outName)
             deps[outName] = inNames
         self.codes = [codeFromOutput[n] for n in toposort.toposort_flatten(deps)]
+
+    def _currentSubSettingValues(self, sub):
+        """what KC subSettings are setting levels right now?"""
+        cs = self.graph.currentState
+        with cs(tripleFilter=(None, L9['sub'], sub)) as g1:
+            for subj in g1.subjects(L9['sub'], sub):
+                with cs(tripleFilter=(subj, None, None)) as g2:
+                    if (subj, RDF.type, L9['SubSetting']) in g2:
+                        v = g2.value(subj, L9['level']).toPython()
+                        yield v
+
+    def currentSubLevel(self, uri):
+        """what's the max level anyone (probably KC) is
+        holding this sub to right now?"""
+        if isinstance(uri, Submaster.Submaster):
+            # likely the uri was spotted and replaced
+            uri = uri.uri
+        if not isinstance(uri, URIRef):
+            raise TypeError("got %r" % uri)
+
+        foundLevels = list(self._currentSubSettingValues(uri))
+        
+        if not foundLevels:
+            return 0
+        
+        return max(foundLevels)
         
     def eval(self, songTime):
         ns = {'t': songTime}
@@ -134,6 +160,7 @@
 
         ns.update(dict(
             curve=lambda c, t: c.eval(t),
+            currentSubLevel=self.currentSubLevel,
             ))
 
         for c in self.codes:
--- a/light9/effecteval/effectloop.py	Sat Jun 13 04:33:52 2015 +0000
+++ b/light9/effecteval/effectloop.py	Sat Jun 13 06:09:24 2015 +0000
@@ -154,7 +154,7 @@
                 now = time.time()
                 if now > self.lastErrorLog + 5:
                     if hasattr(exc, 'expr'):
-                        log.error('in expression %r', 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))
@@ -182,7 +182,7 @@
 
 class ControlBoard(object):
     def __init__(self, dev='/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A7027NYX-if00-port0'):
-        self._dev = None#serial.Serial(dev, baudrate=115200)
+        self._dev = serial.Serial(dev, baudrate=115200)
 
     def _8bitMessage(self, floatArray):
         px255 = (numpy.clip(floatArray, 0, 1) * 255).astype(numpy.uint8)
@@ -221,7 +221,7 @@
 class LedLoop(EffectLoop):
     def initOutput(self):
         self.board = ControlBoard()
-        self.lastSent = {}
+        self.lastSent = {} # what's in arduino's memory
         
     def combineOutputs(self, outputs):
         combined = {'L': Z, 'R': Z,
@@ -229,6 +229,7 @@
                     'rgb': numpy.zeros((1, 3), dtype=numpy.float16)}
         
         for out in outputs:
+            log.debug('combine output %r', out)
             if isinstance(out, Effects.Blacklight):
                 key = 'blacklight%s' % out.which
                 combined[key] = max(combined[key], out)
@@ -247,14 +248,17 @@
                 ('setUv', (0,), combined['blacklight0']),
                 ('setUv', (1,), combined['blacklight1']),
                 ('setRgb', (), combined['rgb']),
-        ]:
+            ]:
             key = (meth, selectArgs)
-            if self.lastSent.get(key) == value:
+            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] = value
+            self.lastSent[key] = compValue
                 
         yield succeed(None) # there was an attempt at an async send