Created
June 10, 2012 07:59
-
-
Save hagino3000/2904435 to your computer and use it in GitHub Desktop.
Get neuro properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedInputStream; | |
import java.io.IOException; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import com.sun.jna.Pointer; | |
import com.sun.jna.ptr.*; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import org.zeromq.*; | |
/** Simple example of JNA interface mapping and usage. */ | |
public class EmoStateLog | |
{ | |
private static void send(String type, String value) { | |
try { | |
String query = java.net.URLEncoder.encode(value); | |
URL url = new URL("http://192.168.11.7:3000/data?val=" + query); | |
HttpURLConnection http = (HttpURLConnection)url.openConnection(); | |
http.setRequestMethod("GET"); | |
http.connect(); | |
BufferedInputStream bis = new BufferedInputStream( http.getInputStream() ); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
public static void main(String[] args) | |
{ | |
Pointer eEvent = Edk.INSTANCE.EE_EmoEngineEventCreate(); | |
Pointer eState = Edk.INSTANCE.EE_EmoStateCreate(); | |
IntByReference userID = null; | |
short composerPort = 1726; | |
int option = 1; | |
int state = 0; | |
userID = new IntByReference(0); | |
switch (option) { | |
case 1: | |
{ | |
if (Edk.INSTANCE.EE_EngineConnect("Emotiv Systems-5") != EdkErrorCode.EDK_OK.ToInt()) { | |
System.out.println("Emotiv Engine start up failed."); | |
return; | |
} | |
break; | |
} | |
case 2: | |
{ | |
System.out.println("Target IP of EmoComposer: [127.0.0.1] "); | |
if (Edk.INSTANCE.EE_EngineRemoteConnect("127.0.0.1", composerPort, "Emotiv Systems-5") != EdkErrorCode.EDK_OK.ToInt()) { | |
System.out.println("Cannot connect to EmoComposer on [127.0.0.1]"); | |
return; | |
} | |
System.out.println("Connected to EmoComposer on [127.0.0.1]"); | |
break; | |
} | |
default: | |
System.out.println("Invalid option..."); | |
return; | |
} | |
while (true) | |
{ | |
state = Edk.INSTANCE.EE_EngineGetNextEvent(eEvent); | |
// New event needs to be handled | |
if (state == EdkErrorCode.EDK_OK.ToInt()) { | |
int eventType = Edk.INSTANCE.EE_EmoEngineEventGetType(eEvent); | |
Edk.INSTANCE.EE_EmoEngineEventGetUserId(eEvent, userID); | |
// Log the EmoState if it has been updated | |
if (eventType == Edk.EE_Event_t.EE_EmoStateUpdated.ToInt()) { | |
try { | |
JSONObject currentState = new JSONObject(); | |
Edk.INSTANCE.EE_EmoEngineEventGetEmoState(eEvent, eState); | |
float timestamp = EmoState.INSTANCE.ES_GetTimeFromStart(eState); | |
//System.out.println(timestamp + " : New EmoState from user " + userID.getValue()); | |
//System.out.print("WirelessSignalStatus: "); | |
//System.out.println(EmoState.INSTANCE.ES_GetWirelessSignalStatus(eState)); | |
JSONArray actions = new JSONArray(); | |
FloatByReference eyeX = new FloatByReference(); | |
FloatByReference eyeY = new FloatByReference(); | |
EmoState.INSTANCE.ES_ExpressivGetEyeLocation(eState, eyeX, eyeY); | |
System.out.println(eyeX.getValue()); | |
System.out.println(eyeY.getValue()); | |
/* | |
if (EmoState.INSTANCE.ES_ExpressivIsBlink(eState) == 1) { | |
System.out.println("Blink"); | |
actions.put("Blink"); | |
} | |
if (EmoState.INSTANCE.ES_ExpressivIsLeftWink(eState) == 1) { | |
System.out.println("LeftWink"); | |
actions.put("Left Wink"); | |
} | |
if (EmoState.INSTANCE.ES_ExpressivIsRightWink(eState) == 1) { | |
System.out.println("RightWink"); | |
actions.put("Right Wink"); | |
} | |
if (EmoState.INSTANCE.ES_ExpressivIsLookingLeft(eState) == 1) { | |
System.out.println("LookingLeft"); | |
actions.put("Looking Left"); | |
} | |
if (EmoState.INSTANCE.ES_ExpressivIsLookingRight(eState) == 1) { | |
System.out.println("LookingRight"); | |
actions.put("Looking Right"); | |
} | |
if (EmoState.INSTANCE.ES_ExpressivIsLookingDown(eState) == 1) { | |
actions.put("Looking Down"); | |
} | |
currentState.put("actions", actions); | |
*/ | |
//System.out.print("ExcitementShortTerm: "); | |
//System.out.println(EmoState.INSTANCE.ES_AffectivGetExcitementShortTermScore(eState)); | |
currentState.put("Excitement ShortTerm", EmoState.INSTANCE.ES_AffectivGetExcitementShortTermScore(eState)); | |
//System.out.print("ExcitementLongTerm: "); | |
//System.out.println(EmoState.INSTANCE.ES_AffectivGetExcitementLongTermScore(eState)); | |
currentState.put("Excitement LongTerm", EmoState.INSTANCE.ES_AffectivGetExcitementLongTermScore(eState)); | |
//System.out.print("EngagementBoredom: "); | |
//System.out.println(EmoState.INSTANCE.ES_AffectivGetEngagementBoredomScore(eState)); | |
currentState.put("Engagement Boredom", EmoState.INSTANCE.ES_AffectivGetEngagementBoredomScore(eState)); | |
currentState.put("Frustration", EmoState.INSTANCE.ES_AffectivGetFrustrationScore(eState)); | |
System.out.println("Frustration:" + EmoState.INSTANCE.ES_AffectivGetFrustrationScore(eState)); | |
currentState.put("Meditation", EmoState.INSTANCE.ES_AffectivGetMeditationScore(eState)); | |
//System.out.print("CognitivGetCurrentAction: "); | |
//System.out.println(EmoState.INSTANCE.ES_CognitivGetCurrentAction(eState)); | |
currentState.put("CognitivGetCurrentAction", EmoState.INSTANCE.ES_CognitivGetCurrentAction(eState)); | |
//System.out.print("CurrentActionPower: "); | |
//System.out.println(EmoState.INSTANCE.ES_CognitivGetCurrentActionPower(eState)); | |
currentState.put("Current Action Power", EmoState.INSTANCE.ES_CognitivGetCurrentActionPower(eState)); | |
send("state", currentState.toString()); | |
} catch (JSONException jse) { | |
System.out.println(jse.toString()); | |
} | |
} | |
} | |
else if (state != EdkErrorCode.EDK_NO_EVENT.ToInt()) { | |
System.out.println("Internal error in Emotiv Engine!"); | |
break; | |
} | |
} | |
Edk.INSTANCE.EE_EngineDisconnect(); | |
System.out.println("Disconnected!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment