-
Notifications
You must be signed in to change notification settings - Fork 0
/
lights_pysimplegui.py
246 lines (201 loc) · 7.48 KB
/
lights_pysimplegui.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import os
from pprint import pprint
from time import sleep
import PySimpleGUI as sg
import serial
from serial.tools.list_ports import comports
from lights import Communication, DyNet1, MockSerial
rpi = False
if 'raspberrypi' in os.uname():
rpi = True
from rpi_backlight import Backlight
backlight = Backlight()
print(backlight.brightness)
with backlight.fade(duration=2):
backlight.brightness = 26
for port in comports():
# print(dir(port))
# print(port.device)
ser = serial.Serial(port.device, 9600, timeout=10) # open serial port
print(ser.name) # check which port was really used
lights = Communication(ser)
break
else:
lights = Communication(MockSerial)
# a = DyNet1(0, 254, 3, 2, 1)
# a, a.render()
# lights.enqueue(DyNet1(0,1,2,3,4))
# lights.send_queue()
# f'{255:02x}', int('ff', 16)
grids = (13, 4)
tab_common_layout = [
[
sg.Button('Wall On', size=grids),
sg.Button('Wall Off', size=grids),
sg.Button('Full Brightness', size=grids),
],
[
sg.Button('Set b 1', size=grids),
sg.Button('Set b 2', size=grids),
sg.Button('Set b 3', size=grids),
],
[
sg.Button('Set c 1', size=grids),
sg.Button('Set c 2', size=grids),
sg.Button('Set c 3', size=grids),
],
]
gridsfourbytwo = (12, 7)
tab_stage_layout = [
[
sg.Button('Setup A', size=gridsfourbytwo),
sg.Button('Setup B', size=gridsfourbytwo),
sg.Button('Setup C', size=gridsfourbytwo),
sg.Button('Setup D', size=gridsfourbytwo),
],
[
sg.Button('Setup E', size=gridsfourbytwo),
sg.Button('Setup F', size=gridsfourbytwo),
sg.Button('Setup G', size=gridsfourbytwo),
sg.Button('All Off', size=gridsfourbytwo, button_color='black on red'),
],
]
gridseightbytwo = (4, 4)
tab_adjust_layout = [
[
sg.Button('↑ 1', size=gridseightbytwo),
sg.Button('↑ 2', size=gridseightbytwo),
sg.Button('↑ 3', size=gridseightbytwo),
sg.Button('↑ 4', size=gridseightbytwo),
sg.Button('↑ 5', size=gridseightbytwo),
sg.Button('↑ 6', size=gridseightbytwo),
sg.Button('↑ 7', size=gridseightbytwo),
sg.Button('↑ 8', size=gridseightbytwo),
],
[
sg.Button('↓ 1', size=gridseightbytwo),
sg.Button('↓ 2', size=gridseightbytwo),
sg.Button('↓ 3', size=gridseightbytwo),
sg.Button('↓ 4', size=gridseightbytwo),
sg.Button('↓ 5', size=gridseightbytwo),
sg.Button('↓ 6', size=gridseightbytwo),
sg.Button('↓ 7', size=gridseightbytwo),
sg.Button('↓ 8', size=gridseightbytwo),
],
]
col_manual_labels = [
[sg.Text('Area', pad=((0, 0), (25, 20)))],
[sg.Text('Data 1', pad=((0, 0), (0, 20)))],
[sg.Text('OpCode', pad=((0, 0), (0, 20)))],
[sg.Text('Data 2', pad=((0, 0), (0, 20)))],
[sg.Text('Data 3', pad=((0, 0), (0, 20)))],
]
col_manual_sliders = [
[sg.Slider((0, 16), 0, 1, orientation="h", size=(40, 15), key="-Manual Area-", enable_events=True)],
[sg.Slider((0, 255), 128, 1, orientation="h", size=(40, 15), key="-Manual Data 1-", enable_events=True)],
[sg.Slider((0, 255), 128, 1, orientation="h", size=(40, 15), key="-Manual OpCode-", enable_events=True)],
[sg.Slider((0, 255), 128, 1, orientation="h", size=(40, 15), key="-Manual Data 2-", enable_events=True)],
[sg.Slider((0, 255), 128, 1, orientation="h", size=(40, 15), key="-Manual Data 3-", enable_events=True)],
]
tab_manual_control_layout = [
[sg.Column(col_manual_labels), sg.Column(col_manual_sliders)],
[sg.Text('', size=(58, 1), key='render repr', font=('Helvetica', 14, 'bold'))],
[sg.Text('', size=(16, 1), key='render hex', font=('Helvetica', 14, 'bold'))],
[sg.Button('Send'), sg.Button('Send All')],
]
tab_backlight_control_layout = [
[sg.Text('Backlight')],
[sg.Slider((6, 100), 26, 2, orientation="h", size=(40, 15), key="Backlight", enable_events=True)],
]
tab_exit_layout = [[sg.Text("Exit from City Lights UI")], [sg.Button("Exit", key='--Exit--')]]
padding = ' ' * 2
layout = [[
sg.TabGroup([[
sg.Tab(padding + 'Presets' + padding, tab_stage_layout),
sg.Tab(padding + 'Adjust' + padding, tab_adjust_layout, visible=True),
sg.Tab(padding + 'Common' + padding, tab_common_layout, visible=False),
sg.Tab(padding + 'Manual' + padding, tab_manual_control_layout, visible=True),
sg.Tab(padding + 'Backlight' + padding, tab_backlight_control_layout, visible=True),
sg.Tab(padding + 'Exit' + padding, tab_exit_layout, visible=True),
]], border_width=0)
]]
# sg.SetOptions(element_padding=(40,20))
# sg.SetOptions()
window = sg.Window(
'Lights', # Title
layout,
no_titlebar = True if rpi else False,
location = (0, 0) if rpi else (50, 50),
size=(800, 480),
keep_on_top=False,
# auto_size_buttons=True,
# auto_size_text=True,
font=('Helvetica', 18 if rpi else 14, 'bold'),
# default_element_size=(30, 1)
)
lastsetup = None
# Create an event loop
while True:
event, values = window.read(timeout=10000) # set time out
# count consecutive no UI event periods, raise a lock screen
if event == "--Exit--" or event == sg.WIN_CLOSED:
break
area = int(values['-Manual Area-'])
data1 = int(values['-Manual Data 1-'])
opcode = int(values['-Manual OpCode-'])
data2 = int(values['-Manual Data 2-'])
data3 = int(values['-Manual Data 3-'])
dynet = DyNet1(area, data1, opcode, data2, data3)
if event.startswith('-Manual'):
window['render hex'].Update(dynet.render())
window['render repr'].Update(dynet)
if event == 'Send':
lights.send(dynet)
if event == 'Send All':
for area in [1, 2, 3, 4, 5, 6, 7, 8]:
lights.send(DyNet1(area, data1, opcode, data2, data3))
if event == 'Wall On':
for area in [1, 2, 3, 4, 5, 6, 7, 8]:
lights.enqueue(DyNet1(area, 255, 0, 0, 1))
lights.send_queue()
if event == 'Wall Off':
for area in [1, 2, 3, 4, 5, 6, 7, 8]:
lights.enqueue(DyNet1(area, 255, 3, 0, 0))
lights.send_queue()
if event == 'Full Brightness':
for area in [1, 2, 3, 4, 5, 6, 7, 8]:
lights.enqueue(DyNet1(area, 255, 0, 0, 2))
lights.send_queue()
rate = 64
if event == 'Setup A':
lights.send(DyNet1(0, rate, 0, 0, 0))
if event == 'Setup B':
lights.send(DyNet1(0, rate, 1, 0, 0))
if event == 'Setup C':
lights.send(DyNet1(0, rate, 2, 0, 0))
if event == 'Setup D':
lights.send(DyNet1(0, rate, 10, 0, 0))
if event == 'Setup E':
lights.send(DyNet1(0, rate, 11, 0, 0))
if event == 'Setup F':
lights.send(DyNet1(0, rate, 12, 0, 0))
if event == 'Setup G':
lights.send(DyNet1(0, rate, 13, 0, 0))
if event == 'All Off':
lights.send(DyNet1(0, 255, 13, 0, 0))
if event.startswith('↓') or event.startswith('↑'):
# Adjust an area 1 count ↑ or ↓
part = event.split(' ')
heading, area = part[0], int(part[1])
if heading == '↑':
lights.send(DyNet1(area, 20, 6, 0, 0))
if heading == '↓':
lights.send(DyNet1(area, 20, 5, 0, 0))
if event == 'Backlight':
if 'raspberrypi' in os.uname():
backlight.brightness = int(values['Backlight'])
print(f'Event: {event}')
if event.startswith('Setup'):
lastsetup = event.split(' ')[1]
print(f'Last Setup: {lastsetup}')
window.close()