aboutsummaryrefslogtreecommitdiff
path: root/LightInstallation.py
blob: aaedcf2267ed3227d1b55f4ac85ce646b2344782 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from xml.etree.ElementTree import ElementTree
from pixelcore.Screen import * 
from pixelcore.PixelStrip import *
import pdb, sys, time, thread
from pygame.locals import *
import util.TimeOps as clock
import util.Config as configGetter 
import util.ComponentRegistry as compReg
from logger import main_log
#Python class to instantiate and drive a Screen through different patterns,
#and effects.
class LightInstallation:
    def __init__(self, configFileName):
        main_log.info("System Initialization began based on: " + str(configFileName))
        self.timer = clock.Stopwatch()
        self.timer.start()
        self.inputs = {} #dict of inputs and their bound behaviors, keyed by InputId
        self.behaviors = {}
        self.lock = thread.allocate_lock()
        self.behaviorOutputs = {} #key: [list of output destinations]
        self.behaviorInputs = {}
        self.componentDict = {}
        self.inputBehaviorRegistry = {} #inputid -> behaviors listening to that
        #input
        self.screen = Screen()
        compReg.initRegistry()
        compReg.registerComponent(self.screen, 'Screen') #TODO: move to constants file
        config = configGetter.loadConfigFile(configFileName)
        #read configs from xml
        rendererConfig = config.find('RendererConfiguration')
        pixelConfig = config.find('PixelConfiguration')
        inputConfig = config.find('InputConfiguration')
        behaviorConfig = config.find('BehaviorConfiguration')
        mapperConfig = config.find('PixelMapperConfiguration')

        installationConfig = config.find('InstallationConfiguration')
        #inits
        self.initializeScreen(pixelConfig)
        self.initializeRenderers(rendererConfig)
        self.initializeInputs(inputConfig)
        self.initializeBehaviors(behaviorConfig)
        self.initializeMapper(mapperConfig)
        main_log.info('All components initialized')
        #registration in dict
        self.registerComponents(self.renderers)
        self.registerComponents(self.inputs)
        self.registerComponents(self.behaviors)
        self.registerComponents(self.mappers)
        self.configureInstallation(installationConfig)
        #Done initializing.  Lets start this thing!
        self.timer.stop()
        #main_log.info('Initialization done.  Time: ', self.timer.elapsed(), 'ms')
        self.mainLoop()
    def configureInstallation(self, installationConfig):
        defaults = configGetter.generateArgDict(installationConfig.find('Defaults'))
        for defaultSelection in defaults:
            componentToMap = compReg.getComponent(defaults[defaultSelection])
            compReg.registerComponent(compReg.getComponent(defaults[defaultSelection]),\
                'Default'+defaultSelection)
            main_log.debug('Default Set: ' + defaultSelection + 'set to ' +\
                defaults[defaultSelection])

    def initializeMapper(self, mapperConfig):
        self.mappers = self.initializeComponent(mapperConfig) 
    def initializeScreen(self, layoutConfig):
        pixelAssemblers = self.initializeComponent(layoutConfig)
        [self.addPixelStrip(l) for l in pixelAssemblers]
    def addPixelStrip(self, layoutEngine):
        pixelStrip = PixelStrip(layoutEngine)
        self.screen.addStrip(pixelStrip)
    def initializeInputs(self, inputConfig):
        inputs = self.initializeComponent(inputConfig)
        self.inputs = inputs
        for inputClass in inputs:
            inputClass.start()
            self.inputBehaviorRegistry[inputClass['Id']] = []
            #empty list is list of bound behaviors
    def initializeRenderers(self, rendererConfig):
        self.renderers = self.initializeComponent(rendererConfig) 
    def registerComponents(self, components):
        for component in components:
            cid = component['Id']
            if cid == None:  #TODO: determine if componenent is critical, and if so, die
                main_log.error('Components must be registered with Ids.  Component not registered')
            else:
                compReg.registerComponent(component)
                main_log.debug(cid + ' registered')
    def initializeComponent(self, config):
        components = []
        if config != None:
            for configItem in config.getchildren():
                try:
                    [module,className] = configItem.find('Class').text.split('.')
                except:
                    main_log.error('Module must have Class element')
                    main_log.warn('Module without class element.  Module not initialized')
                    continue
                try:
                    exec('from ' + module+'.'+className + ' import *')
                    main_log.debug(module +'.' +className + 'imported')
                except Exception as inst:
                    main_log.error('Error importing ' + module+'.'+'.className.  Component not\
                    initialized.')
                    main_log.error(str(inst)) 
                    continue 
                args = configGetter.pullArgsFromItem(configItem)
                args['parentScope'] = self #TODO: we shouldn't give away scope
                #like this, find another way.
                try:
                    components.append(eval(className+'(args)')) 
                    main_log.debug(className + 'initialized with args ' + str(args))
                #right
                except Exception as inst:
                    main_log.error('Failure while initializing ' + className + ' with ' + str(args))
                    main_log.error(str(inst)) 
                
        return components
    def alive(self):
        return True
    def mainLoop(self):
        lastLoopTime = clock.time()
        refreshInterval = 30
        runCount = 200 
        while runCount > 0:
            runCount -= 1
            loopStart = clock.time()
            responses = self.evaluateBehaviors() #inputs are all queued when they
            #happen, so we only need to run the behaviors
            self.timer.start()
            [self.screen.respond(response) for response in responses if
                    response != []]
            self.screen.timeStep()
            [r.render(self.screen, loopStart) for r in self.renderers]
            loopElapsed = clock.time()-loopStart
            sleepTime = max(0,refreshInterval-loopElapsed)
            self.timer.stop()
            #print self.timer.elapsed()
            if sleepTime > 0:
                time.sleep(sleepTime/1000)
    #evaluates all the behaviors (including inter-dependencies) and returns a
    #list of responses to go to the screen.
    def evaluateBehaviors(self):
        responses = {}
        responses['Screen'] = [] #responses to the screen
        for behavior in self.behaviors:
            responses[behavior['Id']] = behavior.timeStep()
            if behavior['RenderToScreen'] == True: #TODO: this uses extra space,
            #we can use less in the future if needbe.
                responses['Screen'] += responses[behavior['Id']]
        return responses['Screen']

    def initializeBehaviors(self, behaviorConfig):
        self.behaviors = self.initializeComponent(behaviorConfig)
        for behavior in self.behaviors:
            self.addBehavior(behavior)
    #Does work needed to add a behavior: currently -- maps behavior inputs into
    #the input behavior registry.
    def addBehavior(self, behavior):
        for inputId in behavior.argDict['Inputs']:
            if inputId in self.inputBehaviorRegistry: #it could be a behavior
                self.inputBehaviorRegistry[inputId].append(behavior['Id'])
    def processResponse(self,inputDict, responseDict):
        inputId = inputDict['Id']
        boundBehaviorIds = self.inputBehaviorRegistry[inputId]
        #TODO: fix this, it crashes because inputs get run before beahviors exist 
        try:
            [compReg.getComponent(b).addInput(responseDict) for b in boundBehaviorIds]
        except:
            pass
            #print 'Behaviors not initialized yet.  WAIT!' 
def main(argv):
    if len(argv) == 1:
        l = LightInstallation('LightInstallationConfig.xml')
    else:
        l = LightInstallation(argv[1])
if __name__ == "__main__":
    try:
        main(sys.argv)
    except KeyboardInterrupt:
        main_log.info('Terminated by keyboard.')