DeviceConfiguration#

#include <stdio.h>
#include <stdlib.h>
#include "ItalaApiC/ItalaC.h"

#define WIDTH 1024
#define HEIGHT 1024
#define PIXEL_FORMAT "Mono8"
#define EXPOSURE_TIME 90000  // microseconds

ItalaError ErrorManager(ItalaError e) {
  size_t sizeMessage = 255;
  char* message = (char*)malloc(sizeof(char) * sizeMessage);
  ERR_GetLastErrorMessage(message, &sizeMessage);
  printf("\nITALA ERROR (%d):\t%s, %zu char", ERR_GetLastErrorCode(), message, sizeMessage);
  free(message);
  return e;
}

int main(int argc, char** argv) {
  printf("***** DeviceConfiguration example started. *****\n");
  ItalaError error = ItalaErrorSuccess;

  error = SYS_Initialize();
  if (error) return ErrorManager(error);

  size_t devicesInfoSize = 0;
  error = SYS_EnumerateDevices(700);
  if (error) return ErrorManager(error);
  error = SYS_GetDeviceCount(&devicesInfoSize);
  if (error) return ErrorManager(error);
  if (devicesInfoSize == 0) {
    printf("No devices found. Example canceled.\n");
    return ItalaErrorError;
  }
  DeviceInfo deviceInfo;
  error = SYS_GetDeviceByIndex(0, &deviceInfo);
  if (error) return ErrorManager(error);

  if (deviceInfo.AccessStatus != AvailableReadWrite) {
    printf("Target device is unaccessible in RW mode. Example canceled.\n");
    return ItalaErrorError;
  }

  H_DEVICE hDevice = NULL;
  error = SYS_CreateDevice(deviceInfo, &hDevice);
  if (error) return ErrorManager(error);
  printf("First device initialized.");
  printf("Camera configuration started..");

  H_NODEMAP hNodeMap = NULL;
  error = DEV_GetNodeMap(hDevice, &hNodeMap);
  if (error) return ErrorManager(error);

  // Get the PixelFormat node
  H_NODE hNodePixelFormat = NULL;
  error = NODEMAP_GetNode(hNodeMap, "PixelFormat", &hNodePixelFormat);
  if (error) return ErrorManager(error);

  bool isWritable = false;
  error = IsNodeWritable(hNodePixelFormat, &isWritable);
  if (error) return ErrorManager(error);
  if (!isWritable) {
    printf("Unable to configure the pixel format. Aborting.\n");
    return ItalaErrorError;
  }

  // Save the currently active pixel format as integer. This value will be
  // used during the deconfiguration phase to restore the original pixel
  // format.
  int64_t originalPixelFormat = 0;
  error = NODE_EnumerationGetIntValue(hNodePixelFormat, &originalPixelFormat);
  if (error) return ErrorManager(error);

  error = NODE_FromString(hNodePixelFormat, PIXEL_FORMAT);
  printf("PixelFormat set to %s\n", PIXEL_FORMAT);

  // The same approach is then used for the other features.
  // Width
  H_NODE hNodeWidth = NULL;
  NODEMAP_GetNode(hNodeMap, "Width", &hNodeWidth);
  if (error) return ErrorManager(error);

  error = IsNodeWritable(hNodeWidth, &isWritable);
  if (error) return ErrorManager(error);
  if (!isWritable) {
    printf("Unable to configure the image width. Aborting.\n");
    return ItalaErrorError;
  }
  int64_t originalWidth = 0;
  error = NODE_IntegerGetValue(hNodeWidth, &originalWidth);
  if (error) return ErrorManager(error);

  error = NODE_IntegerSetValue(hNodeWidth, WIDTH);
  printf("Width set to %d\n", WIDTH);

  // Height
  H_NODE hNodeHeight = NULL;
  NODEMAP_GetNode(hNodeMap, "Height", &hNodeHeight);
  if (error) return ErrorManager(error);

  error = IsNodeWritable(hNodeHeight, &isWritable);
  if (error) return ErrorManager(error);
  if (!isWritable) {
    printf("Unable to configure the image height. Aborting.\n");
    return ItalaErrorError;
  }
  int64_t originalHeight = 0;
  error = NODE_IntegerGetValue(hNodeHeight, &originalHeight);
  if (error) return ErrorManager(error);

  error = NODE_IntegerSetValue(hNodeHeight, HEIGHT);
  printf("Height set to %d\n", HEIGHT);

  // Height
  H_NODE hNodeExposureTime = NULL;
  NODEMAP_GetNode(hNodeMap, "ExposureTime", &hNodeExposureTime);
  if (error) return ErrorManager(error);

  error = IsNodeWritable(hNodeExposureTime, &isWritable);
  if (error) return ErrorManager(error);
  if (!isWritable) {
    printf("Unable to configure the exposure time. Aborting.\n");
    return ItalaErrorError;
  }
  double originalExposureTime = 0;
  error = NODE_FloatGetValue(hNodeExposureTime, &originalExposureTime);
  if (error) return ErrorManager(error);

  error = NODE_FloatSetValue(hNodeExposureTime, EXPOSURE_TIME);
  printf("ExposureTime set to %d\n", EXPOSURE_TIME);

  printf("Rolling back to original camera features state..\n\n");
  error = NODE_EnumerationSetIntValue(hNodePixelFormat, originalPixelFormat);
  if (error) return ErrorManager(error);
  error = NODE_IntegerSetValue(hNodeWidth, originalWidth);
  if (error) return ErrorManager(error);
  error = NODE_IntegerSetValue(hNodeHeight, originalHeight);
  if (error) return ErrorManager(error);
  error = NODE_FloatSetValue(hNodeExposureTime, originalExposureTime);
  if (error) return ErrorManager(error);

  error = DEV_Dispose(hDevice);
  hDevice = NULL;
  if (error) return ErrorManager(error);
  printf("Device instance disposed.\n");

  error = SYS_Dispose();
  if (error) return ErrorManager(error);
  printf("System instance disposed.\n");
}