diff --git a/src/pymodaq_plugins_mockexamples/daq_viewer_plugins/plugins_2D/daq_2Dviewer_BSCamera.py b/src/pymodaq_plugins_mockexamples/daq_viewer_plugins/plugins_2D/daq_2Dviewer_BSCamera.py index 0637605..a8e87ec 100644 --- a/src/pymodaq_plugins_mockexamples/daq_viewer_plugins/plugins_2D/daq_2Dviewer_BSCamera.py +++ b/src/pymodaq_plugins_mockexamples/daq_viewer_plugins/plugins_2D/daq_2Dviewer_BSCamera.py @@ -1,4 +1,4 @@ -from qtpy.QtCore import QThread, Slot, QRectF +from qtpy.QtCore import QThread, QTimer, Slot, QRectF from qtpy import QtWidgets import numpy as np from pymodaq.control_modules.viewer_utility_classes import DAQ_Viewer_base, main, comon_parameters @@ -66,6 +66,7 @@ class DAQ_2DViewer_BSCamera(DAQ_Viewer_base): def ini_attributes(self): self.controller: BeamSteering = None self.live = False + self._live_timer: QTimer = None def commit_settings(self, param: Parameter): """ @@ -145,21 +146,20 @@ def grab_data(self, Naverage=1, **kwargs): kwargs: dict others optionals arguments """ - if 'live' in kwargs: - if kwargs['live']: - self.live = True - # self.live = False # don't want to use that for the moment - - if self.live: - while self.live: - data = self.average_data(Naverage) # hardware averaging - QThread.msleep(kwargs.get('wait_time', 100)) - self.dte_signal.emit(data) - QtWidgets.QApplication.processEvents() + if kwargs.get('live', False): + # A master and its slaves share the same thread: a blocking loop here (even calling processEvents) would + # starve the other detectors, so the live acquisition is driven by a timer of the thread event loop + self.live = True + if self._live_timer is None: + self._live_timer = QTimer(self) + self._live_timer.timeout.connect(self._emit_live_data) + self._live_timer.start(max(int(kwargs.get('wait_time', 100)), 1)) + self._live_Naverage = Naverage else: - data = self.average_data(Naverage) # hardware averaging - QThread.msleep(000) - self.dte_signal.emit(data) + self.dte_signal.emit(self.average_data(Naverage)) # hardware averaging + + def _emit_live_data(self): + self.dte_signal.emit(self.average_data(self._live_Naverage)) def average_data(self, Naverage, init=False): data_tmp = np.zeros_like(self.controller.get_camera_data()) @@ -182,6 +182,8 @@ def stop(self): not implemented. """ self.live = False + if self._live_timer is not None: + self._live_timer.stop() return "" diff --git a/tests/test_bs_live_shared_thread.py b/tests/test_bs_live_shared_thread.py new file mode 100644 index 0000000..cda1daa --- /dev/null +++ b/tests/test_bs_live_shared_thread.py @@ -0,0 +1,52 @@ +"""A master camera and its photodiode slave share one thread: both must stream in live mode""" +import pytest +import qt_themes +from pytest import fixture +from qtpy import QtWidgets + +pytest.importorskip('pymodaq_plugins_mock', reason='The mock instruments of PyMoDAQ are needed') + +from pymodaq_utils.config import GlobalConfig +from pymodaq.control_modules.enums import DAQTypesEnum +from pymodaq.dashboard import create_load_dashboard + + +@fixture +def detectors(qtbot): + config = GlobalConfig() + qt_themes.set_theme(theme=config('gui', 'style', 'theme')[0], style=config('gui', 'style', 'style')[0]) + shared_ui, dashboard = create_load_dashboard(show_dashboard=False) + qtbot.addWidget(shared_ui.parent) + qtbot.addWidget(dashboard.tree) + creator = dashboard.module_creator + cam = creator.create_detector('Cam', DAQTypesEnum.DAQ2D) + creator.set_detector_type(cam, DAQTypesEnum.DAQ2D, 'BSCamera') + creator.add_detector(cam) + cam.init_hardware_ui(True) + qtbot.waitUntil(lambda: cam.initialized_state, timeout=10000) + photo = creator.create_detector('Photo', DAQTypesEnum.DAQ0D) + creator.set_detector_type(photo, DAQTypesEnum.DAQ0D, 'BSPhotodiode') + creator.add_detector(photo) + photo.controller_and_thread.is_master = False + photo.controller_and_thread.controller = cam.controller_and_thread.controller + photo.controller_and_thread.thread = cam.controller_and_thread.thread + photo.init_hardware_ui(True) + qtbot.waitUntil(lambda: photo.initialized_state, timeout=10000) + yield cam, photo + cam.stop_grab() + photo.stop_grab() + QtWidgets.QApplication.processEvents() + photo.quit_fun() # the slave first, then the master that owns the shared thread + cam.quit_fun() + QtWidgets.QApplication.processEvents() + dashboard.quit_fun() + + +def test_live_grab_of_master_and_slave(detectors, qtbot): + cam, photo = detectors + counts = {'cam': 0, 'photo': 0} + cam.grab_done_signal.connect(lambda d: counts.__setitem__('cam', counts['cam'] + 1)) + photo.grab_done_signal.connect(lambda d: counts.__setitem__('photo', counts['photo'] + 1)) + cam.grab() + photo.grab() + qtbot.waitUntil(lambda: counts['cam'] >= 3 and counts['photo'] >= 3, timeout=10000)