Example #1
0
class ColorsIn:
# this method of reporting timeouts only works by convention
# that before calling handle_request() field .timed_out is 
# set to False


    def set_colors(self,path, tags, args, source):
        # which user will be determined by path:
        # we just throw away all slashes and join together what's left
        #print "Here's what we got : path:%s tags:%s args:%s source:%s"%(str(path),str(tags),str(args),str(source))
        pixels = []
        for i in range(0,len(args)/3):
            pixel = (clamp(args[i*3]), clamp(args[i*3+1]), clamp(args[i*3+2]))
            pixels.append( pixel )
        #print "Pixels: %s"%str(pixels)
        #print "Time: "+str((time.time()*1000) % 10000)
        octoapi.write(pixels)
        self.lastwrite=time.time()
        self.server.lastpixels = pixels

    def diff_colors(self, path, tags, args, source):
        # which user will be determined by path:
        # we just throw away all slashes and join together what's left
        #print "Here's what we got : path:%s tags:%s args:%s source:%s"%(str(path),str(tags),str(args),str(source))
        pixels = server.lastpixels
        for i in range(0,len(args)/3):
            pp = (args[i*3],args[i*3+1],args[i*3+2])
            p = pixels[i]
            pixels[i] = (clamp(p[0]+pp[0]), clamp(p[1]+pp[1]), clamp(p[2]+pp[2]))
        #print "Pixels: %s"%str(pixels)
        #print "Time: "+str((time.time()*1000) % 10000)
        octoapi.write(pixels)
        self.lastwrite=time.time()
        self.server.lastpixels = pixels

    def each_frame(self):
        self.server.timed_out = False
        while not self.server.timed_out:
            if time.time() - self.lastwrite > CLEAR_TIME:
                self.lastwrite = time.time()
                octoapi.write([(0,0,0)]*24)
                print "Clearing"
            self.server.handle_request()

    def start(self):
        #server = OSCServer( ("128.174.251.39", 11661) )
        self.server = OSCServer( ("localhost", 11661) )
        self.server.timeout = 0
        self.lastwrite = time.time()        
        self.server.handle_timeout = types.MethodType(handle_timeout, self.server)
        self.server.lastpixels = [(0,0,0)]*24

        self.server.addMsgHandler( "/setcolors", self.set_colors)
        self.server.addMsgHandler( "/diffcolors", self.diff_colors)
        while True:
            self.each_frame()

        self.server.close()
Example #2
0
class ColorsIn:
    def set_colors(self, path, tags, args, source):
        try:
            chroma = ChromaMessage.fromOSC(tags, args)

            octoapi.write(chroma.data)
            #in any case, we slowly fade out every stream remaining
            #this also writes to the device
            #mark the last time we got a packet
        except:
            import traceback
            traceback.print_exc()

    def each_frame(self):
        self.server.timed_out = False
        while not self.server.timed_out:
            #self.handleClearing()
            self.server.handle_request()

    def reload_config(self, path, tags, args, source):
        octoapi.reload_config()

    def whatever(self, path, tags, args, source):
        pass

    def start(self):
        #server = OSCServer( ("128.174.251.39", 11661) )
        self.server = OSCServer(("localhost", 11661))
        self.server.timeout = 0
        self.server.handle_timeout = types.MethodType(handle_timeout,
                                                      self.server)

        self.server.addMsgHandler("/setcolors", self.set_colors)
        self.server.addMsgHandler("/poop", self.whatever)
        self.server.addMsgHandler("/reloadconfig", self.reload_config)
        while True:
            self.each_frame()

        self.server.close()
Example #3
0
class ColorsIn:
    def set_colors(self,path, tags, args, source):
        try:
            chroma = ChromaMessage.fromOSC(tags,args)

            octoapi.write(chroma.data)
            #in any case, we slowly fade out every stream remaining
            #this also writes to the device
            #mark the last time we got a packet
        except:
            import traceback
            traceback.print_exc()

    def each_frame(self):
        self.server.timed_out = False
        while not self.server.timed_out:
            #self.handleClearing()
            self.server.handle_request()

    def reload_config(self, path, tags, args, source):
	octoapi.reload_config()

    def whatever(self, path, tags, args, source):
	pass

    def start(self):
        #server = OSCServer( ("128.174.251.39", 11661) )
        self.server = OSCServer( ("localhost", 11661) )
        self.server.timeout = 0
        self.server.handle_timeout = types.MethodType(handle_timeout, self.server)

        self.server.addMsgHandler( "/setcolors", self.set_colors)
        self.server.addMsgHandler( "/poop", self.whatever)
        self.server.addMsgHandler( "/reloadconfig", self.reload_config)
        while True:
            self.each_frame()

        self.server.close()
