class Pixel(object): def __init__(self): self.behavior = BrightnessBehavior() self.mode = "OFF" self.color = LEDColor("OFF") if not self.mode == "OFF": self.behavior.start() self.brightness = 0.0 def get_state(self): return self.behavior.state def set_color(self, color_name): self.color.set_color(color_name) def start(self): self.behavior.start() def update(self): self.behavior.update() self.color.set_brightness(self.behavior.brightness) return self.color.get_rgb() def cancel(self): self.behavior.cancel()
def __init__(self, update_rate, mask_size=9): self._behavior = { "OFF": self._do_off, # Show background color "DIM": self._do_dim, # Reduce background color "UP": self._do_up, # brighten target color "ON": self._do_on, # hold target color "DOWN": self._do_down, # reduce target color brightness "LIGHT": self._do_light, # brighten background color "WAIT": self._do_wait # Show background color, but wait to start new action } self._brightness = None self._delay_time = None # Variably determined time to wait self._update_rate = update_rate # program looping rate - used to determine how quickly to decrement delay time # self._left_eye_background_color = LEDColor("OFF") # self._right_eye_background_color = LEDColor("OFF") # Mask Code print("Mask Size= %d" % mask_size) assert(mask_size >= 7) # buf buf eye buf eye buf buf self._mask_size = mask_size self._pixel_mask_background_colors = list() for i in range(0, self._mask_size): self._pixel_mask_background_colors.append(LEDColor("OFF")) self._color = LEDColor("OFF") # Constant Parameters self._delta = 0.1 # constant brightness step size self._static_on_time = 8 self._static_wait_time = 15 self._reset()
def __init__(self, color, zone_size=8): self.num_leds = None self.zone_size = zone_size self.zones = list( ) # [range(0,8), range(8,17), range(17,25), range(25,34), range(34, 42), range(42,50)] self.color = LEDColor(color) self.current_zone = 1
def __init__(self): self.behavior = BrightnessBehavior() self.mode = "OFF" self.color = LEDColor("OFF") if not self.mode == "OFF": self.behavior.start() self.brightness = 0.0
def __init__(self, color, direction, offset=0, spacing=6): """ direction: 1=right, -1=left offset: distance from first or last LED to start chasers spacing: distance between lit up pixels """ assert (direction in [1, -1]) self.color = LEDColor(color) self.direction = direction self.offset = offset self.spacing = spacing self.led_positions = None # [True, False, False, ...
def init(self, leds): n = len(leds) num_stripes = n // self.stripe_size color = "RED" for i in range(0, num_stripes): # Alternate colors color = "WHITE" if color == "RED" else "RED" for j in range(0, self.stripe_size): leds[i * self.stripe_size + j] = LEDColor(color).get_rgb() # Assign all remaining leds to last color for j in range(0, n % self.stripe_size): leds[num_stripes * self.stripe_size + j] = LEDColor(color).get_rgb() return leds
class SolidColor(Behavior): """ replacement for StaticOn that doesn't use set_led_color """ def __init__(self, color): self.color = LEDColor(color) def init(self, leds): return self.update(leds) def update(self, leds): for led in leds: update_led_value(led, self.color.get_rgb()) return leds def cancel(self, leds): self.color.set_color("OFF") for led in leds: update_led_value(led, (0, 0, 0)) return leds
def __init__(self, update_rate): self._state = "OFF" # OFF, UP, ON, DOWN, WAIT self._behavior = { "OFF": self._do_off, "UP": self._do_up, "ON": self._do_on, "DOWN": self._do_down, "WAIT": self._do_wait } self._brightness = None self._delay_time = None # Variably determined time to wait self._update_rate = update_rate # program looping rate - used to determine how quickly to decrement delay time self._color = LEDColor("OFF") # Constant Parameters self._delta = 0.1 # constant brightness step size self._static_wait_time = 0.5 self._reset()
class Sparkle(Behavior): """ Random LEDs briefly flare and then disappear again """ def __init__(self, color, zone_size=8): self.num_leds = None self.zone_size = zone_size self.zones = list( ) # [range(0,8), range(8,17), range(17,25), range(25,34), range(34, 42), range(42,50)] self.color = LEDColor(color) self.current_zone = 1 def init(self, leds): # Number of LEDS in the strand self.num_leds = len(leds) print("Num Leds = %d" % self.num_leds) num_zones = self.num_leds // self.zone_size print("Num zones = %d" % num_zones) for i in range(0, num_zones): self.zones.append( range(i * self.zone_size, i * self.zone_size + self.zone_size)) # if self.num_leds % self.zone_size > 0: # self.zones.append(range(num_zones*self.zone_size, self.num_leds)) print(self.zones) return leds def update(self, leds): target = random.choice(self.zones[self.current_zone]) # print("zone %d led %d" % (self.current_zone, target)) update_led_value(leds[target], self.color.get_rgb()) # select new zone (can't be current zone) zones = list(range(0, len(self.zones))) zones.pop(zones.index(self.current_zone)) self.current_zone = random.choice(zones) return leds def cancel(self, leds): for led in leds: update_led_value(led, (0, 0, 0)) return leds
class AddChasers(Behavior): """ Spawns color pixels that move in a direction """ def __init__(self, color, direction, offset=0, spacing=6): """ direction: 1=right, -1=left offset: distance from first or last LED to start chasers spacing: distance between lit up pixels """ assert (direction in [1, -1]) self.color = LEDColor(color) self.direction = direction self.offset = offset self.spacing = spacing self.led_positions = None # [True, False, False, ... def init(self, leds): self.num_leds = len(leds) self.led_positions = deque([False] * self.num_leds) # Initialize positions of LEDS if self.direction == 1: for i in range(0 + self.offset, self.num_leds, self.spacing): self.led_positions[i] = True else: for i in range(self.num_leds - self.offset - 1, -1, -self.spacing): self.led_positions[i] = True return leds def update(self, leds): # Rotate led positions self.led_positions.rotate(self.direction) for i in range(0, self.num_leds): if self.led_positions[i] == True: leds[i] = self.color.get_rgb() return leds def cancel(self, leds): for led in leds: update_led_value(led, (0, 0, 0)) return leds
def __init__(self, stripe_size): self.stripe_size = stripe_size self.red_color = LEDColor("RED") self.white_color = LEDColor("WHITE")
def __init__(self, color_names): self.color_pallet = [LEDColor(name) for name in color_names]
def __init__(self, color): self.color = LEDColor(color)
class EyePixelFader(object): def __init__(self, update_rate, mask_size=9): self._behavior = { "OFF": self._do_off, # Show background color "DIM": self._do_dim, # Reduce background color "UP": self._do_up, # brighten target color "ON": self._do_on, # hold target color "DOWN": self._do_down, # reduce target color brightness "LIGHT": self._do_light, # brighten background color "WAIT": self._do_wait # Show background color, but wait to start new action } self._brightness = None self._delay_time = None # Variably determined time to wait self._update_rate = update_rate # program looping rate - used to determine how quickly to decrement delay time # self._left_eye_background_color = LEDColor("OFF") # self._right_eye_background_color = LEDColor("OFF") # Mask Code print("Mask Size= %d" % mask_size) assert(mask_size >= 7) # buf buf eye buf eye buf buf self._mask_size = mask_size self._pixel_mask_background_colors = list() for i in range(0, self._mask_size): self._pixel_mask_background_colors.append(LEDColor("OFF")) self._color = LEDColor("OFF") # Constant Parameters self._delta = 0.1 # constant brightness step size self._static_on_time = 8 self._static_wait_time = 15 self._reset() def _reset(self): self._brightness = 0.0 self._delay_time = 0.0 self._state = "OFF" def _do_off(self): """ Show Background color full brightness""" self._brightness = 1.0 def _do_dim(self): self._brightness -= (self._delta / 30) if self._brightness < 0.0: self._brightness = 0.0 self._state = "UP" def _do_up(self): self._brightness += self._delta if self._brightness >= 1.0: self._brightness = 1.0 self._state = "ON" self._delay_time = self._static_on_time def _do_on(self): # TODO: Find a more clever way of counting time for each pixel - but not Threaded timers (doesnt work well on pi zero) self._delay_time -= self._update_rate if self._delay_time < 0: self._delay_time = 0.0 self._state = "DOWN" def _do_down(self): self._brightness -= self._delta if self._brightness < 0.0: self._brightness = 0.0 self._state = "LIGHT" def _do_light(self): self._brightness += self._delta if self._brightness >= 1.0: self._brightness = 1.0 self._state = "WAIT" self._delay_time = self._static_wait_time #range(0, int(self._static_wait_time)) def _do_wait(self): self._delay_time -= self._update_rate if self._delay_time < 0: self._delay_time = 0.0 self._state = "OFF" def set_mask_led_background_colors(self, rgb_values): """ Specify the color the LED should use during OFF, DIM, LIGHT, and WAIT states [(r,g,b), (r,g,b), (r,g,b) ...] """ assert(len(rgb_values) == self._mask_size) for i in range(0, self._mask_size): self._pixel_mask_background_colors[i].set_color_by_rgb_value(rgb_values[i]) # def set_background_colors(self, left_rgb_value, right_rgb_value): # """ Specify the color the LED should use during OFF, DIM, LIGHT, and WAIT states """ # self._left_eye_background_color.set_color_by_rgb_value(left_rgb_value) # self._right_eye_background_color.set_color_by_rgb_value(right_rgb_value) def start(self, color_name): """ Trigger light to begin turning on """ if not self._state == "OFF": raise Exception("Pixel.start() error: Pixel already turned on") self._color.set_color(color_name) # Change state to DIM self._brightness = 1.0 self._state = "DIM" def update(self): """ Calculates the new pixel state and brightness settings for this time step """ self._behavior[self._state]() self._color.set_brightness(self._brightness) # self._left_eye_background_color.set_brightness(self._brightness) # self._right_eye_background_color.set_brightness(self._brightness) # Mask Code for i in range(0, self._mask_size): self._pixel_mask_background_colors[i].set_brightness(self._brightness) def cancel(self): self._reset() def get_state(self): """ returns the current state of the pixel (OFF, UP, ON, DOWN, WAIT) """ return self._state # def left_eye_rgb_value(self): # """ returns rgb value # if Eyes are in UP, ON, or DOWN state, brightness is set against regular color. # If Eyes are in OFF, DIM, LIGHT, or WAIT states, brightness is set against background color # """ # return self._color.get_rgb() if self._state in ["UP","ON","DOWN"] else self._left_eye_background_color.get_rgb() # # def right_eye_rgb_value(self): # """ returns rgb value # if Eyes are in UP, ON, or DOWN state, brightness is set against regular color. # If Eyes are in OFF, DIM, LIGHT, or WAIT states, brightness is set against background color # """ # return self._color.get_rgb() if self._state in ["UP","ON","DOWN"] else self._right_eye_background_color.get_rgb() def rgb_mask_values(self): """ returns rgb value if state in UP, ON, or DOWN state: - eye pixels set against regular color. - other pixels set to OFF If state in OFF, DIM, LIGHT, or WAIT states, all pixels brightness is set against background color """ output_rgb_values = list() for i in range(0, self._mask_size): output_rgb_values.append( [0, 0, 0] if self._state in ["UP", "ON", "DOWN"] else self._pixel_mask_background_colors[i].get_rgb()) output_rgb_values[2] = self._color.get_rgb() if self._state in ["UP","ON","DOWN"] else self._pixel_mask_background_colors[2].get_rgb() output_rgb_values[-3] = self._color.get_rgb() if self._state in ["UP","ON","DOWN"] else self._pixel_mask_background_colors[-3].get_rgb() return output_rgb_values
class Pixel(object): def __init__(self, update_rate): self._state = "OFF" # OFF, UP, ON, DOWN, WAIT self._behavior = { "OFF": self._do_off, "UP": self._do_up, "ON": self._do_on, "DOWN": self._do_down, "WAIT": self._do_wait } self._brightness = None self._delay_time = None # Variably determined time to wait self._update_rate = update_rate # program looping rate - used to determine how quickly to decrement delay time self._color = LEDColor("OFF") # Constant Parameters self._delta = 0.1 # constant brightness step size self._static_wait_time = 0.5 self._reset() def _reset(self): self._brightness = 0.0 self._delay_time = 0.0 self._state = "OFF" def _do_off(self): self._brightness = 0.0 def _do_up(self): self._brightness += self._delta if self._brightness >= 1.0: self._brightness = 1.0 self._state = "ON" self._delay_time = self._static_wait_time + random.choice( [0.0, 0.1, 0.2, 0.3, 0.4, 0.5]) def _do_on(self): # TODO: Find a more clever way of counting time for each pixel - but not Threaded timers (doesnt work well on pi zero) self._delay_time -= self._update_rate if self._delay_time < 0: self._delay_time = 0.0 self._state = "DOWN" def _do_down(self): self._brightness -= self._delta if self._brightness < 0.0: self._brightness = 0.0 self._state = "WAIT" # self._delay_time = self._static_wait_time + random.choice([0.0, 0.1, 0.2, 0.3, 0.4, 0.5]) self._delay_time = 0.0 def _do_wait(self): self._delay_time -= self._update_rate if self._delay_time <= 0: self._delay_time = 0.0 self._state = "OFF" def start(self, color_name): """ Trigger light to begin turning on """ if not self._state == "OFF": raise Exception("Pixel.start() error: Pixel already turned on") self._color.set_color(color_name) self._state = "UP" def update(self): """ Calculates the new pixel state and brightness settings for this time step called by Twinkle.update() """ self._behavior[self._state]() self._color.set_brightness(self._brightness) def cancel(self): self._reset() def get_state(self): """ returns the current state of the pixel (OFF, UP, ON, DOWN, WAIT) """ return self._state def rgb_value(self): """ returns rgb value called by Twinkle.update() """ return self._color.get_rgb()