Exemple #1
0
print('Animating pixels for 10 seconds...')
for offset in range(10000):
    # Go through each pixel and set its color based on its position and the
    # current offset.  Constrain these values to fall within the list of colors.
    for i in range(12):
        # Find the color for this pixel.
        color = colors[(i + offset) % len(colors)]
        # Set the pixel color.
        board.set_pixel(i, (offset - 0) % 255, (offset - 50) % 255,
                        (offset - 100) % 255)
        # board.show_pixels()

    # Push the updated colors out to the pixels (this will make the pixels change
    # their color, the previous set_pixel calls just change the memory and not
    # the pixels).
    board.show_pixels()
    time.sleep(0.01)
    if (offset % nframes == 0):
        finish_frame_time = time.time()
        print("Frame rate = ",
              int(nframes / (finish_frame_time - start_frame_time)), " fps")
        start_frame_time = time.time()
    # Sleep for a bit between iterations.
    # time.sleep(0.01)

# Clear all the pixels to turn them off.
board.clear_pixels()
board.show_pixels()  # Make sure to call show after clear!
# time.sleep(0.01)      # Small delay to make sure board connection doesn't close
# before previous command is processed.
# to higher brightness because the color information is 'lost'.  It's best to
# just call set brightness once at the start to set a good max brightness instead
# of trying to make animations with it.
board.set_pixel_brightness(50)

# Animate moving the colors across the pixels 100 times / 10 seconds.
print('Animating pixels for 10 seconds...')
for offset in range(100):
    # Go through each pixel and set its color based on its position and the
    # current offset.  Constrain these values to fall within the list of colors.
    for i in range(10):
        # Find the color for this pixel.
        color = colors[(i+offset)%len(colors)]
        # Set the pixel color.
        board.set_pixel(i, color[0], color[1], color[2])
    # Push the updated colors out to the pixels (this will make the pixels change
    # their color, the previous set_pixel calls just change the memory and not
    # the pixels).
    board.show_pixels()
    # Sleep for a bit between iterations.
    time.sleep(0.1)

# Clear all the pixels to turn them off.
board.clear_pixels()
board.show_pixels()  # Make sure to call show after clear!
time.sleep(0.1)      # Small delay to make sure board connection doesn't close
                     # before previous command is processed.

# Close Firmata board connection when done.
board.close()
# The buttons/switches on Circuit Playground use these pins:
#  - Left button = Digital pin 4
#  - Right button = Digital pin 19
board.set_pin_mode(4, board.INPUT, board.DIGITAL, left_changed)
board.set_pin_mode(19, board.INPUT, board.DIGITAL, right_changed)

# Animate moving the colors across the pixels 100 times / 10 seconds.
print('Animating pixels...')
print('Press left button to cycle colors and right button to cycle speeds.')
while True:
    frequency = FREQUENCIES[current_frequency]
    c0_red, c0_green, c0_blue = COLORS[current_color][0]
    c1_red, c1_green, c1_blue = COLORS[current_color][1]
    t = time.time()
    # Go through each pixel and interpolate its color using a sine wave with
    # phase offset based on pixel position.
    for i in range(10):
        phase = (i / 10.0) * 2.0 * math.pi
        x = math.sin(2.0 * math.pi * frequency * t + phase)
        red = int(lerp(x, -1.0, 1.0, c0_red, c1_red))
        green = int(lerp(x, -1.0, 1.0, c0_green, c1_green))
        blue = int(lerp(x, -1.0, 1.0, c0_blue, c1_blue))
        # Set the pixel color.
        board.set_pixel(i, red, green, blue)
    # Push the updated colors out to the pixels (this will make the pixels change
    # their color, the previous set_pixel calls just change the memory and not
    # the pixels).
    board.show_pixels()
    # Sleep for a bit between iterations.
    time.sleep(0.01)