import dataclasses from contextlib import contextmanager from abc import ABC, abstractmethod from typing import Callable, Optional, Tuple, Sequence, List, Dict import abc import enum import threading import time import pydantic import numpy as np from dataclasses import dataclass import squid.logging from squid.config import AxisConfig, StageConfig, CameraConfig, CameraPixelFormat from squid.exceptions import SquidTimeout import control.utils @dataclass class FilterWheelInfo: """Information about a single filter wheel.""" index: int number_of_slots: int slot_names: List[str] class FilterControllerError(Exception): """Custom exception for FilterController errors.""" pass class AbstractFilterWheelController(ABC): """Abstract base class defining the interface for different filter wheels controllers. A filter wheel controller may control one ormultiple filter wheels.""" @abstractmethod def __init__(self): """Initialize the filter wheel and establish communication.""" pass @abstractmethod def initialize(self, filter_wheel_indices: List[int]): """ Initialize the settings of the filter wheel. Args: filter_wheel_indices: List of filter wheel indices to initialize. """ pass @property @abstractmethod def available_filter_wheels(self) -> List[int]: """List of available filter wheel indices (e.g., [1, 2, 3, 4] for Optospin). Returns: List of integer indices for available wheels. """ pass @abstractmethod def get_filter_wheel_info(self, index: int) -> FilterWheelInfo: """Get information about a specific filter wheel. Args: index: Filter wheel index. Returns: FilterWheelInfo containing slots and names. """ pass @abstractmethod def home(self, index: int = None): """Home the filter wheel with the given index. If index is None, home all filter wheels.""" pass def position_is_known(self, wheel_id: Optional[int] = None) -> bool: """False when the controller cannot vouch for where a wheel is (with no argument: any of its wheels). Controllers that read their position back from the hardware keep this default; one that only tracks its own commands (the Squid wheel, which has no position readback) overrides it, and a wheel it answers False for is homed before use.""" return True def next_position(self, wheel_id: int = 1): """One slot forward. The default stops at the last slot; a rotary wheel that may cross its index flag overrides this and continues at the first. The GUI's Next button calls this and then reads get_filter_wheel_position() back, so the arithmetic lives in one place.""" self._step_to_neighbour(wheel_id, +1) def previous_position(self, wheel_id: int = 1): """One slot back; see next_position().""" self._step_to_neighbour(wheel_id, -1) def _step_to_neighbour(self, wheel_id: int, direction: int): current = self.get_filter_wheel_position().get(wheel_id) if current is None: raise ValueError(f"Filter wheel index {wheel_id} not found") target = current + direction if 1 <= target <= self.get_filter_wheel_info(wheel_id).number_of_slots: self.set_filter_wheel_position({wheel_id: target}) @abstractmethod def set_filter_wheel_position(self, positions: Dict[int, int]): """ Set the filter wheels to the given positions. Args: positions: A dictionary of filter wheel index to target position. For example, {1: 1, 2: 2} means set the first filter wheel to position 1 and the second filter wheel to position 2. """ pass @abstractmethod def get_filter_wheel_position(self) -> Dict[int, int]: """Get the current filter wheel positions. Returns: A dictionary of filter wheel index to current position. """ pass @abstractmethod def set_delay_offset_ms(self, delay_offset_ms: float): """Set the delay offset for the filter wheel.""" pass @abstractmethod def get_delay_offset_ms(self) -> Optional[float]: """Get the delay offset for the filter wheel.""" pass @abstractmethod def set_delay_ms(self, delay_ms: float): """Set the delay for the filter wheel.""" pass @abstractmethod def get_delay_ms(self) -> Optional[float]: """Get the delay for the filter wheel.""" pass @abstractmethod def close(self): """Close the filter wheel connection.""" pass class LightSourceHold: """A declared use of light source channels (LightSource.use): update() when the channels change, release() when done, or use it as a context manager. This one does nothing: a light source with nothing to do on use returns it.""" def update(self, channels) -> None: pass def release(self) -> None: pass def wait_until_ready( self, timeout_s: Optional[float] = None, cancel_fn: Callable[[], bool] = lambda: False ) -> bool: """Block until the held lines can image (e.g. a laser line warmed up, a mechanical shutter open). False on timeout (the light source's own default when None) or when cancel_fn() turns True.""" return True def __enter__(self): return self def __exit__(self, *exc) -> None: self.release() @dataclass(frozen=True) class MechanicalShutter: """A mechanical shutter on a light source line (not the exposure gate that ShutterControlMode selects).""" open_time_s: float # from the open command until the shutter is fully open class LightSource(ABC): """Abstract base class defining the interface for different light sources.""" def use(self, name: str, channels) -> LightSourceHold: """`name` (e.g. "live", "acquisition") images with these channels (channel_mappings values) until the hold is released. A light source that switches lines on, keeps them on or opens a mechanical shutter for use acts on it; by default nothing happens.""" return LightSourceHold() def mechanical_shutter(self, channel) -> Optional[MechanicalShutter]: """The mechanical shutter on this channel's line, if it has one.""" return None def select(self, channel) -> None: """This channel (a channel_mappings value, None when the light source has no line for it) is the one about to be exposed. Called on every channel change, whether or not the power changes. A light source that must arm one line at a time acts on it (the Andor HLE with active blanking); by default nothing happens.""" return None @abstractmethod def __init__(self): """Initialize the light source and establish communication.""" pass @abstractmethod def initialize(self): """ Initialize the connection and settings for the light source. Returns True if successful, False otherwise. """ pass @abstractmethod def set_intensity_control_mode(self, mode): """ Set intensity control mode. Args: mode: IntensityControlMode(Enum) """ pass @abstractmethod def get_intensity_control_mode(self): """ Get current intensity control mode. Returns: IntensityControlMode(Enum) """ pass @abstractmethod def set_shutter_control_mode(self, mode): """ Set shutter control mode. Args: mode: ShutterControlMode(Enum) """ pass @abstractmethod def get_shutter_control_mode(self): """ Get current shutter control mode. Returns: ShutterControlMode(Enum) """ pass @abstractmethod def set_shutter_state(self, channel, state): """ Turn a specific channel on or off. Args: channel: Channel ID state: True to turn on, False to turn off """ pass @abstractmethod def get_shutter_state(self, channel): """ Get the current shutter state of a specific channel. Args: channel: Channel ID Returns: bool: True if channel is on, False if off """ pass @abstractmethod def set_intensity(self, channel, intensity): """ Set the intensity for a specific channel. Args: channel: Channel ID intensity: Intensity value (0-100) """ pass @abstractmethod def get_intensity(self, channel) -> float: """ Get the current intensity of a specific channel. Args: channel: Channel ID Returns: float: Current intensity value """ pass @abstractmethod def shut_down(self): """Safely shut down the light source.""" pass class Pos(pydantic.BaseModel): x_mm: float y_mm: float z_mm: float # NOTE/TODO(imo): If essentially none of our stages have a theta, this is probably fine. But If it's a mix we probably want a better way of handling the "maybe has theta" case. theta_rad: Optional[float] class StageStage(pydantic.BaseModel): busy: bool class AbstractStage(metaclass=abc.ABCMeta): def __init__(self, stage_config: StageConfig): self._config = stage_config self._log = squid.logging.get_logger(self.__class__.__name__) @abc.abstractmethod def move_x(self, rel_mm: float, blocking: bool = True): pass @abc.abstractmethod def move_y(self, rel_mm: float, blocking: bool = True): pass @abc.abstractmethod def move_z(self, rel_mm: float, blocking: bool = True): pass @abc.abstractmethod def move_x_to(self, abs_mm: float, blocking: bool = True): pass @abc.abstractmethod def move_y_to(self, abs_mm: float, blocking: bool = True): pass @abc.abstractmethod def move_z_to(self, abs_mm: float, blocking: bool = True): pass # TODO(imo): We need a stop or halt or something along these lines # @abc.abstractmethod # def stop(self, blocking: bool=True): # pass @abc.abstractmethod def get_pos(self) -> Pos: pass @abc.abstractmethod def get_state(self) -> StageStage: pass @abc.abstractmethod def reports_stopped(self) -> bool: """True only on fresh evidence from the hardware that the stage is not moving; False if unsure or on any exception. A host-side busy flag (get_state().busy can be one) is not such evidence.""" pass @abc.abstractmethod def home(self, x: bool, y: bool, z: bool, theta: bool, blocking: bool = True, objective_lowered: bool = False): pass @abc.abstractmethod def zero(self, x: bool, y: bool, z: bool, theta: bool, blocking: bool = True): pass @abc.abstractmethod def set_limits( self, x_pos_mm: Optional[float] = None, x_neg_mm: Optional[float] = None, y_pos_mm: Optional[float] = None, y_neg_mm: Optional[float] = None, z_pos_mm: Optional[float] = None, z_neg_mm: Optional[float] = None, theta_pos_rad: Optional[float] = None, theta_neg_rad: Optional[float] = None, ): pass def get_config(self) -> StageConfig: return self._config def wait_for_idle(self, timeout_s): start_time = time.time() while time.time() < start_time + timeout_s: if not self.get_state().busy: return # Sleep some small amount of time so we can yield to other threads if needed # while waiting. time.sleep(0.001) error_message = f"Timed out waiting after {timeout_s:0.3f} [s]" self._log.error(error_message) raise SquidTimeout(error_message) def close(self): """Release any transport the stage owns (e.g. a serial handle). Default no-op: CephlaStage/PriorStage are released via their underlying transports. Stages that own a connection (e.g. the PI C-414) override this. """ pass class CameraAcquisitionMode(enum.Enum): SOFTWARE_TRIGGER = "Software Trigger" HARDWARE_TRIGGER = "Hardware Trigger" CONTINUOUS = "Continuous Acquisition" class CameraFrameFormat(enum.Enum): """ This is all known camera frame formats in the Cephla world, but not all cameras will support all of these. """ RAW = "RAW" RGB = "RGB" class CameraGainRange(pydantic.BaseModel): min_gain: float max_gain: float gain_step: float # NOTE(imo): Dataclass because pydantic does not like the np.array since there's no reasonable default # we can provide it. @dataclass class CameraFrame: frame_id: int timestamp: float frame: np.array frame_format: CameraFrameFormat frame_pixel_format: CameraPixelFormat # The run of the camera's stream this frame was READ in (AbstractCamera.stream_generation()); 0 from a driver that # does not stamp it. A frame a driver had already read when the stream was restarted is still delivered afterwards, # and this is how a consumer tells it from the frames of the restarted stream. stream_generation: int = 0 def is_color(self): return CameraPixelFormat.is_color_format(self.frame_pixel_format) class CameraError(RuntimeError): pass class AbstractCamera(metaclass=abc.ABCMeta): @staticmethod def calculate_new_roi_for_binning(old_binning, old_roi, new_binning) -> Tuple[int, int, int, int]: """ When changing binning, we may want the roi to change such that the FOV is the same as before the binning factors change. This calculates the new roi to keep the FOV the same given we change to this new resolution. This only needs to be done for some of the cameras. In other cameras the roi might be updated automatically. The binning factors must be 2-tuples of (binning_factor_x, binning_factor_y). The roi must be a 4-tuple of (offset_x, offset_y, width, height) Returns a 4-tuple of (offset_x, offset_y, width, height) """ if not isinstance(old_binning, tuple) or not isinstance(new_binning, tuple) or not isinstance(old_roi, tuple): raise ValueError("Need tuple args.") def rounded_int(num) -> int: return int(round(num)) return ( old_roi[0] * old_binning[0] / new_binning[0], old_roi[1] * old_binning[1] / new_binning[1], old_roi[2] * old_binning[0] / new_binning[0], old_roi[3] * old_binning[1] / new_binning[1], ) # Counts the runs of the camera's stream: a start_streaming() that starts the camera's capture bumps it, and the # driver stamps every frame with the value current when the frame was READ (CameraFrame.stream_generation). A # class attribute, so a driver that never bumps it reports 0 - the value its unstamped frames carry. _stream_generation: int = 0 def stream_generation(self) -> int: """The current run of the camera's stream; a frame of an earlier run carries a smaller number.""" return self._stream_generation def __init__( self, camera_config: CameraConfig, hw_trigger_fn: Optional[Callable[[Optional[float]], bool]], hw_set_strobe_delay_ms_fn: Optional[Callable[[float], bool]], ): """ Init should open the camera, configure it as needed based on camera_config and reasonable defaults, and make it immediately available for use in grabbing frames. The hw_trigger_fn arguments are: Optional[float] = illumination time in ms (if None, do not control illumination) The hw_set_strobe_delay_ms_fn arguments are: float = hardware strobe delay in ms. If you plan on using the HARDWARE acquisition mode, you *must* provide the hw_trigger_fn and hw_set_strobe_delay_ms_fn. Not doing so will result in failure later on when trying to switch acquisition modes. """ self._config = camera_config self._log = squid.logging.get_logger(self.__class__.__name__) self._hw_trigger_fn: Optional[Callable[[Optional[float]], bool]] = hw_trigger_fn self._hw_set_strobe_delay_ms_fn: Optional[Callable[[float], bool]] = hw_set_strobe_delay_ms_fn # Frame callbacks is a list of (id, callback) managed by add_frame_callback and remove_frame_callback. # Your frame receiving functions should call self._send_frame_to_callbacks(frame), and doesn't need # to do more than that. self._frame_callbacks: List[Tuple[int, Callable[[CameraFrame], None]]] = [] self._frame_callbacks_enabled = True # Software crop is applied after hardware crop (setting ROI). The ratio is based on size after hardware crop. # Default is 1.0, which means no software crop. self._software_crop_width_ratio = 1.0 self._software_crop_height_ratio = 1.0 # Held for the duration of any operation that pauses streaming (see # _pause_streaming); send_trigger try-acquires it and drops the trigger if a # pause is in flight. Reentrant so a driver can hold it across a compound # operation that itself pauses streaming. self._trigger_lock = threading.RLock() @contextmanager def _pause_streaming(self): with self._trigger_lock: was_streaming = self.get_is_streaming() try: if was_streaming: self.stop_streaming() yield finally: if was_streaming: self.start_streaming() def enable_callbacks(self, enabled: bool): """ This enables or disables propagation of frames to all the registered callbacks. This should be used sparingly since any read_frame() with enable_callbacks = False will be lost to all callbacks. Valid use cases are things like during-acquisition auto focus (whereby we need to capture a bunch of frames that aren't a part of the acquisition). This is inherently fragile, though, so all effort should be made to design a system that has enabled_callbacks(True) as the default! """ self._log.debug(f"enable_callbacks: {enabled=}") self._frame_callbacks_enabled = enabled def get_callbacks_enabled(self) -> bool: return self._frame_callbacks_enabled def add_frame_callback(self, frame_callback: Callable[[CameraFrame], None]) -> int: """ Adds a new callback that will be called with the receipt of every new frame. This callback should not block for a long time because it will be called in the frame receiving hot path! This np.ndarray is shared with all callbacks, so you should make a copy if you need to modify it. Returns the callback ID that can be used to remove the callback later if needed. """ try: next_id = max(t[0] for t in self._frame_callbacks) + 1 except ValueError: next_id = 1 self._frame_callbacks.append((next_id, frame_callback)) return next_id def remove_frame_callback(self, callback_id): try: idx_to_remove = [t[0] for t in self._frame_callbacks].index(callback_id) self._log.debug(f"Removing callback with id={callback_id} at idx={idx_to_remove}.") del self._frame_callbacks[idx_to_remove] except ValueError: self._log.warning(f"No callback with id={callback_id}, cannot remove it.") def _propogate_frame(self, camera_frame: CameraFrame): """ Implementations can call this to propogate a new frame to all registered callbacks. You should have already called _process_raw_frame to generate this (aka: all cropping and rotating should be done). Best practice is to send the same frame here as you assign to your self._current_frame (if you have one). """ if not self._frame_callbacks_enabled: return for _, cb in self._frame_callbacks: cb(camera_frame) @abc.abstractmethod def set_exposure_time(self, exposure_time_ms: float): """ Sets the exposure time in ms. This should also take care of setting the strobe delay (if needed). If in HARDWARE acquisition mode, you're guaranteed to have a self._hw_set_strobe_delay_ms_fn to help with this. """ pass @abc.abstractmethod def get_exposure_time(self) -> float: """ Returns the current exposure time in milliseconds. """ pass @abc.abstractmethod def get_exposure_limits(self) -> Tuple[float, float]: """ Return the valid range of exposure times in inclusive milliseconds. """ pass @abc.abstractmethod def get_strobe_time(self) -> float: """ Given the current exposure time we are using, what is the strobe time such that get_strobe_time() + get_exposure_time() == total frame time. In milliseconds. """ pass def get_total_frame_time(self) -> float: """ The total sensor time for a single frame. This is strobe time + exposure time in ms. """ return self.get_exposure_time() + self.get_strobe_time() @abc.abstractmethod def set_frame_format(self, frame_format: CameraFrameFormat): """ If this camera supports the given frame format, set it and make sure that all subsequent frames are in this format. If not, throw a ValueError. """ pass @abc.abstractmethod def get_frame_format(self) -> CameraFrameFormat: pass @abc.abstractmethod def set_pixel_format(self, pixel_format: squid.config.CameraPixelFormat): """ If this camera supports the given pixel format, enable it and make sure that all subsequent captures use this pixel format. If not, throw a ValueError. """ pass @abc.abstractmethod def get_pixel_format(self) -> squid.config.CameraPixelFormat: pass @property def is_color(self) -> bool: """True iff the camera's current pixel format produces colour images. Mirrors the shape used by legacy concrete cameras (FLIR, IDS, TIS) which expose ``is_color`` as a plain attribute. Surfacing it here keeps callers like core_volumetric_imaging / core_PDAF working on any AbstractCamera-derived class (incl. SimulatedCamera) without falling through __getattr__ placeholders. """ return CameraPixelFormat.is_color_format(self.get_pixel_format()) @abc.abstractmethod def get_available_pixel_formats(self) -> Sequence[squid.config.CameraPixelFormat]: """ Returns the pixel formats available at the camera's current settings. Some cameras deliver a different depth per binning or frame format, so re-query after set_binning / set_frame_format. """ pass @abc.abstractmethod def set_binning(self, binning_factor_x: int, binning_factor_y: int): """ Set the binning factor of the camera. Usually we set hardware binning here, so calling this may change buffer size, readout speed, etc. Update these settings as needed. """ pass @abc.abstractmethod def get_binning(self) -> Tuple[int, int]: """ Return the (binning_factor_x, binning_factor_y) of the camera right now. """ pass @abc.abstractmethod def get_binning_options(self) -> Sequence[Tuple[int, int]]: """ Return the list of binning options supported by the camera. """ pass @abc.abstractmethod def get_resolution(self) -> Tuple[int, int]: """ Returns the maximum resolution of the camera under the current binning setting. """ pass @abc.abstractmethod def get_pixel_size_unbinned_um(self) -> float: """ Returns the pixel size without binning in microns. """ pass @abc.abstractmethod def get_pixel_size_binned_um(self) -> float: """ Returns the pixel size after binning in microns. """ pass @abc.abstractmethod def set_analog_gain(self, analog_gain: float): """ Set analog gain as an input multiple. EG 1 = no gain, 100 = 100x gain. """ pass @abc.abstractmethod def get_analog_gain(self) -> float: """ Returns gain in the same units as set_analog_gain. """ pass @abc.abstractmethod def get_gain_range(self) -> CameraGainRange: """ Returns the gain range, and minimum gain step, for this camera. """ pass @abc.abstractmethod def start_streaming(self): """ This starts camera frame streaming. Whether this results in frames immediately depends on the current triggering mode. If frames require triggering, no frames will come until triggers are sent. If the camera is in continuous mode, frames will start immediately. This should be a noop if the camera is already streaming. """ pass @abc.abstractmethod def stop_streaming(self): """ Stops camera frame streaming, which means frames will only come in with a call go get_frame. Frames still in the camera or its SDK are lost with the stream. A frame the driver had already read may still reach the frame callbacks after this returns; it carries the stream_generation() of the stopped run. """ pass @abc.abstractmethod def get_is_streaming(self): pass def _process_raw_frame(self, raw_frame: np.array) -> np.array: """ Takes a raw nd array from a camera, and processes it such that it can be used directly in a CameraFrame as the frame field. This takes care of rotating, resizing, etc the raw frame such that it respects this camera's settings. Your camera's image callback should use this. """ # Apply rotation and flip image = control.utils.rotate_and_flip_image( raw_frame, rotate_image_angle=self._config.rotate_image_angle, flip_image=self._config.flip ) # Apply software crop # Crop size should be scaled wrt the binning factor as the crop_width and crop_height are defined wrt the unbinned image size. crop_width, crop_height = self.get_crop_size() image = control.utils.crop_image(image, crop_width, crop_height) return image def get_crop_size(self) -> Tuple[int, int]: """ Returns the final crop size of the image (after software crop). """ binning_x, binning_y = self.get_binning() crop_width = int(self._config.crop_width / binning_x) if self._config.crop_width else None crop_height = int(self._config.crop_height / binning_y) if self._config.crop_height else None crop_width = int(crop_width * self._software_crop_width_ratio) if crop_width is not None else None crop_height = int(crop_height * self._software_crop_height_ratio) if crop_height is not None else None return crop_width, crop_height def get_fov_size_mm(self) -> float: """ Returns the size of the camera field of view in millimeters (after ROI and software crop). """ return self.get_crop_size()[0] * self.get_pixel_size_binned_um() / 1000 def set_software_crop_ratio(self, width_ratio: float, height_ratio: float): """ Set the software crop ratio. The final image size will be the hardware crop size multiplied by the software crop ratio rounded to the nearest integer. """ if not (0 < width_ratio <= 1): raise ValueError(f"width_ratio must be > 0 and <= 1, got {width_ratio}") if not (0 < height_ratio <= 1): raise ValueError(f"height_ratio must be > 0 and <= 1, got {height_ratio}") self._software_crop_width_ratio = width_ratio self._software_crop_height_ratio = height_ratio def read_frame(self) -> Optional[np.ndarray]: """ If needed, send a trigger to request a frame. Then block and wait until the next frame comes in, and return it. The frame that comes back will be rotated/flipped/etc based on this cameras config, so the caller can assume all that is done for them. These frames will be sent to registered callbacks as well. NOTE(imo): We might change this to get_frame to be consistent with everything else here, but since cameras previously used read_frame this decreases line change noise. Might return None if getting a frame timed out, or another error occurred. """ full_frame = self.read_camera_frame() # read_camera_frame will have already printed an error, so just pass on the none. return full_frame.frame if full_frame else None @abc.abstractmethod def read_camera_frame(self) -> Optional[CameraFrame]: """ Returns the latest frame as a full CameraFrame (read_frame delegates to this and returns just the image data). The frame in the CameraFrame will have had _process_raw_frame called on it already. In SOFTWARE/HARDWARE trigger modes, the returned frame must have been captured at or after the most recent send_trigger() - implementations must never return a pre-trigger frame (e.g. from a cached-frame fast path), since trigger->read callers rely on seeing the post-trigger state. Might return None if getting a frame timed out, or another error occurred. """ pass @abc.abstractmethod def get_frame_id(self) -> int: """ Returns the frame id of the current frame. This should increase by 1 with every frame received from the camera """ pass @abc.abstractmethod def get_white_balance_gains(self) -> Tuple[float, float, float]: """ Returns the (R, G, B) white balance gains """ pass @abc.abstractmethod def set_white_balance_gains(self, red_gain: float, green_gain: float, blue_gain: float): """ Set the (R, G, B) white balance gains. """ pass @abc.abstractmethod def set_auto_white_balance_gains(self, on: bool): """ Turn auto white balance on or off. """ pass @abc.abstractmethod def set_black_level(self, black_level: float): """ Sets the black level of captured images, in counts on the 8-bit scale (0-255) whatever the pixel format; drivers convert to their SDK's units. """ pass @abc.abstractmethod def get_black_level(self) -> float: """ Gets the black level set on the camera, in counts on the 8-bit scale. """ pass def get_available_sensor_modes(self) -> Sequence[str]: """ Driver-defined sensor mode names (e.g. readout speeds, gain modes). An empty list means sensor mode selection is not supported by this camera. """ return [] def get_sensor_mode(self) -> Optional[str]: """ The currently active sensor mode, or None if mode selection is not supported. """ return None def set_sensor_mode(self, mode: str): """ Switch the camera to the given sensor mode. The mode must be one of get_available_sensor_modes(). Implementations must refresh exposure/strobe timing afterwards if the mode change affects it. """ raise NotImplementedError("Sensor mode selection is not implemented for this camera.") def set_acquisition_mode(self, acquisition_mode: CameraAcquisitionMode): """ Sets the acquisition mode. If you are specifying hardware trigger, and an external system needs to send the trigger, you must specify a hw_trigger_fn. This function must be callable in such a way that it immediately sends a hardware trigger, and only returns when the trigger has been sent. hw_trigger_fn and hw_set_strobe_delay_ms_fn to the __init__ must have been valid for the duration of this camera's acquisition mode being set to HARDWARE """ if acquisition_mode is CameraAcquisitionMode.HARDWARE_TRIGGER: if not self._hw_trigger_fn: raise ValueError( "Cannot set HARDWARE_TRIGGER camera acquisition mode without a hw_trigger_fn. You must provide one when constructing the camera." ) if not self._hw_set_strobe_delay_ms_fn: raise ValueError( "Cannot set HARDWARE_TRIGGER camera acquisition mode without a hw_set_strobe_delay_ms_fn. You must provide one when constructing the camera." ) return self._set_acquisition_mode_imp(acquisition_mode=acquisition_mode) @abc.abstractmethod def _set_acquisition_mode_imp(self, acquisition_mode: CameraAcquisitionMode): """ Your subclass must implement this such that it switches the camera to this acquisition mode. The top level set_acquisition_mode handles storing the self._hw_trigger_fn for you so you are guaranteed to have a valid callable self._hw_trigger_fn if in hardware trigger mode. If things like setting a remote strobe, or other settings, are needed when you change the mode you must handle that here. """ pass @abc.abstractmethod def get_acquisition_mode(self) -> CameraAcquisitionMode: """ Returns the current acquisition mode. """ pass def send_trigger(self, illumination_time: Optional[float] = None): """ If in an acquisition mode that needs triggering, send a trigger. If in HARDWARE_TRIGGER mode, you are guaranteed to have a self._hw_trigger_fn and should call that. If in CONTINUOUS mode, this can be a no-op. The illumination_time argument can be used for HARDWARE_TRIGGER cases where the hardware trigger mechanism knows how to control illumination (and may take into account a strobe delay). If not using a hardware trigger system that controls illumination, a non-None illumination_time is allowed (but will be ignored) When this returns, it does not mean it is safe to immediately send another trigger. If a settings change that pauses streaming is in flight (the operation holds self._trigger_lock, see _pause_streaming), the trigger is dropped: sending a trigger into a stream that is being torn down or brought back up is never useful. Callers pace themselves via get_ready_for_trigger; implementations should report not-ready there while streaming is off (see HamamatsuCamera). """ if not self._trigger_lock.acquire(blocking=False): self._log.debug("Camera settings change in progress, dropping trigger.") return try: self._send_trigger_imp(illumination_time) finally: self._trigger_lock.release() @abc.abstractmethod def _send_trigger_imp(self, illumination_time: Optional[float] = None): """ Driver-specific trigger implementation called by send_trigger. Implementations must raise CameraError if the camera cannot accept a trigger (not streaming, not ready, or the vendor call fails). """ pass def get_ready_for_trigger(self) -> bool: """ Returns true if the camera is ready for another trigger, false otherwise. Calling send_trigger when this is False will result in an exception from send_trigger. Never true while a streaming-pausing settings change is in flight (the operation holds self._trigger_lock, see _pause_streaming): the driver's own check is not even consulted, so it cannot race the reconfiguration with SDK calls from the trigger thread. Callers such as LiveController skip the trigger and re-check. """ if not self._trigger_lock.acquire(blocking=False): return False try: return self._get_ready_for_trigger_imp() finally: self._trigger_lock.release() @abc.abstractmethod def _get_ready_for_trigger_imp(self) -> bool: """ Driver-specific part of get_ready_for_trigger, called with no settings change in flight: typically "the previous trigger has been consumed (or timed out)". Must not block. """ pass @abc.abstractmethod def set_region_of_interest(self, offset_x: int, offset_y: int, width: int, height: int): """ Set the region of interest of the camera so that returned frames only contain this subset of the full sensor image. """ pass @abc.abstractmethod def get_region_of_interest(self) -> Tuple[int, int, int, int]: """ Returns the region of interest as a tuple of (x corner, y corner, width, height) """ pass @abc.abstractmethod def set_temperature(self, temperature_deg_c: Optional[float]): """ Set the desired temperature of the camera in degrees C. If None is given as input, use a sane default for the camera. """ pass @abc.abstractmethod def get_temperature(self) -> float: """ Get the current temperature of the camera in deg C. """ pass @abc.abstractmethod def set_temperature_reading_callback(self, callback: Callable): """ Set the callback to be called when the temperature reading changes. """ pass @abc.abstractmethod def close(self): """ Close the camera. """ pass