-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathled_array_interface.py
More file actions
339 lines (228 loc) · 8.35 KB
/
led_array_interface.py
File metadata and controls
339 lines (228 loc) · 8.35 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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env python
# coding: utf-8
# # GUI Interface for LED Array
# ## When used controled by Tektronix PWS4305
#
# ## Requirements
# Ensure that `import-ipynb` module is installed
#
# ## Compiling
# 1. Ensure fbs is installed `pip install fbs`
# 2. Iniate a project `python3 -m fbs startproject`
# 3. Freeze the binary `python3 -m fbs freeze`
# 4. Create an installer `python3 -m fbs installer`
#
# ## Converting to .py
# To save this file for use as a CLI, convert it to a .py file using `jupyter nbconvert --to python <filename>`
# In[1]:
import os
import sys
import re
from collections import namedtuple
# PyQt
from PyQt5 import QtGui
from PyQt5.QtCore import (
Qt,
QCoreApplication,
QTimer,
QThread
)
from PyQt5.QtWidgets import (
QApplication,
QWidget,
QVBoxLayout,
QHBoxLayout,
QFormLayout,
QLabel,
QPushButton,
QComboBox,
QDoubleSpinBox,
QSlider,
QMessageBox
)
# controller
# import import_ipynb # FREEZE
import led_array_controller as lac
import visa
# In[8]:
class LedArrayInterface( QWidget ):
#--- window close ---
def closeEvent( self, event ):
self.__delete_controller()
event.accept()
#--- destructor ---
def __del__( self ):
self.__delete_controller()
#--- initializer ---
# def __init__( self, resources ): # FREEZE
def __init__( self ):
super().__init__()
#--- instance variables ---
# image_folder = resources + '/images/' # FREEZE
image_folder = os.getcwd() + '/images/'
self.img_redLight = QtGui.QPixmap( image_folder + 'red-light.png' ).scaledToHeight( 32 )
self.img_greenLight = QtGui.QPixmap( image_folder + 'green-light.png' ).scaledToHeight( 32 )
self.img_yellowLight = QtGui.QPixmap( image_folder + 'yellow-light.png' ).scaledToHeight( 32 )
self.inst = None # the instrument
#--- timers ---
#--- init UI ---
self.__init_ui()
self.__register_connections()
#--- init variables ---
# self.__updatePort()
def __init_ui( self ):
#--- main window ---
self.setGeometry( 100, 100, 200, 300 )
self.setWindowTitle( 'LED Array Controller' )
lo_main = QVBoxLayout()
lo_main.addLayout( self.__ui_mainToolbar() )
lo_main.addLayout( self.__ui_settings() )
lo_main.addSpacing( 35 )
lo_main.addLayout( self.__ui_commands() )
self.setLayout( lo_main )
self.show()
def __ui_mainToolbar( self ):
lo_mainToolbar = QHBoxLayout()
self.__ui_mainToolbar_connect( lo_mainToolbar )
return lo_mainToolbar
def __ui_settings( self ):
lo_settings = QVBoxLayout()
lo_row_1 = QHBoxLayout()
self.__ui_settings_intensity( lo_row_1 )
lo_settings.addLayout( lo_row_1 )
return lo_settings
def __ui_commands( self ):
lo_commands = QVBoxLayout()
self.__ui_on( lo_commands )
return lo_commands
def __ui_mainToolbar_connect( self, parent ):
# connect / disconnect
self.lbl_statusLight = QLabel()
self.lbl_statusLight.setAlignment( Qt.AlignCenter )
self.lbl_statusLight.setPixmap( self.img_redLight )
self.lbl_status = QLabel( 'Disconnected' )
self.btn_connect = QPushButton( 'Connect' )
lo_statusView = QVBoxLayout()
lo_statusView.addWidget( self.lbl_statusLight )
lo_statusView.addWidget( self.lbl_status )
lo_statusView.setAlignment( Qt.AlignHCenter )
lo_status = QHBoxLayout()
lo_status.addLayout( lo_statusView )
lo_status.addWidget( self.btn_connect )
lo_status.setAlignment( Qt.AlignCenter )
parent.addLayout( lo_status )
def __ui_settings_intensity( self, parent ):
sb_intensity = QDoubleSpinBox()
sb_intensity.setMinimum( 0 )
sb_intensity.setMaximum( 1.1 )
sb_intensity.setSingleStep( 0.1 )
self.sb_intensity = sb_intensity
lbl_intensity = QLabel( 'Intensity' )
lo_intensity = QHBoxLayout()
lo_intensity.addWidget( lbl_intensity )
lo_intensity.addWidget( sb_intensity )
parent.addLayout( lo_intensity )
def __ui_on( self, parent ):
lo_on = QHBoxLayout()
self.btn_on = QPushButton( 'On' )
lo_on.addWidget( self.btn_on )
parent.addLayout( lo_on )
#--- ui functionality ---
def __register_connections( self ):
self.btn_connect.clicked.connect( self.toggle_connect )
self.btn_on.clicked.connect( self.toggle_on )
self.sb_intensity.valueChanged.connect( self.set_intensity )
#--- slot functions ---
def toggle_connect( self ):
"""
Toggles connection between selected com port
"""
# show waiting for communication
self.lbl_status.setText( 'Waiting...' )
self.lbl_statusLight.setPixmap( self.img_yellowLight )
self.repaint()
# create laser controller if doesn't already exist, connect
if self.inst is None:
try:
self.inst = lac.LedArray()
self.inst.connect()
except Exception as err:
self.__update_connected_ui( False )
warning = QMessageBox()
warning.setWindowTitle( 'LED Array Error' )
warning.setText( 'Could not connect\n{}'.format( err ) )
warning.exec()
else:
self.__delete_controller()
# update ui
if self.inst is not None:
self.__update_connected_ui( self.inst.connected )
else:
self.__update_connected_ui( False )
# set voltage and current
self.set_intensity()
def toggle_on( self ):
if self.inst is None:
# not connected
return
method = self.btn_on.text()
if method == 'On':
self.on()
self.btn_on.setText( 'Off' )
if method == 'Off':
self.off()
self.btn_on.setText( 'On' )
def on( self ):
try:
self.inst.on()
except visa.VisaIOError as err:
self.__handle_visa_error( err )
def off( self ):
try:
self.inst.off()
except visa.VisaIOError as err:
self.__handle_visa_error( err )
def set_intensity( self ):
if self.inst is not None:
self.inst.intensity = self.sb_intensity.value()
#--- helper functions ---
def __delete_controller( self ):
if self.inst is not None:
self.off()
self.inst.disconnect()
del self.inst
self.inst = None
def __update_connected_ui( self, connected ):
if connected == True:
statusText = 'Connected'
statusLight = self.img_greenLight
btnText = 'Disconnect'
elif connected == False:
statusText = 'Disconnected'
statusLight = self.img_redLight
btnText = 'Connect'
else:
statusText = 'Error'
statusLight = self.img_yellowLight
btnText = 'Connect'
self.lbl_status.setText( statusText )
self.lbl_statusLight.setPixmap( statusLight )
self.btn_connect.setText( btnText )
def __handle_visa_error( self, err ):
warning = QMessageBox()
warning.setWindowTitle( 'LED Array Controller Error' )
warning.setText( 'Communication timeout' )
warning.exec()
self.__update_connected_ui( False )
# In[7]:
# FREEZE
# app = QCoreApplication.instance()
# if app is None:
# app = QApplication( sys.argv )
# main_window = LedArrayInterface()
# sys.exit( app.exec_() )
# In[6]:
# FREEZE
# %load_ext autoreload
# %autoreload 1
# In[ ]: