def send_colors(self, colors_in, starting=0, fading=False, fading_duration=1.0): """Send colors instruction to a bunch of LEDs Params: * colors_in is a list of color dicts for each LEDs * starting is the index of the first LED to control * fading, see send_color() * fading_duration is the total duration of the fading """ if fading is not False: if fading is not True and isinstance(fading, (int, long)): steps = { int(id): colors.fading(self.current_colors[int(id)], colors_in[id], fading) for id in colors_in } wait = 1.0 / fading else: steps = { int(id): colors.fading( self.current_colors[int(id)], colors_in[id], self.nb_steps_fading * fading_duration ) for id in colors_in } wait = 1.0 / self.nb_steps_fading else: steps = {int(id): [colors_in[id]] for id in colors_in} wait = 0 for k in xrange(len(steps[steps.iterkeys().next()])): for i in steps: # Send the color to the individual LED, and duration is 0 as # it is handled inside this function (globally and not per LED) if i + starting > self.nb_leds: continue # No need to send useless data self.send_color(i + starting, steps[i][k], fading_duration=0) time.sleep(wait)
def send_color(self, id, color, fading=False, fading_duration=1): """Corrects the color and send it over serial Params: * id is the id of the LED to address * color is a RGB255 color dict * fading_duration is the total duration of the fading If fading is passed and is not false, interpolates to do a fading between current and dest color. Fading can be an int to bypass the default number of intermediate steps or just True. """ data = [] id = int(id) if fading is not False: if isinstance(fading, (int, long)): steps = colors.fading(self.current_colors[id], color, fading) wait = float(fading_duration) / fading else: steps = colors.fading(self.current_colors[id], color, self.nb_steps_fading) wait = float(fading_duration) / self.nb_steps_fading else: steps = [color] wait = 0 for step in steps: data += [0x80 + id] tmp_color = self.correct_color(step) data.append(tmp_color["r"]) data.append(tmp_color["g"]) data.append(tmp_color["b"]) for i in xrange(len(data)): self.ser.write(chr(data[i])) if i % 4 == 0: time.sleep(wait) self.current_colors[id] = color
start_color = predefined_colors.black end_color = predefined_colors.rose nb_leds = 9 duration = 3 end_color_duration = 10 filename = "output.mp4" # End iterations = 25 * duration size = int(math.sqrt(nb_leds) * 10 + (math.sqrt(nb_leds) - 1) * 2) # Set up formatting for the movie files Writer = animation.writers['ffmpeg'] writer = Writer(fps=25, bitrate=1800) fading = colors.fading(start_color, end_color, iterations) images = [np.zeros((size, size, 3), np.uint8) for _ in range(iterations+end_color_duration * 25)] fading.extend([end_color for _ in range(end_color_duration * 25)]) count = 0 for img in range(len(images)): for k in range(int(math.sqrt(nb_leds))): for l in range(int(math.sqrt(nb_leds))): for i in range(10): for j in range(10): images[img][i + k*12, j + l*12] = (fading[count]['r'], fading[count]['g'], fading[count]['b']) count += 1