326 lines
9.8 KiB
C

// GrabOnce.c
/* @brief: This example shows users how to use IKapC and IKapBoard libraries to grab image once with CoaXPress camera. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "IKapBoard.h"
#include "IKapC.h"
// Pre-defined.
#define HANDLE void*
#define CALLBACK
// Camera device handle.
ITKDEVICE g_hCamera = NULL;
// Frame grabber device handle.
HANDLE g_hBoard = (void*)-1;
// File name of configuration file.
char configFilename[256] = "";
/* @brief: Determine whether the IKapBoard function is called successfully.
* @param[in] errc: Function return value. */
#define CHECK_IKAPBOARD(errc) if (errc != 1) printIKapBoardErrorAndExit()
/* @brief: Determine whether the IKapC function is called successfully.
* @param[in] errc: Function return value. */
#define CHECK_IKAPC(errc) if (errc != ITKSTATUS_OK) printIKapCErrorAndExit(errc)
/* @brief: Print IKapBoard error message and exit the program. */
void printIKapBoardErrorAndExit();
/* @brief: Print IKapC error message and exit the program.
* @param[in] errc: Function return value. */
void printIKapCErrorAndExit(ITKSTATUS errc);
/* @brief: Users enter Enter to exit the program. */
void pressEnterToExit(void);
/* @brief: Configure camera device.
* @param[out] hCamera: Camera device handle.
* @param[out] hBoard: Frame grabber device handle. */
void ConfigureCamera(ITKDEVICE *hCamera, HANDLE *hBoard);
/* @brief: Configure frame grabber device.
* @param[in] hCamera: Camera device handle.
* @param[in] hBoard: Frame grabber device handle. */
void ConfigureFrameGrabber(ITKDEVICE hCamera, HANDLE hBoard);
/* @brief: Start grabbing images.
* @param[in] hCamera: Camera device handle.
* @param[in] hBoard: Frame grabber device handle. */
void StartGrabImage(ITKDEVICE hCamera, HANDLE hBoard);
/* @brief: Unregister callback functions. */
void UnRegisterCallback();
/* @brief: This function is registered as a callback function. When starting grabbing images, the function will be called.
* @param[in] pContext: Input parameter. */
void CALLBACK OnGrabStart(void *pContext);
/* @brief: This function is registered as a callback function. When a frame of image grabbing ready, the function will be called.
* @param[in] pContext: Input parameter. */
void CALLBACK OnFrameReady(void *pContext);
/* @brief: This function is registered as a callback function. When grabbing images time out, the function will be called.
* @param[in] pContext: Input parameter. */
void CALLBACK OnTimeout(void *pContext);
/* @brief: This function is registered as a callback function. When grabbing frame lost, the function will be called.
* @param[in] pContext: Input parameter. */
void CALLBACK OnFrameLost(void *pContext);
/* @brief: This function is registered as a callback function. When stopping grabbing images, the function will be called.
* @param[in] pContext: Input parameter. */
void CALLBACK OnGrabStop(void *pContext);
int main()
{
// Return value of IKapC functions.
ITKSTATUS res = ITKSTATUS_OK;
// Return value of IKapBoard functions.
int ret = IK_RTN_OK;
// Initialize IKapC runtime environment.
res = ItkManInitialize();
CHECK_IKAPC(res);
// Configure camera device.
ConfigureCamera(&g_hCamera, &g_hBoard);
// Configure frame grabber device.
ConfigureFrameGrabber(g_hCamera, g_hBoard);
// Start grabbing images.
StartGrabImage(g_hCamera, g_hBoard);
// Wait for grabbing images finished.
ret = IKapWaitGrab(g_hBoard);
CHECK_IKAPBOARD(ret);
// Stop grabbing images.
ret = IKapStopGrab(g_hBoard);
CHECK_IKAPBOARD(ret);
// Unregister callback functions.
UnRegisterCallback();
// Close frame grabber device.
ret = IKapClose(g_hBoard);
CHECK_IKAPBOARD(ret);
// Release IKapC runtime environment.
ItkManTerminate();
pressEnterToExit();
return 0;
}
// Print IKapBoard error message and exit the program.
void printIKapBoardErrorAndExit()
{
// Get error code message.
IKAPERRORINFO errc;
IKapGetLastError(&errc, 1);
// Print error message.
fprintf(stderr, "IKapBoard:Index: %d, error code:%04x\n", errc.uBoardIndex, errc.uErrorCode);
pressEnterToExit();
exit(EXIT_FAILURE);
}
// Print IKapC error message and exit the program.
void printIKapCErrorAndExit(ITKSTATUS errc)
{
fprintf(stderr, "IKapC error code: %08X", errc);
ItkManTerminate();
pressEnterToExit();
exit(EXIT_FAILURE);
}
// Configure camera device.
void ConfigureCamera(ITKDEVICE *hCamera, HANDLE *hBoard)
{
ITKSTATUS res = ITKSTATUS_OK;
uint32_t numCameras = 0;
// Enumerate the number of available cameras. Before opening the camera, ItkManGetDeviceCount() function must be called.
res = ItkManGetDeviceCount(&numCameras);
CHECK_IKAPC(res);
// When there is no connected cameras.
if (numCameras == 0)
{
printf("No camera.\n");
ItkManTerminate();
pressEnterToExit();
exit(EXIT_FAILURE);
}
// Open CoaXPress camera.
for (uint32_t i = 0; i < numCameras; i++)
{
ITKDEV_INFO di;
// Get camera device information.
res = ItkManGetDeviceInfo(i, &di);
printf("Using camera: serial: %s, name: %s, interface: %s.\n", di.SerialNumber, di.FullName, di.DeviceClass);
// When the device is CoaXPress camera and the serial number is proper.
if (strcmp(di.DeviceClass, "CoaXPress") == 0 && strcmp(di.SerialNumber, "") != 0)
{
ITK_CXP_DEV_INFO cxp_cam_info;
IKAP_CXP_BOARD_INFO cxp_board_info;
// Open camera.
res = ItkDevOpen(i, ITKDEV_VAL_ACCESS_MODE_EXCLUSIVE, hCamera);
CHECK_IKAPC(res);
// Get CoaXPress camera device information.
res = ItkManGetCXPDeviceInfo(i, &cxp_cam_info);
CHECK_IKAPC(res);
// Open frame grabber.
memcpy(&cxp_board_info, &cxp_cam_info, sizeof cxp_cam_info);
*hBoard = IKapOpenCXP(IKBoardPCIE, cxp_board_info.BoardIndex, cxp_board_info);
if (*hBoard == (void*)-1)
CHECK_IKAPC(IKStatus_OpenBoardFail);
}
}
}
// Configure frame grabber device.
void ConfigureFrameGrabber(ITKDEVICE hCamera, HANDLE hBoard)
{
ITKSTATUS res = ITKSTATUS_OK;
int ret = IK_RTN_OK;
// Input load configuration address
printf("Please input load configuration address, for example: ./GrabOnce.vlcf\n");
gets(configFilename);
// Load configuration file.
ret = IKapLoadConfigurationFromFile(hBoard, configFilename);
CHECK_IKAPBOARD(ret);
// Set frame count of buffer.
ret = IKapSetInfo(hBoard, IKP_FRAME_COUNT, 1);
CHECK_IKAPBOARD(ret);
// Set time out time.
int timeout = -1;
ret = IKapSetInfo(hBoard, IKP_TIME_OUT, timeout);
CHECK_IKAPBOARD(ret);
// Set grab mode.
int grab_mode = IKP_GRAB_NON_BLOCK;
ret = IKapSetInfo(hBoard, IKP_GRAB_MODE, grab_mode);
CHECK_IKAPBOARD(ret);
// Set transfer mode.
int transfer_mode = IKP_FRAME_TRANSFER_SYNCHRONOUS_NEXT_EMPTY_WITH_PROTECT;
ret = IKapSetInfo(hBoard, IKP_FRAME_TRANSFER_MODE, transfer_mode);
CHECK_IKAPBOARD(ret);
// Register callback functions.
ret = IKapRegisterCallback(hBoard, IKEvent_GrabStart, OnGrabStart, hBoard);
CHECK_IKAPBOARD(ret);
ret = IKapRegisterCallback(hBoard, IKEvent_FrameReady, OnFrameReady, hBoard);
CHECK_IKAPBOARD(ret);
ret = IKapRegisterCallback(hBoard, IKEvent_FrameLost, OnFrameLost, hBoard);
CHECK_IKAPBOARD(ret);
ret = IKapRegisterCallback(hBoard, IKEvent_TimeOut, OnTimeout, hBoard);
CHECK_IKAPBOARD(ret);
ret = IKapRegisterCallback(hBoard, IKEvent_GrabStop, OnGrabStop, hBoard);
CHECK_IKAPBOARD(ret);
}
// Start grabbing images.
void StartGrabImage(ITKDEVICE hCamera, HANDLE hBoard)
{
ITKSTATUS res = ITKSTATUS_OK;
int ret = IK_RTN_OK;
res = ItkDevExecuteCommand(hCamera, "AcquisitionStop");
CHECK_IKAPC(res);
ret = IKapStartGrab(hBoard, 1);
CHECK_IKAPBOARD(ret);
res = ItkDevExecuteCommand(hCamera, "AcquisitionStart");
CHECK_IKAPC(res);
}
// Unregister callback functions.
void UnRegisterCallback()
{
int ret = IK_RTN_OK;
ret = IKapUnRegisterCallback(g_hBoard, IKEvent_GrabStart);
ret = IKapUnRegisterCallback(g_hBoard, IKEvent_FrameReady);
ret = IKapUnRegisterCallback(g_hBoard, IKEvent_FrameLost);
ret = IKapUnRegisterCallback(g_hBoard, IKEvent_TimeOut);
ret = IKapUnRegisterCallback(g_hBoard, IKEvent_GrabStop);
}
// Users enter Enter to exit the program.
void pressEnterToExit(void)
{
fprintf(stderr, "\nPress enter to exit.\n");
while (getchar() != '\n');
}
// This function is registered as a callback function. When starting grabbing images, the function will be called.
void CALLBACK OnGrabStart(void *pContext)
{
printf("Start grabbing image.\n");
}
// This function is registered as a callback function. When a frame of image grabbing ready, the function will be called.
void CALLBACK OnFrameReady(void *pContext)
{
HANDLE hDev = (HANDLE)pContext;
unsigned char* pUserBuffer = NULL;
int nFrameSize = 0;
int nFrameCount = 0;
IKAPBUFFERSTATUS status;
IKapGetInfo(hDev, IKP_FRAME_COUNT, &nFrameCount);
IKapGetBufferStatus(hDev, 0, &status);
// When the buffer is full.
if (status.uFull == 1)
{
// Get the size of a frame of image.
IKapGetInfo(hDev, IKP_FRAME_SIZE, &nFrameSize);
// Get the buffer address.
IKapGetBufferAddress(hDev, 0, (void**)&pUserBuffer);
}
printf("Grab 1 frame ready.\n");
}
// This function is registered as a callback function. When grabbing images time out, the function will be called.
void CALLBACK OnTimeout(void *pContext)
{
printf("Grab image timeout.\n");
}
// This function is registered as a callback function. When grabbing frame lost, the function will be called.
void CALLBACK OnFrameLost(void *pContext)
{
printf("Grab frame lost.\n");
}
// This function is registered as a callback function. When stopping grabbing images, the function will be called.
void CALLBACK OnGrabStop(void *pContext)
{
printf("Stop grabbing image.\n");
}