demo_antfs.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using ANT_Managed_Library;
  13. namespace ANTFS_Demo
  14. {
  15. class Demo
  16. {
  17. // ANT Channel Configuration Parameters
  18. // We define them here, since these must match between host and client for communication to work
  19. public readonly static byte SearchRF = (byte)ANT_Managed_Library.ANTFS.RadioFrequency.ANTFSNetwork; // Radio Frequency to use during link state
  20. public readonly static ushort ChannelPeriod = 8192; // ANT channel period. It should match the LinkPeriod below.
  21. public readonly static byte DeviceType = 1; // Device Type (ANT Channel ID)
  22. public readonly static byte TransmissionType = 5; // Transmission Type (ANT Channel ID)
  23. public readonly static byte NetworkNumber = 0;
  24. public readonly static byte[] NetworkKey = { 0xA8, 0xA4, 0x23, 0xB9, 0xF5, 0x5E, 0x63, 0xC1 };
  25. // ANT-FS Configuration Parameters
  26. // These must also match between host and client
  27. public readonly static ushort ClientManufacturerID = 2; // ANT-FS Client Manufacturer ID
  28. public readonly static ushort ClientDeviceType = 416; // ANT-FS Client Device Type
  29. public readonly static ANT_Managed_Library.ANTFS.BeaconPeriod LinkPeriod = ANT_Managed_Library.ANTFS.BeaconPeriod.FourHz;
  30. // This is the channel period included in the beacon. Set to match ChannelPeriod above.
  31. // If using ANT-FS Broadcast and the ChannelPeriod is not a standard value defined in the ANT-FS spec, set to BeaconPeriod.Keep.
  32. public readonly static string[] CursorStrings = { "|", "/", "_", "\\" }; // To display rotating "cursor" while data is being broadcast
  33. public static readonly bool AntfsBroadcast = false; // Set to false to start the session in standard ANT-FS mode. Set to true to start in broadcast mode.
  34. static ANT_Device device0; // ANT USB device
  35. static byte selection = Byte.MaxValue;
  36. /// <summary>
  37. /// Demo program for ANT-FS C# library
  38. /// </summary>
  39. /// <remarks>
  40. /// Usage:
  41. /// demo_antfs.exe [selection]
  42. ///
  43. /// ... where
  44. /// selection: ANT-FS Host = 0, ANT-FS Client = 1
  45. ///
  46. /// ... example
  47. /// demo_antfs.exe 0
  48. /// will connect to an ANT USB stick and start an ANT-FS session as a host
  49. ///
  50. /// If optional arguments are not supplied, user will be prompted
  51. /// to entere these after the program starts
  52. /// </remarks>
  53. /// <param name="args">User selection: 0 = host, 1 = client</param>
  54. static void Main(string[] args)
  55. {
  56. if (args.Length > 0)
  57. {
  58. selection = byte.Parse(args[0]);
  59. }
  60. try
  61. {
  62. Init();
  63. Start(selection);
  64. }
  65. catch (Exception ex)
  66. {
  67. Console.WriteLine("Demo failed with exception: \n" + ex.Message);
  68. }
  69. }
  70. /// <summary>
  71. /// Initialize the demo parameters
  72. /// </summary>
  73. static void Init()
  74. {
  75. try
  76. {
  77. Console.WriteLine("Attempting to connect to an ANT USB device...");
  78. ANT_Common.enableDebugLogs(); // Enable debugging first, to enable ANT-FS specific logging
  79. device0 = new ANT_Device(); // Create a device instance using the automatic constructor (automatic detection of USB device number and baud rate)
  80. Console.WriteLine("Initialization was successful!");
  81. }
  82. catch (Exception ex)
  83. {
  84. if (device0 == null) // Unable to connect to ANT
  85. {
  86. throw new Exception("Could not connect to any device.\n" +
  87. "Details: \n " + ex.Message);
  88. }
  89. else
  90. {
  91. throw new Exception("Error connecting to ANT: " + ex.Message);
  92. }
  93. }
  94. }
  95. /// <summary>
  96. /// Start the demo program
  97. /// </summary>
  98. /// <param name="userSelection">User selection: 0 = host, 1 = client. 255 passed as invalid</param>
  99. static void Start(byte userSelection)
  100. {
  101. selection = userSelection;
  102. // If the selection between host/client has not been specified, prompt the user
  103. do
  104. {
  105. if (selection == Byte.MaxValue)
  106. {
  107. Console.WriteLine("Please select (Host = 0, Client = 1)");
  108. try
  109. {
  110. selection = byte.Parse(Console.ReadLine());
  111. if (selection != 0 && selection != 1)
  112. throw new FormatException("Error: Invalid selection");
  113. }
  114. catch (Exception)
  115. {
  116. selection = Byte.MaxValue;
  117. }
  118. }
  119. } while (selection == Byte.MaxValue);
  120. if (selection == 0)
  121. {
  122. HostDemo theDemo = new HostDemo();
  123. theDemo.Start(device0);
  124. }
  125. else if (selection == 1)
  126. {
  127. ClientDemo theDemo = new ClientDemo();
  128. theDemo.Start(device0);
  129. }
  130. }
  131. /// <summary>
  132. /// Parse user input as a numeric value
  133. /// </summary>
  134. /// <param name="selection">String containing user input</param>
  135. /// <returns>Numeric value</returns>
  136. public static ushort ParseInput(string selection)
  137. {
  138. try
  139. {
  140. return UInt16.Parse(selection);
  141. }
  142. catch (Exception ex)
  143. {
  144. throw new System.ArgumentException("Invalid input", ex);
  145. }
  146. }
  147. }
  148. }