-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathMantaUSB.cpp
More file actions
257 lines (239 loc) · 8.76 KB
/
Copy pathMantaUSB.cpp
File metadata and controls
257 lines (239 loc) · 8.76 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
#include "extern/hidapi/hidapi/hidapi.h"
#include <cassert>
#include "MantaUSB.h"
#include "MantaExceptions.h"
#include <cstdlib>
#include <cstring>
#include <wchar.h>
MantaUSB::MantaUSB(void) :
SerialNumber(0),
DeviceHandle(NULL)
{
mantaList.push_back(this);
MantaIndex = int(mantaList.size());
DebugPrint("%s-%d: Manta %d initialized", __FILE__, __LINE__, MantaIndex);
}
MantaUSB::~MantaUSB(void)
{
Disconnect();
mantaList.remove(this);
if(mantaList.empty())
{
hid_exit();
}
}
bool MantaUSB::MessageQueued(void)
{
return GetQueuedTxMessage() != NULL;
}
/************************************************************************//**
* \brief Writes a USB transfer frame down to the Manta
* \param frame Pointer to the frame to be transmitted
* \param forceQueued Forces this message to be queued instead of merged
*
* WriteFrame() is meant to be called by the Manta subclass, which defines
* methods for the individual messages (setLED, etc). libmanta maintains a
* message queue that gets popped from in the HandleEvents() handler.
*
* The default behavior is that if a message is already queued up for a given
* Manta, subsequent message will be merged into the waiting message instead of
* being further queued (the queued frame will be the end result of all queued
* messages). forceQueued can be set to true to force the message to be queued
* as a separate message instead of being merged
*
* Note: Because WriteFrame() accesses the same message queue that
* HandleEvents() does, they should be protected from each other by a mutex on
* the application level if they're being called from parallel threads.
****************************************************************************/
void MantaUSB::WriteFrame(uint8_t *frame, bool forceQueued)
{
if(NULL == DeviceHandle)
{
throw(MantaNotConnectedException(this));
}
MantaTxQueueEntry *queuedMessage = GetQueuedTxMessage();
if(queuedMessage && !forceQueued)
{
/* replace the queued packet payload with the new one */
for(int i = 0; i < OutPacketLen; ++i)
{
/* the first byte of the report is the report ID (0x00) */
queuedMessage->OutFrame[i+1] = frame[i];
}
DebugPrint("%s-%d: (WriteFrame) Queued Transfer overwritten on Manta %d",
__FILE__, __LINE__, GetSerialNumber());
}
else
{
/* no transfer in progress, queue up a new one */
MantaTxQueueEntry *newMessage = new MantaTxQueueEntry;
newMessage->OutFrame[0] = 0;
newMessage->TargetManta = this;
/* the first byte of the report is the report ID (0x00) */
memcpy(newMessage->OutFrame + 1, frame, OutPacketLen);
txQueue.push_back(newMessage);
DebugPrint("%s-%d: (WriteFrame) Transfer Queued on Manta %d",
__FILE__, __LINE__, GetSerialNumber());
}
}
/************************************************************************//**
* \brief Queries connection status of the Manta
* \returns true if this instance is connected to a physical Manta, false
* if not
*
****************************************************************************/
bool MantaUSB::IsConnected(void)
{
return DeviceHandle != NULL;
}
/************************************************************************//**
* \brief Connects this instance to a Manta
* \param connectionSerial The serial number of the manta to search for.
*
* If connectionSerial is left out or given as 0 then any connected Manta will
* match. If a serial number is given then libmanta will attempt to connect to
* that Manta. If no matching manta is found then Connect will throw a
* MantaNotFoundException. If no exception is thrown then the connection can be
* assumed to have been successful.
*
****************************************************************************/
void MantaUSB::Connect(int connectionSerial)
{
#define SERIAL_STRING_SIZE 32
wchar_t serialString[SERIAL_STRING_SIZE];
if(IsConnected())
{
return;
}
DebugPrint("%s-%d: Attempting to Connect to Manta %d...",
__FILE__, __LINE__, connectionSerial);
if(connectionSerial)
{
swprintf(serialString, SERIAL_STRING_SIZE, L"%d", connectionSerial);
DeviceHandle = hid_open(VendorID, ProductID, serialString);
}
else
{
DeviceHandle = hid_open(VendorID, ProductID, NULL);
}
if(NULL == DeviceHandle)
throw(MantaNotFoundException());
hid_get_serial_number_string(DeviceHandle, serialString, SERIAL_STRING_SIZE);
SerialNumber = int(wcstol(serialString, NULL, 10));
int rc = hid_set_nonblocking(DeviceHandle, 1);
printf("hid_set_nonblocking %d\n", rc);
printf("SerialNumber %d\n", SerialNumber);
}
/************************************************************************//**
* \brief Disconnects this instance from an attached Manta
****************************************************************************/
void MantaUSB::Disconnect(void)
{
if(! IsConnected())
{
return;
}
DebugPrint("%s-%d: Manta %d Disconnecting...", __FILE__, __LINE__, GetSerialNumber());
hid_close(DeviceHandle);
DeviceHandle = NULL;
}
/************************************************************************//**
* \brief Services USB communciation with the Manta
*
* HandleEvents should be called periodically to poll all connected Mantas for
* incoming USB frames as well as to send any messages that have been queued
* up with WriteFrame(). It should be called at least once every 6ms, but you
* may get improved results polling as fast as every 1ms if your application
* supports it.
*
* Note: Because WriteFrame() accesses the same message queue that HandleEvents()
* does, they should be protected from each other by a mutex on the application
* level if they're being called from parallel threads.
****************************************************************************/
void MantaUSB::HandleEvents(void)
{
list<MantaUSB *>::iterator i = mantaList.begin();
/* read from each manta and trigger any events */
while(mantaList.end() != i)
{
MantaUSB *current = *i;
if(current->IsConnected())
{
int bytesRead;
int8_t inFrame[InPacketLen];
bytesRead = hid_read(current->DeviceHandle,
reinterpret_cast<uint8_t *>(inFrame), InPacketLen);
if(bytesRead < 0)
{
current->DebugPrint("%s-%d: Read error on Manta %d",
__FILE__, __LINE__, current->GetSerialNumber());
throw(MantaCommunicationException(current));
}
else if(bytesRead)
{
current->FrameReceived(inFrame);
}
}
++i;
}
/* pop one item off the transmit queue and send down to its target */
if(! txQueue.empty())
{
int bytesWritten;
MantaTxQueueEntry *txMessage = txQueue.front();
txQueue.pop_front();
bytesWritten = hid_write(txMessage->TargetManta->DeviceHandle,
txMessage->OutFrame, OutPacketLen + 1);
txMessage->TargetManta->DebugPrint("%s-%d: Frame Written to Manta %d",
__FILE__, __LINE__, txMessage->TargetManta->GetSerialNumber());
for(int j = 0; j < 16; j += 8)
{
uint8_t *frame = txMessage->OutFrame + 1;
txMessage->TargetManta->DebugPrint("\t\t%x %x %x %x %x %x %x %x",
frame[j], frame[j+1], frame[j+2], frame[j+3], frame[j+4],
frame[j+5], frame[j+6], frame[j+7]);
}
delete txMessage;
if(bytesWritten < 0)
{
txMessage->TargetManta->DebugPrint("%s-%d: Write error on Manta %d",
__FILE__, __LINE__, txMessage->TargetManta->GetSerialNumber());
throw(MantaCommunicationException(txMessage->TargetManta));
}
}
}
/************************************************************************//**
* \brief Queries the serial number of the attached Manta
*
* \returns The serial number as an int
****************************************************************************/
int MantaUSB::GetSerialNumber(void)
{
return SerialNumber;
}
/************************************************************************//**
* \brief Returns the Hardware Version (1 or 2) of the attached Manta
*
* \returns The hardware version
****************************************************************************/
int MantaUSB::GetHardwareVersion(void)
{
return (SerialNumber < 70) ? 1 : 2;
}
MantaUSB::MantaTxQueueEntry *MantaUSB::GetQueuedTxMessage()
{
list<MantaTxQueueEntry *>::iterator i = txQueue.begin();
/* look for the first queued message matching this manta */
while(txQueue.end() != i)
{
if((*i)->TargetManta == this)
{
return *i;
}
++i;
}
return NULL;
}
/* define static class members */
list<MantaUSB *> MantaUSB::mantaList;
list<MantaUSB::MantaTxQueueEntry *> MantaUSB::txQueue;