PolarReceiver.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Serialization;
  4. [System.Serializable]
  5. public struct PolarSensorConfig
  6. {
  7. public int port;
  8. public String ipAddress;
  9. public PolarSensorConfig(int port = 9099, string ipAddress = "0.0.0.0")
  10. {
  11. this.port = port;
  12. this.ipAddress = ipAddress;
  13. }
  14. }
  15. public struct PolarSensorData
  16. {
  17. public Vector3 Acc;
  18. public float EcgValue;
  19. }
  20. public class PolarReceiver
  21. {
  22. private UdpConnection connection;
  23. private PolarSensorData sensorData;
  24. public PolarSensorConfig SensorConfig { get; private set; }
  25. public PolarSensorData SensorData => sensorData;
  26. public PolarReceiver()
  27. {
  28. }
  29. public PolarReceiver(PolarSensorConfig config, bool autoStart = true)
  30. {
  31. SensorConfig = config;
  32. if (autoStart) StartListening();
  33. }
  34. public void StartListening()
  35. {
  36. connection = new UdpConnection(SensorConfig.ipAddress, SensorConfig.port, OnAccData);
  37. connection.Listen();
  38. }
  39. public void Dispose()
  40. {
  41. connection.StopListening();
  42. }
  43. private void OnAccData(AccData data)
  44. {
  45. //Debug.Log(data.Timestamp);
  46. sensorData.Acc = data.Values[0]; //TODO: what about rest of array?
  47. }
  48. //TODO: OnEcgData
  49. }