def _acquire(signal: ophyd.Signal): """ [Context manager] Subscribe to signal, acquire data until the block exits. Parameters ---------- signal : ophyd.Signal Ophyd object to monitor. Returns ------- data : List[PrimitiveType] The data acquired. Guaranteed to have at least one item. """ signal.wait_for_connection() data = [] start_value = signal.get() def acquire(value, **_): data.append(value) with subscription_context(signal, callback=acquire): yield data if not data: data.extend([start_value, signal.get()])
def test_signal_connection(qapp): # Create a signal and attach our listener sig = Signal(name='my_signal', value=1) register_signal(sig) widget = WritableWidget() listener = widget.channels()[0] sig_conn = SignalConnection(listener, 'my_signal') sig_conn.add_listener(listener) # Check that our widget receives the initial value qapp.processEvents() assert widget._write_access assert widget._connected assert widget.value == 1 # Check that we can push values back to the signal which in turn causes the # internal value at the widget to update widget.send_value_signal[int].emit(2) qapp.processEvents() qapp.processEvents() # Must be called twice. Multiple rounds of signals assert sig.get() == 2 assert widget.value == 2 # Try changing types qapp.processEvents() qapp.processEvents() # Must be called twice. Multiple rounds of signals sig_conn.remove_listener(listener) # Check that our signal is disconnected completely and maintains the same # value as the signal updates in the background sig.put(3) qapp.processEvents() assert widget.value == 2 widget.send_value_signal.emit(1) qapp.processEvents() assert sig.get() == 3
def get_data_for_signal(self, signal: ophyd.Signal) -> Any: """ Get data for the given signal, according to the string and data reduction settings. """ if self.reduce_period and self.reduce_period > 0: return self.reduce_method.subscribe_and_reduce( signal, self.reduce_period ) if self.string: return signal.get(as_string=True) return signal.get()
def test_array_signal_put_value(qapp, qtbot): sig = Signal(name='my_array_write', value=np.ones(4)) register_signal(sig) widget = PyDMLineEdit() qtbot.addWidget(widget) widget.channel = 'sig://my_array_write' widget.send_value_signal[np.ndarray].emit(np.zeros(4)) qapp.processEvents() assert all(sig.get() == np.zeros(4))
def test_signal_connection(qapp, qtbot): # Create a signal and attach our listener sig = Signal(name='my_signal', value=1) register_signal(sig) widget = PyDMLineEdit() qtbot.addWidget(widget) widget.channel = 'sig://my_signal' listener = widget.channels()[0] # If PyDMChannel can not connect, we need to connect it ourselves # In PyDM > 1.5.0 this will not be neccesary as the widget will be # connected after we set the channel name if not hasattr(listener, 'connect'): pydm.utilities.establish_widget_connections(widget) # Check that our widget receives the initial value qapp.processEvents() assert widget._write_access assert widget._connected assert widget.value == 1 # Check that we can push values back to the signal which in turn causes the # internal value at the widget to update widget.send_value_signal[int].emit(2) qapp.processEvents() qapp.processEvents() # Must be called twice. Multiple rounds of signals assert sig.get() == 2 assert widget.value == 2 # Try changing types qapp.processEvents() qapp.processEvents() # Must be called twice. Multiple rounds of signals # In PyDM > 1.5.0 we will not need the application to disconnect the # widget, but until then we have to check for the attribute if hasattr(listener, 'disconnect'): listener.disconnect() else: qapp.close_widget_connections(widget) # Check that our signal is disconnected completely and maintains the same # value as the signal updates in the background sig.put(3) qapp.processEvents() assert widget.value == 2 widget.send_value_signal.emit(1) qapp.processEvents() assert sig.get() == 3
def test_array_into_softsignal(): data = np.array([1, 2, 3]) s = Signal(name='np.array') set_and_wait(s, data) assert np.all(s.get() == data)