Events#

from itala import itala
import time

# Define a handler and override the OnDeviceEvent method. The handler can
# be registered to Itala API so that, when a device event is detected,
# the overridden function is automatically called.
class ExposureEndEventHandler(itala.DeviceEventHandler):
    def __init__(self, device):
        itala.DeviceEventHandler.__init__(self)
        self.device = device

    def on_device_event(self, eventId):
        nodemap = self.device.node_map
        event_exposure_end_node = nodemap.EventExposureEnd
        if(eventId == event_exposure_end_node.value):
            event_exposure_end_timestamp_node = nodemap.EventExposureEndTimestamp
            print("\tExposure end event detected by the Handler, occurred at " + str(event_exposure_end_timestamp_node.value))

# Define a callback function which can be registered to a node with GenApi.
# When the target node value changes, the callback is executed. The value
# change is performed automatically by Itala API when the event related
# to the node occurs (e.g. when an exposure end event occurs,
# the EventExposureEndTimestamp node value changes).
def EventExposureEndTimestampCallback(node):        
        print("\tExposureEndEventTimestamp value change detected by the Callback, new value is "+ str(node.value))

system = itala.create_system()
devices_info = system.enumerate_devices(500)
if(len(devices_info) == 0):
    print("No devices found. Example canceled.")
    exit(1)
if(devices_info[0].access_status != itala.DeviceAccessStatus_AvailableReadWrite):
    print("Target device is unaccessible in RW mode. Example cancelled.")
    exit(1)
device = system.create_device(devices_info[0])
print("First device initialized.")
nodemap = device.node_map

# Select the exposure end event..
event_selector_node = nodemap.EventSelector
if(not itala.is_writable(event_selector_node)):
    print("Unable to select the exposure end  event. Aborting")
    exit(1)
event_selector_node.from_string("ExposureEnd")

# ..and enable it on the device. The original value is also kept so
# that it can be restored at the end of the example.
event_notification_node = nodemap.EventNotification
if(not itala.is_writable(event_notification_node)):
    print("Unable to turn on notifications for exposure end event. Aborting")
    exit(1)
original_event_notification = event_notification_node.get_int_value()
event_notification_node.from_string("On")
print("Exposure end event enabled on the device")

# Start to listen for events on the host.
device.enable_events()
print("Event listening enabled on the host")

# Get one of the exposure end event related nodes. In this case, the frame timestamp
# is chosen. WARNING: the event node is not available until the event occurs, so no
# availability check is performed here.
event_exposure_end_timestamp = nodemap.EventExposureEndTimestamp

# Get one of the exposure end event related nodes. In this case, the frame timestamp
# is chosen. WARNING: the event node is not available until the event occurs, so no
# availability check is performed here.
callback_handle = itala.register(event_exposure_end_timestamp.node, EventExposureEndTimestampCallback)
print("Callback registered to the EventExposureEndTimestamp node.")

# Alternatively, an instance of the user-defined handler is created and passed
# to Itala API. When a device event occurs, the handler's function ovverridden
# by the user is called.
exposure_end_handler = ExposureEndEventHandler(device)
device.register_handler(exposure_end_handler)

device.start_acquisition()
print("Acquisition started.")

# Let the camera perform some exposures..
time.sleep(2)

device.stop_acquisition()

# When the callback-on-event functionality is no longer required, the function
# must be unregistered from the node. The same is true for the handler.
# The event listening must be disabled.
itala.deregister(callback_handle)
device.disable_events()
device.deregister_handler()

event_notification_node.set_int_value(original_event_notification)

device.dispose()
print("Device instance disposed.")

system.dispose()
print("System instance disposed.")