mclarkk / lifxlan

Python library for accessing LIFX devices locally using the official LIFX LAN protocol.
MIT License
507 stars 116 forks source link

Possible New Tile Examples #95

Closed natn closed 6 years ago

natn commented 6 years ago

I created these two examples with my kids and used them while testing, if you like them you can include them as samples. I cleaned them up a little bit and updated them to use discovery and keyboard interrupts similar to the falling rainbow. I tested them with a fresh pip install of lifxlan and they work fine.

Dots:

from lifxlan import *
from random import randint, choice
from time import sleep
from copy import deepcopy

def main():
    lan = LifxLAN()
    tilechain_lights = lan.get_tilechain_lights()
    if len(tilechain_lights) != 0:
        tile_chain = lan.get_tilechain_lights()[0]  # grab the first tilechain
        print("Selected TileChain light: {}".format(tile_chain.get_label()))
        (cols, rows) = tile_chain.get_canvas_dimensions()
        original_colors = tile_chain.get_tilechain_colors()

        background_colors = set_background(cols, rows)
        tile_chain.project_matrix(background_colors, 2000)

        dots = []
        max_dots = 50
        duration_ms = 150
        dot_rate = 0.1

        matrix = deepcopy(background_colors)

        try:
            while True:
                dot = [choice(range(rows)), choice(range(cols))]
                dots.append(dot)

                if len(dots) > max_dots:
                    old_dot = dots.pop(0)
                    matrix[int(old_dot[0])][int(old_dot[1])] = background_colors[int(old_dot[0])][int(old_dot[1])]

                matrix[int(dot[0])][int(dot[1])] = get_random_saturated_color()
                #Catch exceptions when the computer sleeps so we can resume when we wake
                try:
                    tile_chain.project_matrix(matrix, duration_ms, rapid=True)
                except:
                    pass
                sleep(dot_rate)
        except KeyboardInterrupt:
                tile_chain.set_tilechain_colors(original_colors)
                print("Done.")
    else:
            print("No TileChain lights found.")

def get_random_saturated_color():
    return randint(0, 65535), 65535, randint(0, 65535), 3000

def set_background(cols, rows):
    hue = 0
    background_colors = []
    for row in range(rows):
        color_row = []
        for col in range(cols):
            color_row.append((hue, 65535, 2000, 4900))
            hue += int(65535.0 / (cols * rows))
        background_colors.append(color_row)
    return background_colors

if __name__ == "__main__":
    main()

tile_Invaders:

from lifxlan import *
from random import randint
from time import sleep

def main():
    lan = LifxLAN()
    tilechain_lights = lan.get_tilechain_lights()
    if len(tilechain_lights) != 0:
        t = lan.get_tilechain_lights()[0]  # grab the first tilechain
        print("Selected TileChain light: {}".format(t.get_label()))
        original_colors = t.get_tilechain_colors()
        num_tiles = t.get_tile_count()

        duration_ms = 10
        DIM_BLUE = BLUE
        DIM_BLUE[2] = DIM_BLUE[2]/3
        palette = {0: [0,0,0,0],
                   1: DIM_BLUE
                    }

        num_frames = 2
        invader_matrix = \
               [[[1, 1, 1, 0, 0, 1, 1, 1],
                 [1, 1, 0, 0, 0, 0, 1, 1],
                 [1, 0, 0, 0, 0, 0, 0, 1],
                 [0, 0, 1, 0, 0, 1, 0, 0],
                 [0, 0, 0, 0, 0, 0, 0, 0],
                 [1, 0, 1, 0, 0, 1, 0, 1],
                 [0, 1, 1, 1, 1, 1, 1, 0],
                 [1, 0, 1, 1, 1, 1, 0, 1]],

                [[1, 1, 1, 0, 0, 1, 1, 1],
                 [1, 1, 0, 0, 0, 0, 1, 1],
                 [1, 0, 0, 0, 0, 0, 0, 1],
                 [0, 0, 1, 0, 0, 1, 0, 0],
                 [0, 0, 0, 0, 0, 0, 0, 0],
                 [1, 1, 0, 1, 1, 0, 1, 1],
                 [1, 0, 1, 0, 0, 1, 0, 1],
                 [0, 1, 0, 1, 1, 0, 1, 0]]]

        try:
            while True:
                for frame in range(num_frames):
                    sprite = []
                    for x in range(8):
                        for y in range(8):
                                sprite.append(palette[invader_matrix[frame][x][y]])
                    for index in range(num_tiles):
                        t.set_tile_colors(index, sprite, duration_ms)
                    sleep(1)
        except KeyboardInterrupt:
                t.set_tilechain_colors(original_colors)
                print("Done.")
    else:
            print("No TileChain lights found.")

def get_random_color():
    return randint(0, 65535), randint(0, 65535), randint(0, 65535), randint(2500, 9000)

if __name__ == "__main__":
    main()
mclarkk commented 6 years ago

This is great, thanks!!! I added them to the examples folder, and I can't wait to test them on my friend's tiles!