Package SmootLight :: Package pixelcore :: Module Screen
[hide private]
[frames] | no frames]

Source Code for Module SmootLight.pixelcore.Screen

  1  from pixelcore.Pixel import *  
  2  from pixelcore.PixelStrip import * 
  3  from operationscore.PixelEvent import * 
  4  from operationscore.PixelMapper import * 
  5  import util.Search as Search 
  6  import util.ComponentRegistry as compReg 
  7  import util.Strings as Strings 
  8  import util.TimeOps as timeops 
  9  import itertools 
 10  import sys 
 11  import pdb 
 12  from logger import main_log 
13 -class Screen:
14 """Class representing a collection of Pixels grouped into PixelStrips. Needs a 15 PixelMapper, currently set via setMapper by may be migrated into the argDict.""" 16
17 - def __init__(self):
18 self.responseQueue = [] 19 self.pixelStrips = [] 20 self.xSortedPixels = [] 21 self.xPixelLocs = [] 22 sizeValid = False 23 self.pixelsSorted = False
24
25 - def addStrip(self, strip):
26 self.pixelStrips.append(strip) 27 self.sizeValid = False #keep track of whether or not our screen size has 28 self.pixelsSorted = False
29 #been invalidated by adding more pixels 30
31 - def pixelsInRange(self, minX, maxX):
32 """Returns (pixelIndex, pixel). Does a binary search. Sorts first if neccesary.""" 33 if not self.pixelsSorted: 34 self.computeXSortedPixels() 35 minIndex = Search.find_ge(self.xPixelLocs, minX) 36 maxIndex = Search.find_le(self.xPixelLocs, maxX)+1 37 return self.xSortedPixels[minIndex:maxIndex]
38
39 - def computeXSortedPixels(self):
40 self.xSortedPixels = [] 41 for pixel in self: 42 self.xSortedPixels.append((pixel.location[0], pixel)) 43 self.xSortedPixels.sort() 44 self.xPixelLocs = [p[0] for p in self.xSortedPixels] 45 self.pixelsSorted = True
46
47 - def __iter__(self): #the iterator of all our pixel strips chained togther
48 return itertools.chain(*[strip.__iter__() for strip in \ 49 self.pixelStrips]) #the * operator breaks the list into args
50 51 #SUBVERTING DESIGN FOR EFFICIENCY 1/24/11, RCOH -- It would be cleaner to store the time on the responses 52 #themselves, however, it is faster to just pass it in.
53 - def timeStep(self, currentTime=None):
54 """Increments time -- This processes all queued responses, adding that to a queue that will 55 be processed on the next time step.""" 56 if currentTime == None: 57 currentTime = timeops.time() 58 tempQueue = list(self.responseQueue) 59 self.responseQueue = [] 60 for response in tempQueue: 61 self.processResponse(response, currentTime)
62 63 #public
64 - def respond(self, responseInfo):
65 self.responseQueue.append(responseInfo)
66
67 - def getSize(self):
68 """Returns the size of the screen in the form: (minx, miny, maxx, maxy)""" 69 if self.sizeValid: 70 return self.size 71 (minX, minY, maxX, maxY) = (sys.maxint,sys.maxint,-sys.maxint,-sys.maxint) 72 for light in self: 73 (x,y) = light.location 74 75 minX = min(x, minX) 76 maxX = max(x, maxX) 77 78 minY = min(y, minY) 79 maxY = max(y, maxY) 80 self.size = (0,0, maxX, maxY) 81 self.sizeValid = True 82 return (minX, minY, maxX, maxY)
83 84 #private
85 - def processResponse(self, responseInfo, currentTime=None): #we need to make a new dict for
86 #each to prevent interference 87 if currentTime == None: 88 currentTime = timeops.time() 89 if type(responseInfo) != type(dict()): 90 pass 91 if 'Mapper' in responseInfo: 92 mapper = compReg.getComponent(responseInfo['Mapper']) 93 else: 94 mapper = compReg.getComponent(Strings.DEFAULT_MAPPER) 95 pixelWeightList = mapper.mapEvent(responseInfo['Location'], self) 96 main_log.debug('Screen processing response. ' + str(len(pixelWeightList)) + ' events\ 97 generated') 98 PixelEvent.addPixelEventIfMissing(responseInfo) 99 for (pixel, weight) in pixelWeightList: 100 pixel.processInput(responseInfo['PixelEvent'], 0,weight, currentTime) #TODO: z-index 101