Skip to content

Instantly share code, notes, and snippets.

@mic159
Created October 25, 2018 14:16
Show Gist options
  • Save mic159/fbbfe239d468412cd067aca47a703195 to your computer and use it in GitHub Desktop.
Save mic159/fbbfe239d468412cd067aca47a703195 to your computer and use it in GitHub Desktop.
lifx tile patterns
from lifxlan import LifxLAN, TileChain
import random
import time
MAX_VAL = 65535
FADE_IN_SPEED = MAX_VAL * 0.125 # 32
FADE_OUT_SPEED = MAX_VAL * 0.078 # 20
tiles = LifxLAN().get_device_by_name('Tiles')
# tiles = TileChain('d0:73:d5:33:5b:c2', '10.10.0.66')
# Palettes from https://www.color-hex.com/ (Using HSL)
PALETTE_TWILIGHT = [
(0.65 * MAX_VAL, 0.53 * MAX_VAL, 0.33 * MAX_VAL, 3500),
(0.76 * MAX_VAL, 0.42 * MAX_VAL, 0.38 * MAX_VAL, 3500),
(0.79 * MAX_VAL, 0.42 * MAX_VAL, 0.73 * MAX_VAL, 3500),
]
PALETTE_SUMMERTIME = [
(0.11 * MAX_VAL, 1 * MAX_VAL, 0.65 * MAX_VAL, 3500),
(0.51 * MAX_VAL, 0.86 * MAX_VAL, 0.38 * MAX_VAL, 3500),
(0.06 * MAX_VAL, 0.81 * MAX_VAL, 0.54 * MAX_VAL, 3500),
(0.58 * MAX_VAL, 0.82 * MAX_VAL, 0.27 * MAX_VAL, 3500),
]
PALETTE_RAINBOW_DASH = [
(0.01 * MAX_VAL, 0.84 * MAX_VAL, 0.57 * MAX_VAL, 3500),
(0.06 * MAX_VAL, 0.89 * MAX_VAL, 0.58 * MAX_VAL, 3500),
(0.15 * MAX_VAL, 0.96 * MAX_VAL, 0.79 * MAX_VAL, 3500),
(0.26 * MAX_VAL, 0.50 * MAX_VAL, 0.51 * MAX_VAL, 3500),
(0.55 * MAX_VAL, 0.97 * MAX_VAL, 0.41 * MAX_VAL, 3500),
]
PALETTES = [
PALETTE_TWILIGHT,
PALETTE_SUMMERTIME,
PALETTE_RAINBOW_DASH,
]
print('found lights', tiles.get_mac_addr(), tiles.get_ip_addr())
# print(tiles.get_tile_info())
# print('get_tilechain_colors', tiles.get_tilechain_colors())
# print('get_canvas_dimensions', tiles.get_canvas_dimensions())
leds = tiles.get_tilechain_colors()
direction_flags = [[0 for _ in range(len(leds[0]))] for _ in range(len(leds))]
print('Got colors')
def update_fade(fade_up_amount, fade_down_amount):
for tile_idx, tile in enumerate(leds):
for idx, led in enumerate(tile):
if direction_flags[tile_idx][idx] == 1:
tile[idx], done = make_lighter(fade_up_amount, *led)
if done:
direction_flags[tile_idx][idx] = 0
else:
tile[idx] = make_darker(fade_down_amount, *led)
def make_darker(amount, h, s, v, k):
new_v = max(0, v - amount)
return (h, s, new_v, k)
def make_lighter(amount, h, s, v, k):
new_v = min(MAX_VAL, v + amount)
return (h, s, new_v, k), new_v == MAX_VAL
def fade_to_black_by(amount):
for tile in leds:
for idx, led in enumerate(tile):
tile[idx] = (
tile[idx][0],
tile[idx][1],
int(tile[idx][2] * amount),
tile[idx][3],
)
def twinkles(palette):
update_fade(FADE_IN_SPEED, FADE_OUT_SPEED)
if random.randint(0, 255) > 128:
tile = random.randint(0, len(leds) - 1)
pos = random.randint(0, len(leds[tile]) - 1)
direction_flags[tile][pos] = 1
leds[tile][pos] = random.choice(palette)
def confetti(hue):
fade_to_black_by(0.85)
tile = random.randint(0, len(leds) - 1)
pos = random.randint(0, len(leds[tile]) - 1)
leds[tile][pos] = (
int(hue + random.randint(0, int(MAX_VAL * 0.25))) % MAX_VAL,
int(MAX_VAL * 0.8),
MAX_VAL,
3500
)
current_palette = random.choice(PALETTES)
current_palette_time = time.time()
current_hue = 0
while True:
twinkles(current_palette)
# confetti(current_hue)
tiles.set_tilechain_colors(leds, rapid=True)
if time.time() - current_palette_time > 10:
current_palette_time = time.time()
current_palette = random.choice(PALETTES)
current_hue = (current_hue + 100) % MAX_VAL
time.sleep(0.01)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment