1
0

CadenceDisplay.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using ANT_Managed_Library;
  5. using System;
  6. public class CadenceDisplay : MonoBehaviour {
  7. public bool autoStartScan = true; //start scan on play
  8. public bool connected = false; //will be set to true once connected
  9. public float wheelCircumference = 2.096f; //700*23C, set this to your wheels size
  10. //windows and mac settings
  11. public bool autoConnectToFirstSensorFound = true; //for windows and mac, either connect to the first sensor found or let you pick a sensor manually in the scanResult list with your own UI and call ConnectToDevice(AntDevice device)
  12. public List<AntDevice> scanResult;
  13. //android settings
  14. public bool useAndroidUI = true; //will open the unified ant+ UI on the android app if set to true, otherwise will connect to the first found device
  15. public bool skipPreferredSearch = true; //- True = Don't automatically connect to user's preferred device, but always go to search for other devices.
  16. //the sensor values we receive fron the onReceiveData event
  17. public int cadence; // The cadence in rpm
  18. private AntChannel backgroundScanChannel;
  19. private AntChannel deviceChannel;
  20. private int stopRevCounter_cadence = 0;
  21. private int prev_measTime_cadence = 0;
  22. private int prev_revCount_cadence = 0;
  23. public int deviceID = 0; //set this to connect to a specific device ID
  24. void Start() {
  25. if (autoStartScan)
  26. StartScan();
  27. }
  28. //Start a background Scan to find the device
  29. public void StartScan() {
  30. Debug.Log("Looking for ANT + Cadence sensor");
  31. #if UNITY_ANDROID && !UNITY_EDITOR
  32. //Java: connect_cadence(String gameobjectName, float wheel, boolean useAndroidUI, boolean skipPreferredSearch, int deviceID, boolean speedCadence ) {
  33. AndroidJNI.AttachCurrentThread();
  34. using (AndroidJavaClass javaClass = new AndroidJavaClass("com.ant.plugin.Ant_Connector")) {
  35. using (AndroidJavaObject activity = javaClass.GetStatic<AndroidJavaObject>("mContext")) {
  36. activity.Call("connect_cadence", this.gameObject.name, wheelCircumference, useAndroidUI,skipPreferredSearch,deviceID, false);
  37. }
  38. }
  39. #else
  40. AntManager.Instance.Init();
  41. scanResult = new List<AntDevice>();
  42. backgroundScanChannel = AntManager.Instance.OpenBackgroundScanChannel(0);
  43. backgroundScanChannel.onReceiveData += ReceivedBackgroundScanData;
  44. #endif
  45. }
  46. //Android function
  47. void ANTPLUG_ConnectEvent(string resultCode) {
  48. switch (resultCode) {
  49. case "SUCCESS":
  50. connected = true;
  51. break;
  52. case "CHANNEL_NOT_AVAILABLE":
  53. //Channel Not Available
  54. break;
  55. case "ADAPTER_NOT_DETECTED":
  56. //ANT Adapter Not Available. Built-in ANT hardware or external adapter required.
  57. Debug.Log("ANT Adapter Not Available. Built-in ANT hardware or external adapter required.");
  58. break;
  59. case "BAD_PARAMS":
  60. //Bad request parameters.
  61. break;
  62. case "OTHER_FAILUR":
  63. //RequestAccess failed. See logcat for details.
  64. break;
  65. case "DEPENDENCY_NOT_INSTALLED":
  66. //You need to install the ANT+ Plugins service or you may need to update your existing version if you already have it.
  67. case "USER_CANCELLED":
  68. //USER_CANCELLED
  69. break;
  70. case "UNRECOGNIZED":
  71. //UNRECOGNIZED. PluginLib Upgrade Required?",
  72. break;
  73. default:
  74. //UNRECOGNIZED
  75. break;
  76. }
  77. }
  78. void ANTPLUG_StateChange(string newDeviceState) {
  79. switch (newDeviceState) {
  80. case "DEAD":
  81. connected = false;
  82. break;
  83. case "CLOSED":
  84. break;
  85. case "SEARCHING":
  86. //searching
  87. break;
  88. case "TRACKING":
  89. //tracking
  90. break;
  91. case "PROCESSING_REQUEST":
  92. break;
  93. default:
  94. //UNRECOGNIZED
  95. break;
  96. }
  97. }
  98. void ANTPLUG_Receive_calculatedCadence(string s) {
  99. cadence = (int)float.Parse(s);
  100. }
  101. //Windows and mac
  102. //If the device is found
  103. void ReceivedBackgroundScanData(Byte[] data) {
  104. byte deviceType = (data[12]); // extended info Device Type byte
  105. switch (deviceType) {
  106. case AntplusDeviceType.BikeCadence: {
  107. int deviceNumber = (data[10]) | data[11] << 8;
  108. byte transType = data[13];
  109. foreach (AntDevice d in scanResult) {
  110. if (d.deviceNumber == deviceNumber && d.transType == transType) //device already found
  111. return;
  112. }
  113. Debug.Log("Cadence sensor found " + deviceNumber);
  114. AntDevice foundDevice = new AntDevice();
  115. foundDevice.deviceType = deviceType;
  116. foundDevice.deviceNumber = deviceNumber;
  117. foundDevice.transType = transType;
  118. foundDevice.period = 8102;
  119. foundDevice.radiofreq = 57;
  120. foundDevice.name = "BikeCadence(" + foundDevice.deviceNumber + ")";
  121. scanResult.Add(foundDevice);
  122. if (autoConnectToFirstSensorFound) {
  123. ConnectToDevice(foundDevice);
  124. }
  125. break;
  126. }
  127. default: {
  128. break;
  129. }
  130. }
  131. }
  132. void ConnectToDevice(AntDevice device) {
  133. AntManager.Instance.CloseBackgroundScanChannel();
  134. byte channelID = AntManager.Instance.GetFreeChannelID();
  135. deviceChannel = AntManager.Instance.OpenChannel(ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00, channelID, (ushort)device.deviceNumber, device.deviceType, device.transType, (byte)device.radiofreq, (ushort)device.period, false);
  136. connected = true;
  137. deviceChannel.onReceiveData += Data;
  138. deviceChannel.onChannelResponse += ChannelResponse;
  139. deviceChannel.hideRXFAIL = true;
  140. }
  141. //Deal with the received Data
  142. public void Data(Byte[] data) {
  143. //CADENCE
  144. int measTime_cadence = (data[4]) | data[5] << 8;
  145. int revCount_cadence = (data[6]) | data[7] << 8;
  146. if (prev_measTime_cadence != 0 && measTime_cadence != prev_measTime_cadence && prev_measTime_cadence < measTime_cadence && prev_revCount_cadence < revCount_cadence) {
  147. cadence = (60 * (revCount_cadence - prev_revCount_cadence) * 1024) / (measTime_cadence - prev_measTime_cadence);
  148. stopRevCounter_cadence = 0;
  149. } else
  150. stopRevCounter_cadence++;
  151. if (stopRevCounter_cadence >= 5) {
  152. stopRevCounter_cadence = 5;
  153. cadence = 0;
  154. }
  155. prev_measTime_cadence = measTime_cadence;
  156. prev_revCount_cadence = revCount_cadence;
  157. }
  158. void ChannelResponse(ANT_Response response) {
  159. }
  160. }