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/Win/WclBleMainThread.cs b/Assets/Scripts/Devices/Ble/Win/WclBleMainThread.cs index c614d4a3..16cd571f 100644 --- a/Assets/Scripts/Devices/Ble/Win/WclBleMainThread.cs +++ b/Assets/Scripts/Devices/Ble/Win/WclBleMainThread.cs @@ -128,7 +128,7 @@ namespace Assets.Scripts.Devices.Ble.Win } } - public override bool Stop() + public void StopGattThreads() { Debug.Log("停止thread"); foreach (var item in this.gattClients.Values) @@ -136,9 +136,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 fa63ea0a..712e1af5 100644 --- a/Assets/Scripts/UI/Prefab/Device/ConnectDeviceModal.cs +++ b/Assets/Scripts/UI/Prefab/Device/ConnectDeviceModal.cs @@ -163,7 +163,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 e953a075..5f7b0beb 100644 --- a/Assets/Scripts/UI/Prefab/Device/DeviceView.cs +++ b/Assets/Scripts/UI/Prefab/Device/DeviceView.cs @@ -236,9 +236,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); @@ -385,6 +391,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); }