summaryrefslogtreecommitdiff
path: root/src/device.h
blob: 7028ebb56e813665c99ff22c3dcd78b6a4f6fda8 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#ifndef device_h
#define device_h
/* Camicro - Microscope camera viewer.
 *
 * Device interface.
 *
 * Copyright (C) 2019 Nicolas Schodet
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Contact :
 *        Web: http://ni.fr.eu.org/
 *      Email: <nico at ni.fr.eu.org>
 */
#include <libusb.h>
#include <stdint.h>
#include <glib.h>

#include "image.h"

/* Error domain for device related errors. */
#define DEVICE_ERROR device_error_quark()

struct device;

/* USB poll function. If the device is handled by this driver, it should
 * return a dynamically allocated string naming this device. */
typedef char *(*device_driver_usb_poll_f)(
	struct libusb_device_descriptor *desc);

/* USB open function. The device driver previously declared that it handles
 * this device. */
typedef struct device *(*device_driver_usb_open_f)(
	libusb_context *usb, libusb_device *usb_device, GError **error);

/* Information on a device driver. */
struct device_driver {
    /* Driver name. */
    const char *name;
    /* Poll this driver for support for an USB device. */
    device_driver_usb_poll_f usb_poll;
    /* Open an USB device. */
    device_driver_usb_open_f usb_open;
};

/* Information on a supported resolution. */
struct device_info_resolution {
    /* Image width. */
    int width;
    /* Image height. */
    int height;
};

/* Informations on an open device. */
struct device_info {
    /* Exposure minimum and maximum values. */
    double exposure_min_ms, exposure_max_ms;
    /* Exposure default value. */
    double exposure_default_ms;
    /* Exposure increment between two settable values. */
    double exposure_step_ms;
    /* Gain minimum and maximum values. */
    double gain_min, gain_max;
    /* Gain default value. */
    double gain_default;
    /* Gain increment between two settable values. */
    double gain_step;
    /* Number of supported resolutions. */
    int resolutions;
    /* Table of supported resolutions. */
    const struct device_info_resolution *resolution;
};

/* Retrieve informations. */
typedef const struct device_info *(*device_get_info_f)(
	struct device *device);

/* Set new exposure value. */
typedef void (*device_set_exposure_f)(
	struct device *device, double exposure_ms);

/* Set new gain value. */
typedef void (*device_set_gain_f)(
	struct device *device, double gain);

/* Set resolution, must be called before first image read.  If stride is zero,
 * let the device decide. */
typedef void (*device_set_resolution_f)(
	struct device *device, int width, int height, int stride);

/* Start streaming. */
typedef bool (*device_start_f)(
	struct device *device, GError **error);

/* Return an image if one is available, else, return NULL. */
typedef struct image *(*device_read_f)(
	struct device *device, GError **error);

/* Stop streaming. */
typedef bool (*device_stop_f)(
	struct device *device, GError **error);

/* Close device. */
typedef bool (*device_close_f)(
	struct device *device, GError **error);

/* Open device. */
struct device {
    /* Retrieve informations. */
    device_get_info_f get_info;
    /* Change exposure. */
    device_set_exposure_f set_exposure;
    /* Change gain. */
    device_set_gain_f set_gain;
    /* Change resolution. */
    device_set_resolution_f set_resolution;
    /* Start streaming. */
    device_start_f start;
    /* Read an image. */
    device_read_f read;
    /* Stop streaming. */
    device_stop_f stop;
    /* Close device. */
    device_close_f close;
};

/* Error codes. */
enum DeviceError {
    /* Error listing devices. */
    DEVICE_ERROR_LIST,
    /* Generic USB error. */
    DEVICE_ERROR_USB,
};

/* Return quark of error domain for device related errors. */
GQuark
device_error_quark(void);

/* Find and open device. */
struct device *
device_open(libusb_context *usb, GError **error);

/* Retrieve informations. */
const struct device_info *
device_get_info(struct device *device);

/* Set new exposure value. */
void
device_set_exposure(struct device *device, double exposure_ms);

/* Set new gain value. */
void
device_set_gain(struct device *device, double gain);

/* Set resolution. */
void
device_set_resolution(struct device *device, int width, int height,
	int stride);

/* Start streaming. */
bool
device_start(struct device *device, GError **error);

/* Read an image. */
struct image *
device_read(struct device *device, GError **error);

/* Stop streaming. */
bool
device_stop(struct device *device, GError **error);

/* Close device. */
bool
device_close(struct device *device, GError **error);

#endif /* device_h */