NodeCallback#

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

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;
}

// Define a callback function which can be registered to a node with GenApi.
// When the target node value changes, the callback is executed.
void OnPayloadSizeChanged(H_NODE hNode, void* pContext) {
  int64_t value = 0;
  NODE_IntegerGetValue(hNode, &value);
  printf("\tOnPayloadSizeChanged function called.\n");
  printf("\tPayloadSize node changed. New value is  %jd \n\n", value);
}

int main(int argc, char** argv) {
  printf("***** NodeCallback 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 found is initialized.\n\n");

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

  // Get the PixelFormat node and set its value to Mono8 for example
  // purposes. The original value is saved and then restored at the end of
  // the program.
  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;
  }
  int64_t originalPixelFormat = 0;
  error = NODE_EnumerationGetIntValue(hNodePixelFormat, &originalPixelFormat);
  if (error) return ErrorManager(error);
  error = NODE_FromString(hNodePixelFormat, "Mono8");
  if (error) return ErrorManager(error);

  // Get the PayloadSize node.
  H_NODE hNodePayloadSize = NULL;
  error = NODEMAP_GetNode(hNodeMap, "PayloadSize", &hNodePayloadSize);
  if (error) return ErrorManager(error);
  bool isReadable = false;
  error = IsNodeReadable(hNodePayloadSize, &isReadable);
  if (error) return ErrorManager(error);
  if (!isReadable) {
    printf("Unable to get the image payload size. Aborting.\n");
    return ItalaErrorError;
  }
  int64_t payloadSize = 0;
  error = NODE_IntegerGetValue(hNodePayloadSize, &payloadSize);
  if (error) return ErrorManager(error);
  printf("The payload size id %jd with Mono8 format.\n", payloadSize);

  // Register the callback to the PayloadSize node so that when a
  // change in its value occurs, the function is executed.
  H_NODECALLBACK hNodeCallback = NULL;
  error = NODE_RegisterCallback(hNodePayloadSize, OnPayloadSizeChanged, NULL, &hNodeCallback);
  if (error) return ErrorManager(error);
  printf("OnPayloadSizeChanged function registered to PayloadSize node.\n\n");

  printf("Setting pixel format to Mono12p..\n");

  // Set the pixel format to Mono12p. At this point, the callback gets
  // automatically executed by GenApi since the PayloadSize feature
  // is changed after the new PixelFormat.
  error = NODE_FromString(hNodePixelFormat, "Mono12p");
  if (error) return ErrorManager(error);

  // Deregister the callback function when done.
  error = NODE_DeregisterCallback(hNodePayloadSize, hNodeCallback);
  if (error) return ErrorManager(error);

  // Restore the original pixel format value.
  error = NODE_EnumerationSetIntValue(hNodePixelFormat, originalPixelFormat);
  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");
  return 0;
}