BikeDataDisplay.cs 2.6 KB

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