forked from OpenPHDGuiding/phd2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcam_QHY5IIbase.cpp
More file actions
284 lines (239 loc) · 9.4 KB
/
cam_QHY5IIbase.cpp
File metadata and controls
284 lines (239 loc) · 9.4 KB
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
/*
* cam_QHY5IIbase.cpp
* PHD Guiding
*
* Created by Craig Stark.
* Copyright (c) 2012 Craig Stark.
* All rights reserved.
*
* This source code is distributed under the following "BSD" license
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of Craig Stark, Stark Labs nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR CONTRIBUTORS 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 "phd.h"
#if defined(QHY5II) || defined(QHY5LII)
#include "cam_QHY5II.h"
typedef DWORD (CALLBACK* Q5II_DW_V)(void);
//EXPORT DWORD _stdcall openUSB; Open USB for the selected camera. Returnd 1 is a camera was found, 0 otherwise
Q5II_DW_V Q5II_OpenUSB;
//EXPORT DWORD _stdcall isExposing(); Indicates if the camera is currently performing an exposure
Q5II_DW_V Q5II_IsExposing;
typedef void (CALLBACK* Q5II_V_V)(void);
//EXPORT void _stdcall CancelExposure(); Cancels the current exposure
Q5II_V_V Q5II_CancelExposure;
//EXPORT void _stdcall closeUSB; Closes the USB connection.
Q5II_V_V Q5II_CloseUSB;
//EXPORT void _stdcall StopCapturing(); Stops any started capturing Thread
Q5II_V_V Q5II_StopCapturing;
//EXPORT void _stdcall SingleExposure(); Clears the buffer and starts a new exposure, stops capturing after one image
Q5II_V_V Q5II_SingleExposure;
//EXPORT void _stdcall SetAutoBlackLevel(); Set automatic black level, QHY5-II Only
// Seems not in there...
//Q5II_V_V Q5II_SetAutoBlackLevel;
typedef void (CALLBACK* Q5II_V_DW)(DWORD);
//EXPORT void _stdcall SetBlackLevel( DWORD n ); QHY5-II Only Sted the blacklevel of the camera (direct register write)
// Seems to actually be the auto black level
Q5II_V_DW Q5II_SetBlackLevel;
//EXPORT void _stdcall SetGain(DWORD x ); Set gainlevel (0-100)
Q5II_V_DW Q5II_SetGain;
//EXPORT void _stdcall SetExposureTime( DWORD milisec ) ; Set exposuretime in miliseconds
Q5II_V_DW Q5II_SetExposureTime;
//EXPORT void _stdcall SetSpeed( DWORD n ); Set camera speed (0=slow, 1=medium, 2=fast). QHY5L may not support all speeds in higher resolutions.
Q5II_V_DW Q5II_SetSpeed;
//EXPORT void _stdcall SetHBlank( DWORD hBlank) ; Sets the USB delay factor to a value between 0 and 2047
Q5II_V_DW Q5II_SetHBlank;
//EXPORT void _stdcall SetLineremoval ( DWORD lineremoval ) ; Enable line noise removal algorithm. QHY5-II Only
Q5II_V_DW Q5II_SetLineRemoval;
//EXPORT DWORD _stdcall CancelGuide( DWORD Axis ); Cancel guides on a specific axis
typedef DWORD (CALLBACK* Q5II_GFD)(PUCHAR, DWORD);
//EXPORT DWORD _stdcall getFrameData(PUCHAR _buffer , DWORD size ) ; Gets the data after a SingleExposure(), 8 bit data
Q5II_GFD Q5II_GetFrameData;
typedef DWORD (CALLBACK* Q5II_GC)(DWORD, DWORD, DWORD);
//EXPORT DWORD _stdcall GuideCommand(DWORD GC, DWORD PulseTimeX, DWORD PulseTimeY) ;
Q5II_GC Q5II_GuideCommand;
Camera_QHY5IIBase::Camera_QHY5IIBase()
{
Connected = false;
m_hasGuideOutput = true;
HasGainControl = true;
RawBuffer = NULL;
Color = false;
}
wxByte Camera_QHY5IIBase::BitsPerPixel()
{
return 8;
}
static FARPROC WINAPI GetProc(HINSTANCE dll, LPCSTR name)
{
FARPROC p = GetProcAddress(dll, name);
if (!p)
{
FreeLibrary(dll);
wxMessageBox(wxString::Format(_("Camera DLL missing entry %s"), name), _("Error"), wxOK | wxICON_ERROR);
}
return p;
}
bool Camera_QHY5IIBase::Connect(const wxString& camId)
{
// returns true on error
CameraDLL = LoadLibrary(m_cameraDLLName);
if (CameraDLL == NULL)
{
wxMessageBox(wxString::Format(_("Cannot load camera dll %s.dll"), m_cameraDLLName), _("Error"),wxOK | wxICON_ERROR);
return true;
}
#define GET_PROC(p, type, name) do { \
if ((p = (type) GetProc(CameraDLL, name)) == 0) \
return true; \
} while (false)
GET_PROC(Q5II_OpenUSB, Q5II_DW_V, "openUSB");
GET_PROC(Q5II_IsExposing, Q5II_DW_V, "isExposing");
GET_PROC(Q5II_CancelExposure, Q5II_V_V, "CancelExposure");
GET_PROC(Q5II_CloseUSB, Q5II_V_V, "closeUSB");
GET_PROC(Q5II_StopCapturing, Q5II_V_V, "StopCapturing");
GET_PROC(Q5II_SingleExposure, Q5II_V_V, "SingleExposure");
// GET_PROC(Q5II_SetAutoBlackLevel, Q5II_V_V, "SetAutoBlackLevel");
GET_PROC(Q5II_SetBlackLevel, Q5II_V_DW, "SetBlackLevel");
GET_PROC(Q5II_SetGain, Q5II_V_DW, "SetGain");
GET_PROC(Q5II_SetExposureTime, Q5II_V_DW, "SetExposureTime");
GET_PROC(Q5II_SetSpeed, Q5II_V_DW, "SetSpeed");
GET_PROC(Q5II_SetHBlank, Q5II_V_DW, "SetHBlank");
GET_PROC(Q5II_GetFrameData, Q5II_GFD, "getFrameData");
GET_PROC(Q5II_GuideCommand, Q5II_GC, "GuideCommand");
#undef GET_PROC
if (!Q5II_OpenUSB())
{
wxMessageBox(_("No camera"));
return true;
}
if (RawBuffer)
delete [] RawBuffer;
size_t size = FullSize.GetWidth() * FullSize.GetHeight();
RawBuffer = new unsigned char[size];
//Q5II_SetAutoBlackLevel();
Q5II_SetBlackLevel(1);
Q5II_SetSpeed(0);
Connected = true;
return false;
}
bool Camera_QHY5IIBase::ST4PulseGuideScope(int direction, int duration)
{
DWORD reg = 0;
DWORD dur = (DWORD) duration / 10;
DWORD ptx, pty;
//if (dur >= 255) dur = 254; // Max guide pulse is 2.54s -- 255 keeps it on always
switch (direction) {
case WEST: reg = 0x80; ptx = dur; pty = 0xFFFFFFFF; break;
case NORTH: reg = 0x20; pty = dur; ptx = 0xFFFFFFFF; break;
case SOUTH: reg = 0x40; pty = dur; ptx = 0xFFFFFFFF; break;
case EAST: reg = 0x10; ptx = dur; pty = 0xFFFFFFFF; break;
default: return true; // bad direction passed in
}
Q5II_GuideCommand(reg,ptx,pty);
WorkerThread::MilliSleep(duration + 10);
return false;
}
void Camera_QHY5IIBase::ClearGuidePort()
{
//Q5II_CancelGuide(3); // 3 clears on both axes
}
void Camera_QHY5IIBase::InitCapture()
{
Q5II_SetGain(GuideCameraGain);
}
bool Camera_QHY5IIBase::Disconnect()
{
Q5II_CloseUSB();
Connected = false;
if (RawBuffer)
delete [] RawBuffer;
RawBuffer = NULL;
FreeLibrary(CameraDLL);
return false;
}
static bool StopExposure()
{
Debug.AddLine("Q5II: cancel exposure");
Q5II_CancelExposure();
return true;
}
bool Camera_QHY5IIBase::Capture(int duration, usImage& img, int options, const wxRect& subframe)
{
// Only does full frames still
static int last_dur = 0;
static int last_gain = 60;
unsigned char *bptr;
unsigned short *dptr;
int x,y;
int xsize = FullSize.GetWidth();
int ysize = FullSize.GetHeight();
// bool firstimg = true;
if (img.Init(FullSize))
{
DisconnectWithAlert(CAPT_FAIL_MEMORY);
return true;
}
if (duration != last_dur) {
Q5II_SetExposureTime(duration);
last_dur = duration;
}
if (GuideCameraGain != last_gain) {
Q5II_SetGain(GuideCameraGain);
last_gain = GuideCameraGain;
}
Q5II_SingleExposure();
CameraWatchdog watchdog(duration, GetTimeoutMs());
if (WorkerThread::MilliSleep(duration, WorkerThread::INT_ANY) &&
(WorkerThread::TerminateRequested() || StopExposure()))
{
return true;
}
while (Q5II_IsExposing())
{
wxMilliSleep(100);
if (WorkerThread::InterruptRequested() &&
(WorkerThread::TerminateRequested() || StopExposure()))
{
return true;
}
if (watchdog.Expired())
{
DisconnectWithAlert(CAPT_FAIL_TIMEOUT);
return true;
}
}
Q5II_GetFrameData(RawBuffer,xsize*ysize);
bptr = RawBuffer;
// Load and crop from the 800 x 525 image that came in
dptr = img.ImageData;
for (y=0; y<ysize; y++) {
for (x=0; x<xsize; x++, bptr++, dptr++) { // CAN SPEED THIS UP
*dptr=(unsigned short) *bptr;
}
}
if (options & CAPTURE_SUBTRACT_DARK) SubtractDark(img);
if (Color && (options & CAPTURE_RECON)) QuickLRecon(img);
return false;
}
#endif