BikeDataDisplay.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using TMPro;
  2. using UnityEngine;
  3. public class BikeDataDisplay : MonoBehaviour
  4. {
  5. [Header("Speed")]
  6. public TextMeshProUGUI speedDisplay;
  7. [Header("Powermeter")]
  8. public int powerMeterId = 0;
  9. public TextMeshProUGUI powerDisplay;
  10. public TextMeshProUGUI cadenceDisplay;
  11. public TextMeshProUGUI torqueDisplay;
  12. [Header("Heart Rate")] public int hrAntId = 0;
  13. public TextMeshProUGUI heartRateDisplay;
  14. private bool speedDisplayAvailable;
  15. private bool powerDisplayAvailable;
  16. private bool cadenceDisplayAvailable;
  17. private bool torqueDisplayAvailable;
  18. private bool heartRateDisplayAvailable;
  19. private BikeSensorData sensorData;
  20. // Start is called before the first frame update
  21. void Start()
  22. {
  23. sensorData = BikeSensorData.Instance;
  24. //powermeter and ANT hr only for displaying for now TODO: move to controller if used for controlling
  25. sensorData.StartListening(powermeterId: powerMeterId, hrAntId: hrAntId);
  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. void Update()
  34. {
  35. //transform.Translate(Vector3.forward * (Time.deltaTime * (speedSensorReceiver?.Speed ?? 0)));
  36. //bicycleController.SetSpeed(speedSensorReceiver?.Speed ?? 0);
  37. //bicycleController.SetTorque(powerMeterReceiver?.CrankTorque ?? 0); //TODO crank to wheel torque;
  38. if (speedDisplayAvailable)
  39. {
  40. speedDisplay.text = $"{(sensorData.SpeedData?.SpeedKmh ?? 0f):n2} km/h";
  41. }
  42. if (powerDisplayAvailable)
  43. {
  44. powerDisplay.text = $"{(sensorData.PowermeterData?.InstantaneousPower ?? 0f):0} W";
  45. }
  46. if (cadenceDisplayAvailable)
  47. {
  48. cadenceDisplay.text = $"{(sensorData.PowermeterData?.InstantaneousCadence ?? 0f):0} rpm";
  49. }
  50. if (torqueDisplayAvailable)
  51. {
  52. torqueDisplay.text = $"{(sensorData.PowermeterData?.CrankTorque ?? 0f):F2} Nm";
  53. }
  54. if (heartRateDisplayAvailable)
  55. {
  56. heartRateDisplay.text = $"{(sensorData.HrData?.HeartRate ?? 0):0} bpm";
  57. }
  58. }
  59. }