/alps/pcitool

To get this branch, use:
bzr branch http://suren.me/webbzr/alps/pcitool

« back to all changes in this revision

Viewing changes to ipecamera/events.c

  • Committer: Suren A. Chilingaryan
  • Date: 2015-04-20 20:01:04 UTC
  • Revision ID: csa@suren.me-20150420200104-b5xny65io8lvoz3w
Big redign of model structures

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#define _BSD_SOURCE
2
 
#define _GNU_SOURCE
3
 
 
4
 
#include <stdio.h>
5
 
#include <stdlib.h>
6
 
#include <unistd.h>
7
 
#include <string.h>
8
 
#include <sys/time.h>
9
 
#include <pthread.h>
10
 
#include <assert.h>
11
 
 
12
 
#include <ufodecode.h>
13
 
 
14
 
#include "../tools.h"
15
 
#include "../error.h"
16
 
 
17
 
#include "pcilib.h"
18
 
#include "public.h"
19
 
#include "private.h"
20
 
#include "events.h"
21
 
 
22
 
int ipecamera_stream(pcilib_context_t *vctx, pcilib_event_callback_t callback, void *user) {
23
 
    int run_flag = 1;
24
 
    int res, err = 0;
25
 
    int do_stop = 0;
26
 
    
27
 
    ipecamera_event_info_t info;
28
 
    ipecamera_t *ctx = (ipecamera_t*)vctx;
29
 
 
30
 
    if (!ctx) {
31
 
        pcilib_error("IPECamera imaging is not initialized");
32
 
        return PCILIB_ERROR_NOTINITIALIZED;
33
 
    }
34
 
 
35
 
    ctx->streaming = 1;
36
 
    ctx->run_streamer = 1;
37
 
 
38
 
    if (!ctx->started) {
39
 
        err = ipecamera_start(vctx, PCILIB_EVENTS_ALL, PCILIB_EVENT_FLAGS_DEFAULT);
40
 
        if (err) {
41
 
            ctx->streaming = 0;
42
 
            return err;
43
 
        }
44
 
        
45
 
        do_stop = 1;
46
 
    }
47
 
    
48
 
    if (ctx->parse_data) {
49
 
            // This loop iterates while the generation
50
 
        while ((run_flag)&&((ctx->run_streamer)||(ctx->reported_id != ctx->event_id))) {
51
 
#ifdef IPECAMERA_ANNOUNCE_READY
52
 
            while (((!ctx->preproc)&&(ctx->reported_id != ctx->event_id))||((ctx->preproc)&&(ctx->reported_id != ctx->preproc_id))) {
53
 
#else /* IPECAMERA_ANNOUNCE_READY */
54
 
            while (ctx->reported_id != ctx->event_id) {
55
 
#endif /* IPECAMERA_ANNOUNCE_READY */
56
 
                if ((ctx->event_id - ctx->reported_id) > (ctx->buffer_size - IPECAMERA_RESERVE_BUFFERS)) ctx->reported_id = ctx->event_id - (ctx->buffer_size - 1 - IPECAMERA_RESERVE_BUFFERS);
57
 
                else ++ctx->reported_id;
58
 
 
59
 
                memcpy(&info, ctx->frame + ((ctx->reported_id-1)%ctx->buffer_size), sizeof(ipecamera_event_info_t));
60
 
 
61
 
                if ((ctx->event_id - ctx->reported_id) < ctx->buffer_size) {
62
 
                    res = callback(ctx->reported_id, (pcilib_event_info_t*)&info, user);
63
 
                    if (res <= 0) {
64
 
                        if (res < 0) err = -res;
65
 
                        run_flag = 0;
66
 
                        break;
67
 
                    }
68
 
                }
69
 
            }
70
 
            usleep(IPECAMERA_NOFRAME_SLEEP);
71
 
        }
72
 
    } else {
73
 
        while ((run_flag)&&(ctx->run_streamer)) {
74
 
            usleep(IPECAMERA_NOFRAME_SLEEP);
75
 
        }
76
 
    }
77
 
 
78
 
    ctx->streaming = 0;
79
 
 
80
 
    if (do_stop) {
81
 
        ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
82
 
    }
83
 
    
84
 
 
85
 
    return err;
86
 
}
87
 
 
88
 
int ipecamera_next_event(pcilib_context_t *vctx, pcilib_timeout_t timeout, pcilib_event_id_t *evid, size_t info_size, pcilib_event_info_t *info) {
89
 
    struct timeval tv;
90
 
    ipecamera_t *ctx = (ipecamera_t*)vctx;
91
 
 
92
 
    if (!ctx) {
93
 
        pcilib_error("IPECamera imaging is not initialized");
94
 
        return PCILIB_ERROR_NOTINITIALIZED;
95
 
    }
96
 
 
97
 
    if (!ctx->started) {
98
 
        pcilib_error("IPECamera is not in grabbing mode");
99
 
        return PCILIB_ERROR_INVALID_REQUEST;
100
 
    }
101
 
    
102
 
    if (!ctx->parse_data) {
103
 
        pcilib_error("RAWData only mode is requested");
104
 
        return PCILIB_ERROR_INVALID_REQUEST;
105
 
    }
106
 
 
107
 
#ifdef IPECAMERA_ANNOUNCE_READY
108
 
    if (((!ctx->preproc)&&(ctx->reported_id == ctx->event_id))||((ctx->preproc)&&(ctx->reported_id == ctx->preproc_id))) {
109
 
#else /* IPECAMERA_ANNOUNCE_READY */
110
 
    if (ctx->reported_id == ctx->event_id) {
111
 
#endif /* IPECAMERA_ANNOUNCE_READY */
112
 
 
113
 
        if (timeout) {
114
 
            if (timeout == PCILIB_TIMEOUT_INFINITE) {
115
 
#ifdef IPECAMERA_ANNOUNCE_READY
116
 
                while ((((!ctx->preproc)&&(ctx->reported_id == ctx->event_id))||((ctx->preproc)&&(ctx->reported_id == ctx->preproc_id)))) {
117
 
#else /* IPECAMERA_ANNOUNCE_READY */
118
 
                while ((ctx->reported_id == ctx->event_id)) {
119
 
#endif /* IPECAMERA_ANNOUNCE_READY */
120
 
                usleep(IPECAMERA_NOFRAME_SLEEP);
121
 
                }
122
 
            } else {        
123
 
                pcilib_calc_deadline(&tv, timeout);
124
 
 
125
 
#ifdef IPECAMERA_ANNOUNCE_READY
126
 
                while ((ctx->started)&&(pcilib_calc_time_to_deadline(&tv) > 0)&&(((!ctx->preproc)&&(ctx->reported_id == ctx->event_id))||((ctx->preproc)&&(ctx->reported_id == ctx->preproc_id)))) {
127
 
#else /* IPECAMERA_ANNOUNCE_READY */
128
 
                while ((ctx->started)&&(pcilib_calc_time_to_deadline(&tv) > 0)&&(ctx->reported_id == ctx->event_id)) {
129
 
#endif /* IPECAMERA_ANNOUNCE_READY */
130
 
                    usleep(IPECAMERA_NOFRAME_SLEEP);
131
 
                }
132
 
            }
133
 
        }
134
 
        
135
 
        if (ctx->reported_id == ctx->event_id) {
136
 
            return PCILIB_ERROR_TIMEOUT;
137
 
        }
138
 
        
139
 
    }
140
 
 
141
 
retry:
142
 
    if ((ctx->event_id - ctx->reported_id) > (ctx->buffer_size - IPECAMERA_RESERVE_BUFFERS)) ctx->reported_id = ctx->event_id - (ctx->buffer_size - 1 - IPECAMERA_RESERVE_BUFFERS);
143
 
    else ++ctx->reported_id;
144
 
 
145
 
    if (evid) *evid = ctx->reported_id;
146
 
 
147
 
    if (info) {
148
 
        if (info_size >= sizeof(ipecamera_event_info_t))
149
 
            memcpy(info, ctx->frame + ((ctx->reported_id-1)%ctx->buffer_size), sizeof(ipecamera_event_info_t));
150
 
        else if (info_size >= sizeof(pcilib_event_info_t))
151
 
            memcpy(info, ctx->frame + ((ctx->reported_id-1)%ctx->buffer_size), sizeof(pcilib_event_info_t));
152
 
        else
153
 
            return PCILIB_ERROR_INVALID_ARGUMENT;
154
 
    }
155
 
 
156
 
    if ((ctx->event_id - ctx->reported_id) >= ctx->buffer_size) goto retry;
157
 
 
158
 
    return 0;
159
 
}
160