BikeDataDisplay.cs 2.2 KB

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