Example #4
0
class ColorsIn:
    #measured in seconds to fade in 100%
    fadeoutrate = 4.0
    fadeinrate = 4.0
    bootthreshold = 0.01
    maxlayers = 3

    def set_colors(self,path, tags, args, source):
        try:
            chroma = ChromaMessage.fromOSC(tags,args)

            #if we have no records of any streams:
            #if we have a record of a current stream, and no record of this stream:
            if not self.activepid or chroma.pid not in self.layers:
                self.activepid = chroma.pid
                #mark all other layers as fading out
                for pid in self.layers:
                    self.layers[pid].state = Layer.FADEOUT
                self.layers[chroma.pid] = Layer(chroma, 0, Layer.FADEIN)
                self.lastwriteany = time.time() - 1/30.0 #force this frame to fade in at the target framerate: 30fps

            self.layers[chroma.pid].chroma = chroma

            #in any case, we slowly fade out every stream remaining
            #this also writes to the device
            self.updateStream()
            #mark the last time we got a packet
            self.lastwrite=time.time()
        except:
            import traceback
            traceback.print_exc()

    """
        applies fadeout rules to any stream not the main one
        and writes to the device
    """
    def updateStream(self):
        for layer in self.layers.values():
            self.applyFadingRules(layer)
        if len(self.layers) > ColorsIn.maxlayers:
            self.layers = dict( (k, v) for k,v in self.layers.iteritems() if self.shouldWeKeepLayer(v) )
        #apply our opacity rules
        pixels = self.applyOpacity(self.layers.values())
        if not STAGING:
            self.streamer.pixels = pixels
	    self.streamer.metadata = self.layers[self.activepid].chroma
            #pixels = self.crazyMofoingReorderingOfLights(pixels)
            octoapi.write(pixels)
        if DEBUG:
            for layer in self.layers.values():
                print "PID: %d, OPACITY: %f"%(layer.chroma.pid, layer.opacity)
        self.lastwriteany=time.time()

    def shouldWeKeepLayer(self,layer):
        if layer.state == Layer.FADEOUT and layer.opacity < self.bootthreshold:
            return False
        return True

    def applyFadingRules(self,layer):
        diff = time.time() - self.lastwriteany
        print diff
        fadein = (1.0/ColorsIn.fadeinrate) * diff
        fadeout = (1.0/ColorsIn.fadeoutrate) * diff
        if layer.state == Layer.FADEIN:
            layer.opacity += fadein 
        if layer.state == Layer.FADEOUT:
            layer.opacity -= fadeout 
        layer.opacity = clampv(0,layer.opacity,1)
        return layer

    """
        compresses down the entire list and gives us the final result
    """
    def applyOpacity(self,layers):
        data = [(0.0,0.0,0.0)] * len(layers[0].chroma.data)
        for layer in layers:
            data = [sum(currentvalue,mult(value,layer.opacity)) for currentvalue,value in itertools.izip(data,layer.chroma.data)]
        data = [clampColor(value) for value in data]
        return data

    def handleClearing(self):
        if time.time() - self.lastwrite > CLEAR_TIME and time.time() - self.lastclear > 1/30.0:
            #print "CLEARING! %f"%time.time()
            self.activepid = 0
            for layer in self.layers.values(): layer.state = Layer.FADEOUT
            shouldweupdate = False
            #don't bother updating if we're below the threshold. not often anyway.
            for layer in self.layers.values():
                if layer.opacity > self.bootthreshold:
                    shouldweupdate = True
            if shouldweupdate: 
                self.updateStream()
            else:
                if not STAGING: 
                    octoapi.write([(0,0,0)]*MAX_LIGHTS)
                if DEBUG:
                    print "CLEARED! %f"%time.time()
            self.lastclear = time.time()

    def each_frame(self):
        self.server.timed_out = False
        while not self.server.timed_out:
            self.handleClearing()
            self.server.handle_request()


    def crazyMofoingReorderingOfLights(self, pixels):
        pixels2 = pixels[:] #make a copy so we don't kerplode someone's work
        """
        what are we expecting? we want the back left (by the couches) of the room to be pixel 0, 
        and by the front is the last row
        whereas in reality, it's the opposite.
        here is the order it'd be nice to have them in:
        
        0  1  2  3
        4  5  6  7
        8  9  10 11
        12 13 14 15
        16 17 18 19
        20 21 22 23

        this is the actual order:   
        23 22 21 20
        19 16 17 18
        15 14 13 12
        11 10 9  8
        3  2  5  4
        6 *0**1**7*   *=not there
        """

        actualorder = [23,22,21,20,19,16,17,18,15,14,13,12,11,10,9,8,3,2,5,4,6,0,1,7]
        badcolors = [] #3,2,5,4,6,0,1,7]
        for i in range(len(actualorder)):
            (r,g,b) = pixels[i]
            r = max(0.0, min(r, 1023.0))
            g = max(0.0, min(g, 1023.0))
            b = max(0.0, min(b, 1023.0))
            pixels2[actualorder[i]] = (r,g,b)
        for i in range(len(badcolors)):
            pixels2[badcolors[i]] = (0.0,0.0,0.0)
        return pixels2


    def start(self):
        #server = OSCServer( ("128.174.251.39", 11661) )
        self.server = OSCServer( ("localhost", 11661) )
        self.server.timeout = 0
        self.lastwrite = time.time()        
        self.server.handle_timeout = types.MethodType(handle_timeout, self.server)
        self.server.lastpixels = [(0,0,0)]*MAX_LIGHTS

        self.layers = {}
        self.activepid = 0
        self.lastclear = time.time()
        self.lastwriteany = 0

        self.streamer = StreamPoster()
        self.streamer.start()

        self.server.addMsgHandler( "/setcolors", self.set_colors)
        while True:
            self.each_frame()

        self.server.close()
