aboutsummaryrefslogtreecommitdiff
path: root/pixelcore
diff options
context:
space:
mode:
authorGravatar Thomas B Thompson <tbent@spice.(none)>2011-01-04 00:04:05 -0500
committerGravatar Thomas B Thompson <tbent@spice.(none)>2011-01-04 00:04:05 -0500
commit134de4472d3f2fa913944770595de9221dd27fdf (patch)
treecf83e29a1ef4931a15f9b30125fedbd8fba8caf9 /pixelcore
parentf9aeaf10e3c9077504a78374640e79415734546b (diff)
worked on profiling, made a bunch of changes, huge speedup!
Diffstat (limited to 'pixelcore')
-rw-r--r--pixelcore/Pixel.py21
-rw-r--r--pixelcore/Screen.py17
2 files changed, 31 insertions, 7 deletions
diff --git a/pixelcore/Pixel.py b/pixelcore/Pixel.py
index f66d0bb..9f5cc85 100644
--- a/pixelcore/Pixel.py
+++ b/pixelcore/Pixel.py
@@ -1,7 +1,7 @@
import util.ColorOps as color
import pdb
from pixelevents.StepEvent import *
-import util.TimeOps as clock
+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,
@@ -10,12 +10,15 @@ import util.TimeOps as clock
class Pixel:
radius = 2
timeOff = -1
+
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
#processInput instead. Also, you shouldn't use this anyway. You should be
#using the input method on the screen!
@@ -23,34 +26,42 @@ class Pixel:
event = StepEvent.generate(time, (255,255,255)) #TODO: Move color to
self.processInput(event, 0)
#arg
+
#Add a pixelEvent to the list of active events
def processInput(self,pixelEvent,zindex): #consider migrating arg to dict
- self.events[clock.time()] = (zindex, pixelEvent)
+ self.events[timeops.time()] = (zindex, pixelEvent)
+
def clearAllEvents(self):
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:
+ if self.events == []:
return (0,0,0)
deadEvents = []
- currentTime = clock.time()
+ currentTime = timeops.time()
resultingColor = (0,0,0)
+ colors = []
for eventTime in self.events: #TODO: right color weighting code
(zindex,event) = self.events[eventTime]
eventResult = event.state(currentTime-eventTime)
if eventResult != None:
- resultingColor = color.combineColors(eventResult, resultingColor)
+ colors.append(eventResult)
else:
deadEvents.append(eventTime)
+
+ resultingColor = color.combineColors(colors)
[self.events.pop(event) for event in deadEvents]
resultingColor = [int(round(c)) for c in resultingColor]
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 da03ad2..b002896 100644
--- a/pixelcore/Screen.py
+++ b/pixelcore/Screen.py
@@ -6,6 +6,8 @@ import util.Search as Search
import util.ComponentRegistry as compReg
import util.Strings as Strings
import itertools
+import sys
+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:
@@ -15,28 +17,35 @@ class Screen:
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 = Search.find_ge(self.xPixelLocs, minX)
maxIndex = Search.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 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
+
#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.
@@ -46,14 +55,15 @@ class Screen:
for response in tempQueue:
self.processResponse(response)
[p.invalidateState() for p in self]
+
#public
def respond(self, responseInfo):
self.responseQueue.append(responseInfo)
+
def getSize(self):
if self.sizeValid:
return self.size
- (minX, minY, maxX, maxY) = (10**10,10**10,-10**10,-10*10) #TODO: don't
- #be lazy
+ (minX, minY, maxX, maxY) = (sys.maxint,sys.maxint,-sys.maxint,-sys.maxint)
for light in self:
(x,y) = light.location
@@ -65,6 +75,7 @@ class Screen:
self.size = (0,0, maxX, maxY)
self.sizeValid = True
return (0, 0, maxX+100, maxY+100) #TODO: cleaner
+
#private
def processResponse(self, responseInfo): #we need to make a new dict for
#each to prevent interference
@@ -78,6 +89,8 @@ class Screen:
#if type(mapper) != type(PixelMapper):
# raise Exception('No default mapper specified.')
pixelWeightList = mapper.mapEvent(responseInfo['Location'], self)
+ main_log.debug(str(len(pixelWeightList)))
+ main_log.debug(pixelWeightList)
PixelEvent.addPixelEventIfMissing(responseInfo)
for (pixel, weight) in pixelWeightList:
pixel.processInput(responseInfo['PixelEvent'].scale(weight), 0) #TODO: z-index