-
Notifications
You must be signed in to change notification settings - Fork 0
/
hue_wave_and_rainbow
206 lines (164 loc) · 6.98 KB
/
hue_wave_and_rainbow
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import time, threading, asyncio, colorsys, phue
import tkcolorpicker as tkc
from tkinter import TclError, colorchooser
from phue import Bridge
import tkinter as tk
root = tk.Tk()
root.title("Philips Hue Light Control")
root.geometry("400x400")
b = Bridge('192.168.0.88')
b.connect()
rooms = {
"Stue": [13, 12, 16, 18, 4, 5, 10, 11, 14],
"Kjøkken": [8, 17, 19, 20, 9],
"Trapp": [15, 14]
}
b.set_group(rooms["Stue"], 'on', True)
def toggle_lights(room, on_or_off):
b.set_group(room, 'on', on_or_off)
# Create a frame for the on/off buttons
on_off_frame = tk.Frame(root)
on_off_frame.pack(pady=10)
# Create a frame for the on/off buttons
on_off_frame = tk.Frame(root, bd=1, relief="solid")
on_off_frame.pack(pady=10)
# Create buttons for turning on/off each room
for i, room in enumerate(rooms):
on_button = tk.Button(on_off_frame, text=f"{room} On", command=lambda room=room: toggle_lights(room, True))
off_button = tk.Button(on_off_frame, text=f"{room} Off", command=lambda room=room: toggle_lights(room, False))
on_button.grid(row=i, column=0, padx=10, pady=10)
off_button.grid(row=i, column=1, padx=10, pady=10)
# Create a frame for the Rainbow Colors and Wave Colors control sections
colors_frame = tk.Frame(root, bd=1, relief="solid")
colors_frame.pack(padx=10, pady=10, side="left", anchor="e")
# Create a frame for the Rainbow Colors control section
rainbow_colors_frame = tk.Frame(colors_frame)
rainbow_colors_frame.pack(pady=10, side="right", anchor="e")
# Create a label for the Rainbow Colors control section
rainbow_colors_label = tk.Label(rainbow_colors_frame, text="Rainbow Colors")
rainbow_colors_label.pack(pady=5)
# Create a start button for the Rainbow Colors control section
start_rainbow_button = tk.Button(rainbow_colors_frame, text="Start", command=lambda: run_in_thread(start_rainbow_colors))
start_rainbow_button.pack(pady=5)
# select color
select_color_button = tk.Button(rainbow_colors_frame, text="Select Color", command=lambda: run_in_thread(select_color))
select_color_button.pack(pady=5)
# Create a frame for the Wave Colors control section
wave_colors_frame = tk.Frame(colors_frame)
wave_colors_frame.pack(pady=10)
# Create a label for the Wave Colors control section
wave_colors_label = tk.Label(wave_colors_frame, text="Wave Colors")
wave_colors_label.pack(pady=5)
# Create a start button for the Wave Colors control section
start_wave_button = tk.Button(wave_colors_frame, text="Start", command=lambda: run_in_thread(lambda: wave_colors(1)))
start_wave_button.pack(pady=5)
# Create an inverted start button for the Wave Colors control section
start_inverted_wave_button = tk.Button(wave_colors_frame, text="Start Inverted", command=lambda: run_in_thread(lambda: wave_colors(-1)))
start_inverted_wave_button.pack(pady=5)
# Create a frame for the brightness slider
brightness_and_speed_frame = tk.Frame(root, bd=1, relief="solid")
brightness_and_speed_frame.pack(padx=10, pady=10, side="left", anchor="e")
# Create a label for the brightness slider
brightness_label = tk.Label(brightness_and_speed_frame, text="Brightness")
brightness_label.pack(pady=5)
# Create a slider for controlling brightness
brightness_slider = tk.Scale(brightness_and_speed_frame, from_=0, to=255, orient="horizontal")
brightness_slider.pack()
# Create a label for the speed slider
speed_label = tk.Label(brightness_and_speed_frame, text="Speed")
speed_label.pack(pady=5)
# Create a slider for controlling speed
speed_slider = tk.Scale(brightness_and_speed_frame, from_=0, to=255, orient="horizontal")
speed_slider.pack()
should_stop = False
lock = threading.Lock()
async def cycle_rainbow_colors():
global should_stop
while True:
with lock:
if should_stop:
break
for hue in range(0, 65535, 5000):
color = [int(x*255) for x in colorsys.hsv_to_rgb(hue/65535, 1, 1)]
brightness = brightness_slider.get()
for room in rooms:
for light_id in rooms[room]:
light = b.get_light(light_id)
if light['state']['on']:
b.set_light(light_id, {'hue': hue, 'bri': brightness, 'sat': 255})
await asyncio.sleep(0.5)
def start_rainbow_colors():
asyncio.run(cycle_rainbow_colors())
def run_in_thread(target):
thread = threading.Thread(target=target)
thread.start()
def stop_function():
global should_stop
should_stop = True
# Create a stop button for the Rainbow Colors control section
stop_rainbow_button = tk.Button(rainbow_colors_frame, text="Stop", command=stop_function)
stop_rainbow_button.pack(pady=5)
def wave_colors(direction=1):
rooms = [
[13],
[12, 4],
[16, 5, 24],
[14, 23, 18],
[8, 10, 21, 11],
[17, 7, 22, 6],
[15, 20, 19],
[9]
]
# Define a list of colors to cycle through
colors = [
(0, 255, 0), # green
(255, 165, 0), # orange
(255, 0, 0), # red
(0, 0, 255), # blue
]
# Convert RGB colors to HSL
hsl_colors = [(r/255, g/255, b/255) for r, g, b in colors]
# Calculate the hue step size based on the number of colors and the number of rows
hue_step = 1 / (len(hsl_colors) * len(rooms))
# Keep track of the current hue
current_hue = 0
# Start the wave loop
while True:
global should_stop
for row_index, row in enumerate(rooms[::direction]):
if should_stop:
break
for light_id in row:
# Calculate the hue for this light based on the current hue and the row index
hue = current_hue + row_index * hue_step
# Convert the hue to an RGB color
rgb = colorsys.hsv_to_rgb(hue % 1, 1, 1)
bri = brightness_slider.get()
# Set the light color
hue_value = int(hue * 65535)
if hue_value > 65535:
hue_value = hue_value % 65535
b.set_light(light_id, {'hue': hue_value, 'bri': bri, 'sat': 255})
# Wait for the appropriate amount of time based on the speed adjuster value
time.sleep(speed_slider.get() / 1000)
# Increment the current hue
current_hue += hue_step
# Wait for a short period before starting the next iteration
time.sleep(speed_slider.get() / 1000)
def select_color():
try:
color = tkc.askcolor()
for room in rooms:
for light_id in rooms[room]:
light = b.get_light(light_id)
if light['state']['on']:
hue = int(color[1][0] * 65535)
sat = int(color[1][1] * 255)
bri = brightness_slider.get()
b.set_light(light_id, {'hue': hue, 'bri': bri, 'sat': sat})
except TclError:
# turn off all lights if user cancels color selection
for room in rooms:
for light_id in rooms[room]:
b.set_light(light_id, 'on', False)
root.mainloop()