Source code for gui.widget_analog_digital_converter

"""
This widget contains the functionality of the analog_digital_converter
hardware class provided in analog_digital_converter.py. The widget can
be executed as a standalone version if the correct National Instruments
adc is connected and the correct port/settings are chosen.

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

1.  Connect to an ADC
2.  Set samples to acquire for an single acquisition
3.  Set acquisition time in seconds fo a single
    acquisition.
"""
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"))

from PyQt5 import QtWidgets
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_analog_digital_converter import Ui_GroupBox_adc as Ui_adc

# Import adc
from analog_digital_converter import AnalogDigitalConverter as ADC

# Logger settings
import logging

# logging.basicConfig(level=logging.INFO)


[docs]class WidgetAnalogDigitalConverter(QtWidgets.QGroupBox, Ui_adc): def __init__(self, *args, obj=None, adc=None, **kwargs): super(QtWidgets.QGroupBox, self).__init__(*args, **kwargs) self.setupUi(self) self.adc = adc self.threadpool = QThreadPool() # Load values self.update_values() # Setting input validators----------------------------- self.lineEdit_adc_samples_to_acquire.setValidator( positive_int_input_validator ) # Assign input validator to this lineEdit self.lineEdit_adc_acquisition_time.setValidator(positive_float_input_validator) # ------------------------------------------------------ # Setup signals self.lineEdit_adc_acquisition_time.editingFinished.connect( self.update_acquisition_time ) self.lineEdit_adc_samples_to_acquire.editingFinished.connect( self.update_samples_to_acquire )
[docs] def update_values(self): # The if statement is necessary because some GUI features/ # elements need a hardware object (here the adc) to work # properly. Within the __init__ hardware objects such as the adc # are defaulted with None. This is because python directly # identifies that None type objects have no further/additional # attributes. And in this case we are interested in the # attributes of the adc class which itself is passed as an # attribute to the adc GUI (as shown in __init__) If you still # do not get it, remove the if statement and try it out for # yourself. if not self.adc == None: self.lineEdit_adc_samples_to_acquire.setText( str(round(self.adc.samples_to_acquire, 3)) ) self.lineEdit_adc_acquisition_time.setText( str(round(self.adc.acquisition_time, 3)) ) else: self.setDisabled(True)
[docs] def update_acquisition_time(self): # Because regular expression of float without 0 is too # complicated we handle this case here with if statement if float(self.lineEdit_adc_acquisition_time.text()) == 0: self.lineEdit_adc_acquisition_time.setText("1") self.work( self.lineEdit_adc_acquisition_time, "acquisition_time", "set_acquisition_time", )
[docs] def update_samples_to_acquire(self): self.work( self.lineEdit_adc_samples_to_acquire, "samples_to_acquire", "set_samples_to_acquire", )
[docs] def work(self, qt_obj: QtWidgets, adc_attr: str, adc_method: str): def run(qt_obj: QtWidgets, adc_attr: str, adc_method: str): # Get current value current_value = getattr(self.adc, adc_attr) # Get requested value requested_value = getattr(qt_obj, "text")() # Convert requested value to correct type requested_value = type(current_value)(requested_value) if round(current_value, 3) != requested_value: # Call method / Execute getattr(self.adc, adc_method)(requested_value) worker = Worker(run, qt_obj, adc_attr, adc_method) worker.signals.finished.connect(self.update_values) self.threadpool.start(worker)
if __name__ == "__main__": logging.basicConfig(level=logging.WARNING) # TESTING ----------------------------------------------------------- class MainWindow(QtWidgets.QMainWindow): def __init__(self, adc, *args, obj=None, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) self.setWindowTitle("Please god let this work") adc_widget = WidgetAnalogDigitalConverter(adc=adc) self.setCentralWidget(adc_widget) app = QtWidgets.QApplication(sys.argv) with ADC( "Dev6", "./hardware_config_files/H-Lab analog input configuration.json", 30 * 3000, "PFI4", 3000, 3000, name="NI_DIO", ) as adc: window = MainWindow(adc) window.show() app.exec_()