From 87a0e7fb6dce0153e312d0818dd42f184b23d932 Mon Sep 17 00:00:00 2001 From: lishuo Date: Fri, 10 Jun 2022 19:31:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=92=E8=88=B9=E6=9C=BA=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Scripts/Devices/Ble/BleDeviceAdapter.cs | 2 +- .../Ble/Characteristic/FtmsRowerData.cs | 27 +++-- .../Scripts/Devices/Ble/Devices/FtmsRower.cs | 2 +- .../Devices/Ble/mobile/BleMobileThread.cs | 110 ++++++++---------- Assets/Scripts/Devices/MainDeviceAdapter.cs | 12 ++ Assets/Scripts/UI/Control/PFUISlider.cs | 6 + .../UI/Prefab/Panel/RowerHomeScript.cs | 28 ++++- .../UI/Prefab/Rower/RowerDeviceView.cs | 2 + 8 files changed, 109 insertions(+), 80 deletions(-) diff --git a/Assets/Scripts/Devices/Ble/BleDeviceAdapter.cs b/Assets/Scripts/Devices/Ble/BleDeviceAdapter.cs index ef9e8d0c..fa91bacc 100644 --- a/Assets/Scripts/Devices/Ble/BleDeviceAdapter.cs +++ b/Assets/Scripts/Devices/Ble/BleDeviceAdapter.cs @@ -19,7 +19,7 @@ namespace Assets.Scripts.Devices.Ble private IDictionary discoveredDevices = new Dictionary(); - private IBleWinHwInterface hwInterface { get; set; } + public IBleWinHwInterface hwInterface { get; private set; } public BleDeviceAdapter(IBleWinHwInterface bleWinHwInterface) { diff --git a/Assets/Scripts/Devices/Ble/Characteristic/FtmsRowerData.cs b/Assets/Scripts/Devices/Ble/Characteristic/FtmsRowerData.cs index 9bfa2610..f109d75e 100644 --- a/Assets/Scripts/Devices/Ble/Characteristic/FtmsRowerData.cs +++ b/Assets/Scripts/Devices/Ble/Characteristic/FtmsRowerData.cs @@ -61,7 +61,7 @@ namespace Assets.Scripts.Devices.Ble.Characteristic { var device = App.MainDeviceAdapter.GetDevices().FirstOrDefault(d => (d.State == DeviceState.Connected) && d.Sensor == SensorType.Rower); if (device == null) return 0; - if (device.Name.Contains("Think")) + if (device.Name.Contains("Think") || device.Name.Contains("PF")) { return thinkRes; } @@ -93,6 +93,8 @@ namespace Assets.Scripts.Devices.Ble.Characteristic public event EventHandler PullChanged; //开始事件 public event EventHandler StartEvent; + //日志事件 + public event EventHandler LogEvent; private ushort _pullValue; public ushort PullValue { @@ -114,6 +116,7 @@ namespace Assets.Scripts.Devices.Ble.Characteristic } } private string[] deviceNamePool = new string[] {"Think","PF" }; + private List tempPullList = new List(); public void HandleAttributeReceived(byte[] data) { @@ -158,7 +161,8 @@ namespace Assets.Scripts.Devices.Ble.Characteristic output += $" 总距离{ vvv }米"; if (vvv < 10) { - App.cacheList.Add(DateTime.Now.ToString() + ":" + output); + LogEvent?.Invoke(DateTime.Now.ToString() + ":" + output,null); + //App.cacheList.Add(DateTime.Now.ToString() + ":" + output); } b += this.SizeOfDataForFlag(RowerDataFlag.TotalDistance); @@ -190,9 +194,11 @@ namespace Assets.Scripts.Devices.Ble.Characteristic } if (this.Flags.HasFlag(RowerDataFlag.ResistanceLevel)) { - this.commonRes = BitConvertHelper.ToInt16(data, b); + var newRes = BitConvertHelper.ToInt16(data, b); + var resChanged = newRes != this.commonRes; + this.commonRes = newRes; var device = App.MainDeviceAdapter.GetDevices().FirstOrDefault(d => (d.State == DeviceState.Connected) && d.Sensor == SensorType.Rower); - if (device != null && deviceNamePool.FirstOrDefault(x => device.Name.ToLower().Contains(x.ToLower())) == null && RowerResChanged != null) + if (resChanged && device != null && deviceNamePool.FirstOrDefault(x => device.Name.ToLower().Contains(x.ToLower())) == null && RowerResChanged != null) { RowerResChanged.Invoke(this.commonRes, null); } @@ -229,9 +235,11 @@ namespace Assets.Scripts.Devices.Ble.Characteristic } if (this.Flags.HasFlag(RowerDataFlag.ThinkDragFactor)) { + var originRes = BitConvertHelper.ToInt16(data, b + 1); + var thinkResChanged = originRes != this.thinkRes; this.thinkRes = BitConvertHelper.ToInt16(data, b + 1); var device = App.MainDeviceAdapter.GetDevices().FirstOrDefault(d => (d.State == DeviceState.Connected) && d.Sensor == SensorType.Rower); - if (device != null && deviceNamePool.FirstOrDefault(x=>device.Name.ToLower().Contains(x.ToLower()))!=null && RowerResChanged != null) + if (thinkResChanged && device != null && deviceNamePool.FirstOrDefault(x=>device.Name.ToLower().Contains(x.ToLower()))!=null && RowerResChanged != null) { RowerResChanged.Invoke(this.thinkRes, null); } @@ -239,17 +247,15 @@ namespace Assets.Scripts.Devices.Ble.Characteristic } if (this.Flags.HasFlag(RowerDataFlag.Pull)) { - List list = new List(); for (int pullB = 4; pullB < data.Length; pullB += 2) { var val = BitConverter.ToUInt16(data, pullB); val = Convert.ToUInt16(((double)val) /9.8f); - list.Add(val); + tempPullList.Add(val); PullValue = val; - } if (pullList == null) pullList = new List(); - foreach (var pull in list) + foreach (var pull in tempPullList) { if (pull == 0) { @@ -262,10 +268,11 @@ namespace Assets.Scripts.Devices.Ble.Characteristic } pullList.Add(pull); } + tempPullList.Clear(); //Debug.Log("拉力:" + string.Join(",",list)); } } - List pullList; + List pullList { get; set; } public void Reset() { this.StrokeRate = 0; diff --git a/Assets/Scripts/Devices/Ble/Devices/FtmsRower.cs b/Assets/Scripts/Devices/Ble/Devices/FtmsRower.cs index 63ffd1df..1779201c 100644 --- a/Assets/Scripts/Devices/Ble/Devices/FtmsRower.cs +++ b/Assets/Scripts/Devices/Ble/Devices/FtmsRower.cs @@ -190,7 +190,7 @@ namespace Assets.Scripts.Devices.Ble.Devices if (this.controlPointCharacteristic != null) { Debug.Log("发送重置命令" + this.controlPointCharacteristic.ToString()); - App.cacheList.Add(DateTime.Now.ToString() + ":发送重置命令" + this.controlPointCharacteristic.ToString()); + //App.cacheList.Add(DateTime.Now.ToString() + ":发送重置命令" + this.controlPointCharacteristic.ToString()); hwInterface.WriteCharacteristic(this.controlPointCharacteristic, new byte[] { 0x01 }); } } diff --git a/Assets/Scripts/Devices/Ble/mobile/BleMobileThread.cs b/Assets/Scripts/Devices/Ble/mobile/BleMobileThread.cs index 2fcfef8b..1c346c78 100644 --- a/Assets/Scripts/Devices/Ble/mobile/BleMobileThread.cs +++ b/Assets/Scripts/Devices/Ble/mobile/BleMobileThread.cs @@ -37,74 +37,14 @@ namespace Assets.Scripts.Devices.Ble } WclBleManagerStatus statusEnum = WclBleManagerStatus.RadioOff; internal BleMobileThread() { - var self = this; - //初始蓝牙 - BluetoothLEHardwareInterface.Initialize(true, false, () => { - statusEnum = WclBleManagerStatus.RadioOn; - managerInitialized?.Invoke(self); - }, - (error) => { - statusEnum = WclBleManagerStatus.RadioOff; - BluetoothLEHardwareInterface.Log("Error: " + error); - if (error.Contains("Bluetooth LE Not Enabled")) - BluetoothLEHardwareInterface.BluetoothEnable(true); - }, (status) => { - - statusEnum = WclBleManagerStatus.RadioOn; - switch (status) - { - case 13: - statusEnum = WclBleManagerStatus.RadioOff; - break; - case 11: - statusEnum = WclBleManagerStatus.RadioOn; - break; - } - if (managerStatusChanged != null) - { - managerStatusChanged.Invoke(self, statusEnum); - } - }); + Initialize();//初始蓝牙 } public void StartWatcher() { var self = this; if (statusEnum == WclBleManagerStatus.RadioOff) { - BluetoothLEHardwareInterface.BluetoothEnable(true); - BluetoothLEHardwareInterface.Initialize(true, false, () => - { - statusEnum = WclBleManagerStatus.RadioOn; - managerInitialized?.Invoke(self); - BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(ServiceUuids.GetServiceUuidList().ToArray(), null, (address, name, rssi, bytes) => - { - ScanInfoReceived?.Invoke(self, address, name, rssi, bytes); - }, true); - }, - (error) => - { - statusEnum = WclBleManagerStatus.RadioOff; - BluetoothLEHardwareInterface.Log("Error: " + error); - if (error.Contains("Bluetooth LE Not Enabled")) - BluetoothLEHardwareInterface.BluetoothEnable(true); - }, (status) => - { - - statusEnum = WclBleManagerStatus.RadioOn; - switch (status) - { - case 13: - statusEnum = WclBleManagerStatus.RadioOff; - break; - case 11: - statusEnum = WclBleManagerStatus.RadioOn; - break; - } - if (managerStatusChanged != null) - { - managerStatusChanged.Invoke(self, statusEnum); - } - }); + Initialize(); } else { @@ -114,6 +54,52 @@ namespace Assets.Scripts.Devices.Ble }, true); } } + public void Initialize() + { + var self = this; + BluetoothLEHardwareInterface.BluetoothEnable(true); + BluetoothLEHardwareInterface.Initialize(true, false, () => + { + statusEnum = WclBleManagerStatus.RadioOn; + managerInitialized?.Invoke(self); + BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(ServiceUuids.GetServiceUuidList().ToArray(), null, (address, name, rssi, bytes) => + { + ScanInfoReceived?.Invoke(self, address, name, rssi, bytes); + }, true); + }, + (error) => + { + statusEnum = WclBleManagerStatus.RadioOff; + BluetoothLEHardwareInterface.Log("Error: " + error); + if (error.Contains("Bluetooth LE Not Enabled")) + BluetoothLEHardwareInterface.BluetoothEnable(true); + }, (status) => + { + statusEnum = WclBleManagerStatus.RadioOn; + switch (status) + { + case 10: + statusEnum = WclBleManagerStatus.RadioOff; + break; + case 13: + statusEnum = WclBleManagerStatus.RadioOff; + break; + case 11: + statusEnum = WclBleManagerStatus.RadioOff; + break; + case 12: + statusEnum = WclBleManagerStatus.RadioOn; + break; + default: + statusEnum = WclBleManagerStatus.RadioOff; + break; + } + if (managerStatusChanged != null) + { + managerStatusChanged.Invoke(self, statusEnum); + } + }); + } public void Stop() { diff --git a/Assets/Scripts/Devices/MainDeviceAdapter.cs b/Assets/Scripts/Devices/MainDeviceAdapter.cs index 2cbe89e9..bc08f4a1 100644 --- a/Assets/Scripts/Devices/MainDeviceAdapter.cs +++ b/Assets/Scripts/Devices/MainDeviceAdapter.cs @@ -119,5 +119,17 @@ namespace Assets.Scripts.Devices str += "---------------------------\r\n"; Debug.Log(str); } + + public BleDeviceAdapter GetBleDeviceAdapter() + { + foreach (var item in adapters) + { + var bleadapter = (BleDeviceAdapter)item; + if (bleadapter != null) { + return bleadapter; + } + } + return null; + } } } diff --git a/Assets/Scripts/UI/Control/PFUISlider.cs b/Assets/Scripts/UI/Control/PFUISlider.cs index 2b473b9d..c394cc9d 100644 --- a/Assets/Scripts/UI/Control/PFUISlider.cs +++ b/Assets/Scripts/UI/Control/PFUISlider.cs @@ -51,6 +51,12 @@ public class PFUISlider : MonoBehaviour public void SetValue(float a) { slider.value = a; + slider.onValueChanged?.Invoke(a); + } + + public float GetValue() + { + return slider.value; } public List SetColorGradient(List list, int divideCount) diff --git a/Assets/Scripts/UI/Prefab/Panel/RowerHomeScript.cs b/Assets/Scripts/UI/Prefab/Panel/RowerHomeScript.cs index 8ebf57d9..b4b3aaaa 100644 --- a/Assets/Scripts/UI/Prefab/Panel/RowerHomeScript.cs +++ b/Assets/Scripts/UI/Prefab/Panel/RowerHomeScript.cs @@ -295,6 +295,8 @@ public class RowerHomeScript : PFUIPanel RowerData.PullChanged += PaintPullCurve; RowerData.StartEvent -= StartFunc; RowerData.StartEvent += StartFunc; + RowerData.RowerResChanged -= ResChanged; + RowerData.RowerResChanged += ResChanged; } rowerType = new RowerType { type = 1, value = 500 }; HandleSelectType(); @@ -464,6 +466,7 @@ public class RowerHomeScript : PFUIPanel int truelyTime = 0; private void StartFunc(object sender, EventArgs e) { + Debug.Log($"开始了:{openTimer}"); if (UIManager.Instance.confirm != null && UIManager.Instance.confirm.IsActive()) { print("当前有弹窗"); @@ -731,12 +734,16 @@ public class RowerHomeScript : PFUIPanel transform.Find("ResBar/BtnSub").GetComponent