LiquidLens#

LiquidLens.py#
from itala import itala


def liquid_lens():
    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

    # Enable the liquid lens functionality. Keep the original activation state and restore
    # it at the end of the program.
    oe_liquid_lens_enable = nodemap.oeLiquidLensEnable
    if(not itala.is_writable(oe_liquid_lens_enable)):
        print("Unable to activate liquid lens functionality. Aborting.")
        exit(1)
    original_liquid_lens_enable = oe_liquid_lens_enable.value
    oe_liquid_lens_enable.value = True

    print("Liquid lens functionality enabled.")

    # Gather some information about the connected liquid lens.
    oe_liquid_lens_temperature = nodemap.oeLiquidLensTemperature
    if(not itala.is_readable(oe_liquid_lens_temperature)):
        print("Unable to read liquid lens temperature. Aborting.")
        exit(1)

    oe_liquid_lens_serial_number = nodemap.oeLiquidLensSerialNumber
    if(not itala.is_readable(oe_liquid_lens_serial_number)):
        print("Unable to read liquid lens serial number. Aborting.")
        exit(1)

    print(f"Connected lens is: {oe_liquid_lens_serial_number.value}")
    print(f"Lens temperature: {oe_liquid_lens_temperature.value}")

    # Liquid lenses can be controlled in current or power mode. In the first case, the
    # input given to the lens is an amount of current which leads to a certain
    # optical power. The second case is the opposite: by specifing the optical power
    # as input, the liquid lens absorbs a certain amount of current.
    # Let's start with current mode.
    oe_liquid_lens_mode = nodemap.oeLiquidLensMode
    if(not itala.is_writable(oe_liquid_lens_mode)):
        print("Unable to configure the liquid lens mode. Aborting.")
        exit(1)
    oe_liquid_lens_mode.from_string("CurrentMode")

    print("\tCurrent mode enabled.")

    # Define the amount of current to be set in mA.
    lens_current = 10

    # Write it to the node
    oe_liquid_lens_current = nodemap.oeLiquidLensCurrent
    if(not itala.is_writable(oe_liquid_lens_current)):
        print("Unable to configure the amount of current of the lens. Aborting.")
        exit(1)
    oe_liquid_lens_current.value=lens_current

    # Read the amount of optical power resulting from the electrical current set
    oe_liquid_lens_resulting_power = nodemap.oeLiquidLensResultingPower
    if(not itala.is_readable(oe_liquid_lens_resulting_power)):
        print("Unable to read the resulting amount of power from the lens. Aborting.")
        exit(1)

    print(f"\tLens current set to {lens_current} mA.")
    print(f"\tResulting lens power is {oe_liquid_lens_resulting_power.value} dpt.")

    # Switch to power mode
    oe_liquid_lens_mode.from_string("PowerMode")
    print("\tPower mode enabled.")

    # Define the lens power to be set in dpt.
    lens_power = 2

    # Write it to the node.
    oe_liquid_lens_power = nodemap.oeLiquidLensPower
    if(not itala.is_writable(oe_liquid_lens_power)):
        print("Unable to configure the amount of power of the lens. Aborting.")
        exit(1)
    oe_liquid_lens_power.value = lens_power

    # Read the amount of current resulting from the optical power set.
    oe_liquid_lens_resulting_current = nodemap.oeLiquidLensResultingCurrent
    if(not itala.is_readable(oe_liquid_lens_resulting_current)):
        print("Unable to read the resulting amount of current from the lens. Aborting.")
        exit(1)

    print(f"\tLens set to {lens_power} dpt.")
    print(f"\tResulting lens is {oe_liquid_lens_resulting_current.value} mA")

    # Restore the original lens activation state.
    oe_liquid_lens_enable.value = original_liquid_lens_enable

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

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


if __name__ == "__main__":
    try:
        liquid_lens()
    except Exception as e:
        print(str(e))
        exit(1)
    exit(0)