Example #5
0
class ColorsIn:
    #measured in seconds to fade in 100%
    fadeoutrate = 4.0
    fadeinrate = 4.0
    bootthreshold = 0.01
    maxlayers = 3

    def set_colors(self,path, tags, args, source):
        try:
            chroma = ChromaMessage.fromOSC(tags,args)

            #if we have no records of any streams:
            #if we have a record of a current stream, and no record of this stream:
            if not self.activepid or chroma.pid not in self.layers:
                self.activepid = chroma.pid
                #mark all other layers as fading out
                for pid in self.layers:
                    self.layers[pid].state = Layer.FADEOUT
                self.layers[chroma.pid] = Layer(chroma, 0, Layer.FADEIN)
                self.lastwriteany = time.time() - 1/30.0 #force this frame to fade in at the target framerate: 30fps

            self.layers[chroma.pid].chroma = chroma

            #in any case, we slowly fade out every stream remaining
            #this also writes to the device
            self.updateStream()
            #mark the last time we got a packet
            self.lastwrite=time.time()
        except:
            import traceback
            traceback.print_exc()

    """
        applies fadeout rules to any stream not the main one
        and writes to the device
    """
    def updateStream(self):
        for layer in self.layers.values():
            self.applyFadingRules(layer)
        if len(self.layers) > ColorsIn.maxlayers:
            self.layers = dict( (k, v) for k,v in self.layers.iteritems() if self.shouldWeKeepLayer(v) )
        #apply our opacity rules
        pixels = self.applyOpacity(self.layers.values())
        if not STAGING:
            octoapi.write(pixels)
        if DEBUG:
            for layer in self.layers.values():
                print "PID: %d, OPACITY: %f"%(layer.chroma.pid, layer.opacity)
        self.lastwriteany=time.time()

    def shouldWeKeepLayer(self,layer):
        if layer.state == Layer.FADEOUT and layer.opacity < self.bootthreshold:
            return False
        return True

    def applyFadingRules(self,layer):
        diff = time.time() - self.lastwriteany
        print diff
        fadein = (1.0/ColorsIn.fadeinrate) * diff
        fadeout = (1.0/ColorsIn.fadeoutrate) * diff
        if layer.state == Layer.FADEIN:
            layer.opacity += fadein 
        if layer.state == Layer.FADEOUT:
            layer.opacity -= fadeout 
        layer.opacity = clampv(0,layer.opacity,1)
        return layer

    """
        compresses down the entire list and gives us the final result
    """
    def applyOpacity(self,layers):
        data = [(0.0,0.0,0.0)] * len(layers[0].chroma.data)
        for layer in layers:
            data = [sum(currentvalue,mult(value,layer.opacity)) for currentvalue,value in itertools.izip(data,layer.chroma.data)]
        data = [clampColor(value) for value in data]
        return data

    def handleClearing(self):
        if time.time() - self.lastwrite > CLEAR_TIME and time.time() - self.lastclear > 1/30.0:
            #print "CLEARING! %f"%time.time()
            self.activepid = 0
            for layer in self.layers.values(): layer.state = Layer.FADEOUT
            shouldweupdate = False
            #don't bother updating if we're below the threshold. not often anyway.
            for layer in self.layers.values():
                if layer.opacity > self.bootthreshold:
                    shouldweupdate = True
            if shouldweupdate: 
                self.updateStream()
            else:
                if not STAGING: 
                    octoapi.write([(0,0,0)]*24)
                if DEBUG:
                    print "CLEARED! %f"%time.time()
            self.lastclear = time.time()

    def each_frame(self):
        self.server.timed_out = False
        while not self.server.timed_out:
            self.handleClearing()
            self.server.handle_request()

    def start(self):
        #server = OSCServer( ("128.174.251.39", 11661) )
        self.server = OSCServer( ("localhost", 11661) )
        self.server.timeout = 0
        self.lastwrite = time.time()        
        self.server.handle_timeout = types.MethodType(handle_timeout, self.server)
        self.server.lastpixels = [(0,0,0)]*24

        self.layers = {}
        self.activepid = 0
        self.lastclear = time.time()
        self.lastwriteany = 0

        self.server.addMsgHandler( "/setcolors", self.set_colors)
        while True:
            self.each_frame()

        self.server.close()
