public void OnPhontrollerDataPacket(PhontrollerDataPacket data) { // get the device PhontrollerDevice device = loadDevice(data.id); device.SetData(data); }
public override void OnUdpMessage(string s) { PhontrollerDataPacket data = new PhontrollerDataPacket(); // split into sub-messages (seperated by newline) String[] msgs = s.Split('\n'); foreach (String nextmsg in msgs) { string msg = nextmsg; // All messages start with "<method code>,<protocol version number>" //Debug.Log ("msg: " + msg); String[] words = msg.Split(':'); if (words.Length > 0) { String[] parts = words[0].Split(','); if (parts.Length > 0) { string methodCode = parts[0]; int methodVersion = 0; if (parts.Length > 1) { int.TryParse(parts[1], out methodVersion); } DecodeDataMessage(methodCode, methodVersion, words, ref data); } } } if (data.validInfo) { OnPhontrollerDataPacket(data); } }
// Received a new message to decode void DecodeDataMessage(string methodCode, int methodVersion, String[] words, ref PhontrollerDataPacket data) { string[] parts; // info if (methodCode == "i") { if (words.Length > 1) { parts = words[1].Split(','); int id = Convert.ToInt16(parts[0]); double time = Convert.ToDouble(parts[1]); string deviceId = ""; string sessionId = ""; long sequenceNumber = -1; // Debug.Log ("Time = " + time); if (parts.Length > 2) { deviceId = parts[2]; sessionId = parts[3]; sequenceNumber = Convert.ToInt64(parts[4]); } data.id = id; data.deviceTime = time; data.deviceId = deviceId; data.sessionId = sessionId; data.sequenceNumber = sequenceNumber; data.validInfo = true; } } // rotation else if (methodCode == "q") { if (words.Length > 1) { parts = words[1].Split(','); float x = Convert.ToSingle(parts[0]); float y = Convert.ToSingle(parts[1]); float z = Convert.ToSingle(parts[2]); float w = Convert.ToSingle(parts[3]); data.rotation = new Quaternion(x, y, z, w); data.validRotation = true; } } // accelerometer else if (methodCode == "a") { if (words.Length > 1) { parts = words[1].Split(','); if (parts.Length >= 3) { data.acceleration = new Vector3(Convert.ToSingle(parts[0]), Convert.ToSingle(parts[1]), Convert.ToSingle(parts[2])); data.validAcceleration = true; } } } // Touches else if (methodCode == "t") { if (words.Length > 1) { // update our fingers array for (int i = 0; i < data.Touches.Length && i < words.Length - 2; i++) { int word = i+2; if (words[word].Length == 0) { data.Touches[i] = new PhontrollerDataPacket.TouchPoint(){isPressed = false, position = Vector2.zero}; } else { String[] values = words[word].Split(','); data.Touches[i] = new PhontrollerDataPacket.TouchPoint(){isPressed = true, position = new Vector2(Convert.ToSingle(values[0]), Convert.ToSingle(values[1]))}; } } String[] size = words[1].Split(','); float w = Convert.ToSingle(size[0]); float h = Convert.ToSingle(size[1]); data.TouchesMaxWidth = w; data.TouchesMaxHeight = h; data.validTouches = true; } } // buttons else if (methodCode == "b") { if (words.Length > 1) { parts = words[1].Split(','); if (parts.Length >= 2) { data.buttonsVolumeUp = parts[0] == "1"; data.buttonsVolumeDown = parts[1] == "1"; data.validButtons = true; } } } }
// Received a new message to decode void DecodeDataMessage(string methodCode, int methodVersion, String[] words, ref PhontrollerDataPacket data) { string[] parts; // info if (methodCode == "i") { if (words.Length > 1) { parts = words[1].Split(','); int id = Convert.ToInt16(parts[0]); double time = Convert.ToDouble(parts[1]); string deviceId = ""; string sessionId = ""; long sequenceNumber = -1; // Debug.Log ("Time = " + time); if (parts.Length > 2) { deviceId = parts[2]; sessionId = parts[3]; sequenceNumber = Convert.ToInt64(parts[4]); } data.id = id; data.deviceTime = time; data.deviceId = deviceId; data.sessionId = sessionId; data.sequenceNumber = sequenceNumber; data.validInfo = true; } } // rotation else if (methodCode == "q") { if (words.Length > 1) { parts = words[1].Split(','); float x = Convert.ToSingle(parts[0]); float y = Convert.ToSingle(parts[1]); float z = Convert.ToSingle(parts[2]); float w = Convert.ToSingle(parts[3]); data.rotation = new Quaternion(x, y, z, w); data.validRotation = true; } } // accelerometer else if (methodCode == "a") { if (words.Length > 1) { parts = words[1].Split(','); if (parts.Length >= 3) { data.acceleration = new Vector3(Convert.ToSingle(parts[0]), Convert.ToSingle(parts[1]), Convert.ToSingle(parts[2])); data.validAcceleration = true; } } } // Touches else if (methodCode == "t") { if (words.Length > 1) { // update our fingers array for (int i = 0; i < data.Touches.Length && i < words.Length - 2; i++) { int word = i + 2; if (words[word].Length == 0) { data.Touches[i] = new PhontrollerDataPacket.TouchPoint() { isPressed = false, position = Vector2.zero }; } else { String[] values = words[word].Split(','); data.Touches[i] = new PhontrollerDataPacket.TouchPoint() { isPressed = true, position = new Vector2(Convert.ToSingle(values[0]), Convert.ToSingle(values[1])) }; } } String[] size = words[1].Split(','); float w = Convert.ToSingle(size[0]); float h = Convert.ToSingle(size[1]); data.TouchesMaxWidth = w; data.TouchesMaxHeight = h; data.validTouches = true; } } // buttons else if (methodCode == "b") { if (words.Length > 1) { parts = words[1].Split(','); if (parts.Length >= 2) { data.buttonsVolumeUp = parts[0] == "1"; data.buttonsVolumeDown = parts[1] == "1"; data.validButtons = true; } } } }