DeviceConfiguration#

using Itala;
using Itala.GenApi;

/// <summary>
/// "DeviceConfiguration" example shows how device parameters can
/// be configured using GenApi, in this case image width, height, pixel
/// format and exposure time.Novice GenApi users should read the
/// dedicated section of the Programmer's Guide available within ItalaApiNET
/// documentation.
/// </summary>
internal class Program
{
  const int WIDTH = 1024;
  const int HEIGHT = 1024;
  const string PIXEL_FORMAT = "Mono8";
  const int EXPOSURE_TIME = 90000;

  private static void DeviceConfiguration_Sample()
  {
    Console.WriteLine("######## DeviceConfiguration ########");
    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.");

    INodeMap deviceNodeMap = device.GetNodeMap();

    // Get the PixelFormat node
    IEnumeration pixelFormat = deviceNodeMap.GetNode<IEnumeration>("PixelFormat");

    // Check its validity and if it's in a valid state for being modification
    if (!pixelFormat.IsWritable)
      throw new ItalaRuntimeException("Unable to configure the pixel format. Aborting.");

    // Save the currently active pixel format as integer. This value will be
    // used during the deconfiguration phase to restore the original pixel
    // format.
    Int64 originalPixelFormat = pixelFormat.IntValue;

    // Set the desired pixel format from its string representation for convenience
    pixelFormat.FromString(PIXEL_FORMAT);
    Console.WriteLine("PixelFormat se to " + PIXEL_FORMAT);

    // The same approach is then used for the other features.
    // Width
    IInteger width = deviceNodeMap.GetNode<IInteger>("Width");
    if (!width.IsWritable)
      throw new ItalaRuntimeException("Unable to configure the image width. Aborting.");
    Int64 originalWidth = width.Value;
    width.Value = WIDTH;
    Console.WriteLine("Width set to " + WIDTH);

    // Height
    IInteger height = deviceNodeMap.GetNode<IInteger>("Height");
    if (!height.IsWritable)
      throw new ItalaRuntimeException("Unable to configure the image height. Aborting.");
    Int64 originalHeight = height.Value;
    height.Value = HEIGHT;
    Console.WriteLine("Height set to " + HEIGHT);

    // ExposureTime
    IFloat exposureTime = deviceNodeMap.GetNode<IFloat>("ExposureTime");
    if (!exposureTime.IsWritable)
      throw new ItalaRuntimeException("Unable to configure the exposure time. Aborting.");
    double originalExposureTime = exposureTime.Value;
    exposureTime.Value = EXPOSURE_TIME;
    Console.WriteLine("ExposureTime set to " + EXPOSURE_TIME);

    Console.WriteLine("\nRolling back to original camera features state... ");
    pixelFormat.IntValue = originalPixelFormat;
    width.Value = originalWidth;
    height.Value = originalHeight;
    exposureTime.Value = originalExposureTime;

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

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