BikeDataDisplay.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using Sensors;
  2. using TMPro;
  3. using UnityEngine;
  4. namespace Display
  5. {
  6. public class BikeDataDisplay : MonoBehaviour
  7. {
  8. public TextMeshProUGUI cadenceDisplay;
  9. private bool cadenceDisplayAvailable;
  10. public TextMeshProUGUI heartRateDisplay;
  11. private bool heartRateDisplayAvailable;
  12. [Header("Heart Rate")] public int hrAntId = 0;
  13. public TextMeshProUGUI powerDisplay;
  14. private bool powerDisplayAvailable;
  15. [Header("Powermeter")] public int powerMeterId = 0;
  16. private BikeSensorData sensorData;
  17. [Header("Speed")] public TextMeshProUGUI speedDisplay;
  18. private bool speedDisplayAvailable;
  19. public TextMeshProUGUI torqueDisplay;
  20. private bool torqueDisplayAvailable;
  21. // Start is called before the first frame update
  22. private void Start()
  23. {
  24. sensorData = BikeSensorData.Instance;
  25. //powermeter and ANT hr only for displaying for now TODO: move to controller if used for controlling
  26. sensorData.StartListening(powermeterId: powerMeterId, hrAntId: hrAntId);
  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. //transform.Translate(Vector3.forward * (Time.deltaTime * (speedSensorReceiver?.Speed ?? 0)));
  37. //bicycleController.SetSpeed(speedSensorReceiver?.Speed ?? 0);
  38. //bicycleController.SetTorque(powerMeterReceiver?.CrankTorque ?? 0); //TODO crank to wheel torque;
  39. if (speedDisplayAvailable) speedDisplay.text = $"{sensorData.SpeedData?.SpeedKmh ?? 0f: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) heartRateDisplay.text = $"{sensorData.HrData?.HeartRate ?? 0:0} bpm";
  45. }
  46. }
  47. }