Changeset - 7772cc48e016
[Not reviewed]
default
! ! !
drewp@bigasterisk.com - 6 years ago 2019-05-21 23:56:12
drewp@bigasterisk.com
reformat all python
Ignore-this: 1135b78893f8b3d31badddda7f45678f
55 files changed:
0 comments (0 inline, 0 general)
bin/ascoltami2
Show inline comments
 
@@ -4,7 +4,7 @@ from twisted.internet import reactor
 
import web, thread, sys, optparse, logging
 
from rdflib import URIRef
 
sys.path.append(".")
 
sys.path.append('/usr/lib/python2.7/dist-packages') # For gi
 
sys.path.append('/usr/lib/python2.7/dist-packages')  # For gi
 

	
 
import gi
 
gi.require_version('Gst', '1.0')
 
@@ -17,7 +17,9 @@ from light9 import networking, showconfi
 

	
 
from gi.repository import GObject, Gst, Gtk
 

	
 

	
 
class App(object):
 

	
 
    def __init__(self, graph, show):
 
        self.graph = graph
 
        self.player = Player(onEOS=self.onEOS)
 
@@ -32,21 +34,27 @@ class App(object):
 

	
 
        try:
 
            nextSong = self.playlist.nextSong(thisSongUri)
 
        except NoSuchSong: # we're at the end of the playlist
 
        except NoSuchSong:  # we're at the end of the playlist
 
            return
 

	
 
        self.player.setSong(songLocation(graph, nextSong), play=False)
 

	
 

	
 
