private async void ReadVoltage() { if (ConnectedDevice == null || ConnectedDevice.ConnectionState == BACConnectionState.DISCONNECTED) { Model.Voltage = 0.0; VoltageLabel.LabelText = "0.0"; VoltageLabel.LabelColor = Color.DimGray; return; } while (ConnectedDevice != null && ConnectedDevice.ConnectionState == BACConnectionState.CONNECTED) { try { Model.Voltage = await ConnectedDevice.Read(265) / (double)32; VoltageLabel.LabelText = Model.Voltage.ToString("F0"); VoltageLabel.LabelColor = Color.Black; } catch (Exception exception) { Console.WriteLine(exception); UnsubscribeToDevice(); } } }
private async void ReadSpeed() { if (ConnectedDevice == null || ConnectedDevice.ConnectionState == BACConnectionState.DISCONNECTED) { Model.Speed = 0; return; } while (ConnectedDevice != null && ConnectedDevice.ConnectionState == BACConnectionState.CONNECTED) { try { var speed = Math.Round(await ConnectedDevice.Read(260) / (float)256 / 1.609344, 0); Model.Speed = speed; if (speed > App.LifetimeSpeed) { Stats.LifetimeSpeed = speed; App.LifetimeSpeed = speed; } if (speed > Stats.SessionSpeed) { Stats.SessionSpeed = speed; } } catch (Exception exception) { Console.WriteLine(exception); UnsubscribeToDevice(); HandleConnectionState(); } } }
/// <summary> /// Start an auto-connection sequence /// </summary> private async void StartAutoConnectionRoutine() { Console.WriteLine("Started auto connect !!"); if (ConnectedDevice != null) { await ConnectedDevice.Disconnect(); ConnectedDevice = null; } while (ConnectedDevice == null && List.Count > 0) { ConnectedDevice = await BacCommunication.CurrentRepository.StartBluetoothLeAutoConnection(List); if (ConnectedDevice != null) { var wheelDiameter = await ConnectedDevice.Read(227); if (Math.Abs(wheelDiameter - 279.4) > 0.1) { try { await ConnectedDevice.Write(227, (short)279.4); } catch (Exception exception) { Console.WriteLine(exception); UnsubscribeToDevice(); HandleConnectionState(); } } var gearRatio = await ConnectedDevice.Read(226) / 256; if (gearRatio != 1) { try { await ConnectedDevice.Write(226, 1); } catch (Exception exception) { Console.WriteLine(exception); UnsubscribeToDevice(); HandleConnectionState(); } } } } if (ConnectedDevice == null) { return; } App.LocalDevice = ConnectedDevice; HandleConnectionState(); SubscribeToConnectedDevice(); }
private async void ReadTemp() { if (ConnectedDevice == null || ConnectedDevice.ConnectionState == BACConnectionState.DISCONNECTED) { Model.Temp = 0.0; return; } while (ConnectedDevice != null && ConnectedDevice.ConnectionState == BACConnectionState.CONNECTED) { try { Model.Temp = await ConnectedDevice.Read(259) * (9 / 5.0) + 32.0; } catch (Exception exception) { Console.WriteLine(exception); UnsubscribeToDevice(); } } }
private async void ReadCurrent() { if (ConnectedDevice == null || ConnectedDevice.ConnectionState == BACConnectionState.DISCONNECTED) { Model.Amps = 0.0; return; } while (ConnectedDevice != null && ConnectedDevice.ConnectionState == BACConnectionState.CONNECTED) { try { Model.Amps = (await ConnectedDevice.Read(262) / (double)32) * 2.0; } catch (Exception exception) { Console.WriteLine(exception); UnsubscribeToDevice(); } } }