From 83242972c09032eb89dd547f3ff3c4dcc2693555 Mon Sep 17 00:00:00 2001 From: rcoh Date: Mon, 14 Feb 2011 01:51:43 -0500 Subject: Fixed some threading bugs. Modified LightInstallation.py so that Behaviors are *Only* run if they are marked to be rendered. Fixed a threading issue tied to the component registry. --- LightInstallation.py | 6 +- behaviors/TimeSwitch.py | 11 +-- behaviors/VerticalBar.py | 11 +-- config/C5Sign.xml | 155 ++++++++++++++++++-------------------- inputs/ContinuousCenterInput.py | 3 +- inputs/ContinuousLocationInput.py | 2 + renderers/PygameRenderer.py | 2 +- util/ComponentRegistry.py | 17 +++-- 8 files changed, 100 insertions(+), 107 deletions(-) diff --git a/LightInstallation.py b/LightInstallation.py index cca1588..91be752 100755 --- a/LightInstallation.py +++ b/LightInstallation.py @@ -103,9 +103,6 @@ class LightInstallation(object): for component in components: cid = compReg.registerComponent(component) main_log.info(cid + ' registered') - compReg.registerComponent(component) - main_log.info(cid + ' registered') - def initializeComponent(self, config): components = [] if config != None: @@ -164,8 +161,8 @@ class LightInstallation(object): responses = {} responses['Screen'] = [] #responses to the screen for behavior in self.behaviors: - responses[behavior['Id']] = behavior.timeStep() if behavior['RenderToScreen'] == True: + responses[behavior['Id']] = behavior.timeStep() responses['Screen'] += responses[behavior['Id']] return responses['Screen'] @@ -184,6 +181,7 @@ class LightInstallation(object): def processResponse(self,inputDict, responseDict): inputId = inputDict['Id'] + #coming from. boundBehaviorIds = self.inputBehaviorRegistry[inputId] try: [compReg.getComponent(b).addInput(responseDict) for b in boundBehaviorIds] diff --git a/behaviors/TimeSwitch.py b/behaviors/TimeSwitch.py index 2cedfcf..cfbfe4a 100644 --- a/behaviors/TimeSwitch.py +++ b/behaviors/TimeSwitch.py @@ -10,15 +10,16 @@ class TimeSwitch(Behavior): """ def behaviorInit(self): self.keyIndex = 0 - self.currentBehaviorId = self['Behaviors'].keys()[self.keyIndex] + self.currentBehaviorId = self['TimeMap'].keys()[self.keyIndex] self.behaviorStart = clock.time() def processResponse(self, sensors, recurs): - if self.behaviorStart + self['Behaviors'][self.currentBehaviorId]*1000 <= clock.time(): + if self.behaviorStart + self['TimeMap'][self.currentBehaviorId]*1000 <= clock.time(): self.keyIndex += 1 - self.keyIndex = self.keyIndex % len(self['Behaviors']) - self.currentBehaviorId = self['Behaviors'].keys()[self.keyIndex] + self.keyIndex = self.keyIndex % len(self['TimeMap']) + self.currentBehaviorId = self['TimeMap'].keys()[self.keyIndex] self.behaviorStart = clock.time() main_log.info('Switching behaviors') - return compReg.getComponent(self.currentBehaviorId).processResponse(sensors, recurs) + sensors = [s for s in sensors if s['InputId'] == self['InputMap'][self.currentBehaviorId]] + return compReg.getComponent(self.currentBehaviorId).immediateProcessInput(sensors, recurs) diff --git a/behaviors/VerticalBar.py b/behaviors/VerticalBar.py index e1a67fe..66c8e56 100644 --- a/behaviors/VerticalBar.py +++ b/behaviors/VerticalBar.py @@ -2,11 +2,11 @@ from operationscore.Behavior import * class VerticalBar(Behavior): def processResponse(self, inputs, recurs): - ret = [] + inputs = list(inputs) for inputset in inputs: #import pdb; pdb.set_trace() - + inputset = dict(inputset) if 'xLoc' not in inputset: inputset['xLoc'] = inputset['Location'][0] xLoc = inputset['xLoc'] @@ -19,12 +19,5 @@ class VerticalBar(Behavior): inputset['Location'] = condition ret.append(inputset) - return (ret, []) - def setLastOutput(self, output): - - coutput = Behavior.deepCopyPacket(output) - for data in coutput: - data['Location'] = data['xLoc'] - return coutput diff --git a/config/C5Sign.xml b/config/C5Sign.xml index d4abdc1..a05cb1e 100644 --- a/config/C5Sign.xml +++ b/config/C5Sign.xml @@ -26,22 +26,30 @@ 1 + + pixelmappers.C5SignMapper + + c5signmapper + 20 + + renderers/C5Renderer.xml - + renderers/Pygame.xml - inputs.PygameInput + inputs.ContinuousLocationInput - pygameclick - 10 - True + centerleft + left + center + 1800 @@ -72,20 +80,9 @@ inputs.ContinuousCenterInput center - 1800 + 700 - - - inputs/MouseFollower.xml - @@ -94,15 +91,56 @@ behaviors.TouchOSC - - behaviors/RandomColor.xml + + behaviors/PixelDecay.xml + + .001 + + + + behaviors.ModifyParam + + incrVertBarLoc + xLoc + {val}+1 + + + + behaviors.VerticalBar - + vertBar + + behaviors.BehaviorChain + + bar_move + + vertBar + incrVertBarLoc + recursivedecay + + + + + behaviors.BehaviorChain + + scanningbars + + centerleft + + + colorchange + mover + slowdecay + + {'mover':'bar_move'} + False + + + + behaviors/RandomColor.xml + behaviors.BehaviorChain @@ -115,7 +153,7 @@ decay gaussmap - True + False @@ -124,12 +162,6 @@ behaviors/SingleFrame.xml - - behaviors/PixelDecay.xml - - .01 - - behaviors.XYMove @@ -168,24 +200,18 @@ - - behaviors.ModifyParam - - ysin - YStep - Sensor - 4*math.sin({x}/float(40)) - - behaviors.TimeSwitch main + centerleft center - {'runcolordecay':10,'expandingcircles':10} - False + {'scanningbars':10,'runcolordecay':10,'expandingcircles':10} + {'scanningbars':'centerleft', 'runcolordecay':'center',\ + 'expandingcircles':'center'} + True @@ -206,10 +232,10 @@ 20 - + behaviors/LoopAndDie.xml - + behaviors/LoopAndDie.xml @@ -233,45 +259,12 @@ mover - - behaviors.RandomWalk - - randmovement - 2 - - - - behaviors/Accelerate.xml - - - behaviors.EchoBehavior - - echo - 0 - False - - behaviors.ColorShift colorshift - - behaviors.BehaviorChain - - mousechaser - - - - echo - innercircle - outercircle - singleframe - - False - - behaviors.Circle @@ -292,7 +285,7 @@ behaviors.ModifyParam incrinner - {val}+.3 + {val}+.6 innercircleRadius @@ -300,7 +293,7 @@ behaviors.ModifyParam incrouter - {val}+.3 + {val}+.6 outercircleRadius @@ -324,13 +317,11 @@ colorchange mover - singleframe + decay + {'mover':'circle_expand'} - - behaviors/RunningBehavior.xml - diff --git a/inputs/ContinuousCenterInput.py b/inputs/ContinuousCenterInput.py index a7635b2..88534f0 100644 --- a/inputs/ContinuousCenterInput.py +++ b/inputs/ContinuousCenterInput.py @@ -4,9 +4,10 @@ import util.Strings as Strings from operationscore.Input import * class ContinuousCenterInput(Input): def inputInit(self): + compReg.getLock().acquire() minX,minY,maxX,maxY = compReg.getComponent('Screen').getSize() + compReg.getLock().release() self.center = ((minX+maxX) / 2, (minY+maxY) / 2) - print self.center def sensingLoop(self): self.respond({Strings.LOCATION: self.center}) diff --git a/inputs/ContinuousLocationInput.py b/inputs/ContinuousLocationInput.py index 52a36ad..72ca8f0 100644 --- a/inputs/ContinuousLocationInput.py +++ b/inputs/ContinuousLocationInput.py @@ -8,7 +8,9 @@ class ContinuousLocationInput(Input): def inputInit(self): xvals = {} yvals = {} + compReg.getLock().acquire() xvals['left'], yvals['bottom'], xvals['right'], yvals['top'] = compReg.getComponent('Screen').getSize() + compReg.getLock().release() (xvals['center'], yvals['center']) = ((xvals['left']+xvals['right']) / 2, (yvals['top']+yvals['bottom']) / 2) self.location = (xvals[self['xloc']], yvals[self['yloc']]) diff --git a/renderers/PygameRenderer.py b/renderers/PygameRenderer.py index 01e2615..8be272c 100644 --- a/renderers/PygameRenderer.py +++ b/renderers/PygameRenderer.py @@ -25,7 +25,7 @@ class PygameRenderer(Renderer): for light in lightSystem: scaledLoc = [l*scale for l in light.location] pygame.draw.circle(self.background, light.state(currentTime), scaledLoc, \ - light.radius) + 5) self.screen.blit(self.background, (0,0)) pygame.display.flip() diff --git a/util/ComponentRegistry.py b/util/ComponentRegistry.py index be913df..3036138 100644 --- a/util/ComponentRegistry.py +++ b/util/ComponentRegistry.py @@ -1,12 +1,18 @@ import pdb import hashlib from logger import main_log +import thread #TODO: make component registry a singleton def initRegistry(): #TODO: don't overwrite existing registry if not 'Registry' in globals(): globals()['Registry'] = {} - + makelock() + + +def makelock(): + global utilLock + utilLock = thread.allocate_lock() def clearRegistry(): initRegistry() @@ -14,6 +20,9 @@ def removeComponent(cid): global Registry Registry.pop(cid) +def getLock(): + global utilLock + return utilLock def getComponent(cid): global Registry return Registry[cid] @@ -31,6 +40,8 @@ def registerComponent(component, cid=None): cid = getNewId() component['Id'] = cid main_log.debug(cid + 'automatically assigned') + if cid in Registry: + import pdb; pdb.set_trace() Registry[cid] = component return cid @@ -42,10 +53,6 @@ def removeComponent(cid): global Registry Registry.pop(cid) -def getComponent(cid): - global Registry - return Registry[cid] - def getNewId(): global Registry trialKey = len(Registry) -- cgit v1.2.3