1
2
3 from xml.etree.ElementTree import ElementTree
4 from pixelcore.Screen import *
5 from pixelcore.PixelStrip import *
6 import pdb, sys, time, thread
7 import util.TimeOps as clock
8 import util.Config as configGetter
9 import util.ComponentRegistry as compReg
10 import util.BehaviorQuerySystem as bqs
11 from logger import main_log
12
13
16 main_log.info("System Initialization began based on: " + str(configFileName))
17 self.timer = clock.Stopwatch()
18 self.timer.start()
19 self.inputs = {}
20 self.behaviors = {}
21 self.lock = thread.allocate_lock()
22 self.behaviorOutputs = {}
23 self.behaviorInputs = {}
24 self.componentDict = {}
25 self.inputBehaviorRegistry = {}
26 self.dieNow = False
27
28 self.screen = Screen()
29 compReg.initRegistry()
30 compReg.registerComponent(self.screen, 'Screen')
31
32 bqs.initBQS()
33
34 config = configGetter.loadConfigFile(configFileName)
35
36 rendererConfig = config.find('RendererConfiguration')
37 self.initializeRenderers(rendererConfig)
38
39 pixelConfig = config.find('PixelConfiguration')
40 self.initializeScreen(pixelConfig)
41
42 inputConfig = config.find('InputConfiguration')
43 self.initializeInputs(inputConfig)
44
45 behaviorConfig = config.find('BehaviorConfiguration')
46 self.initializeBehaviors(behaviorConfig)
47
48 mapperConfig = config.find('PixelMapperConfiguration')
49 self.initializeMapper(mapperConfig)
50
51
52 main_log.info('All components initialized')
53
54 self.registerAllComponents()
55
56 installationConfig = config.find('InstallationConfiguration')
57 self.configureInstallation(installationConfig)
58
59 self.timer.stop()
60
61 self.mainLoop()
62
69
70
79
82
86
90
97
98
101
107 components = []
108 if config != None:
109 for configItem in config.getchildren():
110 try:
111 [module,className] = configItem.find('Class').text.split('.')
112 except:
113 main_log.error('Module must have Class element')
114 continue
115 try:
116 exec('from ' + module+'.'+className + ' import *')
117 main_log.debug(module +'.' +className + 'imported')
118 except Exception as inst:
119 main_log.error('Error importing ' + module+'.'+className+ '. Component not\
120 initialized.')
121 main_log.error(str(inst))
122 continue
123 args = configGetter.pullArgsFromItem(configItem)
124 args['parentScope'] = self
125 try:
126 new_component = eval(className+'(args)')
127 new_component.addDieListener(self)
128 components.append(new_component)
129 main_log.info(className + 'initialized with args ' + str(args))
130 except Exception as inst:
131 main_log.error('Failure while initializing ' + className + ' with ' + str(args))
132 main_log.error(str(inst))
133
134 return components
135
138
139 - def mainLoop(self):
140 lastLoopTime = clock.time()
141 refreshInterval = 30
142 while not self.dieNow:
143 loopStart = clock.time()
144 responses = self.evaluateBehaviors()
145 self.timer.start()
146 [self.screen.respond(response) for response in responses if
147 response != []]
148 self.screen.timeStep(loopStart)
149 [r.render(self.screen, loopStart) for r in self.renderers]
150 loopElapsed = clock.time()-loopStart
151 sleepTime = max(0,refreshInterval-loopElapsed)
152 main_log.debug('Loop complete in ' + str(loopElapsed) + 'ms. Sleeping for ' +\
153 str(sleepTime))
154 self.timer.stop()
155 if sleepTime > 0:
156 time.sleep(sleepTime/1000)
157
159 """Evaluates all the behaviors (including inter-dependencies) and returns a list of responses to
160 go to the screen"""
161 responses = {}
162 responses['Screen'] = []
163 for behavior in self.behaviors:
164 if behavior['RenderToScreen'] == True:
165 responses[behavior['Id']] = behavior.timeStep()
166 responses['Screen'] += responses[behavior['Id']]
167 return responses['Screen']
168
174
176 """Does work needed to add a behavior: currently -- maps behavior inputs into the input behavior
177 registry"""
178 for inputId in behavior.argDict['Inputs']:
179 if inputId in self.inputBehaviorRegistry:
180 self.inputBehaviorRegistry[inputId].append(behavior['Id'])
181
183 inputId = inputDict['Id']
184 boundBehaviorIds = self.inputBehaviorRegistry[inputId]
185 try:
186 [compReg.getComponent(b).addInput(responseDict) for b in boundBehaviorIds]
187 except:
188 pass
189
190
193
195 if len(argv) == 1:
196 l = LightInstallation('config/6thFloor.xml')
197 else:
198 l = LightInstallation(argv[1])
199
200 if __name__ == "__main__":
201 try:
202 main(sys.argv)
203 except KeyboardInterrupt:
204 main_log.info('Terminated by keyboard.')
205