Pārlūkot izejas kodu

修改串口模式下 BowCamera.isTouchMode

slambb 1 gadu atpakaļ
vecāks
revīzija
81867d731a

+ 62 - 45
Assets/BowArrow/Scripts/ArrowSerialPort.cs

@@ -10,7 +10,8 @@ public class ArrowSerialPort : MonoBehaviour
     private int baudrate = 115200;
     private string PortName = "/dev/ttyS0";
     private static Dictionary<string, int> _isInit = new();
-    public bool testMode = false;
+    public bool testMode = true;
+
     private void Awake()
     {
         if (!_isInit.TryGetValue(gameObject.name, out int instanceID) && instanceID == 0)
@@ -31,6 +32,7 @@ public class ArrowSerialPort : MonoBehaviour
     private void Init()
     {
         _isInit[gameObject.name] = GetInstanceID();
+
 #if UNITY_ANDROID && !UNITY_EDITOR
         serialPortUtility = gameObject.GetComponent<SerialPortUtilityPro>();
         serialPortUtility.OpenMethod = openMode;
@@ -68,10 +70,11 @@ public class ArrowSerialPort : MonoBehaviour
         {
             SerialPortHelper.ins.OnConnect(arg0.DeviceName);
             //应用启动 间隔十秒请求一次设备信息
+            CancelInvoke("RequestDeviceIno");
             InvokeRepeating("RequestDeviceIno", 0f, 10f);
             LOG($"{PortName} 串口打开成功!");
-            // 关闭一次摄像头
-            RequestLightState(false);
+            //激光默认打开
+            RequestLightState(true);
         }
         else if (msg.Equals("CLOSED"))//串口断开
         {
@@ -90,19 +93,35 @@ public class ArrowSerialPort : MonoBehaviour
             _isInit.Remove(gameObject.name);
         }
     }
-    
+
     /// <summary>
     /// 串口读取二进制流数据(界面挂载调用)
     /// </summary>
     /// <param name="data"></param>
     public void ReadStreamingBinary(object data)
     {
-        PhraseData(data as byte[]);   
+        var realData = new List<byte>();
+        var tempData = data as byte[];
+        for (int i = 0; i < tempData.Length; i++)
+        {
+            realData.Add(tempData[i]);
+            if (tempData.Length > i + 1)
+            {
+                if (tempData[i + 1] == 0XAA) //多条消息 粘包位置
+                {
+                    //把上一条发送
+                    PhraseData(realData.ToArray());
+                    realData.Clear();
+                }
+            }
+            else
+                PhraseData(realData.ToArray());
+        }
     }
 
     public void TestRead(byte[] bytes)
     {
-        PhraseData(bytes);
+        ReadStreamingBinary(bytes);
     }
 
     /// <summary>
@@ -166,7 +185,6 @@ public class ArrowSerialPort : MonoBehaviour
 
     #region APP请求
     // 异或校验内容:命令+长度+数据内容
-
     /// <summary>
     /// 设置激光状态
     /// </summary>
@@ -202,11 +220,11 @@ public class ArrowSerialPort : MonoBehaviour
         data.Add(0x85);//异或校验
         data.Add(0x55);//结束码
         serialPortUtility?.Write(data.ToArray());
-
+        Debug.Log("发送心跳包");
         DevicesHolder.ins?.ShowConnectTip();
     }
     #endregion
-    
+
     #region 返回消息处理
     /// <summary>
     /// 弹夹消息
