-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNodeEPOCDriver.cc
432 lines (340 loc) · 14.6 KB
/
NodeEPOCDriver.cc
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/* This code is PUBLIC DOMAIN, and is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND. See the accompanying
* LICENSE file.
*/
#include <v8.h>
#include <node.h>
#include <sstream>
#include <fstream>
#include <iostream>
#include <map>
#include "edk.h"
#include "edkErrorCode.h"
#include "EmoStateDLL.h"
using namespace node;
using namespace v8;
using namespace std;
#define REQ_FUN_ARG(I, VAR) \
if (args.Length() <= (I) || !args[I]->IsFunction()) \
return ThrowException(Exception::TypeError( \
String::New("Argument " #I " must be a function"))); \
Local<Function> VAR = Local<Function>::Cast(args[I]);
#define REQ_STRING_ARG(I, VAR) \
if (args.Length() <= (I) || !args[I]->IsString()) \
return ThrowException(Exception::TypeError( \
String::New("Argument " #I " must be a string"))); \
String::Utf8Value VAR(args[I]->ToString());
class NodeEPOCDriver: ObjectWrap
{
private:
int connected;
unsigned int userID;
int run;
int profileLoaded;
int option;
string profileFile;
uv_loop_t* loop;
uv_loop_t* work;
uv_rwlock_t lock;
Persistent<Function> cb;
Persistent<Function> dcb;
public:
static Persistent<FunctionTemplate> s_ct;
static void Init(Handle<Object> target)
{
HandleScope scope;
Local<FunctionTemplate> t = FunctionTemplate::New(New);
s_ct = Persistent<FunctionTemplate>::New(t);
s_ct->InstanceTemplate()->SetInternalFieldCount(1);
s_ct->SetClassName(String::NewSymbol("NodeEPOCDriver"));
NODE_SET_PROTOTYPE_METHOD(s_ct, "connect", connect);
NODE_SET_PROTOTYPE_METHOD(s_ct, "disconnect", disconnect);
NODE_SET_PROTOTYPE_METHOD(s_ct, "rezero", rezero);
target->Set(String::NewSymbol("NodeEPOCDriver"),
s_ct->GetFunction());
}
NodeEPOCDriver() :
connected(0), userID(0), run(0), profileLoaded(0), option(0)
{
work = uv_loop_new();
loop = uv_default_loop();
uv_rwlock_init(&lock);
}
~NodeEPOCDriver()
{
uv_rwlock_destroy(&lock);
EE_EngineDisconnect();
}
struct baton_t {
NodeEPOCDriver *hw;
EmoEngineEventHandle* eEvent;
EmoStateHandle* eState;
bool update;
int gyroX;
int gyroY;
int cog;
float cog_power;
int blink;
int winkLeft;
int winkRight;
int lookLeft;
int lookRight;
float eyebrow;
float furrow;
float smile;
float clench;
float smirkLeft;
float smirkRight;
float laugh;
float shortTermExcitement;
float longTermExcitement;
float engagementOrBoredom;
float timestamp;
};
static Handle<Value> New(const Arguments& args)
{
HandleScope scope;
NodeEPOCDriver* hw = new NodeEPOCDriver();
hw->Wrap(args.This());
return args.This();
}
static Handle<Value> connect(const Arguments& args){
NodeEPOCDriver* hw = ObjectWrap::Unwrap<NodeEPOCDriver>(args.This());
REQ_STRING_ARG(0,param0);
REQ_FUN_ARG(1, cb);
uv_rwlock_wrlock(&hw->lock);
hw->profileFile = string(*param0);
hw->option = 0;
// get the option for engine or remote connect
if (args.Length()==3 && args[2]->IsInt32()){
Local<Integer> i = Local<Integer>::Cast(args[2]);
int selected = (int)(i->Int32Value());
if (selected != 0){
hw->option = 1;
}
}
hw->cb = Persistent<Function>::New(cb);
reconnect(hw);
uv_rwlock_wrunlock(&hw->lock);
return Undefined();
}
static void reconnect(NodeEPOCDriver* hw){
// read arg for option, engine or remote..
// read option for profile
while (hw->connected == 0){
switch(hw->option){
case 0: {
hw->connected = (EE_EngineConnect() == EDK_OK)? 1 : 0;
break;
}
case 1: {
hw->connected = (EE_EngineRemoteConnect("127.0.0.1",1726)==EDK_OK)? 1: 0;
break;
}
default: {break;}
}
cout << "7" <<endl;
}
if (hw->connected == 1){
cout << "connected to emotiv" <<endl;
uv_work_t *req = new uv_work_t();
req->data = hw;
hw->run=1;
int status = uv_queue_work(hw->loop, req, work_cb, after_work);
assert(status == 0);
cout << "starting epoc event loop" <<endl;
}
// uv_rwlock_wrunlock(&hw->lock);
}
static void loadUser(NodeEPOCDriver* hw){
bool errorOnce = true;
if (hw->profileLoaded == 0){
int error = EE_LoadUserProfile(hw->userID, hw->profileFile.c_str());
if (error == 0){
unsigned long activeActions = 0;
EE_CognitivGetActiveActions(hw->userID, &activeActions);
cout << "connected and opened profile '"<<hw->profileFile << "' with " << activeActions << " active actions" << endl;
hw->profileLoaded = 1;
} else if (errorOnce) {
cout << "condition: "<< error << ". either dongle is disconnected or could not find profile '" <<hw->profileFile << "'" <<endl;
}
errorOnce = false;
}
}
static void work_cb(uv_work_t* req){
NodeEPOCDriver *hw = static_cast<NodeEPOCDriver *>(req->data);
uv_rwlock_wrlock(&hw->lock);
loadUser(hw);
EE_HeadsetGyroRezero(hw->userID);
uv_rwlock_wrunlock(&hw->lock);
while (true){
uv_rwlock_rdlock(&hw->lock);
bool connected = (hw->connected == 1);
bool run = (hw->run == 1);
uv_rwlock_rdunlock(&hw->lock);
if (connected && run){
// read in the profile
EmoEngineEventHandle eEvent = EE_EmoEngineEventCreate();
EmoStateHandle eState = EE_EmoStateCreate();
int state = EE_EngineGetNextEvent(eEvent);
if (state == EDK_OK) {
EE_Event_t eventType = EE_EmoEngineEventGetType(eEvent);
// unsigned int userid = 0;
// EE_EmoEngineEventGetUserId(eEvent, &userid);
// EE_GetUserProfile(hw->userID, eEvent);
//EE_Event_t eventType = EE_EmoEngineEventGetType(eEvent);
if (eventType == EE_UserRemoved){
uv_rwlock_wrlock(&hw->lock);
hw->profileLoaded = 0;
uv_rwlock_wrunlock(&hw->lock);
cout << "the epoc dongle is disconnected, please reconnect" <<endl;
} else if (eventType == EE_UserAdded) {
uv_rwlock_wrlock(&hw->lock);
loadUser(hw);
uv_rwlock_wrunlock(&hw->lock);
} else if (eventType == EE_EmoStateUpdated){
EE_EmoEngineEventGetEmoState(eEvent, eState);
baton_t* baton = new baton_t();
baton->hw = hw;
baton->timestamp = ES_GetTimeFromStart(eState);
uv_rwlock_wrlock(&hw->lock);
EE_HeadsetGetGyroDelta(hw->userID, &baton->gyroX, &baton->gyroY);
uv_rwlock_wrunlock(&hw->lock);
baton->cog = static_cast<int>(ES_CognitivGetCurrentAction(eState));
baton->cog_power = ES_CognitivGetCurrentActionPower(eState);
baton->blink = ES_ExpressivIsBlink(eState);
baton->winkLeft = ES_ExpressivIsLeftWink(eState);
baton->winkRight = ES_ExpressivIsRightWink(eState);
baton->lookLeft = ES_ExpressivIsLookingLeft(eState);
baton->lookRight = ES_ExpressivIsLookingRight(eState);
std::map<EE_ExpressivAlgo_t, float> expressivStates;
EE_ExpressivAlgo_t upperFaceAction = ES_ExpressivGetUpperFaceAction(eState);
float upperFacePower = ES_ExpressivGetUpperFaceActionPower(eState);
EE_ExpressivAlgo_t lowerFaceAction = ES_ExpressivGetLowerFaceAction(eState);
float lowerFacePower = ES_ExpressivGetLowerFaceActionPower(eState);
expressivStates[ upperFaceAction ] = upperFacePower;
expressivStates[ lowerFaceAction ] = lowerFacePower;
baton->eyebrow= expressivStates[ EXP_EYEBROW ];
baton->furrow= expressivStates[ EXP_FURROW ]; // furrow
baton->smile= expressivStates[ EXP_SMILE ]; // smile
baton->clench= expressivStates[ EXP_CLENCH ]; // clench
baton->smirkLeft= expressivStates[ EXP_SMIRK_LEFT ]; // smirk left
baton->smirkRight= expressivStates[ EXP_SMIRK_RIGHT ]; // smirk right
baton->laugh= expressivStates[ EXP_LAUGH ]; // laugh
// Affectiv Suite results
baton->shortTermExcitement= ES_AffectivGetExcitementShortTermScore(eState);
baton->longTermExcitement= ES_AffectivGetExcitementLongTermScore(eState);
baton->engagementOrBoredom= ES_AffectivGetEngagementBoredomScore(eState);
baton->update = true;
// create an event to throw on queue for processing..
uv_work_t *req2 = new uv_work_t();
req2->data = baton;
int status = uv_queue_work(hw->loop, req2, process, after_process);
assert(status == 0);
}
}
EE_EmoStateFree(eState);
EE_EmoEngineEventFree(eEvent);
} else {
break;
}
}
}
static void after_work(uv_work_t* req) {
// baton_t *baton = static_cast<baton_t *>(req->data);
NodeEPOCDriver *hw = static_cast<NodeEPOCDriver *>(req->data);
cout << "after work loop" << endl;
uv_rwlock_wrlock(&hw->lock);
hw->profileLoaded = 0;
hw->userID = 0;
Local<Value> argv[0];
hw->cb.Dispose();
uv_rwlock_wrunlock(&hw->lock);
TryCatch try_catch;
if (hw->dcb != Null())
hw->dcb->Call(Context::GetCurrent()->Global(), 0, argv);
if (try_catch.HasCaught()) {
FatalException(try_catch);
}
}
static Handle<Value> disconnect(const Arguments& args){
REQ_FUN_ARG(0, dcb);
NodeEPOCDriver* hw = ObjectWrap::Unwrap<NodeEPOCDriver>(args.This());
uv_rwlock_rdlock(&hw->lock);
bool connected = (hw->connected == 1);
bool run = (hw->run ==1);
uv_rwlock_rdunlock(&hw->lock);
if (connected && run){
uv_rwlock_wrlock(&hw->lock);
if (hw->dcb != Null())
hw->dcb.Dispose();
hw->dcb = Persistent<Function>::New(dcb);
hw->run = 0;
uv_rwlock_wrunlock(&hw->lock);
cout << "disconnecting from epoc event loop" << endl;
}
return Undefined();
}
static Handle<Value> rezero(const Arguments& args){
NodeEPOCDriver* hw = ObjectWrap::Unwrap<NodeEPOCDriver>(args.This());
uv_rwlock_wrlock(&hw->lock);
bool connected = (hw->connected == 1);
bool run = (hw->run ==1);
if (connected && run){
cout << "rezero the gryo" << endl;
EE_HeadsetGyroRezero(hw->userID);
}
uv_rwlock_wrunlock(&hw->lock);
return Undefined();
}
static void process(uv_work_t* req)
{
}
static void after_process(uv_work_t* req) {
HandleScope scope;
baton_t *baton = static_cast<baton_t *>(req->data);
Local<Value> argv[1];
Local<Object> obj = Object::New();
if (baton->update){
// these will be the fields from the event
obj->Set(String::NewSymbol("time"), Number::New(baton->timestamp));
obj->Set(String::NewSymbol("userId"), Number::New(baton->hw->userID));
obj->Set(String::NewSymbol("gyroX"), Number::New(baton->gyroX));
obj->Set(String::NewSymbol("gyroY"), Number::New(baton->gyroY));
obj->Set(String::NewSymbol("wirelessSignalStatus"), Number::New(2));
obj->Set(String::NewSymbol("blink"), Number::New(baton->blink));
obj->Set(String::NewSymbol("winkLeft"), Number::New(baton->winkLeft));
obj->Set(String::NewSymbol("winkRight"), Number::New(baton->winkRight));
obj->Set(String::NewSymbol("lookLeft"), Number::New(baton->lookLeft));
obj->Set(String::NewSymbol("lookRight"), Number::New(baton->lookRight));
obj->Set(String::NewSymbol("eyebrow"), Number::New(baton->eyebrow));
obj->Set(String::NewSymbol("furrow"), Number::New(baton->furrow));
obj->Set(String::NewSymbol("smile"), Number::New(baton->smile));
obj->Set(String::NewSymbol("clench"), Number::New(baton->clench));
obj->Set(String::NewSymbol("smirkLeft"), Number::New(baton->smirkLeft));
obj->Set(String::NewSymbol("smirkRight"), Number::New(baton->smirkRight));
obj->Set(String::NewSymbol("laugh"), Number::New(baton->laugh));
obj->Set(String::NewSymbol("shortTermExcitement"), Number::New(baton->shortTermExcitement));
obj->Set(String::NewSymbol("longTermExcitement"), Number::New(baton->longTermExcitement));
obj->Set(String::NewSymbol("engagementOrBoredom"), Number::New(baton->engagementOrBoredom));
obj->Set(String::NewSymbol("cognitivAction"), Number::New(baton->cog));
obj->Set(String::NewSymbol("cognitivPower"), Number::New(baton->cog_power));
argv[0] = obj;
TryCatch try_catch;
if (baton->hw->cb != Null())
baton->hw->cb->Call(Context::GetCurrent()->Global(), 1, argv);
if (try_catch.HasCaught()) {
FatalException(try_catch);
}
}
delete baton;
}
};
Persistent<FunctionTemplate> NodeEPOCDriver::s_ct;
extern "C" {
static void init (Handle<Object> target)
{
NodeEPOCDriver::Init(target);
}
NODE_MODULE(emojs, init);
}