Repository navigation
Expand file tree
/
Copy pathLaserDiodeDriver.cpp
More file actions
364 lines (293 loc) · 9.59 KB
/
Copy pathLaserDiodeDriver.cpp
File metadata and controls
364 lines (293 loc) · 9.59 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
/* LaserDiodeDriver.cpp
*
* Copyright (C) 2020 John Wigg
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "LaserDiodeDriver.h"
#include <iostream>
#include <sstream>
// Always include Arduino-board commit 1d16c1119ba8a6349a982ad88a5b6d78bc918a21
#include "Arduino.h"
const char* g_BoardArduino = "Arduino";
const char* const g_Msg_DEVICE_INVALID_BOARD_TYPE = "Please choose a valid device Type!";
const char* g_LaserDiodeDriverName = "LaserDiodeDriver";
const char* ON = "On";
const char* OFF = "Off";
#define DEVICE_INVALID_BOARD_TYPE 142
MODULE_API void InitializeModuleData()
{
RegisterDevice(g_LaserDiodeDriverName, MM::GenericDevice, "Laser diode driver device adapter.");
}
MODULE_API MM::Device* CreateDevice(const char* deviceName)
{
if (deviceName == 0)
return 0;
// decide which device class to create based on the deviceName parameter
if (strcmp(deviceName, g_LaserDiodeDriverName) == 0)
{
// create camera
return new LaserDiodeDriver();
}
// ...supplied name not recognized
return 0;
}
MODULE_API void DeleteDevice(MM::Device* pDevice)
{
delete pDevice;
}
LaserDiodeDriver::LaserDiodeDriver()
{
// call the base class method to set-up default error codes/messages
InitializeDefaultErrorMessages();
SetErrorText(DEVICE_INVALID_BOARD_TYPE, g_Msg_DEVICE_INVALID_BOARD_TYPE);
int ret;
CPropertyAction* pAct = new CPropertyAction(this, &LaserDiodeDriver::OnBoardType);
ret = CreateStringProperty("Device Type", "", false, pAct, true);
#ifdef BUILD_DUMMY
AddAllowedValue("Device Type", g_BoardDummy);
#endif
#ifdef BUILD_K8061
AddAllowedValue("Device Type", g_BoardK8061);
#endif
#ifdef BUILD_ARDUINO
AddAllowedValue("Device Type", g_BoardArduino);
#endif
pAct = new CPropertyAction(this, &LaserDiodeDriver::OnPort);
ret = CreateStringProperty("Device Port", "Undefined", false, pAct, true);
for (int i = 0; i < NUMBER_OF_LASERS; ++i) {
CPropertyAction* pActLaserMinPower = new CPropertyAction (this, &LaserDiodeDriver::OnLaserMinPower);
CPropertyAction* pActLaserMaxPower = new CPropertyAction (this, &LaserDiodeDriver::OnLaserMaxPower);
char p_name[64];
// Laser label
sprintf(p_name, "Laser Label %d", i+1);
ret = CreateStringProperty(p_name, "", false, nullptr, true);
// Laser limits
sprintf(p_name, "Min. Laser Power %d (%%)", i+1);
ret = CreateFloatProperty(p_name, 0.0, false, pActLaserMinPower, true);
ret = SetPropertyLimits(p_name, 0.0, 100.0);
sprintf(p_name, "Max. Laser Power %d (%%)", i+1);
ret = CreateFloatProperty(p_name, 100.0, false, pActLaserMaxPower, true);
ret = SetPropertyLimits(p_name, 0.0, 100.0);
}
}
LaserDiodeDriver::~LaserDiodeDriver()
{
if (initialized_)
{
Shutdown();
}
delete interface_;
}
void LaserDiodeDriver::GetName(char* name) const
{
CDeviceUtils::CopyLimitedString(name, g_LaserDiodeDriverName);
}
int LaserDiodeDriver::Initialize()
{
int ret = DEVICE_OK;
// TODO: This is rather ugly
#ifdef BUILD_K8061
if (strcmp(boardType_.c_str(), g_BoardK8061) == 0) {
char dir[MM::MaxStrLength];
GetProperty("Device Port", dir);
std::string deviceDir = std::string(dir);
interface_ = new K8061(deviceDir);
} else
#endif
#ifdef BUILD_DUMMY
if (strcmp(boardType_.c_str(), g_BoardDummy) == 0) {
interface_ = new DummyBoard();
} else
#endif
#ifdef BUILD_ARDUINO
if (strcmp(boardType_.c_str(), g_BoardArduino) == 0) {
char dir[MM::MaxStrLength];
GetProperty("Device Port", dir);
std::string deviceDir = std::string(dir);
interface_ = new Arduino(deviceDir);
} else
#endif
return DEVICE_INVALID_BOARD_TYPE;
interface_->Open();
if (!interface_->DeviceIsOpen()) {
LogMessage("The device could not be opened!");
ret = DEVICE_NOT_CONNECTED;
}
if (ret != DEVICE_OK) {
return ret;
}
std::vector<std::string> digitalValues;
digitalValues.push_back(OFF);
digitalValues.push_back(ON);
for (int i = 0; i < NUMBER_OF_LASERS; ++i) {
CPropertyAction* pActLaserPower = new CPropertyAction (this, &LaserDiodeDriver::OnLaserPower);
CPropertyAction* pActLaserOnOff = new CPropertyAction (this, &LaserDiodeDriver::OnLaserOnOff);
char p_name[64];
// Laser power
sprintf(p_name, "Laser Power %d (%%)", i+1);
ret = CreateFloatProperty(p_name, 0.0, false, pActLaserPower);
ret = SetPropertyLimits(p_name, 0, 100);
// Laser on/off
sprintf(p_name, "Enable Laser %d", i+1);
ret = CreateStringProperty(p_name, OFF, false, pActLaserOnOff);
ret = SetAllowedValues(p_name, digitalValues);
}
if (ret != DEVICE_OK) {
return ret;
}
initialized_ = true;
return DEVICE_OK;
}
int LaserDiodeDriver::Shutdown()
{
if (initialized_ == false)
{
return DEVICE_OK;
}
initialized_ = false;
return DEVICE_OK;
}
int LaserDiodeDriver::OnBoardType(MM::PropertyBase* pProp, MM::ActionType eAct) {
if (eAct == MM::AfterSet)
{
pProp->Get(boardType_);
}
else if (eAct == MM::BeforeGet)
{
pProp->Set(boardType_.c_str());
}
return DEVICE_OK;
}
int LaserDiodeDriver::OnPort(MM::PropertyBase* pProp, MM::ActionType eAct) {
return DEVICE_OK;
}
int LaserDiodeDriver::OnLaserMinPower(MM::PropertyBase* pProp, MM::ActionType eAct) {
if (eAct == MM::AfterSet) {
double value;
std::string pName = pProp->GetName();
pProp->Get(value);
int idx = -1;
sscanf(pName.c_str(), "Min. Laser Power %d (%%)", &idx);
double max_value = GetLaserMaxPower(idx-1);
if (value > max_value) {
value = max_value;
}
}
return DEVICE_OK;
}
int LaserDiodeDriver::OnLaserMaxPower(MM::PropertyBase* pProp, MM::ActionType eAct) {
if (eAct == MM::AfterSet) {
double value;
std::string pName = pProp->GetName();
pProp->Get(value);
int idx = -1;
sscanf(pName.c_str(), "Max. Laser Power %d (%%)", &idx);
double min_value = GetLaserMinPower(idx-1);
if (value < min_value) {
value = min_value;
}
pProp->Set(value);
}
return DEVICE_OK;
}
int LaserDiodeDriver::OnLaserOnOff(MM::PropertyBase* pProp, MM::ActionType eAct) {
if (eAct == MM::AfterSet) {
std::string value;
std::string pName = pProp->GetName();
pProp->Get(value);
int ret;
int idx = -1;
sscanf(pName.c_str(), "Enable Laser %d", &idx);
if (value == ON) {
ret = SetLaserOnOff(idx-1, true);
} else {
ret = SetLaserOnOff(idx-1, false);
}
if (ret != DEVICE_OK) {
// error occured; revert values and return
if (value == ON) {
pProp->Set(OFF);
} else {
pProp->Set(ON);
}
return ret;
}
}
return DEVICE_OK;
}
int LaserDiodeDriver::OnLaserPower(MM::PropertyBase* pProp, MM::ActionType eAct) {
if (eAct == MM::AfterSet) {
double value;
std::string pName = pProp->GetName();
pProp->Get(value);
int ret;
int idx = -1;
sscanf(pName.c_str(), "Laser Power %d", &idx);
ret = SetLaserPower(idx-1, value);
if (ret != DEVICE_OK) {
// error occure
// TODO: Maybe this should be handled in some way?
return ret;
}
}
return DEVICE_OK;
}
double LaserDiodeDriver::GetLaserMaxPower(int idx) {
double value;
char p_name[64];
sprintf(p_name, "Max. Laser Power %d (%%)", idx+1);
GetProperty(p_name, value);
return value;
}
double LaserDiodeDriver::GetLaserMinPower(int idx) {
double value;
char p_name[64];
sprintf(p_name, "Min. Laser Power %d (%%)", idx+1);
GetProperty(p_name, value);
return value;
}
int LaserDiodeDriver::SetLaserOnOff(int idx, bool enabled) {
if (!interface_->DeviceIsOpen()) {
LogMessage("No open device!");
return DEVICE_ERR;
} else {
LogMessage("Device is open; attempting to write.");
}
int ret = interface_->WriteDigital(idx, enabled);
if (ret != DEVICE_OK) { // error
return ret;
}
return DEVICE_OK;
}
int LaserDiodeDriver::SetLaserPower(int idx, double power) {
double min_value = GetLaserMinPower(idx);
double max_value = GetLaserMaxPower(idx);
double relative_value = (min_value + power * (max_value - min_value) / 100.0) / 100.0;
if (relative_value > 1.0) {
relative_value = 1.0;
} else if (relative_value < 0.0) {
relative_value = 0.0;
}
int ret = interface_->WriteAnalogRelative(idx, relative_value);
if (ret == 1) { // error
// Debug
LogMessage("Could not set analog value!", false);
return DEVICE_ERR;
}
return DEVICE_OK;
}