/camera/imageviewer

To get this branch, use:
bzr branch http://darksoft.org/webbzr/camera/imageviewer

« back to all changes in this revision

Viewing changes to cameralink/cl.c

  • Committer: Suren A. Chilingaryan
  • Date: 2011-02-13 01:34:55 UTC
  • Revision ID: csa@dside.dyndns.org-20110213013455-7999955h7v4uf9m8
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <stdlib.h>
 
3
#include <string.h>
 
4
 
 
5
#define fg_apc_data TCameraLink
 
6
 
 
7
#include <fgrab_struct.h>
 
8
#include <fgrab_prototyp.h>
 
9
#include <fgrab_define.h>
 
10
 
 
11
#include "ds_log.h"
 
12
#include "cl.h"
 
13
 
 
14
#define DLLNAME                 "libFullAreaGray8.so"
 
15
#define CL_BOARD                0
 
16
#define CL_PORT                 PORT_A                  // Full speed, PORT_AB ?
 
17
#define CL_SAMPLES_PER_PIXEL    1
 
18
#define CL_BYTES_PER_SAMPLE     1
 
19
#define CL_BUFFERS              16
 
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
 
22
 
 
23
 
 
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);
 
27
    return 0;
 
28
}
 
29
 
 
30
 
 
31
 
 
32
CameraLink *cl_create() {
 
33
    CameraLink *ctx = malloc(sizeof(CameraLink));
 
34
    if (ctx) {
 
35
        memset(ctx, 0, sizeof(CameraLink));
 
36
    }
 
37
}
 
38
 
 
39
int cl_init(CameraLink *ctx) {
 
40
    return 0;
 
41
}
 
42
 
 
43
void cl_free(CameraLink *ctx) {
 
44
    g_assert(ctx);
 
45
    
 
46
    cl_close(ctx);
 
47
}
 
48
 
 
49
void cl_destroy(CameraLink *ctx) {
 
50
    cl_free(ctx);    
 
51
    free(ctx);
 
52
}
 
53
 
 
54
int cl_set_max_resolution(CameraLink *ctx, int width, int height) {
 
55
    g_assert(ctx);
 
56
    g_assert(!ctx->running);
 
57
 
 
58
    ctx->max_width = width;
 
59
    ctx->max_height = height;
 
60
}
 
61
 
 
62
int cl_set_resolution(CameraLink *ctx, int width, int height) {
 
63
    int err;
 
64
    
 
65
    g_assert(ctx);
 
66
    g_assert(ctx->running);
 
67
    g_assert(width <= ctx->max_width);
 
68
    g_assert(height <= ctx->max_height);    
 
69
 
 
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));
 
72
 
 
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));
 
75
 
 
76
    ctx->width = width;
 
77
    ctx->height = height;
 
78
 
 
79
    return 0;
 
80
}
 
81
 
 
82
int cl_open(CameraLink *ctx, int width, int height, int flags) {
 
83
    int err;
 
84
    size_t buf_size;
 
85
    void *buf;
 
86
    
 
87
    double frame_rate;
 
88
    
 
89
    g_assert(ctx);
 
90
    
 
91
    if (!ctx->max_width) ctx->max_width = width;
 
92
    if (!ctx->max_height) ctx->max_height = height;
 
93
    
 
94
    if (ctx->fg) cl_close(ctx);
 
95
    
 
96
    ctx->fg = Fg_Init(DLLNAME, CL_BOARD);
 
97
    ds_exception_reterr(ctx->fg != NULL, -1, "Error in Fg_Init: %s", Fg_getLastErrorDescription(NULL));
 
98
    
 
99
    buf_size = ctx->max_width * ctx->max_height * CL_SAMPLES_PER_PIXEL * CL_BYTES_PER_SAMPLE * CL_BUFFERS;
 
100
    
 
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));
 
103
 
 
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));
 
106
 
 
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));
 
109
    
 
110
    if (ctx->cb) {
 
111
        struct FgApcControl ctrl;
 
112
 
 
113
        ctrl.version = 0;
 
114
        ctrl.data = (struct fg_apc_data*)ctx;
 
115
        ctrl.func = &cl_new_frame_callback;
 
116
        ctrl.flags = FG_APC_DEFAULTS;
 
117
        ctrl.timeout = 1;
 
118
    
 
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));
 
