Эх сурвалжийг харах

修改箭的蓝牙数据解析方式

lvjincheng 4 жил өмнө
parent
commit
1c2c14d77e

+ 6 - 6
Assets/BowArrow/Scenes/Game.unity

@@ -7849,7 +7849,7 @@ RectTransform:
   m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
   m_AnchorMin: {x: 0, y: 0}
   m_AnchorMax: {x: 0, y: 0}
-  m_AnchoredPosition: {x: 30, y: 0}
+  m_AnchoredPosition: {x: 0, y: 0}
   m_SizeDelta: {x: 0, y: 33}
   m_Pivot: {x: 0.5, y: 0.5}
 --- !u!114 &589339043
@@ -9938,7 +9938,7 @@ RectTransform:
   m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
   m_AnchorMin: {x: 0, y: 0}
   m_AnchorMax: {x: 0, y: 0}
-  m_AnchoredPosition: {x: 30, y: 0}
+  m_AnchoredPosition: {x: 0, y: 0}
   m_SizeDelta: {x: 0, y: 33}
   m_Pivot: {x: 0.5, y: 0.5}
 --- !u!114 &694545845
@@ -15869,7 +15869,7 @@ RectTransform:
   m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
   m_AnchorMin: {x: 0, y: 0}
   m_AnchorMax: {x: 0, y: 0}
-  m_AnchoredPosition: {x: 30, y: 0}
+  m_AnchoredPosition: {x: 0, y: 0}
   m_SizeDelta: {x: 0, y: 33}
   m_Pivot: {x: 0.5, y: 0.5}
 --- !u!114 &1218454288
@@ -17987,7 +17987,7 @@ RectTransform:
   m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
   m_AnchorMin: {x: 0, y: 0}
   m_AnchorMax: {x: 0, y: 0}
-  m_AnchoredPosition: {x: 50, y: 0}
+  m_AnchoredPosition: {x: 0, y: 0}
   m_SizeDelta: {x: 0, y: 33}
   m_Pivot: {x: 0.5, y: 0.5}
 --- !u!114 &1338115114
@@ -25982,7 +25982,7 @@ RectTransform:
   m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
   m_AnchorMin: {x: 0, y: 0}
   m_AnchorMax: {x: 0, y: 0}
-  m_AnchoredPosition: {x: 80, y: 0}
+  m_AnchoredPosition: {x: 0, y: 0}
   m_SizeDelta: {x: 0, y: 40}
   m_Pivot: {x: 0.5, y: 0.5}
 --- !u!114 &1845023679
@@ -29340,7 +29340,7 @@ MonoBehaviour:
   outlight: {fileID: 2100000, guid: 502f08d2109aaeb4d92b1b552bfed15c, type: 2}
   text1: {fileID: 1550855296}
   text2: {fileID: 202607251}
-  shootScaleValue: 0
+  shootScaleValue: 1
 --- !u!1 &2104225929
 GameObject:
   m_ObjectHideFlags: 0

+ 50 - 4
Assets/BowArrow/Scripts/Bluetooth/BluetoothShoot.cs

@@ -21,6 +21,7 @@ public class BluetoothShoot : MonoBehaviour
 
     void Start() {
         ins = this;
+        BluetoothDispatcher.shoot = handleDataBytes;
     }
 
     void OnDestroy()
@@ -137,10 +138,7 @@ public class BluetoothShoot : MonoBehaviour
                 byte[] bytes = value;
                 // Log(String.Join(",", bytes));
                 BluetoothClient.UploadData(1, bytes);
-                if (ShootCheck.ins)
-                {
-                    ShootCheck.ins.OnDataReceived(bytes);
-                }
+                handleDataBytes(bytes);
             };
             bluetoothHelper.OnScanEnded += (BluetoothHelper helper, LinkedList<BluetoothDevice> nearbyDevices) =>
             {
@@ -208,4 +206,52 @@ public class BluetoothShoot : MonoBehaviour
             textUI.text = text;
         }
     }
