#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <papiinterface/papiinterface.h>
void printLastError();
int main(int argc, char* argv[])
{
(void)argc;
(void)argv;
peak_status status = PEAK_STATUS_SUCCESS;
//////////////////////////////////////
// Init library
//////////////////////////////////////
printf("Initialising Library...\n");
status = peak_Library_Init();
if(PEAK_ERROR(status))
{
fprintf(stderr, "ERROR: Initialising the library failed! Status: %#06x\n", status);
printLastError();
return status;
}
//////////////////////////////////////
// Update camera list
//////////////////////////////////////
printf("Updating camera list...\n");
status = peak_CameraList_Update(NULL);
if(PEAK_ERROR(status))
{
fprintf(stderr, "ERROR: Updating the camera list failed! Status: %#06x\n", status);
printLastError();
(void)peak_Library_Exit();
return status;
}
//////////////////////////////////////
// Open first available camera in list
//////////////////////////////////////
printf("Opening first accessible camera in list...\n");
peak_camera_handle hCam = NULL;
status = peak_Camera_OpenFirstAvailable(&hCam);
if (PEAK_ERROR(status))
{
fprintf(stderr, "ERROR: Opening camera failed! Status: %#06x\n", status);
printLastError();
}
//////////////////////////////////////
// Setup frame rate
//////////////////////////////////////
double currentFramerate = 0.0; // Used in acquisition to calculate timeout
if(peak_FrameRate_GetAccessStatus(hCam) == PEAK_ACCESS_READWRITE) {
printf("Setting framerate to it's maximum...\n");
double minFramerate = 0.0;
double maxFramerate = 0.0;
double incFramerate = 0.0;
status = peak_FrameRate_GetRange(hCam, &minFramerate, &maxFramerate, &incFramerate);
if (PEAK_ERROR(status)) {
fprintf(stderr, "ERROR: Getting framerate range failed! Status: %#06x.\n", status);
printLastError();
(void) peak_Camera_Close(hCam);
(void) peak_Library_Exit();
return status;
}
status = peak_FrameRate_Set(hCam, maxFramerate);
if (PEAK_ERROR(status)) {
fprintf(stderr, "ERROR: Setting framerate failed! Status: %#06x.\n", status);
printLastError();
(void) peak_Camera_Close(hCam);
(void) peak_Library_Exit();
return status;
}
currentFramerate = maxFramerate;
}
else if(PEAK_IS_READABLE(peak_FrameRate_GetAccessStatus(hCam)))
{
status = peak_FrameRate_Get(hCam, ¤tFramerate);
if(PEAK_ERROR(status))
{
fprintf(stderr, "ERROR: Getting framerate failed! Status: %#06x.\n", status);
printLastError();
(void) peak_Camera_Close(hCam);
(void) peak_Library_Exit();
return status;
}
}
else
{
fprintf(stderr, "ERROR: Framerate cannot be get or set on this camera.\n");
(void)peak_Camera_Close(hCam);
(void)peak_Library_Exit();
return PEAK_STATUS_ACCESS_DENIED;
}
//////////////////////////////////////
// Start acquisition
//////////////////////////////////////
unsigned int framesToAcquire = 100;
// NOTE: Acquisition will stop automatically after framesToAcquire-Images have been received
// via peak_Acquisition_WaitForFrame and released via peak_Frame_Release
status = peak_Acquisition_Start(hCam, framesToAcquire);
if(PEAK_ERROR(status))
{
fprintf(stderr, "ERROR: Starting acquisition failed! Status: %#06x.\n", status);
printLastError();
(void)peak_Camera_Close(hCam);
(void)peak_Library_Exit();
return status;
}
//////////////////////////////////////
// Acquire x frames
//////////////////////////////////////
unsigned int pendingFrames = framesToAcquire;
unsigned int incompleteCount = 0;
unsigned int timeoutCount = 0;
// Calculate timeout for WaitForFrame (being 3 frames long)
uint32_t three_frame_times_timeout_ms = (uint32_t)ceil(3000.0 / currentFramerate);
printf("Will acquire %i frames\n", pendingFrames);
while(pendingFrames > 0)
{
peak_frame_handle hFrame;
status = peak_Acquisition_WaitForFrame(hCam, three_frame_times_timeout_ms, &hFrame);
if(status == PEAK_STATUS_TIMEOUT)
{
printf("t");
timeoutCount++;
if (timeoutCount > 99)
{
fprintf(stderr, "\nWARNING: Too many timeouts. Aborting acquisition.");
break;
}
else
{
continue;
}
}
else if(status == PEAK_STATUS_ABORTED)
{
printf("a");
break;
}
else if(PEAK_ERROR(status))
{
fprintf(stderr, "ERROR: WaitForFrame failed with status %#06x.\n", status);
printLastError();
break;
}
// At this point we successfully got a frame handle. We need to release it when done!
if(peak_Frame_IsComplete(hFrame) == PEAK_FALSE)
{
printf("i");
incompleteCount++;
}
else
{
// Frame successfully received.
printf(".");
}
status = peak_Frame_Release(hCam, hFrame);
if(PEAK_ERROR(status))
{
fprintf(stderr, "ERROR: Releasing frame failed with status %#06x.\n", status);
printLastError();
break;
}
pendingFrames--;
}
printf("\n");
// NOTE: Acquisition should be stopped automatically at this point.
//////////////////////////////////////
// Print statistics
//////////////////////////////////////
printf("Acquisition done.\n");
printf("Timouts: %u\n", timeoutCount);
printf("Incompletes: %u\n", incompleteCount);
//////////////////////////////////////
// Close everything
//////////////////////////////////////
printf("Closing camera...\n");
(void)peak_Camera_Close(hCam);
hCam = NULL;
printf("Camera closed.\n");
printf("Exiting library...\n");
(void)peak_Library_Exit();
printf("Library exited.\n");
return 0;
}
void printLastError()
{
peak_status lastErrorCode = PEAK_STATUS_SUCCESS;
peak_status status = PEAK_STATUS_SUCCESS;
size_t lastErrorMessageSize = 0;
// Get last error message size
status = peak_Library_GetLastError(&lastErrorCode, NULL, &lastErrorMessageSize);
if (PEAK_ERROR(status))
{
// Something went wrong getting the last error!
fprintf(stderr, "Last-Error: Getting last error code failed! Status: %#06x\n", status);
return;
}
// Get the corresponding error message.
char* lastErrorMessage = (char*)malloc(lastErrorMessageSize);
if (lastErrorMessage == NULL)
{
// Cannot allocate lastErrorMessage. Most likely not enough Memory.
fprintf(stderr, "Last-Error: Failed to allocate memory for the error message!\n");
free(lastErrorMessage);
return;
}
memset(lastErrorMessage, 0, lastErrorMessageSize);
status = peak_Library_GetLastError(&lastErrorCode, lastErrorMessage, &lastErrorMessageSize);
if (PEAK_ERROR(status))
{
// Unable to get error message. This shouldn't ever happen.
fprintf(stderr, "Last-Error: Getting last error message failed! Status: %#06x; Last error code: %#06x\n",
status, lastErrorCode);
free(lastErrorMessage);
return;
}
printf("Last-Error: %s | Code: %#06x\n", lastErrorMessage, lastErrorCode);
free(lastErrorMessage);
}
|