-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSDKSample.cpp
More file actions
343 lines (262 loc) · 7.79 KB
/
Copy pathSDKSample.cpp
File metadata and controls
343 lines (262 loc) · 7.79 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
// =STS=> SDKSample.cpp[4270].aa00 closed SMID:1
/////////////////////////////////////////////////////////////////////////////
//
// (c) Copyright 2011 - 2012 Blackrock Microsystems
//
// $Workfile: SDKSample.cpp $
// $Archive: /Cerebus/Human/WindowsApps/SDKSample/SDKSample.cpp $
// $Revision: 1 $
// $Date: 3/29/11 9:23a $
// $Author: Ehsan $
//
// $NoKeywords: $
//
/////////////////////////////////////////////////////////////////////////////
//
// PURPOSE: cbmex SDK example application
//
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <afxmt.h>
#include "SDKSample.h"
#include "SDKSampleDlg.h"
#include "Wincon.h"
#include "Dragonfly.h"
#include "message_defs.h"
#include <iostream>
#include <fstream>
#include <chrono>
#include <ctime>
#include<string.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// CSDKSampleApp
BEGIN_MESSAGE_MAP(CSDKSampleApp, CWinApp)
ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()
// CSDKSampleApp construction
class spikeDataHolder {
public:
int spikesRecieved;
const int maxSpikesStored = 500;
cbPKT_SPK spikeArray[500];
void saveSpike(ofstream* f, cbPKT_SPK spike) {
}
spikeDataHolder() {
spikesRecieved = 0;
}
};
bool testDragonflyMessaging(Dragonfly_Module* mod) {
MDF_TEST_DATA test_message;
test_message.a = 1;
test_message.b = 2;
CMessage msg(MT_TEST_DATA);
msg.SetData(&test_message, sizeof(test_message));
mod->SendMessageDF(&msg);
return true;
}
void connectToMessageLogger() {
MDF_SAVE_MESSAGE_LOG spike_message_log;
spike_message_log.pathname_length = 18;
char pathname[] = "C:\dragonfly_logs";
strcpy(spike_message_log.pathname, pathname);
}
void spikesCallback(UINT32 nInstacne, const cbSdkPktType type, const void* pEventData, void* pCallbackData)
{
spikeDataHolder * pDlg = reinterpret_cast<spikeDataHolder *>(pCallbackData);
//cbPKT_SPK spikesArray[500] = pDlg;
switch (type)
{
case cbSdkPkt_PACKETLOST:
break;
case cbSdkPkt_SPIKE:
if (pDlg && pEventData)
{
cbPKT_SPK spk = *reinterpret_cast<const cbPKT_SPK *>(pEventData);
// Note: Callback should return fast, so it is better to buffer it here
// and use another thread to handle data
//pDlg->AddSpike(spk);
//spikesArray[spikeIndex] = spk;
//pass to some array of spikes?
int index = pDlg->spikesRecieved;
if (index >= pDlg->maxSpikesStored){
index = 0;
pDlg->spikesRecieved = 0;
}
pDlg->spikesRecieved += 1;
pDlg->spikeArray[index] = spk;
}
break;
default:
break;
}
return;
}
void registerCallBack(spikeDataHolder* sData)
{
cbPKT_SPK spikesArray[500];
void* point= &spikesArray;
cbSdkResult res = cbSdkRegisterCallback(0, CBSDKCALLBACK_SPIKE, spikesCallback, point);
if (res != CBSDKRESULT_SUCCESS)
{
std::cout << res << std::endl;
return;
}
std::cout << "Successfully listening to the spikes" << std::endl;
}
void BindStdHandlesToConsole()
{
// Redirect the CRT standard input, output, and error handles to the console
freopen("CONIN$", "r", stdin);
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
//Clear the error state for each of the C++ standard stream objects. We need to do this, as
//attempts to access the standard streams before they refer to a valid target will cause the
//iostream objects to enter an error state. In versions of Visual Studio after 2005, this seems
//to always occur during startup regardless of whether anything has been read from or written to
//the console or not.
std::wcout.clear();
std::cout.clear();
std::wcerr.clear();
std::cerr.clear();
std::wcin.clear();
std::cin.clear();
}
void LaunchDebugConsolse() {
AllocConsole();
BindStdHandlesToConsole();
}
void initFileStreaming(ofstream* f) {
f->open("spikeData.txt");
std::chrono::time_point<std::chrono::system_clock> currentTime;
currentTime = std::chrono::system_clock::now();
std::time_t startTime = std::chrono::system_clock::to_time_t(currentTime);
*f << "Recording spike data at "<< std::ctime(&startTime);
//f->close();
}
void saveSpike(ofstream* f, cbPKT_SPK spk) {
*f << spk.time; //Time
*f << spk.chid << std::endl; //Channel
for (int i = 0; i < cbMAX_PNTS; i++) {
*f << spk.wave[i];
}
}
Dragonfly_Module * init_Dragonfly() {
Dragonfly_Module * mod = new Dragonfly_Module(MID_PRODUCER, 0);
cout << "Attempted to send dragonfly message" << std::endl;
mod->ConnectToMMM();
mod->Subscribe(MT_TEST_DATA);
mod->Subscribe(MT_EXIT);
std::cout << "Request running...\n" << std::endl;
return mod;
}
void send_Spike_Message(Dragonfly_Module* mod, MDF_SPIKE spikeMessage) {
CMessage msg(MT_SPIKE);
msg.SetData(&spikeMessage, sizeof(spikeMessage));
mod->SendMessageDF(&msg);
}
MDF_SPIKE createSpikeMessage(cbPKT_SPK spk) {
MDF_SPIKE spikeMessage;
spikeMessage.chid = spk.chid;
spikeMessage.time = spk.time;
return spikeMessage;
}
void init(){
LaunchDebugConsolse();
cbSdkVersion ver;
cbSdkResult res = cbSdkGetVersion(0, &ver);
std::cout << "Using Library " << ver.major<<"." <<ver.minor<<std::endl;
std::cout<< "Attempting to connect to NSP" << std::endl;
Dragonfly_Module * mod = init_Dragonfly();
//bool messaging_working = testMessaging(mod);
//cbSdkResult res = cbSdkOpen(0);
res = cbSdkOpen(0);
if (res != CBSDKRESULT_SUCCESS)
{
return;
}
cbSdkConnectionType conType;
cbSdkInstrumentType instType;
// Return the actual openned connection
res = cbSdkGetType(0, &conType, &instType);
if (res != CBSDKRESULT_SUCCESS)
{
return;
}
res = cbSdkGetVersion(0, &ver);
if (res != CBSDKRESULT_SUCCESS)
{
return;
}
if (conType < 0 || conType > CBSDKCONNECTION_CLOSED)
conType = CBSDKCONNECTION_CLOSED;
if (instType < 0 || instType > CBSDKINSTRUMENT_COUNT)
instType = CBSDKINSTRUMENT_COUNT;
res = cbSdkGetVersion(0, &ver);
CString strOld, strNew;
if (res == CBSDKRESULT_SUCCESS)
{
res = cbSdkGetType(0, &conType, &instType);
if (res == CBSDKRESULT_SUCCESS)
{
if (instType != CBSDKINSTRUMENT_LOCALNSP && instType != CBSDKINSTRUMENT_NSP)
strOld = "nPlay";
if (conType == CBSDKCONNECTION_CENTRAL)
strOld += "(Central)";
}
strNew.Format("%s v%u.%02u.%02u.%02u", strOld, ver.nspmajor, ver.nspminor, ver.nsprelease, ver.nspbeta);
std::cout << "Connected to" <<strNew<< std::endl;
}
else {
std::cout << "Failed to connect";
}
//cbSdkResult cbSdkSetChannelConfig(UINT32 nInstance, UINT16 channel, cbPKT_CHANINFO * chaninfo);
cbPKT_CHANINFO chaninfo;
UINT16 channel = 1;
res = cbSdkGetChannelConfig(0, channel, &chaninfo);
std::cout<<"Spike threshold " << chaninfo.spkthrlevel <<" "<< chaninfo.spkthrlimit<< std::endl;
spikeDataHolder* sData = new spikeDataHolder();
cout << "Spikeindex " << (*sData).spikesRecieved << std::endl;
std::cout << "Registering call back " << std::endl;
registerCallBack(sData);
ofstream* f = new ofstream();
initFileStreaming(f);
f->close();
}
CSDKSampleApp::CSDKSampleApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
// The one and only CExampleApp object
CSDKSampleApp theApp;
// CSDKSampleApp initialization
// Author & Date: Ehsan Azar 29 March 2011
// Purpose: Initialize SDK example application,
// and make sure at most one instance runs.
// Outputs:
// returns the error code
BOOL CSDKSampleApp::InitInstance()
{
CMutex cbSDKSampleAppMutex(TRUE, "cbSDKSampleAppMutex");
CSingleLock cbSDKSampleLock(&cbSDKSampleAppMutex);
// We let only one instance of nPlay GUI
if (!cbSDKSampleLock.Lock(0))
return FALSE;
init();
CWinApp::InitInstance();
CSDKSampleDlg dlg;
m_pMainWnd = &dlg;
dlg.DoModal();
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
//void main() {
//
// LaunchDebugConsolse();
// init();
//}