Package SmootLight :: Package pixelcore :: Module Pixel
[hide private]
[frames] | no frames]

Source Code for Module SmootLight.pixelcore.Pixel

 1  import util.ColorOps as color 
 2  from logger import main_log 
 3  import pdb 
 4  from pixelevents.StepEvent import * 
 5  import util.TimeOps as timeops 
6 -class Pixel:
7 """Pixel keeps a queue of events (PixelEvent objects) (actually a dictionary 8 keyed by event time). Every time is state is 9 requested, it processes all the members of its queue. If a member returns none, 10 it is removed from the queue. Otherwise, its value added to the Pixels color 11 weighted by z-index. To get the current color of the pixel, call the state method.""" 12 13 radius = 2 14 timeOff = -1 15
16 - def __init__(self, location):
17 self.location = location 18 self.events = [] 19 self.lastRenderTime = timeops.time() 20 self.lastRender = (0,0,0)
21
22 - def turnOn(self):
23 self.turnOnFor(-1)
24 25 #Turn the light white for 'time' ms. Really only meant for testing. Use 26 #processInput instead. Also, you shouldn't use this anyway. You should be 27 #using the input method on the screen!
28 - def turnOnFor(self, time):
29 event = StepEvent.generate(time, (255,255,255)) 30 self.processInput(event, 0)
31 32 #Add a pixelEvent to the list of active events
33 - def processInput(self,pixelEvent,zindex, scale=1,currentTime=None): #consider migrating arg to dict
34 if currentTime == None: 35 currentTime = timeops.time() 36 self.events.append((currentTime, zindex, scale, pixelEvent)) #TODO: clean this up, maybe?
37 - def clearAllEvents(self):
38 self.events = []
39
40 - def state(self, currentTime=None):
41 """Combines all PixelEvents currently active and computes the current color of 42 the pixel.""" 43 if currentTime == None: 44 currentTime = timeops.time() 45 if currentTime-self.lastRenderTime < 5: 46 return self.lastRender 47 if self.events == []: 48 self.lastRenderTime = currentTime 49 return (0,0,0) 50 deadEvents = [] 51 resultingColor = (0,0,0) 52 colors = [] 53 for eventObj in self.events: #TODO: right color weighting code 54 if len(self.events) > 50: 55 main_log.error('High pixel event count! Investigate!') 56 eventTime, zindex, scale, pixelEvent = eventObj 57 eventResult = pixelEvent.state(currentTime-eventTime) 58 if eventResult != None: 59 scaledEvent = color.multiplyColor(eventResult,scale) 60 if (scaledEvent[0] + scaledEvent[1] + scaledEvent[2]) < 5: 61 pass 62 #deadEvents.append(eventObj) 63 else: 64 colors.append(scaledEvent) 65 else: 66 deadEvents.append(eventObj) 67 68 resultingColor = color.combineColors(colors) 69 [self.events.remove(event) for event in deadEvents] 70 resultingColor = [int(round(c)) for c in resultingColor] 71 self.lastRender = tuple(resultingColor) 72 self.lastRenderTime = currentTime 73 return tuple(resultingColor) 74
75 - def __str__(self):
76 return 'Loc: ' + str(self.location)
77