aboutsummaryrefslogtreecommitdiff
path: root/pixelcore
diff options
context:
space:
mode:
authorGravatar Russell Cohen <rcoh@mit.edu>2010-11-24 01:09:12 -0500
committerGravatar Russell Cohen <rcoh@mit.edu>2010-11-24 01:09:12 -0500
commitb042647b68abdc82490ca6e059993b8eba28904c (patch)
treea9ee95a38e98b377c251b7b2e9af9cbd8056cf7c /pixelcore
parent407ac922fc4178021cf3a16dfb1bd875b6083ac4 (diff)
Refactoring complete! Made modules/packages as appropriate. Finally.
Diffstat (limited to 'pixelcore')
-rw-r--r--pixelcore/Pixel.py44
-rw-r--r--pixelcore/PixelStrip.py40
-rw-r--r--pixelcore/Screen.py31
-rw-r--r--pixelcore/__init__.py0
4 files changed, 115 insertions, 0 deletions
diff --git a/pixelcore/Pixel.py b/pixelcore/Pixel.py
new file mode 100644
index 0000000..6784c63
--- /dev/null
+++ b/pixelcore/Pixel.py
@@ -0,0 +1,44 @@
+import Util
+import pdb
+from pixelevents.StepEvent import *
+#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 cue. 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:
+ radius = 2
+ timeOff = -1
+ def __init__(self, location, color):
+ self.location = location
+ self.color = color
+ self.events = {}
+ def turnOn(self):
+ self.turnOnFor(-1)
+ def turnOnFor(self, time):
+ event = StepEvent.generate(time, (255,0,255)) #TODO: Move color to
+ self.processInput(event, 0)
+ #arg
+ def processInput(self,pixelEvent,zindex): #consider migrating arg to dict
+ self.events[Util.time()] = (zindex, pixelEvent)
+ def clearAllEvents(self):
+ self.events = {}
+ def state(self):
+ deadEvents = []
+ currentTime = Util.time()
+ resultingColor = (0,0,0)
+ for eventTime in self.events: #TODO: right color weighting code
+ (zindex,event) = self.events[eventTime]
+ eventResult = event.state(currentTime-eventTime)
+ if eventResult != None:
+ resultingColor = Util.combineColors(eventResult, resultingColor)
+ print resultingColor
+ else:
+ deadEvents.append(eventTime)
+ [self.events.pop(event) for event in deadEvents]
+ if sum(resultingColor) > 0:
+ print resultingColor
+ return tuple(resultingColor)
+ def __str__(self):
+ return 'Loc: ' + str(self.location)
+
diff --git a/pixelcore/PixelStrip.py b/pixelcore/PixelStrip.py
new file mode 100644
index 0000000..14c87d9
--- /dev/null
+++ b/pixelcore/PixelStrip.py
@@ -0,0 +1,40 @@
+from pixelcore.Pixel import *
+from pixelevents.StepEvent import *
+import pygame
+import math
+import Util
+import pdb
+#Python class representing a single Pixel strip (usually 50 Pixels)
+class PixelStrip:
+ def __init__(self, layoutEngine):
+ self.initStrip(layoutEngine)
+ self.argDict = layoutEngine.getStripArgs()
+ def initStrip(self, layoutEngine):
+ pixelLocations = layoutEngine.getPixelLocations()
+ self.pixels = [Pixel(l, (0,0,0)) 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):
+ print 'PixelEvent', responseInfo
+ location = responseInfo[Util.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 = [(Util.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
new file mode 100644
index 0000000..9806daa
--- /dev/null
+++ b/pixelcore/Screen.py
@@ -0,0 +1,31 @@
+from pixelcore.Pixel import *
+from pixelcore.PixelStrip import *
+import itertools
+class Screen:
+ def __init__(self):
+ self.responseQueue = []
+ self.pixelStrips = []
+ def addStrip(self, lS):
+ self.pixelStrips.append(lS)
+ 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])
+ #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.
+ def timeStep(self):
+ tempQueue = list(self.responseQueue)
+ self.responseQueue = []
+ for response in tempQueue:
+ self.processResponse(response)
+ #public
+ def respond(self, responseInfo):
+ self.responseQueue.append(responseInfo)
+ #private
+ def processResponse(self, responseInfo): #we need to make a new dict for
+ #each to prevent interference
+ [strip.respond(dict(responseInfo)) for strip in self.pixelStrips]
+
diff --git a/pixelcore/__init__.py b/pixelcore/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/pixelcore/__init__.py