-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcom_thalmic_myo_Hub.cpp
More file actions
139 lines (126 loc) · 4.45 KB
/
com_thalmic_myo_Hub.cpp
File metadata and controls
139 lines (126 loc) · 4.45 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
#include "com_thalmic_myo_Hub.h"
#include "handle.h"
#include "jnidevicelistener.hpp"
#include <map>
#include <myo/myo.hpp>
std::map<jint, JniDeviceListener*> deviceListenerMap;
std::map<myo::Myo*, jobject> myoMap;
JavaVM *javavm;
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
setbuf(stdout, NULL);
javavm = vm;
return JNI_VERSION_1_6;
}
/*
* Class: com_thalmic_myo_Hub
* Method: initialize
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_com_thalmic_myo_Hub_initialize (JNIEnv *jenv, jobject thisObject, jstring applicationIdentifier) {
try {
const char* mystring = jenv->GetStringUTFChars(applicationIdentifier, NULL);
myo::Hub *hub = new myo::Hub(mystring);
setHandle(jenv, thisObject, hub);
} catch (const std::exception& e) {
jclass exceptionClass = jenv->FindClass("java/lang/RuntimeException");
jenv->ThrowNew(exceptionClass, e.what());
}
}
/*
* Class: com_thalmic_myo_Hub
* Method: waitForMyo
* Signature: (I)Lcom/thalmic/myo/Myo;
*/
JNIEXPORT jobject JNICALL Java_com_thalmic_myo_Hub_waitForMyo(JNIEnv *jenv, jobject thisObject, jint timeout) {
myo::Hub *hub = getHandle<myo::Hub>(jenv, thisObject);
myo::Myo *myo = hub->waitForMyo(timeout);
if (!myo) {
return NULL;
}
jclass cls = jenv->FindClass("com/thalmic/myo/Myo");
jmethodID initMethodID = jenv->GetMethodID(cls, "<init>", "()V");
jobject javaMyoObject = jenv->NewGlobalRef(jenv->NewObject(cls, initMethodID));
setHandle(jenv, javaMyoObject, myo);
myoMap[myo] = javaMyoObject;
return javaMyoObject;
}
/*
* Class: com_thalmic_myo_Hub
* Method: addListener
* Signature: (Lcom/thalmic/myo/DeviceListener;)V
*/
JNIEXPORT void JNICALL Java_com_thalmic_myo_Hub_addListener(JNIEnv *jenv, jobject thisObject, jobject javaDeviceListener) {
myo::Hub *hub = getHandle<myo::Hub>(jenv, thisObject);
if (hub != NULL) {
jmethodID hashCodeMethodId = jenv->GetMethodID(jenv->GetObjectClass(javaDeviceListener), "hashCode", "()I");
jint javaDeviceListenerHashCode = jenv->CallIntMethod(javaDeviceListener, hashCodeMethodId);
if (deviceListenerMap.count(javaDeviceListenerHashCode) == 0) {
jobject newJavaObject = jenv->NewGlobalRef(javaDeviceListener);
JniDeviceListener *jniDeviceListener = new JniDeviceListener(javavm, newJavaObject, myoMap);
deviceListenerMap[javaDeviceListenerHashCode] = jniDeviceListener;
hub->addListener(jniDeviceListener);
}
}
}
/*
* Class: com_thalmic_myo_Hub
* Method: removeListener
* Signature: (Lcom/thalmic/myo/DeviceListener;)V
*/
JNIEXPORT void JNICALL Java_com_thalmic_myo_Hub_removeListener(JNIEnv *jenv, jobject thisObject, jobject javaDeviceListener) {
myo::Hub *hub = getHandle<myo::Hub>(jenv, thisObject);
if (hub != NULL) {
jmethodID hashCodeMethodId = jenv->GetMethodID(jenv->GetObjectClass(javaDeviceListener), "hashCode", "()I");
jint javaDeviceListenerHashCode = jenv->CallIntMethod(javaDeviceListener, hashCodeMethodId);
JniDeviceListener *jniDeviceListener = deviceListenerMap[javaDeviceListenerHashCode];
if (hub != NULL && jniDeviceListener != NULL) {
hub->removeListener(jniDeviceListener);
deviceListenerMap.erase(javaDeviceListenerHashCode);
}
}
}
/*
* Class: com_thalmic_myo_Hub
* Method: run
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_com_thalmic_myo_Hub_run(JNIEnv *jenv, jobject thisObject, jint duration) {
try {
myo::Hub *hub = getHandle<myo::Hub>(jenv, thisObject);
if (hub != NULL) {
hub->run(duration);
}
} catch (const std::exception& e) {
jclass exceptionClass = jenv->FindClass("java/lang/RuntimeException");
jenv->ThrowNew(exceptionClass, e.what());
}
}
/*
* Class: com_thalmic_myo_Hub
* Method: runOnce
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_com_thalmic_myo_Hub_runOnce(JNIEnv *jenv, jobject thisObject, jint duration) {
myo::Hub *hub = getHandle<myo::Hub>(jenv, thisObject);
if (hub != NULL) {
hub->runOnce(duration);
}
}
/*
* Class: com_thalmic_myo_Hub
* Method: setLockingPolicy
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_com_thalmic_myo_Hub_setLockingPolicy(JNIEnv *jenv, jobject thisObject, jint lockingPolicy) {
myo::Hub *hub = getHandle<myo::Hub>(jenv, thisObject);
try {
if (lockingPolicy == 0) {
hub->setLockingPolicy(myo::Hub::lockingPolicyNone);
} else if (lockingPolicy == 1) {
hub->setLockingPolicy(myo::Hub::lockingPolicyStandard);
}
} catch (const std::exception &e) {
jclass exceptionClass = jenv->FindClass("java/lang/RuntimeException");
jenv->ThrowNew(exceptionClass, e.what());
}
}