5
#define fg_apc_data TCameraLink
7
#include <fgrab_struct.h>
8
#include <fgrab_prototyp.h>
9
#include <fgrab_define.h>
14
#define DLLNAME "libFullAreaGray8.so"
16
#define CL_PORT PORT_A // Full speed, PORT_AB ?
17
#define CL_SAMPLES_PER_PIXEL 1
18
#define CL_BYTES_PER_SAMPLE 1
20
#define CL_SECURITY_BUFFERS 2 // We will skip the buffer if less than that amount of free buffers remained before overwrite
21
#define CL_TIMEOUT 2 // In seconds
24
static int cl_new_frame_callback(frameindex_t frame, CameraLink *ctx) {
25
void *data = Fg_getImagePtr(ctx->fg, frame, CL_PORT);
26
ctx->cb(ctx->cbctx, frame, data);
32
CameraLink *cl_create() {
33
CameraLink *ctx = malloc(sizeof(CameraLink));
35
memset(ctx, 0, sizeof(CameraLink));
39
int cl_init(CameraLink *ctx) {
43
void cl_free(CameraLink *ctx) {
49
void cl_destroy(CameraLink *ctx) {
54
int cl_set_max_resolution(CameraLink *ctx, int width, int height) {
56
g_assert(!ctx->running);
58
ctx->max_width = width;
59
ctx->max_height = height;
62
int cl_set_resolution(CameraLink *ctx, int width, int height) {
66
g_assert(ctx->running);
67
g_assert(width <= ctx->max_width);
68
g_assert(height <= ctx->max_height);
70
err = Fg_setParameter(ctx->fg, FG_WIDTH, &width, CL_PORT);
71
ds_exception_reterr(err == 0, -1, "Fg_setParameter(width = %i) failed: %s", width, Fg_getLastErrorDescription(ctx->fg));
73
err = Fg_setParameter(ctx->fg, FG_HEIGHT, &height, CL_PORT);
74
ds_exception_reterr(err == 0, -1, "Fg_setParameter(height = %i) failed: %s", height, Fg_getLastErrorDescription(ctx->fg));
82
int cl_open(CameraLink *ctx, int width, int height, int flags) {
91
if (!ctx->max_width) ctx->max_width = width;
92
if (!ctx->max_height) ctx->max_height = height;
94
if (ctx->fg) cl_close(ctx);
96
ctx->fg = Fg_Init(DLLNAME, CL_BOARD);
97
ds_exception_reterr(ctx->fg != NULL, -1, "Error in Fg_Init: %s", Fg_getLastErrorDescription(NULL));
99
buf_size = ctx->max_width * ctx->max_height * CL_SAMPLES_PER_PIXEL * CL_BYTES_PER_SAMPLE * CL_BUFFERS;
101
buf = Fg_AllocMem(ctx->fg, buf_size, CL_BUFFERS, CL_PORT);
102
ds_exception_reterr(buf != NULL, -1, "Error in Fg_AllocMem (%i bytes): %s", buf_size, Fg_getLastErrorDescription(NULL));
104
err = Fg_setParameter(ctx->fg, FG_WIDTH, &width, CL_PORT);
105
ds_exception_reterr(err == 0, -1, "Fg_setParameter(width = %i) failed: %s", width, Fg_getLastErrorDescription(ctx->fg));
107
err = Fg_setParameter(ctx->fg, FG_HEIGHT, &height, CL_PORT);
108
ds_exception_reterr(err == 0, -1, "Fg_setParameter(height = %i) failed: %s", height, Fg_getLastErrorDescription(ctx->fg));
111
struct FgApcControl ctrl;
114
ctrl.data = (struct fg_apc_data*)ctx;
115
ctrl.func = &cl_new_frame_callback;
116
ctrl.flags = FG_APC_DEFAULTS;
119
err = Fg_registerApcHandler(ctx->fg, CL_PORT, &ctrl, FG_APC_CONTROL_BASIC);
120
ds_exception_reterr(err == 0, -1, "Fg_registerApcHandler() failed: %s", Fg_getLastErrorDescription(ctx->fg));
123
if (flags&CL_FLAGS_SECURE) {
124
err = Fg_AcquireEx(ctx->fg, CL_PORT, GRAB_INFINITE, ACQ_BLOCK, buf);
126
err = Fg_Acquire(ctx->fg, CL_PORT, GRAB_INFINITE);
129
ds_exception_reterr(err == 0, -1, "Fg_Acquire() failed: %s", Fg_getLastErrorDescription(ctx->fg));
133
ctx->height = height;
138
int cl_close(CameraLink *ctx) {
142
Fg_stopAcquire(ctx->fg, CL_PORT);
143
if (ctx->cb) Fg_registerApcHandler(ctx->fg, CL_PORT, NULL, FG_APC_CONTROL_BASIC);
144
Fg_FreeMem(ctx->fg, CL_PORT);
145
Fg_FreeGrabber(ctx->fg);
153
int cl_register_frame_callback(CameraLink *ctx, TCameraFrameCallback callback, void *data) {
155
gl_assert(!ctx->running);
164
int cl_run_frames_continuous(CameraLink *ctx, int *run_flag, TCameraFrameCallback callback, void *cbctx) {
165
frameindex_t cur_frame = 0, last_frame = 1, new_frame;
171
g_assert(ctx->running);
174
new_frame = Fg_getLastPicNumberBlocking(ctx->fg, cur_frame + 1, CL_PORT, CL_TIMEOUT);
177
/* ds_error("Fg_getLastPicNumberBlocking() failed: %s", Fg_getLastErrorDescription(ctx->fg));
180
last_frame = new_frame;
183
if ((cur_frame + CL_BUFFERS - CL_SECURITY_BUFFERS) < last_frame) {
184
cur_frame = last_frame - CL_BUFFERS + CL_SECURITY_BUFFERS;
187
data = Fg_getImagePtr(ctx->fg, ++cur_frame, CL_PORT);
188
if (data) callback(cbctx, cur_frame, data);
197
//int cl_get_next_frame
205
int cl_advance_next_frame(CameraLink *ctx) {
208
ds_exception_reterr(ctx->running, -1, "CameraLink context is not initialized");
210
if (ctx->current_frame == ctx->last_frame) {
211
res = Fg_getLastPicNumberBlocking(ctx->fg, ctx->last_frame + 1, CL_PORT, CL_TIMEOUT);
213
ds_error("Fg_getLastPicNumberBlocking() failed: %s", Fg_getLastErrorDescription(ctx->fg));
217
ctx->last_frame = res;
223
int cl_get_frame(CameraLink *ctx) {
224
ds_exception_reterr(ctx->running, -1, "CameraLink context is not initialized");
226
Fg_getImagePtr(ctx->fg, ctx->current_frame++, CL_PORT);