forked from Danielhiversen/flux_led
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crossfade_example.py
executable file
·89 lines (66 loc) · 2.03 KB
/
crossfade_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python
"""
Example to cycle a bulb between colors in a list, with a smooth fade
between. Assumes the bulb is already on.
The python file with the Flux LED wrapper classes should live in
the same folder as this script
"""
import os
import sys
import time
from itertools import cycle
this_folder = os.path.dirname(os.path.realpath(__file__))
sys.path.append(this_folder)
from flux_led import WifiLedBulb, BulbScanner, LedTimer
def crossFade(bulb, color1, color2):
r1,g1,b1 = color1
r2,g2,b2 = color2
steps = 100
for i in range(1,steps+1):
r = r1 - int(i * float(r1 - r2)/steps)
g = g1 - int(i * float(g1 - g2)/steps)
b = b1 - int(i * float(b1 - b2)/steps)
# (use non-persistent mode to help preserve flash)
bulb.setRgb(r,g,b, persist=False)
def main():
# Find the bulb on the LAN
scanner = BulbScanner()
scanner.scan(timeout=4)
# Specific ID/MAC of the bulb to set
bulb_info = scanner.getBulbInfoByID('ACCF235FFFFF')
if bulb_info:
bulb = WifiLedBulb(bulb_info['ipaddr'])
color_time = 5 # seconds on each color
red = (255,0,0)
orange = (255,125,0)
yellow = (255, 255, 0)
springgreen = (125,255,0)
green = (0,255,0)
turquoise = (0,255,125)
cyan = (0, 255, 255)
ocean = (0,125,255)
blue = (0,0,255)
violet = (125, 0, 255)
magenta = (255, 0, 255)
raspberry = (255, 0, 125)
colorwheel = [red, orange, yellow, springgreen, green, turquoise,
cyan, ocean, blue, violet, magenta, raspberry]
# use cycle() to treat the list in a circular fashion
colorpool = cycle(colorwheel)
# get the first color before the loop
color = next(colorpool)
while True:
bulb.refreshState()
# set to color and wait
# (use non-persistent mode to help preserve flash)
bulb.setRgb(*color, persist=False)
time.sleep(color_time)
#fade from color to next color
next_color = next(colorpool)
crossFade(bulb, color, next_color)
# ready for next loop
color = next_color
else:
print "Can't find bulb"
if __name__ == '__main__':
main()