1
0

AntChannel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System;
  4. using System.Collections;
  5. using ANT_Managed_Library;
  6. using System.Text;
  7. using System.Collections.Generic;
  8. ////////////////////////////////////////////////////////////////////////////////
  9. // AntManager
  10. //
  11. // Call the main Init method from your script to open a channel.
  12. // Ant responses and received DATA are queued when received and dequeued in the update loop by triggering events you can register on your script
  13. //
  14. ////////////////////////////////////////////////////////////////////////////////
  15. public class AntChannel : MonoBehaviour
  16. {
  17. public ANT_Device device;
  18. ANT_Channel channel;
  19. public byte[] txBuffer = { 0, 0, 0, 0, 0, 0, 0, 0 };
  20. bool broadcasting;
  21. Queue<byte[]> RXQueue;
  22. Queue<ANT_Response> messageQueue;
  23. public delegate void OnReceiveData(byte[] data);
  24. public event OnReceiveData onReceiveData; // data event
  25. public delegate void OnChannelResponse(ANT_Response response);
  26. public event OnChannelResponse onChannelResponse; //ant response event
  27. //channel configuration
  28. ANT_ReferenceLibrary.ChannelType channelType;
  29. public byte userChannel; //{ get; private set; }
  30. ushort deviceNum;
  31. byte deviceType;
  32. byte transType;
  33. byte radioFreq;
  34. ushort channelPeriod;
  35. bool pairing;
  36. bool isBackgroundScan = false;
  37. public bool hideRXFAIL = false;
  38. void Update() {
  39. if (RXQueue != null && RXQueue.Count > 0) {
  40. if (onReceiveData != null)
  41. onReceiveData(RXQueue.Dequeue());
  42. }
  43. if (messageQueue != null && messageQueue.Count > 0) {
  44. if (onChannelResponse != null)
  45. onChannelResponse(messageQueue.Dequeue());
  46. }
  47. }
  48. ////////////////////////////////////////////////////////////////////////////////
  49. // ConfigureAnt
  50. //
  51. // You can find how to initialize devices on thisisant.com in the download documents section
  52. // ANT+ DEVICE PROFILES
  53. //
  54. ////////////////////////////////////////////////////////////////////////////////
  55. public void ConfigureAnt(ANT_ReferenceLibrary.ChannelType channelType, byte userChannel, ushort deviceNum, byte deviceType, byte transType, byte radioFreq, ushort channelPeriod, bool pairing, int USBNum)
  56. {
  57. this.channelType = channelType;
  58. this.userChannel = userChannel;
  59. this.deviceNum = deviceNum;
  60. this.deviceType = deviceType;
  61. this.transType = transType;
  62. this.radioFreq = radioFreq;
  63. this.channelPeriod = channelPeriod;
  64. this.pairing = pairing;
  65. RXQueue = new Queue<byte[]>(16);
  66. messageQueue = new Queue<ANT_Response>(16);
  67. device = AntManager.Instance.devices[USBNum];
  68. channel = device.getChannel(userChannel);
  69. channel.channelResponse += new dChannelResponseHandler(ChannelResponse);
  70. channel.assignChannel(channelType, 0, 0);
  71. channel.setChannelID(deviceNum, pairing, deviceType, transType, 0);
  72. channel.setChannelFreq(radioFreq, 0);
  73. channel.setChannelPeriod(channelPeriod, 0);
  74. channel.setLowPrioritySearchTimeout(0);
  75. isBackgroundScan = false;
  76. channel.openChannel();
  77. broadcasting = true;
  78. }
  79. public void ConfigureScan(byte userChannel, ushort USBNum)
  80. {
  81. this.userChannel = userChannel;
  82. RXQueue = new Queue<byte[]>(16);
  83. messageQueue = new Queue<ANT_Response>(16);
  84. device = AntManager.Instance.devices[USBNum];
  85. device.enableRxExtendedMessages(true, 500);
  86. channel = device.getChannel(userChannel);
  87. channel.channelResponse += new dChannelResponseHandler(ChannelResponse);
  88. channel.assignChannelExt(ANT_ReferenceLibrary.ChannelType.ADV_TxRx_Only_or_RxAlwaysWildCard_0x40, 0, ANT_ReferenceLibrary.ChannelTypeExtended.ADV_AlwaysSearch_0x01, 500);
  89. channel.setChannelID(0, false, 0, 0, 500);
  90. channel.setChannelFreq(57, 500);
  91. channel.setChannelSearchTimeout(0);
  92. channel.setLowPrioritySearchTimeout((byte)0xFF);
  93. isBackgroundScan = true;
  94. channel.openChannel();
  95. broadcasting = true;
  96. }
  97. public void ConfigureContinuousScan(ANT_ReferenceLibrary.ChannelType channelType, byte radioFreq, ushort USBNum) {
  98. userChannel = 0;
  99. RXQueue = new Queue<byte[]>(16);
  100. messageQueue = new Queue<ANT_Response>(16);
  101. device = AntManager.Instance.devices[USBNum];
  102. device.enableRxExtendedMessages(true, 500);
  103. channel = device.getChannel(0);
  104. channel.channelResponse += new dChannelResponseHandler(ChannelResponse);
  105. channel.assignChannelExt(ANT_ReferenceLibrary.ChannelType.ADV_TxRx_Only_or_RxAlwaysWildCard_0x40, 0, ANT_ReferenceLibrary.ChannelTypeExtended.ADV_AlwaysSearch_0x01, 500);
  106. channel.setChannelID(0, false, 0, 0, 500);
  107. channel.setChannelFreq(radioFreq, 500);
  108. channel.setChannelSearchTimeout(0);
  109. channel.setLowPrioritySearchTimeout((byte)0xFF);
  110. isBackgroundScan = true;
  111. channel.openChannel();
  112. device.openRxScanMode();
  113. broadcasting = true;
  114. }
  115. public void Close() {
  116. if (channel != null) {
  117. channel.closeChannel();
  118. channel.Dispose();
  119. }
  120. if (isBackgroundScan) {
  121. device.enableRxExtendedMessages(false, 500);
  122. isBackgroundScan = false;
  123. }
  124. broadcasting = false;
  125. AntManager.Instance.channelList.Remove(this);
  126. AntManager.Instance.channelIDUsed[device.getOpenedUSBDeviceNum(), userChannel] = false;
  127. Destroy(this);
  128. }
  129. public void PauseChannel() {
  130. broadcasting = false;
  131. }
  132. public void ReOpen() {
  133. if (broadcasting)
  134. return;
  135. if (!isBackgroundScan)
  136. ConfigureAnt(channelType, userChannel, deviceNum, deviceType, transType, radioFreq, channelPeriod, pairing, device.getOpenedUSBDeviceNum());
  137. else
  138. ConfigureScan(userChannel, (ushort)device.getOpenedUSBDeviceNum());
  139. }
  140. public void ReOpen(ANT_Device device)
  141. {
  142. if (broadcasting)
  143. return;
  144. this.device = device;
  145. if (!isBackgroundScan)
  146. ConfigureAnt(channelType, userChannel, deviceNum, deviceType, transType, radioFreq, channelPeriod, pairing, device.getOpenedUSBDeviceNum());
  147. else
  148. ConfigureScan(userChannel, (ushort)device.getOpenedUSBDeviceNum());
  149. }
  150. public void sendAcknowledgedData(byte[] data) {
  151. channel.sendAcknowledgedData(data);
  152. }
  153. ////////////////////////////////////////////////////////////////////////////////
  154. // ChannelResponse
  155. //
  156. // Called whenever a channel event is received.
  157. //
  158. // response: ANT message
  159. ////////////////////////////////////////////////////////////////////////////////
  160. void ChannelResponse(ANT_Response response) {
  161. //With the messageQueue we can deal with ANT response in the Unity main thread
  162. if (response.responseID == (byte)ANT_ReferenceLibrary.ANTMessageID.RESPONSE_EVENT_0x40)
  163. messageQueue.Enqueue(response);
  164. try {
  165. switch ((ANT_ReferenceLibrary.ANTMessageID)response.responseID) {
  166. case ANT_ReferenceLibrary.ANTMessageID.RESPONSE_EVENT_0x40: {
  167. switch (response.getChannelEventCode()) {
  168. // This event indicates that a message has just been
  169. // sent over the air. We take advantage of this event to set
  170. // up the data for the next message period.
  171. case ANT_ReferenceLibrary.ANTEventID.EVENT_TX_0x03: {
  172. // Broadcast data will be sent over the air on
  173. // the next message period
  174. if (broadcasting) {
  175. channel.sendBroadcastData(txBuffer);
  176. }
  177. break;
  178. }
  179. case ANT_ReferenceLibrary.ANTEventID.EVENT_RX_SEARCH_TIMEOUT_0x01: {
  180. Debug.Log("Search Timeout");
  181. break;
  182. }
  183. case ANT_ReferenceLibrary.ANTEventID.EVENT_RX_FAIL_0x02: {
  184. if (!hideRXFAIL)
  185. Debug.Log("Rx Fail");
  186. break;
  187. }
  188. case ANT_ReferenceLibrary.ANTEventID.EVENT_TRANSFER_RX_FAILED_0x04: {
  189. Debug.Log("Burst receive has failed");
  190. break;
  191. }
  192. case ANT_ReferenceLibrary.ANTEventID.EVENT_TRANSFER_TX_COMPLETED_0x05: {
  193. Debug.Log("Transfer Completed");
  194. break;
  195. }
  196. case ANT_ReferenceLibrary.ANTEventID.EVENT_TRANSFER_TX_FAILED_0x06: {
  197. Debug.Log("Transfer Failed");
  198. break;
  199. }
  200. case ANT_ReferenceLibrary.ANTEventID.EVENT_CHANNEL_CLOSED_0x07: {
  201. channel.unassignChannel(500);
  202. break;
  203. }
  204. case ANT_ReferenceLibrary.ANTEventID.EVENT_RX_FAIL_GO_TO_SEARCH_0x08: {
  205. Debug.Log("Go to Search");
  206. break;
  207. }
  208. case ANT_ReferenceLibrary.ANTEventID.EVENT_CHANNEL_COLLISION_0x09: {
  209. Debug.Log("Channel Collision");
  210. break;
  211. }
  212. case ANT_ReferenceLibrary.ANTEventID.EVENT_TRANSFER_TX_START_0x0A: {
  213. Debug.Log("Burst Started");
  214. break;
  215. }
  216. default: {
  217. Debug.Log("Unhandled Channel Event " + response.getChannelEventCode());
  218. break;
  219. }
  220. }
  221. break;
  222. }
  223. case ANT_ReferenceLibrary.ANTMessageID.BROADCAST_DATA_0x4E:
  224. case ANT_ReferenceLibrary.ANTMessageID.ACKNOWLEDGED_DATA_0x4F:
  225. case ANT_ReferenceLibrary.ANTMessageID.BURST_DATA_0x50:
  226. case ANT_ReferenceLibrary.ANTMessageID.EXT_BROADCAST_DATA_0x5D:
  227. case ANT_ReferenceLibrary.ANTMessageID.EXT_ACKNOWLEDGED_DATA_0x5E:
  228. case ANT_ReferenceLibrary.ANTMessageID.EXT_BURST_DATA_0x5F: {
  229. if (response.isExtended() && isBackgroundScan == true)
  230. RXQueue.Enqueue(response.messageContents);
  231. else
  232. RXQueue.Enqueue(response.getDataPayload());
  233. break;
  234. }
  235. default: {
  236. Debug.Log("Unknown Message " + response.responseID);
  237. break;
  238. }
  239. }
  240. } catch (Exception ex) {
  241. Debug.Log("Channel response processing failed with exception: " + ex.Message);
  242. }
  243. }
  244. }