forked from meantux/GugusseRoller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGugusseGUI.py
More file actions
executable file
·344 lines (294 loc) · 12.1 KB
/
GugusseGUI.py
File metadata and controls
executable file
·344 lines (294 loc) · 12.1 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
336
337
338
339
340
341
342
343
344
import sys
import json
from PyQt5.QtWidgets import (
QApplication,
QMainWindow,
QVBoxLayout,
QHBoxLayout,
QWidget,
QLabel,
QSlider,
QComboBox,
QPushButton,
QLineEdit,
QTextEdit,
QSplitter,
QSizePolicy,
QWidget,
QMessageBox,
)
from PyQt5.QtCore import Qt
from TrinamicSilentMotor import MotorControlWidgets
from GCamera import GCamera
from Lights import LightControlWidget
import CameraSettings
import CaptureSettings
import CaptureLoop
import SensorReport
from ConfigFiles import ConfigFiles
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setWindowTitle("GugusseGUI 2.0")
self.settings = ConfigFiles("GugusseSettings.json")
print(json.dumps(self.settings, indent=4))
fps = self.settings["fps"]
self.picam2 = GCamera(self)
self.picam2.createPreviewWidget()
self.main_layout = QVBoxLayout()
self.out = QTextEdit()
print("--------Available Camera Settings------")
print(json.dumps(self.picam2.camera_controls, indent=4))
print("---------------------------------------")
# topWidget=QWidget()
# top=QHBoxLayout(topWidget)
# topWidget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
# self.main_layout.addLayout(top)
hlayout = QHBoxLayout()
# Row 1: | Exposure stuff
self.AutoExposure = CameraSettings.AutoExposureWidget(self)
hlayout.addWidget(self.AutoExposure.getLabel())
hlayout.addWidget(self.AutoExposure)
self.ExposureDual = CameraSettings.ExposureDualWidget(self)
hlayout.addWidget(self.ExposureDual.getLabel())
hlayout.addWidget(self.ExposureDual)
self.Iso = CameraSettings.IsoWidget(self)
hlayout.addWidget(self.Iso.getLabel())
hlayout.addWidget(self.Iso)
self.main_layout.addLayout(hlayout)
hlayout = QHBoxLayout()
# Row 2: White balance stuff
self.WBMode = CameraSettings.WhiteBalanceModeWidget(self)
hlayout.addWidget(self.WBMode.getLabel())
hlayout.addWidget(self.WBMode)
self.Freeze = CameraSettings.FreezeWidget(self)
hlayout.addWidget(self.Freeze)
self.RedGain = CameraSettings.ColorGainWidget(self, 0)
hlayout.addWidget(self.RedGain.getLabel())
hlayout.addWidget(self.RedGain)
self.BlueGain = CameraSettings.ColorGainWidget(self, 1)
hlayout.addWidget(self.BlueGain.getLabel())
hlayout.addWidget(self.BlueGain)
self.main_layout.addLayout(hlayout)
hlayout = QHBoxLayout()
# Row 3: Post-processing adjustments
self.brightness = CameraSettings.GenericCameraAdjustmentWidget(
self, "Brightness", customMin=-0.5, customMax=0.5
)
hlayout.addWidget(self.brightness.getLabel())
hlayout.addWidget(self.brightness)
self.contrast = CameraSettings.GenericCameraAdjustmentWidget(
self, "Contrast", customMax=2.00
)
hlayout.addWidget(self.contrast.getLabel())
hlayout.addWidget(self.contrast)
self.sharpness = CameraSettings.GenericCameraAdjustmentWidget(
self, "Sharpness", customMax=10.0
)
hlayout.addWidget(self.sharpness.getLabel())
hlayout.addWidget(self.sharpness)
self.saturation = CameraSettings.GenericCameraAdjustmentWidget(
self, "Saturation", customMin=0.01, customMax=1.99
)
hlayout.addWidget(self.saturation.getLabel())
hlayout.addWidget(self.saturation)
self.main_layout.addLayout(hlayout)
# Bottom section divided into left and right
self.bottom_layout = QSplitter(Qt.Horizontal)
left_widget = QWidget()
left_layout = QVBoxLayout()
hlayout = QHBoxLayout()
self.vflip = CameraSettings.FlipWidget(self, "vflip")
self.hflip = CameraSettings.FlipWidget(self, "hflip")
self.saveSettings = CaptureSettings.SaveSettingsWidget(self)
hlayout.addWidget(self.hflip)
hlayout.addWidget(self.vflip)
hlayout.addWidget(self.saveSettings)
left_layout.addLayout(hlayout)
self.hwSettings = ConfigFiles("hardwarecfg.json")
threeMotorsLayout = QHBoxLayout()
self.motors = {}
self.speedmeters = {}
for motor in ["feeder", "filmdrive", "pickup"]:
motorSeparatorLayout = QVBoxLayout()
label = QLabel(motor)
label.setAlignment(Qt.AlignCenter)
label.setStyleSheet("border: 1px solid black;")
motorSeparatorLayout.addWidget(label)
self.speedmeters[motor] = QLabel("peak: ?steps/s")
motorSeparatorLayout.addWidget(self.speedmeters[motor])
threeButtonsLayout = QHBoxLayout()
trace = False
if motor == "filmdrive":
trace = True
self.motors[motor] = MotorControlWidgets(
self, self.hwSettings[motor], trace=trace
)
threeButtonsLayout.addWidget(self.motors[motor].ccw)
threeButtonsLayout.addWidget(self.motors[motor])
threeButtonsLayout.addWidget(self.motors[motor].cw)
motorSeparatorLayout.addLayout(threeButtonsLayout)
threeMotorsLayout.addLayout(motorSeparatorLayout)
left_layout.addLayout(threeMotorsLayout)
# Project name field
self.projectName = CaptureSettings.ProjectNameWidget(self)
hlayout = QHBoxLayout()
self.light_selector = LightControlWidget(self)
hlayout.addWidget(self.light_selector.getLabel())
hlayout.addWidget(self.light_selector)
# Project name row (use a NEW layout, do not reuse the previous one)
hlayout = QHBoxLayout()
hlayout.addWidget(self.projectName.getLabel())
hlayout.addWidget(self.projectName)
self.lastFileLabel = QLabel("...")
hlayout.addWidget(self.lastFileLabel)
left_layout.addLayout(hlayout)
# Capture Mode
self.filmFormat = CaptureSettings.FilmFormatWidget(self)
hlayout = QHBoxLayout()
hlayout.addWidget(self.filmFormat.getLabel())
hlayout.addWidget(self.filmFormat)
self.captureMode = CaptureSettings.CaptureModeWidget(self)
hlayout.addWidget(self.captureMode.getLabel())
hlayout.addWidget(self.captureMode)
left_layout.addLayout(hlayout)
# Reels Direction
self.reelsDirection = CaptureSettings.ReelsDirectionWidget(self)
hlayout = QHBoxLayout()
hlayout.addWidget(self.reelsDirection.getLabel())
hlayout.addWidget(self.reelsDirection)
left_layout.addLayout(hlayout)
# Run/Stop + Take Picture
self.runStop = CaptureLoop.RunStopWidget(self)
self.snapshot = CaptureLoop.SnapshotWidget(self)
hlayout = QHBoxLayout()
hlayout.addWidget(self.snapshot)
hlayout.addWidget(self.runStop)
left_layout.addLayout(hlayout)
self.sensors = SensorReport.SensorsWidgets(self)
left_layout.addLayout(self.sensors)
left_widget.setLayout(left_layout)
self.bottom_layout.addWidget(left_widget)
# Text output area
self.out.setReadOnly(True)
left_layout.addWidget(self.out)
self.main_layout.addWidget(self.bottom_layout)
# Camera preview area
self.bottom_layout.addWidget(self.picam2.camWidget)
widget = QWidget()
widget.setLayout(self.main_layout)
self.setCentralWidget(widget)
# forcing a sync with the auto-exposure value will fix all exposure related values
self.AutoExposure.syncCamera()
self.WBMode.syncCamera()
self.brightness.syncCamera()
self.contrast.syncCamera()
self.saturation.syncCamera()
self.sharpness.syncCamera()
self.hflip.syncCamera() # syncing one syncs the other, and start cam
self.picam2.start()
def disableWidgetsWhenCapture(self):
self.light_selector.setEnabled(False)
self.projectName.setEnabled(False)
self.filmFormat.setEnabled(False)
self.captureMode.setEnabled(False)
self.sensors.learn.setEnabled(False)
self.snapshot.setEnabled(False)
def reenableWidgetsAfterCapture(self):
self.light_selector.setEnabled(True)
self.projectName.setEnabled(True)
self.filmFormat.setEnabled(True)
self.captureMode.setEnabled(True)
self.snapshot.setEnabled(True)
self.sensors.enableLearnIfPossible()
self.motors["feeder"].syncMotorStatus()
self.motors["filmdrive"].syncMotorStatus()
self.motors["pickup"].syncMotorStatus()
def getBottomLayout(self):
return self.bottom_layout
def on_slider_value_changed(self, control, value):
self.out.append(f"Slider value changed for control: {control} to {value}")
def on_light_selector_changed(self, text):
self.out.append(f"Light selector changed to {text}")
def on_selector_changed(self, control, text):
self.out.append(f"Selector changed for control: {control} to {text}")
def on_button_clicked(self, control):
self.bottom_layout.repaint()
self.out.append(f"Button clicked for control: {control}")
def closeEvent(self, event):
if self.saveSettings.thereAreUnsavedSettings():
reply = QMessageBox.question(
self,
"Unsaved Settings",
"There are unsaved settings. Do you want to save before exiting?",
QMessageBox.Save | QMessageBox.Cancel | QMessageBox.Discard,
QMessageBox.Save,
)
if reply == QMessageBox.Save:
# save the work and exit
self.saveSettings.execute()
# Ensure capture loop and threads are cleanly stopped before exit
self._shutdownThreads()
event.accept()
elif reply == QMessageBox.Discard:
# discard the work and exit
self._shutdownThreads()
event.accept()
else:
# cancel the close event
event.ignore()
else:
self._shutdownThreads()
event.accept()
def _shutdownThreads(self):
# Stop snapshot export thread if needed
try:
self.snapshot.disableExportIfRunning()
except Exception:
pass
# If a snapshot capture is underway, wait for it to finish
try:
if hasattr(self.snapshot, "trigger") and self.snapshot.trigger is not None:
if self.snapshot.trigger.isRunning():
self.snapshot.trigger.wait()
except Exception:
pass
# If capture loop is running, stop it and wait for it to finish
try:
if getattr(self.runStop, "running", False):
if hasattr(self.runStop, "run") and self.runStop.run is not None:
self.runStop.run.stopLoop()
# Wait for the QThread to finish to avoid Qt destroying a running thread
self.runStop.run.wait()
except Exception:
pass
# Stop camera preview/processing threads
try:
self.picam2.stop()
except Exception:
pass
# Stop sensors monitoring thread if active
try:
if hasattr(self, "sensors") and self.sensors is not None:
self.sensors.stopMonitoring()
except Exception:
pass
# Final hardware cleanup: turn lights off and disable motors
try:
# Lights off via widget signal to keep UI state consistent
if hasattr(self, "light_selector"):
self.light_selector.handleSignal("off")
except Exception:
pass
try:
# Disable motors if they exist
for name in ["feeder", "filmdrive", "pickup"]:
if name in getattr(self, "motors", {}):
self.motors[name].motor.disable()
except Exception:
pass
app = QApplication(sys.argv)
window = MainWindow()
blayout = window.getBottomLayout()
window.showMaximized()
sys.exit(app.exec_())