if __name__ == "__main__":
 
    GObject.threads_init()
 
    Gst.init(None)
 

	
 
    parser = optparse.OptionParser()
 
    parser.add_option('--show',
 
        help='show URI, like http://light9.bigasterisk.com/show/dance2008', default=showconfig.showUri())
 
    parser.add_option("-v", "--verbose", action="store_true",
 
    parser.add_option(
 
        '--show',
 
        help='show URI, like http://light9.bigasterisk.com/show/dance2008',
 
        default=showconfig.showUri())
 
    parser.add_option("-v",
 
                      "--verbose",
 
                      action="store_true",
 
                      help="logging.DEBUG")
 
    parser.add_option("--twistedlog", action="store_true",
 
    parser.add_option("--twistedlog",
 
                      action="store_true",
 
                      help="twisted logging")
 
    (options, args) = parser.parse_args()
 

	
 
@@ -54,7 +62,7 @@ if __name__ == "__main__":
 

	
 
    if not options.show:
 
        raise ValueError("missing --show http://...")
 
            
 

	
 
    graph = showconfig.getGraph()
 
    app = App(graph, URIRef(options.show))
 
    if options.twistedlog:
bin/bcf_puppet_demo
Show inline comments
 
@@ -5,12 +5,13 @@ tiny bcf2000 controller demo
 
from bcf2000 import BCF2000
 
from twisted.internet import reactor
 

	
 

	
 
class PuppetSliders(BCF2000):
 

	
 
    def valueIn(self, name, value):
 
        if name == 'slider1':
 
            self.valueOut('slider5', value)
 

	
 

	
 

	
 
b = PuppetSliders()
 
reactor.run()
bin/bumppad
Show inline comments
 
#!bin/python
 
from __future__ import division,nested_scopes
 
from __future__ import division, nested_scopes
 
import sys, time, math
 
import Tkinter as tk
 

	
 
@@ -7,64 +7,82 @@ import run_local
 
import light9.dmxclient as dmxclient
 
from light9.TLUtility import make_attributes_from_args
 

	
 
from light9.Submaster import Submaster,sub_maxes
 
from light9.Submaster import Submaster, sub_maxes
 

	
 

	
 
class pad(tk.Frame):
 
    levs = None # Submaster : level
 
    def __init__(self,master,root,mag):
 
        make_attributes_from_args('master','mag')
 
        tk.Frame.__init__(self,master)
 
        self.levs={}
 
        for xy,key,subname in [
 
            ((1,1),'KP_Up','centered'),
 
            ((1,3),"KP_Down",'third-c'),
 
            ((0,2),'KP_Left','scoop-l'),
 
            ((2,2),'KP_Right','scoop-r'),
 
            ((1,0),'KP_Divide','cyc'),
 
            ((0,3),"KP_End",'hottest'),
 
            ((2,3),'KP_Next','deepblues'),
 
            ((0,4),'KP_Insert',"zip_red"),
 
            ((2,4),'KP_Delete',"zip_orange"),
 
            ((3,1),'KP_Add','strobedim'),
 
            ((3,3),'KP_Enter','zip_blue'),
 
            ((1,2),'KP_Begin','scoop-c'),                            
 
                        ]:
 
            
 
    levs = None  # Submaster : level
 

	
 
    def __init__(self, master, root, mag):
 
        make_attributes_from_args('master', 'mag')
 
        tk.Frame.__init__(self, master)
 
        self.levs = {}
 
        for xy, key, subname in [
 
            ((1, 1), 'KP_Up', 'centered'),
 
            ((1, 3), "KP_Down", 'third-c'),
 
            ((0, 2), 'KP_Left', 'scoop-l'),
 
            ((2, 2), 'KP_Right', 'scoop-r'),
 
            ((1, 0), 'KP_Divide', 'cyc'),
 
            ((0, 3), "KP_End", 'hottest'),
 
            ((2, 3), 'KP_Next', 'deepblues'),
 
            ((0, 4), 'KP_Insert', "zip_red"),
 
            ((2, 4), 'KP_Delete', "zip_orange"),
 
            ((3, 1), 'KP_Add', 'strobedim'),
 
            ((3, 3), 'KP_Enter', 'zip_blue'),
 
            ((1, 2), 'KP_Begin', 'scoop-c'),
 
        ]:
 

	
 
            sub = Submaster(subname)
 
            self.levs[sub]=0
 
            
 
            l = tk.Label(self,font="arial 12 bold",anchor='w',height=2,
 
                         relief='groove',bd=5,
 
                         text="%s\n%s" % (key.replace('KP_',''),sub.name))
 
            l.grid(column=xy[0],row=xy[1],sticky='news')
 
            
 
            root.bind("<KeyPress-%s>"%key,
 
                      lambda ev,sub=sub: self.bumpto(sub,1))
 
            root.bind("<KeyRelease-%s>"%key,
 
                      lambda ev,sub=sub: self.bumpto(sub,0))
 
    def bumpto(self,sub,lev):
 
        now=time.time()
 
        self.levs[sub]=lev*self.mag.get()
 
            self.levs[sub] = 0
 

	
 
            l = tk.Label(self,
 
                         font="arial 12 bold",
 
                         anchor='w',
 
                         height=2,
 
                         relief='groove',
 
                         bd=5,
 
                         text="%s\n%s" % (key.replace('KP_', ''), sub.name))
 
            l.grid(column=xy[0], row=xy[1], sticky='news')
 

	
 
            root.bind(
 
                "<KeyPress-%s>" % key, lambda ev, sub=sub: self.bumpto(sub, 1))
 
            root.bind("<KeyRelease-%s>" % key,
 
                      lambda ev, sub=sub: self.bumpto(sub, 0))
 

	
 
    def bumpto(self, sub, lev):
 
        now = time.time()
 
        self.levs[sub] = lev * self.mag.get()
 
        self.master.after_idle(self.output)
 

	
 
    def output(self):
 
        dmx = sub_maxes(*[s*l for s,l in self.levs.items()]).get_dmx_list()
 
        dmxclient.outputlevels(dmx,clientid="bumppad")
 
        
 
root=tk.Tk()
 
        dmx = sub_maxes(*[s * l for s, l in self.levs.items()]).get_dmx_list()
 
        dmxclient.outputlevels(dmx, clientid="bumppad")
 

	
 

	
 
root = tk.Tk()
 
root.tk_setPalette("maroon4")
 
root.wm_title("bumppad")
 
mag = tk.DoubleVar()
 

	
 
tk.Label(root,text="Keypad press/release activate sub; 1..5 set mag",
 
             font="Helvetica -12 italic",anchor='w').pack(side='bottom',fill='x')
 
                      
 
pad(root,root,mag).pack(side='left',fill='both',exp=1)
 
tk.Label(root,
 
         text="Keypad press/release activate sub; 1..5 set mag",
 
         font="Helvetica -12 italic",
 
         anchor='w').pack(side='bottom', fill='x')
 

	
 
pad(root, root, mag).pack(side='left', fill='both', exp=1)
 

	
 
magscl = tk.Scale(root,orient='vertical',from_=1,to=0,res=.01,
 
                   showval=1,variable=mag,label='mag',relief='raised',bd=1)
 
for i in range(1,6):
 
    root.bind("<Key-%s>"%i,lambda ev,i=i: mag.set(math.sqrt((i )/5)))
 
magscl.pack(side='left',fill='y')
 

	
 
magscl = tk.Scale(root,
 
                  orient='vertical',
 
                  from_=1,
 
                  to=0,
 
                  res=.01,
 
                  showval=1,
 
                  variable=mag,
 
                  label='mag',
 
                  relief='raised',
 
                  bd=1)
 
for i in range(1, 6):
 
    root.bind("<Key-%s>" % i, lambda ev, i=i: mag.set(math.sqrt((i) / 5)))
 
magscl.pack(side='left', fill='y')
 

	
 
root.mainloop()
bin/captureDevice
Show inline comments
 
@@ -26,15 +26,18 @@ from rdfdb.patch import Patch
 

	
 
stats = scales.collection('/webServer', scales.PmfStat('setAttr'))
 

	
 

	
 
class Camera(object):
 

	
 
    def __init__(self, imageUrl):
 
        self.imageUrl = imageUrl
 
    
 

	
 
    def takePic(self, uri, writePath):
 
        log.info('takePic %s', uri)
 
        return treq.get(self.imageUrl).addCallbacks(
 
            lambda r: self._done(writePath, r), log.error)
 
        
 
        return treq.get(
 
            self.imageUrl).addCallbacks(lambda r: self._done(writePath, r),
 
                                        log.error)
 

	
 
    @inlineCallbacks
 
    def _done(self, writePath, response):
 
        jpg = yield response.content()
 
@@ -46,18 +49,21 @@ class Camera(object):
 
            out.write(jpg)
 
        log.info('wrote %s', writePath)
 

	
 

	
 
def deferSleep(sec):
 
    d = Deferred()
 
    reactor.callLater(sec, d.callback, None)
 
    return d
 
    
 

	
 

	
 
class Capture(object):
 
    firstMoveTime = 3
 
    settleTime = .5
 

	
 
    def __init__(self, graph, dev):
 
        self.graph = graph
 
        self.dev = dev
 
        
 

	
 
        def steps(a, b, n):
 
            return [round(a + (b - a) * i / n, 5) for i in range(n)]
 

	
 
@@ -72,8 +78,8 @@ class Capture(object):
 
        # aura
 
        rxSteps = steps(0.15, .95, 10)
 
        rySteps = steps(0, .9, 5)
 
        zoomSteps = steps(.6, .9, 3)       
 
        
 
        zoomSteps = steps(.6, .9, 3)
 

	
 
        row = 0
 
        for ry in rySteps:
 
            xSteps = rxSteps[:]
 
@@ -82,31 +88,36 @@ class Capture(object):
 
            row += 1
 
            for rx in xSteps:
 
                for zoom in zoomSteps:
 
                    self.toGather.append(DeviceSettings(graph, [
 
                        (dev, L9['rx'], rx),
 
                        (dev, L9['ry'], ry),
 
                        (dev, L9['color'], '#ffffff'),
 
                        (dev, L9['zoom'], zoom),
 
                        #(dev, L9['focus'], 0.13),
 
                    ]))
 
                    self.toGather.append(
 
                        DeviceSettings(
 
                            graph,
 
                            [
 
                                (dev, L9['rx'], rx),
 
                                (dev, L9['ry'], ry),
 
                                (dev, L9['color'], '#ffffff'),
 
                                (dev, L9['zoom'], zoom),
 
                                #(dev, L9['focus'], 0.13),
 
                            ]))
 

	
 
        self.devTail = dev.rsplit('/')[-1]
 
        self.session = URIRef('/'.join([showconfig.showUri(),
 
                                   'capture', self.devTail, self.captureId]))
 
        self.session = URIRef('/'.join(
 
            [showconfig.showUri(), 'capture', self.devTail, self.captureId]))
 
        self.ctx = URIRef(self.session + '/index')
 
                
 
        self.graph.patch(Patch(addQuads=[
 
            (self.session, RDF.type, L9['CaptureSession'], self.ctx),
 
        ]))
 
                
 

	
 
        self.graph.patch(
 
            Patch(addQuads=[
 
                (self.session, RDF.type, L9['CaptureSession'], self.ctx),
 
            ]))
 

	
 
        self.numPics = 0
 
        self.settingsCache = set()
 
        self.step().addErrback(log.error)
 

	
 
    def off(self):
 
        return sendToCollector(client='captureDevice', session='main',
 
        return sendToCollector(client='captureDevice',
 
                               session='main',
 
                               settings=DeviceSettings(self.graph, []))
 
        
 

	
 
    @inlineCallbacks
 
    def step(self):
 
        if not self.toGather:
 
@@ -115,61 +126,73 @@ class Capture(object):
 
            reactor.stop()
 
            return
 
        settings = self.toGather.pop()
 
        
 

	
 
        log.info('[%s left] move to %r', len(self.toGather), settings)
 
        yield sendToCollector(client='captureDevice', session='main',
 
        yield sendToCollector(client='captureDevice',
 
                              session='main',
 
                              settings=settings)
 
        
 
        yield deferSleep(self.firstMoveTime if self.numPics == 0 else
 
                         self.settleTime)
 
        
 

	
 
        yield deferSleep(self.firstMoveTime if self.numPics ==
 
                         0 else self.settleTime)
 

	
 
        picId = 'pic%s' % self.numPics
 
        path = '/'.join([
 
            'capture', self.devTail, self.captureId, picId]) + '.jpg'
 
        path = '/'.join(['capture', self.devTail, self.captureId, picId
 
                        ]) + '.jpg'
 
        uri = URIRef(self.session + '/' + picId)
 
        
 

	
 
        yield camera.takePic(uri, os.path.join(showconfig.root(), path))
 
        self.numPics += 1
 

	
 
        writeCaptureDescription(self.graph, self.ctx, self.session, uri,
 
                                self.dev,
 
                                path, self.settingsCache, settings)
 
        
 
                                self.dev, path, self.settingsCache, settings)
 

	
 
        reactor.callLater(0, self.step)
 

	
 
        
 

	
 
camera = Camera('http://plus:8200/picamserve/pic?res=1080&resize=800&iso=800&redgain=1.6&bluegain=1.6&shutter=60000&x=0&w=1&y=0&h=.952')
 
camera = Camera(
 
    'http://plus:8200/picamserve/pic?res=1080&resize=800&iso=800&redgain=1.6&bluegain=1.6&shutter=60000&x=0&w=1&y=0&h=.952'
 
)
 

	
 

	
 
class Attrs(PrettyErrorHandler, cyclone.web.RequestHandler):
 

	
 
    def put(self):
 
        with stats.setAttr.time():
 
            client, clientSession, settings, sendTime = parseJsonMessage(self.request.body)
 
            client, clientSession, settings, sendTime = parseJsonMessage(
 
                self.request.body)
 
            self.set_status(202)
 

	
 

	
 
def launch(graph):
 

	
 
    cap = Capture(graph, dev=L9['device/aura5'])
 
    reactor.listenTCP(networking.captureDevice.port,
 
                      cyclone.web.Application(handlers=[
 
                          (r'/()', cyclone.web.StaticFileHandler,
 
                           {"path" : "light9/web", "default_filename" : "captureDevice.html"}),
 
                          (r'/()', cyclone.web.StaticFileHandler, {
 
                              "path": "light9/web",
 
                              "default_filename": "captureDevice.html"
 
                          }),
 
                          (r'/stats', StatsForCyclone),
 
                      ]),
 
                      interface='::')
 
    log.info('serving http on %s', networking.captureDevice.port)
 
    
 

	
 

	
 
def main():
 
    parser = optparse.OptionParser()
 
    parser.add_option("-v", "--verbose", action="store_true",
 
    parser.add_option("-v",
 
                      "--verbose",
 
                      action="store_true",
 
                      help="logging.DEBUG")
 
    (options, args) = parser.parse_args()
 
    log.setLevel(logging.DEBUG if options.verbose else logging.INFO)
 

	
 
    graph = SyncedGraph(networking.rdfdb.url, "captureDevice")
 

	
 
    graph.initiallySynced.addCallback(lambda _: launch(graph)).addErrback(log.error)
 
    graph.initiallySynced.addCallback(lambda _: launch(graph)).addErrback(
 
        log.error)
 
    reactor.run()
 

	
 

	
 
if __name__ == '__main__':
 
    main()
bin/clientdemo
Show inline comments
 
@@ -16,12 +16,13 @@ if __name__ == "__main__":
 
    g = SyncedGraph(networking.rdfdb.url, "clientdemo")
 

	
 
    from light9.Submaster import PersistentSubmaster
 
    sub = PersistentSubmaster(graph=g, uri=URIRef("http://light9.bigasterisk.com/sub/bcools"))
 
    sub = PersistentSubmaster(
 
        graph=g, uri=URIRef("http://light9.bigasterisk.com/sub/bcools"))
 

	
 
    #get sub to show its updating name, then push that all the way into KC gui so we can see just names refresh in there
 

	
 
    L9 = Namespace("http://light9.bigasterisk.com/")
 

	
 
    L9 = Namespace("http://light9.bigasterisk.com/")
 
    def updateDemoValue():
 
        v = list(g.objects(L9['demo'], L9['is']))
 
        print "demo value is %r" % v
 
@@ -29,8 +30,10 @@ if __name__ == "__main__":
 
    g.addHandler(updateDemoValue)
 

	
 
    def adj():
 
        g.patch(Patch(addQuads=[(L9['demo'], L9['is'], Literal(os.getpid()),
 
                                 L9['clientdemo'])],
 
                      delQuads=[]))
 
        g.patch(
 
            Patch(addQuads=[(L9['demo'], L9['is'], Literal(os.getpid()),
 
                             L9['clientdemo'])],
 
                  delQuads=[]))
 

	
 
    reactor.callLater(2, adj)
 
    reactor.run()
bin/collector
Show inline comments
 
@@ -29,6 +29,7 @@ from light9 import networking
 
from rdfdb.syncedgraph import SyncedGraph
 
from light9.greplin_cyclone import StatsForCyclone
 

	
 

	
 
def parseJsonMessage(msg):
 
    body = json.loads(msg)
 
    settings = []
 
@@ -40,14 +41,15 @@ def parseJsonMessage(msg):
 
        settings.append((URIRef(device), URIRef(attr), value))
 
    return body['client'], body['clientSession'], settings, body['sendTime']
 

	
 

	
 
def startZmq(port, collector):
 
    stats = scales.collection('/zmqServer',
 
                              scales.PmfStat('setAttr'))
 
    
 
    stats = scales.collection('/zmqServer', scales.PmfStat('setAttr'))
 

	
 
    zf = ZmqFactory()
 
    addr = 'tcp://*:%s' % port
 
    log.info('creating zmq endpoint at %r', addr)
 
    e = ZmqEndpoint('bind', addr)
 

	
 
    class Pull(ZmqPullConnection):
 
        #highWaterMark = 3
 
        def onPull(self, message):
 
@@ -55,26 +57,28 @@ def startZmq(port, collector):
 
                # todo: new compressed protocol where you send all URIs up
 
                # front and then use small ints to refer to devices and
 
                # attributes in subsequent requests.
 
                client, clientSession, settings, sendTime = parseJsonMessage(message[0])
 
                client, clientSession, settings, sendTime = parseJsonMessage(
 
                    message[0])
 
                collector.setAttrs(client, clientSession, settings, sendTime)
 
        
 

	
 
    s = Pull(zf, e)
 

	
 

	
 
class WebListeners(object):
 

	
 
    def __init__(self):
 
        self.clients = []
 
        self.pendingMessageForDev = {} # dev: (attrs, outputmap)
 
        self.pendingMessageForDev = {}  # dev: (attrs, outputmap)
 
        self.lastFlush = 0
 
        
 

	
 
    def addClient(self, client):
 
        self.clients.append([client, {}]) # seen = {dev: attrs}
 
        self.clients.append([client, {}])  # seen = {dev: attrs}
 
        log.info('added client %s %s', len(self.clients), client)
 

	
 
    def delClient(self, client):
 
        self.clients = [[c, t] for c, t in self.clients if c != client]
 
        log.info('delClient %s, %s left', client, len(self.clients))
 
        
 

	
 
    def outputAttrsSet(self, dev, attrs, outputMap):
 
        """called often- don't be slow"""
 

	
 
@@ -94,8 +98,8 @@ class WebListeners(object):
 
        while self.pendingMessageForDev:
 
            dev, (attrs, outputMap) = self.pendingMessageForDev.popitem()
 

	
 
            msg = None # lazy, since makeMsg is slow
 
            
 
            msg = None  # lazy, since makeMsg is slow
 

	
 
            # this omits repeats, but can still send many
 
            # messages/sec. Not sure if piling up messages for the browser
 
            # could lead to slowdowns in the real dmx output.
 
@@ -112,16 +116,23 @@ class WebListeners(object):
 
        attrRows = []
 
        for attr, val in attrs.items():
 
            output, index = outputMap[(dev, attr)]
 
            attrRows.append({'attr': attr.rsplit('/')[-1],
 
                             'val': val,
 
                             'chan': (output.shortId(), index + 1)})
 
            attrRows.append({
 
                'attr': attr.rsplit('/')[-1],
 
                'val': val,
 
                'chan': (output.shortId(), index + 1)
 
            })
 
        attrRows.sort(key=lambda r: r['chan'])
 
        for row in attrRows:
 
            row['chan'] = '%s %s' % (row['chan'][0], row['chan'][1])
 

	
 
        msg = json.dumps({'outputAttrsSet': {'dev': dev, 'attrs': attrRows}}, sort_keys=True)
 
        msg = json.dumps({'outputAttrsSet': {
 
            'dev': dev,
 
            'attrs': attrRows
 
        }},
 
                         sort_keys=True)
 
        return msg
 
    
 

	
 

	
 
class Updates(cyclone.websocket.WebSocketHandler):
 

	
 
    def connectionMade(self, *args, **kwargs):
 
@@ -134,23 +145,27 @@ class Updates(cyclone.websocket.WebSocke
 
    def messageReceived(self, message):
 
        json.loads(message)
 

	
 

	
 
stats = scales.collection('/webServer', scales.PmfStat('setAttr'))
 

	
 

	
 
class Attrs(PrettyErrorHandler, cyclone.web.RequestHandler):
 

	
 
    def put(self):
 
        with stats.setAttr.time():
 
            client, clientSession, settings, sendTime = parseJsonMessage(self.request.body)
 
            self.settings.collector.setAttrs(client, clientSession, settings, sendTime)
 
            client, clientSession, settings, sendTime = parseJsonMessage(
 
                self.request.body)
 
            self.settings.collector.setAttrs(client, clientSession, settings,
 
                                             sendTime)
 
            self.set_status(202)
 

	
 

	
 
            
 
def launch(graph, doLoadTest=False):
 
    try:
 
        # todo: drive outputs with config files
 
        outputs = [
 
            # EnttecDmx(L9['output/dmxA/'], '/dev/dmx3', 80),
 
             Udmx(L9['output/dmxA/'], bus=5, numChannels=80),
 
            Udmx(L9['output/dmxA/'], bus=5, numChannels=80),
 
            #DummyOutput(L9['output/dmxA/'], 80),
 
            Udmx(L9['output/dmxB/'], bus=7, numChannels=500),
 
        ]
 
@@ -162,15 +177,19 @@ def launch(graph, doLoadTest=False):
 
    c = Collector(graph, outputs, listeners)
 

	
 
    startZmq(networking.collectorZmq.port, c)
 
    
 

	
 
    reactor.listenTCP(networking.collector.port,
 
                      cyclone.web.Application(handlers=[
 
                          (r'/()', cyclone.web.StaticFileHandler,
 
                           {"path" : "light9/collector/web", "default_filename" : "index.html"}),
 
                          (r'/()', cyclone.web.StaticFileHandler, {
 
                              "path": "light9/collector/web",
 
                              "default_filename": "index.html"
 
                          }),
 
                          (r'/updates', Updates),
 
                          (r'/attrs', Attrs),
 
                          (r'/stats', StatsForCyclone),
 
                      ], collector=c, listeners=listeners),
 
                      ],
 
                                              collector=c,
 
                                              listeners=listeners),
 
                      interface='::')
 
    log.info('serving http on %s, zmq on %s', networking.collector.port,
 
             networking.collectorZmq.port)
 
@@ -180,28 +199,38 @@ def launch(graph, doLoadTest=False):
 
        # requests when there's free time
 
        def afterWarmup():
 
            log.info('running collector_loadtest')
 
            d = utils.getProcessValue('bin/python', ['bin/collector_loadtest.py'])
 
            d = utils.getProcessValue('bin/python',
 
                                      ['bin/collector_loadtest.py'])
 

	
 
            def done(*a):
 
                log.info('loadtest done')
 
                reactor.stop()
 

	
 
            d.addCallback(done)
 

	
 
        reactor.callLater(2, afterWarmup)
 
    
 

	
 

	
 
def main():
 
    parser = optparse.OptionParser()
 
    parser.add_option("-v", "--verbose", action="store_true",
 
    parser.add_option("-v",
 
                      "--verbose",
 
                      action="store_true",
 
                      help="logging.DEBUG")
 
    parser.add_option("--loadtest", action="store_true",
 
    parser.add_option("--loadtest",
 
                      action="store_true",
 
                      help="call myself with some synthetic load then exit")
 
    (options, args) = parser.parse_args()
 
    log.setLevel(logging.DEBUG if options.verbose else logging.INFO)
 

	
 
    logging.getLogger('colormath').setLevel(logging.INFO)
 
    
 

	
 
    graph = SyncedGraph(networking.rdfdb.url, "collector")
 

	
 
    graph.initiallySynced.addCallback(lambda _: launch(graph, options.loadtest)).addErrback(lambda e: reactor.crash())
 
    graph.initiallySynced.addCallback(lambda _: launch(graph, options.loadtest)
 
                                     ).addErrback(lambda e: reactor.crash())
 
    reactor.run()
 

	
 

	
 
if __name__ == '__main__':
 
    main()
bin/collector_loadtest.py
Show inline comments
 
@@ -7,6 +7,8 @@ from twisted.internet import reactor
 
import time
 
import logging
 
log.setLevel(logging.DEBUG)
 

	
 

	
 
def loadTest():
 
    print "scheduling loadtest"
 
    n = 2500
 
@@ -14,23 +16,27 @@ def loadTest():
 
    session = "loadtest%s" % time.time()
 
    offset = 0
 
    for i in range(n):
 

	
 
        def send(i):
 
            if i % 100 == 0:
 
                log.info('sendToCollector %s', i)
 
            d = sendToCollector("http://localhost:999999/", session,
 
                    [[DEV["backlight1"], L9["color"], "#ffffff"], 
 
                     [DEV["backlight2"], L9["color"], "#ffffff"], 
 
                     [DEV["backlight3"], L9["color"], "#ffffff"], 
 
                     [DEV["backlight4"], L9["color"], "#ffffff"], 
 
                     [DEV["backlight5"], L9["color"], "#ffffff"], 
 
                     [DEV["down2"], L9["color"], "#ffffff"], 
 
                     [DEV["down3"], L9["color"], "#ffffff"], 
 
                     [DEV["down4"], L9["color"], "#ffffff"], 
 
                     [DEV["houseSide"], L9["level"], .8], 
 
                     [DEV["backlight5"], L9["uv"], 0.011]])
 
                                [[DEV["backlight1"], L9["color"], "#ffffff"],
 
                                 [DEV["backlight2"], L9["color"], "#ffffff"],
 
                                 [DEV["backlight3"], L9["color"], "#ffffff"],
 
                                 [DEV["backlight4"], L9["color"], "#ffffff"],
 
                                 [DEV["backlight5"], L9["color"], "#ffffff"],
 
                                 [DEV["down2"], L9["color"], "#ffffff"],
 
                                 [DEV["down3"], L9["color"], "#ffffff"],
 
                                 [DEV["down4"], L9["color"], "#ffffff"],
 
                                 [DEV["houseSide"], L9["level"], .8],
 
                                 [DEV["backlight5"], L9["uv"], 0.011]])
 

	
 
            def ontime(dt, i=i):
 
                times[i] = dt
 

	
 
            d.addCallback(ontime)
 

	
 
        reactor.callLater(offset, send, i)
 
        offset += .002
 

	
 
@@ -39,8 +45,10 @@ def loadTest():
 
        with open('/tmp/times', 'w') as f:
 
            f.write(''.join('%s\n' % t for t in times))
 
        reactor.stop()
 
    reactor.callLater(offset+.5, done)
 

	
 
    reactor.callLater(offset + .5, done)
 
    reactor.run()
 

	
 

	
 
if __name__ == '__main__':
 
    loadTest()
bin/curvecalc
Show inline comments
 
#!bin/python
 

	
 
"""
 
now launches like this:
 
% bin/curvecalc http://light9.bigasterisk.com/show/dance2007/song1
 
@@ -12,7 +11,7 @@ todo: curveview should preserve more obj
 
from __future__ import division
 

	
 
import sys
 
sys.path.append('/usr/lib/python2.7/dist-packages') # For gtk
 
sys.path.append('/usr/lib/python2.7/dist-packages')  # For gtk
 
from twisted.internet import gtk3reactor
 
gtk3reactor.install()
 
from twisted.internet import reactor
 
@@ -24,13 +23,13 @@ from gi.repository import GObject
 
from gi.repository import Gdk
 

	
 
from urlparse import parse_qsl
 
import louie as dispatcher 
 
import louie as dispatcher
 
from rdflib import URIRef, Literal, RDF, RDFS
 
import logging
 

	
 
from run_local import log
 
from light9 import showconfig, networking
 
from light9.curvecalc import curveview 
 
from light9.curvecalc import curveview
 
from light9.curvecalc.curve import Curveset
 
from light9.curvecalc.curveedit import serveCurveEdit
 
from light9.curvecalc.musicaccess import Music
 
@@ -46,34 +45,40 @@ from rdfdb.patch import Patch
 
from rdfdb.syncedgraph import SyncedGraph
 
from light9.wavelength import wavelength
 

	
 

	
 
class SubtermExists(ValueError):
 
    pass
 

	
 

	
 
class Main(object):
 

	
 
    def __init__(self, graph, opts, session, curveset, music):
 
        self.graph, self.opts, self.session = graph, opts, session
 
        self.curveset, self.music = curveset, music
 
        self.lastSeenInputTime = 0
 
        self.currentSubterms = [] # Subterm objects that are synced to the graph
 
        self.currentSubterms = [
 
        ]  # Subterm objects that are synced to the graph
 

	
 
        self.setTheme()
 
        wtree = self.wtree = Gtk.Builder()
 
        wtree.add_from_file("light9/curvecalc/curvecalc.glade")
 
        mainwin = wtree.get_object("MainWindow")
 
        
 

	
 
        mainwin.connect("destroy", self.onQuit)
 
        wtree.connect_signals(self)
 

	
 
        mainwin.show_all()
 

	
 
        mainwin.connect("delete-event", lambda *args: reactor.crash())
 

	
 
        def updateTitle():
 
            mainwin.set_title("curvecalc - %s" %
 
                              graph.label(
 
                                  graph.value(session, L9['currentSong'])))
 
            mainwin.set_title(
 
                "curvecalc - %s" %
 
                graph.label(graph.value(session, L9['currentSong'])))
 

	
 
        graph.addHandler(updateTitle)
 

	
 
        songChoice = Observable(None) # to be connected with the session song
 
        songChoice = Observable(None)  # to be connected with the session song
 

	
 
        self.registerGraphToSongChoice(wtree, session, graph, songChoice)
 
        self.registerSongChoiceToGraph(session, graph, songChoice)
 
@@ -82,28 +87,31 @@ class Main(object):
 
        ec = EditChoice(graph, songChoice, label="Editing song:")
 
        wtree.get_object("currentSongEditChoice").add(ec)
 
        ec.show()
 
        
 

	
 
        wtree.get_object("subterms").connect("add", self.onSubtermChildAdded)
 
        
 
        self.refreshCurveView()       
 
        
 

	
 
        self.refreshCurveView()
 

	
 
        self.makeStatusLines(wtree.get_object("status"))
 
        self.setupNewSubZone()
 
        self.acceptDragsOnCurveViews()
 
                
 

	
 
        # may not work
 
        wtree.get_object("paned1").set_position(600)
 

	
 
    def registerGraphToSongChoice(self, wtree, session, graph, songChoice):
 

	
 
        def setSong():
 
            current = graph.value(session, L9['currentSong'])
 
            if not wtree.get_object("followPlayerSongChoice").get_active():
 
                songChoice(current)
 
                dispatcher.send("song_has_changed")
 

	
 
        graph.addHandler(setSong)
 

	
 
    def registerSongChoiceToGraph(self, session, graph, songChoice):
 
        self.muteSongChoiceUntil = 0
 

	
 
        def songChoiceToGraph(newSong):
 
            if newSong is Local:
 
                raise NotImplementedError('what do i patch')
 
@@ -116,12 +124,12 @@ class Main(object):
 
                log.debug('muted')
 
                return
 
            self.muteSongChoiceUntil = now + 1
 
            
 
            graph.patchObject(context=session, subject=session,
 
                              predicate=L9['currentSong'], newObject=newSong)
 
            
 
            
 
            
 

	
 
            graph.patchObject(context=session,
 
                              subject=session,
 
                              predicate=L9['currentSong'],
 
                              newObject=newSong)
 

	
 
        songChoice.subscribe(songChoiceToGraph)
 

	
 
    def registerCurrentPlayerSongToUi(self, wtree, graph, songChoice):
 
@@ -129,47 +137,51 @@ class Main(object):
 
        and
 
        current_player_song 'song' param -> songChoice, if you're in autofollow
 
        """
 

	
 
        def current_player_song(song):
 
            # (this is run on every frame)
 
            ps = wtree.get_object("playerSong")
 
            if URIRef(ps.get_uri()) != song:
 
                log.debug("update playerSong to %s", ps.get_uri())
 

	
 
                def setLabel():
 
                    ps.set_label(graph.label(song))
 

	
 
                graph.addHandler(setLabel)
 
                ps.set_uri(song)
 
            if song != songChoice():
 
                if wtree.get_object("followPlayerSongChoice").get_active():
 
                    log.debug('followPlayerSongChoice is on')
 
                    songChoice(song)
 
                
 

	
 
        dispatcher.connect(current_player_song, "current_player_song")
 
        self.current_player_song = current_player_song
 
        
 

	
 
    def setupNewSubZone(self):
 
        self.wtree.get_object("newSubZone").drag_dest_set(
 
            flags=Gtk.DestDefaults.ALL,
 
            targets=[Gtk.TargetEntry('text/uri-list', 0, 0)],
 
            actions=Gdk.DragAction.COPY)
 
        
 

	
 
    def acceptDragsOnCurveViews(self):
 
        w = self.wtree.get_object("curves")
 
        w.drag_dest_set(flags=Gtk.DestDefaults.ALL,
 
                        targets=[Gtk.TargetEntry('text/uri-list', 0, 0)],
 
                        actions=Gdk.DragAction.COPY)
 
        def recv(widget, context, x, y, selection,
 
                       targetType, time):
 

	
 
        def recv(widget, context, x, y, selection, targetType, time):
 
            subUri = URIRef(selection.data.strip())
 
            print "into curves", subUri
 
            with self.graph.currentState(
 
                    tripleFilter=(subUri, RDFS.label, None)) as current:
 
            with self.graph.currentState(tripleFilter=(subUri, RDFS.label,
 
                                                       None)) as current:
 
                subName = current.label(subUri)
 

	
 
            if '?' in subUri:
 
                subName = self.handleSubtermDrop(subUri)
 
            else:
 
                try:
 
                    self.makeSubterm(subName, withCurve=True,
 
                    self.makeSubterm(subName,
 
                                     withCurve=True,
 
                                     sub=subUri,
 
                                     expr="%s(t)" % subName)
 
                except SubtermExists:
 
@@ -177,23 +189,26 @@ class Main(object):
 
                    # correct-- user mihgt need to fix things
 
                    pass
 
            curveView = self.curvesetView.row(subName).curveView
 
            t = self.lastSeenInputTime # curveView.current_time() # new curve hasn't heard the time yet. this has gotten too messy- everyone just needs to be able to reach the time source
 
            t = self.lastSeenInputTime  # curveView.current_time() # new curve hasn't heard the time yet. this has gotten too messy- everyone just needs to be able to reach the time source
 
            print "time", t
 
            curveView.add_points([(t - .5, 0),
 
                                  (t, 1)])
 
            curveView.add_points([(t - .5, 0), (t, 1)])
 

	
 
        w.connect("drag-data-received", recv)
 
        
 

	
 
    def onDragDataInNewSubZone(self, widget, context, x, y, selection,
 
                       targetType, time):
 
                               targetType, time):
 
        data = URIRef(selection.data.strip())
 
        if '?' in data:
 
            self.handleSubtermDrop(data)
 
            return
 
        with self.graph.currentState(tripleFilter=(data, None, None)) as current:
 
        with self.graph.currentState(tripleFilter=(data, None,
 
                                                   None)) as current:
 
            subName = current.label(data)
 
        self.makeSubterm(newname=subName, withCurve=True, sub=data,
 
        self.makeSubterm(newname=subName,
 
                         withCurve=True,
 
                         sub=data,
 
                         expr="%s(t)" % subName)
 
        
 

	
 
    def handleSubtermDrop(self, data):
 
        params = parse_qsl(data.split('?')[1])
 
        flattened = dict(params)
 
@@ -216,21 +231,21 @@ class Main(object):
 

	
 
    def onRedrawCurves(self, *args):
 
        dispatcher.send("all curves rebuild")
 
        
 

	
 
    def onSubtermsMap(self, *args):
 
        # if this was called too soon, like in __init__, the gtktable
 
        # would get its children but it wouldn't lay anything out that
 
        # I can see, and I'm not sure why. Waiting for map event is
 
        # just a wild guess.
 
        self.graph.addHandler(self.set_subterms_from_graph)
 
        
 

	
 
    def onNewSubterm(self, *args):
 
        self.makeSubterm(Literal(""), withCurve=False)
 
        return
 

	
 
        # pretty sure i don't want this back, but not completely sure
 
        # what the UX should be to get the new curve.
 
        
 

	
 
        dialog = self.wtree.get_object("newSubterm")
 
        # the plan is to autocomplete this on existing subterm names
 
        # (but let you make one up, too)
 
@@ -245,9 +260,9 @@ class Main(object):
 

	
 
    def currentSong(self):
 

	
 
        with self.graph.currentState(
 
                tripleFilter=(self.session, L9['currentSong'], None)
 
        ) as current:
 
        with self.graph.currentState(tripleFilter=(self.session,
 
                                                   L9['currentSong'],
 
                                                   None)) as current:
 
            return current.value(self.session, L9['currentSong'])
 

	
 
    def songSubtermsContext(self):
 
@@ -277,13 +292,13 @@ class Main(object):
 
            (uri, RDF.type, L9.Subterm, ctx),
 
            (uri, RDFS.label, Literal(newname), ctx),
 
            (self.currentSong(), L9['subterm'], uri, ctx),
 
            ]
 
        ]
 
        if sub is not None:
 
            quads.append((uri, L9['sub'], sub, ctx))
 
        if expr is not None:
 
            quads.append((uri, L9['expression'], Literal(expr), ctx))
 
        self.graph.patch(Patch(addQuads=quads))
 
            
 

	
 
        return uri
 

	
 
    def all_subterm_labels(self):
 
@@ -301,7 +316,7 @@ class Main(object):
 
                if sub is not None:
 
                    labels.append(current.label(sub))
 
        return labels
 
        
 

	
 
    def set_subterms_from_graph(self):
 
        """rebuild all the gtktable 'subterms' widgets and the
 
        self.currentSubterms list"""
 
@@ -311,7 +326,7 @@ class Main(object):
 
        for st in set(self.graph.objects(song, L9['subterm'])):
 
            log.debug("song %s has subterm %s", song, st)
 
            term = Subterm(self.graph, st, self.songSubtermsContext(),
 
                               self.curveset)
 
                           self.curveset)
 
            newList.append(term)
 
        self.currentSubterms[:] = newList
 

	
 
@@ -335,13 +350,12 @@ class Main(object):
 
          * { font-size: 92%; }
 
          .button:link { font-size: 7px }
 
        ''')
 
        
 

	
 
        screen = Gdk.Display.get_default_screen(Gdk.Display.get_default())
 
        for p in providers:
 
            Gtk.StyleContext.add_provider_for_screen(
 
                screen, p,
 
                Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
 
        
 
                screen, p, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
 

	
 
    def onSubtermChildAdded(self, subtermsTable, *args):
 
        # this would probably work, but isn't getting called
 
        log.info("onSubtermChildAdded")
 
@@ -369,7 +383,7 @@ class Main(object):
 
        ns.update(globals())
 
        ns.update(self.__dict__)
 
        togglePyConsole(self, item, ns)
 
        
 

	
 
    def onSeeCurrentTime(self, item):
 
        dispatcher.send("see time")
 

	
 
@@ -395,15 +409,14 @@ class Main(object):
 
    def makeStatusLines(self, master):
 
        """various labels that listen for dispatcher signals"""
 
        for row, (signame, textfilter) in enumerate([
 
            ('input time', lambda t: "%.2fs"%t),
 
            ('output levels',
 
             lambda levels: textwrap.fill("; ".join(["%s:%.2f"%(n,v)
 
                                                     for n,v in
 
                                                     levels.items()[:2]
 
                                                     if v>0]),70)),
 
            ('update period', lambda t: "%.1fms"%(t*1000)),
 
            ('input time', lambda t: "%.2fs" % t),
 
            ('output levels', lambda levels: textwrap.fill(
 
                "; ".join([
 
                    "%s:%.2f" % (n, v) for n, v in levels.items()[:2] if v > 0
 
                ]), 70)),
 
            ('update period', lambda t: "%.1fms" % (t * 1000)),
 
            ('update status', lambda x: str(x)),
 
            ]):
 
        ]):
 
            key = Gtk.Label("%s:" % signame)
 
            value = Gtk.Label("")
 
            master.resize(row + 1, 2)
 
@@ -412,28 +425,31 @@ class Main(object):
 
            key.set_alignment(1, 0)
 
            value.set_alignment(0, 0)
 

	
 
            dispatcher.connect(lambda val, value=value, tf=textfilter:
 
                               value.set_text(tf(val)),
 
                               signame, weak=False)
 
            dispatcher.connect(lambda val, value=value, tf=textfilter: value.
 
                               set_text(tf(val)),
 
                               signame,
 
                               weak=False)
 
        dispatcher.connect(lambda val: setattr(self, 'lastSeenInputTime', val),
 
                           'input time', weak=False)
 
                           'input time',
 
                           weak=False)
 
        master.show_all()
 

	
 
    def refreshCurveView(self):
 
        wtree = self.wtree
 
        mtimes = [os.path.getmtime(f) for f in [
 
            'light9/curvecalc/curveview.py',
 
            'light9/curvecalc/zoomcontrol.py',
 
            ]]
 
        mtimes = [
 
            os.path.getmtime(f) for f in [
 
                'light9/curvecalc/curveview.py',
 
                'light9/curvecalc/zoomcontrol.py',
 
            ]
 
        ]
 

	
 
        if (not hasattr(self, 'curvesetView') or
 
            self.curvesetView._mtimes != mtimes):
 
                self.curvesetView._mtimes != mtimes):
 
            print "reload curveview.py"
 
            curvesVBox = wtree.get_object("curves")
 
            zoomControlBox = wtree.get_object("zoomControlBox")
 
            [curvesVBox.remove(c) for c in curvesVBox.get_children()]
 
            [zoomControlBox.remove(c) for c in
 
             zoomControlBox.get_children()]
 
            [zoomControlBox.remove(c) for c in zoomControlBox.get_children()]
 
            try:
 
                linecache.clearcache()
 
                reload(curveview)
 
@@ -443,8 +459,8 @@ class Main(object):
 
                    self.curvesetView.live = False
 

	
 
                # mem problem somewhere; need to hold a ref to this
 
                self.curvesetView = curveview.Curvesetview(self.graph, 
 
                    curvesVBox, zoomControlBox, self.curveset)
 
                self.curvesetView = curveview.Curvesetview(
 
                    self.graph, curvesVBox, zoomControlBox, self.curveset)
 
                self.curvesetView._mtimes = mtimes
 

	
 
                # this is scheduled after some tk shuffling, to
 
@@ -463,6 +479,7 @@ class MaxTime(object):
 
    """
 
    looks up the time in seconds for the session's current song
 
    """
 

	
 
    def __init__(self, graph, session):
 
        self.graph, self.session = graph, session
 
        graph.addHandler(self.update)
 
@@ -480,6 +497,7 @@ class MaxTime(object):
 
    def get(self):
 
        return self.maxtime
 

	
 

	
 
def launch(args, graph, session, opts, startTime, music):
 

	
 
    try:
 
@@ -492,7 +510,7 @@ def launch(args, graph, session, opts, s
 
        pass
 

	
 
    curveset = Curveset(graph=graph, session=session)
 
        
 

	
 
    log.debug("startup: output %s", time.time() - startTime)
 

	
 
    mt = MaxTime(graph, session)
 
@@ -501,9 +519,8 @@ def launch(args, graph, session, opts, s
 
    start = Main(graph, opts, session, curveset, music)
 
    out = Output(graph, session, music, curveset, start.currentSubterms)
 

	
 
    dispatcher.send("show all")
 

	
 
    dispatcher.send("show all")
 
        
 
    if opts.startup_only:
 
        log.debug("quitting now because of --startup-only")
 
        return
 
@@ -515,22 +532,24 @@ def launch(args, graph, session, opts, s
 
            requestHandler.set_status(404)
 
            requestHandler.write("not hovering over any time")
 
            return
 
        with graph.currentState(
 
                tripleFilter=(session, L9['currentSong'], None)) as g:
 
        with graph.currentState(tripleFilter=(session, L9['currentSong'],
 
                                              None)) as g:
 
            song = g.value(session, L9['currentSong'])
 
            json.dump({"song": song, "hoverTime" : times[0]}, requestHandler)
 
        
 
            json.dump({"song": song, "hoverTime": times[0]}, requestHandler)
 

	
 
    serveCurveEdit(networking.curveCalc.port, hoverTimeResponse, start.curveset)
 

	
 

	
 
def main():
 
    startTime = time.time()
 
    parser = optparse.OptionParser()
 
    parser.set_usage("%prog [opts] [songURI]")
 
    parser.add_option("--debug", action="store_true",
 
                      help="log at DEBUG")
 
    parser.add_option("--reload", action="store_true",
 
    parser.add_option("--debug", action="store_true", help="log at DEBUG")
 
    parser.add_option("--reload",
 
                      action="store_true",
 
                      help="live reload of themes and code")
 
    parser.add_option("--startup-only", action='store_true',
 
    parser.add_option("--startup-only",
 
                      action='store_true',
 
                      help="quit after loading everything (for timing tests)")
 
    parser.add_option("--profile", help='"hotshot" or "stat"')
 
    clientsession.add_option(parser)
 
@@ -540,16 +559,15 @@ def main():
 

	
 
    log.debug("startup: music %s", time.time() - startTime)
 

	
 

	
 
    session = clientsession.getUri('curvecalc', opts)
 

	
 
    music = Music()
 
    graph = SyncedGraph(networking.rdfdb.url, "curvecalc")
 

	
 
    graph.initiallySynced.addCallback(
 
        lambda _: launch(args, graph, session, opts, startTime, music))
 
    graph.initiallySynced.addCallback(lambda _: launch(args, graph, session,
 
                                                       opts, startTime, music))
 
    from light9 import prof
 
    prof.run(reactor.run, profile=opts.profile)
 

	
 

	
 
main()
 

	
bin/dmx_color_test.py
Show inline comments
 
@@ -7,6 +7,7 @@ from twisted.internet import reactor, ta
 
log.setLevel(logging.INFO)
 
firstDmxChannel = 10
 

	
 

	
 
def step():
 
    hue = (time.time() * .2) % 1.0
 
    r, g, b = colorsys.hsv_to_rgb(hue, 1, 1)
bin/dmxserver
Show inline comments
 
@@ -27,10 +27,10 @@ todo:
 
from __future__ import division
 
from twisted.internet import reactor
 
from twisted.web import xmlrpc, server
 
import sys,time,os
 
import sys, time, os
 
from optparse import OptionParser
 
import run_local
 
import txosc.dispatch, txosc.async
 
import txosc.dispatch, txosc. async
 
from light9.io import ParportDMX, UsbDMX
 

	
 
from light9.updatefreq import Updatefreq
 
@@ -39,21 +39,26 @@ from light9 import networking
 
from txzmq import ZmqEndpoint, ZmqFactory, ZmqPullConnection, ZmqRequestTimeoutError
 
import json
 

	
 

	
 
def startZmq(port, outputlevels):
 
    zf = ZmqFactory()
 
    e = ZmqEndpoint('bind', 'tcp://*:%s' % port)
 
    s = ZmqPullConnection(zf, e)
 

	
 
    def onPull(message):
 
        msg = json.loads(message[0])
 
        outputlevels(msg['clientid'], msg['levellist'])
 

	
 
    s.onPull = onPull
 

	
 

	
 
class ReceiverApplication(object):
 
    """
 
    receive UDP OSC messages. address is /dmx/1 for dmx channel 1,
 
    arguments are 0-1 floats for that channel and any number of
 
    following channels.
 
    """
 

	
 
    def __init__(self, port, lightServer):
 
        self.port = port
 
        self.lightServer = lightServer
 
@@ -61,7 +66,7 @@ class ReceiverApplication(object):
 
        self.receiver.addCallback("/dmx/*", self.pixel_handler)
 
        self._server_port = reactor.listenUDP(
 
            self.port,
 
            txosc.async.DatagramServerProtocol(self.receiver),
 
            txosc. async .DatagramServerProtocol(self.receiver),
 
            interface='0.0.0.0')
 
        print "Listening OSC on udp port %s" % (self.port)
 

	
 
@@ -70,127 +75,127 @@ class ReceiverApplication(object):
 
        startChannel = int(message.address.split('/')[2])
 
        levels = [a.value for a in message.arguments]
 
        allLevels = [0] * (startChannel - 1) + levels
 
        self.lightServer.xmlrpc_outputlevels("osc@%s" % startChannel,
 
                                             allLevels)
 
        self.lightServer.xmlrpc_outputlevels("osc@%s" % startChannel, allLevels)
 

	
 

	
 
class XMLRPCServe(xmlrpc.XMLRPC):
 
    def __init__(self,options):
 

	
 
    def __init__(self, options):
 

	
 
        xmlrpc.XMLRPC.__init__(self)
 
        
 
        self.clientlevels={} # clientID : list of levels
 
        self.lastseen={} # clientID : time last seen
 
        self.clientfreq={} # clientID : updatefreq
 
        
 
        self.combinedlevels=[] # list of levels, after max'ing the clients
 
        self.clientschanged=1 # have clients sent anything since the last send?
 
        self.options=options
 
        self.lastupdate=0 # time of last dmx send
 
        self.laststatsprint=0  # time
 

	
 
        self.clientlevels = {}  # clientID : list of levels
 
        self.lastseen = {}  # clientID : time last seen
 
        self.clientfreq = {}  # clientID : updatefreq
 

	
 
        self.combinedlevels = []  # list of levels, after max'ing the clients
 
        self.clientschanged = 1  # have clients sent anything since the last send?
 
        self.options = options
 
        self.lastupdate = 0  # time of last dmx send
 
        self.laststatsprint = 0  # time
 

	
 
        # desired seconds between sendlevels() calls
 
        self.calldelay=1/options.updates_per_sec 
 
        self.calldelay = 1 / options.updates_per_sec
 

	
 
        print "starting parport connection"
 
        self.parportdmx = UsbDMX(dimmers=90, port=options.dmx_device)
 
        if os.environ.get('DMXDUMMY',0):
 
        if os.environ.get('DMXDUMMY', 0):
 
            self.parportdmx.godummy()
 
        else:
 
            self.parportdmx.golive()
 
            
 

	
 
        self.updatefreq=Updatefreq() # freq of actual dmx sends
 
        self.num_unshown_updates=None
 
        self.lastshownlevels=None
 
        self.updatefreq = Updatefreq()  # freq of actual dmx sends
 
        self.num_unshown_updates = None
 
        self.lastshownlevels = None
 
        # start the loop
 
        self.sendlevels()
 

	
 
        # the other loop
 
        self.purgeclients()
 
        
 

	
 
    def purgeclients(self):
 
        
 
        """forget about any clients who haven't sent levels in a while.
 
        this runs in a loop"""
 

	
 
        purge_age=10 # seconds
 
        
 
        reactor.callLater(1,self.purgeclients)
 
        purge_age = 10  # seconds
 

	
 
        now=time.time()
 
        reactor.callLater(1, self.purgeclients)
 

	
 
        now = time.time()
 
        cids = self.lastseen.keys()
 
        for cid in cids:
 
            lastseen=self.lastseen[cid]
 
            lastseen = self.lastseen[cid]
 
            if lastseen < now - purge_age:
 
                print ("forgetting client %s (no activity for %s sec)" %
 
                       (cid,purge_age))
 
                print("forgetting client %s (no activity for %s sec)" %
 
                      (cid, purge_age))
 
                try:
 
                    del self.clientlevels[cid]
 
                except KeyError:
 
                    pass
 
                del self.clientfreq[cid]
 
                del self.lastseen[cid]
 
        
 

	
 
    def sendlevels(self):
 
        
 
        """sends to dmx if levels have changed, or if we havent sent
 
        in a while"""
 

	
 
        reactor.callLater(self.calldelay,self.sendlevels)
 
        reactor.callLater(self.calldelay, self.sendlevels)
 

	
 
        if self.clientschanged:
 
            # recalc levels
 

	
 
            self.calclevels()
 
         
 
            if (self.num_unshown_updates is None or # first time
 
                self.options.fast_updates or # show always
 
                (self.combinedlevels!=self.lastshownlevels and # changed
 
                 self.num_unshown_updates>5)): # not too frequent
 
                self.num_unshown_updates=0
 

	
 
            if (self.num_unshown_updates is None or  # first time
 
                    self.options.fast_updates or  # show always
 
                (
 
                    self.combinedlevels != self.lastshownlevels and  # changed
 
                    self.num_unshown_updates > 5)):  # not too frequent
 
                self.num_unshown_updates = 0
 
                self.printlevels()
 
                self.lastshownlevels=self.combinedlevels[:]
 
                self.lastshownlevels = self.combinedlevels[:]
 
            else:
 
                self.num_unshown_updates+=1
 
                self.num_unshown_updates += 1
 

	
 
        if time.time()>self.laststatsprint+2:
 
            self.laststatsprint=time.time()
 
        if time.time() > self.laststatsprint + 2:
 
            self.laststatsprint = time.time()
 
            self.printstats()
 

	
 
        # used to be a fixed 1 in here, for the max delay between
 
        # calls, instead of calldelay
 
        if self.clientschanged or time.time() > self.lastupdate + self.calldelay:
 
            self.lastupdate=time.time()
 
        if self.clientschanged or time.time(
 
        ) > self.lastupdate + self.calldelay:
 
            self.lastupdate = time.time()
 
            self.sendlevels_dmx()
 

	
 
        self.clientschanged=0 # clear the flag
 
        
 
        self.clientschanged = 0  # clear the flag
 

	
 
    def calclevels(self):
 
        """combine all the known client levels into self.combinedlevels"""
 
        self.combinedlevels=[]
 
        for chan in range(0,self.parportdmx.dimmers):
 
            x=0
 
        self.combinedlevels = []
 
        for chan in range(0, self.parportdmx.dimmers):
 
            x = 0
 
            for clientlist in self.clientlevels.values():
 
                if len(clientlist)>chan:
 
                if len(clientlist) > chan:
 
                    # clamp client levels to 0..1
 
                    cl=max(0,min(1,clientlist[chan]))
 
                    x=max(x,cl)
 
                    cl = max(0, min(1, clientlist[chan]))
 
                    x = max(x, cl)
 
            self.combinedlevels.append(x)
 

	
 
    def printlevels(self):
 
        """write all the levels to stdout"""
 
        print "Levels:","".join(["% 2d "%(x*100) for
 
                                 x in self.combinedlevels])
 
    
 
        print "Levels:", "".join(
 
            ["% 2d " % (x * 100) for x in self.combinedlevels])
 

	
 
    def printstats(self):
 
        """print the clock, freq, etc, with a \r at the end"""
 

	
 
        sys.stdout.write("dmxserver up at %s, [polls %s] "%
 
                         (time.strftime("%H:%M:%S"),
 
                          str(self.updatefreq),
 
                          ))
 
        for cid,freq in self.clientfreq.items():
 
            sys.stdout.write("[%s %s] " % (cid,str(freq)))
 
        sys.stdout.write("dmxserver up at %s, [polls %s] " % (
 
            time.strftime("%H:%M:%S"),
 
            str(self.updatefreq),
 
        ))
 
        for cid, freq in self.clientfreq.items():
 
            sys.stdout.write("[%s %s] " % (cid, str(freq)))
 
        sys.stdout.write("\r")
 
        sys.stdout.flush()
 

	
 
@@ -198,18 +203,18 @@ class XMLRPCServe(xmlrpc.XMLRPC):
 
        """output self.combinedlevels to dmx, and keep the updates/sec stats"""
 
        # they'll get divided by 100
 
        if self.parportdmx:
 
            self.parportdmx.sendlevels([l*100 for l in self.combinedlevels])
 
            self.parportdmx.sendlevels([l * 100 for l in self.combinedlevels])
 
        self.updatefreq.update()
 
    
 
    def xmlrpc_echo(self,x):
 

	
 
    def xmlrpc_echo(self, x):
 
        return x
 
    
 
    def xmlrpc_outputlevels(self,cid,levellist):
 

	
 
    def xmlrpc_outputlevels(self, cid, levellist):
 
        """send a unique id for your client (name+pid maybe), then
 
        the variable-length dmx levellist (scaled 0..1)"""
 
        if levellist!=self.clientlevels.get(cid,None):
 
            self.clientlevels[cid]=levellist
 
            self.clientschanged=1
 
        if levellist != self.clientlevels.get(cid, None):
 
            self.clientlevels[cid] = levellist
 
            self.clientschanged = 1
 
        self.trackClientFreq(cid)
 
        return "ok"
 

	
 
@@ -227,43 +232,50 @@ class XMLRPCServe(xmlrpc.XMLRPC):
 
            i -= 1
 
        if i < 0:
 
            return []
 
        trunc = trunc[:i+1]
 
        trunc = trunc[:i + 1]
 
        return trunc
 
    
 

	
 
    def trackClientFreq(self, cid):
 
        if cid not in self.lastseen:
 
            print "hello new client %s" % cid
 
            self.clientfreq[cid]=Updatefreq()
 
        self.lastseen[cid]=time.time()
 
            self.clientfreq[cid] = Updatefreq()
 
        self.lastseen[cid] = time.time()
 
        self.clientfreq[cid].update()
 
        
 

	
 

	
 
parser=OptionParser()
 
parser.add_option("-f","--fast-updates",action='store_true',
 
parser = OptionParser()
 
parser.add_option("-f",
 
                  "--fast-updates",
 
                  action='store_true',
 
                  help=('display all dmx output to stdout instead '
 
                        'of the usual reduced output'))
 
parser.add_option("-r","--updates-per-sec",type='float',default=20,
 
parser.add_option("-r",
 
                  "--updates-per-sec",
 
                  type='float',
 
                  default=20,
 
                  help=('dmx output frequency'))
 
parser.add_option("-d","--dmx-device", default='/dev/dmx0',
 
parser.add_option("-d",
 
                  "--dmx-device",
 
                  default='/dev/dmx0',
 
                  help='dmx device name')
 
parser.add_option("-n", "--dummy", action="store_true",
 
parser.add_option("-n",
 
                  "--dummy",
 
                  action="store_true",
 
                  help="dummy mode, same as DMXDUMMY=1 env variable")
 
(options,songfiles)=parser.parse_args()
 
(options, songfiles) = parser.parse_args()
 

	
 
print options
 

	
 
if options.dummy:
 
    os.environ['DMXDUMMY'] = "1"
 

	
 

	
 
port = networking.dmxServer.port
 
print "starting xmlrpc server on port %s" % port
 
xmlrpcServe = XMLRPCServe(options)
 
reactor.listenTCP(port,server.Site(xmlrpcServe))
 
reactor.listenTCP(port, server.Site(xmlrpcServe))
 

	
 
startZmq(networking.dmxServerZmq.port, xmlrpcServe.xmlrpc_outputlevels)
 
                  
 

	
 
oscApp = ReceiverApplication(9051, xmlrpcServe)
 

	
 
reactor.run()
 

	
bin/effecteval
Show inline comments
 
@@ -8,8 +8,8 @@ import cyclone.web, cyclone.websocket, c
 
import sys, optparse, logging, subprocess, json, itertools
 
from rdflib import URIRef, Literal
 

	
 
sys.path.append('/usr/lib/pymodules/python2.7/') # for numpy, on rpi
 
sys.path.append('/usr/lib/python2.7/dist-packages') # For numpy
 
sys.path.append('/usr/lib/pymodules/python2.7/')  # for numpy, on rpi
 
sys.path.append('/usr/lib/python2.7/dist-packages')  # For numpy
 
from light9 import networking, showconfig
 
from light9.effecteval.effect import EffectNode
 
from light9.effect.edit import getMusicStatus, songNotePatch
 
@@ -22,19 +22,24 @@ from greplin import scales
 

	
 
from lib.cycloneerr import PrettyErrorHandler
 

	
 

	
 
class EffectEdit(PrettyErrorHandler, cyclone.web.RequestHandler):
 

	
 
    def get(self):
 
        self.set_header('Content-Type', 'text/html')        
 
        self.set_header('Content-Type', 'text/html')
 
        self.write(open("light9/effecteval/effect.html").read())
 

	
 
    def delete(self):
 
        graph = self.settings.graph
 
        uri = URIRef(self.get_argument('uri'))
 
        with graph.currentState(tripleFilter=(None, L9['effect'], uri)) as g:
 
            song = ctx = list(g.subjects(L9['effect'], uri))[0]
 
        self.settings.graph.patch(Patch(delQuads=[
 
            (song, L9['effect'], uri, ctx),
 
        self.settings.graph.patch(
 
            Patch(delQuads=[
 
                (song, L9['effect'], uri, ctx),
 
            ]))
 
        
 

	
 

	
 
@inlineCallbacks
 
def currentSong():
 
    s = (yield getMusicStatus())['song']
 
@@ -42,13 +47,17 @@ def currentSong():
 
        raise ValueError("no current song")
 
    returnValue(URIRef(s))
 

	
 

	
 
class SongEffects(PrettyErrorHandler, cyclone.web.RequestHandler):
 

	
 
    def wideOpenCors(self):
 
        self.set_header('Access-Control-Allow-Origin', '*')
 
        self.set_header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, OPTIONS')
 
        self.set_header('Access-Control-Allow-Methods',
 
                        'GET, PUT, POST, DELETE, OPTIONS')
 
        self.set_header('Access-Control-Max-Age', '1000')
 
        self.set_header('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With')
 
    
 
        self.set_header('Access-Control-Allow-Headers',
 
                        'Content-Type, Authorization, X-Requested-With')
 

	
 
    def options(self):
 
        self.wideOpenCors()
 
        self.write('')
 
@@ -60,27 +69,34 @@ class SongEffects(PrettyErrorHandler, cy
 

	
 
        try:
 
            song = URIRef(self.get_argument('uri'))
 
        except Exception: # which?
 
        except Exception:  # which?
 
            song = yield currentSong()
 

	
 
        event = self.get_argument('event', default='default')
 
        
 

	
 
        note = self.get_argument('note', default=None)
 
        if note is not None:
 
            note = URIRef(note)
 

	
 
        log.info("adding to %s", song)
 
        note, p = yield songNotePatch(self.settings.graph, dropped, song, event, ctx=song, note=note)
 
        
 
        note, p = yield songNotePatch(self.settings.graph,
 
                                      dropped,
 
                                      song,
 
                                      event,
 
                                      ctx=song,
 
                                      note=note)
 

	
 
        self.settings.graph.patch(p)
 
        self.settings.graph.suggestPrefixes(song, {'song': URIRef(song + '/')})
 
        self.write(json.dumps({'note': note}))
 
        
 

	
 

	
 
class SongEffectsUpdates(cyclone.websocket.WebSocketHandler):
 

	
 
    def connectionMade(self, *args, **kwargs):
 
        self.graph = self.settings.graph
 
        self.graph.addHandler(self.updateClient)
 
        
 

	
 
    def updateClient(self):
 
        # todo: abort if client is gone
 
        playlist = self.graph.value(showconfig.showUri(), L9['playList'])
 
@@ -88,14 +104,18 @@ class SongEffectsUpdates(cyclone.websock
 
        out = []
 
        for s in songs:
 
            out.append({'uri': s, 'label': self.graph.label(s)})
 
            out[-1]['effects'] = [{'uri': uri, 'label': self.graph.label(uri)} for uri in sorted(self.graph.objects(s, L9['effect']))]
 
            out[-1]['effects'] = [{
 
                'uri': uri,
 
                'label': self.graph.label(uri)
 
            } for uri in sorted(self.graph.objects(s, L9['effect']))]
 
        self.sendMessage({'songs': out})
 
        
 
        
 

	
 

	
 
class EffectUpdates(cyclone.websocket.WebSocketHandler):
 
    """
 
    stays alive for the life of the effect page
 
    """
 

	
 
    def connectionMade(self, *args, **kwargs):
 
        log.info("websocket opened")
 
        self.uri = URIRef(self.get_argument('uri'))
 
@@ -113,7 +133,7 @@ class EffectUpdates(cyclone.websocket.We
 
        en = EffectNode(self.graph, self.uri)
 
        codeLines = [c.code for c in en.codes]
 
        self.sendMessage({'codeLines': codeLines})
 
        
 

	
 
    def connectionLost(self, reason):
 
        log.info("websocket closed")
 

	
 
@@ -121,12 +141,12 @@ class EffectUpdates(cyclone.websocket.We
 
        log.info("got message %s" % message)
 
        # write a patch back to the graph
 

	
 

	
 
def replaceObjects(graph, c, s, p, newObjs):
 
    patch = graph.getObjectPatch(
 
        context=c,
 
        subject=s,
 
        predicate=p,
 
        newObject=newObjs[0])
 
    patch = graph.getObjectPatch(context=c,
 
                                 subject=s,
 
                                 predicate=p,
 
                                 newObject=newObjs[0])
 

	
 
    moreAdds = []
 
    for line in newObjs[1:]:
 
@@ -135,8 +155,9 @@ def replaceObjects(graph, c, s, p, newOb
 
                      addQuads=patch.addQuads + moreAdds)
 
    graph.patch(fullPatch)
 

	
 
        
 

	
 
class Code(PrettyErrorHandler, cyclone.web.RequestHandler):
 

	
 
    def put(self):
 
        effect = URIRef(self.get_argument('uri'))
 
        codeLines = []
 
@@ -150,16 +171,18 @@ class Code(PrettyErrorHandler, cyclone.w
 
        if not codeLines:
 
            log.info("no codelines received on PUT /code")
 
            return
 
        with self.settings.graph.currentState(
 
                tripleFilter=(None, L9['effect'], effect)) as g:
 
        with self.settings.graph.currentState(tripleFilter=(None, L9['effect'],
 
                                                            effect)) as g:
 
            song = g.subjects(L9['effect'], effect).next()
 
            
 

	
 
        replaceObjects(self.settings.graph, song, effect, L9['code'], codeLines)
 
            
 

	
 
        # right here we could tell if the code has a python error and return it
 
        self.send_error(202)
 
        
 

	
 

	
 
class EffectEval(PrettyErrorHandler, cyclone.web.RequestHandler):
 

	
 
    @inlineCallbacks
 
    def get(self):
 
        # return dmx list for that effect
 
@@ -176,6 +199,7 @@ class EffectEval(PrettyErrorHandler, cyc
 
# Completely not sure where the effect background loop should
 
# go. Another process could own it, and get this request repeatedly:
 
class SongEffectsEval(PrettyErrorHandler, cyclone.web.RequestHandler):
 

	
 
    def get(self):
 
        song = URIRef(self.get_argument('song'))
 
        effects = effectsForSong(self.settings.graph, song)
 
@@ -183,34 +207,44 @@ class SongEffectsEval(PrettyErrorHandler
 
        self.write(maxDict(effectDmxDict(e) for e in effects))
 
        # return dmx dict for all effects in the song, already combined
 

	
 

	
 
class App(object):
 

	
 
    def __init__(self, show, outputWhere):
 
        self.show = show
 
        self.outputWhere = outputWhere
 
        self.graph = SyncedGraph(networking.rdfdb.url, "effectEval")
 
        self.graph.initiallySynced.addCallback(self.launch).addErrback(log.error)
 
        self.graph.initiallySynced.addCallback(self.launch).addErrback(
 
            log.error)
 

	
 
        self.stats = scales.collection('/',
 
                                       scales.PmfStat('sendLevels'),
 
                                       scales.PmfStat('getMusic'),
 
                                       scales.PmfStat('evals'),
 
                                       scales.PmfStat('sendOutput'),
 
                                       scales.IntStat('errors'),
 
                                       )
 
        self.stats = scales.collection(
 
            '/',
 
            scales.PmfStat('sendLevels'),
 
            scales.PmfStat('getMusic'),
 
            scales.PmfStat('evals'),
 
            scales.PmfStat('sendOutput'),
 
            scales.IntStat('errors'),
 
        )
 

	
 
    def launch(self, *args):
 
        log.info('launch')
 
        if self.outputWhere:
 
            self.loop = makeEffectLoop(self.graph, self.stats, self.outputWhere)
 
            self.loop.startLoop()
 
        
 

	
 
        SFH = cyclone.web.StaticFileHandler
 
        self.cycloneApp = cyclone.web.Application(handlers=[
 
            (r'/()', SFH,
 
             {'path': 'light9/effecteval', 'default_filename': 'index.html'}),
 
            (r'/()', SFH, {
 
                'path': 'light9/effecteval',
 
                'default_filename': 'index.html'
 
            }),
 
            (r'/effect', EffectEdit),
 
            (r'/effect\.js', StaticCoffee, {'src': 'light9/effecteval/effect.coffee'}),
 
            (r'/(effect-components\.html)', SFH, {'path': 'light9/effecteval'}),
 
            (r'/effect\.js', StaticCoffee, {
 
                'src': 'light9/effecteval/effect.coffee'
 
            }),
 
            (r'/(effect-components\.html)', SFH, {
 
                'path': 'light9/effecteval'
 
            }),
 
            (r'/effectUpdates', EffectUpdates),
 
            (r'/code', Code),
 
            (r'/songEffectsUpdates', SongEffectsUpdates),
 
@@ -224,25 +258,33 @@ class App(object):
 
                                                  stats=self.stats)
 
        reactor.listenTCP(networking.effectEval.port, self.cycloneApp)
 
        log.info("listening on %s" % networking.effectEval.port)
 
        
 

	
 

	
 
class StaticCoffee(PrettyErrorHandler, cyclone.web.RequestHandler):
 

	
 
    def initialize(self, src):
 
        super(StaticCoffee, self).initialize()
 
        self.src = src
 

	
 
    def get(self):
 
        self.set_header('Content-Type', 'application/javascript')
 
        self.write(subprocess.check_output([
 
            '/usr/bin/coffee', '--compile', '--print', self.src]))
 
        self.write(
 
            subprocess.check_output(
 
                ['/usr/bin/coffee', '--compile', '--print', self.src]))
 

	
 
        
 

	
 
if __name__ == "__main__":
 
    parser = optparse.OptionParser()
 
    parser.add_option('--show',
 
    parser.add_option(
 
        '--show',
 
        help='show URI, like http://light9.bigasterisk.com/show/dance2008',
 
                      default=showconfig.showUri())
 
    parser.add_option("-v", "--verbose", action="store_true",
 
        default=showconfig.showUri())
 
    parser.add_option("-v",
 
                      "--verbose",
 
                      action="store_true",
 
                      help="logging.DEBUG")
 
    parser.add_option("--twistedlog", action="store_true",
 
    parser.add_option("--twistedlog",
 
                      action="store_true",
 
                      help="twisted logging")
 
    parser.add_option("--output", metavar="WHERE", help="dmx or leds")
 
    (options, args) = parser.parse_args()
 
@@ -250,7 +292,7 @@ if __name__ == "__main__":
 

	
 
    if not options.show:
 
        raise ValueError("missing --show http://...")
 
            
 

	
 
    app = App(URIRef(options.show), options.output)
 
    if options.twistedlog:
 
        from twisted.python import log as twlog
bin/effectsequencer
Show inline comments
 
@@ -17,7 +17,9 @@ from light9.collector.collector_client i
 

	
 
from light9 import clientsession
 

	
 

	
 
class App(object):
 

	
 
    def __init__(self, show, session):
 
        self.show = show
 
        self.session = session
 
@@ -25,23 +27,25 @@ class App(object):
 
        self.graph = SyncedGraph(networking.rdfdb.url, "effectSequencer")
 
        self.graph.initiallySynced.addCallback(self.launch)
 

	
 
        self.stats = scales.collection(
 
            '/',
 
            scales.PmfStat('sendLevels'),
 
            scales.PmfStat('getMusic'),
 
            scales.PmfStat('evals'),
 
            scales.PmfStat('sendOutput'),
 
            scales.IntStat('errors'),
 
        )
 

	
 
        self.stats = scales.collection('/',
 
                                       scales.PmfStat('sendLevels'),
 
                                       scales.PmfStat('getMusic'),
 
                                       scales.PmfStat('evals'),
 
                                       scales.PmfStat('sendOutput'),
 
                                       scales.IntStat('errors'),
 
                                       )
 
    def launch(self, *args):
 
        self.seq = Sequencer(
 
            self.graph,
 
            lambda settings: sendToCollector('effectSequencer', self.session,
 
                                             settings))
 
            self.graph, lambda settings: sendToCollector(
 
                'effectSequencer', self.session, settings))
 

	
 
        self.cycloneApp = cyclone.web.Application(handlers=[
 
            (r'/()', cyclone.web.StaticFileHandler,
 
             {"path" : "light9/effect/", "default_filename" : "sequencer.html"}),
 
            (r'/()', cyclone.web.StaticFileHandler, {
 
                "path": "light9/effect/",
 
                "default_filename": "sequencer.html"
 
            }),
 
            (r'/updates', Updates),
 
            (r'/stats', StatsForCyclone),
 
        ],
 
@@ -55,12 +59,16 @@ class App(object):
 

	
 
if __name__ == "__main__":
 
    parser = optparse.OptionParser()
 
    parser.add_option('--show',
 
    parser.add_option(
 
        '--show',
 
        help='show URI, like http://light9.bigasterisk.com/show/dance2008',
 
                      default=showconfig.showUri())
 
    parser.add_option("-v", "--verbose", action="store_true",
 
        default=showconfig.showUri())
 
    parser.add_option("-v",
 
                      "--verbose",
 
                      action="store_true",
 
                      help="logging.DEBUG")
 
    parser.add_option("--twistedlog", action="store_true",
 
    parser.add_option("--twistedlog",
 
                      action="store_true",
 
                      help="twisted logging")
 
    clientsession.add_option(parser)
 
    (options, args) = parser.parse_args()
 
@@ -68,7 +76,7 @@ if __name__ == "__main__":
 

	
 
    if not options.show:
 
        raise ValueError("missing --show http://...")
 
        
 

	
 
    session = clientsession.getUri('effectSequencer', options)
 

	
 
    app = App(URIRef(options.show), session)
bin/homepageConfig
Show inline comments
 
@@ -17,6 +17,7 @@ if not webServer:
 
    raise ValueError('no %r :webServer' % netHome)
 
print "listen %s;" % splitport(urlparse(webServer).netloc)[1]
 

	
 

	
 
def location(path, server):
 
    print """
 
    location /%(path)s/ {
 
@@ -32,6 +33,7 @@ def location(path, server):
 
      rewrite /[^/]+/(.*) /$1 break;
 
    }""" % vars()
 

	
 

	
 
for role, server in sorted(graph.predicate_objects(netHome)):
 
    if not server.startswith('http') or role == L9['webServer']:
 
        continue
 
@@ -41,11 +43,11 @@ for role, server in sorted(graph.predica
 
    server = server.rstrip('/')
 
    location(path, server)
 

	
 

	
 

	
 
showPath = showconfig.showUri().split('/', 3)[-1]
 
print """
 
    location /%(path)s {
 
      root %(root)s;
 
    }""" % {'path': showPath,
 
            'root': showconfig.root()[:-len(showPath)]}
 
    }""" % {
 
    'path': showPath,
 
    'root': showconfig.root()[:-len(showPath)]
 
}
bin/inputdemo
Show inline comments
 
#!bin/python
 
import sys
 
sys.path.append('/usr/lib/python2.7/dist-packages') # For gtk
 
sys.path.append('/usr/lib/python2.7/dist-packages')  # For gtk
 
from twisted.internet import gtk3reactor
 
gtk3reactor.install()
 
from twisted.internet import reactor
 
@@ -14,12 +14,13 @@ from rdfdb.syncedgraph import SyncedGrap
 
import cyclone.httpclient
 
from light9.curvecalc.client import sendLiveInputPoint
 

	
 

	
 
class App(object):
 

	
 
    def __init__(self):
 
        parser = optparse.OptionParser()
 
        parser.set_usage("%prog [opts] [curve uri]")
 
        parser.add_option("--debug", action="store_true",
 
                          help="log at DEBUG")
 
        parser.add_option("--debug", action="store_true", help="log at DEBUG")
 
        clientsession.add_option(parser)
 
        opts, args = parser.parse_args()
 

	
 
@@ -30,16 +31,20 @@ class App(object):
 

	
 
        self.graph.initiallySynced.addCallback(lambda _: self.launch())
 

	
 
        self.curve = args[0] if args else URIRef('http://light9.bigasterisk.com/show/dance2014/song1/curve/c-1401259747.675542')
 
        self.curve = args[0] if args else URIRef(
 
            'http://light9.bigasterisk.com/show/dance2014/song1/curve/c-1401259747.675542'
 
        )
 
        print "sending points on curve %s" % self.curve
 
        
 

	
 
        reactor.run()
 

	
 
    def launch(self):
 
        win = Gtk.Window()
 

	
 
        slider = Gtk.Scale.new_with_range(orientation=Gtk.Orientation.VERTICAL,
 
                                          min=0, max=1, step=.001)
 
                                          min=0,
 
                                          max=1,
 
                                          step=.001)
 
        slider.props.inverted = True
 
        slider.connect('value-changed', self.onChanged)
 

	
 
@@ -52,8 +57,10 @@ class App(object):
 
    def onChanged(self, scale):
 
        t1 = time.time()
 
        d = sendLiveInputPoint(self.curve, scale.get_value())
 

	
 
        @d.addCallback
 
        def done(result):
 
            print "posted in %.1f ms" % (1000 * (time.time() - t1))
 

	
 

	
 
App()
bin/inputquneo
Show inline comments
 
@@ -14,18 +14,20 @@ from rdfdb.syncedgraph import SyncedGrap
 
from light9 import networking
 

	
 
import sys
 
sys.path.append('/usr/lib/python2.7/dist-packages') # For pygame
 
sys.path.append('/usr/lib/python2.7/dist-packages')  # For pygame
 
import pygame.midi
 

	
 
curves = {
 
    23: URIRef('http://light9.bigasterisk.com/show/dance2014/song1/curve/c-2'),
 
    24: URIRef('http://light9.bigasterisk.com/show/dance2014/song1/curve/c-3'),
 
    25: URIRef('http://light9.bigasterisk.com/show/dance2014/song1/curve/c-4'),
 
    6:URIRef('http://light9.bigasterisk.com/show/dance2014/song1/curve/c-5'),
 
    6: URIRef('http://light9.bigasterisk.com/show/dance2014/song1/curve/c-5'),
 
    18: URIRef('http://light9.bigasterisk.com/show/dance2014/song1/curve/c-6'),
 
}
 

	
 

	
 
class WatchMidi(object):
 

	
 
    def __init__(self, graph):
 
        self.graph = graph
 
        pygame.midi.init()
 
@@ -36,7 +38,7 @@ class WatchMidi(object):
 

	
 
        self.noteIsOn = {}
 

	
 
        self.effectMap = {} # note: effect class uri
 
        self.effectMap = {}  # note: effect class uri
 
        self.graph.addHandler(self.setupNotes)
 

	
 
    def setupNotes(self):
 
@@ -45,14 +47,15 @@ class WatchMidi(object):
 
            if qn:
 
                self.effectMap[int(qn)] = e
 
        log.info("setup with %s effects", len(self.effectMap))
 
        
 

	
 
    def findQuneo(self):
 
        for dev in range(pygame.midi.get_count()):
 
            interf, name, isInput, isOutput, opened = pygame.midi.get_device_info(dev)
 
            interf, name, isInput, isOutput, opened = pygame.midi.get_device_info(
 
                dev)
 
            if 'QUNEO' in name and isInput:
 
                return dev
 
        raise ValueError("didn't find quneo input device")
 
        
 

	
 
    def step(self):
 
        if not self.inp.poll():
 
            return
 
@@ -62,7 +65,6 @@ class WatchMidi(object):
 
            if status in [NOTEON, NOTEOFF]:
 
                print status, d1, d2
 

	
 

	
 
            if status == NOTEON:
 
                if not self.noteIsOn.get(d1):
 
                    self.noteIsOn[d1] = True
 
@@ -71,7 +73,10 @@ class WatchMidi(object):
 
                        cyclone.httpclient.fetch(
 
                            url=networking.effectEval.path('songEffects'),
 
                            method='POST',
 
                            headers={'Content-Type': ['application/x-www-form-urlencoded']},
 
                            headers={
 
                                'Content-Type':
 
                                ['application/x-www-form-urlencoded']
 
                            },
 
                            postdata=urllib.urlencode([('drop', e)]),
 
                        )
 
                    except KeyError:
 
@@ -80,10 +85,9 @@ class WatchMidi(object):
 
            if status == NOTEOFF:
 
                self.noteIsOn[d1] = False
 

	
 

	
 
            if 0:
 
                # curve editing mode, not done yet
 
                for group in [(23,24,25), (6, 18)]:
 
                for group in [(23, 24, 25), (6, 18)]:
 
                    if d1 in group:
 
                        if not self.noteIsOn.get(group):
 
                            print "start zero"
 
@@ -91,18 +95,20 @@ class WatchMidi(object):
 
                            for d in group:
 
                                sendLiveInputPoint(curves[d], 0)
 
                            self.noteIsOn[group] = True
 
                        else: # miss first update
 
                        else:  # miss first update
 
                            sendLiveInputPoint(curves[d1], d2 / 127)
 

	
 
                    if status == 128: #noteoff
 
                    if status == 128:  #noteoff
 
                        for d in group:
 
                            sendLiveInputPoint(curves[d], 0)
 
                        self.noteIsOn[group] = False
 

	
 

	
 
def main():
 
    log.setLevel(logging.DEBUG)
 
    graph = SyncedGraph(networking.rdfdb.url, "inputQuneo")
 
    wm = WatchMidi(graph)
 
    reactor.run()
 

	
 

	
 
main()
bin/kcclient
Show inline comments
 
#!/usr/bin/env python
 

	
 
"""send KeyboardComposer a fade request, for use from the shell"""
 

	
 
import sys
 
@@ -10,10 +9,8 @@ from light9 import networking
 
subname = sys.argv[1]
 
level = sys.argv[2]
 
fadesecs = '0'
 
if len(sys.argv)>3:
 
if len(sys.argv) > 3:
 
    fadesecs = sys.argv[3]
 

	
 
levelServer = Resource(networking.keyboardComposer.url)
 
levelServer.post('fadesub', subname=subname, level=level, secs=fadesecs)
 

	
 

	
bin/keyboardcomposer
Show inline comments
 
@@ -27,43 +27,58 @@ from light9.effect.simple_outputs import
 

	
 
from bcf2000 import BCF2000
 

	
 
nudge_keys = {
 
    'up'   : list('qwertyui'),
 
    'down' : list('asdfghjk')
 
}
 
nudge_keys = {'up': list('qwertyui'), 'down': list('asdfghjk')}
 

	
 

	
 
class DummySliders:
 

	
 
    def valueOut(self, name, value):
 
        pass
 

	
 
    def close(self):
 
        pass
 

	
 
    def reopen(self):
 
        pass
 

	
 

	
 
class SubScale(tk.Scale, Fadable):
 

	
 
    def __init__(self, master, *args, **kw):
 
        self.scale_var = kw.get('variable') or tk.DoubleVar()
 
        kw.update({'variable' : self.scale_var,
 
                   'from' : 1, 'to' : 0, 'showvalue' : 0,
 
                   'sliderlength' : 15, 'res' : 0.01,
 
                   'width' : 40, 'troughcolor' : 'black', 'bg' : 'grey40',
 
                   'highlightthickness' : 1, 'bd' : 1,
 
                   'highlightcolor' : 'red', 'highlightbackground' : 'black',
 
                   'activebackground' : 'red'})
 
        kw.update({
 
            'variable': self.scale_var,
 
            'from': 1,
 
            'to': 0,
 
            'showvalue': 0,
 
            'sliderlength': 15,
 
            'res': 0.01,
 
            'width': 40,
 
            'troughcolor': 'black',
 
            'bg': 'grey40',
 
            'highlightthickness': 1,
 
            'bd': 1,
 
            'highlightcolor': 'red',
 
            'highlightbackground': 'black',
 
            'activebackground': 'red'
 
        })
 
        tk.Scale.__init__(self, master, *args, **kw)
 
        Fadable.__init__(self, var=self.scale_var, wheel_step=0.05)
 
        self.draw_indicator_colors()
 

	
 
    def draw_indicator_colors(self):
 
        if self.scale_var.get() == 0:
 
            self['troughcolor'] = 'black'
 
        else:
 
            self['troughcolor'] = 'blue'
 

	
 

	
 
class SubmasterBox(tk.Frame):
 
    """
 
    this object owns the level of the submaster (the rdf graph is the
 
    real authority)
 
    """
 

	
 
    def __init__(self, master, graph, sub, session, col, row):
 
        self.graph = graph
 
        self.sub = sub
 
@@ -71,22 +86,31 @@ class SubmasterBox(tk.Frame):
 
        self.col, self.row = col, row
 
        bg = self.graph.value(sub, L9.color, default='#000000')
 
        rgb = webcolors.hex_to_rgb(bg)
 
        hsv = colorsys.rgb_to_hsv(*[x/255 for x in rgb])
 
        darkBg = webcolors.rgb_to_hex(tuple([int(x * 255) for x in colorsys.hsv_to_rgb(
 
            hsv[0], hsv[1], .2)]))
 
        hsv = colorsys.rgb_to_hsv(*[x / 255 for x in rgb])
 
        darkBg = webcolors.rgb_to_hex(
 
            tuple([
 
                int(x * 255) for x in colorsys.hsv_to_rgb(hsv[0], hsv[1], .2)
 
            ]))
 
        tk.Frame.__init__(self, master, bd=1, relief='raised', bg=bg)
 
        self.name = self.graph.label(sub)
 
        self.slider_var = tk.DoubleVar()
 
        self.pauseTrace = False
 
        self.scale = SubScale(self, variable=self.slider_var, width=20)
 

	
 
        self.namelabel = tk.Label(self, font="Arial 9", bg=darkBg,
 
            fg='white', pady=0)
 
        self.namelabel = tk.Label(self,
 
                                  font="Arial 9",
 
                                  bg=darkBg,
 
                                  fg='white',
 
                                  pady=0)
 
        self.graph.addHandler(self.updateName)
 

	
 
        self.namelabel.pack(side=tk.TOP)
 
        levellabel = tk.Label(self, textvariable=self.slider_var, font="Arial 6",
 
            bg='black', fg='white', pady=0)
 
        levellabel = tk.Label(self,
 
                              textvariable=self.slider_var,
 
                              font="Arial 6",
 
                              bg='black',
 
                              fg='white',
 
                              pady=0)
 
        levellabel.pack(side=tk.TOP)
 
        self.scale.pack(side=tk.BOTTOM, expand=1, fill=tk.BOTH)
 

	
 
@@ -111,7 +135,9 @@ class SubmasterBox(tk.Frame):
 
        self.updateGraphWithLevel(self.sub, self.slider_var.get())
 

	
 
        # needs fixing: plan is to use dispatcher or a method call to tell a hardware-mapping object who changed, and then it can make io if that's a current hw slider
 
        dispatcher.send("send_to_hw", sub=self.sub, hwCol=self.col + 1,
 
        dispatcher.send("send_to_hw",
 
                        sub=self.sub,
 
                        hwCol=self.col + 1,
 
                        boxRow=self.row)
 

	
 
    def updateGraphWithLevel(self, uri, level):
 
@@ -125,8 +151,10 @@ class SubmasterBox(tk.Frame):
 
                                subject=self.session,
 
                                predicate=L9['subSetting'],
 
                                nodeClass=L9['SubSetting'],
 
                                keyPred=L9['sub'], newKey=uri,
 
                                valuePred=L9['level'], newValue=Literal(level))
 
                                keyPred=L9['sub'],
 
                                newKey=uri,
 
                                valuePred=L9['level'],
 
                                newValue=Literal(level))
 

	
 
    def updateLevelFromGraph(self):
 
        """read rdf level, write it to subbox.slider_var"""
 
@@ -135,30 +163,34 @@ class SubmasterBox(tk.Frame):
 

	
 
        for setting in graph.objects(self.session, L9['subSetting']):
 
            if graph.value(setting, L9['sub']) == self.sub:
 
                self.pauseTrace = True # don't bounce this update back to server
 
                self.pauseTrace = True  # don't bounce this update back to server
 
                try:
 
                    self.slider_var.set(graph.value(setting, L9['level']))
 
                finally:
 
                    self.pauseTrace = False
 

	
 
    def updateName(self):
 

	
 
        def shortUri(u):
 
            return '.../' + u.split('/')[-1]
 
        self.namelabel.config(text=self.graph.label(self.sub) or shortUri(self.sub))
 

	
 
        self.namelabel.config(
 
            text=self.graph.label(self.sub) or shortUri(self.sub))
 

	
 

	
 
class KeyboardComposer(tk.Frame, SubClient):
 
    def __init__(self, root, graph, session,
 
                 hw_sliders=True):
 

	
 
    def __init__(self, root, graph, session, hw_sliders=True):
 
        tk.Frame.__init__(self, root, bg='black')
 
        SubClient.__init__(self)
 
        self.graph = graph
 
        self.session = session
 

	
 
        self.subbox = {} # sub uri : SubmasterBox
 
        self.slider_table = {} # coords : SubmasterBox
 
        self.rows = [] # this holds Tk Frames for each row
 
        self.subbox = {}  # sub uri : SubmasterBox
 
        self.slider_table = {}  # coords : SubmasterBox
 
        self.rows = []  # this holds Tk Frames for each row
 

	
 
        self.current_row = 0 # should come from session graph
 
        self.current_row = 0  # should come from session graph
 

	
 
        self.use_hw_sliders = hw_sliders
 
        self.connect_to_hw(hw_sliders)
 
@@ -170,7 +202,7 @@ class KeyboardComposer(tk.Frame, SubClie
 

	
 
        self.codeWatcher = CodeWatcher(
 
            onChange=lambda: self.graph.addHandler(self.redraw_sliders))
 
        
 

	
 
        self.send_levels_loop(delay=.05)
 
        self.graph.addHandler(self.rowFromGraph)
 

	
 
@@ -180,19 +212,28 @@ class KeyboardComposer(tk.Frame, SubClie
 

	
 
        self.sliders_status_var = tk.IntVar()
 
        self.sliders_status_var.set(self.use_hw_sliders)
 
        self.sliders_checkbutton = tk.Checkbutton(self.buttonframe,
 
            text="Sliders", variable=self.sliders_status_var,
 
        self.sliders_checkbutton = tk.Checkbutton(
 
            self.buttonframe,
 
            text="Sliders",
 
            variable=self.sliders_status_var,
 
            command=lambda: self.toggle_slider_connectedness(),
 
            bg='black', fg='white')
 
            bg='black',
 
            fg='white')
 
        self.sliders_checkbutton.pack(side=tk.LEFT)
 

	
 
        self.alltozerobutton = tk.Button(self.buttonframe, text="All to Zero",
 
            command=self.alltozero, bg='black', fg='white')
 
        self.alltozerobutton = tk.Button(self.buttonframe,
 
                                         text="All to Zero",
 
                                         command=self.alltozero,
 
                                         bg='black',
 
                                         fg='white')
 
        self.alltozerobutton.pack(side='left')
 

	
 
        self.save_stage_button = tk.Button(self.buttonframe, text="Save",
 
        self.save_stage_button = tk.Button(
 
            self.buttonframe,
 
            text="Save",
 
            command=lambda: self.save_current_stage(self.sub_name.get()),
 
            bg='black', fg='white')
 
            bg='black',
 
            fg='white')
 
        self.save_stage_button.pack(side=tk.LEFT)
 
        self.sub_name = tk.Entry(self.buttonframe, bg='black', fg='white')
 
        self.sub_name.pack(side=tk.LEFT)
 
@@ -222,11 +263,9 @@ class KeyboardComposer(tk.Frame, SubClie
 

	
 
        withgroups = []
 
        for effect in self.graph.subjects(RDF.type, L9['Effect']):
 
            withgroups.append((
 
                self.graph.value(effect, L9['group']),
 
                self.graph.value(effect, L9['order']),
 
                self.graph.label(effect),
 
                effect))
 
            withgroups.append((self.graph.value(effect, L9['group']),
 
                               self.graph.value(effect, L9['order']),
 
                               self.graph.label(effect), effect))
 
        withgroups.sort()
 

	
 
        log.info("withgroups %s", withgroups)
 
@@ -240,13 +279,15 @@ class KeyboardComposer(tk.Frame, SubClie
 
                rowcount += 1
 
                col = 0
 

	
 
            subbox = SubmasterBox(row, self.graph, effect, self.session, col, rowcount)
 
            subbox = SubmasterBox(row, self.graph, effect, self.session, col,
 
                                  rowcount)
 
            subbox.place(relx=col / 8, rely=0, relwidth=1 / 8, relheight=1)
 
            self.subbox[effect] = self.slider_table[(rowcount, col)] = subbox
 

	
 
            self.setup_key_nudgers(subbox.scale)
 

	
 
            self.effectEval[effect] = light9.effect.effecteval.EffectEval(self.graph, effect, simpleOutputs)
 
            self.effectEval[effect] = light9.effect.effecteval.EffectEval(
 
                self.graph, effect, simpleOutputs)
 

	
 
            col = (col + 1) % 8
 
            last_group = group
 
@@ -277,15 +318,19 @@ class KeyboardComposer(tk.Frame, SubClie
 
        keyhintrow = tk.Frame(self)
 

	
 
        col = 0
 
        for upkey, downkey in zip(nudge_keys['up'],
 
                                  nudge_keys['down']):
 
        for upkey, downkey in zip(nudge_keys['up'], nudge_keys['down']):
 
            # what a hack!
 
            downkey = downkey.replace('semicolon', ';')
 
            upkey, downkey = (upkey.upper(), downkey.upper())
 

	
 
            # another what a hack!
 
            keylabel = tk.Label(keyhintrow, text='%s\n%s' % (upkey, downkey),
 
                width=1, font=('Arial', 10), bg='red', fg='white', anchor='c')
 
            keylabel = tk.Label(keyhintrow,
 
                                text='%s\n%s' % (upkey, downkey),
 
                                width=1,
 
                                font=('Arial', 10),
 
                                bg='red',
 
                                fg='white',
 
                                anchor='c')
 
            keylabel.pack(side=tk.LEFT, expand=1, fill=tk.X)
 
            col += 1
 

	
 
@@ -322,7 +367,9 @@ class KeyboardComposer(tk.Frame, SubClie
 
        self.change_row(self.current_row + diff)
 

	
 
    def rowFromGraph(self):
 
        self.change_row(int(self.graph.value(self.session, L9['currentRow'], default=0)), fromGraph=True)
 
        self.change_row(int(
 
            self.graph.value(self.session, L9['currentRow'], default=0)),
 
                        fromGraph=True)
 

	
 
    def change_row(self, row, fromGraph=False):
 
        old_row = self.current_row
 
@@ -357,8 +404,7 @@ class KeyboardComposer(tk.Frame, SubClie
 
                self.sliders.valueOut("button-upper%d" % col, False)
 
                self.sliders.valueOut("slider%d" % col, 0)
 
                continue
 
            self.send_to_hw(sub=subbox.sub, hwCol=col,
 
                            boxRow=self.current_row)
 
            self.send_to_hw(sub=subbox.sub, hwCol=col, boxRow=self.current_row)
 

	
 
    def got_nudger(self, number, direction, full=0):
 
        try:
 
@@ -382,7 +428,7 @@ class KeyboardComposer(tk.Frame, SubClie
 
        try:
 
            subbox = self.slider_table[(self.current_row, col)]
 
        except KeyError:
 
            return # no slider assigned at that column
 
            return  # no slider assigned at that column
 

	
 
        if hasattr(self, 'pendingHwSet'):
 
            import twisted.internet.error
 
@@ -400,7 +446,7 @@ class KeyboardComposer(tk.Frame, SubClie
 

	
 
        if boxRow != self.current_row:
 
            return
 
        
 

	
 
        try:
 
            level = self.get_levels()[sub]
 
        except KeyError:
 
@@ -421,13 +467,16 @@ class KeyboardComposer(tk.Frame, SubClie
 
        """group is a URI or None"""
 
        row = tk.Frame(self, bd=2, bg='black')
 
        row.subGroup = group
 

	
 
        def onDrop(ev):
 
            self.change_group(sub=URIRef(ev.data), row=row)
 
            return "link"
 
        
 
        dropTargetRegister(row, onDrop=onDrop, typeList=['*'],
 

	
 
        dropTargetRegister(row,
 
                           onDrop=onDrop,
 
                           typeList=['*'],
 
                           hoverStyle=dict(background="#555500"))
 
        
 

	
 
        row.pack(expand=1, fill=tk.BOTH)
 
        self.setup_key_nudgers(row)
 
        self.rows.append(row)
 
@@ -437,9 +486,10 @@ class KeyboardComposer(tk.Frame, SubClie
 
        """update this sub's group, and maybe other sub groups as needed, so
 
        this sub displays in this row"""
 
        group = row.subGroup
 
        self.graph.patchObject(
 
            context=self.session,
 
            subject=sub, predicate=L9['group'], newObject=group)
 
        self.graph.patchObject(context=self.session,
 
                               subject=sub,
 
                               predicate=L9['group'],
 
                               newObject=group)
 

	
 
    def highlight_row(self, row):
 
        row = self.rows[row]
 
@@ -450,8 +500,9 @@ class KeyboardComposer(tk.Frame, SubClie
 
        row['bg'] = 'black'
 

	
 
    def get_levels(self):
 
        return dict([(uri, box.slider_var.get())
 
            for uri, box in self.subbox.items()])
 
        return dict([
 
            (uri, box.slider_var.get()) for uri, box in self.subbox.items()
 
        ])
 

	
 
    def get_output_settings(self, _graph=None):
 
        _graph = _graph or self.graph
 
@@ -481,11 +532,11 @@ class KeyboardComposer(tk.Frame, SubClie
 
            (effect, RDF.type, L9['Effect'], ctx),
 
            (effect, RDFS.label, Literal(subname), ctx),
 
            (effect, L9['publishAttr'], L9['strength'], ctx),
 
            ])
 
        ])
 

	
 
        self.graph.suggestPrefixes(ctx, {'eff': effect + '/'})
 
        self.graph.patch(Patch(addQuads=stmts, delQuads=[]))
 
              
 

	
 
        self.sub_name.delete(0, tk.END)
 

	
 
    def alltozero(self):
 
@@ -493,6 +544,7 @@ class KeyboardComposer(tk.Frame, SubClie
 
            if subbox.scale.scale_var.get() != 0:
 
                subbox.scale.fade(value=0.0, length=0)
 

	
 

	
 
# move to web lib
 
def postArgGetter(request):
 
    """return a function that takes arg names and returns string
 
@@ -500,19 +552,23 @@ def postArgGetter(request):
 
    support for repeated args."""
 
    # this is something nevow normally does for me
 
    request.content.seek(0)
 
    fields = cgi.FieldStorage(request.content, request.received_headers,
 
    fields = cgi.FieldStorage(request.content,
 
                              request.received_headers,
 
                              environ={'REQUEST_METHOD': 'POST'})
 

	
 
    def getArg(n):
 
        try:
 
            return request.args[n][0]
 
        except KeyError:
 
            return fields[n].value
 

	
 
    return getArg
 

	
 

	
 
class LevelServerHttp(resource.Resource):
 
    isLeaf = True
 
    def __init__(self,name_to_subbox):
 

	
 
    def __init__(self, name_to_subbox):
 
        self.name_to_subbox = name_to_subbox
 

	
 
    def render_POST(self, request):
 
@@ -521,15 +577,18 @@ class LevelServerHttp(resource.Resource)
 
        if request.path == '/fadesub':
 
            # fadesub?subname=scoop&level=0&secs=.2
 
            self.name_to_subbox[arg('subname')].scale.fade(
 
                float(arg('level')),
 
                float(arg('secs')))
 
                float(arg('level')), float(arg('secs')))
 
            return "set %s to %s" % (arg('subname'), arg('level'))
 
        else:
 
            raise NotImplementedError(repr(request))
 

	
 

	
 
class Sliders(BCF2000):
 

	
 
    def __init__(self, kc):
 
        devices = ['/dev/snd/midiC3D0', '/dev/snd/midiC2D0', '/dev/snd/midiC1D0']
 
        devices = [
 
            '/dev/snd/midiC3D0', '/dev/snd/midiC2D0', '/dev/snd/midiC1D0'
 
        ]
 
        for dev in devices:
 
            try:
 
                BCF2000.__init__(self, dev=dev)
 
@@ -541,6 +600,7 @@ class Sliders(BCF2000):
 

	
 
        self.kc = kc
 
        log.info('found sliders on %s', dev)
 

	
 
    def valueIn(self, name, value):
 
        kc = self.kc
 
        if name.startswith("slider"):
 
@@ -572,21 +632,25 @@ class Sliders(BCF2000):
 
            kc.change_row(kc.current_row + diff)
 
            self.valueOut(name, 0)
 

	
 

	
 
def launch(opts, root, graph, session):
 
    tl = toplevelat("Keyboard Composer - %s" % opts.session,
 
                    existingtoplevel=root, graph=graph, session=session)
 
                    existingtoplevel=root,
 
                    graph=graph,
 
                    session=session)
 

	
 
    kc = KeyboardComposer(tl, graph, session,
 
                          hw_sliders=not opts.no_sliders)
 
    kc = KeyboardComposer(tl, graph, session, hw_sliders=not opts.no_sliders)
 
    kc.pack(fill=tk.BOTH, expand=1)
 

	
 
    for helpline in ["Bindings: B3 mute"]:
 
        tk.Label(root,text=helpline, font="Helvetica -12 italic",
 
                 anchor='w').pack(side='top',fill='x')
 
            
 
        tk.Label(root, text=helpline, font="Helvetica -12 italic",
 
                 anchor='w').pack(side='top', fill='x')
 

	
 

	
 
if __name__ == "__main__":
 
    parser = OptionParser()
 
    parser.add_option('--no-sliders', action='store_true',
 
    parser.add_option('--no-sliders',
 
                      action='store_true',
 
                      help="don't attach to hardware sliders")
 
    clientsession.add_option(parser)
 
    parser.add_option('-v', action='store_true', help="log info level")
 
@@ -598,16 +662,16 @@ if __name__ == "__main__":
 

	
 
    # i think this also needs delayed start (like subcomposer has), to have a valid graph
 
    # before setting any stuff from the ui
 
    
 

	
 
    root = tk.Tk()
 
    initTkdnd(root.tk, 'tkdnd/trunk/')
 

	
 
    session = clientsession.getUri('keyboardcomposer', opts)
 

	
 
    graph.initiallySynced.addCallback(
 
        lambda _: launch(opts, root, graph, session))
 
    graph.initiallySynced.addCallback(lambda _: launch(opts, root, graph,
 
                                                       session))
 

	
 
    root.protocol('WM_DELETE_WINDOW', reactor.stop)
 

	
 
    tksupport.install(root,ms=20)
 
    tksupport.install(root, ms=20)
 
    prof.run(reactor.run, profile=None)
bin/lightsim
Show inline comments
 
@@ -21,26 +21,33 @@ from light9.namespaces import L9
 
from lightsim.openglsim import Surface
 

	
 
log = logging.getLogger()
 
logging.basicConfig(format="%(asctime)s %(levelname)-5s %(name)s %(filename)s:%(lineno)d: %(message)s")
 
logging.basicConfig(
 
    format=
 
    "%(asctime)s %(levelname)-5s %(name)s %(filename)s:%(lineno)d: %(message)s")
 
log.setLevel(logging.DEBUG)
 

	
 

	
 
def filenamesForChan(graph, chan):
 
    for lyr in graph.objects(chan, L9['previewLayer']):
 
        for imgPath in graph.objects(lyr, L9['path']):
 
            yield imgPath
 

	
 

	
 
_lastLevels = None
 

	
 

	
 
def poll(graph, serv, pollFreq, oglSurface):
 
    pollFreq.update()
 
    dispatcher.send("status", key="pollFreq", value=str(pollFreq))
 
    d = serv.callRemote("currentlevels", dmxclient._id)
 

	
 
    def received(dmxLevels):
 
        global _lastLevels
 
        if dmxLevels == _lastLevels:
 
            return
 
        _lastLevels = dmxLevels
 

	
 
        level = {} # filename : level
 
        level = {}  # filename : level
 
        for i, lev in enumerate(dmxLevels):
 
            if lev == 0:
 
                continue
 
@@ -54,18 +61,21 @@ def poll(graph, serv, pollFreq, oglSurfa
 
                level[str(imgPath)] = lev
 

	
 
        oglSurface.newLevels(levels=level)
 

	
 
    d.addCallback(received)
 
    return d
 

	
 

	
 
class StatusKeys(QWidget):
 
    """listens for dispatcher signal 'status' and displays the key/value args"""
 

	
 
    def __init__(self, parent):
 
        QWidget.__init__(self)
 
        self.layout = QVBoxLayout()
 
        self.setLayout(self.layout)
 
        self.row = {} # key name : (Frame, value Label)
 
        self.row = {}  # key name : (Frame, value Label)
 
        dispatcher.connect(self.status, "status")
 
        
 

	
 
    def status(self, key, value):
 
        if key not in self.row:
 
            row = QWidget()
 
@@ -81,7 +91,9 @@ class StatusKeys(QWidget):
 
            lab = self.row[key]
 
            lab.setText(value)
 

	
 

	
 
class Window(QMainWindow):
 

	
 
    def __init__(self, filenames):
 
        QMainWindow.__init__(self, None)
 
        self.setWindowTitle(dmxclient._id)
 
@@ -90,13 +102,14 @@ class Window(QMainWindow):
 
        self.setCentralWidget(w)
 
        mainLayout = QVBoxLayout()
 
        w.setLayout(mainLayout)
 
        
 
        self.glWidget = Surface(self, filenames, imgRescaleTo=128*2)
 

	
 
        self.glWidget = Surface(self, filenames, imgRescaleTo=128 * 2)
 

	
 
        mainLayout.addWidget(self.glWidget)
 

	
 
        status = StatusKeys(mainLayout)
 
        mainLayout.addWidget(status)      
 
        mainLayout.addWidget(status)
 

	
 

	
 
def requiredImages(graph):
 
    """filenames that we'll need to show, based on a config structure
 
@@ -110,6 +123,7 @@ def requiredImages(graph):
 
            filenames.append(str(p))
 
    return filenames
 

	
 

	
 
if __name__ == '__main__':
 
    app = reactor.qApp
 

	
 
@@ -123,4 +137,3 @@ if __name__ == '__main__':
 
    LoopingCall(poll, graph, serv, pollFreq, window.glWidget).start(.05)
 

	
 
    reactor.run()
 

	
bin/listsongs
Show inline comments
 
#!bin/python
 

	
 
"""for completion, print the available song uris on stdout
 

	
 
in .zshrc:
 
@@ -17,6 +16,7 @@ from rdfdb.syncedgraph import SyncedGrap
 

	
 
graph = SyncedGraph(networking.rdfdb.url, "listsongs")
 

	
 

	
 
@graph.initiallySynced.addCallback
 
def printSongs(result):
 
    with graph.currentState() as current:
 
@@ -24,4 +24,5 @@ def printSongs(result):
 
            print song
 
    reactor.stop()
 

	
 

	
 
reactor.run()
bin/mpd_timing_test
Show inline comments
 
#!/usr/bin/python
 

	
 
"""
 
records times coming out of ascoltami
 

	
 
@@ -17,5 +16,5 @@ import xmlrpclib, time
 
s = xmlrpclib.ServerProxy("http://localhost:8040")
 
start = time.time()
 
while 1:
 
    print time.time()-start,s.gettime()
 
    print time.time() - start, s.gettime()
 
    time.sleep(.01)
bin/musicPad
Show inline comments
 
@@ -11,7 +11,7 @@ logging.basicConfig(level=logging.INFO)
 
log = logging.getLogger()
 

	
 
introPad = 4
 
postPad = 9 # 5 + autostop + 4
 
postPad = 9  # 5 + autostop + 4
 

	
 
playlist = Playlist.fromShow(showconfig.getGraph(), showconfig.showUri())
 
for p in playlist.allSongPaths():
 
@@ -22,18 +22,16 @@ for p in playlist.allSongPaths():
 
    try:
 
        os.makedirs(outputDir)
 
    except OSError:
 
        pass # exists
 
        pass  # exists
 
    outputPath = os.path.join(outputDir, os.path.basename(p))
 
    outputWave = wave.open(outputPath, 'w')
 
    outputWave.setparams(inputWave.getparams())
 

	
 
    bytesPerSecond = (inputWave.getnchannels() * inputWave.getsampwidth() *
 
                      inputWave.getframerate())
 
    
 

	
 
    outputWave.writeframesraw("\x00" * (bytesPerSecond * introPad))
 
    outputWave.writeframesraw(inputWave.readframes(inputWave.getnframes()))
 
    outputWave.writeframesraw("\x00" * (bytesPerSecond * postPad))
 
    outputWave.close()
 
    log.info("wrote %s", outputPath)
 

	
 
    
bin/musictime
Show inline comments
 
@@ -6,9 +6,12 @@ import Tkinter as tk
 
import time
 
import restkit, jsonlib
 

	
 

	
 
class MusicTime:
 

	
 
    def __init__(self, url):
 
        self.player = restkit.Resource(url)
 

	
 
    def get_music_time(self):
 
        playtime = None
 
        while not playtime:
 
@@ -20,31 +23,43 @@ class MusicTime:
 
                time.sleep(2)
 
        return playtime
 

	
 

	
 
class MusicTimeTk(tk.Frame, MusicTime):
 

	
 
    def __init__(self, master, url):
 
        tk.Frame.__init__(self)
 
        MusicTime.__init__(self, url)
 
        self.timevar = tk.DoubleVar()
 
        self.timelabel = tk.Label(self, textvariable=self.timevar, bd=2,
 
            relief='raised', width=10, padx=2, pady=2, anchor='w')
 
        self.timelabel = tk.Label(self,
 
                                  textvariable=self.timevar,
 
                                  bd=2,
 
                                  relief='raised',
 
                                  width=10,
 
                                  padx=2,
 
                                  pady=2,
 
                                  anchor='w')
 
        self.timelabel.pack(expand=1, fill='both')
 

	
 
        def print_time(evt, *args):
 
            self.timevar.set(self.get_music_time())
 
            print self.timevar.get(), evt.keysym
 

	
 
        self.timelabel.bind('<KeyPress>', print_time)
 
        self.timelabel.bind('<1>', print_time)
 
        self.timelabel.focus()
 
        self.update_time()
 

	
 
    def update_time(self):
 
        self.timevar.set(self.get_music_time())
 
        self.after(100, self.update_time)
 

	
 

	
 
if __name__ == "__main__":
 
    from optparse import OptionParser
 
    parser = OptionParser()
 
    parser.add_option("-u", "--url", default=light9.networking.musicPlayer.url)
 
    options, args = parser.parse_args()
 
    
 

	
 
    root = tk.Tk()
 
    root.title("Time")
 
    MusicTimeTk(root, options.url).pack(expand=1, fill='both')
bin/paintserver
Show inline comments
 
@@ -17,31 +17,38 @@ from lib.cycloneerr import PrettyErrorHa
 
from light9.namespaces import RDF, L9, DEV
 

	
 

	
 

	
 
class Solve(PrettyErrorHandler, cyclone.web.RequestHandler):
 

	
 
class Solve(PrettyErrorHandler, cyclone.web.RequestHandler):
 
    def post(self):
 
        painting = json.loads(self.request.body)
 
        with self.settings.stats.solve.time():
 
            img = self.settings.solver.draw(painting)
 
            sample, sampleDist = self.settings.solver.bestMatch(img, device=DEV['aura2'])
 
            sample, sampleDist = self.settings.solver.bestMatch(
 
                img, device=DEV['aura2'])
 
            with self.settings.graph.currentState() as g:
 
                bestPath = g.value(sample, L9['imagePath']).replace(L9[''], '')
 
            #out = solver.solve(painting)
 
            #layers = solver.simulationLayers(out)
 
            
 
        self.write(json.dumps({
 
            'bestMatch': {'uri': sample, 'path': bestPath, 'dist': sampleDist},
 
        #    'layers': layers,
 
        #    'out': out,
 
        }))
 

	
 
        self.write(
 
            json.dumps({
 
                'bestMatch': {
 
                    'uri': sample,
 
                    'path': bestPath,
 
                    'dist': sampleDist
 
                },
 
                #    'layers': layers,
 
                #    'out': out,
 
            }))
 

	
 
    def reloadSolver(self):
 
        reload(light9.paint.solve)
 
        self.settings.solver = light9.paint.solve.Solver(self.settings.graph)
 
        self.settings.solver.loadSamples()
 

	
 

	
 
class BestMatches(PrettyErrorHandler, cyclone.web.RequestHandler):
 

	
 
    def post(self):
 
        body = json.loads(self.request.body)
 
        painting = body['painting']
 
@@ -49,35 +56,40 @@ class BestMatches(PrettyErrorHandler, cy
 
        with self.settings.stats.solve.time():
 
            img = self.settings.solver.draw(painting)
 
            outSettings = self.settings.solver.bestMatches(img, devs)
 
            self.write(json.dumps({
 
                'settings': outSettings.asList()
 
                }))
 
        
 
            self.write(json.dumps({'settings': outSettings.asList()}))
 

	
 

	
 
class App(object):
 

	
 
    def __init__(self, show, session):
 
        self.show = show
 
        self.session = session
 

	
 
        self.graph = SyncedGraph(networking.rdfdb.url, "paintServer")
 
        self.graph.initiallySynced.addCallback(self.launch).addErrback(log.error)
 
        
 
        self.stats = scales.collection('/', scales.PmfStat('solve'),
 
                                       )
 
       
 
        self.graph.initiallySynced.addCallback(self.launch).addErrback(
 
            log.error)
 

	
 
        self.stats = scales.collection(
 
            '/',
 
            scales.PmfStat('solve'),
 
        )
 

	
 
    def launch(self, *args):
 

	
 
        self.solver = light9.paint.solve.Solver(self.graph, sessions=[
 
            L9['show/dance2017/capture/aura1/cap1876596'],
 
            L9['show/dance2017/capture/aura2/cap1876792'],
 
            L9['show/dance2017/capture/aura3/cap1877057'],
 
            L9['show/dance2017/capture/aura4/cap1877241'],
 
            L9['show/dance2017/capture/aura5/cap1877406'],
 
            L9['show/dance2017/capture/q1/cap1874255'],
 
            L9['show/dance2017/capture/q2/cap1873665'],
 
            L9['show/dance2017/capture/q3/cap1876223'],
 
        ])
 
        self.solver = light9.paint.solve.Solver(
 
            self.graph,
 
            sessions=[
 
                L9['show/dance2017/capture/aura1/cap1876596'],
 
                L9['show/dance2017/capture/aura2/cap1876792'],
 
                L9['show/dance2017/capture/aura3/cap1877057'],
 
                L9['show/dance2017/capture/aura4/cap1877241'],
 
                L9['show/dance2017/capture/aura5/cap1877406'],
 
                L9['show/dance2017/capture/q1/cap1874255'],
 
                L9['show/dance2017/capture/q2/cap1873665'],
 
                L9['show/dance2017/capture/q3/cap1876223'],
 
            ])
 
        self.solver.loadSamples()
 
        
 

	
 
        self.cycloneApp = cyclone.web.Application(handlers=[
 
            (r'/stats', StatsForCyclone),
 
            (r'/solve', Solve),
 
@@ -93,12 +105,16 @@ class App(object):
 

	
 
if __name__ == "__main__":
 
    parser = optparse.OptionParser()
 
    parser.add_option('--show',
 
    parser.add_option(
 
        '--show',
 
        help='show URI, like http://light9.bigasterisk.com/show/dance2008',
 
                      default=showconfig.showUri())
 
    parser.add_option("-v", "--verbose", action="store_true",
 
        default=showconfig.showUri())
 
    parser.add_option("-v",
 
                      "--verbose",
 
                      action="store_true",
 
                      help="logging.DEBUG")
 
    parser.add_option("--twistedlog", action="store_true",
 
    parser.add_option("--twistedlog",
 
                      action="store_true",
 
                      help="twisted logging")
 
    clientsession.add_option(parser)
 
    (options, args) = parser.parse_args()
 
@@ -106,7 +122,7 @@ if __name__ == "__main__":
 

	
 
    if not options.show:
 
        raise ValueError("missing --show http://...")
 
        
 

	
 
    session = clientsession.getUri('paint', options)
 

	
 
    app = App(URIRef(options.show), session)
bin/picamserve
Show inline comments
 
#!env_pi/bin/python
 
from __future__ import division
 
from run_local import log
 
import sys;sys.path.append('/usr/lib/python2.7/dist-packages/')
 
import sys
 
sys.path.append('/usr/lib/python2.7/dist-packages/')
 
import io, logging, traceback, time
 
import cyclone.web
 
from twisted.internet import reactor, threads
 
@@ -12,16 +13,24 @@ try:
 
    import picamera
 
    cameraCls = picamera.PiCamera
 
except ImportError:
 

	
 
    class cameraCls(object):
 
        def __enter__(self): return self
 
        def __exit__(self, *a): pass
 

	
 
        def __enter__(self):
 
            return self
 

	
 
        def __exit__(self, *a):
 
            pass
 

	
 
        def capture(self, out, *a, **kw):
 
            out.write(open('yuv.demo').read())
 

	
 
        def capture_continuous(self, *a, **kw):
 
            for i in range(1000):
 
                time.sleep(1)
 
                yield str(i)
 

	
 

	
 
def setCameraParams(c, arg):
 
    res = int(arg('res', 480))
 
    c.resolution = {
 
@@ -33,14 +42,15 @@ def setCameraParams(c, arg):
 
    c.exposure_mode = arg('exposure_mode', 'fixedfps')
 
    c.awb_mode = arg('awb_mode', 'off')
 
    c.brightness = int(arg('brightness', 50))
 
    c.exposure_compensation= int(arg('exposure_compensation', 0))
 
    c.exposure_compensation = int(arg('exposure_compensation', 0))
 
    c.awb_gains = (float(arg('redgain', 1)), float(arg('bluegain', 1)))
 
    c.ISO = int(arg('iso', 250))
 
    c.rotation = int(arg('rotation', '0'))
 

	
 

	
 
def setupCrop(c, arg):
 
    c.crop = (float(arg('x', 0)), float(arg('y', 0)),
 
              float(arg('w', 1)), float(arg('h', 1)))
 
    c.crop = (float(arg('x', 0)), float(arg('y', 0)), float(arg('w', 1)),
 
              float(arg('h', 1)))
 
    rw = rh = int(arg('resize', 100))
 
    # width 1920, showing w=.3 of image, resize=100 -> scale is 100/.3*1920
 
    # scl is [ output px / camera px ]
 
@@ -53,7 +63,8 @@ def setupCrop(c, arg):
 
        # height is the constraint
 
        rw = int(scl2 * c.crop[2] * c.resolution[0])
 
    return rw, rh
 
    
 

	
 

	
 
@prof.logTime
 
def getFrame(c, arg):
 
    setCameraParams(c, arg)
 
@@ -61,8 +72,10 @@ def getFrame(c, arg):
 
    out = io.BytesIO('w')
 
    prof.logTime(c.capture)(out, 'jpeg', use_video_port=True, resize=resize)
 
    return out.getvalue()
 
    
 

	
 

	
 
class Pic(cyclone.web.RequestHandler):
 

	
 
    def get(self):
 
        try:
 
            self.set_header('Content-Type', 'image/jpeg')
 
@@ -70,20 +83,24 @@ class Pic(cyclone.web.RequestHandler):
 
        except Exception:
 
            traceback.print_exc()
 

	
 

	
 
def captureContinuousAsync(c, resize, onFrame):
 
    """
 
    Calls c.capture_continuous is called in another thread. onFrame is
 
    called in this reactor thread with each (frameTime, frame)
 
    result. Runs until onFrame raises StopIteration.
 
    """
 

	
 
    def runner(c, resize):
 
        stream = io.BytesIO()
 
        t = time.time()
 
        for nextFrame in c.capture_continuous(stream, 'jpeg', use_video_port=True,
 
        for nextFrame in c.capture_continuous(stream,
 
                                              'jpeg',
 
                                              use_video_port=True,
 
                                              resize=resize):
 
            t2 = time.time()
 
            log.debug(" - framecap got %s bytes in %.1f ms",
 
                     len(stream.getvalue()), 1000 * (t2 - t))
 
                      len(stream.getvalue()), 1000 * (t2 - t))
 
            try:
 
                # This is slow, like 13ms. Hopefully
 
                # capture_continuous is working on gathering the next
 
@@ -91,8 +108,8 @@ def captureContinuousAsync(c, resize, on
 
                # Instead, we could be stashing frames onto a queue or
 
                # something that the main thread can pull when
 
                # possible (and toss if it gets behind).
 
                threads.blockingCallFromThread(reactor,
 
                                               onFrame, t, stream.getvalue())
 
                threads.blockingCallFromThread(reactor, onFrame, t,
 
                                               stream.getvalue())
 
            except StopIteration:
 
                break
 
            t3 = time.time()
 
@@ -100,30 +117,35 @@ def captureContinuousAsync(c, resize, on
 
            stream.truncate()
 
            stream.seek(0)
 
            t = time.time()
 

	
 
    return threads.deferToThread(runner, c, resize)
 

	
 

	
 
class FpsReport(object):
 

	
 
    def __init__(self):
 
        self.frameTimes = []
 
        self.lastFpsLog = 0
 
        
 

	
 
    def frame(self):
 
        now = time.time()
 
        
 

	
 
        self.frameTimes.append(now)
 
        
 

	
 
        if len(self.frameTimes) > 15:
 
            del self.frameTimes[:5]
 
            
 

	
 
        if now > self.lastFpsLog + 2 and len(self.frameTimes) > 5:
 
            deltas = [(b - a) for a, b in zip(self.frameTimes[:-1],
 
                                              self.frameTimes[1:])]
 
            deltas = [(b - a)
 
                      for a, b in zip(self.frameTimes[:-1], self.frameTimes[1:])
 
                     ]
 
            avg = sum(deltas) / len(deltas)
 
            log.info("fps: %.1f", 1 / avg)
 
            self.lastFpsLog = now
 
        
 
    
 

	
 

	
 
class Pics(cyclone.web.RequestHandler):
 

	
 
    @inlineCallbacks
 
    def get(self):
 
        try:
 
@@ -131,21 +153,22 @@ class Pics(cyclone.web.RequestHandler):
 
            c = self.settings.camera
 
            setCameraParams(c, self.get_argument)
 
            resize = setupCrop(c, self.get_argument)
 
                          
 

	
 
            self.running = True
 
            log.info("connection open from %s", self.request.remote_ip)
 
            fpsReport = FpsReport()
 
            
 

	
 
            def onFrame(frameTime, frame):
 
                if not self.running:
 
                    raise StopIteration
 
                    
 

	
 
                now = time.time()
 
                self.write("%s %s\n" % (len(frame), frameTime))
 
                self.write(frame)
 
                self.flush()
 

	
 
                fpsReport.frame()
 

	
 
            # another camera request coming in at the same time breaks
 
            # the server. it would be nice if this request could
 
            # let-go-and-reopen when it knows about another request
 
@@ -153,21 +176,30 @@ class Pics(cyclone.web.RequestHandler):
 
            yield captureContinuousAsync(c, resize, onFrame)
 
        except Exception:
 
            traceback.print_exc()
 
            
 

	
 
    def on_connection_close(self, *a, **kw):
 
        log.info("connection closed")
 
        self.running = False
 
            
 

	
 

	
 
log.setLevel(logging.INFO)
 

	
 
with cameraCls() as camera:
 
    port = 8208
 
    reactor.listenTCP(port, cyclone.web.Application(handlers=[
 
        (r'/pic', Pic),
 
        (r'/pics', Pics),
 
        (r'/static/(.*)', cyclone.web.StaticFileHandler, {'path': 'light9/web/'}),
 
        (r'/(|gui.js)', cyclone.web.StaticFileHandler, {'path': 'light9/vidref/',
 
                                                 'default_filename': 'index.html'}),
 
        ], debug=True, camera=camera))
 
    reactor.listenTCP(
 
        port,
 
        cyclone.web.Application(handlers=[
 
            (r'/pic', Pic),
 
            (r'/pics', Pics),
 
            (r'/static/(.*)', cyclone.web.StaticFileHandler, {
 
                'path': 'light9/web/'
 
            }),
 
            (r'/(|gui.js)', cyclone.web.StaticFileHandler, {
 
                'path': 'light9/vidref/',
 
                'default_filename': 'index.html'
 
            }),
 
        ],
 
                                debug=True,
 
                                camera=camera))
 
    log.info("serving on %s" % port)
 
    reactor.run()
bin/rdfdb
Show inline comments
 
@@ -5,8 +5,9 @@ from light9 import networking, showconfi
 
import rdfdb.service
 

	
 
rdfdb.service.main(
 
    dirUriMap={os.environ['LIGHT9_SHOW'].rstrip('/') + '/':
 
               showconfig.showUri() + '/'},
 
    dirUriMap={
 
        os.environ['LIGHT9_SHOW'].rstrip('/') + '/': showconfig.showUri() + '/'
 
    },
 
    prefixes={
 
        'show': showconfig.showUri() + '/',
 
        '': 'http://light9.bigasterisk.com/',
 
@@ -17,5 +18,4 @@ rdfdb.service.main(
 
        'dev': 'http://light9.bigasterisk.com/device/',
 
    },
 
    port=networking.rdfdb.port,
 
    )
 

	
 
)
bin/run_local.py
Show inline comments
 
@@ -4,30 +4,37 @@
 

	
 
import sys, os, socket
 

	
 

	
 
def fixSysPath():
 
    root = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '..')) + '/'
 
    root = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]),
 
                                        '..')) + '/'
 

	
 
    # this is site-packages/zope.interface-4.5.0-py2.7-nspkg.pth,
 
    # slightly edited.
 
    import types
 
    has_mfs = sys.version_info > (3, 5);
 
    has_mfs = sys.version_info > (3, 5)
 
    p = root + 'env/local/lib/python2.7/site-packages/zope'
 
    importlib = has_mfs and __import__('importlib.util');
 
    has_mfs and __import__('importlib.machinery');
 
    importlib = has_mfs and __import__('importlib.util')
 
    has_mfs and __import__('importlib.machinery')
 
    m = has_mfs and sys.modules.setdefault(
 
        'zope', importlib.util.module_from_spec(
 
            importlib.machinery.PathFinder.find_spec(
 
                'zope', [os.path.dirname(p)])));
 
    m = m or sys.modules.setdefault('zope', types.ModuleType('zope'));
 
    mp = (m or []) and m.__dict__.setdefault('__path__',[]);
 
        'zope',
 
        importlib.util.module_from_spec(
 
            importlib.machinery.PathFinder.find_spec('zope',
 
                                                     [os.path.dirname(p)])))
 
    m = m or sys.modules.setdefault('zope', types.ModuleType('zope'))
 
    mp = (m or []) and m.__dict__.setdefault('__path__', [])
 
    (p not in mp) and mp.append(p)
 
    
 

	
 
    p = root + 'env/local/lib/python2.7/site-packages/greplin'
 
    importlib = has_mfs and __import__('importlib.util');
 
    has_mfs and __import__('importlib.machinery');
 
    m = has_mfs and sys.modules.setdefault('greplin', importlib.util.module_from_spec(importlib.machinery.PathFinder.find_spec('greplin', [os.path.dirname(p)])));
 
    m = m or sys.modules.setdefault('greplin', types.ModuleType('greplin'));
 
    mp = (m or []) and m.__dict__.setdefault('__path__',[]);
 
    importlib = has_mfs and __import__('importlib.util')
 
    has_mfs and __import__('importlib.machinery')
 
    m = has_mfs and sys.modules.setdefault(
 
        'greplin',
 
        importlib.util.module_from_spec(
 
            importlib.machinery.PathFinder.find_spec('greplin',
 
                                                     [os.path.dirname(p)])))
 
    m = m or sys.modules.setdefault('greplin', types.ModuleType('greplin'))
 
    mp = (m or []) and m.__dict__.setdefault('__path__', [])
 
    (p not in mp) and mp.append(p)
 

	
 
    sys.path = [
 
@@ -46,6 +53,7 @@ def fixSysPath():
 
        root + 'env/lib/python2.7/site-packages/gtk-2.0',
 
    ]
 

	
 

	
 
fixSysPath()
 

	
 
from twisted.python.failure import Failure
 
@@ -55,12 +63,14 @@ try:
 
except ImportError:
 
    pass
 
else:
 

	
 
    def rce(self, exc, val, tb):
 
        sys.stderr.write("Exception in Tkinter callback\n")
 
        if True:
 
            sys.excepthook(exc, val, tb)
 
        else:
 
            Failure(val, exc, tb).printDetailedTraceback()
 

	
 
    Tkinter.Tk.report_callback_exception = rce
 

	
 
import coloredlogs, logging, time
 
@@ -71,10 +81,12 @@ except ImportError:
 
    pass
 

	
 
progName = sys.argv[0].split('/')[-1]
 
log = logging.getLogger() # this has to get the root logger
 
log.name = progName # but we can rename it for clarity
 
log = logging.getLogger()  # this has to get the root logger
 
log.name = progName  # but we can rename it for clarity
 

	
 

	
 
class FractionTimeFilter(logging.Filter):
 

	
 
    def filter(self, record):
 
        record.fractionTime = (
 
            time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(record.created)) +
 
@@ -82,6 +94,7 @@ class FractionTimeFilter(logging.Filter)
 
        # Don't filter the record.
 
        return 1
 

	
 

	
 
coloredlogs.install(
 
    level='DEBUG',
 
    fmt='%(fractionTime)s %(name)s[%(process)d] %(levelname)s %(message)s')
 
@@ -90,10 +103,13 @@ logging.getLogger().handlers[0].addFilte
 

	
 
def setTerminalTitle(s):
 
    if os.environ.get('TERM', '') in ['xterm', 'rxvt', 'rxvt-unicode-256color']:
 
        print "\033]0;%s\007" % s # not escaped/protected correctly
 
        print "\033]0;%s\007" % s  # not escaped/protected correctly
 

	
 

	
 
if 'listsongs' not in sys.argv[0] and 'homepageConfig' not in sys.argv[0]:
 
    setTerminalTitle('[%s] %s' % (socket.gethostname(), ' '.join(sys.argv).replace('bin/', '')))
 
    setTerminalTitle(
 
        '[%s] %s' %
 
        (socket.gethostname(), ' '.join(sys.argv).replace('bin/', '')))
 

	
 
# see http://www.youtube.com/watch?v=3cIOT9kM--g for commands that make
 
# profiles and set background images
bin/staticclient
Show inline comments
 
@@ -16,7 +16,8 @@ from light9 import dmxclient, showconfig
 

	
 
if __name__ == "__main__":
 
    parser = OptionParser(usage="%prog")
 
    parser.add_option('--chan', help='channel number, starts at 1', type=int) #todo: or name or uri
 
    parser.add_option('--chan', help='channel number, starts at 1',
 
                      type=int)  #todo: or name or uri
 
    parser.add_option('--level', help='0..1', type=float)
 
    parser.add_option('-v', action='store_true', help="log debug level")
 

	
 
@@ -26,9 +27,11 @@ if __name__ == "__main__":
 

	
 
    levels = [0] * (opts.chan - 1) + [opts.level]
 
    log.info('staticclient will write this forever: %r', levels)
 

	
 
    def write():
 
        log.debug('writing %r', levels)
 
        dmxclient.outputlevels(levels, twisted=1)
 

	
 
    log.info('looping...')
 
    task.LoopingCall(write).start(1)
 
    reactor.run()
bin/subcomposer
Show inline comments
 
@@ -69,6 +69,7 @@ class Subcomposer(tk.Frame):
 
      Submaster.editLevel -> graph (handled in Submaster)
 

	
 
    """
 

	
 
    def __init__(self, master, graph, session):
 
        tk.Frame.__init__(self, master, bg='black')
 
        self.graph = graph
 
@@ -83,11 +84,12 @@ class Subcomposer(tk.Frame):
 
        self._currentChoice = Observable(Local)
 

	
 
        # this is a PersistentSubmaster (even for local)
 
        self.currentSub = Observable(Submaster.PersistentSubmaster(
 
                    graph, self.switchToLocal()))
 
        self.currentSub = Observable(
 
            Submaster.PersistentSubmaster(graph, self.switchToLocal()))
 

	
 
        def pc(val):
 
            log.info("change viewed sub to %s", val)
 

	
 
        self._currentChoice.subscribe(pc)
 

	
 
        ec = self.editChoice = EditChoice(self, self.graph, self._currentChoice)
 
@@ -105,39 +107,41 @@ class Subcomposer(tk.Frame):
 
        e.pack()
 
        b = tk.Button(box, text="Make global")
 
        b.pack()
 

	
 
        def clicked(*args):
 
            self.makeGlobal(newName=e.get())
 
            box.destroy()
 
            
 

	
 
        b.bind("<Button-1>", clicked)
 
        e.focus()
 
        
 

	
 
    def makeGlobal(self, newName):
 
        """promote our local submaster into a non-local, named one"""
 
        uri = self.currentSub().uri
 
        newUri = showconfig.showUri() + ("/sub/%s" %
 
                                         urllib.quote(newName, safe=''))
 
        with self.graph.currentState(
 
                tripleFilter=(uri, None, None)) as current:
 
        with self.graph.currentState(tripleFilter=(uri, None, None)) as current:
 
            if (uri, RDF.type, L9['LocalSubmaster']) not in current:
 
                raise ValueError("%s is not a local submaster" % uri)
 
            if (newUri, None, None) in current:
 
                raise ValueError("new uri %s is in use" % newUri)
 
                
 

	
 
        # the local submaster was storing in ctx=self.session, but now
 
        # we want it to be in ctx=uri
 

	
 
        self.relocateSub(newUri, newName)
 

	
 
        # these are in separate patches for clarity as i'm debugging this
 
        self.graph.patch(Patch(addQuads=[
 
            (newUri, RDFS.label, Literal(newName), newUri),
 
        ], delQuads=[
 
            (newUri, RDF.type, L9['LocalSubmaster'], newUri),
 
                           ]))
 
        self.graph.patchObject(self.session,
 
                               self.session, L9['currentSub'], newUri)
 
        
 
        self.graph.patch(
 
            Patch(addQuads=[
 
                (newUri, RDFS.label, Literal(newName), newUri),
 
            ],
 
                  delQuads=[
 
                      (newUri, RDF.type, L9['LocalSubmaster'], newUri),
 
                  ]))
 
        self.graph.patchObject(self.session, self.session, L9['currentSub'],
 
                               newUri)
 

	
 
    def relocateSub(self, newUri, newName):
 
        # maybe this goes in Submaster
 
        uri = self.currentSub().uri
 
@@ -146,15 +150,16 @@ class Subcomposer(tk.Frame):
 
            if u == uri:
 
                return newUri
 
            return u
 

	
 
        delQuads = self.currentSub().allQuads()
 
        addQuads = [(repl(s), p, repl(o), newUri) for s,p,o,c in delQuads]
 
        addQuads = [(repl(s), p, repl(o), newUri) for s, p, o, c in delQuads]
 
        # patch can't span contexts yet
 
        self.graph.patch(Patch(addQuads=addQuads, delQuads=[]))
 
        self.graph.patch(Patch(addQuads=[], delQuads=delQuads))
 
        
 
        
 

	
 
    def setupSubChoiceLinks(self):
 
        graph = self.graph
 

	
 
        def ann():
 
            print "currently: session=%s currentSub=%r _currentChoice=%r" % (
 
                self.session, self.currentSub(), self._currentChoice())
 
@@ -165,7 +170,7 @@ class Subcomposer(tk.Frame):
 
            # are failing. this calms things down.
 
            log.warn('skip graphChanged')
 
            return
 
        
 

	
 
            s = graph.value(self.session, L9['currentSub'])
 
            log.debug('HANDLER getting session currentSub from graph: %s', s)
 
            if s is None:
 
@@ -176,12 +181,12 @@ class Subcomposer(tk.Frame):
 
        def subChanged(newSub):
 
            log.debug('HANDLER currentSub changed to %s', newSub)
 
            if newSub is None:
 
                graph.patchObject(self.session,
 
                                  self.session, L9['currentSub'], None)
 
                graph.patchObject(self.session, self.session, L9['currentSub'],
 
                                  None)
 
                return
 
            self.sendupdate()
 
            graph.patchObject(self.session,
 
                              self.session, L9['currentSub'], newSub.uri)
 
            graph.patchObject(self.session, self.session, L9['currentSub'],
 
                              newSub.uri)
 

	
 
            localStmt = (newSub.uri, RDF.type, L9['LocalSubmaster'])
 
            with graph.currentState(tripleFilter=localStmt) as current:
 
@@ -195,7 +200,7 @@ class Subcomposer(tk.Frame):
 
                    self._currentChoice(newSub.uri)
 

	
 
        dispatcher.connect(self.levelsChanged, "sub levels changed")
 
            
 

	
 
        @self._currentChoice.subscribe
 
        def choiceChanged(newChoice):
 
            log.debug('HANDLER choiceChanged to %s', newChoice)
 
@@ -203,13 +208,14 @@ class Subcomposer(tk.Frame):
 
                newChoice = self.switchToLocal()
 
            if newChoice is not None:
 
                newSub = Submaster.PersistentSubmaster(graph, newChoice)
 
                log.debug('write new choice to currentSub, from %r to %r', self.currentSub(), newSub)
 
                log.debug('write new choice to currentSub, from %r to %r',
 
                          self.currentSub(), newSub)
 
                self.currentSub(newSub)
 

	
 
    def levelsChanged(self, sub):
 
        if sub == self.currentSub():
 
            self.sendupdate()
 
        
 

	
 
    def switchToLocal(self):
 
        """
 
        change our display to a local submaster
 
@@ -220,27 +226,30 @@ class Subcomposer(tk.Frame):
 
        self.localSerial += 1
 
        new = URIRef("http://light9.bigasterisk.com/sub/local/%s" % localId)
 
        log.debug('making up a local sub %s', new)
 
        self.graph.patch(Patch(addQuads=[
 
            (new, RDF.type, L9['Submaster'], self.session),
 
            (new, RDF.type, L9['LocalSubmaster'], self.session),
 
        ]))
 
        
 
        self.graph.patch(
 
            Patch(addQuads=[
 
                (new, RDF.type, L9['Submaster'], self.session),
 
                (new, RDF.type, L9['LocalSubmaster'], self.session),
 
            ]))
 

	
 
        return new
 
        
 

	
 
    def setupLevelboxUi(self):
 
        self.levelbox = Levelbox(self, self.graph, self.currentSub)
 
        self.levelbox.pack(side='top')
 

	
 
        tk.Button(self, text="All to zero",
 
             command=lambda *args: self.currentSub().clear()).pack(side='top')
 
        tk.Button(
 
            self,
 
            text="All to zero",
 
            command=lambda *args: self.currentSub().clear()).pack(side='top')
 

	
 
    def savenewsub(self, subname):
 
        leveldict={}
 
        for i,lev in zip(range(len(self.levels)),self.levels):
 
            if lev!=0:
 
                leveldict[get_channel_name(i+1)]=lev
 
        leveldict = {}
 
        for i, lev in zip(range(len(self.levels)), self.levels):
 
            if lev != 0:
 
                leveldict[get_channel_name(i + 1)] = lev
 

	
 
        s=Submaster.Submaster(subname, levels=leveldict)
 
        s = Submaster.Submaster(subname, levels=leveldict)
 
        s.save()
 

	
 
    def sendupdate(self):
 
@@ -252,14 +261,15 @@ def launch(opts, args, root, graph, sess
 
    if not opts.no_geometry:
 
        toplevelat("subcomposer - %s" % opts.session, root, graph, session)
 

	
 
        
 
    sc = Subcomposer(root, graph, session)
 
    sc.pack()
 
    
 

	
 
    subcomposerweb.init(graph, session, sc.currentSub)
 

	
 
    tk.Label(root,text="Bindings: B1 adjust level; B2 set full; B3 instant bump",
 
             font="Helvetica -12 italic",anchor='w').pack(side='top',fill='x')
 
    tk.Label(root,
 
             text="Bindings: B1 adjust level; B2 set full; B3 instant bump",
 
             font="Helvetica -12 italic",
 
             anchor='w').pack(side='top', fill='x')
 

	
 
    if len(args) == 1:
 
        # it might be a little too weird that cmdline arg to this
 
@@ -269,8 +279,7 @@ def launch(opts, args, root, graph, sess
 
        # same window pos), so maybe it doesn't matter. But still,
 
        # this tool should probably default to making new sessions
 
        # usually instead of loading the same one
 
        graph.patchObject(session,
 
                          session, L9['currentSub'], URIRef(args[0]))
 
        graph.patchObject(session, session, L9['currentSub'], URIRef(args[0]))
 

	
 
    task.LoopingCall(sc.sendupdate).start(10)
 

	
 
@@ -279,7 +288,8 @@ def launch(opts, args, root, graph, sess
 

	
 
if __name__ == "__main__":
 
    parser = OptionParser(usage="%prog [suburi]")
 
    parser.add_option('--no-geometry', action='store_true',
 
    parser.add_option('--no-geometry',
 
                      action='store_true',
 
                      help="don't save/restore window geometry")
 
    parser.add_option('-v', action='store_true', help="log debug level")
 

	
 
@@ -287,8 +297,8 @@ if __name__ == "__main__":
 
    opts, args = parser.parse_args()
 

	
 
    log.setLevel(logging.DEBUG if opts.v else logging.INFO)
 
    
 
    root=tk.Tk()
 

	
 
    root = tk.Tk()
 
    root.config(bg='black')
 
    root.tk_setPalette("#004633")
 

	
 
@@ -297,8 +307,9 @@ if __name__ == "__main__":
 
    graph = SyncedGraph(networking.rdfdb.url, "subcomposer")
 
    session = clientsession.getUri('subcomposer', opts)
 

	
 
    graph.initiallySynced.addCallback(lambda _: launch(opts, args, root, graph, session))
 
    graph.initiallySynced.addCallback(lambda _: launch(opts, args, root, graph,
 
                                                       session))
 

	
 
    root.protocol('WM_DELETE_WINDOW', reactor.stop)
 
    tksupport.install(root,ms=10)
 
    tksupport.install(root, ms=10)
 
    prof.run(reactor.run, profile=False)
bin/subserver
Show inline comments
 
@@ -16,17 +16,19 @@ from light9.namespaces import L9, DCTERM
 
from light9 import networking, showconfig
 

	
 
from lib.cycloneerr import PrettyErrorHandler
 
        
 

	
 

	
 
class Static(PrettyErrorHandler, cyclone.web.StaticFileHandler):
 

	
 
    def get(self, path, *args, **kw):
 
        if path in ['', 'effects']:
 
            return self.respondStaticJade("light9/subserver/%s.jade" %
 
                                          (path or 'index'))
 
            
 

	
 
        if path.endswith(".js"):
 
            return self.responseStaticCoffee(
 
                'light9/subserver/%s' %
 
                path.replace(".js", ".coffee")) # potential security hole
 
                path.replace(".js", ".coffee"))  # potential security hole
 

	
 
        cyclone.web.StaticFileHandler.get(self, path, *args, **kw)
 

	
 
@@ -35,14 +37,18 @@ class Static(PrettyErrorHandler, cyclone
 
        self.write(html)
 

	
 
    def responseStaticCoffee(self, src):
 
        self.write(subprocess.check_output([
 
            '/usr/bin/coffee', '--compile', '--print', src]))
 
        self.write(
 
            subprocess.check_output(
 
                ['/usr/bin/coffee', '--compile', '--print', src]))
 

	
 

	
 
class Snapshot(PrettyErrorHandler, cyclone.web.RequestHandler):
 

	
 
    @defer.inlineCallbacks
 
    def post(self):
 
        about = URIRef(self.get_argument("about"))
 
        response = yield cyclone.httpclient.fetch(networking.vidref.path("snapshot"), method="POST", timeout=1)
 
        response = yield cyclone.httpclient.fetch(
 
            networking.vidref.path("snapshot"), method="POST", timeout=1)
 

	
 
        snapUri = URIRef(json.loads(response.body)['snapshot'])
 
        # vidref could write about when it was taken, etc. would it be
 
@@ -50,15 +56,17 @@ class Snapshot(PrettyErrorHandler, cyclo
 
        # the graph, and then it doesn't even have to return anything?
 

	
 
        ctx = showconfig.showUri() + "/snapshots"
 
        
 
        self.settings.graph.patch(Patch(addQuads=[
 
            (about, L9['image'], snapUri, ctx),
 
            (snapUri, DCTERMS['created'],
 
             Literal(datetime.datetime.now(tzlocal())), ctx),
 

	
 
        self.settings.graph.patch(
 
            Patch(addQuads=[
 
                (about, L9['image'], snapUri, ctx),
 
                (snapUri, DCTERMS['created'],
 
                 Literal(datetime.datetime.now(tzlocal())), ctx),
 
            ]))
 
        
 

	
 
        self.write(json.dumps({'snapshot': snapUri}))
 

	
 

	
 
def newestImage(subject):
 
    newest = (None, None)
 
    for img in graph.objects(subject, L9['image']):
 
@@ -66,10 +74,13 @@ def newestImage(subject):
 
        if created > newest[0]:
 
            newest = (created, img)
 
    return newest[1]
 
        
 

	
 

	
 
if __name__ == "__main__":
 
    parser = optparse.OptionParser()
 
    parser.add_option("-v", "--verbose", action="store_true",
 
    parser.add_option("-v",
 
                      "--verbose",
 
                      action="store_true",
 
                      help="logging.DEBUG")
 
    (options, args) = parser.parse_args()
 

	
 
@@ -77,13 +88,17 @@ if __name__ == "__main__":
 

	
 
    graph = SyncedGraph(networking.rdfdb.url, "subServer")
 

	
 
    
 
    port = networking.subServer.port
 
    reactor.listenTCP(port, cyclone.web.Application(handlers=[
 
        (r'/snapshot', Snapshot),
 
        (r'/(.*)', Static,
 
         {"path" : "light9/subserver",
 
          "default_filename" : "index.jade"}),
 
        ], debug=True, graph=graph))
 
    reactor.listenTCP(
 
        port,
 
        cyclone.web.Application(handlers=[
 
            (r'/snapshot', Snapshot),
 
            (r'/(.*)', Static, {
 
                "path": "light9/subserver",
 
                "default_filename": "index.jade"
 
            }),
 
        ],
 
                                debug=True,
 
                                graph=graph))
 
    log.info("serving on %s" % port)
 
    reactor.run()
bin/tkdnd_minimal_drop.py
Show inline comments
 
@@ -13,8 +13,7 @@ label.config(text="drop target %s" % lab
 
frame1 = tk.Frame()
 
frame1.pack()
 

	
 
labelInner = tk.Label(frame1, borderwidth=2,
 
                      relief='groove', padx=10, pady=10)
 
labelInner = tk.Label(frame1, borderwidth=2, relief='groove', padx=10, pady=10)
 
labelInner.pack(side='left')
 
labelInner.config(text="drop target inner %s" % labelInner._w)
 
tk.Label(frame1, text="not a target").pack(side='left')
 
@@ -22,21 +21,36 @@ tk.Label(frame1, text="not a target").pa
 

	
 
def onDrop(ev):
 
    print "onDrop", ev
 

	
 

	
 
def enter(ev):
 
    print 'enter', ev
 

	
 

	
 
def leave(ev):
 
    print 'leave', ev
 
dropTargetRegister(label, onDrop=onDrop, onDropEnter=enter, onDropLeave=leave,
 

	
 

	
 
dropTargetRegister(label,
 
                   onDrop=onDrop,
 
                   onDropEnter=enter,
 
                   onDropLeave=leave,
 
                   hoverStyle=dict(background="yellow", relief='groove'))
 

	
 
dropTargetRegister(labelInner, onDrop=onDrop, onDropEnter=enter, onDropLeave=leave,
 
dropTargetRegister(labelInner,
 
                   onDrop=onDrop,
 
                   onDropEnter=enter,
 
                   onDropLeave=leave,
 
                   hoverStyle=dict(background="yellow", relief='groove'))
 

	
 

	
 
def prn():
 
    print "cont", root.winfo_containing(201,151)
 
    print "cont", root.winfo_containing(201, 151)
 

	
 

	
 
b = tk.Button(root, text="coord", command=prn)
 
b.pack()
 

	
 
#tk.mainloop()
 
tksupport.install(root,ms=10)
 
tksupport.install(root, ms=10)
 
reactor.run()
bin/tracker
Show inline comments
 
#!/usr/bin/python
 
from __future__ import division,nested_scopes
 
from __future__ import division, nested_scopes
 

	
 
import sys
 
sys.path.append("../../editor/pour")
 
sys.path.append("../light8")
 

	
 
from Submaster import Submaster
 
from skim.zooming import Zooming,Pair
 
from math import sqrt,sin,cos
 
from skim.zooming import Zooming, Pair
 
from math import sqrt, sin, cos
 
from pygame.rect import Rect
 
from xmlnodebase import xmlnodeclass,collectiveelement,xmldocfile
 
from xmlnodebase import xmlnodeclass, collectiveelement, xmldocfile
 
from dispatch import dispatcher
 

	
 
import dmxclient
 

	
 
import Tkinter as tk
 

	
 
defaultfont="arial 8"
 
defaultfont = "arial 8"
 

	
 
def pairdist(pair1,pair2):
 

	
 
def pairdist(pair1, pair2):
 
    return pair1.dist(pair2)
 

	
 
def canvashighlighter(canvas,obj,attribute,normalval,highlightval):
 

	
 
def canvashighlighter(canvas, obj, attribute, normalval, highlightval):
 
    """creates bindings on a canvas obj that make attribute go
 
    from normal to highlight when the mouse is over the obj"""
 
    canvas.tag_bind(obj,"<Enter>",
 
                    lambda ev: canvas.itemconfig(obj,**{attribute:highlightval}))
 
    canvas.tag_bind(obj,"<Leave>",
 
                    lambda ev: canvas.itemconfig(obj,**{attribute:normalval}))
 
    canvas.tag_bind(
 
        obj, "<Enter>", lambda ev: canvas.itemconfig(
 
            obj, **{attribute: highlightval}))
 
    canvas.tag_bind(
 
        obj,
 
        "<Leave>", lambda ev: canvas.itemconfig(obj, **{attribute: normalval}))
 

	
 

	
 
class Field(xmlnodeclass):
 
    
 
    """one light has a field of influence. for any point on the
 
    canvas, you can ask this field how strong it is. """
 

	
 
    def name(self,newval=None):
 
    def name(self, newval=None):
 
        """light/sub name"""
 
        return self._getorsetattr("name",newval)
 
    def center(self,x=None,y=None):
 
        return self._getorsetattr("name", newval)
 

	
 
    def center(self, x=None, y=None):
 
        """x,y float coords for the center of this light in the field. returns
 
        a Pair, although it accepts x,y"""
 
        return Pair(self._getorsettypedattr("x",float,x),
 
                    self._getorsettypedattr("y",float,y))
 
        return Pair(self._getorsettypedattr("x", float, x),
 
                    self._getorsettypedattr("y", float, y))
 

	
 
    def falloff(self,dist=None):
 
        
 
    def falloff(self, dist=None):
 
        """linear falloff from 1 at center, to 0 at dist pixels away
 
        from center"""
 
        return self._getorsettypedattr("falloff",float,dist)
 
        return self._getorsettypedattr("falloff", float, dist)
 

	
 
    def getdistforintensity(self,intens):
 
    def getdistforintensity(self, intens):
 
        """returns the distance you'd have to be for the given intensity (0..1)"""
 
        return (1-intens)*self.falloff()
 
        return (1 - intens) * self.falloff()
 

	
 
    def calc(self,x,y):
 
    def calc(self, x, y):
 
        """returns field strength at point x,y"""
 
        dist=pairdist(Pair(x,y),self.center())
 
        return max(0,(self.falloff()-dist)/self.falloff())
 
        dist = pairdist(Pair(x, y), self.center())
 
        return max(0, (self.falloff() - dist) / self.falloff())
 

	
 

	
 
class Fieldset(collectiveelement):
 
    """group of fields. persistent."""
 
    def childtype(self): return Field
 

	
 
    def childtype(self):
 
        return Field
 

	
 
    def version(self):
 
        """read-only version attribute on fieldset tag"""
 
        return self._getorsetattr("version",None)
 
        return self._getorsetattr("version", None)
 

	
 
    def report(self,x,y):
 
    def report(self, x, y):
 
        """reports active fields and their intensities"""
 
        active=0
 
        active = 0
 
        for f in self.getall():
 
            name=f.name()
 
            intens=f.calc(x,y)
 
            if intens>0:
 
                print name,intens,
 
                active+=1
 
        if active>0:
 
            name = f.name()
 
            intens = f.calc(x, y)
 
            if intens > 0:
 
                print name, intens,
 
                active += 1
 
        if active > 0:
 
            print
 
        self.dmxsend(x,y)
 
    def dmxsend(self,x,y):
 
        self.dmxsend(x, y)
 

	
 
    def dmxsend(self, x, y):
 
        """output lights to dmx"""
 
        levels=dict([(f.name(),f.calc(x,y)) for f in self.getall()])
 
        dmxlist=Submaster(None,levels).get_dmx_list()
 
        levels = dict([(f.name(), f.calc(x, y)) for f in self.getall()])
 
        dmxlist = Submaster(None, levels).get_dmx_list()
 
        dmxclient.outputlevels(dmxlist)
 
        
 
        
 

	
 
    def getbounds(self):
 
        """returns xmin,xmax,ymin,ymax for the non-zero areas of this field"""
 
        r=None
 
        r = None
 
        for f in self.getall():
 
            rad=f.getdistforintensity(0)
 
            fx,fy=f.center()
 
            fieldrect=Rect(fx-rad,fy-rad,rad*2,rad*2)
 
            rad = f.getdistforintensity(0)
 
            fx, fy = f.center()
 
            fieldrect = Rect(fx - rad, fy - rad, rad * 2, rad * 2)
 
            if r is None:
 
                r=fieldrect
 
                r = fieldrect
 
            else:
 
                r=r.union(fieldrect)
 
        return r.left,r.right,r.top,r.bottom
 
                r = r.union(fieldrect)
 
        return r.left, r.right, r.top, r.bottom
 

	
 

	
 
class Fieldsetfile(xmldocfile):
 
    def __init__(self,filename):
 
        self._openornew(filename,topleveltype=Fieldset)
 

	
 
    def __init__(self, filename):
 
        self._openornew(filename, topleveltype=Fieldset)
 

	
 
    def fieldset(self):
 
        return self._gettoplevel()
 

	
 

	
 
########################################################################
 
########################################################################
 

	
 

	
 
class FieldDisplay:
 
    """the view for a Field."""
 
    def __init__(self,canvas,field):
 
        self.canvas=canvas
 
        self.field=field
 
        self.tags=[str(id(self))] # canvas tag to id our objects
 
        
 

	
 
    def __init__(self, canvas, field):
 
        self.canvas = canvas
 
        self.field = field
 
        self.tags = [str(id(self))]  # canvas tag to id our objects
 

	
 
    def setcoords(self):
 
        """adjust canvas obj coords to match the field"""
 
        # this uses the canvas object ids saved by makeobjs
 
        f=self.field
 
        c=self.canvas
 
        w2c=self.canvas.world2canvas
 
        f = self.field
 
        c = self.canvas
 
        w2c = self.canvas.world2canvas
 

	
 
        # rings
 
        for intens,ring in self.rings.items():
 
            rad=f.getdistforintensity(intens)
 
            p1=w2c(*(f.center()-Pair(rad,rad)))
 
            p2=w2c(*(f.center()+Pair(rad,rad)))
 
            c.coords(ring,p1[0],p1[1],p2[0],p2[1])
 
        for intens, ring in self.rings.items():
 
            rad = f.getdistforintensity(intens)
 
            p1 = w2c(*(f.center() - Pair(rad, rad)))
 
            p2 = w2c(*(f.center() + Pair(rad, rad)))
 
            c.coords(ring, p1[0], p1[1], p2[0], p2[1])
 

	
 
        # text
 
        p1=w2c(*f.center())
 
        c.coords(self.txt,*p1)
 
        p1 = w2c(*f.center())
 
        c.coords(self.txt, *p1)
 

	
 
    def makeobjs(self):
 
        """(re)create the canvas objs (null coords) and make their bindings"""
 
        c=self.canvas
 
        f=self.field
 
        c = self.canvas
 
        f = self.field
 
        c.delete(self.tags)
 

	
 
        w2c=self.canvas.world2canvas
 
        w2c = self.canvas.world2canvas
 

	
 
        # make rings
 
        self.rings={} # rad,canvasobj
 
        for intens,color in (#(1,'white'),
 
                             (.8,'gray90'),(.6,'gray80'),(.4,'gray60'),(.2,'gray50'),
 
                             (0,'#000080')):
 
            self.rings[intens]=c.create_oval(0,0,0,0,
 
                                          outline=color,width=2,tags=self.tags,
 
                                          outlinestipple='gray50')
 
        self.rings = {}  # rad,canvasobj
 
        for intens, color in (  #(1,'white'),
 
            (.8, 'gray90'), (.6, 'gray80'), (.4, 'gray60'), (.2, 'gray50'),
 
            (0, '#000080')):
 
            self.rings[intens] = c.create_oval(0,
 
                                               0,
 
                                               0,
 
                                               0,
 
                                               outline=color,
 
                                               width=2,
 
                                               tags=self.tags,
 
                                               outlinestipple='gray50')
 

	
 
        # make text
 
        self.txt=c.create_text(0,0,text=f.name(),font=defaultfont+" bold",
 
                               fill='white',anchor='c',
 
                               tags=self.tags)
 
        self.txt = c.create_text(0,
 
                                 0,
 
                                 text=f.name(),
 
                                 font=defaultfont + " bold",
 
                                 fill='white',
 
                                 anchor='c',
 
                                 tags=self.tags)
 

	
 
        # highlight text bindings
 
        canvashighlighter(c,self.txt,'fill',normalval='white',highlightval='red')
 
        canvashighlighter(c,
 
                          self.txt,
 
                          'fill',
 
                          normalval='white',
 
                          highlightval='red')
 

	
 
        # position drag bindings
 
        def press(ev):
 
            self._lastmouse=ev.x,ev.y
 
            self._lastmouse = ev.x, ev.y
 

	
 
        def motion(ev):
 
            dcan=Pair(*[a-b for a,b in zip((ev.x,ev.y),self._lastmouse)])
 
            dworld=c.canvas2world_vector(*dcan)
 
            self.field.center(*(self.field.center()+dworld))
 
            self._lastmouse=ev.x,ev.y
 
            self.setcoords() # redraw
 
            dcan = Pair(*[a - b for a, b in zip((ev.x, ev.y), self._lastmouse)])
 
            dworld = c.canvas2world_vector(*dcan)
 
            self.field.center(*(self.field.center() + dworld))
 
            self._lastmouse = ev.x, ev.y
 
            self.setcoords()  # redraw
 

	
 
        def release(ev):
 
            if hasattr(self,'_lastmouse'):
 
            if hasattr(self, '_lastmouse'):
 
                del self._lastmouse
 
            dispatcher.send("field coord changed") # updates bounds
 
            
 
        c.tag_bind(self.txt,"<ButtonPress-1>",press)
 
        c.tag_bind(self.txt,"<B1-Motion>",motion)
 
        c.tag_bind(self.txt,"<B1-ButtonRelease>",release)
 
            dispatcher.send("field coord changed")  # updates bounds
 

	
 
        c.tag_bind(self.txt, "<ButtonPress-1>", press)
 
        c.tag_bind(self.txt, "<B1-Motion>", motion)
 
        c.tag_bind(self.txt, "<B1-ButtonRelease>", release)
 

	
 
        # radius drag bindings
 
        outerring=self.rings[0]
 
        canvashighlighter(c,outerring,
 
                          'outline',normalval='#000080',highlightval='#4040ff')
 
        outerring = self.rings[0]
 
        canvashighlighter(c,
 
                          outerring,
 
                          'outline',
 
                          normalval='#000080',
 
                          highlightval='#4040ff')
 

	
 
        def motion(ev):
 
            worldmouse=self.canvas.canvas2world(ev.x,ev.y)
 
            currentdist=pairdist(worldmouse,self.field.center())
 
            worldmouse = self.canvas.canvas2world(ev.x, ev.y)
 
            currentdist = pairdist(worldmouse, self.field.center())
 
            self.field.falloff(currentdist)
 
            self.setcoords()
 
        c.tag_bind(outerring,"<B1-Motion>",motion)
 
        c.tag_bind(outerring,"<B1-ButtonRelease>",release) # from above
 

	
 
        c.tag_bind(outerring, "<B1-Motion>", motion)
 
        c.tag_bind(outerring, "<B1-ButtonRelease>", release)  # from above
 

	
 
        self.setcoords()
 
    
 

	
 

	
 
class Tracker(tk.Frame):
 

	
 
    """whole tracker widget, which is mostly a view for a
 
    Fieldset. tracker makes its own fieldset"""
 

	
 
    # world coords of the visible canvas (preserved even in window resizes)
 
    xmin=0
 
    xmax=100
 
    ymin=0
 
    ymax=100
 
    xmin = 0
 
    xmax = 100
 
    ymin = 0
 
    ymax = 100
 

	
 
    fieldsetfile = None
 
    displays = None  # Field : FieldDisplay. we keep these in sync with the fieldset
 

	
 
    fieldsetfile=None
 
    displays=None # Field : FieldDisplay. we keep these in sync with the fieldset
 
    
 
    def __init__(self,master):
 
        tk.Frame.__init__(self,master)
 
    def __init__(self, master):
 
        tk.Frame.__init__(self, master)
 

	
 
        self.displays={}
 
        
 
        c=self.canvas=Zooming(self,bg='black',closeenough=5)
 
        c.pack(fill='both',exp=1)
 
        self.displays = {}
 

	
 
        c = self.canvas = Zooming(self, bg='black', closeenough=5)
 
        c.pack(fill='both', exp=1)
 

	
 
        # preserve edge coords over window resize
 
        c.bind("<Configure>",self.configcoords)
 
        
 
        c.bind("<Motion>",
 
               lambda ev: self._fieldset().report(*c.canvas2world(ev.x,ev.y)))
 
        c.bind("<Configure>", self.configcoords)
 

	
 
        c.bind("<Motion>", lambda ev: self._fieldset().report(*c.canvas2world(
 
            ev.x, ev.y)))
 

	
 
        def save(ev):
 
            print "saving"
 
            self.fieldsetfile.save()
 
        master.bind("<Key-s>",save)
 
        dispatcher.connect(self.autobounds,"field coord changed")
 

	
 
        master.bind("<Key-s>", save)
 
        dispatcher.connect(self.autobounds, "field coord changed")
 

	
 
    def _fieldset(self):
 
        return self.fieldsetfile.fieldset()
 

	
 
    def load(self,filename):
 
        self.fieldsetfile=Fieldsetfile(filename)
 
    def load(self, filename):
 
        self.fieldsetfile = Fieldsetfile(filename)
 
        self.displays.clear()
 
        for f in self.fieldsetfile.fieldset().getall():
 
            self.displays[f]=FieldDisplay(self.canvas,f)
 
            self.displays[f] = FieldDisplay(self.canvas, f)
 
            self.displays[f].makeobjs()
 
        self.autobounds()
 

	
 
    def configcoords(self,*args):
 
    def configcoords(self, *args):
 
        # force our canvas coords to stay at the edges of the window
 
        c=self.canvas
 
        cornerx,cornery=c.canvas2world(0,0)
 
        c.move(cornerx-self.xmin,
 
               cornery-self.ymin)
 
        c.setscale(0,0,
 
                   c.winfo_width()/(self.xmax-self.xmin),
 
                   c.winfo_height()/(self.ymax-self.ymin))
 
        c = self.canvas
 
        cornerx, cornery = c.canvas2world(0, 0)
 
        c.move(cornerx - self.xmin, cornery - self.ymin)
 
        c.setscale(0, 0,
 
                   c.winfo_width() / (self.xmax - self.xmin),
 
                   c.winfo_height() / (self.ymax - self.ymin))
 

	
 
    def autobounds(self):
 
        """figure out our bounds from the fieldset, and adjust the display zooms.
 
        writes the corner coords onto the canvas."""
 
        self.xmin,self.xmax,self.ymin,self.ymax=self._fieldset().getbounds()
 
        self.xmin, self.xmax, self.ymin, self.ymax = self._fieldset().getbounds(
 
        )
 

	
 
        self.configcoords()
 
        
 
        c=self.canvas
 

	
 
        c = self.canvas
 
        c.delete('cornercoords')
 
        for x,anc2 in ((self.xmin,'w'),(self.xmax,'e')):
 
            for y,anc1 in ((self.ymin,'n'),(self.ymax,'s')):
 
                pos=c.world2canvas(x,y)
 
                c.create_text(pos[0],pos[1],text="%s,%s"%(x,y),
 
                              fill='white',anchor=anc1+anc2,
 
        for x, anc2 in ((self.xmin, 'w'), (self.xmax, 'e')):
 
            for y, anc1 in ((self.ymin, 'n'), (self.ymax, 's')):
 
                pos = c.world2canvas(x, y)
 
                c.create_text(pos[0],
 
                              pos[1],
 
                              text="%s,%s" % (x, y),
 
                              fill='white',
 
                              anchor=anc1 + anc2,
 
                              tags='cornercoords')
 
        [d.setcoords() for d in self.displays.values()]
 

	
 

	
 
########################################################################
 
########################################################################
 
                
 
root=tk.Tk()
 

	
 
root = tk.Tk()
 
root.wm_geometry('700x350')
 
tra=Tracker(root)
 
tra.pack(fill='both',exp=1)
 
tra = Tracker(root)
 
tra.pack(fill='both', exp=1)
 

	
 
tra.load("fieldsets/demo")
 

	
bin/vidref
Show inline comments
 
#!bin/python
 
from run_local import log
 
import sys
 
sys.path.append('/usr/lib/python2.7/dist-packages') # For gtk
 
sys.path.append('/usr/lib/python2.7/dist-packages')  # For gtk
 
from twisted.internet import gtk2reactor
 
gtk2reactor.install()
 
from twisted.internet import reactor, defer
 
@@ -15,19 +15,19 @@ from light9.vidref.main import Gui
 
from light9.vidref.replay import snapshotDir
 
from rdfdb.syncedgraph import SyncedGraph
 

	
 
 # find replay dirs correctly. show multiple
 
 # replays. trash. reorder/pin. dump takes that are too short; they're
 
 # just from seeking
 
# find replay dirs correctly. show multiple
 
# replays. trash. reorder/pin. dump takes that are too short; they're
 
# just from seeking
 

	
 
parser = optparse.OptionParser()
 
parser.add_option("-v", "--verbose", action="store_true",
 
                  help="logging.DEBUG")
 
parser.add_option("-v", "--verbose", action="store_true", help="logging.DEBUG")
 
(options, args) = parser.parse_args()
 

	
 

	
 
log.setLevel(logging.DEBUG if options.verbose else logging.INFO)
 

	
 

	
 
class Snapshot(cyclone.web.RequestHandler):
 

	
 
    @defer.inlineCallbacks
 
    def post(self):
 
        # save next pic
 
@@ -38,7 +38,7 @@ class Snapshot(cyclone.web.RequestHandle
 
            assert outputFilename.startswith(snapshotDir())
 
            out = networking.vidref.path(
 
                "snapshot/%s" % outputFilename[len(snapshotDir()):].lstrip('/'))
 
            
 

	
 
            self.write(json.dumps({'snapshot': out}))
 
            self.set_header("Location", out)
 
            self.set_status(303)
 
@@ -47,11 +47,13 @@ class Snapshot(cyclone.web.RequestHandle
 
            traceback.print_exc()
 
            raise
 

	
 

	
 
class SnapshotPic(cyclone.web.StaticFileHandler):
 
    pass
 

	
 

	
 
class Time(cyclone.web.RequestHandler):
 

	
 
    def put(self):
 
        body = json.loads(self.request.body)
 
        t = body['t']
 
@@ -65,14 +67,21 @@ graph = SyncedGraph(networking.rdfdb.url
 
gui = Gui(graph)
 

	
 
port = networking.vidref.port
 
reactor.listenTCP(port, cyclone.web.Application(handlers=[
 
    (r'/()', cyclone.web.StaticFileHandler,
 
     {'path': 'light9/vidref', 'default_filename': 'vidref.html'}),
 
    (r'/snapshot', Snapshot),
 
    (r'/snapshot/(.*)', SnapshotPic, {"path": snapshotDir()}),
 
    (r'/time', Time),
 
    ], debug=True, gui=gui))
 
reactor.listenTCP(
 
    port,
 
    cyclone.web.Application(handlers=[
 
        (r'/()', cyclone.web.StaticFileHandler, {
 
            'path': 'light9/vidref',
 
            'default_filename': 'vidref.html'
 
        }),
 
        (r'/snapshot', Snapshot),
 
        (r'/snapshot/(.*)', SnapshotPic, {
 
            "path": snapshotDir()
 
        }),
 
        (r'/time', Time),
 
    ],
 
                            debug=True,
 
                            gui=gui))
 
log.info("serving on %s" % port)
 

	
 
reactor.run()
 

	
bin/vidrefsetup
Show inline comments
 
@@ -16,17 +16,23 @@ from light9 import networking, showconfi
 

	
 
from lib.cycloneerr import PrettyErrorHandler
 

	
 

	
 
class RedirToCamera(PrettyErrorHandler, cyclone.web.RequestHandler):
 

	
 
    def get(self):
 
        return self.redirect(networking.picamserve.path(
 
            'pic?' + self.request.query))
 
        
 
        return self.redirect(
 
            networking.picamserve.path('pic?' + self.request.query))
 

	
 

	
 
class UrlToCamera(PrettyErrorHandler, cyclone.web.RequestHandler):
 

	
 
    def get(self):
 
        self.set_header('Content-Type', 'text/plain')
 
        self.write(networking.picamserve.path('pic'))
 
                   
 

	
 

	
 
class VidrefCamRequest(PrettyErrorHandler, cyclone.web.RequestHandler):
 

	
 
    def get(self):
 
        graph = self.settings.graph
 
        show = showconfig.showUri()
 
@@ -35,7 +41,7 @@ class VidrefCamRequest(PrettyErrorHandle
 
            if ret is None:
 
                self.send_error(404)
 
            self.redirect(ret)
 
            
 

	
 
    def put(self):
 
        graph = self.settings.graph
 
        show = showconfig.showUri()
 
@@ -45,9 +51,12 @@ class VidrefCamRequest(PrettyErrorHandle
 
                          newObject=URIRef(self.get_argument('uri')))
 
        self.send_error(202)
 

	
 

	
 
def main():
 
    parser = optparse.OptionParser()
 
    parser.add_option("-v", "--verbose", action="store_true",
 
    parser.add_option("-v",
 
                      "--verbose",
 
                      action="store_true",
 
                      help="logging.DEBUG")
 
    (options, args) = parser.parse_args()
 

	
 
@@ -55,15 +64,23 @@ def main():
 
    graph = SyncedGraph(networking.rdfdb.url, "vidrefsetup")
 

	
 
    # deliberately conflict with vidref since they can't talk at once to cam
 
    port = networking.vidref.port 
 
    port = networking.vidref.port
 

	
 
    reactor.listenTCP(port, cyclone.web.Application(handlers=[
 
        (r'/pic', RedirToCamera),
 
        (r'/picUrl', UrlToCamera),
 
        (r'/vidrefCamRequest', VidrefCamRequest),
 
        (r'/()', cyclone.web.StaticFileHandler, {'path': 'light9/vidref/', 'default_filename': 'vidref.html'}),
 
        ], debug=True, graph=graph))
 
    reactor.listenTCP(
 
        port,
 
        cyclone.web.Application(handlers=[
 
            (r'/pic', RedirToCamera),
 
            (r'/picUrl', UrlToCamera),
 
            (r'/vidrefCamRequest', VidrefCamRequest),
 
            (r'/()', cyclone.web.StaticFileHandler, {
 
                'path': 'light9/vidref/',
 
                'default_filename': 'vidref.html'
 
            }),
 
        ],
 
                                debug=True,
 
                                graph=graph))
 
    log.info("serving on %s" % port)
 
    reactor.run()
 

	
 

	
 
main()
bin/wavecurve
Show inline comments
 
@@ -3,24 +3,30 @@ import optparse
 
import run_local
 
from light9.wavepoints import simp
 

	
 

	
 
def createCurve(inpath, outpath, t):
 
    print "reading %s, writing %s" % (inpath, outpath)
 
    points = simp(inpath.replace('.ogg', '.wav'), seconds_per_average=t)
 

	
 
    f = file(outpath, 'w')
 
    for time_val in points:
 
        print >>f, "%s %s" % time_val
 
        print >> f, "%s %s" % time_val
 

	
 

	
 
parser = optparse.OptionParser(usage="""%prog inputSong.wav outputCurve
 

	
 
You probably just want -a
 

	
 
""")
 
parser.add_option("-t", type="float", default=.01,
 
parser.add_option("-t",
 
                  type="float",
 
                  default=.01,
 
                  help="seconds per sample (default .01, .07 is smooth)")
 
parser.add_option("-a", "--all", action="store_true",
 
parser.add_option("-a",
 
                  "--all",
 
                  action="store_true",
 
                  help="make standard curves for all songs")
 
options,args = parser.parse_args()
 
options, args = parser.parse_args()
 

	
 
if options.all:
 
    from light9 import showconfig
 
@@ -32,8 +38,7 @@ if options.all:
 
    playlist = Playlist.fromShow(showconfig.getGraph(), showconfig.showUri())
 
    for song in playlist.allSongs():
 
        inpath = showconfig.songOnDisk(song)
 
        for curveName, t in [('music', .01),
 
                             ('smooth_music', .07)]:
 
        for curveName, t in [('music', .01), ('smooth_music', .07)]:
 
            outpath = showconfig.curvesDir() + "/%s-%s" % (
 
                showconfig.songFilenameFromURI(song), curveName)
 
            createCurve(inpath, outpath, t)
bin/webcontrol
Show inline comments
 
@@ -21,14 +21,14 @@ from light9 import showconfig, networkin
 
from light9.namespaces import L9
 
from urllib import urlencode
 

	
 

	
 
# move to web lib
 
def post(url, **args):
 
    return getPage(url,
 
                   method='POST',
 
                   postdata=urlencode(args))
 
    return getPage(url, method='POST', postdata=urlencode(args))
 

	
 

	
 
class Commands(object):
 

	
 
    @staticmethod
 
    def playSong(graph, songUri):
 
        s = xmlrpclib.ServerProxy(networking.musicPlayer.url)
 
@@ -45,17 +45,22 @@ class Commands(object):
 
    @staticmethod
 
    def worklightsOn(graph):
 
        return post(networking.keyboardComposer.path('fadesub'),
 
                    subname='scoop', level=.5, secs=.5)
 
                    subname='scoop',
 
                    level=.5,
 
                    secs=.5)
 

	
 
    @staticmethod
 
    def worklightsOff(graph):
 
        return post(networking.keyboardComposer.path('fadesub'),
 
                    subname='scoop', level=0, secs=.5)
 
                    subname='scoop',
 
                    level=0,
 
                    secs=.5)
 

	
 
    @staticmethod
 
    def dimmerSet(graph, dimmer, value):
 
        raise NotImplementedError("subcomposer doesnt have an http port yet")
 

	
 

	
 
class Main(rend.Page):
 
    docFactory = loaders.xmlfile(sibpath(__file__, "../light9/webcontrol.html"))
 

	
 
@@ -75,9 +80,9 @@ class Main(rend.Page):
 
        out = []
 
        for song in songs:
 
            out.append(
 
                T.form(method="post", action="playSong")[
 
                    T.input(type='hidden', name='songUri', value=song),
 
                    T.button(type='submit')[graph.label(song)]])
 
                T.form(method="post", action="playSong")
 
                [T.input(type='hidden', name='songUri', value=song),
 
                 T.button(type='submit')[graph.label(song)]])
 
        return out
 

	
 
    @inlineCallbacks
 
@@ -85,18 +90,19 @@ class Main(rend.Page):
 
        try:
 
            func = getattr(Commands, segments[0])
 
            req = inevow.IRequest(ctx)
 
            simpleArgDict = dict((k, v[0]) for k,v in req.args.items())
 
            simpleArgDict = dict((k, v[0]) for k, v in req.args.items())
 
            try:
 
                ret = yield robust_apply(func, func, self.graph,
 
                                         **simpleArgDict)
 
            except KeyboardInterrupt: raise
 
            except KeyboardInterrupt:
 
                raise
 
            except Exception, e:
 
                print "Error on command %s" % segments[0]
 
                traceback.print_exc()
 
                returnValue((url.here.up().
 
                             add('status', str(e)).
 
                             add('error', 1), segments[1:]))
 
                
 
                returnValue((url.here.up().add('status',
 
                                               str(e)).add('error',
 
                                                           1), segments[1:]))
 

	
 
            returnValue((url.here.up().add('status', ret), segments[1:]))
 
            #actually return the orig page, with a status message from the func
 
        except AttributeError:
 
@@ -105,7 +111,8 @@ class Main(rend.Page):
 

	
 
    def child_icon(self, ctx):
 
        return static.File("/usr/share/pyshared/elisa/plugins/poblesec/tango")
 
            
 

	
 

	
 
graph = showconfig.getGraph()
 
show = showconfig.showUri()
 

	
light9/Effects.py
Show inline comments
 
@@ -11,21 +11,24 @@ from light9.namespaces import L9
 
log = logging.getLogger()
 

	
 
registered = []
 

	
 

	
 
def register(f):
 
    registered.append(f)
 
    return f
 

	
 

	
 
@register
 
class Strip(object):
 
    """list of r,g,b tuples for sending to an LED strip"""
 
    which = 'L' # LR means both. W is the wide one
 
    which = 'L'  # LR means both. W is the wide one
 
    pixels = []
 

	
 
    def __repr__(self):
 
        return '<Strip which=%r px0=%r>' % (self.which, self.pixels[0])
 
    
 

	
 
    @classmethod
 
    def solid(cls, which='L', color=(1,1,1), hsv=None):
 
    def solid(cls, which='L', color=(1, 1, 1), hsv=None):
 
        """hsv overrides color"""
 
        if hsv is not None:
 
            color = colorsys.hsv_to_rgb(hsv[0] % 1.0, hsv[1], hsv[2])
 
@@ -37,24 +40,34 @@ class Strip(object):
 
    def __mul__(self, f):
 
        if not isinstance(f, (int, float)):
 
            raise TypeError
 
            
 

	
 
        s = Strip()
 
        s.which = self.which
 
        s.pixels = [(r*f, g*f, b*f) for r,g,b in self.pixels]
 
        s.pixels = [(r * f, g * f, b * f) for r, g, b in self.pixels]
 
        return s
 

	
 
    __rmul__ = __mul__
 

	
 

	
 
@register
 
class Blacklight(float):
 
    """a level for the blacklight PWM output"""
 

	
 
    def __mul__(self, f):
 
        return Blacklight(float(self) * f)
 

	
 
    __rmul__ = __mul__
 
    
 

	
 

	
 
@register
 
def chase(t, ontime=0.5, offset=0.2, onval=1.0, 
 
          offval=0.0, names=None, combiner=max, random=False):
 
def chase(t,
 
          ontime=0.5,
 
          offset=0.2,
 
          onval=1.0,
 
          offval=0.0,
 
          names=None,
 
          combiner=max,
 
          random=False):
 
    """names is list of URIs. returns a submaster that chases through
 
    the inputs"""
 
    if random:
 
@@ -69,24 +82,27 @@ def chase(t, ontime=0.5, offset=0.2, onv
 
            dmx = Patch.dmx_from_uri(uri)
 
        except KeyError:
 
            log.info(("chase includes %r, which doesn't resolve to a dmx chan" %
 
                   uri))
 
                      uri))
 
            continue
 
        lev[dmx] = value
 

	
 
    return Submaster.Submaster(name="chase" ,levels=lev)
 
    return Submaster.Submaster(name="chase", levels=lev)
 

	
 

	
 
@register
 
def hsv(h, s, v, light='all', centerScale=.5):
 
    r,g,b = colorsys.hsv_to_rgb(h % 1.0, s, v)
 
    r, g, b = colorsys.hsv_to_rgb(h % 1.0, s, v)
 
    lev = {}
 
    if light in ['left', 'all']:
 
        lev[73], lev[74], lev[75] = r,g,b
 
        lev[73], lev[74], lev[75] = r, g, b
 
    if light in ['right', 'all']:
 
        lev[80], lev[81], lev[82] = r,g,b
 
        lev[80], lev[81], lev[82] = r, g, b
 
    if light in ['center', 'all']:
 
        lev[88], lev[89], lev[90] = r*centerScale,g*centerScale,b*centerScale
 
        lev[88], lev[89], lev[
 
            90] = r * centerScale, g * centerScale, b * centerScale
 
    return Submaster.Submaster(name='hsv', levels=lev)
 
    
 

	
 

	
 
@register
 
def stack(t, names=None, fade=0):
 
    """names is list of URIs. returns a submaster that stacks the the inputs
 
@@ -102,19 +118,22 @@ def stack(t, names=None, fade=0):
 
            try:
 
                dmx = Patch.dmx_from_uri(uri)
 
            except KeyError:
 
                log.info(("stack includes %r, which doesn't resolve to a dmx chan"%
 
                       uri))
 
                log.info(
 
                    ("stack includes %r, which doesn't resolve to a dmx chan" %
 
                     uri))
 
                continue
 
            lev[dmx] = 1
 
        else:
 
            break
 
    
 

	
 
    return Submaster.Submaster(name="stack", levels=lev)
 

	
 

	
 
@register
 
def smoove(x):
 
    return -2 * (x ** 3) + 3 * (x ** 2)
 
    
 
    return -2 * (x**3) + 3 * (x**2)
 

	
 

	
 
def configExprGlobals():
 
    graph = showconfig.getGraph()
 
    ret = {}
 
@@ -130,8 +149,10 @@ def configExprGlobals():
 

	
 
    ret['nsin'] = lambda x: (math.sin(x * (2 * math.pi)) + 1) / 2
 
    ret['ncos'] = lambda x: (math.cos(x * (2 * math.pi)) + 1) / 2
 

	
 
    def nsquare(t, on=.5):
 
        return (t % 1.0) < on
 

	
 
    ret['nsquare'] = nsquare
 

	
 
    _smooth_random_items = [random_mod.random() for x in range(100)]
 
@@ -156,7 +177,4 @@ def configExprGlobals():
 
    ret['noise2'] = smooth_random2
 
    ret['notch2'] = notch_random2
 

	
 

	
 

	
 
    
 
    return ret
light9/Fadable.py
Show inline comments
 
@@ -2,6 +2,7 @@
 
from Tix import *
 
import time
 

	
 

	
 
class Fadable:
 
    """Fading mixin: must mix in with a Tk widget (or something that has
 
    'after' at least) This is currently used by VolumeBox and MixerTk.
 
@@ -21,24 +22,28 @@ class Fadable:
 
    raise or lower the volume.  Shift-mouse wheeling will cause a more
 
    precise volume adjustment.  Control-mouse wheeling will cause a
 
    longer fade."""
 
    def __init__(self, var, wheel_step=5, use_fades=1, key_bindings=1,
 

	
 
    def __init__(self,
 
                 var,
 
                 wheel_step=5,
 
                 use_fades=1,
 
                 key_bindings=1,
 
                 mouse_bindings=1):
 
        self.use_fades = use_fades # whether increase and decrease should fade
 
        self.wheel_step = wheel_step # amount that increase and descrease should
 
                                     # change volume (by default)
 
        
 
        self.use_fades = use_fades  # whether increase and decrease should fade
 
        self.wheel_step = wheel_step  # amount that increase and descrease should
 
        # change volume (by default)
 

	
 
        self.fade_start_level = 0
 
        self.fade_end_level = 0
 
        self.fade_start_time = 0
 
        self.fade_length = 1
 
        self.fade_step_time = 10
 
        self.fade_var = var
 
        self.fading = 0 # whether a fade is in progress
 
        self.fading = 0  # whether a fade is in progress
 

	
 
        if key_bindings:
 
            for k in range(1, 10):
 
                self.bind("<Key-%d>" % k,
 
                    lambda evt, k=k: self.fade(k / 10.0))
 
                self.bind("<Key-%d>" % k, lambda evt, k=k: self.fade(k / 10.0))
 
            self.bind("<Key-0>", lambda evt: self.fade(1.0))
 
            self.bind("<grave>", lambda evt: self.fade(0))
 

	
 
@@ -61,7 +66,7 @@ class Fadable:
 
            self.bind('<Control-4>', lambda evt: self.increase(length=1))
 
            self.bind('<Control-5>', lambda evt: self.decrease(length=1))
 

	
 
        self.last_level = None # used for muting
 
        self.last_level = None  # used for muting
 

	
 
    def set_var_rounded(self, value):
 
        """use this instead of just self.fade_var.set(value) so we can
 
@@ -72,26 +77,27 @@ class Fadable:
 
        # variable's display instead of using Label(textvariable=var)
 
        # and format it there.
 
        self.fade_var.set(round(value, 7))
 
        
 

	
 
    def fade(self, value, length=0.5, step_time=10):
 
        """Fade to value in length seconds with steps every step_time
 
        milliseconds"""
 
        if length == 0: # 0 seconds fades happen right away and prevents
 
                        # and prevents us from entering the fade loop,
 
                        # which would cause a divide by zero
 
        if length == 0:  # 0 seconds fades happen right away and prevents
 
            # and prevents us from entering the fade loop,
 
            # which would cause a divide by zero
 
            self.set_var_rounded(value)
 
            self.fading = 0 # we stop all fades
 
        else: # the general case
 
            self.fading = 0  # we stop all fades
 
        else:  # the general case
 
            self.fade_start_time = time.time()
 
            self.fade_length = length
 

	
 
            self.fade_start_level = self.fade_var.get()
 
            self.fade_end_level = value
 
            
 

	
 
            self.fade_step_time = step_time
 
            if not self.fading:
 
                self.fading = 1
 
                self.do_fade()
 

	
 
    def do_fade(self):
 
        """Actually performs the fade for Fadable.fade.  Shouldn't be called
 
        directly."""
 
@@ -106,6 +112,7 @@ class Fadable:
 
            self.after(self.fade_step_time, self.do_fade)
 
        else:
 
            self.fading = 0
 

	
 
    def increase(self, multiplier=1, length=0.3):
 
        """Increases the volume by multiplier * wheel_step.  If use_fades is
 
        true, it do this as a fade over length time."""
 
@@ -116,6 +123,7 @@ class Fadable:
 
            newlevel = self.fade_var.get() + amount
 
        newlevel = min(100, newlevel)
 
        self.set_volume(newlevel, length)
 

	
 
    def decrease(self, multiplier=1, length=0.3):
 
        """Descreases the volume by multiplier * wheel_step.  If use_fades
 
        is true, it do this as a fade over length time."""
 
@@ -126,6 +134,7 @@ class Fadable:
 
            newlevel = self.fade_var.get() - amount
 
        newlevel = max(0, newlevel)
 
        self.set_volume(newlevel, length)
 

	
 
    def set_volume(self, newlevel, length=0.3):
 
        """Sets the volume to newlevel, performing a fade of length if
 
        use_fades is true."""
 
@@ -133,13 +142,14 @@ class Fadable:
 
            self.fade(newlevel, length=length)
 
        else:
 
            self.set_var_rounded(newlevel)
 

	
 
    def toggle_mute(self):
 
        """Toggles whether the volume is being muted."""
 
        if self.last_level is None:
 
            self.last_level = self.fade_var.get()
 
            if self.last_level == 0: # we don't want last_level to be zero,
 
                                     # since it will make us toggle between 0
 
                                     # and 0
 
            if self.last_level == 0:  # we don't want last_level to be zero,
 
                # since it will make us toggle between 0
 
                # and 0
 
                newlevel = 1
 
            else:
 
                newlevel = 0
 
@@ -148,4 +158,3 @@ class Fadable:
 
            self.last_level = None
 

	
 
        self.set_var_rounded(newlevel)
 

	
light9/FlyingFader.py
Show inline comments
 
from Tix import *
 
from time import time,sleep
 
from time import time, sleep
 
from __future__ import division
 

	
 

	
 
class Mass:
 

	
 
    def __init__(self):
 
        self.x=0 # position
 
        self.xgoal=0 # position goal
 
        
 
        self.v=0 # velocity
 
        self.maxspeed = .8 # maximum speed, in position/second
 
        self.maxaccel = 3 # maximum acceleration, in position/second^2
 
        self.eps = .03 # epsilon - numbers within this much are considered the same
 
        self.x = 0  # position
 
        self.xgoal = 0  # position goal
 

	
 
        self._lastupdate=time()
 
        self._stopped=1
 
        self.v = 0  # velocity
 
        self.maxspeed = .8  # maximum speed, in position/second
 
        self.maxaccel = 3  # maximum acceleration, in position/second^2
 
        self.eps = .03  # epsilon - numbers within this much are considered the same
 

	
 
    def equal(self,a,b):
 
        return abs(a-b)<self.eps
 
        self._lastupdate = time()
 
        self._stopped = 1
 

	
 
    def equal(self, a, b):
 
        return abs(a - b) < self.eps
 

	
 
    def stop(self):
 
        self.v=0
 
        self.xgoal=self.x
 
        self._stopped=1
 
        
 
        self.v = 0
 
        self.xgoal = self.x
 
        self._stopped = 1
 

	
 
    def update(self):
 
        t0 = self._lastupdate
 
        tnow = time()
 
        self._lastupdate = tnow
 

	
 
        dt = tnow-t0
 
        dt = tnow - t0
 

	
 
        self.x += self.v*dt
 
        self.x += self.v * dt
 
        # hitting the ends stops the slider
 
        if self.x>1: self.v=max(self.v,0); self.x=1
 
        if self.x<0: self.v=min(self.v,0); self.x=0
 
            
 
        if self.equal(self.x,self.xgoal):
 
            self.x=self.xgoal # clean up value
 
        if self.x > 1:
 
            self.v = max(self.v, 0)
 
            self.x = 1
 
        if self.x < 0:
 
            self.v = min(self.v, 0)
 
            self.x = 0
 

	
 
        if self.equal(self.x, self.xgoal):
 
            self.x = self.xgoal  # clean up value
 
            self.stop()
 
            return
 
        
 
        self._stopped=0
 
        dir = (-1.0,1,0)[self.xgoal>self.x]
 

	
 
        if abs(self.xgoal-self.x) < abs(self.v*5*dt):
 
        self._stopped = 0
 
        dir = (-1.0, 1, 0)[self.xgoal > self.x]
 

	
 
        if abs(self.xgoal - self.x) < abs(self.v * 5 * dt):
 
            # apply the brakes on the last 5 steps
 
            dir *= -.5
 

	
 
        self.v += dir*self.maxaccel*dt # velocity changes with acceleration in the right direction
 
        self.v = min(max(self.v,-self.maxspeed),self.maxspeed) # clamp velocity
 
        self.v += dir * self.maxaccel * dt  # velocity changes with acceleration in the right direction
 
        self.v = min(max(self.v, -self.maxspeed),
 
                     self.maxspeed)  # clamp velocity
 

	
 
        #print "x=%+.03f v=%+.03f a=%+.03f %f" % (self.x,self.v,self.maxaccel,self.xgoal)
 

	
 
    def goto(self,newx):
 
        self.xgoal=newx
 
    def goto(self, newx):
 
        self.xgoal = newx
 

	
 
    def ismoving(self):
 
        return not self._stopped
 

	
 

	
 
class FlyingFader(Frame):
 
    def __init__(self, master, variable, label, fadedur=1.5, font=('Arial', 8), labelwidth=12,
 

	
 
    def __init__(self,
 
                 master,
 
                 variable,
 
                 label,
 
                 fadedur=1.5,
 
                 font=('Arial', 8),
 
                 labelwidth=12,
 
                 **kw):
 
        Frame.__init__(self, master)
 
        self.name = label
 
        self.variable = variable
 

	
 
        self.mass = Mass()
 
        
 
        self.config({'bd':1, 'relief':'raised'})
 
        scaleopts = {'variable' : variable, 'showvalue' : 0, 'from' : 1.0,
 
                     'to' : 0, 'res' : 0.001, 'width' : 20, 'length' : 200, 'orient':'vert'}
 

	
 
        self.config({'bd': 1, 'relief': 'raised'})
 
        scaleopts = {
 
            'variable': variable,
 
            'showvalue': 0,
 
            'from': 1.0,
 
            'to': 0,
 
            'res': 0.001,
 
            'width': 20,
 
            'length': 200,
 
            'orient': 'vert'
 
        }
 
        scaleopts.update(kw)
 
        if scaleopts['orient']=='vert':
 
            side1=TOP
 
            side2=BOTTOM
 
        if scaleopts['orient'] == 'vert':
 
            side1 = TOP
 
            side2 = BOTTOM
 
        else:
 
            side1=RIGHT
 
            side2=LEFT
 
        
 
            side1 = RIGHT
 
            side2 = LEFT
 

	
 
        self.scale = Scale(self, **scaleopts)
 
        self.vlabel = Label(self, text="0.0", width=6, font=font)
 
        self.label = Label(self, text=label, font=font, anchor='w',width=labelwidth) #wraplength=40, )
 
        self.label = Label(self,
 
                           text=label,
 
                           font=font,
 
                           anchor='w',
 
                           width=labelwidth)  #wraplength=40, )
 

	
 
        self.oldtrough = self.scale['troughcolor']
 

	
 
@@ -89,8 +116,8 @@ class FlyingFader(Frame):
 
        self.label.pack(side=side2, expand=0, fill=X)
 

	
 
        for k in range(1, 10):
 
            self.scale.bind("<Key-%d>" % k, 
 
                lambda evt, k=k: self.newfade(k / 10.0, evt))
 
            self.scale.bind(
 
                "<Key-%d>" % k, lambda evt, k=k: self.newfade(k / 10.0, evt))
 

	
 
        self.scale.bind("<Key-0>", lambda evt: self.newfade(1.0, evt))
 
        self.scale.bind("<grave>", lambda evt: self.newfade(0, evt))
 
@@ -100,10 +127,10 @@ class FlyingFader(Frame):
 
        self.scale.bind("<3>", self.mousefade)
 

	
 
        self.trace_ret = self.variable.trace('w', self.updatelabel)
 
        self.bind("<Destroy>",self.ondestroy)
 
        self.bind("<Destroy>", self.ondestroy)
 

	
 
    def ondestroy(self,*ev):
 
        self.variable.trace_vdelete('w',self.trace_ret)
 
    def ondestroy(self, *ev):
 
        self.variable.trace_vdelete('w', self.trace_ret)
 

	
 
    def cancelfade(self, evt):
 
        self.fadegoal = self.variable.get()
 
@@ -116,16 +143,15 @@ class FlyingFader(Frame):
 
        self.newfade(target, evt)
 

	
 
    def ismoving(self):
 
        return self.fadevel!=0 or self.fadeacc!=0
 
        return self.fadevel != 0 or self.fadeacc != 0
 

	
 
    def newfade(self, newlevel, evt=None, length=None):
 

	
 
        # these are currently unused-- Mass needs to accept a speed input
 
        mult = 1
 
        if evt.state & 8 and evt.state & 4: mult = 0.25 # both
 
        elif evt.state & 8: mult = 0.5 # alt
 
        elif evt.state & 4: mult = 2   # control
 

	
 
        if evt.state & 8 and evt.state & 4: mult = 0.25  # both
 
        elif evt.state & 8: mult = 0.5  # alt
 
        elif evt.state & 4: mult = 2  # control
 

	
 
        self.mass.x = self.variable.get()
 
        self.mass.goto(newlevel)
 
@@ -139,9 +165,9 @@ class FlyingFader(Frame):
 
        if not self.mass.ismoving():
 
            self.scale['troughcolor'] = self.oldtrough
 
            return
 
        
 

	
 
        # blink the trough while the thing's moving
 
        if time()%.4>.2:
 
        if time() % .4 > .2:
 
            # self.scale.config(troughcolor=self.oldtrough)
 
            self.scale.config(troughcolor='orange')
 
        else:
 
@@ -154,6 +180,8 @@ class FlyingFader(Frame):
 
    def updatelabel(self, *args):
 
        if self.variable:
 
            self.vlabel['text'] = "%.3f" % self.variable.get()
 

	
 

	
 
#        if self.fadetimes[1] == 0: # no fade
 
#            self.vlabel['fg'] = 'black'
 
#        elif self.curfade[1] > self.curfade[0]:
 
@@ -167,25 +195,32 @@ class FlyingFader(Frame):
 
    def set(self, val):
 
        self.scale.set(val)
 

	
 

	
 
def colorfade(scale, lev):
 
    low = (255, 255, 255)
 
    high = (0, 0, 0)
 
    out = [int(l+lev*(h-l)) for h, l in zip(high,low)]
 
    col="#%02X%02X%02X" % tuple(out)
 
    out = [int(l + lev * (h - l)) for h, l in zip(high, low)]
 
    col = "#%02X%02X%02X" % tuple(out)
 
    scale.config(troughcolor=col)
 

	
 

	
 
if __name__ == '__main__':
 
    root = Tk()
 
    root.tk_focusFollowsMouse()
 

	
 
    FlyingFader(root, variable=DoubleVar(), label="suck").pack(side=LEFT, 
 
        expand=1, fill=BOTH)
 
    FlyingFader(root, variable=DoubleVar(), label="suck").pack(side=LEFT,
 
                                                               expand=1,
 
                                                               fill=BOTH)
 
    FlyingFader(root, variable=DoubleVar(), label="moof").pack(side=LEFT,
 
        expand=1, fill=BOTH)
 
                                                               expand=1,
 
                                                               fill=BOTH)
 
    FlyingFader(root, variable=DoubleVar(), label="zarf").pack(side=LEFT,
 
        expand=1, fill=BOTH)
 
    FlyingFader(root, variable=DoubleVar(), 
 
        label="long name goes here.  got it?").pack(side=LEFT, expand=1, 
 
        fill=BOTH)
 
                                                               expand=1,
 
                                                               fill=BOTH)
 
    FlyingFader(root,
 
                variable=DoubleVar(),
 
                label="long name goes here.  got it?").pack(side=LEFT,
 
                                                            expand=1,
 
                                                            fill=BOTH)
 

	
 
    root.mainloop()
light9/Patch.py
Show inline comments
 
@@ -8,12 +8,14 @@ def resolve_name(channelname):
 
    "Ensure that we're talking about the primary name of the light."
 
    return get_channel_name(get_dmx_channel(channelname))
 

	
 

	
 
def get_all_channels():
 
    """returns primary names for all channels (sorted)"""
 
    prinames = reverse_patch.values()[:]
 
    prinames.sort()
 
    return prinames
 

	
 

	
 
def get_dmx_channel(name):
 
    if str(name) in patch:
 
        return patch[str(name)]
 
@@ -24,6 +26,7 @@ def get_dmx_channel(name):
 
    except ValueError:
 
        raise ValueError("Invalid channel name: %r" % name)
 

	
 

	
 
def get_channel_name(dmxnum):
 
    """if you pass a name, it will get normalized"""
 
    try:
 
@@ -31,12 +34,15 @@ def get_channel_name(dmxnum):
 
    except KeyError:
 
        return str(dmxnum)
 

	
 

	
 
def get_channel_uri(name):
 
    return uri_map[name]
 

	
 

	
 
def dmx_from_uri(uri):
 
    return uri_patch[uri]
 

	
 

	
 
def reload_data():
 
    global patch, reverse_patch, uri_map, uri_patch
 
    patch = {}
 
@@ -67,6 +73,6 @@ def reload_data():
 
                    else:
 
                        reverse_patch[name] = norm_name
 

	
 

	
 
# importing patch will load initial data
 
reload_data()
 

	
light9/ascoltami/player.py
Show inline comments
 
#!/usr/bin/python
 

	
 
"""
 
alternate to the mpd music player, for ascoltami
 
"""
 
@@ -8,10 +7,11 @@ import time, logging, traceback
 
from gi.repository import GObject, Gst
 
from twisted.internet import reactor, task
 

	
 

	
 
log = logging.getLogger()
 

	
 

	
 
class Player(object):
 

	
 
    def __init__(self, autoStopOffset=4, onEOS=None):
 
        """autoStopOffset is the number of seconds before the end of
 
        song before automatically stopping (which is really pausing).
 
@@ -20,27 +20,27 @@ class Player(object):
 
        It is called with one argument which is the URI of the song that
 
        just finished."""
 
        self.autoStopOffset = autoStopOffset
 
        self.playbin = self.pipeline = Gst.ElementFactory.make('playbin',None)
 
        self.playbin = self.pipeline = Gst.ElementFactory.make('playbin', None)
 

	
 
        self.playStartTime = 0
 
        self.lastWatchTime = 0
 
        self.autoStopTime = 0
 
        self.lastSetSongUri = None
 
        self.onEOS = onEOS
 
        
 

	
 
        task.LoopingCall(self.watchTime).start(.050)
 

	
 
        bus = self.pipeline.get_bus()
 
        # not working- see notes in pollForMessages
 
        #self.watchForMessages(bus)
 
      
 

	
 
    def watchTime(self):
 
        try:
 
            self.pollForMessages()
 
            
 

	
 
            t = self.currentTime()
 
            log.debug("watch %s < %s < %s",
 
                      self.lastWatchTime, self.autoStopTime, t)
 
            log.debug("watch %s < %s < %s", self.lastWatchTime,
 
                      self.autoStopTime, t)
 
            if self.lastWatchTime < self.autoStopTime < t:
 
                log.info("autostop")
 
                self.pause()
 
@@ -58,6 +58,7 @@ class Player(object):
 
            print "onEos", args
 
            if self.onEOS is not None:
 
                self.onEOS(self.getSong())
 

	
 
        bus.connect('message::eos', onEos)
 

	
 
        def onStreamStatus(bus, message):
 
@@ -65,14 +66,16 @@ class Player(object):
 
            (statusType, _elem) = message.parse_stream_status()
 
            if statusType == Gst.StreamStatusType.ENTER:
 
                self.setupAutostop()
 

	
 
        bus.connect('message::stream-status', onStreamStatus)
 
            
 

	
 
    def pollForMessages(self):
 
        """bus.add_signal_watch seems to be having no effect, but this works"""
 
        bus = self.pipeline.get_bus()
 
        mt = Gst.MessageType
 
        msg = bus.poll(mt.EOS | mt.STREAM_STATUS | mt.ERROR,# | mt.ANY,
 
                       0)
 
        msg = bus.poll(
 
            mt.EOS | mt.STREAM_STATUS | mt.ERROR,  # | mt.ANY,
 
            0)
 
        if msg is not None:
 
            log.debug("bus message: %r %r", msg.src, msg.type)
 
            # i'm trying to catch here a case where the pulseaudio
 
@@ -88,7 +91,7 @@ class Player(object):
 
                (statusType, _elem) = msg.parse_stream_status()
 
                if statusType == Gst.StreamStatusType.ENTER:
 
                    self.setupAutostop()
 
            
 

	
 
    def seek(self, t):
 
        isSeekable = self.playbin.seek_simple(
 
            Gst.Format.TIME,
 
@@ -149,9 +152,15 @@ class Player(object):
 
        """json-friendly object describing the interesting states of
 
        the player nodes"""
 
        success, state, pending = self.playbin.get_state(timeout=0)
 
        return {"current": {"name":state.value_nick},
 
                "pending": {"name":state.value_nick}}
 
        
 
        return {
 
            "current": {
 
                "name": state.value_nick
 
            },
 
            "pending": {
 
                "name": state.value_nick
 
            }
 
        }
 

	
 
    def pause(self):
 
        self.pipeline.set_state(Gst.State.PAUSED)
 

	
 
@@ -161,7 +170,8 @@ class Player(object):
 
        """
 
        pos = self.currentTime()
 
        autoStop = self.duration() - self.autoStopOffset
 
        return not self.isPlaying() and abs(pos - autoStop) < 1 # i've seen .4 difference here
 
        return not self.isPlaying() and abs(
 
            pos - autoStop) < 1  # i've seen .4 difference here
 

	
 
    def resume(self):
 
        self.pipeline.set_state(Gst.State.PLAYING)
light9/ascoltami/playlist.py
Show inline comments
 
from light9.showconfig import songOnDisk
 
from light9.namespaces import L9
 

	
 

	
 
class NoSuchSong(ValueError):
 
    """Raised when a song is requested that doesn't exist (e.g. one
 
    after the last song in the playlist)."""
 

	
 

	
 
class Playlist(object):
 

	
 
    def __init__(self, graph, playlistUri):
 
        self.graph = graph
 
        self.songs = list(graph.items(playlistUri))
 
        
 

	
 
    def nextSong(self, currentSong):
 
        """Returns the next song in the playlist or raises NoSuchSong if 
 
        we are at the end of the playlist."""
 
@@ -30,14 +33,14 @@ class Playlist(object):
 
    def allSongs(self):
 
        """Returns a list of all song URIs in order."""
 
        return self.songs
 
    
 

	
 
    def allSongPaths(self):
 
        """Returns a list of the filesystem paths to all songs in order."""
 
        paths = []
 
        for song in self.songs:
 
            paths.append(songOnDisk(song))
 
        return paths
 
    
 

	
 
    def songPath(self, uri):
 
        """filesystem path to a song"""
 
        raise NotImplementedError("see showconfig.songOnDisk")
light9/ascoltami/webapp.py
Show inline comments
 
@@ -9,32 +9,40 @@ render = render_genshi([sibpath(__file__
 

	
 
from lib.cycloneerr import PrettyErrorHandler
 

	
 
_songUris = {} # locationUri : song
 
_songUris = {}  # locationUri : song
 

	
 

	
 
def songLocation(graph, songUri):
 
    loc = URIRef("file://%s" % songOnDisk(songUri))
 
    _songUris[loc] = songUri
 
    return loc
 
    
 

	
 

	
 
def songUri(graph, locationUri):
 
    return _songUris[locationUri]
 

	
 

	
 
class root(PrettyErrorHandler, cyclone.web.RequestHandler):
 

	
 
    def get(self):
 
        self.set_header("Content-Type", "application/xhtml+xml")
 
        # todo: use a template; embed the show name and the intro/post
 
        # times into the page
 
        self.write(render.index(host=socket.gethostname()))
 

	
 

	
 
def playerSongUri(graph, player):
 
    """or None"""
 
    
 

	
 
    playingLocation = player.getSong()
 
    if playingLocation:
 
        return songUri(graph, URIRef(playingLocation))
 
    else:
 
        return None
 

	
 
class timeResource(PrettyErrorHandler,cyclone.web.RequestHandler):
 

	
 
class timeResource(PrettyErrorHandler, cyclone.web.RequestHandler):
 

	
 
    def get(self):
 
        player = self.settings.app.player
 
        graph = self.settings.app.graph
 
@@ -47,14 +55,15 @@ class timeResource(PrettyErrorHandler,cy
 
        else:
 
            nextAction = 'play'
 

	
 
        self.write(json.dumps({
 
            "song" : playerSongUri(graph, player),
 
            "started" : player.playStartTime,
 
            "duration" : player.duration(),
 
            "playing" : player.isPlaying(),
 
            "t" : player.currentTime(),
 
            "state" : player.states(),
 
            "next" : nextAction,
 
        self.write(
 
            json.dumps({
 
                "song": playerSongUri(graph, player),
 
                "started": player.playStartTime,
 
                "duration": player.duration(),
 
                "playing": player.isPlaying(),
 
                "t": player.currentTime(),
 
                "state": player.states(),
 
                "next": nextAction,
 
            }))
 

	
 
    def post(self):
 
@@ -74,28 +83,39 @@ class timeResource(PrettyErrorHandler,cy
 
        self.set_header("Content-Type", "text/plain")
 
        self.write("ok")
 

	
 

	
 
class songs(PrettyErrorHandler, cyclone.web.RequestHandler):
 

	
 
    def get(self):
 
        graph = self.settings.app.graph
 

	
 
        songs = getSongsFromShow(graph, self.settings.app.show)
 

	
 
        self.set_header("Content-Type", "application/json")
 
        self.write(json.dumps({"songs" : [
 
            {"uri" : s,
 
             "path" : graph.value(s, L9['showPath']),
 
             "label" : graph.label(s)} for s in songs]}))
 
        self.write(
 
            json.dumps({
 
                "songs": [{
 
                    "uri": s,
 
                    "path": graph.value(s, L9['showPath']),
 
                    "label": graph.label(s)
 
                } for s in songs]
 
            }))
 

	
 

	
 
class songResource(PrettyErrorHandler, cyclone.web.RequestHandler):
 

	
 
    def post(self):
 
        """post a uri of song to switch to (and start playing)"""
 
        graph = self.settings.app.graph
 

	
 
        self.settings.app.player.setSong(songLocation(graph, URIRef(self.request.body)))
 
        self.settings.app.player.setSong(
 
            songLocation(graph, URIRef(self.request.body)))
 
        self.set_header("Content-Type", "text/plain")
 
        self.write("ok")
 
    
 

	
 

	
 
class seekPlayOrPause(PrettyErrorHandler, cyclone.web.RequestHandler):
 

	
 
    def post(self):
 
        player = self.settings.app.player
 

	
 
@@ -106,12 +126,16 @@ class seekPlayOrPause(PrettyErrorHandler
 
            player.seek(data['t'])
 
            player.resume()
 

	
 

	
 
class output(PrettyErrorHandler, cyclone.web.RequestHandler):
 

	
 
    def post(self):
 
        d = json.loads(self.request.body)
 
        subprocess.check_call(["bin/movesinks", str(d['sink'])])
 

	
 

	
 
class goButton(PrettyErrorHandler, cyclone.web.RequestHandler):
 

	
 
    def post(self):
 
        """
 
        if music is playing, this silently does nothing.
 
@@ -124,10 +148,11 @@ class goButton(PrettyErrorHandler, cyclo
 
            pass
 
        else:
 
            player.resume()
 
            
 

	
 
        self.set_header("Content-Type", "text/plain")
 
        self.write("ok")
 

	
 

	
 
def makeWebApp(app):
 
    return cyclone.web.Application(handlers=[
 
        (r"/", root),
 
@@ -137,5 +162,5 @@ def makeWebApp(app):
 
        (r"/seekPlayOrPause", seekPlayOrPause),
 
        (r"/output", output),
 
        (r"/go", goButton),
 
        ], app=app)
 

	
 
    ],
 
                                   app=app)
light9/chase.py
Show inline comments
 
from __future__ import division
 

	
 
def chase(t, ontime=0.5, offset=0.2, onval=1.0, 
 
          offval=0.0, names=None, combiner=max):
 

	
 
def chase(t,
 
          ontime=0.5,
 
          offset=0.2,
 
          onval=1.0,
 
          offval=0.0,
 
          names=None,
 
          combiner=max):
 
    names = names or []
 
    # maybe this is better:
 
    # period = ontime + ((offset + ontime) * (len(names) - 1))
 
@@ -26,11 +32,16 @@ def chase(t, ontime=0.5, offset=0.2, onv
 
            outputs[name] = value
 
    return outputs
 

	
 

	
 
if __name__ == "__main__":
 
    # a little testing
 
    for x in range(80):
 
        x /= 20.0
 
        output = chase(x, onval='x', offval=' ', ontime=0.1, offset=0.2,
 
        output = chase(x,
 
                       onval='x',
 
                       offval=' ',
 
                       ontime=0.1,
 
                       offset=0.2,
 
                       names=('a', 'b', 'c', 'd'))
 
        output = output.items()
 
        output.sort()
light9/clientsession.py
Show inline comments
 
@@ -6,12 +6,15 @@ from rdflib import URIRef
 
from urllib import quote
 
from light9 import showconfig
 

	
 

	
 
def add_option(parser):
 
    parser.add_option(
 
        '-s', '--session',
 
        '-s',
 
        '--session',
 
        help="name of session used for levels and window position",
 
        default='default')
 

	
 

	
 
def getUri(appName, opts):
 
    return URIRef("%s/sessions/%s/%s" % (showconfig.showUri(), appName,
 
                                         quote(opts.session, safe='')))
 
    return URIRef("%s/sessions/%s/%s" %
 
                  (showconfig.showUri(), appName, quote(opts.session, safe='')))
light9/collector/collector.py
Show inline comments
 
@@ -16,6 +16,7 @@ ClientSessionType = TypeVar('ClientSessi
 

	
 
log = logging.getLogger('collector')
 

	
 

	
 
def outputMap(graph, outputs):
 
    # type: (Graph, List[Output]) -> Dict[Tuple[URIRef, URIRef], Tuple[Output, int]]
 
    """From rdf config graph, compute a map of
 
@@ -46,8 +47,10 @@ def outputMap(graph, outputs):
 
                ret[(dev, outputAttr)] = (output, index)
 
                log.debug('    map %s to %s,%s', outputAttr, output, index)
 
    return ret
 
        
 

	
 

	
 
class Collector(Generic[ClientType, ClientSessionType]):
 

	
 
    def __init__(self, graph, outputs, listeners=None, clientTimeoutSec=10):
 
        # type: (Graph, List[Output], List[Listener], float) -> None
 
        self.graph = graph
 
@@ -60,15 +63,17 @@ class Collector(Generic[ClientType, Clie
 
        self.graph.addHandler(self.rebuildOutputMap)
 

	
 
        # client : (session, time, {(dev,devattr): latestValue})
 
        self.lastRequest = {} # type: Dict[Tuple[ClientType, ClientSessionType], Tuple[float, Dict[Tuple[URIRef, URIRef], float]]]
 
        self.lastRequest = {
 
        }  # type: Dict[Tuple[ClientType, ClientSessionType], Tuple[float, Dict[Tuple[URIRef, URIRef], float]]]
 

	
 
        # (dev, devAttr): value to use instead of 0
 
        self.stickyAttrs = {} # type: Dict[Tuple[URIRef, URIRef], float] 
 
        self.stickyAttrs = {}  # type: Dict[Tuple[URIRef, URIRef], float]
 

	
 
    def rebuildOutputMap(self):
 
        self.outputMap = outputMap(self.graph, self.outputs) # (device, outputattr) : (output, index)
 
        self.deviceType = {} # uri: type that's a subclass of Device
 
        self.remapOut = {} # (device, deviceAttr) : (start, end)
 
        self.outputMap = outputMap(
 
            self.graph, self.outputs)  # (device, outputattr) : (output, index)
 
        self.deviceType = {}  # uri: type that's a subclass of Device
 
        self.remapOut = {}  # (device, deviceAttr) : (start, end)
 
        for dc in self.graph.subjects(RDF.type, L9['DeviceClass']):
 
            for dev in self.graph.subjects(RDF.type, dc):
 
                self.allDevices.add(dev)
 
@@ -93,7 +98,7 @@ class Collector(Generic[ClientType, Clie
 
    # todo: move to settings.py
 
    def resolvedSettingsDict(self, settingsList):
 
        # type: (List[Tuple[URIRef, URIRef, float]]) -> Dict[Tuple[URIRef, URIRef], float]
 
        out = {} # type: Dict[Tuple[URIRef, URIRef], float]
 
        out = {}  # type: Dict[Tuple[URIRef, URIRef], float]
 
        for d, da, v in settingsList:
 
            if (d, da) in out:
 
                out[(d, da)] = resolve(d, da, [out[(d, da)], v])
 
@@ -103,13 +108,15 @@ class Collector(Generic[ClientType, Clie
 

	
 
    def _warnOnLateRequests(self, client, now, sendTime):
 
        requestLag = now - sendTime
 
        if requestLag > .1 and now > self.initTime + 10 and getattr(self, '_lastWarnTime', 0) < now - 3:
 
        if requestLag > .1 and now > self.initTime + 10 and getattr(
 
                self, '_lastWarnTime', 0) < now - 3:
 
            self._lastWarnTime = now
 
            log.warn('collector.setAttrs from %s is running %.1fms after the request was made',
 
                     client, requestLag * 1000)
 
            log.warn(
 
                'collector.setAttrs from %s is running %.1fms after the request was made',
 
                client, requestLag * 1000)
 

	
 
    def _merge(self, lastRequests):
 
        deviceAttrs = {} # device: {deviceAttr: value}       
 
        deviceAttrs = {}  # device: {deviceAttr: value}
 
        for _, lastSettings in lastRequests:
 
            for (device, deviceAttr), value in lastSettings.iteritems():
 
                if (device, deviceAttr) in self.remapOut:
 
@@ -118,7 +125,8 @@ class Collector(Generic[ClientType, Clie
 

	
 
                attrs = deviceAttrs.setdefault(device, {})
 
                if deviceAttr in attrs:
 
                    value = resolve(device, deviceAttr, [attrs[deviceAttr], value])
 
                    value = resolve(device, deviceAttr,
 
                                    [attrs[deviceAttr], value])
 
                attrs[deviceAttr] = value
 
                # list should come from the graph. these are attrs
 
                # that should default to holding the last position,
 
@@ -131,7 +139,7 @@ class Collector(Generic[ClientType, Clie
 
            daDict = deviceAttrs.setdefault(d, {})
 
            if da not in daDict:
 
                daDict[da] = v
 
                    
 

	
 
        return deviceAttrs
 

	
 
    def setAttrs(self, client, clientSession, settings, sendTime):
 
@@ -156,8 +164,8 @@ class Collector(Generic[ClientType, Clie
 
        self.lastRequest[(client, clientSession)] = (now, uniqueSettings)
 

	
 
        deviceAttrs = self._merge(self.lastRequest.itervalues())
 
        
 
        outputAttrs = {} # device: {outputAttr: value}
 

	
 
        outputAttrs = {}  # device: {outputAttr: value}
 
        for d in self.allDevices:
 
            try:
 
                devType = self.deviceType[d]
 
@@ -167,11 +175,12 @@ class Collector(Generic[ClientType, Clie
 
            try:
 
                outputAttrs[d] = toOutputAttrs(devType, deviceAttrs.get(d, {}))
 
                if self.listeners:
 
                    self.listeners.outputAttrsSet(d, outputAttrs[d], self.outputMap)
 
                    self.listeners.outputAttrsSet(d, outputAttrs[d],
 
                                                  self.outputMap)
 
            except Exception as e:
 
                log.error('failing toOutputAttrs on %s: %r', d, e)
 
        
 
        pendingOut = {} # output : values
 

	
 
        pendingOut = {}  # output : values
 
        for out in self.outputs:
 
            pendingOut[out] = [0] * out.numChannels
 

	
 
@@ -183,9 +192,10 @@ class Collector(Generic[ClientType, Clie
 
        self.flush(pendingOut)
 
        dt2 = 1000 * (time.time() - now)
 
        if dt1 > 30:
 
            log.warn("slow setAttrs: %.1fms -> flush -> %.1fms. lr %s da %s oa %s" % (
 
                dt1, dt2, len(self.lastRequest), len(deviceAttrs), len(outputAttrs)
 
            ))
 
            log.warn(
 
                "slow setAttrs: %.1fms -> flush -> %.1fms. lr %s da %s oa %s" %
 
                (dt1, dt2, len(
 
                    self.lastRequest), len(deviceAttrs), len(outputAttrs)))
 

	
 
    def setAttr(self, device, outputAttr, value, pendingOut):
 
        output, index = self.outputMap[(device, outputAttr)]
light9/collector/collector_client.py
Show inline comments
 
@@ -3,38 +3,43 @@ from light9 import networking
 
from light9.effect.settings import DeviceSettings
 
from twisted.internet import defer
 
from txzmq import ZmqEndpoint, ZmqFactory, ZmqPushConnection
 
import json, time,logging
 
import json, time, logging
 
import treq
 

	
 

	
 
log = logging.getLogger('coll_client')
 

	
 
_zmqClient=None
 
_zmqClient = None
 

	
 

	
 
class TwistedZmqClient(object):
 

	
 
    def __init__(self, service):
 
        zf = ZmqFactory()
 
        e = ZmqEndpoint('connect', 'tcp://%s:%s' % (service.host, service.port))
 
        self.conn = ZmqPushConnection(zf, e)
 
        
 

	
 
    def send(self, msg):
 
        self.conn.push(msg)
 

	
 

	
 
def toCollectorJson(client, session, settings):
 
    assert isinstance(settings, DeviceSettings)
 
    return json.dumps({'settings': settings.asList(),
 
                       'client': client,
 
                       'clientSession': session,
 
                       'sendTime': time.time(),
 
                  })
 
        
 
    return json.dumps({
 
        'settings': settings.asList(),
 
        'client': client,
 
        'clientSession': session,
 
        'sendTime': time.time(),
 
    })
 

	
 

	
 
def sendToCollectorZmq(msg):
 
    global _zmqClient
 
    if _zmqClient is None:
 
        _zmqClient = TwistedZmqClient(networking.collectorZmq)
 
    _zmqClient.send(msg)
 
    return defer.succeed(0)
 
        
 

	
 

	
 
def sendToCollector(client, session, settings, useZmq=False):
 
    """deferred to the time in seconds it took to get a response from collector"""
 
    sendTime = time.time()
 
@@ -44,14 +49,17 @@ def sendToCollector(client, session, set
 
        d = sendToCollectorZmq(msg)
 
    else:
 
        d = treq.put(networking.collector.path('attrs'), data=msg, timeout=1)
 
    
 

	
 
    def onDone(result):
 
        dt = time.time() - sendTime
 
        if dt > .1:
 
            log.warn('sendToCollector request took %.1fms', dt * 1000)
 
        return dt
 

	
 
    d.addCallback(onDone)
 

	
 
    def onErr(err):
 
        log.warn('sendToCollector failed: %r', err)
 

	
 
    d.addErrback(onErr)
 
    return d
light9/collector/collector_test.py
Show inline comments
 
@@ -35,9 +35,11 @@ THEATER = '''
 

	
 
'''
 

	
 
t0 = 0 # time
 
t0 = 0  # time
 

	
 

	
 
class MockOutput(object):
 

	
 
    def __init__(self, uri, connections):
 
        self.connections = connections
 
        self.updates = []
 
@@ -53,31 +55,41 @@ class MockOutput(object):
 
    def flush(self):
 
        self.updates.append('flush')
 

	
 
@unittest.skip("outputMap got rewritten and mostly doesn't raise on these cases")
 

	
 
@unittest.skip("outputMap got rewritten and mostly doesn't raise on these cases"
 
              )
 
class TestOutputMap(unittest.TestCase):
 

	
 
    def testWorking(self):
 
        out0 = MockOutput(UDMX, [(0, DMX0['c1'])])
 
        m = outputMap(MockSyncedGraph(PREFIX + '''
 
        m = outputMap(
 
            MockSyncedGraph(PREFIX + '''
 
          dmx0:c1 :connectedTo dev:inst1Brightness .
 
          dev:inst1 a :Device; :brightness dev:inst1Brightness .
 
        '''), [out0])
 
        self.assertEqual({(DEV['inst1'], L9['brightness']): (out0, 0)}, m)
 
        
 

	
 
    def testMissingOutput(self):
 
        out0 = MockOutput(UDMX, [(0, DMX0['c1'])])
 
        self.assertRaises(KeyError, outputMap, MockSyncedGraph(PREFIX + '''
 
        self.assertRaises(
 
            KeyError, outputMap,
 
            MockSyncedGraph(PREFIX + '''
 
          dev:inst1 a :Device; :brightness dev:inst1Brightness .
 
        '''), [out0])
 

	
 
    def testMissingOutputConnection(self):
 
        out0 = MockOutput(UDMX, [(0, DMX0['c1'])])
 
        self.assertRaises(ValueError, outputMap, MockSyncedGraph(PREFIX + '''
 
        self.assertRaises(
 
            ValueError, outputMap,
 
            MockSyncedGraph(PREFIX + '''
 
          dev:inst1 a :Device; :brightness dev:inst1Brightness .
 
        '''), [out0])
 

	
 
    def testMultipleOutputConnections(self):
 
        out0 = MockOutput(UDMX, [(0, DMX0['c1'])])
 
        self.assertRaises(ValueError, outputMap, MockSyncedGraph(PREFIX + '''
 
        self.assertRaises(
 
            ValueError, outputMap,
 
            MockSyncedGraph(PREFIX + '''
 
          dmx0:c1 :connectedTo dev:inst1Brightness .
 
          dmx0:c2 :connectedTo dev:inst1Brightness .
 
          dev:inst1 a :Device; :brightness dev:inst1Brightness .
 
@@ -85,6 +97,7 @@ class TestOutputMap(unittest.TestCase):
 

	
 

	
 
class TestCollector(unittest.TestCase):
 

	
 
    def setUp(self):
 
        self.config = MockSyncedGraph(PREFIX + THEATER + '''
 

	
 
@@ -101,10 +114,8 @@ class TestCollector(unittest.TestCase):
 
        ''')
 

	
 
        self.dmx0 = MockOutput(DMX0[None], [(0, DMX0['c1'])])
 
        self.udmx = MockOutput(UDMX[None], [(0, UDMX['c1']),
 
                                      (1, UDMX['c2']),
 
                                      (2, UDMX['c3']),
 
                                      (3, UDMX['c4'])])
 
        self.udmx = MockOutput(UDMX[None], [(0, UDMX['c1']), (1, UDMX['c2']),
 
                                            (2, UDMX['c3']), (3, UDMX['c4'])])
 

	
 
    def testRoutesColorOutput(self):
 
        c = Collector(self.config, outputs=[self.dmx0, self.udmx])
 
@@ -123,9 +134,9 @@ class TestCollector(unittest.TestCase):
 
        c.setAttrs('client2', 'sess1',
 
                   [(DEV['colorStrip'], L9['color'], '#333333')], t0)
 

	
 
        self.assertEqual([[215, 255, 0, 0], 'flush',
 
                          [215, 255, 51, 51], 'flush'],
 
                         self.udmx.updates)
 
        self.assertEqual(
 
            [[215, 255, 0, 0], 'flush', [215, 255, 51, 51], 'flush'],
 
            self.udmx.updates)
 
        self.assertEqual([[0, 0, 0, 0], 'flush', [0, 0, 0, 0], 'flush'],
 
                         self.dmx0.updates)
 

	
 
@@ -139,36 +150,36 @@ class TestCollector(unittest.TestCase):
 
        c.setAttrs('client1', 'sess1',
 
                   [(DEV['colorStrip'], L9['color'], '#050000')], t0)
 

	
 
        self.assertEqual([[215, 8, 0, 0], 'flush',
 
                          [215, 8, 0, 0], 'flush',
 
                          [215, 6, 0, 0], 'flush'],
 
                         self.udmx.updates)
 
        self.assertEqual([[0, 0, 0, 0], 'flush',
 
                          [0, 0, 0, 0], 'flush',
 
                          [0, 0, 0, 0], 'flush'],
 
                         self.dmx0.updates)
 
        self.assertEqual([[215, 8, 0, 0], 'flush', [215, 8, 0, 0], 'flush',
 
                          [215, 6, 0, 0], 'flush'], self.udmx.updates)
 
        self.assertEqual([[0, 0, 0, 0], 'flush', [0, 0, 0, 0], 'flush',
 
                          [0, 0, 0, 0], 'flush'], self.dmx0.updates)
 

	
 
    def testClientsOnDifferentOutputs(self):
 
        c = Collector(self.config, outputs=[self.dmx0, self.udmx])
 

	
 
        c.setAttrs('client1', 'sess1', [(DEV['colorStrip'], L9['color'], '#aa0000')], t0)
 
        c.setAttrs('client2', 'sess1', [(DEV['inst1'], L9['brightness'], .5)], t0)
 
        c.setAttrs('client1', 'sess1',
 
                   [(DEV['colorStrip'], L9['color'], '#aa0000')], t0)
 
        c.setAttrs('client2', 'sess1', [(DEV['inst1'], L9['brightness'], .5)],
 
                   t0)
 

	
 
        # ok that udmx is flushed twice- it can screen out its own duplicates
 
        self.assertEqual([[215, 170, 0, 0], 'flush',
 
                          [215, 170, 0, 0], 'flush'], self.udmx.updates)
 
        self.assertEqual([[0, 0, 0, 0], 'flush',
 
                          [127, 0, 0, 0], 'flush'], self.dmx0.updates)
 
        self.assertEqual([[215, 170, 0, 0], 'flush', [215, 170, 0, 0], 'flush'],
 
                         self.udmx.updates)
 
        self.assertEqual([[0, 0, 0, 0], 'flush', [127, 0, 0, 0], 'flush'],
 
                         self.dmx0.updates)
 

	
 
    def testNewSessionReplacesPreviousOutput(self):
 
        # ..as opposed to getting max'd with it
 
        c = Collector(self.config, outputs=[self.dmx0, self.udmx])
 

	
 
        c.setAttrs('client1', 'sess1', [(DEV['inst1'], L9['brightness'], .8)], t0)
 
        c.setAttrs('client1', 'sess2', [(DEV['inst1'], L9['brightness'], .5)], t0)
 
        c.setAttrs('client1', 'sess1', [(DEV['inst1'], L9['brightness'], .8)],
 
                   t0)
 
        c.setAttrs('client1', 'sess2', [(DEV['inst1'], L9['brightness'], .5)],
 
                   t0)
 

	
 
        self.assertEqual([[204, 0, 0, 0], 'flush',
 
                          [127, 0, 0, 0], 'flush'], self.dmx0.updates)
 
        self.assertEqual([[204, 0, 0, 0], 'flush', [127, 0, 0, 0], 'flush'],
 
                         self.dmx0.updates)
 

	
 
    def testNewSessionDropsPreviousSettingsOfOtherAttrs(self):
 
        c = Collector(MockSyncedGraph(PREFIX + THEATER + '''
 
@@ -183,15 +194,16 @@ class TestCollector(unittest.TestCase):
 
        dev:inst1 a :Device, :SimpleDimmer;
 
          :dmxUniverse dmx0:; :dmxBase 0;
 
          :level dev:inst1Brightness .
 
        '''), outputs=[self.dmx0, self.udmx])
 
        '''),
 
                      outputs=[self.dmx0, self.udmx])
 

	
 
        c.setAttrs('client1', 'sess1',
 
                   [(DEV['colorStrip'], L9['color'], '#ff0000')], t0)
 
        c.setAttrs('client1', 'sess2',
 
                   [(DEV['colorStrip'], L9['color'], '#00ff00')], t0)
 

	
 
        self.assertEqual([[215, 255, 0, 0], 'flush',
 
                          [215, 0, 255, 0], 'flush'], self.udmx.updates)
 
        self.assertEqual([[215, 255, 0, 0], 'flush', [215, 0, 255, 0], 'flush'],
 
                         self.udmx.updates)
 

	
 
    def testClientIsForgottenAfterAWhile(self):
 
        with freeze_time(datetime.datetime.now()) as ft:
 
@@ -200,37 +212,34 @@ class TestCollector(unittest.TestCase):
 
                       time.time())
 
            ft.tick(delta=datetime.timedelta(seconds=1))
 
            # this max's with cli1's value so we still see .5
 
            c.setAttrs('cli2', 'sess1', [(DEV['inst1'], L9['brightness'], .2)], 
 
            c.setAttrs('cli2', 'sess1', [(DEV['inst1'], L9['brightness'], .2)],
 
                       time.time())
 
            ft.tick(delta=datetime.timedelta(seconds=9.1))
 
            # now cli1 is forgotten, so our value appears
 
            c.setAttrs('cli2', 'sess1', [(DEV['inst1'], L9['brightness'], .4)], 
 
            c.setAttrs('cli2', 'sess1', [(DEV['inst1'], L9['brightness'], .4)],
 
                       time.time())
 
            self.assertEqual([[127, 0, 0, 0], 'flush',
 
                              [127, 0, 0, 0], 'flush',
 
                              [102, 0, 0, 0], 'flush'],
 
                             self.dmx0.updates)
 
            self.assertEqual([[127, 0, 0, 0], 'flush', [127, 0, 0, 0], 'flush',
 
                              [102, 0, 0, 0], 'flush'], self.dmx0.updates)
 

	
 
    def testClientUpdatesAreNotMerged(self):
 
        # second call to setAttrs forgets the first
 
        c = Collector(self.config, outputs=[self.dmx0, self.udmx])
 
        t0 = time.time()
 
        c.setAttrs('client1', 'sess1', [(DEV['inst1'], L9['brightness'], .5)], t0)
 
        c.setAttrs('client1', 'sess1', [(DEV['inst1'], L9['brightness'], 1)], t0)
 
        c.setAttrs('client1', 'sess1', [(DEV['colorStrip'], L9['color'], '#00ff00')], t0)
 
        c.setAttrs('client1', 'sess1', [(DEV['inst1'], L9['brightness'], .5)],
 
                   t0)
 
        c.setAttrs('client1', 'sess1', [(DEV['inst1'], L9['brightness'], 1)],
 
                   t0)
 
        c.setAttrs('client1', 'sess1',
 
                   [(DEV['colorStrip'], L9['color'], '#00ff00')], t0)
 

	
 
        self.assertEqual([[215, 0, 0, 0], 'flush',
 
                          [215, 0, 0, 0], 'flush',
 
                          [215, 0, 255, 0], 'flush'],
 
                         self.udmx.updates)
 
        self.assertEqual([[127, 0, 0, 0], 'flush',
 
                          [255, 0, 0, 0], 'flush',
 
                          [0, 0, 0, 0], 'flush'],
 
                         self.dmx0.updates)
 
        self.assertEqual([[215, 0, 0, 0], 'flush', [215, 0, 0, 0], 'flush',
 
                          [215, 0, 255, 0], 'flush'], self.udmx.updates)
 
        self.assertEqual([[127, 0, 0, 0], 'flush', [255, 0, 0, 0], 'flush',
 
                          [0, 0, 0, 0], 'flush'], self.dmx0.updates)
 

	
 
    def testRepeatedAttributesInOneRequestGetResolved(self):
 
        c = Collector(self.config, outputs=[self.dmx0, self.udmx])
 
        
 

	
 
        c.setAttrs('client1', 'sess1', [
 
            (DEV['inst1'], L9['brightness'], .5),
 
            (DEV['inst1'], L9['brightness'], .3),
 
@@ -241,6 +250,5 @@ class TestCollector(unittest.TestCase):
 
            (DEV['inst1'], L9['brightness'], .3),
 
            (DEV['inst1'], L9['brightness'], .5),
 
        ], t0)
 
        self.assertEqual([[127, 0, 0, 0], 'flush',
 
                          [127, 0, 0, 0], 'flush'], self.dmx0.updates)
 

	
 
        self.assertEqual([[127, 0, 0, 0], 'flush', [127, 0, 0, 0], 'flush'],
 
                         self.dmx0.updates)
light9/collector/device.py
Show inline comments
 
@@ -19,7 +19,8 @@ class ChauvetColorStrip(Device):
 
     device attrs:
 
       color
 
    """
 
        
 

	
 

	
 
class Mini15(Device):
 
    """
 
    plan:
 
@@ -31,14 +32,18 @@ class Mini15(Device):
 
        goboShake
 
        imageAim (configured with a file of calibration data)
 
    """
 

	
 

	
 
def clamp255(x):
 
    return min(255, max(0, x))
 
    
 

	
 

	
 
def _8bit(f):
 
    if not isinstance(f, (int, float)):
 
        raise TypeError(repr(f))
 
    return clamp255(int(f * 255))
 

	
 

	
 
def resolve(deviceType, deviceAttr, values):
 
    """
 
    return one value to use for this attr, given a set of them that
 
@@ -66,6 +71,7 @@ def resolve(deviceType, deviceAttr, valu
 
        return Literal(sum(floatVals) / len(floatVals))
 
    return max(values)
 

	
 

	
 
def toOutputAttrs(deviceType, deviceAttrSettings):
 
    """
 
    Given device attr settings like {L9['color']: Literal('#ff0000')},
 
@@ -74,6 +80,7 @@ def toOutputAttrs(deviceType, deviceAttr
 

	
 
    :outputAttrRange happens before we get here.
 
    """
 

	
 
    def floatAttr(attr, default=0):
 
        out = deviceAttrSettings.get(attr)
 
        if out is None:
 
@@ -86,12 +93,10 @@ def toOutputAttrs(deviceType, deviceAttr
 
        return r, g, b
 

	
 
    def cmyAttr(attr):
 
        rgb = sRGBColor.new_from_rgb_hex(deviceAttrSettings.get(attr, '#000000'))
 
        rgb = sRGBColor.new_from_rgb_hex(deviceAttrSettings.get(
 
            attr, '#000000'))
 
        out = colormath.color_conversions.convert_color(rgb, CMYColor)
 
        return (
 
            _8bit(out.cmy_c),
 
            _8bit(out.cmy_m),
 
            _8bit(out.cmy_y))
 
        return (_8bit(out.cmy_c), _8bit(out.cmy_m), _8bit(out.cmy_y))
 

	
 
    def fine16Attr(attr, scale=1.0):
 
        x = floatAttr(attr) * scale
 
@@ -106,20 +111,15 @@ def toOutputAttrs(deviceType, deviceAttr
 
        if deviceAttrSettings.get(attr) == L9['g2']:
 
            return 10
 
        return 0
 
        
 

	
 
    if deviceType == L9['ChauvetColorStrip']:
 
        r, g, b = rgbAttr(L9['color'])
 
        return {
 
            L9['mode']: 215,
 
            L9['red']: r,
 
            L9['green']: g,
 
            L9['blue']: b
 
            }
 
        return {L9['mode']: 215, L9['red']: r, L9['green']: g, L9['blue']: b}
 
    elif deviceType == L9['SimpleDimmer']:
 
        return {L9['level']: _8bit(floatAttr(L9['brightness']))}
 
    elif deviceType == L9['Mini15']:
 
        out = {
 
            L9['rotationSpeed']: 0, # seems to have no effect
 
            L9['rotationSpeed']: 0,  # seems to have no effect
 
            L9['dimmer']: 255,
 
            L9['colorChange']: 0,
 
            L9['colorSpeed']: 0,
 
@@ -131,17 +131,18 @@ def toOutputAttrs(deviceType, deviceAttr
 
            L9['mini15Gobo1']: 10,
 
            L9['mini15Gobo2']: 20,
 
            L9['mini15Gobo3']: 30,
 
            }[deviceAttrSettings.get(L9['mini15GoboChoice'], L9['open'])]
 
        
 
        }[deviceAttrSettings.get(L9['mini15GoboChoice'], L9['open'])]
 

	
 
        out[L9['red']], out[L9['green']], out[L9['blue']] = rgbAttr(L9['color'])
 
        out[L9['xRotation']], out[L9['xFine']] = fine16Attr(L9['rx'], 1/540)
 
        out[L9['yRotation']], out[L9['yFine']] = fine16Attr(L9['ry'], 1/240)
 
        out[L9['xRotation']], out[L9['xFine']] = fine16Attr(L9['rx'], 1 / 540)
 
        out[L9['yRotation']], out[L9['yFine']] = fine16Attr(L9['ry'], 1 / 240)
 
        # didn't find docs on this, but from tests it looks like 64 fine steps takes you to the next coarse step
 

	
 
        return out
 
    elif deviceType == L9['ChauvetHex12']:
 
        out = {}
 
        out[L9['red']], out[L9['green']], out[L9['blue']] = r, g, b = rgbAttr(L9['color'])
 
        out[L9['red']], out[L9['green']], out[L9['blue']] = r, g, b = rgbAttr(
 
            L9['color'])
 
        out[L9['amber']] = 0
 
        out[L9['white']] = min(r, g, b)
 
        out[L9['uv']] = _8bit(floatAttr(L9['uv']))
 
@@ -153,7 +154,7 @@ def toOutputAttrs(deviceType, deviceAttr
 
        out[L9['fixed255']] = 255
 
        for num in range(7):
 
            out[L9['fixed128_%s' % num]] = 128
 
        return out        
 
        return out
 
    elif deviceType == L9['MacAura']:
 
        out = {
 
            L9['shutter']: 22,
 
@@ -184,21 +185,23 @@ def toOutputAttrs(deviceType, deviceAttr
 
        out = {
 
            L9['dimmerFadeLo']: 0,
 
            L9['fixtureControl']: 0,
 
            L9['fx1Select']:  0,
 
            L9['fx1Adjust']:  0,
 
            L9['fx2Select']:  0,
 
            L9['fx2Adjust']:  0,
 
            L9['fxSync']:  0,            
 
            }
 
            L9['fx1Select']: 0,
 
            L9['fx1Adjust']: 0,
 
            L9['fx2Select']: 0,
 
            L9['fx2Adjust']: 0,
 
            L9['fxSync']: 0,
 
        }
 

	
 
        # note these values are set to 'fade', so they update slowly. Haven't found where to turn that off.
 
        out[L9['cyan']], out[L9['magenta']], out[L9['yellow']] = cmyAttr(L9['color'])
 
        
 
        out[L9['cyan']], out[L9['magenta']], out[L9['yellow']] = cmyAttr(
 
            L9['color'])
 

	
 
        out[L9['focusHi']], out[L9['focusLo']] = fine16Attr(L9['focus'])
 
        out[L9['panHi']], out[L9['panLo']] = fine16Attr(L9['rx'])
 
        out[L9['tiltHi']], out[L9['tiltLo']] = fine16Attr(L9['ry'])
 
        out[L9['zoomHi']], out[L9['zoomLo']] = fine16Attr(L9['zoom'])
 
        out[L9['dimmerFadeHi']] = 0 if deviceAttrSettings.get(L9['color'], '#000000') == '#000000' else 255
 
        out[L9['dimmerFadeHi']] = 0 if deviceAttrSettings.get(
 
            L9['color'], '#000000') == '#000000' else 255
 

	
 
        out[L9['goboChoice']] = {
 
            L9['open']: 0,
 
@@ -208,7 +211,7 @@ def toOutputAttrs(deviceType, deviceAttr
 
            L9['brush']: 51,
 
            L9['whirlpool']: 56,
 
            L9['stars']: 61,
 
            }[deviceAttrSettings.get(L9['quantumGoboChoice'], L9['open'])]
 
        }[deviceAttrSettings.get(L9['quantumGoboChoice'], L9['open'])]
 

	
 
        # my goboSpeed deviceAttr goes 0=stopped to 1=fastest (using one direction only)
 
        x = .5 + .5 * floatAttr(L9['goboSpeed'])
 
@@ -220,13 +223,13 @@ def toOutputAttrs(deviceType, deviceAttr
 
            out[L9['shutter']] = 30
 
        else:
 
            out[L9['shutter']] = 50 + int(150 * (strobe - .1) / .9)
 
        
 
        out.update( {
 

	
 
        out.update({
 
            L9['colorWheel']: 0,
 
            L9['goboStaticRotate']: 0,
 
            L9['prismRotation']: _8bit(floatAttr(L9['prism'])),
 
            L9['iris']: _8bit(floatAttr(L9['iris']) * (200/255)),
 
            })
 
            L9['iris']: _8bit(floatAttr(L9['iris']) * (200 / 255)),
 
        })
 
        return out
 
    else:
 
        raise NotImplementedError('device %r' % deviceType)
light9/collector/device_test.py
Show inline comments
 
@@ -4,50 +4,67 @@ from light9.namespaces import L9
 

	
 
from light9.collector.device import toOutputAttrs, resolve
 

	
 

	
 
class TestUnknownDevice(unittest.TestCase):
 

	
 
    def testFails(self):
 
        self.assertRaises(NotImplementedError, toOutputAttrs, L9['bogus'], {})
 

	
 

	
 
class TestColorStrip(unittest.TestCase):
 

	
 
    def testConvertDeviceToOutputAttrs(self):
 
        out = toOutputAttrs(L9['ChauvetColorStrip'],
 
                            {L9['color']: Literal('#ff0000')})
 
        self.assertEqual({L9['mode']: 215,
 
                          L9['red']: 255,
 
                          L9['green']: 0,
 
                          L9['blue']: 0
 
                      }, out)
 
        
 
        self.assertEqual(
 
            {
 
                L9['mode']: 215,
 
                L9['red']: 255,
 
                L9['green']: 0,
 
                L9['blue']: 0
 
            }, out)
 

	
 

	
 
class TestDimmer(unittest.TestCase):
 

	
 
    def testConvert(self):
 
        self.assertEqual({L9['level']: 127},
 
                         toOutputAttrs(L9['SimpleDimmer'], {L9['brightness']: .5}))
 
                         toOutputAttrs(L9['SimpleDimmer'],
 
                                       {L9['brightness']: .5}))
 

	
 

	
 
class TestMini15(unittest.TestCase):
 

	
 
    def testConvertColor(self):
 
        out = toOutputAttrs(L9['Mini15'], {L9['color']: '#010203'})
 
        self.assertEqual(255, out[L9['dimmer']])
 
        self.assertEqual(1, out[L9['red']])
 
        self.assertEqual(2, out[L9['green']])
 
        self.assertEqual(3, out[L9['blue']])
 

	
 
    def testConvertRotation(self):
 
        out = toOutputAttrs(L9['Mini15'], {L9['rx']: Literal(90), L9['ry']: Literal(45)})
 
        out = toOutputAttrs(L9['Mini15'], {
 
            L9['rx']: Literal(90),
 
            L9['ry']: Literal(45)
 
        })
 
        self.assertEqual(42, out[L9['xRotation']])
 
        self.assertEqual(127, out[L9['xFine']])
 
        self.assertEqual(47, out[L9['yRotation']])
 
        self.assertEqual(207, out[L9['yFine']])
 
        self.assertEqual(0, out[L9['rotationSpeed']])
 
        
 

	
 

	
 
class TestResolve(unittest.TestCase):
 

	
 
    def testMaxes1Color(self):
 
        # do not delete - this one catches a bug in the rgb_to_hex(...) lines
 
        self.assertEqual('#ff0300',
 
                         resolve(None, L9['color'], ['#ff0300']))
 
        self.assertEqual('#ff0300', resolve(None, L9['color'], ['#ff0300']))
 

	
 
    def testMaxes2Colors(self):
 
        self.assertEqual('#ff0400',
 
                         resolve(None, L9['color'], ['#ff0300', '#000400']))
 

	
 
    def testMaxes3Colors(self):
 
        self.assertEqual('#112233',
 
                         resolve(None, L9['color'],
 
                                 ['#110000', '#002200', '#000033']))
 
        
 
        self.assertEqual(
 
            '#112233',
 
            resolve(None, L9['color'], ['#110000', '#002200', '#000033']))
light9/collector/output.py
Show inline comments
 
@@ -8,6 +8,7 @@ from twisted.internet import task, threa
 
from greplin import scales
 
log = logging.getLogger('output')
 

	
 

	
 
# eliminate this: lists are always padded now
 
def setListElem(outList, index, value, fill=0, combine=lambda old, new: new):
 
    if len(outList) < index:
 
@@ -17,6 +18,7 @@ def setListElem(outList, index, value, f
 
    else:
 
        outList[index] = combine(outList[index], value)
 

	
 

	
 
class Output(object):
 
    """
 
    send an array of values to some output device. Call update as
 
@@ -25,9 +27,10 @@ class Output(object):
 
    """
 
    uri = None  # type: URIRef
 
    numChannels = None  # type: int
 

	
 
    def __init__(self):
 
        raise NotImplementedError
 
        
 

	
 
    def allConnections(self):
 
        """
 
        sequence of (index, uri) for the uris we can output, and which
 
@@ -35,7 +38,6 @@ class Output(object):
 
        """
 
        raise NotImplementedError
 

	
 
    
 
    def update(self, values):
 
        """
 
        output takes a flattened list of values, maybe dmx channels, or
 
@@ -53,11 +55,13 @@ class Output(object):
 
        """short string to distinguish outputs"""
 
        raise NotImplementedError
 

	
 

	
 
class DummyOutput(Output):
 

	
 
    def __init__(self, uri, numChannels=1, **kw):
 
        self.uri = uri
 
        self.numChannels = numChannels
 
    
 

	
 
    def update(self, values):
 
        pass
 

	
 
@@ -67,8 +71,9 @@ class DummyOutput(Output):
 
    def shortId(self):
 
        return 'null'
 

	
 
        
 

	
 
class DmxOutput(Output):
 

	
 
    def __init__(self, uri, numChannels):
 
        self.uri = uri
 
        self.numChannels = numChannels
 
@@ -85,16 +90,14 @@ class DmxOutput(Output):
 
                self.countError()
 
            else:
 
                self.lastSentBuffer = sendingBuffer
 
            reactor.callLater(max(0, start + 1/20 - time.time()),
 
                              self._loop)
 
            reactor.callLater(max(0, start + 1 / 20 - time.time()), self._loop)
 

	
 
        d = threads.deferToThread(self.sendDmx, sendingBuffer)
 
        d.addCallback(done)
 
        
 

	
 

	
 
class EnttecDmx(DmxOutput):
 
    stats = scales.collection('/output/enttecDmx',
 
                              scales.PmfStat('write'),
 
    stats = scales.collection('/output/enttecDmx', scales.PmfStat('write'),
 
                              scales.PmfStat('update'))
 

	
 
    def __init__(self, uri, devicePath='/dev/dmx0', numChannels=80):
 
@@ -128,16 +131,16 @@ class EnttecDmx(DmxOutput):
 
    def shortId(self):
 
        return 'enttec'
 

	
 
                                  
 

	
 
class Udmx(DmxOutput):
 
    stats = scales.collection('/output/udmx',
 
                              scales.PmfStat('update'),
 
    stats = scales.collection('/output/udmx', scales.PmfStat('update'),
 
                              scales.PmfStat('write'),
 
                              scales.IntStat('usbErrors'))
 

	
 
    def __init__(self, uri, bus, numChannels):
 
        DmxOutput.__init__(self, uri, numChannels)
 
        self._shortId = self.uri.rstrip('/')[-1]
 
        
 

	
 
        from light9.io.udmx import Udmx
 
        self.dev = Udmx(bus)
 
        self.currentBuffer = ''
 
@@ -173,14 +176,14 @@ class Udmx(DmxOutput):
 
            except usb.core.USBError as e:
 
                # not in main thread
 
                if e.errno != 75:
 
                  msg = 'usb: sending %s bytes to %r; error %r' % (len(buf), self.uri, e)
 
                  print msg
 
                    msg = 'usb: sending %s bytes to %r; error %r' % (
 
                        len(buf), self.uri, e)
 
                    print msg
 
                return False
 

	
 
    def countError(self):
 
        # in main thread
 
        Udmx.stats.usbErrors += 1
 
        
 

	
 
    def shortId(self):
 
        return self._shortId
 

	
light9/collector/output_test.py
Show inline comments
 
@@ -2,38 +2,47 @@ import unittest
 
from light9.namespaces import L9
 
from light9.collector.output import setListElem, DmxOutput
 

	
 

	
 
class TestSetListElem(unittest.TestCase):
 

	
 
    def testSetExisting(self):
 
        x = [0, 1]
 
        setListElem(x, 0, 9)
 
        self.assertEqual([9, 1], x)
 

	
 
    def testSetNext(self):
 
        x = [0, 1]
 
        setListElem(x, 2, 9)
 
        self.assertEqual([0, 1, 9], x)
 

	
 
    def testSetBeyond(self):
 
        x = [0, 1]
 
        setListElem(x, 3, 9)
 
        self.assertEqual([0, 1, 0, 9], x)
 

	
 
    def testArbitraryFill(self):
 
        x = [0, 1]
 
        setListElem(x, 5, 9, fill=8)
 
        self.assertEqual([0, 1, 8, 8, 8, 9], x)
 

	
 
    def testSetZero(self):
 
        x = [0, 1]
 
        setListElem(x, 5, 0)
 
        self.assertEqual([0, 1, 0, 0, 0, 0], x)
 

	
 
    def testCombineMax(self):
 
        x = [0, 1]
 
        setListElem(x, 1, 0, combine=max)
 
        self.assertEqual([0, 1], x)
 

	
 
    def testCombineHasNoEffectOnNewElems(self):
 
        x = [0, 1]
 
        setListElem(x, 2, 1, combine=max)
 
        self.assertEqual([0, 1, 1], x)
 
        
 

	
 

	
 
class TestDmxOutput(unittest.TestCase):
 

	
 
    def testFlushIsNoop(self):
 
        out = DmxOutput(L9['output/udmx/'], 3)
 
        out.flush()
 
        
light9/curvecalc/client.py
Show inline comments
 
@@ -6,16 +6,19 @@ from light9 import networking
 
import urllib
 
from run_local import log
 

	
 

	
 
def sendLiveInputPoint(curve, value):
 
    f = cyclone.httpclient.fetch(
 
        networking.curveCalc.path('liveInputPoint'),
 
        method='POST', timeout=1,
 
        postdata=urllib.urlencode({
 
            'curve': curve,
 
            'value': str(value),
 
        }))
 
    f = cyclone.httpclient.fetch(networking.curveCalc.path('liveInputPoint'),
 
                                 method='POST',
 
                                 timeout=1,
 
                                 postdata=urllib.urlencode({
 
                                     'curve': curve,
 
                                     'value': str(value),
 
                                 }))
 

	
 
    @f.addCallback
 
    def cb(result):
 
        if result.code // 100 != 2:
 
            raise ValueError("curveCalc said %s: %s", result.code, result.body)
 

	
 
    return f
light9/curvecalc/cursors.py
Show inline comments
 

	
 
import logging
 
log = logging.getLogger("cursors")
 

	
 
# accept ascii images, read file images, add hotspots, read xbm as
 
# cursor with @filename form
 

	
 
_pushed = {} # widget : [old, .., newest]
 
def push(widget,new_cursor):
 
_pushed = {}  # widget : [old, .., newest]
 

	
 

	
 
def push(widget, new_cursor):
 
    global _pushed
 
    _pushed.setdefault(widget,[]).append(widget.cget("cursor"))
 
    _pushed.setdefault(widget, []).append(widget.cget("cursor"))
 

	
 

	
 
def pop(widget):
 
    global _pushed
 
@@ -18,5 +20,3 @@ def pop(widget):
 
        log.debug("cursor pop from empty stack")
 
        return
 
    widget.config(cursor=c)
 
    
 
    
light9/curvecalc/curve.py
Show inline comments
 
from __future__ import division
 
import glob, time, logging, ast, os
 
from bisect import bisect_left,bisect
 
from bisect import bisect_left, bisect
 
import louie as dispatcher
 
from twisted.internet import reactor
 
from rdflib import Literal
 
@@ -13,12 +13,14 @@ log = logging.getLogger()
 
introPad = 4
 
postPad = 4
 

	
 

	
 
class Curve(object):
 
    """curve does not know its name. see Curveset"""
 

	
 
    def __init__(self, uri, pointsStorage='graph'):
 
        self.uri = uri
 
        self.pointsStorage = pointsStorage
 
        self.points = [] # x-sorted list of (x,y)
 
        self.points = []  # x-sorted list of (x,y)
 
        self._muted = False
 

	
 
    def __repr__(self):
 
@@ -27,24 +29,28 @@ class Curve(object):
 

	
 
    def muted():
 
        doc = "Whether to currently send levels (boolean, obviously)"
 

	
 
        def fget(self):
 
            return self._muted
 

	
 
        def fset(self, val):
 
            self._muted = val
 
            dispatcher.send('mute changed', sender=self)
 

	
 
        return locals()
 

	
 
    muted = property(**muted())
 

	
 
    def toggleMute(self):
 
        self.muted = not self.muted
 

	
 
    def load(self,filename):
 
        self.points[:]=[]
 
    def load(self, filename):
 
        self.points[:] = []
 
        for line in file(filename):
 
            x, y = line.split()
 
            self.points.append((float(x), ast.literal_eval(y)))
 
        self.points.sort()
 
        dispatcher.send("points changed",sender=self)
 
        dispatcher.send("points changed", sender=self)
 

	
 
    def set_from_string(self, pts):
 
        self.points[:] = []
 
@@ -53,22 +59,24 @@ class Curve(object):
 
        for x, y in pairs:
 
            self.points.append((float(x), ast.literal_eval(y)))
 
        self.points.sort()
 
        dispatcher.send("points changed",sender=self)
 
        dispatcher.send("points changed", sender=self)
 

	
 
    def points_as_string(self):
 

	
 
        def outVal(x):
 
            if isinstance(x, basestring): # markers
 
            if isinstance(x, basestring):  # markers
 
                return x
 
            return "%.4g" % x
 
        return ' '.join("%s %s" % (outVal(p[0]), outVal(p[1]))
 
                        for p in self.points)
 
        
 
    def save(self,filename):
 

	
 
        return ' '.join(
 
            "%s %s" % (outVal(p[0]), outVal(p[1])) for p in self.points)
 

	
 
    def save(self, filename):
 
        # this is just around for markers, now
 
        if filename.endswith('-music') or filename.endswith('_music'):
 
            print "not saving music track"
 
            return
 
        f = file(filename,'w')
 
        f = file(filename, 'w')
 
        for p in self.points:
 
            f.write("%s %r\n" % p)
 
        f.close()
 
@@ -78,25 +86,26 @@ class Curve(object):
 
            return 0
 
        if not self.points:
 
            raise ValueError("curve has no points")
 
        i = bisect_left(self.points,(t,None))-1
 
        i = bisect_left(self.points, (t, None)) - 1
 

	
 
        if i == -1:
 
            return self.points[0][1]
 
        if self.points[i][0]>t:
 
        if self.points[i][0] > t:
 
            return self.points[i][1]
 
        if i>=len(self.points)-1:
 
        if i >= len(self.points) - 1:
 
            return self.points[i][1]
 

	
 
        p1,p2 = self.points[i],self.points[i+1]
 
        frac = (t-p1[0])/(p2[0]-p1[0])
 
        y = p1[1]+(p2[1]-p1[1])*frac
 
        p1, p2 = self.points[i], self.points[i + 1]
 
        frac = (t - p1[0]) / (p2[0] - p1[0])
 
        y = p1[1] + (p2[1] - p1[1]) * frac
 
        return y
 
    __call__=eval
 

	
 
    __call__ = eval
 

	
 
    def insert_pt(self, new_pt):
 
        """returns index of new point"""
 
        i = bisect(self.points, (new_pt[0],None))
 
        self.points.insert(i,new_pt)
 
        i = bisect(self.points, (new_pt[0], None))
 
        self.points.insert(i, new_pt)
 
        # missing a check that this isn't the same X as the neighbor point
 
        dispatcher.send("points changed", sender=self)
 
        return i
 
@@ -109,7 +118,7 @@ class Curve(object):
 
        self.insert_pt(new_pt)
 
        dispatcher.send("points changed", sender=self)
 
        # now simplify to the left
 
        
 

	
 
    def set_points(self, updates):
 
        for i, pt in updates:
 
            self.points[i] = pt
 
@@ -118,7 +127,7 @@ class Curve(object):
 
        # lot. need a new solution.
 
        #self.checkOverlap()
 
        dispatcher.send("points changed", sender=self)
 
            
 

	
 
    def checkOverlap(self):
 
        x = None
 
        for p in self.points:
 
@@ -130,20 +139,20 @@ class Curve(object):
 
        p = self.points.pop(i)
 
        dispatcher.send("points changed", sender=self)
 
        return p
 
            
 

	
 
    def remove_point(self, pt):
 
        self.points.remove(pt)
 
        dispatcher.send("points changed", sender=self)
 
            
 

	
 
    def indices_between(self, x1, x2, beyond=0):
 
        leftidx = max(0, bisect(self.points, (x1,None)) - beyond)
 
        leftidx = max(0, bisect(self.points, (x1, None)) - beyond)
 
        rightidx = min(len(self.points),
 
                       bisect(self.points, (x2,None)) + beyond)
 
                       bisect(self.points, (x2, None)) + beyond)
 
        return range(leftidx, rightidx)
 
        
 

	
 
    def points_between(self, x1, x2):
 
        """returns (x,y) points"""
 
        return [self.points[i] for i in self.indices_between(x1,x2)]
 
        return [self.points[i] for i in self.indices_between(x1, x2)]
 

	
 
    def point_before(self, x):
 
        """(x,y) of the point left of x, or None"""
 
@@ -153,15 +162,17 @@ class Curve(object):
 
        return self.points[leftidx]
 

	
 
    def index_before(self, x):
 
        leftidx = bisect(self.points, (x,None)) - 1
 
        leftidx = bisect(self.points, (x, None)) - 1
 
        if leftidx < 0:
 
            return None
 
        return leftidx
 

	
 

	
 
class CurveResource(object):
 
    """
 
    holds a Curve, deals with graphs
 
    """
 

	
 
    def __init__(self, graph, uri):
 
        # probably newCurve and loadCurve should be the constructors instead.
 
        self.graph, self.uri = graph, uri
 
@@ -177,15 +188,16 @@ class CurveResource(object):
 
        if hasattr(self, 'curve'):
 
            raise ValueError('CurveResource already has a curve %r' %
 
                             self.curve)
 
        self.graph.patch(Patch(addQuads=[
 
            (self.uri, RDF.type, L9['Curve'], ctx),
 
            (self.uri, RDFS.label, label, ctx),
 
        self.graph.patch(
 
            Patch(addQuads=[
 
                (self.uri, RDF.type, L9['Curve'], ctx),
 
                (self.uri, RDFS.label, label, ctx),
 
            ]))
 
        self.curve = Curve(self.uri)
 
        self.curve.points.extend([(0, 0)])
 
        self.saveCurve()
 
        self.watchCurvePointChanges()
 
        
 

	
 
    def loadCurve(self):
 
        if hasattr(self, 'curve'):
 
            raise ValueError('CurveResource already has a curve %r' %
 
@@ -198,7 +210,7 @@ class CurveResource(object):
 
        else:
 
            # given a currentState graph
 
            self.pointsFromGraph()
 
        
 

	
 
    def pointsFromGraph(self):
 
        pts = self.graph.value(self.uri, L9['points'])
 
        if pts is not None:
 
@@ -222,18 +234,20 @@ class CurveResource(object):
 
            #cur.save("%s-%s" % (basename,name))
 
            return []
 
        elif self.curve.pointsStorage == 'graph':
 
            return [self.graph.getObjectPatch(
 
                self.curvePointsContext(),
 
                subject=self.uri,
 
                predicate=L9['points'],
 
                newObject=Literal(self.curve.points_as_string()))]
 
            return [
 
                self.graph.getObjectPatch(self.curvePointsContext(),
 
                                          subject=self.uri,
 
                                          predicate=L9['points'],
 
                                          newObject=Literal(
 
                                              self.curve.points_as_string()))
 
            ]
 
        else:
 
            raise NotImplementedError(self.curve.pointsStorage)
 

	
 
    def watchCurvePointChanges(self):
 
        """start watching and saving changes to the graph"""
 
        dispatcher.connect(self.onChange, 'points changed', sender=self.curve)
 
        
 

	
 
    def onChange(self):
 

	
 
        # Don't write a patch for the edited curve points until they've been
 
@@ -243,31 +257,34 @@ class CurveResource(object):
 
        # this is just the wrong timing algorithm- it should be a max rate,
 
        # not a max-hold-still-time.
 
        HOLD_POINTS_GRAPH_COMMIT_SECS = .1
 
        
 

	
 
        if getattr(self, 'pendingSave', None):
 
            self.pendingSave.cancel()
 
        self.pendingSave = reactor.callLater(HOLD_POINTS_GRAPH_COMMIT_SECS,
 
                                             self.saveCurve)
 
        
 

	
 

	
 
class Markers(Curve):
 
    """Marker is like a point but the y value is a string"""
 

	
 
    def eval(self):
 
        raise NotImplementedError()
 

	
 

	
 
def slope(p1,p2):
 
def slope(p1, p2):
 
    if p2[0] == p1[0]:
 
        return 0
 
    return (p2[1] - p1[1]) / (p2[0] - p1[0])
 

	
 
       
 

	
 
class Curveset(object):
 

	
 
    def __init__(self, graph, session):
 
        self.graph, self.session = graph, session
 

	
 
        self.currentSong = None
 
        self.curveResources = {} # uri : CurveResource
 
        
 
        self.curveResources = {}  # uri : CurveResource
 

	
 
        self.markers = Markers(uri=None, pointsStorage='file')
 

	
 
        graph.addHandler(self.loadCurvesForSong)
 
@@ -285,7 +302,7 @@ class Curveset(object):
 
        dispatcher.send("clear_curves")
 
        self.curveResources.clear()
 
        self.markers = Markers(uri=None, pointsStorage='file')
 
        
 

	
 
        self.currentSong = self.graph.value(self.session, L9['currentSong'])
 
        if self.currentSong is None:
 
            return
 
@@ -298,11 +315,14 @@ class Curveset(object):
 
                curvename = self.graph.label(uri)
 
                if not curvename:
 
                    raise ValueError("curve %r has no label" % uri)
 
                dispatcher.send("add_curve", sender=self,
 
                                uri=uri, label=curvename, curve=cr.curve)
 
                dispatcher.send("add_curve",
 
                                sender=self,
 
                                uri=uri,
 
                                label=curvename,
 
                                curve=cr.curve)
 
            except Exception as e:
 
                log.error("loading %s failed: %s", uri, e)
 
                
 

	
 
        basename = os.path.join(
 
            showconfig.curvesDir(),
 
            showconfig.songFilenameFromURI(self.currentSong))
 
@@ -314,7 +334,7 @@ class Curveset(object):
 
    def save(self):
 
        """writes a file for each curve with a name
 
        like basename-curvename, or saves them to the rdf graph"""
 
        basename=os.path.join(
 
        basename = os.path.join(
 
            showconfig.curvesDir(),
 
            showconfig.songFilenameFromURI(self.currentSong))
 

	
 
@@ -326,23 +346,23 @@ class Curveset(object):
 
        # this will cause reloads that will rebuild our curve list
 
        for p in patches:
 
            self.graph.patch(p)
 
            
 

	
 
    def sorter(self, name):
 
        return self.curves[name].uri
 
        
 

	
 
    def curveUrisInOrder(self):
 
        return sorted(self.curveResources.keys())
 

	
 
    def currentCurves(self):
 
        # deprecated
 
        for uri, cr in sorted(self.curveResources.items()):
 
            with self.graph.currentState(
 
                    tripleFilter=(uri, RDFS['label'], None)) as g:
 
            with self.graph.currentState(tripleFilter=(uri, RDFS['label'],
 
                                                       None)) as g:
 
                yield uri, g.label(uri), cr.curve
 
        
 

	
 
    def globalsdict(self):
 
        raise NotImplementedError('subterm used to get a dict of name:curve')
 
    
 

	
 
    def get_time_range(self):
 
        return 0, dispatcher.send("get max time")[0][1]
 

	
 
@@ -358,8 +378,8 @@ class Curveset(object):
 
        cr.curve.points.extend([(s, 0), (e, 0)])
 

	
 
        ctx = self.currentSong
 
        self.graph.patch(Patch(addQuads=[
 
            (self.currentSong, L9['curve'], uri, ctx),
 
        self.graph.patch(
 
            Patch(addQuads=[
 
                (self.currentSong, L9['curve'], uri, ctx),
 
            ]))
 
        cr.saveCurve()
 

	
light9/curvecalc/curveedit.py
Show inline comments
 
@@ -9,40 +9,46 @@ from lib.cycloneerr import PrettyErrorHa
 
from run_local import log
 
from louie import dispatcher
 

	
 

	
 
def serveCurveEdit(port, hoverTimeResponse, curveset):
 
    """
 
    /hoverTime requests actually are handled by the curvecalc gui
 
    """
 
    curveEdit = CurveEdit(curveset)
 
    
 

	
 
    class HoverTime(PrettyErrorHandler, cyclone.web.RequestHandler):
 

	
 
        def get(self):
 
            hoverTimeResponse(self)
 

	
 
    class LiveInputPoint(PrettyErrorHandler, cyclone.web.RequestHandler):
 

	
 
        def post(self):
 
            params = cgi.parse_qs(self.request.body)
 
            curve = URIRef(params['curve'][0])
 
            value = float(params['value'][0])
 
            curveEdit.liveInputPoint(curve, value)
 
            self.set_status(204)
 
            
 
    reactor.listenTCP(port, cyclone.web.Application(handlers=[
 
        (r'/hoverTime', HoverTime),
 
        (r'/liveInputPoint', LiveInputPoint),
 
        ], debug=True))
 

	
 
    
 
    reactor.listenTCP(
 
        port,
 
        cyclone.web.Application(handlers=[
 
            (r'/hoverTime', HoverTime),
 
            (r'/liveInputPoint', LiveInputPoint),
 
        ],
 
                                debug=True))
 

	
 

	
 
class CurveEdit(object):
 

	
 
    def __init__(self, curveset):
 
        self.curveset = curveset
 
        dispatcher.connect(self.inputTime, "input time")
 
        self.currentTime = 0
 
        
 

	
 
    def inputTime(self, val):
 
        self.currentTime = val
 
        
 

	
 
    def liveInputPoint(self, curveUri, value):
 
        curve = self.curveset.curveFromUri(curveUri)
 
        curve.live_input_point((self.currentTime, value), clear_ahead_secs=.5)
 
        

Changeset was too big and was cut off... Show full diff anyway

0 comments (0 inline, 0 general)