BikeDataDisplay.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 TextMeshProUGUI cadenceDisplay;
  10. public TextMeshProUGUI heartRateDisplay;
  11. public TextMeshProUGUI powerDisplay;
  12. [Header("Speed")] public RbBicycleController bicycleController;
  13. public TextMeshProUGUI speedDisplay;
  14. public bool useSpeedFromBicycleController = true;
  15. public TextMeshProUGUI torqueDisplay;
  16. private bool cadenceDisplayAvailable;
  17. private bool heartRateDisplayAvailable;
  18. private bool powerDisplayAvailable;
  19. private BikeSensorData sensorData;
  20. private bool speedDisplayAvailable;
  21. private bool torqueDisplayAvailable;
  22. // Start is called before the first frame update
  23. private void Start()
  24. {
  25. sensorData = BikeSensorData.Instance;
  26. speedDisplayAvailable = speedDisplay != null;
  27. powerDisplayAvailable = powerDisplay != null;
  28. torqueDisplayAvailable = torqueDisplay != null;
  29. cadenceDisplayAvailable = cadenceDisplay != null;
  30. heartRateDisplayAvailable = heartRateDisplay != null;
  31. }
  32. // Update is called once per frame
  33. private void Update()
  34. {
  35. var bikeSpeed = useSpeedFromBicycleController
  36. ? bicycleController.CurrentSpeedKph
  37. : sensorData.SpeedData?.SpeedKmh ?? 0f;
  38. if (speedDisplayAvailable) speedDisplay.text = $"{bikeSpeed:n2} km/h";
  39. if (powerDisplayAvailable) powerDisplay.text = $"{sensorData.PowermeterData?.InstantaneousPower ?? 0f:0} W";
  40. if (cadenceDisplayAvailable)
  41. cadenceDisplay.text = $"{sensorData.PowermeterData?.InstantaneousCadence ?? 0f:0} rpm";
  42. if (torqueDisplayAvailable) torqueDisplay.text = $"{sensorData.PowermeterData?.CrankTorque ?? 0f:F2} Nm";
  43. if (heartRateDisplayAvailable) heartRateDisplay.text = $"{sensorData.HrData?.HeartRate ?? 0:0} bpm";
  44. }
  45. }
  46. }