HrReceiver.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System.Collections.Generic;
  2. using ANT_Managed_Library;
  3. using UnityEngine;
  4. namespace Sensors.ANT
  5. {
  6. public struct HrSensorData
  7. {
  8. public float HeartRate;
  9. }
  10. public class HrReceiver
  11. {
  12. private AntChannel backgroundScanChannel;
  13. private AntChannel deviceChannel;
  14. private HrSensorData sensorData;
  15. public HrReceiver()
  16. {
  17. }
  18. public HrReceiver(int deviceID)
  19. {
  20. DeviceID = deviceID;
  21. }
  22. public int DeviceID { get; }
  23. public bool Connected { get; private set; }
  24. public List<AntDevice> ScanResult { get; private set; }
  25. public HrSensorData SensorData => sensorData;
  26. //Start a background Scan to find the device
  27. public void StartScan()
  28. {
  29. Debug.Log("Looking for ANT + HeartRate sensor");
  30. AntManager.Instance.Init();
  31. ScanResult = new List<AntDevice>();
  32. backgroundScanChannel = AntManager.Instance.OpenBackgroundScanChannel(0);
  33. backgroundScanChannel.onReceiveData += ReceivedBackgroundScanData;
  34. }
  35. private void ReceivedBackgroundScanData(byte[] data)
  36. {
  37. var deviceType = data[12]; // extended info Device Type byte
  38. switch (deviceType)
  39. {
  40. case AntplusDeviceType.HeartRate:
  41. {
  42. var deviceNumber = data[10] | (data[11] << 8);
  43. var transType = data[13];
  44. foreach (var d in ScanResult)
  45. if (d.deviceNumber == deviceNumber && d.transType == transType) //device already found
  46. return;
  47. var foundDevice = new AntDevice();
  48. foundDevice.deviceType = deviceType;
  49. foundDevice.deviceNumber = deviceNumber;
  50. foundDevice.transType = transType;
  51. foundDevice.period = 8070;
  52. foundDevice.radiofreq = 57;
  53. foundDevice.name = "heartrate(" + foundDevice.deviceNumber + ")";
  54. ScanResult.Add(foundDevice);
  55. if (deviceNumber == DeviceID)
  56. {
  57. Debug.Log($"Desired HR Sensor with id {deviceNumber} found!");
  58. ConnectToDevice(foundDevice);
  59. }
  60. else
  61. {
  62. Debug.Log($"HR sensor ({deviceNumber}) found and added to ScanResult");
  63. }
  64. break;
  65. }
  66. }
  67. }
  68. private void ConnectToDevice(AntDevice device)
  69. {
  70. AntManager.Instance.CloseBackgroundScanChannel();
  71. var channelID = AntManager.Instance.GetFreeChannelID();
  72. deviceChannel = AntManager.Instance.OpenChannel(ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00,
  73. channelID, (ushort) device.deviceNumber, device.deviceType, device.transType, (byte) device.radiofreq,
  74. (ushort) device.period, false);
  75. Connected = true;
  76. deviceChannel.onReceiveData += Data;
  77. deviceChannel.onChannelResponse += ChannelResponse;
  78. deviceChannel.hideRXFAIL = true;
  79. }
  80. //Deal with the received Data
  81. private void Data(byte[] data)
  82. {
  83. sensorData.HeartRate = data[7];
  84. }
  85. private void ChannelResponse(ANT_Response response)
  86. {
  87. }
  88. }
  89. }