HrReceiver.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using ANT_Managed_Library;
  4. using UnityEngine;
  5. namespace Sensors.ANT
  6. {
  7. public struct HrSensorData
  8. {
  9. public float HeartRate;
  10. }
  11. public class HrReceiver
  12. {
  13. private AntChannel backgroundScanChannel;
  14. private AntChannel deviceChannel;
  15. private HrSensorData sensorData;
  16. public int DeviceID { get; } = 0;
  17. public bool Connected { get; private set; } = false;
  18. public List<AntDevice> ScanResult { get; private set; }
  19. public HrSensorData SensorData => sensorData;
  20. public HrReceiver()
  21. {
  22. }
  23. public HrReceiver(int deviceID) => this.DeviceID = deviceID;
  24. //Start a background Scan to find the device
  25. public void StartScan()
  26. {
  27. Debug.Log("Looking for ANT + HeartRate sensor");
  28. AntManager.Instance.Init();
  29. ScanResult = new List<AntDevice>();
  30. backgroundScanChannel = AntManager.Instance.OpenBackgroundScanChannel(0);
  31. backgroundScanChannel.onReceiveData += ReceivedBackgroundScanData;
  32. }
  33. void ReceivedBackgroundScanData(Byte[] data)
  34. {
  35. byte deviceType = (data[12]); // extended info Device Type byte
  36. switch (deviceType)
  37. {
  38. case AntplusDeviceType.HeartRate:
  39. {
  40. int deviceNumber = (data[10]) | data[11] << 8;
  41. byte transType = data[13];
  42. foreach (AntDevice d in ScanResult)
  43. {
  44. if (d.deviceNumber == deviceNumber && d.transType == transType) //device already found
  45. return;
  46. }
  47. AntDevice 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. void ConnectToDevice(AntDevice device)
  69. {
  70. AntManager.Instance.CloseBackgroundScanChannel();
  71. byte 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. void ChannelResponse(ANT_Response response)
  86. {
  87. }
  88. }
  89. }