summaryrefslogtreecommitdiffstats
path: root/src/cameras/uca_pco.c
blob: 3effde4f931cbc591d18b755d9cad1177490caf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170

#include <stdlib.h>
#include <string.h>
#include <clser.h>
#include <fgrab_struct.h>
#include <fgrab_prototyp.h>
#include <libpco/libpco.h>
#include "uca.h"
#include "uca_pco.h"

struct pco_cam_t {
    struct pco_edge_t *pco;
    Fg_Struct *fg;
};

#define GET_PCO(uca) (((struct pco_cam_t *)(uca->user))->pco)
#define GET_FG(uca) (((struct pco_cam_t *)(uca->user))->fg)


static uint32_t uca_pco_set_bitdepth(struct uca_t *uca, uint8_t *bitdepth)
{
    /* TODO: it's not possible via CameraLink so do it via frame grabber */
    return 0;
}

static uint32_t uca_pco_set_exposure(struct uca_t *uca, uint32_t *exposure)
{
    uint32_t e, d;
    if (pco_get_delay_exposure(GET_PCO(uca), &d, &e) != PCO_NOERROR)
        return UCA_ERR_PROP_GENERAL;
    if (pco_set_delay_exposure(GET_PCO(uca), d, *exposure) != PCO_NOERROR)
        return UCA_ERR_PROP_GENERAL;
    return UCA_NO_ERROR;
}

static uint32_t uca_pco_set_delay(struct uca_t *uca, uint32_t *delay)
{
    uint32_t e, d;
    if (pco_get_delay_exposure(GET_PCO(uca), &d, &e) != PCO_NOERROR)
        return UCA_ERR_PROP_GENERAL;
    if (pco_set_delay_exposure(GET_PCO(uca), *delay, e) != PCO_NOERROR)
        return UCA_ERR_PROP_GENERAL;
    return UCA_NO_ERROR;
}

static uint32_t uca_pco_acquire_image(struct uca_t *uca, void *buffer)
{
    return UCA_NO_ERROR;
}

static uint32_t uca_pco_destroy(struct uca_t *uca)
{
    Fg_FreeGrabber(GET_FG(uca));
    pco_destroy(GET_PCO(uca));
    free(uca->user);
    return UCA_NO_ERROR;
}

static uint32_t uca_pco_set_property(struct uca_t *uca, int32_t property, void *data)
{
    switch (property) {
        case UCA_PROP_WIDTH:
            Fg_setParameter(GET_FG(uca), FG_WIDTH, (uint32_t *) data, PORT_A);
            break;

        case UCA_PROP_HEIGHT:
            Fg_setParameter(GET_FG(uca), FG_HEIGHT, (uint32_t *) data, PORT_A);
            break;

        case UCA_PROP_EXPOSURE:
            return uca_pco_set_exposure(uca, (uint32_t *) data);

        case UCA_PROP_DELAY:
            return uca_pco_set_delay(uca, (uint32_t *) data);

        default:
            return UCA_ERR_PROP_INVALID;
    }
    return UCA_NO_ERROR;
}

static uint32_t uca_pco_get_property(struct uca_t *uca, int32_t property, void *data)
{
    struct pco_edge_t *pco = GET_PCO(uca);

    switch (property) {
        /* FIXME: how to ensure, that data is large enough? */
        case UCA_PROP_NAME: 
            {
                SC2_Camera_Name_Response name;
                if (pco_read_property(pco, GET_CAMERA_NAME, &name, sizeof(name)) == PCO_NOERROR)
                    strcpy((char *) data, name.szName);
            }
            break;

        case UCA_PROP_MAX_WIDTH:
            {
                uint32_t w, h;
                pco_get_actual_size(pco, &w, &h);
                *((uint32_t *) data) = w;
            }

        case UCA_PROP_MAX_HEIGHT:
            {
                int w, h;
                pco_get_actual_size(pco, &w, &h);
                *((uint32_t *) data) = h;
            }

        default:
            return UCA_ERR_PROP_INVALID;
    }
    return UCA_NO_ERROR;
}

uint32_t uca_pco_init(struct uca_t *uca)
{
    uca->user = (struct pco_cam_t *) malloc(sizeof(struct pco_cam_t));

    struct pco_cam_t *pco_cam = uca->user;
    struct pco_edge_t *pco = pco_cam->pco = pco_init();

    if ((pco->serial_ref == NULL) || !pco_active(pco)) {
        pco_destroy(pco);
        return UCA_ERR_INIT_NOT_FOUND;
    }

    Fg_Struct *fg = pco_cam->fg = Fg_Init("libFullAreaGray8.so", 0);

    pco_scan_and_set_baud_rate(pco);

    /* Camera found, set function pointers... */
    uca->cam_destroy = &uca_pco_destroy;
    uca->cam_set_property = &uca_pco_set_property;
    uca->cam_get_property = &uca_pco_get_property;
    uca->cam_acquire_image = &uca_pco_acquire_image;

    /* Prepare camera for recording */
    pco_set_rec_state(pco, 0);
    pco_set_timestamp_mode(pco, 2);
    pco_set_timebase(pco, 1, 1); 
    pco_arm_camera(pco);

    if (pco->transfer.DataFormat != (SCCMOS_FORMAT_TOP_CENTER_BOTTOM_CENTER | PCO_CL_DATAFORMAT_5x16))
        pco->transfer.DataFormat = SCCMOS_FORMAT_TOP_CENTER_BOTTOM_CENTER | PCO_CL_DATAFORMAT_5x16;

    /* Prepare frame grabber for recording */
    int val = FG_CL_8BIT_FULL_10;
    Fg_setParameter(fg, FG_CAMERA_LINK_CAMTYP, &val, PORT_A);

    val = FG_GRAY;
    Fg_setParameter(fg, FG_FORMAT, &val, PORT_A);

    val = FREE_RUN;
    Fg_setParameter(fg, FG_TRIGGERMODE, &val, PORT_A);

    int width, height;
    pco_get_actual_size(pco, &width, &height);

    /* Yes, we really have to take an image twice as large because we set the
     * CameraLink interface to 8-bit 10 Taps, but are actually using 5x16 bits. */
    width *= 2;
    height *= 2;
    Fg_setParameter(fg, FG_WIDTH, &width, PORT_A);
    Fg_setParameter(fg, FG_HEIGHT, &height, PORT_A);

    pco_set_rec_state(pco, 1);

    return 0;
}