aboutsummaryrefslogtreecommitdiff
path: root/pixelcore
diff options
context:
space:
mode:
authorGravatar Russell Cohen <rcoh@mit.edu>2010-12-03 04:20:17 -0500
committerGravatar Russell Cohen <rcoh@mit.edu>2010-12-03 04:20:17 -0500
commit353ab16db64c86122c0fcb9e1852b85c14b354b8 (patch)
tree8904f1cd312974a849ad5bcf0c8520fd20175cda /pixelcore
parent0366f46d3d7e946e254f933888aea4beb4e70658 (diff)
Speed optimizations abound! Caching on parameter validation, Binary
searching for light locations in pixel mappers, actual thread safety for inputs means we don't drop them randomly, anymore. Caching on PixelStates to reduce multi-renderer costs + a short circuit to speed up processing on pixels that are turned off.
Diffstat (limited to 'pixelcore')
-rw-r--r--pixelcore/Pixel.py8
-rw-r--r--pixelcore/Screen.py17
2 files changed, 24 insertions, 1 deletions
diff --git a/pixelcore/Pixel.py b/pixelcore/Pixel.py
index 8c42581..4c8ec89 100644
--- a/pixelcore/Pixel.py
+++ b/pixelcore/Pixel.py
@@ -12,6 +12,7 @@ class Pixel:
def __init__(self, location):
self.location = location
self.events = {}
+ self.memState = None
def turnOn(self):
self.turnOnFor(-1)
#Turn the light white for 'time' ms. Really only meant for testing. Use
@@ -28,7 +29,13 @@ class Pixel:
self.events = {}
#Combines all PixelEvents currently active and computes the current color of
#the pixel.
+ def invalidateState(self):
+ self.memState = None
def state(self):
+ if self.memState != None:
+ return self.memState
+ if len(self.events) == 0:
+ return (0,0,0)
deadEvents = []
currentTime = Util.time()
resultingColor = (0,0,0)
@@ -40,6 +47,7 @@ class Pixel:
else:
deadEvents.append(eventTime)
[self.events.pop(event) for event in deadEvents]
+ self.memState = tuple(resultingColor)
return tuple(resultingColor)
def __str__(self):
return 'Loc: ' + str(self.location)
diff --git a/pixelcore/Screen.py b/pixelcore/Screen.py
index 6b5c737..71b9b0b 100644
--- a/pixelcore/Screen.py
+++ b/pixelcore/Screen.py
@@ -7,11 +7,24 @@ class Screen:
def __init__(self):
self.responseQueue = []
self.pixelStrips = []
+ self.xSortedPixels = []
+ self.xPixelLocs = []
sizeValid = False
def addStrip(self, lS):
self.pixelStrips.append(lS)
self.sizeValid = False #keep track of whether or not our screen size has
#been invalidated by adding more pixels
+ self.computeXSortedPixels()
+ #Returns (pixelIndex, pixel). Does a binary search.
+ def pixelsInRange(self, minX, maxX):
+ minIndex = Util.find_ge(self.xPixelLocs, minX)
+ maxIndex = Util.find_le(self.xPixelLocs, maxX)+1
+ return self.xSortedPixels[minIndex:maxIndex]
+ def computeXSortedPixels(self):
+ for pixel in self:
+ self.xSortedPixels.append((pixel.location[0], pixel))
+ self.xSortedPixels.sort()
+ self.xPixelLocs = [p[0] for p in self.xSortedPixels]
def render(self, surface):
[lS.render(surface) for lS in self.pixelStrips]
def setMapper(self, mapper):
@@ -29,6 +42,7 @@ class Screen:
self.responseQueue = []
for response in tempQueue:
self.processResponse(response)
+ [p.invalidateState() for p in self]
#public
def respond(self, responseInfo):
self.responseQueue.append(responseInfo)
@@ -53,7 +67,8 @@ class Screen:
#each to prevent interference
#[strip.respond(dict(responseInfo)) for strip in self.pixelStrips]
if type(responseInfo) != type(dict()):
- pdb.set_trace()
+ pass
+ #pdb.set_trace()
pixelWeightList = self.mapper.mapEvent(responseInfo['Location'], self)
Util.addPixelEventIfMissing(responseInfo)
for (pixel, weight) in pixelWeightList: