1
0

demo.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. /*
  2. This software is subject to the license described in the License.txt file
  3. included with this software distribution. You may not use this file except
  4. in compliance with this license.
  5. Copyright (c) Dynastream Innovations Inc. 2016
  6. All rights reserved.
  7. */
  8. #include <stdio.h>
  9. #include <assert.h>
  10. #include "demo.h"
  11. #include "types.h"
  12. #include "antdefines.h"
  13. #include "antmessage.h"
  14. #include "libant.h"
  15. #include <stdio.h>
  16. #include <assert.h>
  17. #define ENABLE_EXTENDED_MESSAGES // Un - comment to enable extended messages.
  18. #define USER_BAUDRATE (57600) // For AP1, use 50000; for AT3/AP2, use 57600
  19. #define USER_RADIOFREQ (35) // RF Frequency + 2400 MHz
  20. #define USER_ANTCHANNEL (0) // ANT channel to use
  21. #define USER_DEVICENUM (49) // Device number
  22. #define USER_DEVICETYPE (1) // Device type
  23. #define USER_TRANSTYPE (1) // Transmission type
  24. #define USER_NETWORK_KEY {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,}
  25. #define USER_NETWORK_NUM (0) // The network key is assigned to this network number
  26. #define MAX_CHANNEL_EVENT_SIZE (MESG_MAX_SIZE_VALUE) // Channel event buffer size, assumes worst case extended message size
  27. #define MAX_RESPONSE_SIZE (MESG_MAX_SIZE_VALUE) // Protocol response buffer size
  28. // Indexes into message recieved from ANT
  29. #define MESSAGE_BUFFER_DATA1_INDEX ((UCHAR) 0)
  30. #define MESSAGE_BUFFER_DATA2_INDEX ((UCHAR) 1)
  31. #define MESSAGE_BUFFER_DATA3_INDEX ((UCHAR) 2)
  32. #define MESSAGE_BUFFER_DATA4_INDEX ((UCHAR) 3)
  33. #define MESSAGE_BUFFER_DATA5_INDEX ((UCHAR) 4)
  34. #define MESSAGE_BUFFER_DATA6_INDEX ((UCHAR) 5)
  35. #define MESSAGE_BUFFER_DATA7_INDEX ((UCHAR) 6)
  36. #define MESSAGE_BUFFER_DATA8_INDEX ((UCHAR) 7)
  37. #define MESSAGE_BUFFER_DATA9_INDEX ((UCHAR) 8)
  38. #define MESSAGE_BUFFER_DATA10_INDEX ((UCHAR) 9)
  39. #define MESSAGE_BUFFER_DATA11_INDEX ((UCHAR) 10)
  40. #define MESSAGE_BUFFER_DATA12_INDEX ((UCHAR) 11)
  41. #define MESSAGE_BUFFER_DATA13_INDEX ((UCHAR) 12)
  42. #define MESSAGE_BUFFER_DATA14_INDEX ((UCHAR) 13)
  43. // Local Variables
  44. static UCHAR ucChannelType; // Channel type as chosen by user (master or slave)
  45. static UCHAR aucChannelBuffer[MAX_CHANNEL_EVENT_SIZE];
  46. static UCHAR aucResponseBuffer[MAX_RESPONSE_SIZE];
  47. static UCHAR aucTransmitBuffer[ANT_STANDARD_DATA_PAYLOAD_SIZE];
  48. static BOOL bDone;
  49. static BOOL bDisplay;
  50. static BOOL bBroadcasting;
  51. static UCHAR ucDeviceNumber;
  52. // Declare callback functions into the library.
  53. static BOOL Test_ChannelCallback(UCHAR ucChannel_, UCHAR ucEvent_);
  54. static BOOL Test_ResponseCallback(UCHAR ucChannel_, UCHAR ucMessageId_);
  55. static void PrintMenu();
  56. ////////////////////////////////////////////////////////////////////////////////
  57. // main
  58. //
  59. // Usage:
  60. //
  61. // c:\DEMO_DLL.exe [device_no] [channel_type]
  62. //
  63. // ... where
  64. //
  65. // device_no: USB Device port, starting at 0
  66. // channel_type: Master = 0, Slave = 1
  67. //
  68. // ... example
  69. //
  70. // c:\Demo_DLL.exe 0 0
  71. //
  72. // Comment to USB port 0 and open a Master channel
  73. //
  74. // If optional arguements are not supplied, user will
  75. // be prompted to enter these after the program starts.
  76. //
  77. ////////////////////////////////////////////////////////////////////////////////
  78. int main(int argc, char **argv)
  79. {
  80. ucDeviceNumber = 0xFF;
  81. UCHAR ucChannelType = CHANNEL_TYPE_INVALID;
  82. if(argc > 2)
  83. {
  84. ucDeviceNumber = (UCHAR) atoi(argv[1]);
  85. ucChannelType = (UCHAR) atoi(argv[2]);
  86. }
  87. Test_Init(ucDeviceNumber,ucChannelType);
  88. Test_Start();
  89. return 0;
  90. }
  91. ////////////////////////////////////////////////////////////////////////////////
  92. // Test_Init
  93. //
  94. // Initialize test parameters.
  95. //
  96. // ucDeviceNumber_: USB Device Number (0 for first USB stick plugged and so on)
  97. // If not specified on command line, 0xFF is passed in as invalid.
  98. // ucChannelType_: ANT Channel Type. 0 = Master, 1 = Slave
  99. // If not specified, 2 is passed in as invalid.
  100. //
  101. ////////////////////////////////////////////////////////////////////////////////
  102. void Test_Init(UCHAR ucDeviceNumber_, UCHAR ucChannelType_)
  103. {
  104. BOOL bStatus;
  105. // Load the ANT DLL functions.
  106. if(!ANT_Load())
  107. {
  108. printf("Failed to load ANT Library\n");
  109. exit(0);
  110. }
  111. // Get library version
  112. if(ANT_LibVersionSupport())
  113. {
  114. printf("ANT Library Version %s\n", ANT_LibVersion());
  115. }
  116. // If no device number was specified on the command line,
  117. // prompt the user for input.
  118. if(ucDeviceNumber_ == 0xFF)
  119. {
  120. printf("Device number?\n"); fflush(stdout);
  121. scanf("%u", &ucDeviceNumber_);
  122. char st[1024];
  123. fgets(st, sizeof(st), stdin);
  124. sscanf(st, "%u", &ucDeviceNumber_);
  125. }
  126. ucChannelType = ucChannelType_;
  127. bDone = FALSE;
  128. bDisplay = TRUE;
  129. bBroadcasting = FALSE;
  130. // The device number depends on how many USB sticks have been
  131. // plugged into the PC. The first USB stick plugged will be 0
  132. // the next 1 and so on.
  133. //
  134. // The Baud Rate depends on the ANT solution being used. AP1
  135. // is 50000, all others are 57600
  136. bStatus = ANT_Init(ucDeviceNumber_, USER_BAUDRATE);
  137. assert(bStatus);
  138. // Assign callback functions. One for serial message responses
  139. // and the other for channel events. Each channel event can have
  140. // its own callback function defined. Since we are only
  141. // going to open one channel, setup one callback function
  142. // for the channel callback
  143. ANT_AssignResponseFunction(Test_ResponseCallback, aucResponseBuffer);
  144. ANT_AssignChannelEventFunction(USER_ANTCHANNEL,Test_ChannelCallback, aucChannelBuffer);
  145. printf("Initialization was successful!\n"); fflush(stdout);
  146. return;
  147. }
  148. ////////////////////////////////////////////////////////////////////////////////
  149. // Test_Start
  150. //
  151. // Start the Test program.
  152. //
  153. ////////////////////////////////////////////////////////////////////////////////
  154. void Test_Start()
  155. {
  156. // Print out the menu to start
  157. PrintMenu();
  158. // If the channel type has not been set at the command line,
  159. // prompt the user to specify now.
  160. do
  161. {
  162. if(ucChannelType == CHANNEL_TYPE_INVALID)
  163. {
  164. printf("Channel Type? (Master = 0, Slave = 1)\n");
  165. char st[1024];
  166. fgets(st, sizeof(st), stdin);
  167. sscanf(st, "%u", &ucChannelType);
  168. }
  169. if(ucChannelType != 0 && ucChannelType != 1)
  170. {
  171. ucChannelType = CHANNEL_TYPE_INVALID;
  172. printf("Error: invalid channel type\n");
  173. }
  174. } while(ucChannelType == CHANNEL_TYPE_INVALID);
  175. // Reset system
  176. printf("Resetting module...\n");
  177. ANT_ResetSystem(); // Soft reset
  178. ANT_Nap(1000); // Need to delay after a reset
  179. // Set Network key to start of ANT initialization
  180. printf("Setting network key...\n");
  181. UCHAR ucNetKey[8] = USER_NETWORK_KEY;
  182. ANT_SetNetworkKey(USER_NETWORK_NUM, ucNetKey);
  183. while(!bDone)
  184. {
  185. UCHAR ucChar;
  186. char st[1024];
  187. fgets(st, sizeof(st), stdin);
  188. sscanf(st, "%c", &ucChar);
  189. switch(ucChar)
  190. {
  191. case 'M':
  192. case 'm':
  193. {
  194. // Printout options
  195. PrintMenu();
  196. break;
  197. }
  198. case 'Q':
  199. case 'q':
  200. {
  201. // Quit
  202. printf("Closing channel...\n");
  203. bBroadcasting = FALSE;
  204. ANT_CloseChannel(USER_ANTCHANNEL);
  205. break;
  206. }
  207. case 'A':
  208. case 'a':
  209. {
  210. // Send Acknowledged data
  211. UCHAR aucTempBuffer[] = {1,2,3,4,5,6,7,8};
  212. ANT_SendAcknowledgedData(USER_ANTCHANNEL, aucTempBuffer);
  213. break;
  214. }
  215. case 'B':
  216. case 'b':
  217. {
  218. // Send Burst Data (10 packets)
  219. UCHAR aucTempBuffer[8*10];
  220. for (int i = 0; i < 8*10; i++)
  221. aucTempBuffer[i] = i;
  222. ANT_SendBurstTransfer(USER_ANTCHANNEL, aucTempBuffer, 10);
  223. break;
  224. }
  225. case 'r':
  226. case 'R':
  227. {
  228. // Reset system
  229. printf("Resetting module...\n");
  230. ANT_ResetSystem(); // Soft reset
  231. ANT_Nap(1000); // Need to delay after a reset
  232. // Restart the test by setting network key
  233. printf("Setting network key...\n");
  234. ANT_SetNetworkKey(USER_NETWORK_NUM, ucNetKey);
  235. break;
  236. }
  237. case 'c':
  238. case 'C':
  239. {
  240. // Request capabilites.
  241. ANT_RequestMessage(USER_ANTCHANNEL, MESG_CAPABILITIES_ID);
  242. break;
  243. }
  244. case 'v':
  245. case 'V':
  246. {
  247. // Request version
  248. ANT_RequestMessage(USER_ANTCHANNEL, MESG_VERSION_ID);
  249. break;
  250. }
  251. case 'S':
  252. case 's':
  253. {
  254. // Request channel status
  255. ANT_RequestMessage(USER_ANTCHANNEL, MESG_CHANNEL_STATUS_ID);
  256. break;
  257. }
  258. case 'I':
  259. case 'i':
  260. {
  261. // Request channel ID
  262. ANT_RequestMessage(USER_ANTCHANNEL, MESG_CHANNEL_ID_ID);
  263. break;
  264. }
  265. case 'd':
  266. case 'D':
  267. {
  268. bDisplay = !bDisplay;
  269. break;
  270. }
  271. case 'u':
  272. case 'U':
  273. {
  274. // Print out information about the USB device we are connected to
  275. printf("USB Device Description\n");
  276. USHORT usDevicePID;
  277. USHORT usDeviceVID;
  278. UCHAR aucDeviceDescription[256];
  279. UCHAR aucDeviceSerial[256];
  280. // Retrieve info
  281. if(ANT_GetDeviceUSBVID(&usDeviceVID))
  282. {
  283. printf(" VID: 0x%X\n", usDeviceVID);
  284. }
  285. if(ANT_GetDeviceUSBPID(&usDevicePID))
  286. {
  287. printf(" PID: 0x%X\n", usDevicePID);
  288. }
  289. if(ANT_GetDeviceUSBInfo(ucDeviceNumber, aucDeviceDescription, aucDeviceSerial))
  290. {
  291. printf(" Product Description: %s\n", aucDeviceDescription);
  292. printf(" Serial String: %s\n", aucDeviceSerial);
  293. }
  294. break;
  295. }
  296. default:
  297. {
  298. break;
  299. }
  300. }
  301. ANT_Nap(0);
  302. }
  303. // Clean up ANT
  304. printf("Disconnecting module...\n");
  305. ANT_Close();
  306. printf("Demo has completed successfully!\n");
  307. return;
  308. }
  309. ////////////////////////////////////////////////////////////////////////////////
  310. // Test_ChannelCallback
  311. //
  312. // Callback function passed to ANT DLL. Called whenever a channel event
  313. // is recieved.
  314. //
  315. // ucChannel_: Channel number of event
  316. // ucEvent_: Channel event as described in section 9.5.6 of the messaging document
  317. ////////////////////////////////////////////////////////////////////////////////
  318. BOOL Test_ChannelCallback(UCHAR ucChannel_, UCHAR ucEvent_)
  319. {
  320. BOOL bPrintBuffer = FALSE;
  321. UCHAR ucDataOffset = MESSAGE_BUFFER_DATA2_INDEX; // For most data messages
  322. switch(ucEvent_)
  323. {
  324. case EVENT_TX:
  325. {
  326. // This event indicates that a message has just been
  327. // sent over the air. We take advantage of this event to set
  328. // up the data for the next message period.
  329. static UCHAR ucIncrement = 0; // Increment the first byte of the buffer
  330. aucTransmitBuffer[0] = ucIncrement++;
  331. // Broadcast data will be sent over the air on
  332. // the next message period.
  333. if(bBroadcasting)
  334. {
  335. ANT_SendBroadcastData( USER_ANTCHANNEL,aucTransmitBuffer);
  336. // Echo what the data will be over the air on the next message period.
  337. if(bDisplay)
  338. {
  339. printf("Tx:(%d): [%02x],[%02x],[%02x],[%02x],[%02x],[%02x],[%02x],[%02x]\n",
  340. USER_ANTCHANNEL,
  341. aucTransmitBuffer[MESSAGE_BUFFER_DATA1_INDEX],
  342. aucTransmitBuffer[MESSAGE_BUFFER_DATA2_INDEX],
  343. aucTransmitBuffer[MESSAGE_BUFFER_DATA3_INDEX],
  344. aucTransmitBuffer[MESSAGE_BUFFER_DATA4_INDEX],
  345. aucTransmitBuffer[MESSAGE_BUFFER_DATA5_INDEX],
  346. aucTransmitBuffer[MESSAGE_BUFFER_DATA6_INDEX],
  347. aucTransmitBuffer[MESSAGE_BUFFER_DATA7_INDEX],
  348. aucTransmitBuffer[MESSAGE_BUFFER_DATA8_INDEX]);
  349. }
  350. else
  351. {
  352. static int iIndex = 0;
  353. static char ac[] = {'|','/','-','\\'};
  354. printf("Tx: %c\r",ac[iIndex++]); fflush(stdout);
  355. iIndex &= 3;
  356. }
  357. }
  358. break;
  359. }
  360. case EVENT_RX_FLAG_ACKNOWLEDGED:
  361. case EVENT_RX_FLAG_BURST_PACKET:
  362. case EVENT_RX_FLAG_BROADCAST:
  363. {
  364. // This is an event generated by the DLL. It is not strictly
  365. // returned by the ANT part. To enable, must call ANT_RxExtMesgsEnable first.
  366. // This event has a flag at the end of the data payload
  367. // that indicates additional information. Process this here
  368. // and then fall-through to process the data payload.
  369. UCHAR ucFlag = aucChannelBuffer[MESSAGE_BUFFER_DATA10_INDEX];
  370. if(bDisplay && ucFlag & ANT_EXT_MESG_BITFIELD_DEVICE_ID)
  371. {
  372. // Channel ID of the device that we just recieved a message from.
  373. USHORT usDeviceNumber = aucChannelBuffer[MESSAGE_BUFFER_DATA11_INDEX] | (aucChannelBuffer[MESSAGE_BUFFER_DATA12_INDEX] << 8);
  374. UCHAR ucDeviceType = aucChannelBuffer[MESSAGE_BUFFER_DATA13_INDEX];
  375. UCHAR ucTransmissionType = aucChannelBuffer[MESSAGE_BUFFER_DATA14_INDEX];
  376. printf("Chan ID(%d/%d/%d) - ", usDeviceNumber, ucDeviceType, ucTransmissionType);
  377. }
  378. // INTENTIONAL FALLTHROUGH
  379. }
  380. case EVENT_RX_ACKNOWLEDGED:
  381. case EVENT_RX_BURST_PACKET:
  382. case EVENT_RX_BROADCAST:
  383. {
  384. // This is an event generated by the DLL. It is not strictly
  385. // returned by the ANT part.
  386. // Display recieved message
  387. bPrintBuffer = TRUE;
  388. ucDataOffset = MESSAGE_BUFFER_DATA2_INDEX; // For most data messages
  389. if(bDisplay)
  390. {
  391. if(ucEvent_ == EVENT_RX_ACKNOWLEDGED || ucEvent_ == EVENT_RX_FLAG_ACKNOWLEDGED)
  392. printf("Acked Rx:(%d): ", aucChannelBuffer[MESSAGE_BUFFER_DATA1_INDEX]);
  393. else if(ucEvent_ == EVENT_RX_BURST_PACKET || ucEvent_ == EVENT_RX_FLAG_BURST_PACKET)
  394. printf("Burst(0x%02x) Rx:(%d): ", ((aucChannelBuffer[MESSAGE_BUFFER_DATA1_INDEX] & 0xE0) >> 5), aucChannelBuffer[MESSAGE_BUFFER_DATA1_INDEX] & 0x1F );
  395. else
  396. printf("Rx:(%d): ", aucChannelBuffer[MESSAGE_BUFFER_DATA1_INDEX]);
  397. }
  398. break;
  399. }
  400. case EVENT_RX_EXT_ACKNOWLEDGED:
  401. case EVENT_RX_EXT_BURST_PACKET:
  402. case EVENT_RX_EXT_BROADCAST:
  403. {
  404. // This is an event generated by the DLL. It is not strictly
  405. // returned by the ANT part. To enable, must call ANT_RxExtMesgsEnable first.
  406. // The "extended" part of this message is the 4-byte channel
  407. // id of the device that we recieved this message from. This event
  408. // is only available on the AT3. The AP2 uses the EVENT_RX_FLAG_BROADCAST
  409. // as shown above.
  410. // Channel ID of the device that we just recieved a message from.
  411. USHORT usDeviceNumber = aucChannelBuffer[MESSAGE_BUFFER_DATA2_INDEX] | (aucChannelBuffer[MESSAGE_BUFFER_DATA3_INDEX] << 8);
  412. UCHAR ucDeviceType = aucChannelBuffer[MESSAGE_BUFFER_DATA4_INDEX];
  413. UCHAR ucTransmissionType = aucChannelBuffer[MESSAGE_BUFFER_DATA5_INDEX];
  414. bPrintBuffer = TRUE;
  415. ucDataOffset = MESSAGE_BUFFER_DATA6_INDEX; // For most data messages
  416. if(bDisplay)
  417. {
  418. // Display the channel id
  419. printf("Chan ID(%d/%d/%d) ", usDeviceNumber, ucDeviceType, ucTransmissionType );
  420. if(ucEvent_ == EVENT_RX_EXT_ACKNOWLEDGED)
  421. printf("- Acked Rx:(%d): ", aucChannelBuffer[MESSAGE_BUFFER_DATA1_INDEX]);
  422. else if(ucEvent_ == EVENT_RX_EXT_BURST_PACKET)
  423. printf("- Burst(0x%02x) Rx:(%d): ", ((aucChannelBuffer[MESSAGE_BUFFER_DATA1_INDEX] & 0xE0) >> 5), aucChannelBuffer[MESSAGE_BUFFER_DATA1_INDEX] & 0x1F );
  424. else
  425. printf("- Rx:(%d): ", aucChannelBuffer[MESSAGE_BUFFER_DATA1_INDEX]);
  426. }
  427. break;
  428. }
  429. case EVENT_RX_SEARCH_TIMEOUT:
  430. {
  431. printf("Search Timeout\n");
  432. break;
  433. }
  434. case EVENT_RX_FAIL:
  435. {
  436. printf("Rx Fail\n");
  437. break;
  438. }
  439. case EVENT_TRANSFER_RX_FAILED:
  440. {
  441. printf("Burst receive has failed\n");
  442. break;
  443. }
  444. case EVENT_TRANSFER_TX_COMPLETED:
  445. {
  446. printf("Tranfer Completed.\n");
  447. break;
  448. }
  449. case EVENT_TRANSFER_TX_FAILED:
  450. {
  451. printf("Tranfer Failed.\n");
  452. break;
  453. }
  454. case EVENT_CHANNEL_CLOSED:
  455. {
  456. // This event should be used to determine that the channel is closed.
  457. printf("Channel Closed\n");
  458. printf("Unassigning Channel...\n");
  459. ANT_UnAssignChannel(USER_ANTCHANNEL);
  460. break;
  461. }
  462. case EVENT_RX_FAIL_GO_TO_SEARCH:
  463. {
  464. printf("Go to Search.\n");
  465. break;
  466. }
  467. case EVENT_CHANNEL_COLLISION:
  468. {
  469. printf("Channel Collision\n");
  470. break;
  471. }
  472. case EVENT_TRANSFER_TX_START:
  473. {
  474. printf("Burst Started\n");
  475. break;
  476. }
  477. default:
  478. {
  479. printf("Unhandled channel event 0x%X\n", ucEvent_);
  480. break;
  481. }
  482. }
  483. // If we recieved a data message, diplay its contents here.
  484. if(bPrintBuffer)
  485. {
  486. if(bDisplay)
  487. {
  488. printf("[%02x],[%02x],[%02x],[%02x],[%02x],[%02x],[%02x],[%02x]\n",
  489. aucChannelBuffer[ucDataOffset + 0],
  490. aucChannelBuffer[ucDataOffset + 1],
  491. aucChannelBuffer[ucDataOffset + 2],
  492. aucChannelBuffer[ucDataOffset + 3],
  493. aucChannelBuffer[ucDataOffset + 4],
  494. aucChannelBuffer[ucDataOffset + 5],
  495. aucChannelBuffer[ucDataOffset + 6],
  496. aucChannelBuffer[ucDataOffset + 7]);
  497. }
  498. else
  499. {
  500. static int iIndex = 0;
  501. static char ac[] = {'|','/','-','\\'};
  502. printf("Rx: %c\r",ac[iIndex++]); fflush(stdout);
  503. iIndex &= 3;
  504. }
  505. }
  506. return(TRUE);
  507. }
  508. ////////////////////////////////////////////////////////////////////////////////
  509. // Test_ResponseCallback
  510. //
  511. // Callback function passed to ANT DLL. Called whenever a message is recieved from ANT
  512. // unless that message is a channel event message.
  513. //
  514. // ucChannel_: Channel number of relevent to the message
  515. // ucMessageId_: Message ID of the message recieved from ANT.
  516. ////////////////////////////////////////////////////////////////////////////////
  517. BOOL Test_ResponseCallback(UCHAR ucChannel_, UCHAR ucMessageId_)
  518. {
  519. BOOL bSuccess = FALSE;
  520. switch(ucMessageId_)
  521. {
  522. case MESG_STARTUP_MESG_ID:
  523. {
  524. printf("RESET Complete, reason: ");
  525. UCHAR ucReason = aucResponseBuffer[MESSAGE_BUFFER_DATA1_INDEX];
  526. if(ucReason == RESET_POR)
  527. printf("RESET_POR");
  528. if(ucReason & RESET_SUSPEND)
  529. printf("RESET_SUSPEND ");
  530. if(ucReason & RESET_SYNC)
  531. printf("RESET_SYNC ");
  532. if(ucReason & RESET_CMD)
  533. printf("RESET_CMD ");
  534. if(ucReason & RESET_WDT)
  535. printf("RESET_WDT ");
  536. if(ucReason & RESET_RST)
  537. printf("RESET_RST ");
  538. printf("\n");
  539. break;
  540. }
  541. case MESG_CAPABILITIES_ID:
  542. {
  543. printf("CAPABILITIES:\n");
  544. printf(" Max ANT Channels: %d\n",aucResponseBuffer[MESSAGE_BUFFER_DATA1_INDEX]);
  545. printf(" Max ANT Networks: %d\n",aucResponseBuffer[MESSAGE_BUFFER_DATA2_INDEX]);
  546. UCHAR ucStandardOptions = aucResponseBuffer[MESSAGE_BUFFER_DATA3_INDEX];
  547. UCHAR ucAdvanced = aucResponseBuffer[MESSAGE_BUFFER_DATA4_INDEX];
  548. UCHAR ucAdvanced2 = aucResponseBuffer[MESSAGE_BUFFER_DATA5_INDEX];
  549. printf("Standard Options:\n");
  550. if( ucStandardOptions & CAPABILITIES_NO_RX_CHANNELS )
  551. printf("CAPABILITIES_NO_RX_CHANNELS\n");
  552. if( ucStandardOptions & CAPABILITIES_NO_TX_CHANNELS )
  553. printf("CAPABILITIES_NO_TX_CHANNELS\n");
  554. if( ucStandardOptions & CAPABILITIES_NO_RX_MESSAGES )
  555. printf("CAPABILITIES_NO_RX_MESSAGES\n");
  556. if( ucStandardOptions & CAPABILITIES_NO_TX_MESSAGES )
  557. printf("CAPABILITIES_NO_TX_MESSAGES\n");
  558. if( ucStandardOptions & CAPABILITIES_NO_ACKD_MESSAGES )
  559. printf("CAPABILITIES_NO_ACKD_MESSAGES\n");
  560. if( ucStandardOptions & CAPABILITIES_NO_BURST_TRANSFER )
  561. printf("CAPABILITIES_NO_BURST_TRANSFER\n");
  562. printf("Advanced Options:\n");
  563. if( ucAdvanced & CAPABILITIES_OVERUN_UNDERRUN )
  564. printf("CAPABILITIES_OVERUN_UNDERRUN\n");
  565. if( ucAdvanced & CAPABILITIES_NETWORK_ENABLED )
  566. printf("CAPABILITIES_NETWORK_ENABLED\n");
  567. if( ucAdvanced & CAPABILITIES_AP1_VERSION_2 )
  568. printf("CAPABILITIES_AP1_VERSION_2\n");
  569. if( ucAdvanced & CAPABILITIES_SERIAL_NUMBER_ENABLED )
  570. printf("CAPABILITIES_SERIAL_NUMBER_ENABLED\n");
  571. if( ucAdvanced & CAPABILITIES_PER_CHANNEL_TX_POWER_ENABLED )
  572. printf("CAPABILITIES_PER_CHANNEL_TX_POWER_ENABLED\n");
  573. if( ucAdvanced & CAPABILITIES_LOW_PRIORITY_SEARCH_ENABLED )
  574. printf("CAPABILITIES_LOW_PRIORITY_SEARCH_ENABLED\n");
  575. if( ucAdvanced & CAPABILITIES_SCRIPT_ENABLED )
  576. printf("CAPABILITIES_SCRIPT_ENABLED\n");
  577. if( ucAdvanced & CAPABILITIES_SEARCH_LIST_ENABLED )
  578. printf("CAPABILITIES_SEARCH_LIST_ENABLED\n");
  579. printf("Advanced 2 Options 1:\n");
  580. if( ucAdvanced2 & CAPABILITIES_LED_ENABLED )
  581. printf("CAPABILITIES_LED_ENABLED\n");
  582. if( ucAdvanced2 & CAPABILITIES_EXT_MESSAGE_ENABLED )
  583. printf("CAPABILITIES_EXT_MESSAGE_ENABLED\n");
  584. if( ucAdvanced2 & CAPABILITIES_SCAN_MODE_ENABLED )
  585. printf("CAPABILITIES_SCAN_MODE_ENABLED\n");
  586. if( ucAdvanced2 & CAPABILITIES_RESERVED )
  587. printf("CAPABILITIES_RESERVED\n");
  588. if( ucAdvanced2 & CAPABILITIES_PROX_SEARCH_ENABLED )
  589. printf("CAPABILITIES_PROX_SEARCH_ENABLED\n");
  590. if( ucAdvanced2 & CAPABILITIES_EXT_ASSIGN_ENABLED )
  591. printf("CAPABILITIES_EXT_ASSIGN_ENABLED\n");
  592. if( ucAdvanced2 & CAPABILITIES_FS_ANTFS_ENABLED)
  593. printf("CAPABILITIES_FREE_1\n");
  594. if( ucAdvanced2 & CAPABILITIES_FIT1_ENABLED )
  595. printf("CAPABILITIES_FIT1_ENABLED\n");
  596. break;
  597. }
  598. case MESG_CHANNEL_STATUS_ID:
  599. {
  600. printf("Got Status\n");
  601. char astrStatus[][32] = { "STATUS_UNASSIGNED_CHANNEL",
  602. "STATUS_ASSIGNED_CHANNEL",
  603. "STATUS_SEARCHING_CHANNEL",
  604. "STATUS_TRACKING_CHANNEL" };
  605. UCHAR ucStatusByte = aucResponseBuffer[MESSAGE_BUFFER_DATA2_INDEX] & STATUS_CHANNEL_STATE_MASK; // MUST MASK OFF THE RESERVED BITS
  606. printf("STATUS: %s\n",astrStatus[ucStatusByte]);
  607. break;
  608. }
  609. case MESG_CHANNEL_ID_ID:
  610. {
  611. // Channel ID of the device that we just recieved a message from.
  612. USHORT usDeviceNumber = aucResponseBuffer[MESSAGE_BUFFER_DATA2_INDEX] | (aucResponseBuffer[MESSAGE_BUFFER_DATA3_INDEX] << 8);
  613. UCHAR ucDeviceType = aucResponseBuffer[MESSAGE_BUFFER_DATA4_INDEX];
  614. UCHAR ucTransmissionType = aucResponseBuffer[MESSAGE_BUFFER_DATA5_INDEX];
  615. printf("CHANNEL ID: (%d/%d/%d)\n", usDeviceNumber, ucDeviceType, ucTransmissionType);
  616. break;
  617. }
  618. case MESG_VERSION_ID:
  619. {
  620. printf("VERSION: %s\n", (char*) &aucResponseBuffer[MESSAGE_BUFFER_DATA1_INDEX]);
  621. break;
  622. }
  623. case MESG_RESPONSE_EVENT_ID:
  624. {
  625. switch (aucResponseBuffer[MESSAGE_BUFFER_DATA2_INDEX])
  626. {
  627. case MESG_NETWORK_KEY_ID:
  628. {
  629. if(aucResponseBuffer[MESSAGE_BUFFER_DATA3_INDEX] != RESPONSE_NO_ERROR)
  630. {
  631. printf("Error configuring network key: Code 0%d\n", aucResponseBuffer[MESSAGE_BUFFER_DATA3_INDEX]);
  632. break;
  633. }
  634. printf("Network key set\n");
  635. printf("Assigning channel...\n");
  636. if(ucChannelType == CHANNEL_TYPE_MASTER)
  637. {
  638. bSuccess = ANT_AssignChannel(USER_ANTCHANNEL, PARAMETER_TX_NOT_RX, USER_NETWORK_NUM);
  639. }
  640. else if(ucChannelType == CHANNEL_TYPE_SLAVE)
  641. {
  642. bSuccess = ANT_AssignChannel(USER_ANTCHANNEL, 0, USER_NETWORK_NUM);
  643. }
  644. break;
  645. }
  646. case MESG_ASSIGN_CHANNEL_ID:
  647. {
  648. if(aucResponseBuffer[MESSAGE_BUFFER_DATA3_INDEX] != RESPONSE_NO_ERROR)
  649. {
  650. printf("Error assigning channel: Code 0%d\n", aucResponseBuffer[MESSAGE_BUFFER_DATA3_INDEX]);
  651. break;
  652. }
  653. printf("Channel assigned\n");
  654. printf("Setting Channel ID...\n");
  655. bSuccess = ANT_SetChannelId(USER_ANTCHANNEL, USER_DEVICENUM, USER_DEVICETYPE, USER_TRANSTYPE);
  656. break;
  657. }
  658. case MESG_CHANNEL_ID_ID:
  659. {
  660. if(aucResponseBuffer[MESSAGE_BUFFER_DATA3_INDEX] != RESPONSE_NO_ERROR)
  661. {
  662. printf("Error configuring Channel ID: Code 0%d\n", aucResponseBuffer[MESSAGE_BUFFER_DATA3_INDEX]);
  663. break;
  664. }
  665. printf("Channel ID set\n");
  666. printf("Setting Radio Frequency...\n");
  667. bSuccess = ANT_SetChannelRFFreq(USER_ANTCHANNEL, USER_RADIOFREQ);
  668. break;
  669. }
  670. case MESG_CHANNEL_RADIO_FREQ_ID:
  671. {
  672. if(aucResponseBuffer[MESSAGE_BUFFER_DATA3_INDEX] != RESPONSE_NO_ERROR)
  673. {
  674. printf("Error configuring Radio Frequency: Code 0%d\n", aucResponseBuffer[MESSAGE_BUFFER_DATA3_INDEX]);
  675. break;
  676. }
  677. printf("Radio Frequency set\n");
  678. printf("Opening channel...\n");
  679. bBroadcasting = TRUE;
  680. bSuccess = ANT_OpenChannel(USER_ANTCHANNEL);
  681. break;
  682. }
  683. case MESG_OPEN_CHANNEL_ID:
  684. {
  685. if(aucResponseBuffer[MESSAGE_BUFFER_DATA3_INDEX] != RESPONSE_NO_ERROR)
  686. {
  687. printf("Error opening channel: Code 0%d\n", aucResponseBuffer[MESSAGE_BUFFER_DATA3_INDEX]);
  688. bBroadcasting = FALSE;
  689. break;
  690. }
  691. printf("Channel opened\n");
  692. #if defined (ENABLE_EXTENDED_MESSAGES)
  693. printf("Enabling extended messages...\n");
  694. ANT_RxExtMesgsEnable(TRUE);
  695. #endif
  696. break;
  697. }
  698. case MESG_RX_EXT_MESGS_ENABLE_ID:
  699. {
  700. if(aucResponseBuffer[MESSAGE_BUFFER_DATA3_INDEX] == INVALID_MESSAGE)
  701. {
  702. printf("Extended messages not supported in this ANT product\n");
  703. break;
  704. }
  705. else if(aucResponseBuffer[MESSAGE_BUFFER_DATA3_INDEX] != RESPONSE_NO_ERROR)
  706. {
  707. printf("Error enabling extended messages: Code 0%d\n", aucResponseBuffer[MESSAGE_BUFFER_DATA3_INDEX]);
  708. break;
  709. }
  710. printf("Extended messages enabled\n");
  711. break;
  712. }
  713. case MESG_UNASSIGN_CHANNEL_ID:
  714. {
  715. if(aucResponseBuffer[MESSAGE_BUFFER_DATA3_INDEX] != RESPONSE_NO_ERROR)
  716. {
  717. printf("Error unassigning channel: Code 0%d\n", aucResponseBuffer[MESSAGE_BUFFER_DATA3_INDEX]);
  718. break;
  719. }
  720. printf("Channel unassigned\n");
  721. printf("Press enter to exit\n");
  722. bDone = TRUE;
  723. break;
  724. }
  725. case MESG_CLOSE_CHANNEL_ID:
  726. {
  727. if(aucResponseBuffer[MESSAGE_BUFFER_DATA3_INDEX]== CHANNEL_IN_WRONG_STATE)
  728. {
  729. // We get here if we tried to close the channel after the search timeout (slave)
  730. printf("Channel is already closed\n");
  731. printf("Unassigning channel...\n");
  732. ANT_UnAssignChannel(USER_ANTCHANNEL);
  733. break;
  734. }
  735. else if(aucResponseBuffer[MESSAGE_BUFFER_DATA3_INDEX] != RESPONSE_NO_ERROR)
  736. {
  737. printf("Error closing channel: Code 0%d\n", aucResponseBuffer[MESSAGE_BUFFER_DATA3_INDEX]);
  738. break;
  739. }
  740. // If this message was successful, wait for EVENT_CHANNEL_CLOSED to confirm the channel is closed
  741. break;
  742. }
  743. case MESG_REQUEST_ID:
  744. {
  745. if(aucResponseBuffer[MESSAGE_BUFFER_DATA3_INDEX] == INVALID_MESSAGE)
  746. {
  747. printf("Requested message not supported in this ANT product\n");
  748. break;
  749. }
  750. }
  751. default:
  752. {
  753. printf("Unhandled response %d to message 0x%X\n", aucResponseBuffer[MESSAGE_BUFFER_DATA3_INDEX], aucResponseBuffer[MESSAGE_BUFFER_DATA2_INDEX]);
  754. break;
  755. }
  756. }
  757. }
  758. }
  759. return(TRUE);
  760. }
  761. ////////////////////////////////////////////////////////////////////////////////
  762. // PrintMenu
  763. //
  764. // Start the Test program.
  765. //
  766. ////////////////////////////////////////////////////////////////////////////////
  767. void PrintMenu()
  768. {
  769. // Printout options
  770. printf("\n");
  771. printf("M - Print this menu\n");
  772. printf("A - Send Acknowledged message\n");
  773. printf("B - Send Burst message\n");
  774. printf("R - Reset\n");
  775. printf("C - Request Capabilites\n");
  776. printf("V - Request Version\n");
  777. printf("I - Request Channel ID\n");
  778. printf("S - Request Status\n");
  779. printf("U - Request USB Descriptor\n");
  780. printf("D - Toggle Display\n");
  781. printf("Q - Quit\n");
  782. printf("\n");
  783. fflush(stdout);
  784. }