SpeedCadenceDisplay.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using ANT_Managed_Library;
  5. using System;
  6. public class SpeedCadenceDisplay : 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 float speed; // The speed in km/h
  18. public int cadence; // The cadence in rpm
  19. public float distance; //the distance in meters
  20. private AntChannel backgroundScanChannel;
  21. private AntChannel deviceChannel;
  22. private int stopRevCounter_speed = 0;
  23. private int stopRevCounter_cadence = 0;
  24. private int prev_measTime_speed = 0;
  25. private int prev_revCount_speed = 0;
  26. private int prev_measTime_cadence = 0;
  27. private int prev_revCount_cadence = 0;
  28. private int revCountZero = 0;
  29. public int deviceID = 0; //set this to connect to a specific device ID
  30. void Start() {
  31. if (autoStartScan)
  32. StartScan();
  33. }
  34. //Start a background Scan to find the device
  35. public void StartScan() {
  36. Debug.Log("Looking for ANT + Speed and Cadence sensor");
  37. #if UNITY_ANDROID && !UNITY_EDITOR
  38. //connect_cadence(String gameobjectName, float wheel, boolean useAndroidUI, boolean skipPreferredSearch, int deviceID, boolean speedCadence )
  39. //last param must be true for speed and cadence
  40. AndroidJNI.AttachCurrentThread();
  41. using (AndroidJavaClass javaClass = new AndroidJavaClass("com.ant.plugin.Ant_Connector")) {
  42. using (AndroidJavaObject activity = javaClass.GetStatic<AndroidJavaObject>("mContext")) {
  43. activity.Call("connect_cadence", this.gameObject.name, wheelCircumference,useAndroidUI, skipPreferredSearch,deviceID, true);
  44. }
  45. }
  46. #else
  47. AntManager.Instance.Init();
  48. scanResult = new List<AntDevice>();
  49. backgroundScanChannel = AntManager.Instance.OpenBackgroundScanChannel(0);
  50. backgroundScanChannel.onReceiveData += ReceivedBackgroundScanData;
  51. #endif
  52. }
  53. //Android function
  54. void ANTPLUG_ConnectEvent(string resultCode) {
  55. switch (resultCode) {
  56. case "SUCCESS":
  57. connected = true;
  58. break;
  59. case "CHANNEL_NOT_AVAILABLE":
  60. //Channel Not Available
  61. break;
  62. case "ADAPTER_NOT_DETECTED":
  63. //ANT Adapter Not Available. Built-in ANT hardware or external adapter required.
  64. Debug.Log("ANT Adapter Not Available. Built-in ANT hardware or external adapter required.");
  65. break;
  66. case "BAD_PARAMS":
  67. //Bad request parameters.
  68. break;
  69. case "OTHER_FAILUR":
  70. //RequestAccess failed. See logcat for details.
  71. break;
  72. case "DEPENDENCY_NOT_INSTALLED":
  73. //You need to install the ANT+ Plugins service or you may need to update your existing version if you already have it.
  74. case "USER_CANCELLED":
  75. //USER_CANCELLED
  76. break;
  77. case "UNRECOGNIZED":
  78. //UNRECOGNIZED. PluginLib Upgrade Required?",
  79. break;
  80. default:
  81. //UNRECOGNIZED
  82. break;
  83. }
  84. }
  85. void ANTPLUG_StateChange(string newDeviceState) {
  86. switch (newDeviceState) {
  87. case "DEAD":
  88. connected = false;
  89. break;
  90. case "CLOSED":
  91. break;
  92. case "SEARCHING":
  93. //searching
  94. break;
  95. case "TRACKING":
  96. //tracking
  97. break;
  98. case "PROCESSING_REQUEST":
  99. break;
  100. default:
  101. //UNRECOGNIZED
  102. break;
  103. }
  104. }
  105. void ANTPLUG_Receive_calculatedCadence(string s) {
  106. cadence = (int)float.Parse(s) * 2;
  107. }
  108. void ANTPLUG_Receive_calculatedSpeed(string s) {
  109. speed = float.Parse(s) *3.6f;
  110. }
  111. void ANTPLUG_Receive_CalculatedAccumulatedDistance(string s) {
  112. distance = float.Parse(s);
  113. }
  114. //Windows and mac
  115. //If the device is found
  116. void ReceivedBackgroundScanData(Byte[] data) {
  117. byte deviceType = (data[12]); // extended info Device Type byte
  118. switch (deviceType) {
  119. case AntplusDeviceType.BikeSpeedCadence: {
  120. int deviceNumber = (data[10]) | data[11] << 8;
  121. byte transType = data[13];
  122. foreach (AntDevice d in scanResult) {
  123. if (d.deviceNumber == deviceNumber && d.transType == transType) //device already found
  124. return;
  125. }
  126. Debug.Log("Speed & Cadence sensor found " + deviceNumber);
  127. AntDevice foundDevice = new AntDevice();
  128. foundDevice.deviceType = deviceType;
  129. foundDevice.deviceNumber = deviceNumber;
  130. foundDevice.transType = transType;
  131. foundDevice.period = 8086;
  132. foundDevice.radiofreq = 57;
  133. foundDevice.name = "BikeSpeedCadence(" + foundDevice.deviceNumber+")";
  134. scanResult.Add(foundDevice);
  135. if (autoConnectToFirstSensorFound) {
  136. ConnectToDevice(foundDevice);
  137. }
  138. break;
  139. }
  140. default: {
  141. break;
  142. }
  143. }
  144. }
  145. void ConnectToDevice(AntDevice device) {
  146. AntManager.Instance.CloseBackgroundScanChannel();
  147. byte channelID = AntManager.Instance.GetFreeChannelID();
  148. 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);
  149. connected = true;
  150. deviceChannel.onReceiveData += Data;
  151. deviceChannel.onChannelResponse += ChannelResponse;
  152. deviceChannel.hideRXFAIL = true;
  153. }
  154. //Deal with the received Data
  155. public void Data(Byte[] data) {
  156. //SPEED
  157. int measTime_speed = (data[4]) | data[5] << 8;
  158. int revCount_speed = (data[6]) | data[7] << 8;
  159. if (prev_measTime_speed != 0 && measTime_speed != prev_measTime_speed && prev_measTime_speed < measTime_speed && prev_revCount_speed < revCount_speed) {
  160. speed = (wheelCircumference * (revCount_speed - prev_revCount_speed) * 1024) / (measTime_speed - prev_measTime_speed);
  161. speed *= 3.6f; // km/h
  162. stopRevCounter_speed = 0;
  163. } else
  164. stopRevCounter_speed++;
  165. if (stopRevCounter_speed >= 5) {
  166. stopRevCounter_speed = 5;
  167. speed = 0;
  168. }
  169. prev_measTime_speed = measTime_speed;
  170. prev_revCount_speed = revCount_speed;
  171. //CADENCE
  172. int measTime_cadence = (data[0]) | data[1] << 8;
  173. int revCount_cadence = (data[2]) | data[3] << 8;
  174. if (prev_measTime_cadence != 0 && measTime_cadence != prev_measTime_cadence && prev_measTime_cadence < measTime_cadence && prev_revCount_cadence < revCount_cadence) {
  175. cadence = (60 * (revCount_cadence - prev_revCount_cadence) * 1024) / (measTime_cadence - prev_measTime_cadence);
  176. stopRevCounter_cadence = 0;
  177. } else
  178. stopRevCounter_cadence++;
  179. if (stopRevCounter_cadence >= 5) {
  180. stopRevCounter_cadence = 5;
  181. cadence = 0;
  182. }
  183. prev_measTime_cadence = measTime_cadence;
  184. prev_revCount_cadence = revCount_cadence;
  185. //DISTANCE
  186. if (revCountZero == 0)
  187. revCountZero = revCount_speed;
  188. distance = wheelCircumference * (revCount_speed - revCountZero);
  189. }
  190. void ChannelResponse(ANT_Response response) {
  191. }
  192. }