Repository navigation
Expand file tree
/
Copy pathfunctions.py
More file actions
139 lines (113 loc) · 4.58 KB
/
Copy pathfunctions.py
File metadata and controls
139 lines (113 loc) · 4.58 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
import requests
import statefile as cnf
import time
import traceback
import typing
from collections.abc import Sequence
ip_address=None
from ip_address import ip_address
if ip_address is None: ip_address="192.168.0.109"
try:
import tkinter as tk
import tkinter.ttk as ttk
from tkcolorpicker import askcolor
except Exception as e: print(e)
import colorsys
state=cnf.state
PCA9685=1
limit=4095 if PCA9685 else 1024
state= [255,255,0] if state is None else list(state)
hsv=list(colorsys.rgb_to_hsv(*state))
try:
root = tk.Tk()
except Exception as e:
print(e)
def on_mousewheel(event) -> None:
'''If the widget over which the mousewheel was scrolled is a scale, its value will get moved by the wheel in the corresponding direction'''
d=event.delta/120*(limit>>7)
if event.widget.widgetName=='scale': event.widget.set(event.widget.get()+d)
print(type(event))
return
def multBrightness(rgb: list, brightness: int):
'''This Function combines the Brightness values with the raw RGB values to adjust them accordingly, returning the adjusted RGB values.'''
rgbb=[int(i/limit*brightness) for i in rgb]
return rgbb
def hsv2rgb(hsv: list) -> list:
'''Converts a list containing HSV values into and returns a list containing RGB values'''
hsv=[hsv[0]/limit, hsv[1]/limit, hsv[2]]
rgb=[int(i) for i in list(colorsys.hsv_to_rgb(*hsv))]
return(rgb)
def pickColor() -> None:
'''Opens up a tkinter window to pick a color from the palette. Upon closing the window it sends the value to the LED controller'''
global state
try:
rgb=getData()[1:]
color = list(askcolor( tuple( [int(s*255/limit) for s in rgb] ), root)[0] )
rgb = [int(s*limit/255) for s in color ]
sendMore(rgb=rgb)
saveState(rgb)
except Exception as e:
print('No Color Picked')
print(e)
return
def sendMore(rgb: list = None, w: int = None, toggle: bool = None, enable: bool = None, wrgb: list = None, debug: bool = 0, diff: bool = None) -> list:
'''This function sends the color/white values aswell as the toggle or enable flag to the LED controller.
The values for the LEDs can be passed separately as RGB and W or combined as WRGB.
The set values are then received as a response and are then returned by the function'''
data={}
if wrgb is not None:
w=wrgb[0]
rgb=wrgb[1:]
if rgb is not None:
data['R']=rgb[0]
data['G']=rgb[1]
data['B']=rgb[2]
if w is not None:
data['W']=w
if diff is not None: data['Diff']=diff
if enable is not None: data['enable']= enable
elif toggle is not None: data['toggle']= toggle
try:
r=requests.post(f'http://{ip_address}/data/', data=data)
data=handleResponse(r)
if debug: print(data)
except Exception as e:
print('problem posting information')
print(e)
traceback.print_exc()
return data
def getData(debug=0) -> list:
'''This function retrieves the data from the LED controller by sending an empty request.
The response contains the current values of the LEDs which is returned as a list'''
try:
r=requests.post(f'http://{ip_address}/data/')
data=handleResponse(r)
if debug: print(data)
except Exception as e:
print('problem posting information')
print(e)
traceback.print_exc()
data = cnf.state.append(0)
return data
def saveState(rgb: list) -> None:
'''This saves the current LED state to a local file.
Its use is getting deprecated since the LED state can be received as a response from the LED controller at any moment.'''
with open('statefile.py', 'w') as f: f.write('state=['+str(rgb[0]) +","+str(rgb[1]) +","+str(rgb[2]) +"]")
state=rgb
return
def handleResponse(r) -> list:
'''This parses the text response from the LED controller server to form a list containing the LED values.
Carefull. This function and the server response need to match in order to handle the text succesfully'''
data=[int(float(i)) for i in r.text[6:].split(', ')]
return data
def fadeTo(next: list, t: int = 1) -> None:
'''This function fades the LED lights to the colors passed as the argument.
The keyworded argument 't' defines the ime in seconds it should take to fade to the next color with a default value of 1.'''
steps=t*50
prev=getData()
diff=[next[i]-prev[i] for i in range(len(next))]
for i in range(steps+1):
wrgb=[int(prev[j]+diff[j]/steps*i) for j in range(len(prev))]
sendMore(wrgb=wrgb)
time.sleep(0.02)
return