适配c2;新版设计稿;对战切换手势;主逻辑接通;使用think的阻力值,修改影子模式船位置

This commit is contained in:
Caiyanpeng committed 2022-03-25 18:02:34 +08:00
1 parent e6f3e2ecfa
commit ab653b994f
82 files changed
+25241 -8988

No files matched your search

@@ -0,0 +1,82 @@
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 C2RowerData : ICharacteristic, IRowerCommonData
{
public Guid Uuid => ServiceUuids.Characteristics.C2RowerData;
public Guid ServiceUuid => ServiceUuids.Characteristics.C2Service;
public bool IsOptional => false;
public static event EventHandler EnableChanged;
private static bool? _isEnabled;
public static bool? IsEnabled
{
get => _isEnabled;
set
{
_isEnabled = value;
if (EnableChanged != null)
{
EnableChanged(_isEnabled, new EventArgs());
}
}
}
//0.1 lbs of force, max=6553.5m 单位0.1磅
public double PeakDriveForce { get; set; }
public double AverageDriveForce { get; set; }
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 void HandleAttributeReceived(byte[] data)
{
Debug.Log("数据" + string.Join(",", data));
if (data[0] == 53)
{
PeakDriveForce = LbsToNewton(Convert.ToDouble(BitConvertHelper.ToUInt16(data, 13)) / 10);
AverageDriveForce = LbsToNewton(Convert.ToDouble(BitConvertHelper.ToUInt16(data, 15)) / 10);
Debug.Log($"峰力值:{PeakDriveForce} 均力值{AverageDriveForce}");
}
else if (data[0] == 61)
{
List<ushort> r = new List<ushort>();
for (int i = 3; i < data.Length; i += 2)
{
ushort pull = data[i] < 10 ? (ushort)0 : Convert.ToUInt16(Math.Round(LbsToNewton(data[i])));
r.Add(pull);
PullValue = pull;
}
Debug.Log("拉力曲线:"+ string.Join(",",r));
}
}
private double LbsToNewton(double lbs) => 4.4482216 * lbs;
public void SetUnavailable()
{
//throw new NotImplementedException();
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 64ae3a6d26480b04c8fed17cc833d179
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,4 +1,5 @@
using Assets.Scripts.Ble;
using Assets.Scripts.Devices.Ant;
using Assets.Scripts.Devices.Ble.Interfaces;
using System;
using System.Collections.Generic;
@@ -9,7 +10,7 @@ using UnityEngine;
namespace Assets.Scripts.Devices.Ble.Characteristic
{
public class FtmsRowerData : ICharacteristic
public class FtmsRowerData : ICharacteristic, IRowerCommonData
{
/// <summary>
/// 划桨频率
@@ -37,8 +38,26 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
public int InstantaneousPower { get; private set; } = 0;
public int AveragePower { get; private set; } = 0;
public int ResistanceLevel { get; private set; } = 0;
public static event EventHandler RowerResChanged;
private int commonRes = 50;
private int thinkRes = 50;
public int ResistanceLevel
{
get
{
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"))
{
return thinkRes;
}
else
{
return commonRes;
}
}
}
/// <summary>
/// 运行时间
/// </summary>
@@ -49,6 +68,8 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
public int EnergyPerHour { get; private set; } = 0;
public int EnergyPerMinute { get; private set; } = 0;
public double PeakDriveForce { get; set; } = 0;
public double AverageDriveForce { get; set; } = 0;
public Guid Uuid => ServiceUuids.Characteristics.RowerData;
@@ -152,7 +173,12 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
}
if (this.Flags.HasFlag(RowerDataFlag.ResistanceLevel))
{
this.ResistanceLevel = BitConvertHelper.ToInt16(data, b);
this.commonRes = BitConvertHelper.ToInt16(data, b);
var device = App.MainDeviceAdapter.GetDevices().FirstOrDefault(d => (d.State == DeviceState.Connected) && d.Sensor == SensorType.Rower);
if (device != null && !device.Name.Contains("Think") && RowerResChanged != null)
{
RowerResChanged.Invoke(this.commonRes, null);
}
b += this.SizeOfDataForFlag(RowerDataFlag.ResistanceLevel);
}
if (this.Flags.HasFlag(RowerDataFlag.ExpendedEnergy))
@@ -184,6 +210,16 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
{
b += this.SizeOfDataForFlag(RowerDataFlag.RemainingTime);
}
if (this.Flags.HasFlag(RowerDataFlag.ThinkDragFactor))
{
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 && device.Name.Contains("Think") && RowerResChanged != null)
{
RowerResChanged.Invoke(this.thinkRes, null);
}
b += this.SizeOfDataForFlag(RowerDataFlag.ThinkDragFactor);
}
if (this.Flags.HasFlag(RowerDataFlag.Pull))
{
List<ushort> list = new List<ushort>();
@@ -194,10 +230,24 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
PullValue = val;
}
if (pullList == null) pullList = new List<ushort>();
foreach (var pull in list)
{
if (pull == 0)
{
if (pullList.Count != 0)
{
PeakDriveForce = pullList.Max();
AverageDriveForce = (double)pullList.Average(x=>x);
}
pullList.Clear();
}
pullList.Add(pull);
}
Debug.Log("拉力:" + string.Join(",",list));
}
//Debug.Log("原始数据:" + string.Join(",", data));
}
List<ushort> pullList;
public void Reset()
{
this.StrokeRate = 0;
@@ -208,7 +258,9 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
this.AveragePace = 0;
this.InstantaneousPower = 0;
this.AveragePower = 0;
this.ResistanceLevel = 0;
this.commonRes = 50;
this.thinkRes = 50;
//this.ResistanceLevel = 0;
this.TotalEnergy = 0;
this.EnergyPerHour = 0;
this.EnergyPerMinute = 0;
@@ -258,6 +310,9 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
case RowerDataFlag.RemainingTime:
num = 2;
break;
case RowerDataFlag.ThinkDragFactor:
num = 5;
break;
default:
break;
}
@@ -288,7 +343,9 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
//单周期能耗
SingleEnergy = 8192,
//拉力曲綫
Pull = 16384
Pull = 16384,
//Think 阻力
ThinkDragFactor = 32768
}
+103 -22
View File
@@ -6,6 +6,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
@@ -16,9 +17,12 @@ namespace Assets.Scripts.Devices.Ble.Devices
private FtmsRowerData _rowerData;
public FtmsRowerData rowerData { get => _rowerData; }
C2RowerData _c2RowerData;
public C2RowerData c2RowerData { get => _c2RowerData; }
private List<BleServiceInfo> Services;
private BleCharacteristicInfo controlPointCharacteristic;
private BleCharacteristicInfo c2Control;
public FtmsRower(BlePeripheralInfo peripheralInfo, IBleWinHwInterface bleWinHwInterface) :base(peripheralInfo, bleWinHwInterface, Ant.SensorType.Rower)
{
Debug.Log(24);
@@ -26,34 +30,50 @@ namespace Assets.Scripts.Devices.Ble.Devices
//划船机
this._rowerData = new FtmsRowerData();
base.Characteristics.Add(rowerData);
this._c2RowerData = new C2RowerData();
base.Characteristics.Add(c2RowerData);
bleWinHwInterface.CharacteristicReadEvent -= CharacteristicReadMainCallback;
bleWinHwInterface.CharacteristicReadEvent += CharacteristicReadMainCallback;
}
protected override void CreateServices(List<BleServiceInfo> discoveredServices)
{
this.Services = discoveredServices;
Debug.Log("服务+" + string.Join(",", Services.Select(x => x.Id.ToString())));
foreach (var service in this.Services)
{
hwInterface.DiscoverCharacteristic(service, (hwInterface, service1, response) =>
{
foreach (var character in response.Data)
{
//Debug.Log(character.Id.ToString());
if (character.MatchGuid(ServiceUuids.Characteristics.FitnessMachineControlPoint))
{
Debug.Log("中控台获取");
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));
});
Debug.Log("ftms划船机" + service.Id);
this.hwInterface.SubscribeCharacteristic(character, null);
}
else if (character.MatchGuid(ServiceUuids.Characteristics.C2RowerData))
{
Debug.Log("c2划船机基本数据" + service.Id);
//C2RowerData.IsEnabled = true;
this.hwInterface.SubscribeCharacteristic(character, null);
}
else if (character.MatchGuid(ServiceUuids.Characteristics.C2RowerData1))
{
Debug.Log("c2划船机拉力数据" + service.Id);
//C2RowerData.IsEnabled = true;
this.hwInterface.SubscribeCharacteristic(character, null);
}
else if (character.MatchGuid(ServiceUuids.Characteristics.C2RowerControl))
{
Debug.Log("c2划船机控制台");
this.c2Control = character;
}
}
});
@@ -62,26 +82,87 @@ namespace Assets.Scripts.Devices.Ble.Devices
private void CharacteristicReadMainCallback(IBleWinHwInterface hwInterface, BleCharacteristicInfo characteristic, BleResponse<byte[]> response)
{
foreach (var item in base.Characteristics)
//var tc = base.Characteristics.SingleOrDefault(x => x.Uuid == ServiceUuids.Characteristics.C2RowerData);
//tc.HandleAttributeReceived(new byte[] { 61, 89, 0, 0, 0, 33, 0, 33, 0, 46, 0, 46, 0, 53, 0, 60, 0, 60, 0, 68, 0, 68, 0, 0, 0 });
//tc.HandleAttributeReceived(new byte[] { 53, 223, 211, 0, 162, 57, 0, 82, 62, 96, 0, 94, 2, 183, 3, 125, 2, 208, 7, 203, 0 });
if (characteristic.MatchGuid(ServiceUuids.Characteristics.RowerData))
{
if (characteristic.MatchGuid(item.Uuid))
{
//Debug.Log(string.Join(",", response.Data));
item.HandleAttributeReceived(response.Data);
}
var c = base.Characteristics.SingleOrDefault(x => x.Uuid == ServiceUuids.Characteristics.RowerData);
c.HandleAttributeReceived(response.Data);
}
else
{
var c = base.Characteristics.SingleOrDefault(x => x.Uuid == ServiceUuids.Characteristics.C2RowerData);
var data = new byte[] { characteristic.Id.ToByteArray()[0] }.Concat(response.Data).ToArray();
c.HandleAttributeReceived(data);
}
//foreach (var item in base.Characteristics)
//{
// if (characteristic.MatchGuid(item.Uuid) || characteristic.Id == ServiceUuids.Characteristics.C2RowerData1)
// {
// if (item.Uuid == ServiceUuids.Characteristics.C2RowerData1)
// {
// Debug.Log("c2");
// var data = new byte[] { item.Uuid.ToByteArray()[0] }.Concat(response.Data).ToArray();
// item.HandleAttributeReceived(data);
// }
// else if (item.Uuid == ServiceUuids.Characteristics.RowerData)
// {
// Debug.Log("ftms");
// item.HandleAttributeReceived(response.Data);
// }
// }
//}
}
public void Reset()
{
Debug.Log("发送重置命令"+this.controlPointCharacteristic.ToString());
hwInterface.WriteCharacteristic(this.controlPointCharacteristic, new byte[] { 0x01 });
if (C2RowerData.IsEnabled == true)
{
//等对csafe协议研究透彻后写
//if (this.c2Control != null)
//{
// Debug.Log("发送重置命令" + this.c2Control.ToString());
// hwInterface.WriteCharacteristic(this.c2Control, new byte[] { 0xF1, 0x86, 0x87, 0x01, 0xF2 });
// //hwInterface.WriteCharacteristic(this.c2Control, new byte[] { 0xF1, 0x81, 0x81, 0xF2 });
//}
}
else
{
if (this.controlPointCharacteristic != null)
{
Debug.Log("发送重置命令" + this.controlPointCharacteristic.ToString());
hwInterface.WriteCharacteristic(this.controlPointCharacteristic, new byte[] { 0x01 });
}
}
}
public void SetResistanceLevel(ushort v)
{
var r = new byte[] { 0x04 }.Concat(BitConverter.GetBytes(v)).ToArray();
Debug.Log($"设置阻力{v}, {string.Join(",", r)}");
hwInterface.WriteCharacteristic(this.controlPointCharacteristic, r);
if (C2RowerData.IsEnabled == true)
{
//等对csafe协议研究透彻后写
//if (this.c2Control != null)
//{
// var data = new byte[] { 0x29 }.Concat(BitConverter.GetBytes(v)).ToArray();
// byte checksum = (byte)((byte)(data[0] ^ data[1]) ^ data[2]);
// var r = new byte[] { 0xF1 }.Concat(data).Concat(new byte[] { checksum, 0xf2 }) .ToArray();
// Debug.Log($"设置阻力{v}, {string.Join(",", r)}");
// hwInterface.WriteCharacteristic(this.c2Control, r);
//}
}
else
{
if (this.controlPointCharacteristic != null)
{
//特注:81是think专有指令,后期可能需要兼容其他设备
var r = new byte[] { 0x81 }.Concat(BitConverter.GetBytes((ushort)(50 + v))).ToArray();
Debug.Log($"设置阻力{v}, {string.Join(",", r)}");
hwInterface.WriteCharacteristic(this.controlPointCharacteristic, r);
}
}
}
}
}
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assets.Scripts.Devices.Ble.Interfaces
{
interface IRowerCommonData
{
event EventHandler PullChanged;
ushort PullValue { get; set; }
double PeakDriveForce { get; set; }
double AverageDriveForce { get; set; }
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7b9b93075aa03cf4587d75a49b2bfaa8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -367,6 +367,15 @@ namespace Assets.Scripts.Ble
public static Guid WahooFitnessSensorMeasurementInput = new Guid("A026E016-0A7D-4AB3-97FA-F1500F9FEB8B");
public static Guid RowerData = new Guid("00002AD1-0000-1000-8000-00805F9B34FB");
public static Guid C2RowerControl = new Guid("ce060021-43e5-11e4-916c-0800200c9a66");
//0x35 有峰力值和均力值
public static Guid C2RowerData = new Guid("ce060035-43e5-11e4-916c-0800200c9a66");
//拉力曲线
public static Guid C2RowerData1 = new Guid("ce06003D-43e5-11e4-916c-0800200c9a66");
public static Guid C2Service = new Guid("CE060030-43E5-11E4-916C-0800200C9A66");
//ce060030-43e5-11e4-916c-0800200c9a66
}
}
@@ -230,7 +230,8 @@ namespace Assets.Scripts.Ble
{
callback?.Invoke(self, info, s);
this.callbacks.Remove(info);
Debug.Log("连接成功!");
Debug.Log("连接成功!"+info.Name);
Devices.Ble.Characteristic.C2RowerData.IsEnabled = info.Name.Contains("PM");
if (this.callbacks.Any())
{
this.ConnectPeripheral(this.callbacks.First().Key, this.callbacks.First().Value);
@@ -373,8 +374,11 @@ namespace Assets.Scripts.Ble
{
this.characteristicNotificationCallbacks.Add(characteristic.Peripheral, callback);
}
BluetoothLEHardwareInterface.Log("SubscribeCharacteristic" + characteristic.Peripheral.Name + characteristic.Service.ToString() + characteristic.Service.ToString());
BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(characteristic.Peripheral.Address, characteristic.Service.ToString(), characteristic.Id.ToString(), null, (deviceAddress, characteristric, bytes) =>
Debug.Log("SubscribeCharacteristic" + characteristic.Peripheral.Name + characteristic.Service.ToString() + characteristic.Service.ToString());
BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(characteristic.Peripheral.Address, characteristic.Service.ToString(), characteristic.Id.ToString(), (a,b)=>
{
Debug.Log("返回特征值," + a + "," + b);
}, (deviceAddress, characteristric, bytes) =>
{
if (characteristicReadEvent != null)
{