diff options
Diffstat (limited to 'pixelcore')
-rw-r--r-- | pixelcore/Pixel.py | 47 | ||||
-rw-r--r-- | pixelcore/PixelEventManager.py | 2 | ||||
-rw-r--r-- | pixelcore/PixelStrip.py | 24 | ||||
-rw-r--r-- | pixelcore/Screen.py | 38 |
4 files changed, 48 insertions, 63 deletions
diff --git a/pixelcore/Pixel.py b/pixelcore/Pixel.py index 1fbea2c..4e7cfce 100644 --- a/pixelcore/Pixel.py +++ b/pixelcore/Pixel.py @@ -1,19 +1,21 @@ import util.ColorOps as color +from logger import main_log import pdb from pixelevents.StepEvent import * import util.TimeOps as timeops -#Pixel keeps a queue of events (PixelEvent objects) (actually a dictionary -#keyed by event time). Every time is state is -#requested, it processes all the members of its queue. If a member returns none, -#it is removed from the queue. Otherwise, its value added to the Pixels color -#weighted by z-index. class Pixel: + """Pixel keeps a queue of events (PixelEvent objects) (actually a dictionary + keyed by event time). Every time is state is + requested, it processes all the members of its queue. If a member returns none, + it is removed from the queue. Otherwise, its value added to the Pixels color + weighted by z-index.""" + radius = 2 timeOff = -1 def __init__(self, location): self.location = location - self.events = {} + self.events = [] self.lastRenderTime = timeops.time() self.lastRender = (0,0,0) @@ -24,44 +26,47 @@ class Pixel: #processInput instead. Also, you shouldn't use this anyway. You should be #using the input method on the screen! def turnOnFor(self, time): - event = StepEvent.generate(time, (255,255,255)) #TODO: Move color to + event = StepEvent.generate(time, (255,255,255)) self.processInput(event, 0) - #arg #Add a pixelEvent to the list of active events def processInput(self,pixelEvent,zindex, scale=1,currentTime=None): #consider migrating arg to dict if currentTime == None: currentTime = timeops.time() - self.events[currentTime] = (zindex,scale, pixelEvent) - + self.events.append((currentTime, zindex, scale, pixelEvent)) #TODO: clean this up, maybe? def clearAllEvents(self): - self.events = {} + self.events = [] - #Combines all PixelEvents currently active and computes the current color of - #the pixel. - def state(self, currentTime=timeops.time()): #TODO: this only evaluates at import time, I think + def state(self, currentTime=None): + """Combines all PixelEvents currently active and computes the current color of + the pixel.""" + if currentTime == None: + currentTime = timeops.time() if currentTime-self.lastRenderTime < 5: return self.lastRender - if self.events == {}: + if self.events == []: self.lastRenderTime = currentTime return (0,0,0) deadEvents = [] resultingColor = (0,0,0) colors = [] - for eventTime in self.events: #TODO: right color weighting code - (zindex,scale,event) = self.events[eventTime] - eventResult = event.state(currentTime-eventTime) + for eventObj in self.events: #TODO: right color weighting code + if len(self.events) > 50: + main_log.error('High pixel event count! Investigate!') + eventTime, zindex, scale, pixelEvent = eventObj + eventResult = pixelEvent.state(currentTime-eventTime) if eventResult != None: scaledEvent = color.multiplyColor(eventResult,scale) if (scaledEvent[0] + scaledEvent[1] + scaledEvent[2]) < 5: - deadEvents.append(eventTime) + pass + #deadEvents.append(eventObj) else: colors.append(scaledEvent) else: - deadEvents.append(eventTime) + deadEvents.append(eventObj) resultingColor = color.combineColors(colors) - [self.events.pop(event) for event in deadEvents] + [self.events.remove(event) for event in deadEvents] resultingColor = [int(round(c)) for c in resultingColor] self.lastRender = tuple(resultingColor) self.lastRenderTime = currentTime diff --git a/pixelcore/PixelEventManager.py b/pixelcore/PixelEventManager.py deleted file mode 100644 index 779a0ce..0000000 --- a/pixelcore/PixelEventManager.py +++ /dev/null @@ -1,2 +0,0 @@ -class PixelEventManager(object): - def init(self) diff --git a/pixelcore/PixelStrip.py b/pixelcore/PixelStrip.py index 647991d..29d3b31 100644 --- a/pixelcore/PixelStrip.py +++ b/pixelcore/PixelStrip.py @@ -4,33 +4,17 @@ import util.Geo as Geo from pixelevents.StepEvent import * import math import pdb -#Python class representing a single Pixel strip (usually 50 Pixels) class PixelStrip: + """Python class representing a single Pixel strip (usually 50 Pixels)""" + def __init__(self, layoutEngine): self.initStrip(layoutEngine) self.argDict = layoutEngine.getStripArgs() + def initStrip(self, layoutEngine): pixelLocations = layoutEngine.getPixelLocations() self.pixels = [Pixel(l) for l in pixelLocations] + def __iter__(self): return self.pixels.__iter__() - def allOn(self, time): - [l.turnOnFor(time) for l in self.pixels] #TODO: add test-on method to - #pixels - def respond(self, responseInfo): - location = responseInfo[Strings.LOCATION] - if not 'PixelEvent' in responseInfo: - if 'Color' in responseInfo: - color = responseInfo['Color'] - else: - raise Exception('Need Color. Probably') - responseInfo['PixelEvent'] = StepEvent.generate(300, color) - (dist, pixel) = self.getPixelNearest(location) - pixel.processInput(responseInfo['PixelEvent'], 0) #TODO: z-index - - def getPixelNearest(self, location): - dists = [(Geo.dist(location, pixel.location), pixel) for pixel in self.pixels] - dists.sort() - return dists[0] - #just for now. diff --git a/pixelcore/Screen.py b/pixelcore/Screen.py index a6fc8c4..ada8d4a 100644 --- a/pixelcore/Screen.py +++ b/pixelcore/Screen.py @@ -10,48 +10,49 @@ import itertools import sys import pdb from logger import main_log -#Class representing a collection of Pixels grouped into PixelStrips. Needs a -#PixelMapper, currently set via setMapper by may be migrated into the argDict. class Screen: + """Class representing a collection of Pixels grouped into PixelStrips. Needs a + PixelMapper, currently set via setMapper by may be migrated into the argDict.""" + def __init__(self): self.responseQueue = [] self.pixelStrips = [] self.xSortedPixels = [] self.xPixelLocs = [] sizeValid = False - - def addStrip(self, lS): - self.pixelStrips.append(lS) + self.pixelsSorted = False + + def addStrip(self, strip): + self.pixelStrips.append(strip) self.sizeValid = False #keep track of whether or not our screen size has + self.pixelsSorted = False #been invalidated by adding more pixels - self.computeXSortedPixels() - #Returns (pixelIndex, pixel). Does a binary search. def pixelsInRange(self, minX, maxX): + """Returns (pixelIndex, pixel). Does a binary search. Sorts first if neccesary.""" + if not self.pixelsSorted: + self.computeXSortedPixels() minIndex = Search.find_ge(self.xPixelLocs, minX) maxIndex = Search.find_le(self.xPixelLocs, maxX)+1 return self.xSortedPixels[minIndex:maxIndex] def computeXSortedPixels(self): + self.xSortedPixels = [] for pixel in self: self.xSortedPixels.append((pixel.location[0], pixel)) self.xSortedPixels.sort() self.xPixelLocs = [p[0] for p in self.xSortedPixels] - - #For debug only - def allOn(self): - [lS.allOn(-1) for lS in self.pixelStrips] - + self.pixelsSorted = True + def __iter__(self): #the iterator of all our pixel strips chained togther return itertools.chain(*[strip.__iter__() for strip in \ self.pixelStrips]) #the * operator breaks the list into args - #increment time -- This processes all queued responses. Responses generated - #during this period are added to the queue that will be processed on the next - #time step. #SUBVERTING DESIGN FOR EFFICIENCY 1/24/11, RCOH -- It would be cleaner to store the time on the responses #themselves, however, it is faster to just pass it in. def timeStep(self, currentTime=None): + """Increments time -- This processes all queued responses, adding that to a queue that will + be processed on the next time step.""" if currentTime == None: currentTime = timeops.time() tempQueue = list(self.responseQueue) @@ -64,6 +65,7 @@ class Screen: self.responseQueue.append(responseInfo) def getSize(self): + """Returns the size of the screen in the form: (minx, miny, maxx, maxy)""" if self.sizeValid: return self.size (minX, minY, maxX, maxY) = (sys.maxint,sys.maxint,-sys.maxint,-sys.maxint) @@ -77,23 +79,19 @@ class Screen: maxY = max(y, maxY) self.size = (0,0, maxX, maxY) self.sizeValid = True - return (0, 0, maxX+100, maxY+100) #TODO: cleaner + return (0, 0, maxX, maxY) #private def processResponse(self, responseInfo, currentTime=None): #we need to make a new dict for #each to prevent interference - #[strip.respond(dict(responseInfo)) for strip in self.pixelStrips] if currentTime == None: currentTime = timeops.time() - print 'cachetime fail' if type(responseInfo) != type(dict()): pass if 'Mapper' in responseInfo: mapper = compReg.getComponent(responseInfo['Mapper']) else: mapper = compReg.getComponent(Strings.DEFAULT_MAPPER) - #if type(mapper) != type(PixelMapper): - # raise Exception('No default mapper specified.') pixelWeightList = mapper.mapEvent(responseInfo['Location'], self) main_log.debug('Screen processing response. ' + str(len(pixelWeightList)) + ' events\ generated') |