GrabTrigger#

using Itala;
using Itala.GenApi;

/// <summary>
/// "GrabTrigger" is an extension of the simple Grab example which introduces
/// the trigger functionality. A triggered acquisition consist of a signal (hardware or
/// virtual, i.e. a command) sent to the camera by the user. When a trigger is received,
/// the camera acquires a frame and makes it available for grab. As usual, the
/// trigger functionality is available via GenApi.
/// </summary>
internal class Program
{
  const int ACQUIRE_COUNT = 5;

  private static void GrabTrigger_Sample()
  {
    Console.WriteLine("######## GrabTrigger ########");
    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.");
    Console.WriteLine("\nConfiguring trigger mode...");

    // Select the "action" that must be performed when the trigger is received.
    IEnumeration triggerSelector = device.GetNodeMap().GetNode<IEnumeration>("TriggerSelector");
    if (!triggerSelector.IsWritable)
      throw new ItalaRuntimeException("Unable to select the trigger type. Aborting.");
    triggerSelector.FromString("FrameBurstStart");

    // Set the source of the trigger. In this case, a software trigger (which is
    // just a command) is used.
    IEnumeration triggerSource = device.GetNodeMap().GetNode<IEnumeration>("TriggerSource");
    if (!triggerSource.IsWritable)
      throw new ItalaRuntimeException("Unable to configure the trigger. Aborting.");
    Int64 originalTriggerSource = triggerSource.IntValue;
    triggerSource.FromString("Software");

    // Finally, enable the trigger mode.
    IEnumeration triggerMode = device.GetNodeMap().GetNode<IEnumeration>("TriggerMode");
    if (!triggerMode.IsWritable)
      throw new ItalaRuntimeException("Unable to configure the trigger. Aborting.");
    Int64 originalTriggerMode = triggerMode.IntValue;
    triggerMode.FromString("On");

    device.StartAcquisition();
    Console.WriteLine("Acquisition started.\n");

    IImage image;
    for(int i = 0; i< ACQUIRE_COUNT; i++)
    {
      ICommand triggerSoftware = device.GetNodeMap().GetNode<ICommand>("TriggerSoftware");
      if (!triggerSoftware.IsWritable)
        throw new ItalaRuntimeException("Unable to send software trigger command. Aborting.");
      triggerSoftware.Execute();

      Console.WriteLine("\tSoftware trigger sent, image acquired.");
      Console.WriteLine("\tPress Enter to grab the acquired image.");
      Console.ReadKey();

      image = device.GetNextImage(1000);
      if (image.IsIncomplete)
      {
        // Report some info if the image is partially filled.
        Console.WriteLine("Image " + i + "is incomplete.");
        Console.WriteLine("\tBytes filled: "+ image.BytesFilled+"/"+image.PayloadSize);
      }
      else
      {
        Console.WriteLine("Image " + i + " grabbed.");
      }

      // DO SOMETHING WITH THE IMAGE..

      image.Dispose();
      Console.WriteLine();
    }

    device.StopAcquisition();

    // Restore the original values
    triggerSource.IntValue = originalTriggerSource;
    triggerMode.IntValue = originalTriggerMode;

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

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