-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilterWheel.cpp
More file actions
230 lines (175 loc) · 6.3 KB
/
FilterWheel.cpp
File metadata and controls
230 lines (175 loc) · 6.3 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
///////////////////////////////////////////////////////////////////////////////
// FILE: FilterWheel.cpp
// PROJECT: Micro-Manager
// SUBSYSTEM: DeviceAdapters
//-----------------------------------------------------------------------------
// DESCRIPTION: The example implementation of the demo camera.
// Simulates generic digital camera and associated automated
// microscope devices and enables testing of the rest of the
// system without the need to connect to the actual hardware.
//
// AUTHOR: Shirish Goyal <shirish.goyal@gmail.com>, BioCurious, Sunnyvale, CA
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define snprintf _snprintf
#endif
#include "FilterWheel.h"
#include <cstdlib>
#include <string>
#include <map>
#include <math.h>
#include <sstream>
#include "../../MMDevice/ModuleInterface.h"
using namespace std;
// External names used by the rest of the system
// to load particular device from DLL
const char *g_DeviceName = "Arduino Filter Wheel";
///////////////////////////////////////////////////////////////////////////////
// Exported MMDevice API
///////////////////////////////////////////////////////////////////////////////
//MODULE_API void InitializeModuleData() {
// RegisterDevice(g_DeviceName, MM::StateDevice, "Arduino Filter Wheel");
//}
//
//MODULE_API MM::Device *CreateDevice(const char *deviceName) {
// if (deviceName == 0)
// return 0;
//
//
// if (strcmp(deviceName, g_DeviceName) == 0) {
// return new ArduinoFilterWheel();
// }
//
// return 0;
//}
//
//MODULE_API void DeleteDevice(MM::Device *pDevice) {
// delete pDevice;
//}
///////////////////////////////////////////////////////////////////////////////
// Arduino Filter Wheel implementation
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ArduinoFilterWheel::ArduinoFilterWheel() :
numPos_(6),
initialized_(false),
changedTime_(0.0),
position_(0),
port_("") {
InitializeDefaultErrorMessages();
//Com port
CPropertyAction *pAct = new CPropertyAction(this, &ArduinoFilterWheel::OnCOMPort);
CreateProperty(MM::g_Keyword_Port, "", MM::String, false, pAct, true);
EnableDelay(); // signals that the delay setting will be used
}
ArduinoFilterWheel::~ArduinoFilterWheel() {
Shutdown();
}
void ArduinoFilterWheel::GetName(char *Name) const {
CDeviceUtils::CopyLimitedString(Name, g_DeviceName);
}
int ArduinoFilterWheel::Initialize() {
if (initialized_)
return DEVICE_OK;
// set property list
// -----------------
// Name
int ret = CreateProperty(MM::g_Keyword_Name, g_DeviceName, MM::String, true);
if (DEVICE_OK != ret)
return ret;
// Description
ret = CreateProperty(MM::g_Keyword_Description, "Arduino Filter Wheel Driver", MM::String, true);
if (DEVICE_OK != ret)
return ret;
// Set timer for the Busy signal, or we'll get a time-out the first time we check the state of the shutter, for good measure, go back 'delay' time into the past
changedTime_ = GetCurrentMMTime();
// create default positions and labels
const int bufSize = 64;
char buf[bufSize];
// add Stop Position
snprintf(buf, bufSize, "Stop");
SetPositionLabel(0, buf);
// add Home Position **&*&*&&*&*&* TODO: HOME position value *^^*^*^*^^
snprintf(buf, bufSize, "Home");
SetPositionLabel(0, buf);
for (long i = 1; i <= numPos_; i++) {
snprintf(buf, bufSize, "Filter %ld", i);
SetPositionLabel(i, buf);
}
// State
// -----
CPropertyAction *pAct = new CPropertyAction(this, &ArduinoFilterWheel::OnState);
ret = CreateProperty(MM::g_Keyword_State, "0", MM::Integer, false, pAct);
if (ret != DEVICE_OK)
return ret;
// Label
// -----
pAct = new CPropertyAction(this, &CStateBase::OnLabel);
ret = CreateProperty(MM::g_Keyword_Label, "", MM::String, false, pAct);
if (ret != DEVICE_OK)
return ret;
ret = UpdateStatus();
if (ret != DEVICE_OK)
return ret;
initialized_ = true;
return DEVICE_OK;
}
bool ArduinoFilterWheel::Busy() {
MM::MMTime interval = GetCurrentMMTime() - changedTime_;
MM::MMTime delay(GetDelayMs() * 1000.0);
return (interval < delay);
}
int ArduinoFilterWheel::Shutdown() {
if (initialized_) {
initialized_ = false;
}
return DEVICE_OK;
}
///////////////////////////////////////////////////////////////////////////////
// Action handlers
///////////////////////////////////////////////////////////////////////////////
int ArduinoFilterWheel::OnState(MM::PropertyBase *pProp, MM::ActionType eAct) {
if (eAct == MM::BeforeGet) {
printf("Getting position of Filter Wheel\n");
pProp->Set(position_);
// nothing to do, let the caller to use cached property
} else if (eAct == MM::AfterSet) {
// Set timer for the Busy signal
changedTime_ = GetCurrentMMTime();
long pos;
pProp->Get(pos);
//char* deviceName;
//GetName(deviceName);
printf("Moving to position %ld\n", position_);
if (pos >= numPos_ || pos < 0) {
pProp->Set(position_); // revert
return ERR_UNKNOWN_POSITION;
}
const int bufSize = 20;
char buf[bufSize];
snprintf(buf, bufSize, "pos=%ld", pos);
SendSerialCommand(port_.c_str(), buf, "\r");
position_ = pos;
}
return DEVICE_OK;
}
int ArduinoFilterWheel::OnCOMPort(MM::PropertyBase *pProp, MM::ActionType eAct) {
if (eAct == MM::BeforeGet) {
pProp->Set(port_.c_str());
} else if (eAct == MM::AfterSet) {
if (initialized_) {
pProp->Set(port_.c_str());
}
pProp->Get(port_);
}
return DEVICE_OK;
}
void ArduinoFilterWheel::InitializeFilterWheel() {
SendSerialCommand(port_.c_str(), "sensors=0", "\r");
SendSerialCommand(port_.c_str(), "pos?", "\r");
std::string ans = "";
GetSerialAnswer(port_.c_str(), "\n", ans);
int i = 0;
from_string<int>(i, (ans).c_str(), std::dec);
position_ = i;
}