LiquidLens#

using Itala;
using Itala.GenApi;

/// <summary>
/// "LiquidLens" example shows how to control optics
///  with integrated liquid lens technology directly from the device,
///  in both current and power mode. The liquid lens functionality is
///  exposed as a custom set of GenICam features in the device nodemap.
///  WARNING: To avoid additional complexity, the following example
///  doesn't restore the initial liquid lens configuration, just its
///  activation state.
/// </summary>
internal class Program
{

  private static void LiquidLens_Sample()
  {
    Console.WriteLine("######## LiquidLens ########");
    ISystem system = SystemFactory.Create();
    List<DeviceInfo> deviceInfos = system.EnumerateDevices();

    if (deviceInfos.Count == 0)
      throw new ItalaRuntimeException("No devices found. Example canceled.");
    if (deviceInfos[0].AccessStatus != DeviceAccessStatus.AvailableReadWrite)
      throw new ItalaRuntimeException("Target device is unaccessible in RW mode. Example canceled.");
    IDevice device = system.CreateDevice(deviceInfos[0]);
    Console.WriteLine("First device initialized.");

    // Enable the liquid lens functionality. Keep the original activation state and restore
    // it at the end of the program.
    IBoolean oeLiquidLensEnable = device.GetNodeMap().GetNode<IBoolean>("oeLiquidLensEnable");
    if (!oeLiquidLensEnable.IsWritable)
      throw new ItalaRuntimeException("Unable to activate liquid lens functionality. Aborting.");
    bool originalOeliquidLensEnable = oeLiquidLensEnable.Value;
    oeLiquidLensEnable.Value = true;

    Console.WriteLine("Liquid lens functionality enabled.");

    // Gather some information about the connected liquid lens.
    IFloat oeLiquidLensTemperature = device.GetNodeMap().GetNode<IFloat>("oeLiquidLensTemperature");
    if (!oeLiquidLensTemperature.IsReadable)
      throw new ItalaRuntimeException("Unable to read liquid lens temperature. Aborting.");

    IString oeLiquidLensSerialNumber = device.GetNodeMap().GetNode<IString>("oeLiquidLensSerialNumber");
    if (!oeLiquidLensSerialNumber.IsReadable)
      throw new ItalaRuntimeException("Unable to read liquid lens serial number. Aborting.");

    Console.WriteLine("Connected lens is " + oeLiquidLensSerialNumber.Value);
    Console.WriteLine("Lens temperature: " + oeLiquidLensTemperature.Value + "C");

    // 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 results in 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.
    IEnumeration oeLiquidLensMode = device.GetNodeMap().GetNode<IEnumeration>("oeLiquidLensMode");
    if (!oeLiquidLensMode.IsWritable)
      throw new ItalaRuntimeException("Unable to configure the liquid lens mode. Aborting.");
    oeLiquidLensMode.FromString("CurrentMode");

    Console.WriteLine("\tCurrent mode enabled.");

    // Define the amount of current to be set in mA.
    double lensCurrent = 10.0;

    // Write it to the node.
    IFloat oeLiquidLensCurrent = device.GetNodeMap().GetNode<IFloat>("oeLiquidLensCurrent");
    if (!oeLiquidLensCurrent.IsWritable)
      throw new ItalaRuntimeException("Unable to configure the amount of current of the lens. Aborting.");
    oeLiquidLensCurrent.Value = lensCurrent;

    // Read the amount of optical power resulting from the electrical current set.
    IFloat oeLiquidLensResultingPower = device.GetNodeMap().GetNode<IFloat>("oeLiquidLensResultingPower");
    if (!oeLiquidLensResultingPower.IsReadable)
      throw new ItalaRuntimeException("Unable to read the amount of power of the lens. Aborting.");

    Console.WriteLine("\tLens current set to " + lensCurrent + "mA.");
    Console.WriteLine("\tResulting lens power is  " + oeLiquidLensResultingPower.Value + " dpt.\n");

    // Switch to power mode.
    oeLiquidLensMode.FromString("PowerMode");
    Console.WriteLine("\tPower mode enabled.");

    // Define the lens power to be set in dpt.
    double lensPower = 2.0;

    // Write it to the node.
    IFloat oeLiquidLensPower = device.GetNodeMap().GetNode<IFloat>("oeLiquidLensPower");
    if (!oeLiquidLensPower.IsWritable)
      throw new ItalaRuntimeException("Unable to configure the amount of power of the lens. Aborting.");
    oeLiquidLensPower.Value = lensPower;

    // Read the amount of current resulting from the optical power set.
    IFloat oeLiquidLensResultingCurrent = device.GetNodeMap().GetNode<IFloat>("oeLiquidLensResultingCurrent");
    if (!oeLiquidLensResultingCurrent.IsReadable)
      throw new ItalaRuntimeException("Unable to read the amount of current from the lens. Aborting.");

    Console.WriteLine("\tLens set to " + lensPower + " dpt.");
    Console.WriteLine("\tResulting lens current is  " + oeLiquidLensResultingCurrent.Value + " mA.\n");

    // Restore the original lens activation state.
    oeLiquidLensEnable.Value = originalOeliquidLensEnable;

    device.Dispose();
    Console.WriteLine("Device instance disposed.");
    system.Dispose();
    Console.WriteLine("System instance disposed.");
  }

  private static void Main(string[] args)
  {
    try
    {
      LiquidLens_Sample();
    }
    catch (Exception ex)
    {
      Console.WriteLine(ex.ToString());
    }
  }
}