Repository navigation
Expand file tree
/
Copy pathMMMALAutofocus.cpp
More file actions
376 lines (315 loc) · 8.96 KB
/
Copy pathMMMALAutofocus.cpp
File metadata and controls
376 lines (315 loc) · 8.96 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
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
/*
* MMMALAutofocus.cpp
*
* Created on: Jul 13, 2013
* Author: Ji-Yu
*/
#include "MMMALAutofocus.h"
namespace MMMAL
{
const char * const MMMALAutofocus::DeviceName_ = "IXZDC";
const char * const MMMALAutofocus::Description_ = "Olympus IX Auto Focus";
const char * const MMMALAutofocus::Keyword_AFSearchRange_ = "Search Range";
const char * const MMMALAutofocus::Keyword_AFOffset_ = "Offset";
const char * const MMMALAutofocus::Keyword_AFStatus_ = "Status";
const char * const MMMALAutofocus::Keyword_ContinuousFocus_ ="Continuous Focus";
const char * const Keyword_AberlensPostion_ = "";
const char * const Keyword_AFNearLimit_ = "";
const char * const Keyword_AFFarLimit_ = "";
const char * const Keyword_OffsetOrigin_ = "";
const char * const Keyword_OffsetDistance_ = "";
const char * const Keyword_AutoMovement_ = "";
MMMALAutofocus::MMMALAutofocus() : initialized_(false), hub_(NULL), continuousFocusing_(false)
{
InitializeDefaultErrorMessages();
CreateProperty(MM::g_Keyword_Name, DeviceName_, MM::String, true); // Name
CreateProperty(MM::g_Keyword_Description, Description_, MM::String, true); // Description
}
MMMALAutofocus::~MMMALAutofocus()
{
Shutdown();
}
void MMMALAutofocus::GetName(char *pszName) const
{
GetProperty(MM::g_Keyword_Name, pszName);
}
bool MMMALAutofocus::Busy()
{
return hub_->IsAFBusy();
}
int MMMALAutofocus::Shutdown()
{
if (hub_ != NULL)
{
hub_->SetAutofocusDevice(NULL);
}
initialized_ = false;
return DEVICE_OK;
}
void MMMALAutofocus::GetStatusString(char * buf)
{
MAL_MS_AFSTATUS status;
status = hub_->GetAFStatus();
switch(status)
{
case MAL_MS_AF_OFF:
strcpy(buf, "Off");
break;
case MAL_MS_AF_FOCUS:
strcpy(buf, "Focus");
break;
case MAL_MS_AF_TRACE:
strcpy(buf, "Trace");
break;
case MAL_MS_AF_WAIT:
strcpy(buf,"Wait");
break;
case MAL_MS_AF_SEARCH:
strcpy(buf,"Search");
break;
case MAL_MS_AF_PAUSE:
strcpy(buf, "Pause");
break;
default:
strcpy(buf, "Unknown");
break;
}
}
int MMMALAutofocus::Initialize()
{
int ret;
if (initialized_)
{
return DEVICE_OK;
}
hub_ = static_cast<MMMALHub*>(GetParentHub());
if (hub_ == NULL)
{
return DEVICE_ERR;
}
hub_->SetAutofocusDevice(this);
//autofocus parameters
if (hub_->HasZDC2() || hub_->HasZDC())
{
CPropertyAction* pAct;
pAct = new CPropertyAction (this, &MMMALAutofocus::OnContinuousFocus);
ret = CreateProperty(Keyword_ContinuousFocus_, "Off", MM::String, false, pAct);
if (ret != DEVICE_OK)
{
return ret;
}
AddAllowedValue(Keyword_ContinuousFocus_, "On");
AddAllowedValue(Keyword_ContinuousFocus_, "Off");
if (hub_->HasZDC2())
{
pAct = new CPropertyAction (this, &MMMALAutofocus::OnAutoFocusOffset);
ret = CreateProperty(Keyword_AFOffset_, "0", MM::Integer, false, pAct);
if (ret != DEVICE_OK)
{
return ret;
}
SetPropertyLimits(Keyword_AFOffset_, -2080, 2080);
}
LONGLONG range;
ret = hub_->GetAFSearchRange(&range);
if (ret != DEVICE_OK)
{
return ret;
}
const char * buf = CDeviceUtils::ConvertToString((double)range/ 1e6);
pAct = new CPropertyAction (this, &MMMALAutofocus::OnAutoFocusSearchRange);
ret = CreateProperty(Keyword_AFSearchRange_, buf, MM::Float, false, pAct);
if (ret != DEVICE_OK)
{
return ret;
}
this->SetPropertyLimits(Keyword_AFSearchRange_, 0, 1000);
pAct = new CPropertyAction (this, &MMMALAutofocus::OnAutoFocusStatus);
ret = CreateProperty(Keyword_AFStatus_, buf, MM::String, true, pAct);
if (ret != DEVICE_OK)
{
return ret;
}
}
initialized_ = true;
return DEVICE_OK;
}
int MMMALAutofocus::SetContinuousFocusing(bool state)
{
if (!hub_->HasZDC2())
{
return DEVICE_UNSUPPORTED_COMMAND;
}
continuousFocusing_ = state;
if (continuousFocusing_)
{
hub_->SetAFStatus(MAL_MS_AF_ON);
}
else
{
hub_->SetAFStatus(MAL_MS_AF_OFF);
}
return DEVICE_OK;
}
int MMMALAutofocus::GetContinuousFocusing(bool& state)
{
state = continuousFocusing_;
return DEVICE_OK;
}
bool MMMALAutofocus::IsContinuousFocusLocked()
{
if (! hub_->HasZDC2())
{
return false;
}
return ( hub_->GetAFStatus() == MAL_MS_AF_FOCUS);
}
int MMMALAutofocus::FullFocus()
{
return hub_->SetAFStatus(MAL_MS_AF_SHOT);
}
int MMMALAutofocus::IncrementalFocus()
{
return DEVICE_UNSUPPORTED_COMMAND;
}
int MMMALAutofocus::GetLastFocusScore(double& score)
{
return DEVICE_UNSUPPORTED_COMMAND;
}
int MMMALAutofocus::GetCurrentFocusScore(double& score)
{
return DEVICE_UNSUPPORTED_COMMAND;
}
int MMMALAutofocus::AutoSetParameters()
{
LONGLONG range;
hub_->GetAFSearchRange(&range);
if (range == 0)
{
hub_->SetAFSearchRange((LONGLONG)30 * (LONGLONG)1000000);
}
return DEVICE_OK;
}
int MMMALAutofocus::GetOffset(double &offset)
{
if (hub_->HasZDC2())
{
long lOffset;
int ret = hub_->GetAFOffset(&lOffset);
offset = (double) lOffset;
return ret;
}
else
{
return DEVICE_UNSUPPORTED_COMMAND;
}
}
int MMMALAutofocus::SetOffset(double offset)
{
if (hub_->HasZDC2())
{
int ret = hub_->SetAFOffset((long) offset);
return ret;
}
else
{
return DEVICE_UNSUPPORTED_COMMAND;
}
}
int MMMALAutofocus::AFStatusChanged()
{
int ret = DEVICE_OK;
if (IsCallbackRegistered())
{
char buf[MM::MaxStrLength];
GetStatusString(buf);
ret = GetCoreCallback()->OnPropertyChanged(this, Keyword_AFStatus_, buf);
}
return ret;
}
int MMMALAutofocus::AFOffsetChanged()
{
int ret = DEVICE_OK;
if (IsCallbackRegistered())
{
long offset;
if (hub_->GetAFOffset(& offset) == DEVICE_OK)
{
ret = GetCoreCallback()->OnPropertyChanged(this, Keyword_AFOffset_, CDeviceUtils::ConvertToString(offset));
}
}
else
{
long offset;
hub_->GetAFOffset(& offset);
std::cout << "Focus offset " << offset << std::endl;
}
return ret;
}
int MMMALAutofocus::OnContinuousFocus(MM::PropertyBase* pProp, MM::ActionType eAct)
{
int ret = DEVICE_OK;
std::string mode;
if (eAct == MM::BeforeGet)
{
}
else if (eAct == MM::AfterSet)
{
pProp->Get(mode);
if (mode.compare("On") == 0)
{
ret = SetContinuousFocusing(true);
}
else if (mode.compare("Off") == 0)
{
ret = SetContinuousFocusing(false);
}
}
return DEVICE_OK;
}
int MMMALAutofocus::OnAutoFocusOffset(MM::PropertyBase* pProp, MM::ActionType eAct)
{
int ret = DEVICE_OK;
if (eAct == MM::BeforeGet)
{
double offset;
ret = GetOffset(offset);
pProp->Set(long(offset));
}
else if (eAct == MM::AfterSet)
{
long lOffset;
pProp->Get(lOffset);
ret = SetOffset((double) lOffset);
}
return ret;
}
int MMMALAutofocus::OnAutoFocusSearchRange(MM::PropertyBase* pProp, MM::ActionType eAct)
{
int ret = DEVICE_OK;
if (eAct == MM::BeforeGet)
{
LONGLONG llRange;
ret = hub_->GetAFSearchRange(&llRange);
pProp->Set((double) llRange / 1e6);
}
else if (eAct == MM::AfterSet)
{
double range;
pProp->Get(range);
ret = hub_->SetAFSearchRange((LONGLONG)(range * 1e6));
}
return ret;
}
int MMMALAutofocus::OnAutoFocusStatus(MM::PropertyBase* pProp, MM::ActionType eAct)
{
int ret = DEVICE_OK;
if (eAct == MM::BeforeGet)
{
char buf[MM::MaxStrLength];
GetStatusString(buf);
pProp->Set(buf);
}
return ret;
}
}