划船机优化
This commit is contained in:
1 parent
2f64aa494f
commit
87a0e7fb6d
8 files changed
+109
-80
No files matched your search
@@ -19,7 +19,7 @@ namespace Assets.Scripts.Devices.Ble
|
||||
|
||||
private IDictionary<string, BleDevice> discoveredDevices = new Dictionary<string, BleDevice>();
|
||||
|
||||
private IBleWinHwInterface hwInterface { get; set; }
|
||||
public IBleWinHwInterface hwInterface { get; private set; }
|
||||
public BleDeviceAdapter(IBleWinHwInterface bleWinHwInterface)
|
||||
{
|
||||
|
||||
|
||||
@@ -61,7 +61,7 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
|
||||
{
|
||||
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"))
|
||||
if (device.Name.Contains("Think") || device.Name.Contains("PF"))
|
||||
{
|
||||
return thinkRes;
|
||||
}
|
||||
@@ -93,6 +93,8 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
|
||||
public event EventHandler PullChanged;
|
||||
//开始事件
|
||||
public event EventHandler StartEvent;
|
||||
//日志事件
|
||||
public event EventHandler LogEvent;
|
||||
private ushort _pullValue;
|
||||
public ushort PullValue
|
||||
{
|
||||
@@ -114,6 +116,7 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
|
||||
}
|
||||
}
|
||||
private string[] deviceNamePool = new string[] {"Think","PF" };
|
||||
private List<ushort> tempPullList = new List<ushort>();
|
||||
|
||||
public void HandleAttributeReceived(byte[] data)
|
||||
{
|
||||
@@ -158,7 +161,8 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
|
||||
output += $" 总距离{ vvv }米";
|
||||
if (vvv < 10)
|
||||
{
|
||||
App.cacheList.Add(DateTime.Now.ToString() + ":" + output);
|
||||
LogEvent?.Invoke(DateTime.Now.ToString() + ":" + output,null);
|
||||
//App.cacheList.Add(DateTime.Now.ToString() + ":" + output);
|
||||
}
|
||||
b += this.SizeOfDataForFlag(RowerDataFlag.TotalDistance);
|
||||
|
||||
@@ -190,9 +194,11 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
|
||||
}
|
||||
if (this.Flags.HasFlag(RowerDataFlag.ResistanceLevel))
|
||||
{
|
||||
this.commonRes = BitConvertHelper.ToInt16(data, b);
|
||||
var newRes = BitConvertHelper.ToInt16(data, b);
|
||||
var resChanged = newRes != this.commonRes;
|
||||
this.commonRes = newRes;
|
||||
var device = App.MainDeviceAdapter.GetDevices().FirstOrDefault(d => (d.State == DeviceState.Connected) && d.Sensor == SensorType.Rower);
|
||||
if (device != null && deviceNamePool.FirstOrDefault(x => device.Name.ToLower().Contains(x.ToLower())) == null && RowerResChanged != null)
|
||||
if (resChanged && device != null && deviceNamePool.FirstOrDefault(x => device.Name.ToLower().Contains(x.ToLower())) == null && RowerResChanged != null)
|
||||
{
|
||||
RowerResChanged.Invoke(this.commonRes, null);
|
||||
}
|
||||
@@ -229,9 +235,11 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
|
||||
}
|
||||
if (this.Flags.HasFlag(RowerDataFlag.ThinkDragFactor))
|
||||
{
|
||||
var originRes = BitConvertHelper.ToInt16(data, b + 1);
|
||||
var thinkResChanged = originRes != this.thinkRes;
|
||||
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 && deviceNamePool.FirstOrDefault(x=>device.Name.ToLower().Contains(x.ToLower()))!=null && RowerResChanged != null)
|
||||
if (thinkResChanged && device != null && deviceNamePool.FirstOrDefault(x=>device.Name.ToLower().Contains(x.ToLower()))!=null && RowerResChanged != null)
|
||||
{
|
||||
RowerResChanged.Invoke(this.thinkRes, null);
|
||||
}
|
||||
@@ -239,17 +247,15 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
|
||||
}
|
||||
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);
|
||||
val = Convert.ToUInt16(((double)val) /9.8f);
|
||||
list.Add(val);
|
||||
tempPullList.Add(val);
|
||||
PullValue = val;
|
||||
|
||||
}
|
||||
if (pullList == null) pullList = new List<ushort>();
|
||||
foreach (var pull in list)
|
||||
foreach (var pull in tempPullList)
|
||||
{
|
||||
if (pull == 0)
|
||||
{
|
||||
@@ -262,10 +268,11 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
|
||||
}
|
||||
pullList.Add(pull);
|
||||
}
|
||||
tempPullList.Clear();
|
||||
//Debug.Log("拉力:" + string.Join(",",list));
|
||||
}
|
||||
}
|
||||
List<ushort> pullList;
|
||||
List<ushort> pullList { get; set; }
|
||||
public void Reset()
|
||||
{
|
||||
this.StrokeRate = 0;
|
||||
|
||||
@@ -190,7 +190,7 @@ namespace Assets.Scripts.Devices.Ble.Devices
|
||||
if (this.controlPointCharacteristic != null)
|
||||
{
|
||||
Debug.Log("发送重置命令" + this.controlPointCharacteristic.ToString());
|
||||
App.cacheList.Add(DateTime.Now.ToString() + ":发送重置命令" + this.controlPointCharacteristic.ToString());
|
||||
//App.cacheList.Add(DateTime.Now.ToString() + ":发送重置命令" + this.controlPointCharacteristic.ToString());
|
||||
hwInterface.WriteCharacteristic(this.controlPointCharacteristic, new byte[] { 0x01 });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,74 +37,14 @@ namespace Assets.Scripts.Devices.Ble
|
||||
}
|
||||
WclBleManagerStatus statusEnum = WclBleManagerStatus.RadioOff;
|
||||
internal BleMobileThread() {
|
||||
var self = this;
|
||||
//初始蓝牙
|
||||
BluetoothLEHardwareInterface.Initialize(true, false, () => {
|
||||
statusEnum = WclBleManagerStatus.RadioOn;
|
||||
managerInitialized?.Invoke(self);
|
||||
},
|
||||
(error) => {
|
||||
statusEnum = WclBleManagerStatus.RadioOff;
|
||||
BluetoothLEHardwareInterface.Log("Error: " + error);
|
||||
if (error.Contains("Bluetooth LE Not Enabled"))
|
||||
BluetoothLEHardwareInterface.BluetoothEnable(true);
|
||||
}, (status) => {
|
||||
|
||||
statusEnum = WclBleManagerStatus.RadioOn;
|
||||
switch (status)
|
||||
{
|
||||
case 13:
|
||||
statusEnum = WclBleManagerStatus.RadioOff;
|
||||
break;
|
||||
case 11:
|
||||
statusEnum = WclBleManagerStatus.RadioOn;
|
||||
break;
|
||||
}
|
||||
if (managerStatusChanged != null)
|
||||
{
|
||||
managerStatusChanged.Invoke(self, statusEnum);
|
||||
}
|
||||
});
|
||||
Initialize();//初始蓝牙
|
||||
}
|
||||
|
||||
public void StartWatcher() {
|
||||
var self = this;
|
||||
if (statusEnum == WclBleManagerStatus.RadioOff)
|
||||
{
|
||||
BluetoothLEHardwareInterface.BluetoothEnable(true);
|
||||
BluetoothLEHardwareInterface.Initialize(true, false, () =>
|
||||
{
|
||||
statusEnum = WclBleManagerStatus.RadioOn;
|
||||
managerInitialized?.Invoke(self);
|
||||
BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(ServiceUuids.GetServiceUuidList().ToArray(), null, (address, name, rssi, bytes) =>
|
||||
{
|
||||
ScanInfoReceived?.Invoke(self, address, name, rssi, bytes);
|
||||
}, true);
|
||||
},
|
||||
(error) =>
|
||||
{
|
||||
statusEnum = WclBleManagerStatus.RadioOff;
|
||||
BluetoothLEHardwareInterface.Log("Error: " + error);
|
||||
if (error.Contains("Bluetooth LE Not Enabled"))
|
||||
BluetoothLEHardwareInterface.BluetoothEnable(true);
|
||||
}, (status) =>
|
||||
{
|
||||
|
||||
statusEnum = WclBleManagerStatus.RadioOn;
|
||||
switch (status)
|
||||
{
|
||||
case 13:
|
||||
statusEnum = WclBleManagerStatus.RadioOff;
|
||||
break;
|
||||
case 11:
|
||||
statusEnum = WclBleManagerStatus.RadioOn;
|
||||
break;
|
||||
}
|
||||
if (managerStatusChanged != null)
|
||||
{
|
||||
managerStatusChanged.Invoke(self, statusEnum);
|
||||
}
|
||||
});
|
||||
Initialize();
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -114,6 +54,52 @@ namespace Assets.Scripts.Devices.Ble
|
||||
}, true);
|
||||
}
|
||||
}
|
||||
public void Initialize()
|
||||
{
|
||||
var self = this;
|
||||
BluetoothLEHardwareInterface.BluetoothEnable(true);
|
||||
BluetoothLEHardwareInterface.Initialize(true, false, () =>
|
||||
{
|
||||
statusEnum = WclBleManagerStatus.RadioOn;
|
||||
managerInitialized?.Invoke(self);
|
||||
BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(ServiceUuids.GetServiceUuidList().ToArray(), null, (address, name, rssi, bytes) =>
|
||||
{
|
||||
ScanInfoReceived?.Invoke(self, address, name, rssi, bytes);
|
||||
}, true);
|
||||
},
|
||||
(error) =>
|
||||
{
|
||||
statusEnum = WclBleManagerStatus.RadioOff;
|
||||
BluetoothLEHardwareInterface.Log("Error: " + error);
|
||||
if (error.Contains("Bluetooth LE Not Enabled"))
|
||||
BluetoothLEHardwareInterface.BluetoothEnable(true);
|
||||
}, (status) =>
|
||||
{
|
||||
statusEnum = WclBleManagerStatus.RadioOn;
|
||||
switch (status)
|
||||
{
|
||||
case 10:
|
||||
statusEnum = WclBleManagerStatus.RadioOff;
|
||||
break;
|
||||
case 13:
|
||||
statusEnum = WclBleManagerStatus.RadioOff;
|
||||
break;
|
||||
case 11:
|
||||
statusEnum = WclBleManagerStatus.RadioOff;
|
||||
break;
|
||||
case 12:
|
||||
statusEnum = WclBleManagerStatus.RadioOn;
|
||||
break;
|
||||
default:
|
||||
statusEnum = WclBleManagerStatus.RadioOff;
|
||||
break;
|
||||
}
|
||||
if (managerStatusChanged != null)
|
||||
{
|
||||
managerStatusChanged.Invoke(self, statusEnum);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user