Debian Codegraph @

examples/crossfade_example.py

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