public void InitializeSubscriptionSocket(string topic) { if (!subscriptionSocketForTopic.ContainsKey(topic)) { subscriptionSocketForTopic.Add(topic, new SubscriberSocket(IPHeader + subport)); subscriptionSocketForTopic [topic].Subscribe(topic); //André: Is this necessary?? // subscriptionSocketForTopic[topic].Options.SendHighWatermark = PupilSettings.numberOfMessages;// 6; subscriptionSocketForTopic[topic].ReceiveReady += (s, a) => { int i = 0; NetMQMessage m = new NetMQMessage(); while (a.Socket.TryReceiveMultipartMessage(ref m)) { // We read all the messages from the socket, but disregard the ones after a certain point // if ( i > PupilSettings.numberOfMessages ) // 6) // continue; mStream = new MemoryStream(m[1].ToByteArray()); string msgType = m[0].ConvertToString(); if (PupilTools.Settings.debug.printMessageType) { Debug.Log(msgType); } if (PupilTools.Settings.debug.printMessage) { Debug.Log(MessagePackSerializer.ToJson(m[1].ToByteArray())); } switch (msgType) { case "notify.calibration.successful": PupilTools.Settings.calibration.currentStatus = Calibration.Status.Succeeded; PupilTools.CalibrationFinished(); Debug.Log(msgType); break; case "notify.calibration.failed": PupilTools.Settings.calibration.currentStatus = Calibration.Status.NotSet; PupilTools.CalibrationFailed(); Debug.Log(msgType); break; case "gaze": case "pupil.0": case "pupil.1": var dictionary = MessagePackSerializer.Deserialize <Dictionary <string, object> > (mStream); if (PupilTools.ConfidenceForDictionary(dictionary) > 0.6f) { if (msgType == "gaze") { PupilTools.gazeDictionary = dictionary; } else if (msgType == "pupil.0") { PupilTools.pupil0Dictionary = dictionary; } else if (msgType == "pupil.1") { PupilTools.pupil1Dictionary = dictionary; } } break; default: Debug.Log(msgType); // foreach (var item in MessagePackSerializer.Deserialize<Dictionary<string,object>> (mStream)) // { // Debug.Log(item.Key); // Debug.Log(item.Value.ToString()); // } break; } i++; } }; } }
public void InterpreteUDPData(byte[] data) { switch (data[0]) { // Connection established case (byte)'0': switch (data [1]) { case (byte)'I': UnityEngine.Debug.Log("Connection established"); PupilTools.IsConnected = true; break; case (byte)'i': UnityEngine.Debug.Log("Connection closed"); PupilTools.IsConnected = false; break; case (byte)'S': // Start gazing command received break; case (byte)'s': // Stop gazing command received break; default: UnityEngine.Debug.Log("Unknown response: " + (char)data[1]); break; } break; case (byte)'E': switch (data [1]) { case (byte)'C': if (data [2] == (byte)'S') // "notify.calibration.successful" { UnityEngine.Debug.Log("notify.calibration.successful"); PupilTools.CalibrationFinished(); } else if (data [2] == (byte)'F') // "notify.calibration.failed" { UnityEngine.Debug.Log("notify.calibration.failed"); PupilTools.CalibrationFailed(); } else { UnityEngine.Debug.Log("Unknown calibration ended event"); } break; case (byte)'G': if (data [2] == (byte)'2') { if (data [3] == (byte)'1') { //UnityEngine.Debug.Log("Left eye position received"); var leftEyePosition = FloatArrayFromPacket(data, 4); PupilData._2D.LeftEyePosUDP.x = leftEyePosition [0]; PupilData._2D.LeftEyePosUDP.y = leftEyePosition [1]; // UnityEngine.Debug.Log ("Left eye position: " + PupilData._2D.LeftEyePosUDP.ToString()); } else if (data [3] == (byte)'0') { //UnityEngine.Debug.Log("Right eye position received"); var rightEyePosition = FloatArrayFromPacket(data, 4); PupilData._2D.RightEyePosUDP.x = rightEyePosition [0]; PupilData._2D.RightEyePosUDP.y = rightEyePosition [1]; // UnityEngine.Debug.Log ("Right Eye Position: " + PupilData._2D.RightEyePosUDP.ToString()); } else if (data [3] == (byte)'2') { var gaze2DPosition = FloatArrayFromPacket(data, 4); PupilData._2D.Gaze2DPosUDP.x = gaze2DPosition [0]; PupilData._2D.Gaze2DPosUDP.y = gaze2DPosition [1]; // UnityEngine.Debug.Log ("Gazepoint 2D: " + PupilData._2D.Gaze2DPosUDP.ToString()); } else { UnityEngine.Debug.Log("Unknown gaze 2d data"); } } else if (data [2] == (byte)'3') { var gaze3DPosition = FloatArrayFromPacket(data, 4); PupilData._3D.Gaze3DPosUDP.x = gaze3DPosition [0] / PupilSettings.PupilUnitScalingFactor; PupilData._3D.Gaze3DPosUDP.y = gaze3DPosition [1] / PupilSettings.PupilUnitScalingFactor; PupilData._3D.Gaze3DPosUDP.z = gaze3DPosition [2] / PupilSettings.PupilUnitScalingFactor; // UnityEngine.Debug.Log ("Gazepoint 3D: " + PupilData._3D.Gaze3DPosUDP.ToString()); } else { UnityEngine.Debug.Log("Unknown gaze event"); } break; default: UnityEngine.Debug.Log("Unknown event"); break; } break; case 90: UnityEngine.Debug.Log("Start/stop calibration command"); if (data [1] == 1) { PupilTools.StartCalibration(); } else { PupilTools.StopCalibration(); } break; case 91: UnityEngine.Debug.Log("Forcing 2D calibration mode (Pupil version < 1 detected)"); PupilTools.CalibrationMode = Calibration.Mode._2D; break; default: UnityEngine.Debug.Log(StringFromPacket(data)); break; } }
public void InitializeSubscriptionSocket(string topic) { if (!subscriptionSocketForTopic.ContainsKey(topic)) { subscriptionSocketForTopic.Add(topic, new SubscriberSocket(IPHeader + subport)); subscriptionSocketForTopic[topic].Subscribe(topic); //André: Is this necessary?? // subscriptionSocketForTopic[topic].Options.SendHighWatermark = PupilSettings.numberOfMessages;// 6; subscriptionSocketForTopic[topic].ReceiveReady += (s, a) => { int i = 0; NetMQMessage m = new NetMQMessage(); while (a.Socket.TryReceiveMultipartMessage(ref m)) { // We read all the messages from the socket, but disregard the ones after a certain point // if ( i > PupilSettings.numberOfMessages ) // 6) // continue; string msgType = m[0].ConvertToString(); mStream = new MemoryStream(m[1].ToByteArray()); byte[] thirdFrame = null; if (m.FrameCount >= 3) { thirdFrame = m[2].ToByteArray(); } if (PupilSettings.Instance.debug.printMessageType) { Debug.Log(msgType); } if (PupilSettings.Instance.debug.printMessage) { Debug.Log(MessagePackSerializer.ToJson(m[1].ToByteArray())); } if (PupilTools.ReceiveDataIsSet) { PupilTools.ReceiveData(msgType, MessagePackSerializer.Deserialize <Dictionary <string, object> >(mStream), thirdFrame); // Uncomment if this is not the problem for skipping gazepoint //the continue block subscribing to multiple topics // continue; } switch (msgType) { case "notify.calibration.successful": PupilTools.CalibrationFinished(); Debug.Log(msgType); break; case "notify.calibration.failed": PupilTools.CalibrationFailed(); Debug.Log(msgType); break; case "gaze": case "gaze.2d.0.": case "gaze.2d.1.": case "pupil.0": var dictionary0 = MessagePackSerializer.Deserialize <Dictionary <string, object> >(mStream); confidence0 = PupilTools.FloatFromDictionary(dictionary0, "confidence"); if (PupilTools.IsCalibrating) { string eyeID = PupilTools.StringFromDictionary(dictionary0, "id"); PupilTools.UpdateCalibrationConfidence(eyeID, confidence0); break; } if ((confidence0 > confidenceThreshold) && msgType.StartsWith("gaze")) { PupilTools.gazeDictionary = dictionary0; } break; case "pupil.1": var dictionary = MessagePackSerializer.Deserialize <Dictionary <string, object> >(mStream); confidence1 = PupilTools.FloatFromDictionary(dictionary, "confidence"); if (PupilTools.IsCalibrating) { string eyeID = PupilTools.StringFromDictionary(dictionary, "id"); PupilTools.UpdateCalibrationConfidence(eyeID, confidence1); break; } if ((confidence1 > confidenceThreshold) && msgType.StartsWith("gaze")) { PupilTools.gazeDictionary = dictionary; } break; case "frame.eye.0": case "frame.eye.1": break; default: Debug.Log(msgType); break; } i++; } }; } }
public void InterpreteUDPData(byte[] data) { if (debuging) { Debug.Log("Receive data: " + Encoding.ASCII.GetString(data)); } switch (data[0]) { // Connection established case (byte)'0': switch (data [1]) { case (byte)'I': UnityEngine.Debug.Log("Connection established"); PupilTools.IsConnected = true; break; case (byte)'i': UnityEngine.Debug.Log("Connection closed"); PupilTools.IsConnected = false; break; case (byte)'S': // Start gazing command received break; case (byte)'s': // Stop gazing command received break; default: { UnityEngine.Debug.Log("Unknown response: " + (char)data[1]); debuging = false; } break; } break; case (byte)'E': switch (data [1]) { case (byte)'C': if (data [2] == (byte)'S') // "notify.calibration.successful" { UnityEngine.Debug.Log("notify.calibration.successful"); PupilTools.CalibrationFinished(); } else if (data [2] == (byte)'F') // "notify.calibration.failed" { UnityEngine.Debug.Log("notify.calibration.failed"); PupilTools.CalibrationFailed(); } else { UnityEngine.Debug.Log("Unknown calibration ended event"); } break; case (byte)'G': if (data [2] == (byte)'2') { if (data [3] == (byte)'1') { //UnityEngine.Debug.Log("Left eye position received"); PupilTools.UpdateGazePostion(PupilSettings.gaze2DLeftEyeKey, FloatArrayFromPacket(data, 4)); } else if (data [3] == (byte)'0') { //UnityEngine.Debug.Log("Right eye position received"); PupilTools.UpdateGazePostion(PupilSettings.gaze2DRightEyeKey, FloatArrayFromPacket(data, 4)); } else if (data [3] == (byte)'2') { PupilTools.UpdateGazePostion(PupilSettings.gaze2DKey, FloatArrayFromPacket(data, 4)); } else { UnityEngine.Debug.Log("Unknown gaze 2d data"); } } else if (data [2] == (byte)'3') { PupilTools.UpdateGazePostion(PupilSettings.gaze3DKey, FloatArrayFromPacket(data, 4)); } else { UnityEngine.Debug.Log("Unknown gaze event"); } break; default: UnityEngine.Debug.Log("Unknown event"); break; } break; case 90: UnityEngine.Debug.Log("Start/stop calibration command"); if (data [1] == 1) { PupilTools.StartCalibration(); } else { PupilTools.StopCalibration(); } break; case 91: UnityEngine.Debug.Log("Forcing 2D calibration mode (Pupil version < 1 detected)"); PupilTools.CalibrationMode = Calibration.Mode._2D; break; case (byte)'R': if (PupilSettings.Instance.debug.printSampling) { Debug.Log("Reference points received"); } break; default: UnityEngine.Debug.Log(StringFromPacket(data)); break; } }