Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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())
Expand All @@ -182,6 +182,8 @@ def stop(self):
not implemented.
"""
self.live = False
if self._live_timer is not None:
self._live_timer.stop()
return ""


Expand Down
52 changes: 52 additions & 0 deletions tests/test_bs_live_shared_thread.py
Original file line number Diff line number Diff line change
@@ -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)