Example #6
0
class ColorsInSucks:
    #measured in seconds to fade in 100%
    fadeoutrate = 4.0
    fadeinrate = 4.0
    bootthreshold = 0.01
    maxlayers = 3

    def set_colors(self, path, tags, args, source):
        try:
            chroma = ChromaMessage.fromOSC(tags, args)

            #if we have no records of any streams:
            #if we have a record of a current stream, and no record of this stream:
            if not self.activepid or chroma.pid not in self.layers:
                self.activepid = chroma.pid
                #mark all other layers as fading out
                for pid in self.layers:
                    self.layers[pid].state = Layer.FADEOUT
                self.layers[chroma.pid] = Layer(chroma, 0, Layer.FADEIN)
                self.lastwriteany = time.time(
                ) - 1 / 30.0  #force this frame to fade in at the target framerate: 30fps

            self.layers[chroma.pid].chroma = chroma

            #in any case, we slowly fade out every stream remaining
            #this also writes to the device
            self.updateStream()
            #mark the last time we got a packet
            self.lastwrite = time.time()
        except:
            import traceback
            traceback.print_exc()

    """
        applies fadeout rules to any stream not the main one
        and writes to the device
    """

    def updateStream(self):
        for layer in self.layers.values():
            self.applyFadingRules(layer)
        if len(self.layers) > ColorsIn.maxlayers:
            self.layers = dict((k, v) for k, v in self.layers.iteritems()
                               if self.shouldWeKeepLayer(v))
        #apply our opacity rules
        pixels = self.applyOpacity(self.layers.values())
        if not STAGING and self.activepid:
            self.streamer.pixels = pixels
            self.streamer.metadata = self.layers[self.activepid].chroma
            #pixels = self.crazyMofoingReorderingOfLights(pixels)
            octoapi.write(pixels)
        if DEBUG:
            for layer in self.layers.values():
                print "PID: %d, OPACITY: %f" % (layer.chroma.pid,
                                                layer.opacity)
        self.lastwriteany = time.time()

    def shouldWeKeepLayer(self, layer):
        if layer.state == Layer.FADEOUT and layer.opacity < self.bootthreshold:
            return False
        return True

    def applyFadingRules(self, layer):
        diff = time.time() - self.lastwriteany
        print diff
        fadein = (1.0 / ColorsIn.fadeinrate) * diff
        fadeout = (1.0 / ColorsIn.fadeoutrate) * diff
        if layer.state == Layer.FADEIN:
            layer.opacity += fadein
        if layer.state == Layer.FADEOUT:
            layer.opacity -= fadeout
        layer.opacity = clampv(0, layer.opacity, 1)
        return layer

    """
        compresses down the entire list and gives us the final result
    """

    def applyOpacity(self, layers):
        data = [(0.0, 0.0, 0.0)] * len(layers[0].chroma.data)
        for layer in layers:
            data = [
                sum(currentvalue, mult(value, layer.opacity))
                for currentvalue, value in itertools.izip(
                    data, layer.chroma.data)
            ]
        data = [clampColor(value) for value in data]
        return data

    def handleClearing(self):
        if time.time() - self.lastwrite > CLEAR_TIME and time.time(
        ) - self.lastclear > 1 / 30.0:
            #print "CLEARING! %f"%time.time()
            self.activepid = 0
            for layer in self.layers.values():
                layer.state = Layer.FADEOUT
            shouldweupdate = False
            #don't bother updating if we're below the threshold. not often anyway.
            for layer in self.layers.values():
                if layer.opacity > self.bootthreshold:
                    shouldweupdate = True
            if shouldweupdate:
                self.updateStream()
            else:
                if not STAGING:
                    octoapi.write([(0, 0, 0)] * MAX_LIGHTS)
                if DEBUG:
                    print "CLEARED! %f" % time.time()
            self.lastclear = time.time()

    def each_frame(self):
        self.server.timed_out = False
        while not self.server.timed_out:
            self.handleClearing()
            self.server.handle_request()

    def crazyMofoingReorderingOfLights(self, pixels):
        pixels2 = pixels[:]  #make a copy so we don't kerplode someone's work
        """
        what are we expecting? we want the back left (by the couches) of the room to be pixel 0, 
        and by the front is the last row
        whereas in reality, it's the opposite.
        here is the order it'd be nice to have them in:
        
        0  1  2  3
        4  5  6  7
        8  9  10 11
        12 13 14 15
        16 17 18 19
        20 21 22 23

        this is the actual order:   
        23 22 21 20
        19 16 17 18
        15 14 13 12
        11 10 9  8
        3  2  5  4
        6 *0**1**7*   *=not there
        """

        actualorder = [
            23, 22, 21, 20, 19, 16, 17, 18, 15, 14, 13, 12, 11, 10, 9, 8, 3, 2,
            5, 4, 6, 0, 1, 7
        ]
        badcolors = []  #3,2,5,4,6,0,1,7]
        for i in range(len(actualorder)):
            (r, g, b) = pixels[i]
            r = max(0.0, min(r, 1023.0))
            g = max(0.0, min(g, 1023.0))
            b = max(0.0, min(b, 1023.0))
            pixels2[actualorder[i]] = (r, g, b)
        for i in range(len(badcolors)):
            pixels2[badcolors[i]] = (0.0, 0.0, 0.0)
        return pixels2

    def start(self):
        #server = OSCServer( ("128.174.251.39", 11661) )
        self.server = OSCServer(("localhost", 11661))
        self.server.timeout = 0
        self.lastwrite = time.time()
        self.server.handle_timeout = types.MethodType(handle_timeout,
                                                      self.server)
        self.server.lastpixels = [(0, 0, 0)] * MAX_LIGHTS

        self.layers = {}
        self.activepid = 0
        self.lastclear = time.time()
        self.lastwriteany = 0

        self.streamer = StreamPoster()
        self.streamer.start()

        self.server.addMsgHandler("/setcolors", self.set_colors)
        while True:
            self.each_frame()

        self.server.close()