-
Notifications
You must be signed in to change notification settings - Fork 0
/
LEDDriverUI.py
319 lines (270 loc) · 11.5 KB
/
LEDDriverUI.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
from WrapperClasses.LampController import LampController
from PyQt5 import QtCore, QtWidgets, uic
import sys
import time
class LEDDriverUI(QtWidgets.QMainWindow):
"""
Standalone GUI for lighting control.
"""
def __init__(self):
super(LEDDriverUI, self).__init__() # Call the inherited classes __init__ method
uic.loadUi(r'C:\Users\User\PycharmProjects\SpintronicsMOKE\res\LED_driver_UI.ui', self) # Load the .ui file
self.show()
# Define enums
self.led_binary_enum = {
"left1": 2,
"left2": 1,
"right1": 8,
"right2": 4,
"up1": 32,
"up2": 16,
"down1": 128,
"down2": 64}
self.led_id_enum = {
"left1": 2,
"left2": 1,
"right1": 4,
"right2": 3,
"up1": 6,
"up2": 5,
"down1": 8,
"down2": 7}
self.enabled_leds_spi = {"left1": False,
"left2": False,
"right1": False,
"right2": False,
"up1": False,
"up2": False,
"down1": False,
"down2": False}
self.LED_brightnesses = {"left1": 180,
"left2": 180,
"right1": 180,
"right2": 180,
"up1": 180,
"up2": 180,
"down1": 180,
"down2": 180}
self.enabled_led_pairs = {"left": False,
"right": False,
"up": False,
"down": False}
self.control_all = False
# Connect Signals
self.button_left_led1.clicked.connect(self.__on_individual_led)
self.button_right_led1.clicked.connect(self.__on_individual_led)
self.button_up_led1.clicked.connect(self.__on_individual_led)
self.button_down_led1.clicked.connect(self.__on_individual_led)
self.button_left_led2.clicked.connect(self.__on_individual_led)
self.button_right_led2.clicked.connect(self.__on_individual_led)
self.button_up_led2.clicked.connect(self.__on_individual_led)
self.button_down_led2.clicked.connect(self.__on_individual_led)
self.button_leds_off.clicked.connect(self.__disable_all_leds)
self.button_long_pol.clicked.connect(self.__on_long_pol)
self.button_trans_pol.clicked.connect(self.__on_trans_pol)
self.button_polar.clicked.connect(self.__on_polar)
self.button_control_all.clicked.connect(self.__on_control_change)
self.button_reset_brightness.clicked.connect(self.__reset_brightness)
self.scroll_LED_brightness.valueChanged.connect(self.__on_brightness_slider)
self.scroll_LED_brightness.setSingleStep(-1)
self.scroll_blocker = QtCore.QSignalBlocker(self.scroll_LED_brightness)
self.scroll_blocker.unblock()
# Start the LED control
self.lamp_controller = LampController()
self.lamp_controller.disable_all()
def __reset_pairs(self):
self.enabled_led_pairs.update(
{"left": False,
"right": False,
"up": False,
"down": False})
def __reset_led_spis(self):
self.enabled_leds_spi.update(
{"left1": False,
"left2": False,
"right1": False,
"right2": False,
"up1": False,
"up2": False,
"down1": False,
"down2": False}
)
def __reset_brightness(self):
self.LED_brightnesses.update(
{"left1": 180,
"left2": 180,
"right1": 180,
"right2": 180,
"up1": 180,
"up2": 180,
"down1": 180,
"down2": 180}
)
self.lamp_controller.set_all_brightness(180)
def __disable_all_leds(self):
print("ArtieLabUI: Disabling all LEDs")
self.__reset_led_spis()
self.__reset_pairs()
self.button_long_pol.setChecked(False)
self.button_trans_pol.setChecked(False)
self.button_polar.setChecked(False)
self.button_up_led1.setChecked(False)
self.button_up_led2.setChecked(False)
self.button_down_led1.setChecked(False)
self.button_down_led2.setChecked(False)
self.button_left_led1.setChecked(False)
self.button_left_led2.setChecked(False)
self.button_right_led1.setChecked(False)
self.button_right_led2.setChecked(False)
self.__update_controller_pairs()
def __on_individual_led(self, state):
if self.__check_for_any_active_mode():
self.button_long_pol.setChecked(False)
self.button_trans_pol.setChecked(False)
self.button_polar.setChecked(False)
self.__update_active_LEDs()
self.__update_controller_spi()
def __on_long_pol(self, checked):
if checked:
self.enabled_led_pairs.update(
{"left": False,
"right": False,
"up": True,
"down": False})
self.button_up_led1.setChecked(True)
self.button_up_led2.setChecked(True)
self.button_down_led1.setChecked(False)
self.button_down_led2.setChecked(False)
self.button_left_led1.setChecked(False)
self.button_left_led2.setChecked(False)
self.button_right_led1.setChecked(False)
self.button_right_led2.setChecked(False)
self.button_trans_pol.setChecked(False)
self.button_polar.setChecked(False)
self.__update_active_LEDs()
self.__update_controller_pairs()
else:
if not self.__check_for_any_active_mode():
self.__disable_all_leds()
def __on_trans_pol(self, checked):
if checked:
self.enabled_led_pairs.update({"left": True,
"right": False,
"up": False,
"down": False})
self.button_up_led1.setChecked(False)
self.button_up_led2.setChecked(False)
self.button_down_led1.setChecked(False)
self.button_down_led2.setChecked(False)
self.button_left_led1.setChecked(True)
self.button_left_led2.setChecked(True)
self.button_right_led1.setChecked(False)
self.button_right_led2.setChecked(False)
self.button_long_pol.setChecked(False)
self.button_polar.setChecked(False)
self.__update_active_LEDs()
self.__update_controller_pairs()
else:
if not self.__check_for_any_active_mode():
self.__disable_all_leds()
def __on_polar(self, checked):
if checked:
self.__reset_pairs()
self.button_up_led1.setChecked(True)
self.button_up_led2.setChecked(False)
self.button_down_led1.setChecked(True)
self.button_down_led2.setChecked(False)
self.button_left_led1.setChecked(False)
self.button_left_led2.setChecked(False)
self.button_right_led1.setChecked(False)
self.button_right_led2.setChecked(False)
self.button_long_pol.setChecked(False)
self.button_trans_pol.setChecked(False)
self.__update_active_LEDs()
self.__update_controller_spi()
else:
if not self.__check_for_any_active_mode():
self.__disable_all_leds()
def __check_for_any_active_mode(self):
return bool(
self.button_long_pol.isChecked() +
self.button_trans_pol.isChecked() +
self.button_polar.isChecked()
)
def __update_active_LEDs(self):
self.enabled_leds_spi["up1"] = self.button_up_led1.isChecked()
self.enabled_leds_spi["up2"] = self.button_up_led2.isChecked()
self.enabled_leds_spi["down1"] = self.button_down_led1.isChecked()
self.enabled_leds_spi["down2"] = self.button_down_led2.isChecked()
self.enabled_leds_spi["left1"] = self.button_left_led1.isChecked()
self.enabled_leds_spi["left2"] = self.button_left_led2.isChecked()
self.enabled_leds_spi["right1"] = self.button_right_led1.isChecked()
self.enabled_leds_spi["right2"] = self.button_right_led2.isChecked()
self.__update_brightness_slider()
def __update_controller_pairs(self):
self.lamp_controller.enable_assortment_pairs(self.enabled_led_pairs)
def __update_controller_spi(self):
keys = self.led_binary_enum.keys()
value = 0
for key in keys:
value += self.led_binary_enum[key] * self.enabled_leds_spi[key]
self.lamp_controller.enable_leds_using_SPI(value)
def __on_control_change(self, control_all):
self.control_all = control_all
if self.control_all:
self.button_control_all.setText("Control\nSelected")
else:
self.button_control_all.setText("Control\nAll")
self.__update_brightness(180 - self.scroll_LED_brightness.value())
def __on_brightness_slider(self, value):
value = 180 - value
print("Slider Value Changed to: ", value)
self.__update_brightness(value)
def __update_brightness_slider(self):
if self.control_all:
keys = self.LED_brightnesses.keys()
else:
keys = [key for key, value in self.enabled_leds_spi.items() if value is True]
if keys:
print(keys)
brightest_val = max([self.LED_brightnesses[key] for key in keys])
print("Brightest val: ", brightest_val)
self.scroll_blocker.reblock()
self.scroll_LED_brightness.setValue(180-brightest_val)
self.scroll_blocker.unblock()
self.__update_brightness(brightest_val)
def __update_brightness(self, value):
if self.control_all:
keys = self.LED_brightnesses.keys()
else:
keys = [key for key, value in self.enabled_leds_spi.items() if value is True]
if self.control_all:
self.LED_brightnesses = {key: value for key in self.LED_brightnesses}
self.lamp_controller.set_all_brightness(value)
else:
for key in keys:
self.LED_brightnesses[key] = value
self.lamp_controller.set_some_brightness([value] * len(keys), [self.led_id_enum[key] for key in keys])
def closeEvent(self, event):
self.lamp_controller.close(reset=True)
print("LEDDriverUI: Closing threads and exiting")
super(LEDDriverUI, self).closeEvent(event)
sys.exit()
if __name__ == '__main__':
# Back up the reference to the exceptionhook
sys._excepthook = sys.excepthook
def my_exception_hook(exctype, value, traceback):
# Print the error and traceback
print(exctype, value, traceback)
# Call the normal Exception hook after
sys._excepthook(exctype, value, traceback)
sys.exit(1)
# Set the exception hook to our wrapping function
sys.excepthook = my_exception_hook
app = QtWidgets.QApplication(sys.argv)
app.setStyle('fusion')
window = LEDDriverUI()
try:
sys.exit(app.exec_())
except:
print("Exiting")