forked from OpenPHDGuiding/phd2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cam_LEwebcam.cpp
190 lines (157 loc) · 5.72 KB
/
cam_LEwebcam.cpp
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
/*
* cam_LESerialWebcam.cpp
* PHD Guiding
*
* Created by Craig Stark.
* Copyright (c) 2013 Craig Stark.
* Ported to opencv by Bret McKee.
* Copyright (c) 2013 Bret McKee.
* 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(LE_CAMERA)
#include "cam_wdm_base.h"
CameraLEWebcam::CameraLEWebcam(void)
: CameraWDM()
{
Name = _T("Generic LE Webcam");
PropertyDialogType = PROPDLG_WHEN_CONNECTED;
HasDelayParam = true;
}
CameraLEWebcam::~CameraLEWebcam(void)
{
}
bool CameraLEWebcam::Connect(const wxString& camId)
{
bool bError = false;
try
{
if (CameraWDM::Connect(camId))
{
throw ERROR_INFO("Unable to open base class camera");
}
LEControl(LECAMERA_LED_OFF | LECAMERA_SHUTTER_CLOSED | LECAMERA_EXPOSURE_FIELD_NONE | LECAMERA_AMP_OFF);
}
catch (const wxString& Msg)
{
POSSIBLY_UNUSED(Msg);
bError = true;
}
return bError;
}
bool CameraLEWebcam::Disconnect()
{
bool bError = false;
bError = CameraWDM::Disconnect();
return bError;
}
bool CameraLEWebcam::Capture(int duration, usImage& img, int options, const wxRect& subframe)
{
bool bError = false;
try
{
int ampOffTime = 400;
int ampOnTime = duration - ampOffTime;
if (ampOnTime <= 0)
{
ampOffTime = duration;
}
else
{
// do the "amp on" part of the exposure
LEControl(LECAMERA_LED_GREEN | LECAMERA_SHUTTER_OPEN | LECAMERA_EXPOSURE_FIELD_A | LECAMERA_EXPOSURE_FIELD_B | LECAMERA_AMP_ON);
wxMilliSleep(ampOnTime);
}
// do the "amp off" part of the exposure.
LEControl(LECAMERA_LED_RED | LECAMERA_SHUTTER_OPEN | LECAMERA_EXPOSURE_FIELD_A | LECAMERA_EXPOSURE_FIELD_B | LECAMERA_AMP_OFF);
wxMilliSleep(ampOffTime);
// exposure complete - release the frame
LEControl(LECAMERA_SHUTTER_CLOSED | LECAMERA_AMP_ON | LECAMERA_EXPOSURE_FIELD_NONE | LECAMERA_AMP_OFF);
// wait the final delay before reading (if there is one)
if (ReadDelay)
{
wxMilliSleep(ReadDelay);
}
// now record the frame.
// Start by grabbing three frames
usImage frame1;
if (CaptureOneFrame(frame1, options, subframe))
{
throw ERROR_INFO("CaptureOneFrame(frame1) failed");
}
usImage frame2;
if (CaptureOneFrame(frame2, options, subframe))
{
throw ERROR_INFO("CaptureOneFrame(frame2) failed");
}
usImage frame3;
if (CaptureOneFrame(frame3, options, subframe))
{
throw ERROR_INFO("CaptureOneFrame(frame3) failed");
}
unsigned short *dptr1 = frame1.ImageData;
unsigned short *dptr2 = frame2.ImageData;
unsigned short *dptr3 = frame3.ImageData;
UINT64 sum1=0;
UINT64 sum2=0;
UINT64 sum3=0;
// we only use the data from the frame with the largest sum.
// This is because we are not exactly sure when we will capture the "Long Exposure"
// frame
for (unsigned int i = 0;i < frame1.NPixels; i++)
{
sum1 += *dptr1++;
sum2 += *dptr2++;
sum3 += *dptr3++;
}
Debug.Write(wxString::Format("sum1=%lld sum2=%lld sum3=%lld\n", sum1, sum2, sum3));
usImage *srcPtr = &frame1;
if (sum2 > sum1 && sum2 > sum3)
{
srcPtr = &frame2;
}
else if (sum3 > sum1 && sum3 > sum2)
{
srcPtr = &frame3;
}
if (img.Init(srcPtr->Size))
{
throw ERROR_INFO("img.Init() failed");
}
img.SwapImageData(*srcPtr);
}
catch (const wxString& Msg)
{
POSSIBLY_UNUSED(Msg);
bError = true;
}
// turn off the LE camera
LEControl(LECAMERA_LED_OFF | LECAMERA_SHUTTER_CLOSED | LECAMERA_EXPOSURE_FIELD_NONE | LECAMERA_AMP_OFF);
return bError;
}
#endif // defined(LE_CAMERA)