Jelajahi Sumber

Merge branch 'master' of http://81.70.224.233:10080/yichael/SmartBow

lvjincheng 4 tahun lalu
induk
melakukan
6937cdf442

+ 85 - 5
Assets/BowArrow/Scripts/Bluetooth/ShootCheck.cs

@@ -2,6 +2,7 @@
 using UnityEngine;
 using UnityEngine.UI;
 using ArduinoBluetoothAPI;
+using BestHTTP.WebSocket;
 
 public class ShootCheck : MonoBehaviour
 {
@@ -12,12 +13,78 @@ public class ShootCheck : MonoBehaviour
     public float shootSpeed;
     public static ShootCheck ins; 
 
+    WebSocket webSocket;
     void Start()
     {
         ins = this;
         BluetoothDispatcher.shoot = OnDataReceived;
+
+         //socket
+        string serverIP = "192.168.1.103";
+        string address = "ws://" + serverIP + ":8088/Ble/";
+        webSocket = new WebSocket(new Uri(address));
+
+#if !UNITY_WEBGL
+            webSocket.StartPingThread = true;
+#endif
+
+            // Subscribe to the WS events
+            webSocket.OnOpen += OnOpen;
+            webSocket.OnMessage += OnMessageRecv;
+            webSocket.OnBinary += OnBinaryRecv;
+            webSocket.OnClosed += OnClosed;
+            webSocket.OnError += OnError;
+            // Debug.Log("OnOpen: ");
+            // Start connecting to the server
+            webSocket.Open();
     } 
 
+//socket
+    public void Destroy() 
+    {
+        if (webSocket != null) 
+        {
+            webSocket.Close();
+            webSocket = null;
+        }
+    }
+
+    void OnOpen(WebSocket ws) 
+    {
+        // Debug.Log("OnOpen: ");
+        webSocket.Send("unity");
+    }
+
+    void OnMessageRecv(WebSocket ws, string message)
+    {
+        Debug.LogFormat("OnMessageRecv: msg={0}", message);
+    }
+
+    void OnBinaryRecv(WebSocket ws, byte[] data) 
+    {
+        Debug.LogFormat("OnBinaryRecv: len={0}", data.Length);
+    }
+
+    void OnClosed(WebSocket ws, UInt16 code, string message) 
+    {
+        Debug.LogFormat("OnClosed: code={0}, msg={1}", code, message);
+        webSocket = null;
+    }
+
+    void OnError(WebSocket ws, Exception ex)  
+    {
+        string errorMsg = string.Empty;
+#if !UNITY_WEBGL || UNITY_EDITOR
+            if (ws.InternalRequest.Response != null)
+            {
+                errorMsg = string.Format("Status Code from Server: {0} and Message: {1}", ws.InternalRequest.Response.StatusCode, ws.InternalRequest.Response.Message);
+            }
+#endif
+        Debug.LogFormat("OnError: error occured: {0}\n", (ex != null ? ex.Message : "Unknown Error " + errorMsg));
+        webSocket = null;
+    }
+//socket
+
     void OnDestroy()
     {
         ins = null;
@@ -29,17 +96,30 @@ public class ShootCheck : MonoBehaviour
 
 
     public void OnDataReceived(byte[] bytes) {
-        for (int i = 0; i < bytes.Length / 10; i++)
+
+        string str = "";
+
+        for (int i = 0; i < (bytes.Length-2)/6; i++)
         {
-            float acc = ToAcceleratedSpeed(bytes[i * 10 + 7], bytes[i * 10 + 8]);
+            // float acc = ToAcceleratedSpeed(bytes[i * 10 + 7], bytes[i * 10 + 8]);
+            // string t = "(采样时间:"+(int)bytes[i * 10 + 5] + "分"+ (int)bytes[i * 10 + 6]+"秒"+ TwoByteToInt(bytes[i * 10 + 3], bytes[i * 10 + 4])+"毫秒)" ;
+
+            float acc = ToAcceleratedSpeed(bytes[i * 6 + 5], bytes[i * 6 + 6]);
+
+            string t = "(采样时间:"+(int)bytes[i * 6 + 3] + "分"+ (int)bytes[i * 6 + 4]+"秒"+ TwoByteToInt(bytes[i * 6 + 1], bytes[i * 6 + 2])+"毫秒)" ;
+            str += "加速度:"+acc+t+"\n";
+            
+            // ts[3] = "(采样时间:"+(int)bytes[33] + "分"+ (int)bytes[34]+"秒"+ TwoByteToInt(bytes[31], bytes[32])+"毫秒)" ;
+
             if (ins.check(acc))
             {
                 if (ArmBow.ins != null)
                 {
-                    ArmBow.ins.ADS_fire();        
+                    ArmBow.ins.ADS_fire();   
+                    webSocket.Send(str);     
                 }
             }
-        }
+        }   
     }
 
     float ToAcceleratedSpeed(byte b1, byte b2)
@@ -68,7 +148,7 @@ public class ShootCheck : MonoBehaviour
             return false;
         } else if (acc < cmd.getAcc() && maxAcc != 0) {
             shootSpeed = maxAcc;
-            Log("最大加速度:" + maxAcc);
+            // Log("最大加速度:" + maxAcc);
             maxAcc = 0;
             Dolock();
             Invoke("Unlock", 0.8f);

+ 3 - 1
Assets/BowArrow/Scripts/Game/ArmBow.cs

@@ -125,7 +125,9 @@ public class ArmBow : MonoBehaviour
         arrowComp.armBow = this;
         // Arrow.speed = DebugBowPower.ins.getPowerPercent() * 100f;
         // Arrow.speed = BaseSpeedSlider.ins.getValue();
-        Arrow.speed = Mathf.Pow(ShootCheck.ins.shootSpeed, ShootCheck.ins.shootSpeed < 13 ? 2f: (ShootCheck.ins.shootSpeed < 15 ? 2.5f : 3.0f));
+        // Arrow.speed = Mathf.Pow(ShootCheck.ins.shootSpeed, ShootCheck.ins.shootSpeed < 13 ? 2f: (ShootCheck.ins.shootSpeed < 15 ? 2.5f : 3.0f));
+
+        Arrow.speed = ShootCheck.ins.shootSpeed/16*90; 
 
         arrowCopy.SetActive(true);  
         arrow.SetActive(false);