划船机暂存

This commit is contained in:
Caiyanpeng committed 2021-09-28 11:27:24 +08:00
1 parent 1c73701e73
commit c73545d912
164 files changed
+38281 -2181

No files matched your search

@@ -0,0 +1,14 @@
using Assets.Scripts.Devices.Ble.Characteristic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assets.Scripts.Devices.Ant.Interfaces
{
interface IRowerDevice
{
FtmsRowerData rowerData { get; }
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c4094cd7dbe5f1a489e9bdf431722ddc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+1
View File
@@ -38,6 +38,7 @@ namespace Assets.Scripts.Devices.Ant
SpeedCadence,
Trainer,
VirtualPower,
Rower
}
public enum NetworkType
@@ -44,5 +44,9 @@ namespace Assets.Scripts
{
return BitConverter.ToUInt32(bytes, startIndex);
}
public static uint ToUInt24(byte[] bytes, int startIndex)
{
return (uint)((int)bytes[startIndex] | (int)bytes[startIndex + 1] << 8 | (int)bytes[startIndex + 2] << 16);
}
}
}
+27 -8
View File
@@ -68,29 +68,48 @@ namespace Assets.Scripts.Devices.Ble
{
return;
}
if(device.SensorType == Ant.SensorType.Trainer)
if (device.SensorType == Ant.SensorType.Trainer)
{
//var device1 = new Ftms(device.Peripheral, hwInterface);
//discoveredDevices.Add(device.Peripheral.Address, device1);
//var device0 = new Ftms(device.Peripheral, hwInterface);
//discoveredDevices.Add(device.Peripheral.Address, device0);
var device1 = new Tacx(device.Peripheral, hwInterface);
discoveredDevices.Add(device.Peripheral.Address, device1);
if (discoveredDevices.ContainsKey(device.Peripheral.Address))
{
discoveredDevices[device.Peripheral.Address] = device1;
}
else
{
discoveredDevices.Add(device.Peripheral.Address, device1);
}
}
else if(device.SensorType == Ant.SensorType.HeartRate)
else if (device.SensorType == Ant.SensorType.HeartRate)
{
var device1 = new HeartRate(device.Peripheral, hwInterface);
discoveredDevices.Add(device.Peripheral.Address, device1);
}
else if(device.SensorType == Ant.SensorType.Power)
else if (device.SensorType == Ant.SensorType.Power)
{
var device1 = new CyclingPower(device.Peripheral, hwInterface);
discoveredDevices.Add(device.Peripheral.Address, device1);
}
else if(device.SensorType == Ant.SensorType.SpeedCadence)
else if (device.SensorType == Ant.SensorType.SpeedCadence)
{
var device1 = new SpeedCadence(device.Peripheral, hwInterface);
discoveredDevices.Add(device.Peripheral.Address, device1);
}
else if (device.SensorType == Ant.SensorType.Rower)
{
var device1 = new FtmsRower(device.Peripheral, hwInterface);
if (discoveredDevices.ContainsKey(device.Peripheral.Address))
{
discoveredDevices[device.Peripheral.Address] = device1;
}
else
{
discoveredDevices.Add(device.Peripheral.Address, device1);
}
}
else
{
return;
@@ -0,0 +1,344 @@
using Assets.Scripts.Ble;
using Assets.Scripts.Devices.Ble.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace Assets.Scripts.Devices.Ble.Characteristic
{
public class FtmsRowerData : ICharacteristic
{
/// <summary>
/// 划桨频率
/// </summary>
public uint StrokeRate { get; private set; } = 0;
/// <summary>
/// 划桨次数
/// </summary>
public UInt16 StrokeCount { get; private set; } = 0;
/// <summary>
/// 平均划桨频率
/// </summary>
public int AverageStrokeRate { get; private set; } = 0;
public UInt32 TotalDistance { get; private set; } = 0;
/// <summary>
/// 即时配速
/// </summary>
public UInt16 InstantaneousPace { get; private set; } = 0;
/// <summary>
/// 平均配速
/// </summary>
public UInt16 AveragePace { get; private set; } = 0;
public int InstantaneousPower { get; private set; } = 0;
public int AveragePower { get; private set; } = 0;
public int ResistanceLevel { get; private set; } = 0;
/// <summary>
/// 运行时间
/// </summary>
public int ElapsedTime { get; private set; } = 0;
public int TotalEnergy { get; private set; } = 0;
public int EnergyPerHour { get; private set; } = 0;
public int EnergyPerMinute { get; private set; } = 0;
public Guid Uuid => ServiceUuids.Characteristics.RowerData;
public Guid ServiceUuid => ServiceUuids.Get(ServiceUuids.Ftms).IdGuid;
public bool IsOptional => false;
public RowerDataFlag Flags;
//拉力相关
public event EventHandler PullChanged;
private ushort _pullValue;
public ushort PullValue
{
get { return _pullValue; }
set
{
_pullValue = value;
if (this.PullChanged != null)
{
this.PullChanged(this, new EventArgs());
}
}
}
public bool MoreDataFlag
{
get
{
return this.Flags.HasFlag(RowerDataFlag.MoreData);
}
}
public void HandleAttributeReceived(byte[] data)
{
if (data == null || data.Length < 2)
{
throw new ArgumentException("数据异常" + string.Join(",", data));
}
//throw new NotImplementedException();
//if(data[0] == 1)
//{
// return;
//}
var output = "";
output += "收到消息:" + string.Join("\t", data);
this.Flags = (RowerDataFlag)BitConverter.ToUInt16(data, 0);
int b = 2;
if (!this.MoreDataFlag)
{
//this.instantSpeed = new double?((double)BitConverter.ToUInt16(data, (int)b) * 0.01);
this.StrokeRate = (uint)Math.Round(BitConvertHelper.ToUInt8(data, b) / 2d);
b += 1;
this.StrokeCount = BitConvertHelper.ToUInt16(data, b);
b += 2;
b += this.SizeOfDataForFlag(RowerDataFlag.MoreData);
}
//平均浆频
if (this.Flags.HasFlag(RowerDataFlag.AverageStrokeRate))
{
var vvv = BitConvertHelper.ToUInt8(data, (int)b) / 2;
//Debug.WriteLine($"平均浆频:{ vvv }");
output += $" 平均浆频:{ vvv }";
this.AverageStrokeRate = vvv;
b += this.SizeOfDataForFlag(RowerDataFlag.AverageStrokeRate);
//output += $" 次数:{ BitConvertHelper.ToUInt16(data, b) }";
//b += 3;
}
if (this.Flags.HasFlag(RowerDataFlag.TotalDistance))
{
var vvv = BitConvertHelper.ToUInt24(data, (int)b);
output += $" 总距离{ vvv }米";
b += this.SizeOfDataForFlag(RowerDataFlag.TotalDistance);
this.TotalDistance = vvv;
}
if (this.Flags.HasFlag(RowerDataFlag.InstantaneousPace))
{
//this.InstantCadence = new int?((int)(BitConvertHelper.ToUInt16(data, (int)b) / 2));
this.InstantaneousPace = BitConvertHelper.ToUInt16(data, b);
b += this.SizeOfDataForFlag(RowerDataFlag.InstantaneousPace);
}
if (this.Flags.HasFlag(RowerDataFlag.AveragePace))
{
this.AveragePace = BitConvertHelper.ToUInt16(data, b);
b += this.SizeOfDataForFlag(RowerDataFlag.AveragePace);
}
if (this.Flags.HasFlag(RowerDataFlag.InstantPower))
{
var power = new int?((int)BitConvertHelper.ToInt16(data, (int)b));
output += $" 功率:{ power }";
this.InstantaneousPower = power.GetValueOrDefault(0);
b += this.SizeOfDataForFlag(RowerDataFlag.InstantPower);
}
if (this.Flags.HasFlag(RowerDataFlag.AveragePower))
{
//var power = new int?((int)BitConverter.ToInt16(data, (int)b));
this.AveragePower = BitConvertHelper.ToInt16(data, b);
b += this.SizeOfDataForFlag(RowerDataFlag.AveragePower);
}
if (this.Flags.HasFlag(RowerDataFlag.ResistanceLevel))
{
this.ResistanceLevel = BitConvertHelper.ToInt16(data, b);
b += this.SizeOfDataForFlag(RowerDataFlag.ResistanceLevel);
}
if (this.Flags.HasFlag(RowerDataFlag.ExpendedEnergy))
{
this.TotalEnergy = BitConvertHelper.ToInt16(data, b);
b += 2;
this.EnergyPerHour = BitConvertHelper.ToInt16(data, b);
b += 2;
this.EnergyPerMinute = data[b];
b += 1;
//b += this.SizeOfDataForFlag(RowerDataFlag.ExpendedEnergy);
}
if (this.Flags.HasFlag(RowerDataFlag.HeartRate))
{
//this.HeartRate = new int?((int)BitConverter.ToUInt8(data, (int)b));
//(int)data[(int)b];
b += this.SizeOfDataForFlag(RowerDataFlag.HeartRate);
}
if (this.Flags.HasFlag(RowerDataFlag.MetabolicEquivalent))
{
b += this.SizeOfDataForFlag(RowerDataFlag.MetabolicEquivalent);
}
if (this.Flags.HasFlag(RowerDataFlag.ElapsedTime))
{
this.ElapsedTime = BitConvertHelper.ToInt16(data, b);
b += this.SizeOfDataForFlag(RowerDataFlag.ElapsedTime);
}
if (this.Flags.HasFlag(RowerDataFlag.RemainingTime))
{
b += this.SizeOfDataForFlag(RowerDataFlag.RemainingTime);
}
if (this.Flags.HasFlag(RowerDataFlag.Pull))
{
List<ushort> list = new List<ushort>();
for (int pullB = 4; pullB < data.Length; pullB += 2)
{
var val = BitConverter.ToUInt16(data, pullB);
list.Add(val);
PullValue = val;
}
}
Debug.Log("原始数据:" + string.Join(",", data));
}
public void Reset()
{
this.StrokeRate = 0;
this.StrokeCount = 0;
this.AverageStrokeRate = 0;
this.TotalDistance = 0;
this.InstantaneousPace = 0;
this.AveragePace = 0;
this.InstantaneousPower = 0;
this.AveragePower = 0;
this.ResistanceLevel = 0;
this.TotalEnergy = 0;
this.EnergyPerHour = 0;
this.EnergyPerMinute = 0;
this.ElapsedTime = 0;
}
public int SizeOfDataForFlag(RowerDataFlag flag)
{
int num = 0;
switch (flag)
{
//case RowerDataFlag.MoreData:
// break;
case RowerDataFlag.AverageStrokeRate:
num = 1;
break;
case RowerDataFlag.TotalDistance:
num = 3;
break;
case RowerDataFlag.InstantaneousPace:
num = 2;
break;
case RowerDataFlag.AveragePace:
num = 2;
break;
case RowerDataFlag.InstantPower:
num = 2;
break;
case RowerDataFlag.AveragePower:
num = 2;
break;
case RowerDataFlag.ResistanceLevel:
num = 2;
break;
case RowerDataFlag.ExpendedEnergy:
num = 5;
break;
case RowerDataFlag.HeartRate:
num = 1;
break;
case RowerDataFlag.MetabolicEquivalent:
num = 1;
break;
case RowerDataFlag.ElapsedTime:
num = 2;
break;
case RowerDataFlag.RemainingTime:
num = 2;
break;
default:
break;
}
return num;
}
public void SetUnavailable()
{
//throw new NotImplementedException();
}
//254 0是前七字節 1 41是第二包 1 64是拉力
[Flags]
public enum RowerDataFlag
{
MoreData = 1,
AverageStrokeRate = 2,
TotalDistance = 4,
InstantaneousPace = 8,
AveragePace = 16,
InstantPower = 32,
AveragePower = 64,
ResistanceLevel = 128,
ExpendedEnergy = 256,
HeartRate = 512,
MetabolicEquivalent = 1024,
ElapsedTime = 2048,
RemainingTime = 4096,
//单周期能耗
SingleEnergy = 8192,
//拉力曲綫
Pull = 16384
}
private byte SizeOfDataForFlag1(RowerDataFlag flag)
{
if (flag <= RowerDataFlag.InstantPower)
{
//if (flag <= RowerDataFlag.AverageCadence)
//{
// if (flag - RowerDataFlag.MoreData > 1 && flag != RowerDataFlag.InstantCadence && flag != RowerDataFlag.AverageCadence)
// {
// return 0;
// }
//}
//else
{
if (flag == RowerDataFlag.TotalDistance)
{
return 3;
}
if (flag != RowerDataFlag.ResistanceLevel && flag != RowerDataFlag.InstantPower)
{
return 0;
}
}
}
else if (flag <= RowerDataFlag.HeartRate)
{
if (flag != RowerDataFlag.AveragePower)
{
if (flag == RowerDataFlag.ExpendedEnergy)
{
return 5;
}
if (flag != RowerDataFlag.HeartRate)
{
return 0;
}
return 1;
}
}
else
{
if (flag != RowerDataFlag.MetabolicEquivalent && flag != RowerDataFlag.ElapsedTime && flag != RowerDataFlag.RemainingTime)
{
return 0;
}
return 0;
}
return 2;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 87207b21c4f11ef4b866ac490a88620f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+17 -4
View File
@@ -7,6 +7,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace Assets.Scripts.Devices.Ble.Devices
{
@@ -16,21 +17,22 @@ namespace Assets.Scripts.Devices.Ble.Devices
public int Power { get => _ftmsIndoorBikeData.InstantPower.GetValueOrDefault(0); set => throw new NotImplementedException(); }
public double Speed { get => _ftmsIndoorBikeData.InstantSpeed.GetValueOrDefault(0); set => throw new NotImplementedException(); }
public int Cadence { get => _ftmsIndoorBikeData.InstantCadence.GetValueOrDefault(0); set => throw new NotImplementedException(); }
private FtmsRowerData _rowerData;
public FtmsRowerData rowerData { get => _rowerData; }
private FtmsIndoorBikeData _ftmsIndoorBikeData;
private List<BleServiceInfo> Services;
private BleCharacteristicInfo controlPointCharacteristic;
public Ftms(BlePeripheralInfo peripheralInfo, IBleWinHwInterface bleWinHwInterface) :base(peripheralInfo, bleWinHwInterface, Ant.SensorType.Trainer)
{
this._ftmsIndoorBikeData = new FtmsIndoorBikeData();
base.Characteristics.Add(this._ftmsIndoorBikeData);
base.Characteristics.Add(new FtmsFitnessMachineFeature());
//划船机
this._rowerData = new FtmsRowerData();
base.Characteristics.Add(rowerData);
bleWinHwInterface.CharacteristicReadEvent += CharacteristicReadMainCallback;
}
protected override void CreateServices(List<BleServiceInfo> discoveredServices)
@@ -56,6 +58,17 @@ namespace Assets.Scripts.Devices.Ble.Devices
{
this.controlPointCharacteristic = character;
}
else if (character.MatchGuid(ServiceUuids.Characteristics.RowerData))
{
Debug.Log("划船机");
this.hwInterface.SubscribeCharacteristic(character, (hw, cha, res) =>
{
//Debug.Log("1111111111111111111111");
//Debug.WriteLine(res.ToString());
var res1 = (res as BleResponse<byte[]>);
Debug.Log("收到消息划船机:" + string.Join(",", res1));
});
}
}
});
}
@@ -0,0 +1,70 @@
using Assets.Scripts.Ble;
using Assets.Scripts.Devices.Ant.Interfaces;
using Assets.Scripts.Devices.Ble.Characteristic;
using Assets.Scripts.Devices.Ble.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace Assets.Scripts.Devices.Ble.Devices
{
public class FtmsRower : BleDevice, IRowerDevice
{
private FtmsRowerData _rowerData;
public FtmsRowerData rowerData { get => _rowerData; }
private List<BleServiceInfo> Services;
private BleCharacteristicInfo controlPointCharacteristic;
public FtmsRower(BlePeripheralInfo peripheralInfo, IBleWinHwInterface bleWinHwInterface) :base(peripheralInfo, bleWinHwInterface, Ant.SensorType.Rower)
{
Debug.Log(24);
base.Characteristics.Add(new FtmsFitnessMachineFeature());
//划船机
this._rowerData = new FtmsRowerData();
base.Characteristics.Add(rowerData);
bleWinHwInterface.CharacteristicReadEvent += CharacteristicReadMainCallback;
}
protected override void CreateServices(List<BleServiceInfo> discoveredServices)
{
this.Services = discoveredServices;
foreach (var service in this.Services)
{
hwInterface.DiscoverCharacteristic(service, (hwInterface, service1, response) =>
{
foreach (var character in response.Data)
{
if (character.MatchGuid(ServiceUuids.Characteristics.RowerData))
{
Debug.Log("划船机");
this.hwInterface.SubscribeCharacteristic(character, (hw, cha, res) =>
{
//Debug.Log("1111111111111111111111");
//Debug.WriteLine(res.ToString());
var res1 = (res as BleResponse<byte[]>);
Debug.Log("收到消息划船机:" + string.Join(",", res1));
});
}
}
});
}
}
private void CharacteristicReadMainCallback(IBleWinHwInterface hwInterface, BleCharacteristicInfo characteristic, BleResponse<byte[]> response)
{
foreach (var item in base.Characteristics)
{
if (characteristic.MatchGuid(item.Uuid))
{
//Debug.Log(string.Join(",", response.Data));
item.HandleAttributeReceived(response.Data);
}
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e62cf4fc3378d5c4eb76643a9d3ca528
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -365,6 +365,8 @@ namespace Assets.Scripts.Ble
// Token: 0x04002908 RID: 10504
public static Guid WahooFitnessSensorMeasurementInput = new Guid("A026E016-0A7D-4AB3-97FA-F1500F9FEB8B");
public static Guid RowerData = new Guid("00002AD1-0000-1000-8000-00805F9B34FB");
}
}
@@ -134,9 +134,17 @@ namespace Assets.Scripts.Ble
{
continue;
}
Debug.Log($"設備信息,{item.IdGuid},{string.Join(";",item.IdByteArray)}" );
if (item.IdByteArray == ServiceUuids.Ftms)
{
sensor = SensorType.Trainer;
if (App.IsRowerMode)
{
sensor = SensorType.Rower;
}
else
{
sensor = SensorType.Trainer;
}
}
else if (item.IdByteArray == ServiceUuids.HeartRate)
{