121
    }
 
122
 
 
123
    if (flags&CL_FLAGS_SECURE) {
 
124
        err = Fg_AcquireEx(ctx->fg, CL_PORT, GRAB_INFINITE, ACQ_BLOCK, buf);
 
125
    } else {
 
126
        err = Fg_Acquire(ctx->fg, CL_PORT, GRAB_INFINITE);
 
127
    }
 
128
    
 
129
    ds_exception_reterr(err == 0, -1, "Fg_Acquire() failed: %s", Fg_getLastErrorDescription(ctx->fg));
 
130
 
 
131
    ctx->running = 1;
 
132
    ctx->width = width;
 
133
    ctx->height = height;
 
134
    
 
135
    return 0;
 
136
}
 
137
 
 
138
int cl_close(CameraLink *ctx) {
 
139
    ctx->running = 0;
 
140
    
 
141
    if (ctx->fg) {
 
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);
 
146
        ctx->fg = NULL;
 
147
    }
 
148
    
 
149
    return 0;
 
150
}
 
151
 
 
152
 
 
153
int cl_register_frame_callback(CameraLink *ctx, TCameraFrameCallback callback, void *data) {
 
154
    g_assert(ctx);
 
155
    gl_assert(!ctx->running);
 
156
    
 
157
    ctx->cb = callback; 
 
158
    ctx->cbctx = data;
 
159
    
 
160
    return 0;
 
161
}
 
162
 
 
163
 
 
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;
 
166
    void *data;
 
167
 
 
168
    g_assert(ctx);
 
169
    g_assert(run_flag);
 
170
    g_assert(callback);
 
171
    g_assert(ctx->running);
 
172
 
 
173
    while (*run_flag) {
 
174
        new_frame = Fg_getLastPicNumberBlocking(ctx->fg, cur_frame + 1, CL_PORT, CL_TIMEOUT);
 
175
        if (new_frame < 0) {
 
176
            continue;
 
177
/*          ds_error("Fg_getLastPicNumberBlocking() failed: %s", Fg_getLastErrorDescription(ctx->fg));
 
178
            return -1;*/
 
179
        } else {
 
180
            last_frame = new_frame;
 
181
        }
 
182
        
 
183
        if ((cur_frame + CL_BUFFERS - CL_SECURITY_BUFFERS) < last_frame) {
 
184
            cur_frame = last_frame - CL_BUFFERS + CL_SECURITY_BUFFERS;
 
185
        }
 
186
        
 
187
        data = Fg_getImagePtr(ctx->fg, ++cur_frame, CL_PORT);
 
188
        if (data) callback(cbctx, cur_frame, data);
 
189
    }
 
190
 
 
191
    return 0;
 
192
}
 
193
 
 
194
 
 
195
 
 
196
 
 
197
//int cl_get_next_frame
 
198
 
 
199
 
 
200
 
 
201
 
 
202
 
 
203
 
 
204
/*
 
205
int cl_advance_next_frame(CameraLink *ctx) {
 
206
    frameindex_t res;
 
207
    
 
208
    ds_exception_reterr(ctx->running, -1, "CameraLink context is not initialized");
 
209
 
 
210
    if (ctx->current_frame == ctx->last_frame) {
 
211
        res = Fg_getLastPicNumberBlocking(ctx->fg, ctx->last_frame + 1, CL_PORT, CL_TIMEOUT);
 
212
        if (!res) {
 
213
            ds_error("Fg_getLastPicNumberBlocking() failed: %s", Fg_getLastErrorDescription(ctx->fg));
 
214
            return -1;
 
215
        }
 
216
        
 
217
        ctx->last_frame = res;
 
218
    }
 
219
    
 
220
    return 0;
 
221
}
 
222
 
 
223
int cl_get_frame(CameraLink *ctx) {
 
224
    ds_exception_reterr(ctx->running, -1, "CameraLink context is not initialized");
 
225
 
 
226
    Fg_getImagePtr(ctx->fg, ctx->current_frame++, CL_PORT);
 
227
}
 
228
*/