-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
156 lines (129 loc) · 3.98 KB
/
main.py
File metadata and controls
156 lines (129 loc) · 3.98 KB
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
import platform
import requests as rq
import threading
import time
import numpy
import urllib3
import keyboard
import os
import sys
from colorsys import hsv_to_rgb
from rgbxy import Converter
from rgbxy import GamutC
import light_specific_vars
import light_utils
if platform.system() == 'Linux':
import termios
import tty
clear_command = 'clear'
elif platform.system() == 'Windows':
clear_command = 'cls'
converter = Converter(GamutC)
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) # Disable warning about insecure connection
header = light_specific_vars.header
light_url = light_specific_vars.light_url
break_from_function = False
def hsv2rgb(h, s, v):
return tuple(round(i * 255) for i in hsv_to_rgb(h, s, v))
def hsv2xy(h):
r, g, b = hsv2rgb(h, 1, 1)
x, y = converter.rgb_to_xy(r, g, b)
return x, y
def change_color(x, y):
r = rq.put(light_url, headers=header,
json={
"color": {
"xy": {
"x": x,
"y": y
}
}
}, verify=False)
# print(it)
if (r.status_code == 200) or (r.status_code == 207):
pass
else:
print(r.status_code)
time.sleep(0.1)
def listen_for_break_key():
global break_from_function
if platform.system() == 'Linux':
orig_settings = termios.tcgetattr(sys.stdin)
tty.setcbreak(sys.stdin)
key = ''
while key != 'q':
key = sys.stdin.read(1)
break_from_function = True
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, orig_settings)
else:
while True:
if keyboard.is_pressed('q'):
break_from_function = True
break
# "keyboard" library requires root access in Linux environment, so different method is used in the "if" block
# source : https://copyprogramming.com/howto/python-detect-which-key-is-pressed-python-linux
def change_power():
os.system(clear_command)
light_utils.change_power()
os.system(clear_command)
def set_brightness_level():
os.system(clear_command)
brightness_level = int(input('Enter brightness level: '))
if brightness_level > 100:
brightness_level = 100
elif brightness_level < 0:
brightness_level = 0
light_utils.change_brightness(brightness_level)
os.system(clear_command)
def cycle_colors():
while True:
for j in numpy.arange(0, 1.01, 0.01):
if break_from_function:
return
x, y = hsv2xy(j)
change_color(x, y)
def cycle_colors_option():
os.system(clear_command)
print('Cycling through colors...')
print('Press q to quit to main menu!')
cycle_colors_thread = threading.Thread(target=cycle_colors)
listen_for_break_key_thread = threading.Thread(target=listen_for_break_key)
cycle_colors_thread.start()
listen_for_break_key_thread.start()
cycle_colors_thread.join()
listen_for_break_key_thread.join()
cycle_colors()
global break_from_function
break_from_function = False
os.system(clear_command)
def invalid_option():
os.system(clear_command)
print("Choose valid option!")
time.sleep(1)
os.system(clear_command)
def main():
while True:
print('1 - power on/off')
print('2 - brightness level')
print('3 - cycle through colors')
print('4 - exit program')
choice = input('Enter your choice: ')
if choice == '':
invalid_option()
elif choice == '1':
change_power()
elif choice == '2':
set_brightness_level()
elif choice == '3':
cycle_colors_option()
elif choice == '4':
sys.exit()
else:
invalid_option()
if __name__ == "__main__":
main()
# print('Hello!')
# while True:
# if keyboard.is_pressed('q'):
# print('Q pressed!')
# sys.exit()