适配速度计和踏频计

This commit is contained in:
suntao 2021-08-31 09:09:49 +08:00
parent f14a071179
commit 29aef943b9
8 changed files with 84 additions and 12 deletions

View File

@ -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);

View File

@ -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)

View File

@ -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
};
}
}
}

View File

@ -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<BleServiceInfo> Services;
public SpeedCadence(BlePeripheralInfo peripheralInfo, BleWinHwInterface bleWinHwInterface) : base(peripheralInfo, bleWinHwInterface, Ant.SensorType.SpeedCadence)
{

View File

@ -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()

View File

@ -157,7 +157,7 @@ namespace Assets.Scripts.Ble
// manager,
// status
//}, WclBleManager.log);
callback(manager, status);
callback?.Invoke(manager, status);
}
// Token: 0x04001390 RID: 5008

View File

@ -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();

View File

@ -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);
}