aboutsummaryrefslogtreecommitdiff
path: root/LightInstallation.py
blob: 0c24981a25a25f28dd4c63fb28d425bd13c23ea9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from xml.etree.ElementTree import ElementTree
from Screen import Screen
from PixelStrip import PixelStrip
import pdb, sys, time, Util
from pygame.locals import *
#Python class to instantiate and drive a Screen through different patterns,
#and effects.
class LightInstallation:
    def __init__(self, configFileName):
        self.inputs = {} #dict of inputs and their bound behaviors, keyed by InputId
        self.behaviors = {}
        config = ElementTree()
        config.parse(configFileName)
        self.screen = Screen()
        rendererConfig = config.find('RendererConfiguration')
        layoutConfig = config.find('LayoutConfiguration')
        inputConfig = config.find('InputConfiguration')
        behaviorConfig = config.find('BehaviorConfiguration')
        self.initializeLights(layoutConfig)
        self.initializeRenderers(rendererConfig)
        self.initializeInputs(inputConfig)
        self.initializeBehaviors(behaviorConfig)
        
        self.mainLoop()
    def initializeLights(self, layoutConfig):
        layoutEngines = self.initializeComponent(layoutConfig)
        [self.addPixelStrip(l) for l in layoutEngines]
    def addPixelStrip(self, layoutEngine):
        pixelStrip = PixelStrip(layoutEngine)
        self.screen.addStrip(pixelStrip)
    def initializeInputs(self, inputConfig):
        inputs = self.initializeComponent(inputConfig)
        for inputClass in inputs:
            inputClass.start()
            self.inputs[inputClass.argDict['InputId']] = (inputClass, [])
    def initializeRenderers(self, rendererConfig):
        self.renderers = self.initializeComponent(rendererConfig) 
        print self.renderers
    def initializeComponent(self, config):
        components = []
        if config != None:
            for configItem in config.getchildren():
                className = configItem.find('Class').text  
                exec('from ' + className + ' import ' + className)
                args = Util.generateArgDict(configItem.find('Args'))
                args['parentScope'] = self
                components.append(eval(className+'(args)')) #TODO: doesn't error
                #right
        return components
    def alive(self):
        return True
    def mainLoop(self):
        #self.screen.allOn()
        while 1:
            time.sleep(.1)
            responses = []
            for behaviorId in self.behaviors:
                [responses.append(b) for b in \
                    self.behaviors[behaviorId].timeStep()] #processes all queued inputs
            [self.screen.respond(response) for response in responses if
                    response != []]
            self.screen.timeStep()
            if responses != []:
                print responses
            [r.render(self.screen) for r in self.renderers]
    def initializeBehaviors(self, behaviorConfig):
        behaviors = self.initializeComponent(behaviorConfig)
        for behavior in behaviors:
            print behavior.argDict
            self.addBehavior(behavior)
        print self.inputs
        print self.behaviors
    def addBehavior(self, behavior):
        self.behaviors[behavior.argDict['behaviorId']] = behavior
        for inputId in behavior.argDict['Inputs']:
            self.inputs[inputId][1].append(behavior.argDict['behaviorId'])
    def processResponse(self,inputDict, responseDict):
        #pdb.set_trace()
        inputId = inputDict['InputId']
        boundBehaviors = self.inputs[inputId][1]
        [self.behaviors[b].addInput(responseDict) for b in boundBehaviors]

def main(argv):
    print argv
    if len(argv) == 1:
        l = LightInstallation('LightInstallationConfig.xml')
    else:
        l = LightInstallation(argv[1])
if __name__ == "__main__":
    main(sys.argv)