划船机优化
This commit is contained in:
parent
2f64aa494f
commit
87a0e7fb6d
@ -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()
|
||||
{
|
||||
|
||||
@ -119,5 +119,17 @@ namespace Assets.Scripts.Devices
|
||||
str += "---------------------------\r\n";
|
||||
Debug.Log(str);
|
||||
}
|
||||
|
||||
public BleDeviceAdapter GetBleDeviceAdapter()
|
||||
{
|
||||
foreach (var item in adapters)
|
||||
{
|
||||
var bleadapter = (BleDeviceAdapter)item;
|
||||
if (bleadapter != null) {
|
||||
return bleadapter;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -51,6 +51,12 @@ public class PFUISlider : MonoBehaviour
|
||||
public void SetValue(float a)
|
||||
{
|
||||
slider.value = a;
|
||||
slider.onValueChanged?.Invoke(a);
|
||||
}
|
||||
|
||||
public float GetValue()
|
||||
{
|
||||
return slider.value;
|
||||
}
|
||||
|
||||
public List<Color> SetColorGradient(List<Color> list, int divideCount)
|
||||
|
||||
@ -295,6 +295,8 @@ public class RowerHomeScript : PFUIPanel
|
||||
RowerData.PullChanged += PaintPullCurve;
|
||||
RowerData.StartEvent -= StartFunc;
|
||||
RowerData.StartEvent += StartFunc;
|
||||
RowerData.RowerResChanged -= ResChanged;
|
||||
RowerData.RowerResChanged += ResChanged;
|
||||
}
|
||||
rowerType = new RowerType { type = 1, value = 500 };
|
||||
HandleSelectType();
|
||||
@ -464,6 +466,7 @@ public class RowerHomeScript : PFUIPanel
|
||||
int truelyTime = 0;
|
||||
private void StartFunc(object sender, EventArgs e)
|
||||
{
|
||||
Debug.Log($"开始了:{openTimer}");
|
||||
if (UIManager.Instance.confirm != null && UIManager.Instance.confirm.IsActive())
|
||||
{
|
||||
print("当前有弹窗");
|
||||
@ -731,12 +734,16 @@ public class RowerHomeScript : PFUIPanel
|
||||
transform.Find("ResBar/BtnSub").GetComponent<Button>().interactable = !(bool)sender;
|
||||
transform.Find("ResBar/BtnAdd").GetComponent<Button>().interactable = !(bool)sender;
|
||||
}
|
||||
|
||||
bool initRes = false;
|
||||
private void ResChanged(object sender, EventArgs e)
|
||||
{
|
||||
if ((Convert.ToSingle(sender) >= 50) && slider)
|
||||
Debug.Log(Convert.ToSingle(sender));
|
||||
if (!initRes && slider)
|
||||
{
|
||||
slider.GetComponent<PFUISlider>().SetValue((Convert.ToSingle(sender) - 50) / 300f);
|
||||
var origin = Convert.ToSingle(sender);
|
||||
var wrapped = origin< 50 ? 50 : origin;
|
||||
slider.GetComponent<PFUISlider>().SetValue((wrapped - 50) / 300f);
|
||||
initRes = true;
|
||||
}
|
||||
}
|
||||
|
||||
@ -798,6 +805,13 @@ public class RowerHomeScript : PFUIPanel
|
||||
if (Rower == null)
|
||||
{
|
||||
//HandleDiscardDirect();
|
||||
UIManager.ShowConfirm(App.GetLocalString("Warning"), App.GetLocalString("Save the game?"), () =>
|
||||
{
|
||||
HandleSaveDirect();
|
||||
UIManager.CloseConfirm();
|
||||
}, 2, ()=> {
|
||||
Init();
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (RowerData == null) return;
|
||||
@ -827,6 +841,9 @@ public class RowerHomeScript : PFUIPanel
|
||||
}
|
||||
if (stopSeconds >= 2)
|
||||
{
|
||||
if (UIManager.Instance.confirm != null && UIManager.Instance.confirm.IsActive())
|
||||
return;
|
||||
|
||||
transform.Find("Stopped").gameObject.SetActive(true);
|
||||
//if (stopSeconds == 6)
|
||||
//{
|
||||
@ -1078,8 +1095,7 @@ public class RowerHomeScript : PFUIPanel
|
||||
int pointCount = 0;
|
||||
void PaintPullCurve(ushort y)
|
||||
{
|
||||
|
||||
Debug.Log("收到拉力" + y + ","+ DateTime.Now.Ticks);
|
||||
Debug.Log($"收到拉力 x={x} , y={y}");
|
||||
//#if !UNITY_EDITOR
|
||||
// if (!openTimer)
|
||||
// {
|
||||
@ -1285,7 +1301,7 @@ public class RowerHomeScript : PFUIPanel
|
||||
bool isFirstReset = false;
|
||||
void HandleStatic()
|
||||
{
|
||||
var flag = Rower != null && RowerData != null;
|
||||
var flag = Rower != null && RowerData != null && !(UIManager.Instance.confirm != null && UIManager.Instance.confirm.IsActive());
|
||||
if (transform.Find("Ready").gameObject.activeInHierarchy)
|
||||
{
|
||||
transform.Find("Ready/DeviceStatus").gameObject.SetActive(!flag);
|
||||
|
||||
@ -135,6 +135,7 @@ public class RowerDeviceView : MonoBehaviour
|
||||
UIManager.ShowConfirm("警告", "是否断开该设备?", () =>
|
||||
{
|
||||
device.Disconnect(false);
|
||||
DeviceCache.Remove(device);
|
||||
UIManager.CloseConfirm();
|
||||
}, 2, () => {
|
||||
UIManager.CloseConfirm();
|
||||
@ -151,6 +152,7 @@ public class RowerDeviceView : MonoBehaviour
|
||||
var device = GetDevice();
|
||||
if (device != null && device.State == DeviceState.Connected)
|
||||
{
|
||||
DeviceCache.Remove(device);
|
||||
device.Disconnect(false);
|
||||
}
|
||||
UIManager.CloseConfirm();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user