BikeDataDisplay.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using Controller.Bicycle;
  2. using Sensors;
  3. using TMPro;
  4. using UnityEngine;
  5. using UnityEngine.Serialization;
  6. namespace Display
  7. {
  8. public class BikeDataDisplay : MonoBehaviour
  9. {
  10. [FormerlySerializedAs("usePolarForHeartRate")]
  11. public bool useBleForHeartRate = true;
  12. public bool useBleForPower = true;
  13. public TextMeshProUGUI cadenceDisplay;
  14. public TextMeshProUGUI heartRateDisplay;
  15. public TextMeshProUGUI powerDisplay;
  16. [Header("Speed")] public RbBicycleController bicycleController;
  17. public TextMeshProUGUI speedDisplay;
  18. public bool useSpeedFromBicycleController = true;
  19. public TextMeshProUGUI torqueDisplay;
  20. private bool cadenceDisplayAvailable;
  21. private bool heartRateDisplayAvailable;
  22. private bool powerDisplayAvailable;
  23. private BikeSensorData sensorData;
  24. private bool speedDisplayAvailable;
  25. private bool torqueDisplayAvailable;
  26. // Start is called before the first frame update
  27. private void Start()
  28. {
  29. sensorData = BikeSensorData.Instance;
  30. speedDisplayAvailable = speedDisplay != null;
  31. powerDisplayAvailable = powerDisplay != null;
  32. torqueDisplayAvailable = torqueDisplay != null;
  33. cadenceDisplayAvailable = cadenceDisplay != null;
  34. heartRateDisplayAvailable = heartRateDisplay != null;
  35. }
  36. // Update is called once per frame
  37. private void Update()
  38. {
  39. var bikeSpeed = useSpeedFromBicycleController
  40. ? bicycleController.CurrentSpeedKph
  41. : sensorData.SpeedData?.SpeedKmh ?? 0f;
  42. if (speedDisplayAvailable) speedDisplay.text = $"{bikeSpeed:n2} km/h";
  43. var powerData = useBleForPower
  44. ? sensorData.BleData?.PowermeterData.power ?? 0f
  45. : sensorData.PowermeterData?.InstantaneousPower ?? 0f;
  46. var cadenceData = useBleForPower
  47. ? sensorData.BleData?.PowermeterData.cadence ?? 0f
  48. : sensorData.PowermeterData?.InstantaneousCadence ?? 0f;
  49. var torqueData = useBleForPower
  50. ? sensorData.BleData?.PowermeterData.torque ?? 0f
  51. : sensorData.PowermeterData?.CrankTorque ?? 0f;
  52. if (powerDisplayAvailable)
  53. powerDisplay.text =
  54. $"{powerData:0} W";
  55. if (cadenceDisplayAvailable)
  56. cadenceDisplay.text = $"{cadenceData:0} rpm";
  57. if (torqueDisplayAvailable) torqueDisplay.text = $"{torqueData:F2} Nm";
  58. if (heartRateDisplayAvailable)
  59. heartRateDisplay.text =
  60. $"{(useBleForHeartRate ? sensorData.BleData?.Hr : sensorData.HrData?.HeartRate) ?? 0:0} bpm";
  61. }
  62. }
  63. }