BikeDataDisplay.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. private bool cadenceDisplayAvailable;
  11. public TextMeshProUGUI heartRateDisplay;
  12. private bool heartRateDisplayAvailable;
  13. [Header("Heart Rate")] public int hrAntId = 0;
  14. public TextMeshProUGUI powerDisplay;
  15. private bool powerDisplayAvailable;
  16. [Header("Powermeter")] public int powerMeterId = 0;
  17. private BikeSensorData sensorData;
  18. [Header("Speed")] public RbBicycleController bicycleController;
  19. public TextMeshProUGUI speedDisplay;
  20. public bool useSpeedFromBicycleController = true;
  21. private bool speedDisplayAvailable;
  22. public TextMeshProUGUI torqueDisplay;
  23. private bool torqueDisplayAvailable;
  24. // Start is called before the first frame update
  25. private void Start()
  26. {
  27. sensorData = BikeSensorData.Instance;
  28. //powermeter and ANT hr only for displaying for now TODO: move to controller if used for controlling
  29. sensorData.StartListening(powermeterId: powerMeterId, hrAntId: hrAntId);
  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. if (powerDisplayAvailable) powerDisplay.text = $"{sensorData.PowermeterData?.InstantaneousPower ?? 0f:0} W";
  44. if (cadenceDisplayAvailable)
  45. cadenceDisplay.text = $"{sensorData.PowermeterData?.InstantaneousCadence ?? 0f:0} rpm";
  46. if (torqueDisplayAvailable) torqueDisplay.text = $"{sensorData.PowermeterData?.CrankTorque ?? 0f:F2} Nm";
  47. if (heartRateDisplayAvailable) heartRateDisplay.text = $"{sensorData.HrData?.HeartRate ?? 0:0} bpm";
  48. }
  49. }
  50. }