summaryrefslogtreecommitdiff
path: root/include/qhy/qhycam.h
blob: a4ea23635d1a3013afbe78371b34d5b3c9e6d065 (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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/*
 QHYCCD SDK
 
 Copyright (c) 2014 QHYCCD.
 All Rights Reserved.
 
 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.
 
 The full GNU General Public License is included in this distribution in the
 file called LICENSE.
 */

/*
 * @file qhycam.h
 * @brief QHYCAM class define
 */
#ifndef _QHYCAM_H_
#define _QHYCAM_H_

#include <math.h>
#include "qhyccdstruct.h"
#include "unlockimagequeue.h"

#ifdef WIN32
#include "CyAPI.h"
#include <process.h>
#else // Linux & Mac 
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
//#include <libusb-1.0/libusb.h>
#include <libusb.h>
#endif // WIN32

#include "stdint.h"

#define IDLE 0
#define EXPOSING 1
#define DOWNLOADING 2

#define QHYCCD_USBTYPE_NONE    (0xFF)
#define QHYCCD_USBTYPE_CYUSB   (0x00)
#define QHYCCD_USBTYPE_WINUSB  (0x01)

#define LIBUSB_PACKET_LENGTH  (4096)
#define LIBUSB_WR_TIMEOUT  (5000)

#define LIBUSB_CONTROL_TRANSFER_RD_TIMEOUT (5000)
#define LIBUSB_CONTROL_TRANSFER_WR_TIMEOUT (1000)

#define LIBUSB_BULK_TRANSFER_RD_TIMEOUT  (5000) 
#define LIBUSB_BULK_TRANSFER_WR_TIMEOUT  (1000) 

#define LIBUSB_ASYNC_BULK_TRANSFER_TIMEOUT (3600000)

#define EVENT_TIMEOUT_SEC (5)
#define EVENT_TIMEOUT_USEC (0);

#define USB_ENDPOINT  (0x81)
#define CAM_16_BITS  (16)

#ifdef WIN32

typedef void *qhyccd_device;
typedef CCyUSBDevice qhyccd_handle;

#else // Linux & Mac

typedef struct libusb_device qhyccd_device;
typedef struct libusb_device_handle qhyccd_handle;

#endif // WIN32

/**
 * @brief QHYCAM class define
 *
 * include all functions for qhycam
 */
class QHYCAM {
public:

    QHYCAM(); 
    virtual ~QHYCAM();
    
    //inline UnlockImageQueue *GetImageQueue() {
    //	return m_pImageQueue;
    //}
    
    //static void* EventThreadFunc(void *arg);
    //static void AsyImageDataCallBack(struct libusb_transfer *transfer);
    
    //int AllocFillSubmitBulkTransfers(qhyccd_handle *pCamera);        
    //int32_t InitAsyQCamLive2(qhyccd_handle *pDevHandle, int32_t x, int32_t y, int32_t depth, int32_t frameSize);
    //bool BeginAsyQCamLive2(qhyccd_handle *pDevHandle);
    //void StopAsyQCamLive2(qhyccd_handle *pDevHandle);
    //uint32_t ReadAsyQCamLiveFrame2(qhyccd_handle *pDevHandle, uint8_t *pBuffer, int32_t *pFrameFlag);   
    //void SetFlagRawExit2(bool value);
    //int GetEventCount2();
    //int IncreaseEventCount2();
    //int DecreaseEventCount2();
    //bool IsFlagRawExit2();
    
    /**
     @fn uint32_t openCamera(qhyccd_deivce *d,qhyccd_handle **h)
     @brief open the camera,open the device handle
     @param d deivce deivce
     @param h device control handle
     @return
     success return QHYCCD_SUCCESS \n
     another QHYCCD_ERROR code on other failures
     */
    uint32_t openCamera(qhyccd_device *d, qhyccd_handle **h);

    /**
     @fn void closeCamera(qhyccd_device *h)
     @brief close the camera,close the deivce handle
     @param h deivce control handle
     */
    void closeCamera(qhyccd_handle *h);

    /**
     @fn uint32_t sendForceStop(qhyccd_handle *h)
     @brief force stop exposure
     @param h device control handle
         @return 
         success return QHYCCD_SUCCESS \n
     another QHYCCD_ERROR code on other failures
     */
    uint32_t sendForceStop(qhyccd_handle *h);

    /**
     @fn uint32_t sendInterrupt(qhyccd_handle *handle,uint8_t length,uint8_t *data)
     @brief send a package to deivce using the interrupt endpoint
     @param handle device control handle
     @param length the package size(unit:byte)
     @param data package buffer
     @return
     success return QHYCCD_SUCCESS \n
     another QHYCCD_ERROR code on other failures
     */
    uint32_t sendInterrupt(qhyccd_handle *handle, uint8_t length, uint8_t *data);

    /**
     @fn uint32_t vendTXD(qhyccd_handle *dev_handle, uint8_t req, uint8_t *data, uint16_t length)
     @brief send a package to deivce using the vendor request
     @param dev_handle device control handle
     @param req request command
     @param data package buffer
     @param length the package size(unit:byte)
     @return
     success return QHYCCD_SUCCESS \n
     another QHYCCD_ERROR code on other failures
     */
    uint32_t vendTXD(qhyccd_handle *dev_handle, uint8_t req, uint8_t *data, uint16_t length);

    /**
     @fn uint32_t vendRXD(qhyccd_handle *dev_handle, uint8_t req, uint8_t *data, uint16_t length)
     @brief get a package from deivce using the vendor request
     @param dev_handle device control handle
     @param req request command
     @param data package buffer
     @param length the package size(unit:byte)
     @return
     success return QHYCCD_SUCCESS \n
     another QHYCCD_ERROR code on other failures
     */
    uint32_t vendRXD(qhyccd_handle *dev_handle, uint8_t req, uint8_t *data, uint16_t length);

    /**
     @fn iTXD(qhyccd_handle *dev_handle,uint8_t *data, int32_t length)
     @brief send a package to deivce using the bulk endpoint
     @param dev_handle device control handle
     @param data package buffer
     @param length the package size(unit:byte)
     @return
     success return QHYCCD_SUCCESS \n
     another QHYCCD_ERROR code on other failures
     */
    uint32_t iTXD(qhyccd_handle *dev_handle, uint8_t *data, int32_t length);

    /**
     @fn iTXD_Ex(qhyccd_handle *dev_handle,uint8_t *data, int32_t length,uint8_t ep)
     @brief send a package to deivce using the bulk endpoint
     @param dev_handle device control handle
     @param data package buffer
     @param length the package size(unit:byte)
         @param ep the endpoint number
     @return
     success return QHYCCD_SUCCESS \n
     another QHYCCD_ERROR code on other failures
     */
    uint32_t iTXD_Ex(qhyccd_handle *dev_handle, uint8_t *data, int32_t length, uint8_t ep);

    /**
     @fn iRXD(qhyccd_handle *dev_handle,uint8_t *data, int32_t length)
     @brief get a package from deivce using the bulk endpoint
     @param dev_handle device control handle
     @param data package buffer
     @param length the package size(unit:byte)
     @return
     success return QHYCCD_SUCCESS \n
     another QHYCCD_ERROR code on other failures
     */
    uint32_t iRXD(qhyccd_handle *dev_handle, uint8_t *data, int32_t length);

    /**
     @fn iRXD_Ex(qhyccd_handle *dev_handle,uint8_t *data, int32_t length,uint8_t ep)
     @brief get a package from deivce using the bulk endpoint
     @param dev_handle device control handle
     @param data package buffer
     @param length the package size(unit:byte)
         @param ep the endpoint number
     @return
     success return QHYCCD_SUCCESS \n
     another QHYCCD_ERROR code on other failures
     */
    uint32_t iRXD_Ex(qhyccd_handle *dev_handle, uint8_t *data, uint32_t length, uint8_t ep);

    /**
     @fn uint32_t vendTXD_Ex(qhyccd_handle *dev_handle, uint8_t req,uint16_t value,uint16_t index,uint8_t* data, uint16_t length)
     @brief send a package to deivce using the vendor request,extend the index and value interface
     @param dev_handle device control handle
     @param req request command
     @param value value interface
     @param index index interface
     @param data package buffer
     @param length the package size(unit:byte)
     @return
     success return QHYCCD_SUCCESS \n
     another QHYCCD_ERROR code on other failures
     */
    uint32_t vendTXD_Ex(qhyccd_handle *dev_handle, uint8_t req, uint16_t value, uint16_t index, uint8_t* data, uint16_t length);

    /**
     @fn uint32_t vendRXD_Ex(qhyccd_handle *dev_handle, uint8_t req,uint16_t value,uint16_t index,uint8_t* data, uint16_t length)
     @brief get a package from deivce using the vendor request,extend the index and value interface
     @param dev_handle device control handle
     @param req request command
     @param value value interface
     @param index index interface
     @param data package buffer
     @param length the package size(unit:byte)
     @return
     success return QHYCCD_SUCCESS \n
     another QHYCCD_ERROR code on other failures
     */
    uint32_t vendRXD_Ex(qhyccd_handle *dev_handle, uint8_t req, uint16_t value, uint16_t index, uint8_t* data, uint16_t length);

    uint32_t QHY5IIIreadUSB2B(qhyccd_handle *dev_handle, uint8_t *data, uint32_t p_num, uint32_t timeout);
    /**
     @fn uint32_t readUSB2B(qhyccd_handle *dev_handle, uint8_t *data,uint32_t p_size, uint32_t p_num, uint32_t* pos)
     @brief get image packages from deivce using bulk endpoint
     @param dev_handle device control handle
     @param data package buffer
     @param p_size package size
     @param p_num package num
     @param pos reserved
         @param duration the exposure time
     @return
     success return QHYCCD_SUCCESS \n
     another QHYCCD_ERROR code on other failures
     */
    uint32_t readUSB2B(qhyccd_handle *dev_handle, uint8_t *data, uint32_t p_size, uint32_t p_num, uint32_t* pos, uint32_t timeout);

    /**
     @fn uint32_t beginVideo(qhyccd_handle *handle)
     @brief send begin exposure signal to deivce
     @param handle device control handle
     @return
     success return QHYCCD_SUCCESS \n
     another QHYCCD_ERROR code on other failures
     */
    uint32_t beginVideo(qhyccd_handle *handle);

    /**
     @fn uint32_t sendRegisterQHYCCDOld(qhyccd_handle *handle,
     CCDREG reg, uint32_t P_Size, uint32_t *Total_P, uint32_t *PatchNumber)
     @brief send register params to deivce and some other info
     @param handle deivce control handle
     @param reg register struct
     @param P_Size the package size
     @param Total_P the total packages
     @param PatchNumber the patch for usb transfer,the total size must multiple 512 bytes.
     @return
     success return QHYCCD_SUCCESS \n
     another QHYCCD_ERROR code on other failures
     */
    uint32_t sendRegisterQHYCCDOld(qhyccd_handle *handle,
            CCDREG reg, uint32_t P_Size, uint32_t *Total_P, uint32_t *PatchNumber);

    /**
     @fn uint32_t sendRegisterQHYCCDNew(qhyccd_handle *handle,
     CCDREG reg, uint32_t P_Size, uint32_t *Total_P, uint32_t *PatchNumber)
     @brief New interface!!! send register params to deivce and some other info
     @param handle deivce control handle
     @param reg register struct
     @param P_Size the package size
     @param Total_P the total packages
     @param PatchNumber the patch for usb transfer,the total size must multiple 512 bytes.
     @return
     success return QHYCCD_SUCCESS \n
     another QHYCCD_ERROR code on other failures
     */
    uint32_t sendRegisterQHYCCDNew(qhyccd_handle *handle,
            CCDREG reg, uint32_t P_Size, uint32_t *Total_P, uint32_t *PatchNumber);

    /**
    @fn uint32_t sendRegisterBioCCD(qhyccd_handle *handle,BIOREG reg)
    @brief api like sendRegisterQHYCCDOld,just a different version for some kinds of cameras
    @param handle deivce control handle
    @param reg register struct
    @return
    success return QHYCCD_SUCCESS \n
    another QHYCCD_ERROR code on other failures
     */
    uint32_t sendRegisterBioCCD(qhyccd_handle *handle, BIOREG reg);

    /**
    @fn uint32_t setDisableGuider_INT(qhyccd_handle *handle)
    @brief turn off the ST4 port(guide port)
    @param handle deivce control handle
    @return
    success return QHYCCD_SUCCESS \n
    another QHYCCD_ERROR code on other failures
     */
    uint32_t setDisableGuider_INT(qhyccd_handle *handle);

    /**
    @fn uint32_t setBioCCDDigitalGain_INT(qhyccd_handle *handle,uint8_t gain)
    @brief set bioccd digitalgain
    @param handle deivce control handle
    @param gain gain value
    @return
    success return QHYCCD_SUCCESS \n
    another QHYCCD_ERROR code on other failures
     */
    uint32_t setBioCCDDigitalGain_INT(qhyccd_handle *handle, uint8_t gain);

    /**
    @fn uint32_t setBioCCDGain_INT(qhyccd_handle *handle,uint8_t gain)
    @brief set bioccd analog gain
    @param handle deivce control handle
    @param gain gain value
    @return
    success return QHYCCD_SUCCESS \n
    another QHYCCD_ERROR code on other failures
     */
    uint32_t setBioCCDGain_INT(qhyccd_handle *handle, uint16_t gain);

    /**
    @fn uint32_t getExpSetting(double T, uint32_t *ExpTime, int32_t *LiveExpTime, double A,double B, double V, double LinePeriod)
    @brief calc the exposure time
    @param T
    @param ExpTime
    @param LiveExpTime
    @param A
    @param B
    @param V
    @param LinePeriod
    @return
    success return QHYCCD_SUCCESS \n
    another QHYCCD_ERROR code on other failures
     */
    uint32_t getExpSetting(double T, uint32_t *ExpTime, int32_t *LiveExpTime, double A, double B, double V, double LinePeriod);

    /**
    @fn uint32_t setBioCCDExp_INT(qhyccd_handle *handle,uint32_t ExpTime)
    @brief set bioccd exposure time
    @param handle deivce control handle
    @param ExpTime exposure time value
    @return
    success return QHYCCD_SUCCESS \n
    another QHYCCD_ERROR code on other failures
     */
    uint32_t setBioCCDExp_INT(qhyccd_handle *handle, uint32_t ExpTime);

    /**
    @fn uint32_t setBioCCDLiveExp_INT(qhyccd_handle *handle,uint16_t VideoExpTime)
    @brief set bioccd live exposure time
    @param handle deivce control handle
    @param VideoExpTime live exposure time value
    @return
    success return QHYCCD_SUCCESS \n
    another QHYCCD_ERROR code on other failures
     */
    uint32_t setBioCCDLiveExp_INT(qhyccd_handle *handle, uint16_t VideoExpTime);

    /**
     @fn uint32_t setDC201FromInterrupt(qhyccd_handle *handle,uint8_t PWM,uint8_t FAN)
     @brief control the DC201 using the interrupt endpoint
     @param handle deivce control handle
     @param PWM cool power
     @param FAN switch for camera's fan (NOT SUPPORT NOW)
     @return
     success return QHYCCD_SUCCESS \n
     another QHYCCD_ERROR code on other failures
     */
    uint32_t setDC201FromInterrupt(qhyccd_handle *handle, uint8_t PWM, uint8_t FAN);

    /**
     @fn signed short getDC201FromInterrupt(qhyccd_handle *handle)
     @brief get the temprature value from DC201 using the interrupt endpoint
     @param handle deivce control handle
     @return
     success return  temprature value \n
     another QHYCCD_ERROR code on other failures
     */
    int16_t getDC201FromInterrupt(qhyccd_handle *handle);

    /**
     @fn uint8_t getFromInterrupt(qhyccd_handle *handle,uint8_t length,uint8_t *data)
     @brief get some special information from deivce,using interrupt endpoint
     @param handle deivce control handle
     @param length package size
     @param data package buffer
     @return
     success return QHYCCD_SUCCESS \n
     another QHYCCD_ERROR code on other failures
     */
    uint8_t getFromInterrupt(qhyccd_handle *handle, uint8_t length, uint8_t *data);

    /**
     @fn double GetCCDTemp(qhyccd_handle *handle)
     @brief get ccd/cmos chip temperature
     @param handle deivce control handle
     @return
     success return temperature \n
     */
    double GetCCDTemp(qhyccd_handle *handle);

    /**
     @fn double RToDegree(double R)
     @brief calc,transfer R to degree
     @param R R
     @return
     success return degree
     */
    double RToDegree(double R);

    /**
     @fn double mVToDegree(double V)
     @brief calc,transfer mV to degree
     @param V mv
     @return
     success return degree
     */
    double mVToDegree(double V);

    /**
     @fn double DegreeTomV(double degree)
     @brief calc,transfer degree to mv
     @param degree degree
     @return
     success return mv
     */
    double DegreeTomV(double degree);

    /**
     @fn double DegreeToR(double degree)
     @brief calc,transfer degree to R
     @param degree degree
     @return
     success return R
     */
    double DegreeToR(double degree);

    /**
     @fn uint32_t I2CTwoWrite(qhyccd_handle *handle,uint16_t addr,uint16_t value)
     @brief set param by I2C
     @param handle device control handle
     @param addr inter reg addr
     @param value param value
     @return
     success return QHYCCD_SUCCESS \n
     another QHYCCD_ERROR code on other failures
     */
    uint32_t I2CTwoWrite(qhyccd_handle *handle, uint16_t addr, uint16_t value);

    /**
     @fn uint16_t I2CTwoRead(qhyccd_handle *handle,uint16_t addr)
     @brief get param by I2C
     @param handle device control handle
     @param addr inter reg addr
     @return
     success return param value \n
     another QHYCCD_ERROR code on other failures
     */
    uint16_t I2CTwoRead(qhyccd_handle *handle, uint16_t addr);

    /**
     @fn uint8_t MSB(uint16_t i)
     @brief apart the 16 bits value to 8bits(high 8bits)
     @param i 16bits value
     @return
     success return 8bits value(high 8bits) \n
     another QHYCCD_ERROR code on other failures
     */
    uint8_t MSB(uint16_t i);

    /**
     @fn uint8_t LSB(uint16_t i)
     @brief apart the 16 bits value to 8bits(low 8bits)
     @param i 16bits value
     @return
     success return 8bits value(low 8bits) \n
     another QHYCCD_ERROR code on other failures
     */
    uint8_t LSB(uint16_t i);
    uint8_t MSB3(uint32_t i);
    uint8_t MSB2(uint32_t i);
    uint8_t MSB1(uint32_t i);
    uint8_t MSB0(uint32_t i);

    void SWIFT_MSBLSB12BITS(uint8_t * Data, uint32_t x, uint32_t y);
    void SWIFT_MSBLSB16BITS(uint8_t * Data, uint32_t x, uint32_t y);
    void SWIFT_MSBLSB14BITS(uint8_t * Data, uint32_t x, uint32_t y);

    void QHY5II_SWIFT_MSBLSB12BITS(uint8_t * Data, uint32_t x, uint32_t y);
    void QHY5II_SWIFT_MSBLSB14BITS(uint8_t * Data, uint32_t x, uint32_t y);
    void QHY5II_SWIFT_8BitsTo16Bits(uint8_t * Dst, uint8_t * Src, uint32_t x, uint32_t y);

    void QHY5II_DeNoise(uint8_t *data, uint32_t x, uint32_t y, double curgain);
    //void OutputDebugPrintf(const char * strOutputString,...);

    static void *pollHandleEvents(void *arg);
    static void findCompleteFrame(uint8_t *rawarray, uint32_t length);
    static void asyImageDataCallBack(struct libusb_transfer *transfer);
    const char* STDCALL GetTimeStamp();
    
    uint32_t camstatus; // the camera current status
    uint32_t ep1num; // ep1in transfer data length
    uint32_t usbintrep; // usb interrupt read endpoint
    uint32_t psize; // usb transfer package size at onece
    uint32_t totalp; // the total usb transfer packages
    uint32_t patchnumber; // patch for image transfer packages 
    uint32_t readp; // the number of alreay read usb transfer packages

    int32_t m_UsbTransferred;

    uint8_t m_disableTempReadout;

    uint8_t usbep; // usb transfer endpoint
    uint8_t usbintwep; // usb interrupt write endpoint
    uint8_t intepflag;
    uint8_t usbtype;
    uint8_t m_UsbPacket[LIBUSB_PACKET_LENGTH];
    
    CCDREG ccdreg; // ccd registers params
    BIOREG imgreg; // bioccd registers params 
    char timeStampStr[128];

#ifdef WIN32
    CRITICAL_SECTION csep;
#else
    pthread_mutex_t mutex;
#endif
    
};

#endif