From 29aef943b9c4605a0ae16741921e229baf0dfc89 Mon Sep 17 00:00:00 2001 From: suntao Date: Tue, 31 Aug 2021 09:09:49 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8D=E9=80=9F=E5=BA=A6=E8=AE=A1?= =?UTF-8?q?=E5=92=8C=E8=B8=8F=E9=A2=91=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scripts/Devices/Ant/AntConnector.cs | 4 ++ Assets/Scripts/Devices/Ant/CadenceDevice.cs | 2 +- Assets/Scripts/Devices/Ant/SpeedDevice.cs | 59 +++++++++++++++++++ .../Devices/Ble/Devices/SpeedCadence.cs | 2 +- .../Devices/Ble/Win/WclBleMainThread.cs | 9 ++- .../Scripts/Devices/Ble/Win/WclBleManager.cs | 2 +- .../UI/Prefab/Device/ConnectDeviceModal.cs | 2 +- Assets/Scripts/UI/Prefab/Device/DeviceView.cs | 16 ++++- 8 files changed, 84 insertions(+), 12 deletions(-) create mode 100644 Assets/Scripts/Devices/Ant/SpeedDevice.cs diff --git a/Assets/Scripts/Devices/Ant/AntConnector.cs b/Assets/Scripts/Devices/Ant/AntConnector.cs index 40405e70..f2e63aaf 100644 --- a/Assets/Scripts/Devices/Ant/AntConnector.cs +++ b/Assets/Scripts/Devices/Ant/AntConnector.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using ANT_Managed_Library; +using UnityEngine; namespace Assets.Scripts.Devices.Ant { @@ -86,6 +87,7 @@ namespace Assets.Scripts.Devices.Ant deviceList.Add(new CadenceDevice("")); deviceList.Add(new HeartRateDevice("")); deviceList.Add(new BikeSpdCadDevice("")); + deviceList.Add(new SpeedDevice("")); var timer = new System.Timers.Timer(1000); timer.AutoReset = true; @@ -343,6 +345,7 @@ namespace Assets.Scripts.Devices.Ant case SensorType.None: break; case SensorType.Cadence: + Debug.LogError("发现踏频设备"+id); device = new CadenceDevice(id); break; case SensorType.HeartRate: @@ -352,6 +355,7 @@ namespace Assets.Scripts.Devices.Ant device = new PowerDevice(id); break; case SensorType.Speed: + device = new SpeedDevice(id); break; case SensorType.SpeedCadence: device = new BikeSpdCadDevice(id); diff --git a/Assets/Scripts/Devices/Ant/CadenceDevice.cs b/Assets/Scripts/Devices/Ant/CadenceDevice.cs index 1d5c631a..e6ca3b48 100644 --- a/Assets/Scripts/Devices/Ant/CadenceDevice.cs +++ b/Assets/Scripts/Devices/Ant/CadenceDevice.cs @@ -29,7 +29,7 @@ namespace Assets.Scripts.Devices.Ant private RotationData _rotationData = new RotationData(); public CadenceDevice(string id) - : base(id, "Ant+ Cadence", racerSportType.Biking, SensorType.Cadence) + : base(id, "Ant+ Cadence", racerSportType.Unknown, SensorType.Cadence) { Priority = 2; //if (speedSensor.isInUse) diff --git a/Assets/Scripts/Devices/Ant/SpeedDevice.cs b/Assets/Scripts/Devices/Ant/SpeedDevice.cs new file mode 100644 index 00000000..f27cfafc --- /dev/null +++ b/Assets/Scripts/Devices/Ant/SpeedDevice.cs @@ -0,0 +1,59 @@ +using ANT_Managed_Library; +using Assets.Scripts.Devices.Ant.Interfaces; +using Assets.Scripts.Devices.Ant.LegacyPages; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Assets.Scripts.Devices.Ant +{ + public class SpeedDevice : AbstractAntDevice, ISpeedDevice + { + public double Speed { get; set; } + private DateTime now = DateTime.Now; + public const double DEFAULT_WHEEL_CIRCUMFERENCE_m = 2096; //Average 700cx23mm road tire + public readonly double wheelCircumfrence_m; + + + private RotationData _rotationData = new RotationData(); + public SpeedDevice(string id, double wheelCircumfrence_m = DEFAULT_WHEEL_CIRCUMFERENCE_m) + : base(id, "Ant+ Speed", racerSportType.Unknown, SensorType.Speed) + { + Priority = 2; + this.wheelCircumfrence_m = wheelCircumfrence_m; + } + + public override void handleChannelResponse(ANT_Response response) + { + //throw new NotImplementedException(); + var page = response.messageContents.Skip(1).ToArray(); + int? value = _rotationData.CalculateRpm(now, page[4] | page[5] << 8, page[6] | page[7] << 8); + if (value.HasValue) + { + Speed = DoubleExtensions.CalculateSpeed(value.Value, wheelCircumfrence_m); + } + else + { + Speed = 0; + } + Console.WriteLine(Math.Round(Speed, 1)); + } + + protected override AntChannelProfile getDefaultSearchProfile() + { + //throw new NotImplementedException(); + + return new AntChannelProfile() + { + rfOffset = 57, + transType = 0, + deviceType = 123, + deviceNumber = 0, + messagePeriod = 8118, + pairingEnabled = false + }; + } + } +} diff --git a/Assets/Scripts/Devices/Ble/Devices/SpeedCadence.cs b/Assets/Scripts/Devices/Ble/Devices/SpeedCadence.cs index 504a4fd0..b9eff14d 100644 --- a/Assets/Scripts/Devices/Ble/Devices/SpeedCadence.cs +++ b/Assets/Scripts/Devices/Ble/Devices/SpeedCadence.cs @@ -16,7 +16,7 @@ namespace Assets.Scripts.Devices.Ble.Devices public int Cadence { get => _cyclingSpeedCadenceMeasurement.Cadence; set => throw new NotImplementedException(); } public double Speed { get => _cyclingSpeedCadenceMeasurement.Speed; set => throw new NotImplementedException(); } - private double _wheelCircumference; + private double _wheelCircumference = 2096; private List Services; public SpeedCadence(BlePeripheralInfo peripheralInfo, BleWinHwInterface bleWinHwInterface) : base(peripheralInfo, bleWinHwInterface, Ant.SensorType.SpeedCadence) { diff --git a/Assets/Scripts/Devices/Ble/Win/WclBleMainThread.cs b/Assets/Scripts/Devices/Ble/Win/WclBleMainThread.cs index 78d461e6..0d5ecdcb 100644 --- a/Assets/Scripts/Devices/Ble/Win/WclBleMainThread.cs +++ b/Assets/Scripts/Devices/Ble/Win/WclBleMainThread.cs @@ -80,7 +80,7 @@ namespace Assets.Scripts.Devices.Ble.Win { this.wclWatcher.StopScan(); } - //this.StopGattThreads(); + this.StopGattThreads(); if (this.wclBleManager.Active) { this.wclBleManager.ManagerStatusChanged = null; @@ -120,7 +120,7 @@ namespace Assets.Scripts.Devices.Ble.Win }); } - public override bool Stop() + public void StopGattThreads() { Debug.Log("停止thread"); foreach (var item in this.gattClients.Values) @@ -128,9 +128,8 @@ namespace Assets.Scripts.Devices.Ble.Win item.Stop(); } - wclBleManager.Dispose(); - - return true; + //wclBleManager.Dispose(); + //return true; } public string Test() diff --git a/Assets/Scripts/Devices/Ble/Win/WclBleManager.cs b/Assets/Scripts/Devices/Ble/Win/WclBleManager.cs index 8c6178ee..9522745c 100644 --- a/Assets/Scripts/Devices/Ble/Win/WclBleManager.cs +++ b/Assets/Scripts/Devices/Ble/Win/WclBleManager.cs @@ -157,7 +157,7 @@ namespace Assets.Scripts.Ble // manager, // status //}, WclBleManager.log); - callback(manager, status); + callback?.Invoke(manager, status); } // Token: 0x04001390 RID: 5008 diff --git a/Assets/Scripts/UI/Prefab/Device/ConnectDeviceModal.cs b/Assets/Scripts/UI/Prefab/Device/ConnectDeviceModal.cs index ecb1ca01..d995b825 100644 --- a/Assets/Scripts/UI/Prefab/Device/ConnectDeviceModal.cs +++ b/Assets/Scripts/UI/Prefab/Device/ConnectDeviceModal.cs @@ -142,7 +142,7 @@ public class ConnectDeviceModal : PFUIPanel { case SensorType.SpeedCadence: case SensorType.Cadence: - devices = App.MainDeviceAdapter.GetDevices().Where(d => d.Sensor == SensorType || (d is ICadenceDevice && d.State == DeviceState.Connected)).OrderBy(d=>d.Priority).ToList(); + devices = App.MainDeviceAdapter.GetDevices().Where(d => (d.Sensor == SensorType.Cadence || d.Sensor == SensorType.SpeedCadence) || (d is ICadenceDevice && d.State == DeviceState.Connected)).OrderBy(d=>d.Priority).ToList(); break; case SensorType.HeartRate: devices = App.MainDeviceAdapter.GetDevices().Where(d => d.Sensor == SensorType || (d is IHeartRateDevice && d.State == DeviceState.Connected)).OrderBy(d => d.Priority).ToList(); diff --git a/Assets/Scripts/UI/Prefab/Device/DeviceView.cs b/Assets/Scripts/UI/Prefab/Device/DeviceView.cs index fcb139fd..c4511ae7 100644 --- a/Assets/Scripts/UI/Prefab/Device/DeviceView.cs +++ b/Assets/Scripts/UI/Prefab/Device/DeviceView.cs @@ -227,9 +227,15 @@ public class DeviceView : MonoBehaviour logo.sprite = sprite0; noDevice.text = "NO DEVICE"; - searchState.text = "Searching..."; - - var hasDevice = App.MainDeviceAdapter.GetDevices().Any(d => d.Sensor == SensorType); + searchState.text = "Searching..."; + + var hasDevice = false; + if (SensorType == SensorType.SpeedCadence) { + hasDevice = App.MainDeviceAdapter.GetDevices().Any(d => d.Sensor == SensorType.SpeedCadence || d.Sensor == SensorType.Cadence); + } + else { + hasDevice = App.MainDeviceAdapter.GetDevices().Any(d => d.Sensor == SensorType); + } if (hasDevice) { mSearchButton.SetActive(false); @@ -366,6 +372,10 @@ public class DeviceView : MonoBehaviour AbstractDevice GetCurrentDevice() { //return AntConnector.Instance().discoveredDevices.FirstOrDefault(d => (d.State == DeviceState.Connected || d.State == DeviceState.Connecting) && d.Sensor == SensorType); + if(SensorType == SensorType.SpeedCadence || SensorType == SensorType.Cadence) + { + return App.MainDeviceAdapter.GetDevices().FirstOrDefault(d => (d.State == DeviceState.Connected || d.State == DeviceState.Connecting) && (d.Sensor == SensorType.SpeedCadence || d.Sensor == SensorType.Cadence)); + } return App.MainDeviceAdapter.GetDevices().FirstOrDefault(d => (d.State == DeviceState.Connected || d.State == DeviceState.Connecting) && d.Sensor == SensorType); }