-
Notifications
You must be signed in to change notification settings - Fork 0
/
libqhy.c
406 lines (380 loc) · 13.3 KB
/
libqhy.c
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Copyright (c) 2016 CloudMakers, s. r. o.
// All rights reserved.
//
// You can use this software under the terms of 'INDIGO Astronomy
// open-source license' (see LICENSE.md).
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHORS 'AS IS' AND ANY EXPRESS
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <syslog.h>
#include <libusb-1.0/libusb.h>
#include <libqhy/libqhy.h>
#include <libqhy/libqhy.h>
#include "config.h"
#include "libqhy_base.h"
#include "libqhy_5ii.h"
static struct {
const int vid, pid;
const libqhy_camera_type type;
const unsigned char *firmware;
const char *name;
const bool is_guider;
} qhy_cameras[] = {
{ 0x1618, 0x0920, QHY_UNINITIALIZED, libqhy_5ii_firmware, "Uninitialized QHY5-II", false },
{ 0x1618, 0x0921, QHY_5II, NULL, "QHY5-II", true },
{ 0, 0, 0, NULL, NULL, false }
};
bool libqhy_debug_level = true;
bool libqhy_use_syslog = false;
const char *libqhy_version = LIBQHY_VERSION;
const char *libqhy_os = LIBQHY_OS;
const char *libqhy_arch = LIBQHY_ARCH;
void qhy_log(const char *format, ...) {
if (!libqhy_debug_level)
return;
static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;
static char buffer[1024];
va_list argList;
va_start(argList, format);
pthread_mutex_lock(&log_mutex);
vsnprintf(buffer, sizeof(buffer), format, argList);
char *line = buffer;
if (libqhy_use_syslog) {
while (line) {
char *eol = strchr(line, '\n');
if (eol)
*eol = 0;
if (eol > line)
syslog (LOG_NOTICE, "%s", buffer);
syslog (LOG_NOTICE, "%s\n", line);
if (eol)
line = eol + 1;
else
line = NULL;
}
} else {
char timestamp[16];
struct timeval tmnow;
gettimeofday(&tmnow, NULL);
strftime (timestamp, 9, "%H:%M:%S", localtime(&tmnow.tv_sec));
#ifdef __APPLE__
snprintf(timestamp + 8, sizeof(timestamp) - 8, ".%06d", tmnow.tv_usec);
#else
snprintf(timestamp + 8, sizeof(timestamp) - 8, ".%06ld", tmnow.tv_usec);
#endif
while (line) {
char *eol = strchr(line, '\n');
if (eol)
*eol = 0;
if (*line)
fprintf(stderr, "%s libqhy: %s\n", timestamp, line);
if (eol)
line = eol + 1;
else
line = NULL;
}
}
pthread_mutex_unlock(&log_mutex);
}
//static bool qhy_control_write(libusb_device_handle *handle, unsigned req, unsigned char* data, unsigned length) {
// int rc = libusb_control_transfer(handle, QHYCCD_REQUEST_WRITE, req, 0, 0, data, length, 0);
// QHY_DEBUG(qhy_log("libusb_control_transfer -> %s\n", rc < 0 ? libusb_error_name(rc) : "OK" ));
// return rc >= 0;
//}
//
//static bool qhy_control_read(libusb_device_handle *handle, unsigned req, unsigned char* data, unsigned length) {
// int rc = libusb_control_transfer(handle, QHYCCD_REQUEST_READ, req, 0, 0, data, length, 0);
// QHY_DEBUG(qhy_log("libusb_control_transfer -> %s\n", rc < 0 ? libusb_error_name(rc) : "OK" ));
// return rc >= 0;
//}
//
//static bool qhy_write(libusb_device_handle *handle, unsigned char *data, unsigned length) {
// int length_transfered;
// int rc = libusb_bulk_transfer(handle, QHYCCD_INTERRUPT_WRITE_ENDPOINT, data, length, &length_transfered, 0);
// QHY_DEBUG(qhy_log("libusb_bulk_transfer -> %s\n", rc < 0 ? libusb_error_name(rc) : "OK" ));
// return rc >= 0;
//}
//
//static bool qhy_read(libusb_device_handle *handle, unsigned char *data, unsigned length) {
// int length_transfered;
// int rc = libusb_bulk_transfer(handle, QHYCCD_INTERRUPT_READ_ENDPOINT, data, length, &length_transfered, 0);
// QHY_DEBUG(qhy_log("libusb_bulk_transfer -> %s\n", rc < 0 ? libusb_error_name(rc) : "OK" ));
// return rc >= 0;
//}
int libqhy_i2c_write(libusb_device_handle *handle, unsigned addr,unsigned short value) {
unsigned char data[2];
data[0] = (value & 0xff00) >> 8;
data[1] = value & 0x00FF;
int rc = libusb_control_transfer(handle, REQUEST_WRITE, 0xbb, 0, addr, data, 2, 0);
QHY_DEBUG(qhy_log("libusb_control_transfer -> %s", rc < 0 ? libusb_error_name(rc) : "OK"));
return rc;
}
int libqhy_i2c_read(libusb_device_handle *handle, unsigned addr, unsigned short *value) {
unsigned char data[2];
int rc = libusb_control_transfer(handle, REQUEST_READ, 0xb7, 0, addr, data, 2, 0);
QHY_DEBUG(qhy_log("libusb_control_transfer -> %s", rc < 0 ? libusb_error_name(rc) : "OK"));
if (rc >= 0)
*value = data[0] * 256 + data[1];
return rc;
}
typedef struct {
libusb_device *device;
const unsigned char *data;
} firmware_context;
static void *qhy_firmware(firmware_context *context) {
unsigned char stop = 1;
unsigned char reset = 0;
libusb_device_handle *handle;
const unsigned char *data = context->data;
int rc = libusb_open(context->device, &handle);
QHY_DEBUG(qhy_log("libusb_open -> %s", rc < 0 ? libusb_error_name(rc) : "OK"));
if (libusb_has_capability(LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER)) {
if (libusb_kernel_driver_active(handle, 0) == 1) {
rc = libusb_detach_kernel_driver(handle, 0);
QHY_DEBUG(qhy_log("libusb_detach_kernel_driver -> %s", rc < 0 ? libusb_error_name(rc) : "OK"));
}
}
if (rc >= 0) {
rc = libusb_claim_interface(handle, 0);
QHY_DEBUG(qhy_log("libusb_claim_interface -> %s", rc < 0 ? libusb_error_name(rc) : "OK"));
}
if (rc >= 0) {
rc = rc < 0 ? rc : libusb_control_transfer(handle, REQUEST_WRITE, 0xA0, 0xe600, 0, &stop, 1, 3000);
QHY_DEBUG(qhy_log("libusb_control_transfer -> %s", rc < 0 ? libusb_error_name(rc) : "OK"));
while (true) {
unsigned char byte_count = data[0];
if (byte_count == 0)
break;
unsigned short address = ((data[1] & 0xFF) << 8) | (data[2] & 0xFF);
rc = libusb_control_transfer(handle, REQUEST_WRITE, 0xA0, address, 0, (unsigned char *)(data+3), byte_count, 3000);
if (rc != byte_count) {
QHY_DEBUG(qhy_log("libusb_control_transfer -> %s", rc < 0 ? libusb_error_name(rc) : "OK"));
break;
}
data += byte_count + 3;
}
rc = rc < 0 ? rc : libusb_control_transfer(handle, REQUEST_WRITE, 0xA0, 0xe600, 0, &reset, 1, 3000);
QHY_DEBUG(qhy_log("libusb_control_transfer -> %s", rc < 0 ? libusb_error_name(rc) : "OK"));
rc = libusb_release_interface(handle, 0);
QHY_DEBUG(qhy_log("libusb_release_interface -> %s", rc < 0 ? libusb_error_name(rc) : "OK"));
libusb_close(handle);
QHY_DEBUG(qhy_log("libusb_close"));
}
libusb_unref_device(context->device);
free(context);
return NULL;
}
bool libqhy_camera(libusb_device *device, libqhy_camera_type *type, const char **name, bool *is_guider) {
struct libusb_device_descriptor descriptor;
int rc = libusb_get_device_descriptor(device, &descriptor);
QHY_DEBUG(qhy_log("libusb_get_device_descriptor -> %s", rc < 0 ? libusb_error_name(rc) : "OK"));
QHY_DEBUG(qhy_log("*** vid: 0x%04x", descriptor.idVendor));
QHY_DEBUG(qhy_log("*** pid: 0x%04x", descriptor.idProduct));
*type = 0;
for (int i = 0; qhy_cameras[i].vid != 0; i++) {
if (descriptor.idVendor == qhy_cameras[i].vid && qhy_cameras[i].pid == descriptor.idProduct) {
if (qhy_cameras[i].type == QHY_UNINITIALIZED) {
QHY_DEBUG(qhy_log("*** type: %s", qhy_cameras[i].name));
firmware_context *context = malloc(sizeof(firmware_context));
libusb_ref_device(context->device = device);
context->data = qhy_cameras[i].firmware;
pthread_t async_thread;
pthread_create(&async_thread, NULL, (void *)(void *)qhy_firmware, context);
return false;
} else {
if (type)
*type = qhy_cameras[i].type;
if (name)
*name = qhy_cameras[i].name;
if (is_guider)
*is_guider = qhy_cameras[i].is_guider;
QHY_DEBUG(qhy_log("*** type: %s", qhy_cameras[i].name));
return true;
}
}
}
return false;
}
bool libqhy_open(libusb_device *device, libqhy_device_context **device_context) {
libqhy_camera_type type;
libqhy_camera(device, &type, NULL, NULL);
libusb_device_handle *handle = NULL;
int rc = libusb_open(device, &handle);
QHY_DEBUG(qhy_log("libusb_open -> %s", rc < 0 ? libusb_error_name(rc) : "OK"));
if (rc >= 0) {
if (libusb_has_capability(LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER)) {
if (libusb_kernel_driver_active(handle, 0) == 1) {
rc = libusb_detach_kernel_driver(handle, 0);
QHY_DEBUG(qhy_log("libusb_detach_kernel_driver -> %s", rc < 0 ? libusb_error_name(rc) : "OK"));
}
}
}
if (rc >= 0) {
rc = libusb_set_configuration(handle, 1);
QHY_DEBUG(qhy_log("libusb_set_configuration -> %s", rc < 0 ? libusb_error_name(rc) : "OK"));
}
if (rc >= 0) {
rc = libusb_claim_interface(handle, 0);
QHY_DEBUG(qhy_log("libusb_claim_interface -> %s", rc < 0 ? libusb_error_name(rc) : "OK"));
}
if (rc >= 0) {
libqhy_device_context *context = malloc(sizeof(libqhy_device_context));
memset(context, 0, sizeof(libqhy_device_context));
context->handle = handle;
context->type = type;
pthread_mutex_init(&context->lock, NULL);
bool initialized = false;
switch (type) {
case QHY_5II:
initialized = libqhy_5ii_init(context);
default:
break;
}
if (initialized) {
QHY_DEBUG(qhy_log("*** pixel count: %d x %d", context->width, context->height));
QHY_DEBUG(qhy_log("*** pixel size: %3.2f x %3.2f", context->pixel_width, context->pixel_height));
QHY_DEBUG(qhy_log("*** max bin: %d x %d", context->max_bin_hor, context->max_bin_vert));
QHY_DEBUG(qhy_log("*** has guider port: %s", context->has_guider_port ? "true" : "false"));
QHY_DEBUG(qhy_log("*** has cooler: %s", context->has_cooler ? "true" : "false"));
QHY_DEBUG(qhy_log("*** is colour: %s", context->is_colour ? "true" : "false"));
pthread_mutex_init(&context->usb_mutex, NULL);
*device_context = context;
QHY_DEBUG(qhy_log("QHY open -> OK"));
} else {
free (context);
}
}
return rc >= 0;
}
bool libqhy_get_handle(libqhy_device_context *context, libusb_device_handle **handle) {
assert(handle != NULL);
*handle = context->handle;
return context->handle != NULL;
}
bool libqhy_get_resolution(libqhy_device_context *context, unsigned *width, unsigned *height, unsigned *bits_per_pixel) {
assert(width != NULL);
assert(height != NULL);
assert(bits_per_pixel != NULL);
*width = context->frame_width;
*height = context->frame_height;
*bits_per_pixel = context->frame_bits_per_pixel;
return context->handle != NULL;
}
bool libqhy_get_pixel_size(libqhy_device_context *context, double *width, double *height) {
assert(width != NULL);
assert(height != NULL);
*width = context->pixel_width;
*height = context->pixel_height;
return context->handle != NULL;
}
bool libqhy_get_temperature(libqhy_device_context *context, double *temperature) {
switch (context->type) {
case QHY_5II:
case QHY_5TII:
case QHY_5PII:
case QHY_5LII:
case QHY_5RII:
case QHY_5HII:
return libqhy_5ii_get_temperature(context, temperature);
default:
break;
}
QHY_DEBUG(qhy_log("QHY check temperature -> Failed"));
return false;
}
bool libqhy_set_exposure_time(libqhy_device_context *context, double exposure) {
switch (context->type) {
case QHY_5II:
case QHY_5TII:
case QHY_5PII:
case QHY_5LII:
case QHY_5RII:
case QHY_5HII:
return libqhy_5ii_set_exposure_time(context, exposure);
default:
break;
}
QHY_DEBUG(qhy_log("QHY start exposure -> Failed"));
return false;
}
bool libqhy_set_gain(libqhy_device_context *context, double gain) {
switch (context->type) {
case QHY_5II:
case QHY_5TII:
case QHY_5PII:
case QHY_5LII:
case QHY_5RII:
case QHY_5HII:
return libqhy_5ii_set_gain(context, gain);
default:
break;
}
QHY_DEBUG(qhy_log("QHY start exposure -> Failed"));
return false;
}
bool libqhy_start(libqhy_device_context *context) {
switch (context->type) {
case QHY_5II:
case QHY_5TII:
case QHY_5PII:
case QHY_5LII:
case QHY_5RII:
case QHY_5HII:
return libqhy_5ii_start(context);
default:
break;
}
QHY_DEBUG(qhy_log("QHY start exposure -> Failed"));
return false;
}
bool libqhy_read_pixels(libqhy_device_context *context, unsigned short *image) {
switch (context->type) {
case QHY_5II:
case QHY_5TII:
case QHY_5PII:
case QHY_5LII:
case QHY_5RII:
case QHY_5HII:
return libqhy_5ii_read_pixels(context, image);
default:
break;
}
QHY_DEBUG(qhy_log("QHY read pixels -> Failed"));
return false;
}
bool libqhy_stop(libqhy_device_context *context) {
switch (context->type) {
case QHY_5II:
case QHY_5TII:
case QHY_5PII:
case QHY_5LII:
case QHY_5RII:
case QHY_5HII:
return libqhy_5ii_stop(context);
default:
break;
}
QHY_DEBUG(qhy_log("QHY start exposure -> Failed"));
return false;
}
void libqhy_close(libqhy_device_context *device_context) {
libusb_close(device_context->handle);
free(device_context);
QHY_DEBUG(qhy_log("QHY close"));
}