diff options
author | rcoh <rcoh@mit.edu> | 2011-01-27 16:50:59 -0500 |
---|---|---|
committer | rcoh <rcoh@mit.edu> | 2011-01-27 16:50:59 -0500 |
commit | 5fb3ea060025241105dc8e9a174513c112f9a133 (patch) | |
tree | e98b1b3eab0b05b0e518b08cbab086d224fd9250 /pixelcore | |
parent | 5d29906fff79bc6e4ba83be7028e1380a0014d21 (diff) |
A metric $#%$-ton of changes. Added doc-strings to EVERYTHING. Phew. Fixed a massive bug that
increases performance in by up to a factor of 60. A bunch of new behaviors for the class.
Diffstat (limited to 'pixelcore')
-rw-r--r-- | pixelcore/Pixel.py | 32 | ||||
-rw-r--r-- | pixelcore/PixelEventManager.py | 2 | ||||
-rw-r--r-- | pixelcore/PixelStrip.py | 29 | ||||
-rw-r--r-- | pixelcore/Screen.py | 13 |
4 files changed, 27 insertions, 49 deletions
diff --git a/pixelcore/Pixel.py b/pixelcore/Pixel.py index 2f21fd8..6ff2e67 100644 --- a/pixelcore/Pixel.py +++ b/pixelcore/Pixel.py @@ -2,12 +2,13 @@ import util.ColorOps as color 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 @@ -24,26 +25,22 @@ 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 - #TODO: fix for multiple pixel events if currentTime == None: currentTime = timeops.time() - #if not currentTime in self.events: - # self.events[currentTime] = [] - #self.events[currentTime].append((zindex,scale, pixelEvent)) - self.events.append((currentTime, zindex, scale, pixelEvent)) #TODO: this is kindof - #gross + self.events.append((currentTime, zindex, scale, pixelEvent)) #TODO: clean this up, maybe? def clearAllEvents(self): 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 == []: @@ -54,8 +51,7 @@ class Pixel: colors = [] for eventObj in self.events: #TODO: right color weighting code if len(self.events) > 50: - pdb.set_trace() - #TODO: this sucks. fix it + main_log.error('High pixel event count! Investigate!') eventTime, zindex, scale, pixelEvent = eventObj eventResult = pixelEvent.state(currentTime-eventTime) if eventResult != None: 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 662b8fe..595ce72 100644 --- a/pixelcore/PixelStrip.py +++ b/pixelcore/PixelStrip.py @@ -4,36 +4,21 @@ 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 render(self, surface): - [l.render(surface) for l in self.pixels] - #step - 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 b595847..9a81df7 100644 --- a/pixelcore/Screen.py +++ b/pixelcore/Screen.py @@ -10,9 +10,10 @@ 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 = [] @@ -20,14 +21,15 @@ class Screen: self.xPixelLocs = [] sizeValid = False self.pixelsSorted = False + def addStrip(self, lS): self.pixelStrips.append(lS) self.sizeValid = False #keep track of whether or not our screen size has self.pixelsSorted = False #been invalidated by adding more pixels - #Returns (pixelIndex, pixel). Does a binary search. def pixelsInRange(self, minX, maxX): + """Returns (pixelIndex, pixel). Does a binary search.""" if not self.pixelsSorted: self.computeXSortedPixels() minIndex = Search.find_ge(self.xPixelLocs, minX) @@ -41,10 +43,7 @@ class Screen: self.xSortedPixels.sort() self.xPixelLocs = [p[0] for p in self.xSortedPixels] self.pixelsSorted = True - #For debug only - def allOn(self): - [lS.allOn(-1) for lS in self.pixelStrips] - + 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 |