Source code for gui.widget_thorlabs_mc2000

"""
This widget contains the functionality of the thorlabs chopper mc2000
hardware class provided in thorlabs_mc2000.py. The widget can be
executed as a standalone version if the correct chopper is connected and
the correct port/settings are chosen.

########################################
Functionalities provided in this widget:
########################################

1.  Choose the chopper which should be controlled via a comboBox
2.  Display the blade type
3.  Set and read out the phase of the chopper
4.  Set and read out the harmonic divider
5.  Start the chopper with the given parameters
6.  Update the chopper values described above in case someone used the
    actual chopper controller to change some settings.
"""
if __name__ == "__main__":
    # Add directories to path for imports
    import os, sys, inspect

    currentdir = os.path.dirname(
        os.path.abspath(inspect.getfile(inspect.currentframe()))
    )
    parentdir = os.path.dirname(currentdir)

    sys.path.insert(0, os.path.join(currentdir, "ui_files"))
    sys.path.insert(0, parentdir)
    sys.path.insert(0, os.path.join(parentdir, "hardware_interfaces"))

# PyQt imports
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtCore import QRunnable, QThreadPool

# Import RegEx input validators
from regex_validators import *

# Import Qt Threading wrapper
from qt_multithreading_wrapper import Worker

# Import of file made from UI designer
from ui_thorlabs_mc2000 import Ui_GroupBox_chopper as Ui_chopper

from thorlabs_mc2000 import ChopperController as Chopper

# Logger Settings
import logging

# logging.basicConfig(level=logging.INFO)


[docs]class WidgetChopper(QtWidgets.QGroupBox, Ui_chopper): def __init__(self, *args, obj=None, chopper: dict = {}, **kwargs): super(QtWidgets.QGroupBox, self).__init__(*args, **kwargs) self.setupUi(self) self.chopper = chopper self.threadpool = QThreadPool() # In case chopper is handed over with init we need to update # combobox This only relevant when running the widget directly if self.chopper: self.update_combobox() self.update_values() # Setting input validators ----------------------------- self.lineEdit_chopper_phase.setValidator( positive_int_with_0_input_validator ) # Assign input validator to this lineEdit self.lineEdit_chopper_harm.setValidator( positive_int_input_validator ) # Assign input validator to this lineEdit # ------------------------------------------------------ # Setup signals --------------------------------------- # LineEdits self.lineEdit_chopper_phase.returnPressed.connect(self.update_phase) self.lineEdit_chopper_harm.returnPressed.connect(self.update_harmonic_divider) # pushButtons self.pushButton_chopper.clicked.connect(self.update_enable) self.pushButton_chopper_update.clicked.connect(self.read_values) # Changing stage in ComboBox self.comboBox_chopper.currentIndexChanged.connect(self.read_values) # ------------------------------------------------------
[docs] def current_chopper(self): """ Create method which holds information about which combobox item is currently selected. Returns: str: Returns key to currently active chopper. """ # Create an attribute which holds information about which # combobox item is currently selected return self.comboBox_chopper.currentText()
[docs] def update_combobox(self): # Add different chopper to combo box drop down menu self.comboBox_chopper.addItems(self.chopper.keys())
[docs] def update_values(self): # Check if dictionary is empty if self.chopper: key = self.current_chopper() self.lineEdit_chopper_blade.setText(self.chopper[key].blade_type) self.lineEdit_chopper_phase.setText(self.chopper[key].phase) self.lineEdit_chopper_harm.setText(self.chopper[key].harmonic_divider) # If chopper is running, set button text to stop if self.chopper[key].enabled == "1": self.pushButton_chopper.setText("Stop Chopper") # If chopper is not running, set button text to start elif self.chopper[key].enabled == "0": self.pushButton_chopper.setText("Start Chopper") else: self.setDisabled(True)
[docs] def read_values(self): def run(): # Get currently "opened" chopper key = self.current_chopper() self.chopper[key].get_blade_type() self.chopper[key].get_phase() self.chopper[key].get_harmonic_divider() self.chopper[key].get_enable() worker = Worker(run) worker.signals.started.connect(lambda: self.setDisabled(True)) worker.signals.finished.connect(lambda: self.setEnabled(True)) worker.signals.finished.connect(self.update_values) self.threadpool.start(worker)
[docs] def work(self, qt_obj: QtWidgets, chopper_attr: str, chopper_method: str): def run(qt_obj: QtWidgets, chopper_attr: str, chopper_method: str): # Get currently "opened" stage key = self.current_chopper() # Get current value current_value = getattr(self.chopper[key], chopper_attr) # Get requested value requested_value = getattr(qt_obj, "text")() if current_value != requested_value: getattr(self.chopper[key], chopper_method)(requested_value) worker = Worker(run, qt_obj, chopper_attr, chopper_method) worker.signals.started.connect(lambda: self.setDisabled(True)) worker.signals.finished.connect(self.read_values) worker.signals.finished.connect(lambda: self.setEnabled(True)) self.threadpool.start(worker)
[docs] def update_phase(self): self.work(self.lineEdit_chopper_phase, "phase", "set_phase")
[docs] def update_harmonic_divider(self): self.work( self.lineEdit_chopper_harm, "harmonic_divider", "set_harmonic_divider" )
[docs] def update_enable(self): key = self.current_chopper() def run(state): self.chopper[key].set_enable(state) if self.chopper[key].enabled == "1": worker = Worker(run, 0) else: worker = Worker(run, 1) worker.signals.started.connect(lambda: self.setDisabled(True)) worker.signals.finished.connect(self.read_values) worker.signals.finished.connect(lambda: self.setEnabled(True)) self.threadpool.start(worker)
if __name__ == "__main__": # TESTING ----------------------------------------------------------- chopper = {} class MainWindow(QtWidgets.QMainWindow): def __init__(self, *args, obj=None, chopper=chopper, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) self.setWindowTitle("Please god let this work") widget = WidgetChopper(chopper=chopper) self.setCentralWidget(widget) ir_chopper_port = "COM6" ir_name = "IR Chopper" app = QtWidgets.QApplication(sys.argv) with Chopper(ir_chopper_port, name=ir_name) as ir_chopper: chopper = {} chopper["IR Chopper"] = ir_chopper # chopper["UV Chopper"] = uv_chopper window = MainWindow(chopper=chopper) window.show() app.exec()