-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduinoHub.cpp
211 lines (164 loc) · 6.35 KB
/
ArduinoHub.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//
// Created by Shirish Goyal on 9/14/16.
//
#include "ArduinoHub.h"
#include "../../MMDevice/ModuleInterface.h"
#include "FilterWheel.h"
#include <sstream>
#include <cstdio>
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define snprintf _snprintf
#endif
const char *g_DeviceNameArduinoHub = "Arduino-Hub";
const char *g_DeviceNameArduinoFilterWheel = "Arduino-Filter-Wheel";
const int g_Min_MMVersion = 1;
const int g_Max_MMVersion = 2;
const char *g_versionProp = "Version";
const char* g_On = "On";
const char* g_Off = "Off";
// static lock
MMThreadLock ArduinoHub::lock_;
///////////////////////////////////////////////////////////////////////////////
// Exported MMDevice API
///////////////////////////////////////////////////////////////////////////////
MODULE_API void InitializeModuleData() {
RegisterDevice(g_DeviceNameArduinoHub, MM::HubDevice, "Arduino Hub (required)");
RegisterDevice(g_DeviceNameArduinoFilterWheel, MM::StateDevice, "Arduino Filter Wheel");
}
MODULE_API MM::Device *CreateDevice(const char *deviceName) {
if (deviceName == 0)
return 0;
if (strcmp(deviceName, g_DeviceNameArduinoHub) == 0) {
return new ArduinoHub;
} else if (strcmp(deviceName, g_DeviceNameArduinoFilterWheel) == 0) {
return new ArduinoFilterWheel;
}
return 0;
}
MODULE_API void DeleteDevice(MM::Device *pDevice) {
delete pDevice;
}
///////////////////////////////////////////////////////////////////////////////
int ArduinoHub::Initialize() {
return 0;
}
bool ArduinoHub::Busy() {
return false;
}
int ArduinoHub::Shutdown() {
return 0;
}
void ArduinoHub::GetName(char *name) const {
}
ArduinoHub::ArduinoHub() :
initialized_(false),
filterWheelState_(0) {
portAvailable_ = false;
// invertedLogic_ = false;
// timedOutputActive_ = false;
InitializeDefaultErrorMessages();
SetErrorText(ERR_PORT_OPEN_FAILED, "Failed opening Arduino USB device");
SetErrorText(ERR_BOARD_NOT_FOUND,
"Did not find an Arduino board with the correct firmware. Is the Arduino board connected to this serial port?");
SetErrorText(ERR_NO_PORT_SET, "Hub Device not found. The Arduino Hub device is needed to create this device");
std::ostringstream errorText;
errorText
<< "The firmware version on the Arduino is not compatible with this adapter. Please use firmware version ";
errorText << g_Min_MMVersion << " to " << g_Max_MMVersion;
SetErrorText(ERR_VERSION_MISMATCH, errorText.str().c_str());
CPropertyAction *pAct = new CPropertyAction(this, &ArduinoHub::OnPort);
CreateProperty(MM::g_Keyword_Port, "Undefined", MM::String, false, pAct, true);
// pAct = new CPropertyAction(this, &ArduinoHub::OnLogic);
// CreateProperty("Logic", g_invertedLogicString, MM::String, false, pAct, true);
//
// AddAllowedValue("Logic", g_invertedLogicString);
// AddAllowedValue("Logic", g_normalLogicString);
}
ArduinoHub::~ArduinoHub() {
Shutdown();
}
int ArduinoHub::GetArduinoVersion(int &version) {
int ret = DEVICE_OK;
unsigned char command[1];
command[0] = 1;
version = 0;
ret = WriteToComPort(port_.c_str(), (const unsigned char *) command, 1);
if (ret != DEVICE_OK)
return ret;
std::string answer;
ret = GetSerialAnswer(port_.c_str(), "\r\n", answer);
if (ret != DEVICE_OK)
return ret;
if (answer != "Arduino-FW")
return ERR_BOARD_NOT_FOUND;
// Check version number of the Arduino
command[0] = 2;
ret = WriteToComPort(port_.c_str(), (const unsigned char *) command, 1);
if (ret != DEVICE_OK)
return ret;
std::string ans;
ret = GetSerialAnswer(port_.c_str(), "\r\n", ans);
if (ret != DEVICE_OK) {
return ret;
}
std::istringstream is(ans);
is >> version;
return ret;
}
bool ArduinoHub::SupportsDeviceDetection(void) {
return true;
}
MM::DeviceDetectionStatus ArduinoHub::DetectDevice(void) {
if (initialized_)
return MM::CanCommunicate;
MM::DeviceDetectionStatus result = MM::Misconfigured;
char answerTO[MM::MaxStrLength];
try {
std::string portLowerCase = port_;
for (std::string::iterator its = portLowerCase.begin(); its != portLowerCase.end(); ++its) {
*its = (char) tolower(*its);
}
if (0 < portLowerCase.length() && 0 != portLowerCase.compare("undefined") &&
0 != portLowerCase.compare("unknown")) {
result = MM::CanNotCommunicate;
// record the default answer time out
GetCoreCallback()->GetDeviceProperty(port_.c_str(), "AnswerTimeout", answerTO);
// device specific default communication parameters
// for Arduino Duemilanova
GetCoreCallback()->SetDeviceProperty(port_.c_str(), MM::g_Keyword_Handshaking, g_Off);
GetCoreCallback()->SetDeviceProperty(port_.c_str(), MM::g_Keyword_BaudRate, "57600");
GetCoreCallback()->SetDeviceProperty(port_.c_str(), MM::g_Keyword_StopBits, "1");
// Arduino timed out in GetArduinoVersion even if AnswerTimeout = 300 ms
GetCoreCallback()->SetDeviceProperty(port_.c_str(), "AnswerTimeout", "500.0");
GetCoreCallback()->SetDeviceProperty(port_.c_str(), "DelayBetweenCharsMs", "0");
MM::Device *pS = GetCoreCallback()->GetDevice(this, port_.c_str());
pS->Initialize();
// The first second or so after opening the serial port, the Arduino is waiting for firmware upgrades.
// Simply sleep 2 seconds.
CDeviceUtils::SleepMs(2000);
MMThreadGuard myLock(lock_);
PurgeComPort(port_.c_str());
int v = 0;
int ret = GetArduinoVersion(v);
// later, Initialize will explicitly check the version #
if (DEVICE_OK != ret) {
LogMessageCode(ret, true);
} else {
// to succeed must reach here....
result = MM::CanCommunicate;
}
pS->Shutdown();
// always restore the AnswerTimeout to the default
GetCoreCallback()->SetDeviceProperty(port_.c_str(), "AnswerTimeout", answerTO);
}
}
catch (...) {
LogMessage("Exception in DetectDevice!", false);
}
return result;
}
int ArduinoHub::DetectInstalledDevices() {
return HubBase::DetectInstalledDevices();
}