@@ -233,37 +251,36 @@ public class ArrowSerialPort : MonoBehaviour
     {
         Debug.Log($"{PortName} 收到设备信息响应!");
         var check = bytes[1] + bytes[2] + bytes[3] + bytes[4];//校验:命令+长度+数据内容
-        //if (check != bytes[5])
-        //    LOG("OnDeviceInfoBack 数据校验错误!");
-        //else
-        //{
-            //0x01 HOUYI Pro
-            //0x02 ARTEMIS Pro
-            //0x03 Pistol 1
-            var deviceType = bytes[3];//设备类型
-            Debug.Log("设备类型 Device Type: " + deviceType);
-            switch (deviceType)
-            {
-                case 0x01:
-                    UserSettings.ins.selectDevicesName = "HOUYI Pro";
-                    DevicesHolder.ins.SwitchDeviceByType(AimDeviceType.HOUYIPRO);
-                    break;
-                case 0x02:
-                    UserSettings.ins.selectDevicesName = "ARTEMIS Pro";
-                    DevicesHolder.ins.SwitchDeviceByType(AimDeviceType.ARTEMISPRO);
-                    break;
-                case 0x03:
-                    UserSettings.ins.selectDevicesName = "Pistol M9";
-                    DevicesHolder.ins.SwitchDeviceByType(AimDeviceType.Gun);
-                    break;
-            }
-            UserSettings.ins.Save();
-            //刷新界面
-            var setting = FindAnyObjectByType<CustomUIView.BoxUserSettings>();
-            setting?.FlushDeviceSelect();
-        //}
+        if (HomeView.ins == null) {
+            return;
+        }
+        //0x01 HOUYI Pro
+        //0x02 ARTEMIS Pro
+        //0x03 Pistol 1
+        var deviceType = bytes[3];//设备类型
+        Debug.Log("设备类型 Device Type: " + deviceType);
+        switch (deviceType)
+        {
+            case 0x01:
+                UserSettings.ins.selectDevicesName = "HOUYI Pro";
+                DevicesHolder.ins.SwitchDeviceByType(AimDeviceType.HOUYIPRO);
+                break;
+            case 0x02:
+
+                UserSettings.ins.selectDevicesName = "ARTEMIS Pro";
+                DevicesHolder.ins.SwitchDeviceByType(AimDeviceType.ARTEMISPRO);
+                break;
+            case 0x03:
+                UserSettings.ins.selectDevicesName = "Pistol M9";
+                DevicesHolder.ins.SwitchDeviceByType(AimDeviceType.Gun);
+                break;
+        }
+        UserSettings.ins.Save();
+        //刷新界面
+        var setting = FindAnyObjectByType<CustomUIView.BoxUserSettings>();
+        setting?.FlushDeviceSelect();
     }
-    
+
     /// <summary>
     /// 射击消息
     /// </summary>
@@ -272,11 +289,11 @@ public class ArrowSerialPort : MonoBehaviour
         LOG($"{PortName} 收到设备射击消息!");
 
         var check = bytes[1] + bytes[2] + bytes[3] + bytes[4];//校验:命令+长度+数据内容
-        //if (check != bytes[5])
-        //{
-        //    LOG("OnDeviceShoot 数据校验错误!");
-        //}else
-            SerialPortHelper.shoot?.Invoke(bytes);
+                                                              //if (check != bytes[5])
+                                                              //{
+                                                              //    LOG("OnDeviceShoot 数据校验错误!");
+                                                              //}else
+        SerialPortHelper.shoot?.Invoke(bytes);
     }
 
     /// <summary>
@@ -299,6 +316,6 @@ public class ArrowSerialPort : MonoBehaviour
             RequestDeviceIno();
         SerialPortHelper.aim?.Invoke(bytes);
     }
-    
+
     #endregion
 }

+ 10 - 1
Assets/BowArrow/Scripts/Bluetooth/DevicesHolder.cs

@@ -51,8 +51,17 @@ public class DevicesHolder : MonoBehaviour
         bConnected = true;
         //打开图标
         SimulateMouseController.ins?.SetBleConnected(true);
+        //打靶和打猎需要设置为false
+        BowCamera.isTouchMode = false;
         //切换后算连接成功
         PopupMgr.ins.ClearAllTip();
+
+        //这里判断一下如果是当前连接的不用重复调用设置了
+        if (AimHandler.ins.aimDeviceInfos.arry.Count != 0) {
+            AimDeviceInfo aimDeviceInfo = AimHandler.ins.aimDeviceInfos.arry[0];
+            if (aimDeviceInfo.type == (int)deviceType) return;
+        }
+
         //PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("BConnectTipSuccess"));
         //1.默认选择第一个玩家(这里沿用了之前的设置信息代码,和蓝牙无关)
         BluetoothAim.ins.setBLEPlayer(BluetoothPlayer.FIRST_PLAYER);
@@ -81,7 +90,7 @@ public class DevicesHolder : MonoBehaviour
         Debug.Log("[DevicesHolder]重新初始化时候 bInitMac :" + AimHandler.ins.aimDeviceInfo.bInitMac);
 
         //切换 GlobalData.MyDeviceMode 
-        BluetoothAim.ins.SetMainConnectDeviceType();
+        if (HomeView.ins) BluetoothAim.ins.SetMainConnectDeviceType();
 
 
     }