forked from qdot/np_mindset
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnp_mindset.flext.cpp
More file actions
415 lines (364 loc) · 8.61 KB
/
np_mindset.flext.cpp
File metadata and controls
415 lines (364 loc) · 8.61 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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
* Implementation file for Neurosky Mindset Max/Pd External
*
* Copyright (c) 2009-2010 Kyle Machulis/Nonpolynomial Labs <kyle@nonpolynomial.com>
*
* More info on Nonpolynomial Labs @ http://www.nonpolynomial.com
*
* Project @ http://www.github.com/qdot/np_mindset
*
* Example code from flext tutorials. http://www.parasitaere-kapazitaeten.net/ext/flext/
*/
#ifndef FLEXT_THREADS
#define FLEXT_THREADS
#endif
// include flext header
#include <flext.h>
//god damnit, cycling '74.
#ifdef PI
#undef PI
#endif
// check for appropriate flext version
#if !defined(FLEXT_VERSION) || (FLEXT_VERSION < 400)
#error You need at least flext version 0.4.0
#endif
#include <iostream>
#include <string>
#include <boost/array.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/asio/serial_port.hpp>
#include "ThinkGearStreamParser.h"
class NeuroskyMindset
{
public:
NeuroskyMindset();
~NeuroskyMindset();
int open(std::string port_name);
int close();
int read();
// void dataPacketHandler( unsigned char extendedCodeLevel,
// unsigned char code,
// unsigned char numBytes,
// const unsigned char *value,
// void *customData );
protected:
boost::shared_ptr<ThinkGearStreamParser> m_streamParser;
boost::asio::io_service m_ioService;
boost::asio::serial_port m_serialPort;
};
void dataPacketHandler( unsigned char extendedCodeLevel,
unsigned char code,
unsigned char numBytes,
const unsigned char *value,
void *customData );
NeuroskyMindset::NeuroskyMindset() :
m_ioService(),
m_serialPort(m_ioService),
m_streamParser(new ThinkGearStreamParser())
{
}
NeuroskyMindset::~NeuroskyMindset()
{
}
int NeuroskyMindset::open(std::string port_name)
{
m_serialPort.open(port_name);
if(!m_serialPort.is_open())
return false;
m_serialPort.set_option(boost::asio::serial_port::baud_rate(57600));
return true;
}
int NeuroskyMindset::close()
{
m_serialPort.close();
}
int NeuroskyMindset::read()
{
std::vector<char> values(16);
boost::system::error_code connection_error;
m_serialPort.read_some(boost::asio::buffer(values), connection_error);
for(int i = 0; i < values.size(); ++i)
{
THINKGEAR_parseByte(m_streamParser.get(), values[i]);
}
}
class np_mindset:
// inherit from basic flext class
public flext_base,
public NeuroskyMindset
{
// obligatory flext header (class name,base class name)
FLEXT_HEADER(np_mindset,flext_base)
// Same as boost ScopedMutex, just using flext's mutex class.
class ScopedMutex
{
ScopedMutex() {}
public:
ScopedMutex(ThrMutex& tm)
{
m = &tm;
m->Lock();
}
~ScopedMutex()
{
m->Unlock();
}
private:
ThrMutex* m;
};
public:
// constructor
np_mindset() :
m_runThread(false),
m_attentionUpdated(false),
m_meditationUpdated(false),
m_connectionUpdated(false),
m_isOpen(false),
m_powerUpdated(false),
m_attentionValue(0),
m_meditationValue(0),
m_connectionQuality(0),
m_outputRaw(false),
m_rawLevel(0),
m_sleepTime(.002)
{
// external setup
AddInAnything("Command Input");
AddInInt("Toggle Raw Wave Output");
AddOutBang("Bangs on successful connection/command");
AddOutInt("Connection Quality Value");
AddOutInt("eSense Attention Value");
AddOutInt("eSense Meditation Value");
AddOutInt("Raw Value");
AddOutList("Wave Values (delta, theta, low-alpha, high-alpha, low-beta, high-beta, low-gamma, and mid-gamma)");
FLEXT_ADDBANG(0, mindset_output);
FLEXT_ADDMETHOD(1, mindset_raw);
FLEXT_ADDMETHOD_(0, "port", mindset_anything);
FLEXT_ADDMETHOD_(0, "start", mindset_start);
FLEXT_ADDMETHOD_(0, "stop", mindset_stop);
FLEXT_ADDMETHOD_(0, "close", mindset_close);
post("Neurosky Mindset External v1.1.6");
post("by Nonpolynomial Labs (http://www.nonpolynomial.com)");
post("Updates at http://www.github.com/qdot/np_mindset");
post("Compiled on " __DATE__ " " __TIME__);
THINKGEAR_initParser( m_streamParser.get(),
PARSER_TYPE_PACKETS,
dataPacketHandler,
(void*)this );
}
virtual void Exit()
{
mindset_close();
flext_base::Exit();
}
virtual ~np_mindset()
{
mindset_close();
}
void setAttention(int val)
{
if(m_attentionValue == val) return;
ScopedMutex m(m_mindsetMutex);
m_attentionValue = val;
m_attentionUpdated = true;
}
void setMeditation(int val)
{
if(m_meditationValue == val) return;
ScopedMutex m(m_mindsetMutex);
m_meditationValue = val;
m_meditationUpdated = true;
}
void setConnectionQuality(int val)
{
if(m_connectionQuality == val) return;
ScopedMutex m(m_mindsetMutex);
m_connectionQuality = val;
m_connectionUpdated = true;
}
void setPowerLevels(boost::array<unsigned int, 7> power_levels)
{
ScopedMutex m(m_mindsetMutex);
//This is guaranteed to pretty much always change
m_powerLevels = power_levels;
m_powerUpdated = true;
}
void setRawLevel(int raw_level)
{
ScopedMutex m(m_mindsetMutex);
//This is guaranteed to pretty much always change
m_rawLevel = raw_level;
}
protected:
int m_attentionValue;
int m_meditationValue;
int m_connectionQuality;
int m_rawLevel;
std::string m_portName;
boost::array<unsigned int, 7> m_powerLevels;
t_atom m_powerList[7];
volatile bool m_hasUpdated;
volatile bool m_runThread;
volatile bool m_outputRaw;
volatile float m_sleepTime;
bool m_attentionUpdated;
bool m_meditationUpdated;
bool m_connectionUpdated;
bool m_powerUpdated;
bool m_isOpen;
ThrMutex m_mindsetMutex;
ThrMutex m_threadMutex;
void mindset_close()
{
if(!m_isOpen)
{
post("np_mindset - Mindset not currently open");
return;
}
post("np_mindset - Closing Mindset");
if(!m_threadMutex.TryLock())
{
mindset_stop();
m_threadMutex.Lock();
}
m_threadMutex.Unlock();
close();
m_isOpen = false;
post("np_mindset - Closed Mindset");
}
void mindset_anything(const t_symbol *msg,int argc,t_atom *argv)
{
if(!strcmp(msg->s_name, "port"))
{
m_portName = GetString(argv[0]);
post("np_mindset - Port set to %s", m_portName.c_str());
ToOutBang(0);
return;
}
post("np_mindset - not a valid np_mindset message: %s", msg->s_name);
}
void mindset_raw(long t)
{
m_outputRaw = (t > 0);
ToOutBang(0);
}
void mindset_stop()
{
m_runThread = false;
StopThreads();
ToOutBang(0);
}
void mindset_start()
{
if(!m_threadMutex.TryLock())
{
post("np_mindset - Thread already running");
return;
}
m_threadMutex.Unlock();
if(m_portName == "")
{
post("np_mindset - Port not set. Please use port command to set port name.");
return;
}
ScopedMutex m(m_threadMutex);
post("np_mindset - Entering mindset thread");
if(!open(m_portName))
{
post("np_mindset - Cannot open device at %s, exiting", m_portName.c_str());
return;
}
post("np_mindset - Opened Device");
m_isOpen = true;
ToOutBang(0);
m_runThread = true;
while(!ShouldExit() && m_runThread)
{
read();
mindset_output();
Sleep(m_sleepTime);
}
close();
m_isOpen = false;
post("np_mindset - Exiting mindset thread");
}
void mindset_output()
{
//If we have nothing to update, don't even worry about locking
if(!m_connectionUpdated && !m_attentionUpdated && !m_meditationUpdated && !m_powerUpdated && !m_outputRaw)
return;
ScopedMutex m(m_mindsetMutex);
Lock();
if(m_connectionUpdated)
ToOutInt(1, m_connectionQuality);
if(m_attentionUpdated)
ToOutInt(2, m_attentionValue);
if(m_meditationUpdated)
ToOutInt(3, m_meditationValue);
if(m_outputRaw)
{
ToOutInt(4, m_rawLevel);
}
if(m_powerUpdated)
{
for(int i = 0; i < 7; ++i)
{
SetInt(m_powerList[i], m_powerLevels[i]);
}
ToOutList(5, 7, m_powerList);
}
m_attentionUpdated = false;
m_connectionUpdated = false;
m_meditationUpdated = false;
m_powerUpdated = false;
Unlock();
}
private:
FLEXT_CALLBACK_A(mindset_anything)
FLEXT_THREAD(mindset_start)
FLEXT_CALLBACK(mindset_stop)
FLEXT_CALLBACK(mindset_close)
FLEXT_CALLBACK(mindset_output)
FLEXT_CALLBACK_I(mindset_raw)
};
FLEXT_NEW("np_mindset", np_mindset)
void dataPacketHandler( unsigned char extendedCodeLevel,
unsigned char code,
unsigned char numBytes,
const unsigned char *value,
void *customData )
{
np_mindset* m = (np_mindset*)customData;
if(code == 0x02)
{
m->setConnectionQuality(value[0]);
}
else if (code == 0x04)
{
m->setAttention(value[0]);
}
else if (code == 0x05)
{
m->setMeditation(value[0]);
}
else if (code == 0x80)
{
short l = 0;
l = value[0] << 8 | value[1];
int a = l;
m->setRawLevel(a);
}
else if (code == 0x83)
{
boost::array<unsigned int, 7> b;
for(int i = 0; i < 7; ++i)
{
unsigned int l = 0;
l |= value[i * 3] << 16;
l |= value[(i * 3) + 1] << 8;
l |= value[(i * 3) + 2];
b[i] = l;
}
m->setPowerLevels(b);
}
}