+
+    bool dataGetting = false;
+    int dataGetLen = 0;
+    List<byte> dataBytes = new List<byte>();
+
+    int TwoByteToInt(byte b1, byte b2) 
+    {
+        ushort twoByte = (ushort) (b1 * 256 + b2);
+        short shortNum = (short) twoByte;
+        return (int) shortNum; 
+    }
+
+    void cacheDataBytes(byte[] bytes, int startIndex) {
+        for (int i = startIndex; i < bytes.Length; i++) {
+            byte b = bytes[i];
+            if (dataBytes.Count < dataGetLen) {
+                dataBytes.Add(b);
+            } else {
+                if (ShootCheck.ins)  ShootCheck.ins.OnDataReceived(dataBytes.ToArray());
+                dataGetting = false;
+                dataBytes.Clear();
+                break;
+            }
+        }
+    }
+
+    public void handleDataBytes(byte[] bytes) {
+        if (dataGetting) {
+            cacheDataBytes(bytes, 0);
+        } else {
+            if (bytes.Length >= 5) {
+                int byte3Value = 0; //轴数据识别,1:x轴,2:y轴,3:z轴
+                if (ShootCheck.ins) {
+                    CMD cmd = ShootCheck.ins.cmd;
+                    if (cmd.a == "x") byte3Value = 1;
+                    if (cmd.a == "y") byte3Value = 2;
+                    if (cmd.a == "z") byte3Value = 3;
+                }
+                if (bytes[0] == 255 || bytes[1] == 255 && bytes[2] == byte3Value) {
+                    dataGetting = true;
+                    dataGetLen = TwoByteToInt(bytes[3], bytes[4]);
+                    cacheDataBytes(bytes, 5);
+                }
+            } else if (bytes.Length == 2) {
+                DeviceBatteryView.ins.RenderBattery(2, bytes[0]);
+            }
+        }
+    }
 }

+ 6 - 13
Assets/BowArrow/Scripts/Bluetooth/ShootCheck.cs

@@ -12,7 +12,7 @@ using BestHTTP.WebSocket;
 public class ShootCheck : MonoBehaviour
 {
     [SerializeField] Text text;
-    CMD cmd = new CMD();
+    public CMD cmd = new CMD();
     bool locked = false;
     float maxAcc = 0;
     Queue<Vector3> keyAccList = new Queue<Vector3>();
@@ -24,7 +24,6 @@ public class ShootCheck : MonoBehaviour
     void Start()
     {
         ins = this;
-        BluetoothDispatcher.shoot = OnDataReceived;   
     } 
 
  //用户输入时的变化
@@ -158,13 +157,7 @@ public class ShootCheck : MonoBehaviour
     }
 
     public void OnDataReceived(byte[] bytes) 
-    {
-        if (bytes.Length == 2) 
-        {
-            DeviceBatteryView.ins.RenderBattery(2, bytes[0]);
-            return;
-        }   
-        
+    {    
         string str1 = "byte=";
         if (webSocket != null)
         { 
@@ -180,11 +173,11 @@ public class ShootCheck : MonoBehaviour
 
         if (transportMode == 0)
         {
-            for (int i = 0; i < (bytes.Length -2 ) /6; i++)
+            for (int i = 0; i < bytes.Length / 6; i++)
             {
-                float acc = ToAcceleratedSpeed(bytes[i * 6 + 5], bytes[i * 6 + 6]);
+                float acc = ToAcceleratedSpeed(bytes[i * 6 + 4], bytes[i * 6 + 5]);
 
-                string t = "(采样时间:"+(int)bytes[i * 6 + 3] + "分"+ (int)bytes[i * 6 + 4]+"秒"+ TwoByteToInt(bytes[i * 6 + 1], bytes[i * 6 + 2])+"毫秒)" ;
+                string t = "(采样时间:"+(int)bytes[i * 6 + 0] + "分"+ (int)bytes[i * 6 + 1]+"秒"+ TwoByteToInt(bytes[i * 6 + 2], bytes[i * 6 + 3])+"毫秒)" ;
                 str2 += "加速度:"+ acc + t + "\n";
 
                 if (ins.check(0, acc, 0, t) && ArmBow.ins) 
@@ -350,7 +343,7 @@ public class ShootCheck : MonoBehaviour
 }
 
 [Serializable]
-class CMD {
+public class CMD {
     // public string ax = "y";
     // public int a = 6000;
     // public int r = 2;

+ 2 - 2
Assets/BowArrow/Scripts/Game/GameAssistUI.cs

@@ -126,7 +126,7 @@ public class GameAssistUI : MonoBehaviour
 
     // ------ 发射加速功能 ------
 
-    public int shootScaleValue = 0;
+    public int shootScaleValue = 1;
     Sequence seq2 = null;
 
     bool openScaleShoot()
@@ -152,7 +152,7 @@ public class GameAssistUI : MonoBehaviour
     }
     void closeScaleShoot()
     {
-        shootScaleValue = 0;
+        shootScaleValue = 1;
         if (GameDebug.ins) GameDebug.ins.caluculateAbsoluteAngle();
     }
 }