ANT_Common.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 in compliance
  4. with this license.
  5. Copyright (c) Dynastream Innovations Inc. 2013
  6. All rights reserved.
  7. */
  8. using UnityEngine;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Runtime.InteropServices;
  14. namespace ANT_Managed_Library
  15. {
  16. /// <summary>
  17. /// This is a static class that manages all the functions and variables common to the whole scope of the library.
  18. /// </summary>
  19. public static class ANT_Common
  20. {
  21. /// <summary>
  22. /// Enables or disables all devices from resetting on startup, shutdown, and on CWTestMode Failure.
  23. /// Default = true.
  24. /// </summary>
  25. static public bool autoResetIsEnabled = true;
  26. #region constants
  27. #if EXT_FUNCTIONALITY
  28. #warning "The Extended build is for internal use only, and even then, only for specific projects, do not use without discussing with the ANT team"
  29. internal const String ANT_UNMANAGED_WRAPPER = "ANT_WrappedLib_Ext.dll";
  30. #else
  31. internal const String ANT_UNMANAGED_WRAPPER = "ANT_WrappedLib";
  32. #endif
  33. internal const String ANT_SI_LIBRARY = "DSI_SiUSBXp_3_1";
  34. internal const String ANT_SI_LIBRARY2 = "DSI_CP210xManufacturing_3_1";
  35. #endregion
  36. #region DLL imports
  37. [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention=CallingConvention.Cdecl)]
  38. private static extern UInt32 ANT_GetNumDevices();
  39. [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention=CallingConvention.Cdecl)]
  40. private static extern int ANT_EnableDebugLogging();
  41. [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention=CallingConvention.Cdecl)]
  42. private static extern void ANT_DisableDebugLogging();
  43. [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention=CallingConvention.Cdecl)]
  44. private static extern int ANT_SetDebugLogDirectory(string pcDirectory);
  45. [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention = CallingConvention.Cdecl)]
  46. private static extern int ANT_DebugThreadInit(string pucName);
  47. [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention = CallingConvention.Cdecl)]
  48. private static extern int ANT_DebugThreadWrite(string pcMessage);
  49. [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention = CallingConvention.Cdecl)]
  50. internal static extern int ANT_DebugResetTime();
  51. #endregion
  52. /// <summary>
  53. /// Returns the number of ANT USB devices currently detected by the system.
  54. /// </summary>
  55. public static UInt32 getNumDetectedUSBDevices()
  56. {
  57. return ANT_GetNumDevices();
  58. }
  59. /// <overloads>Enables debug files</overloads>
  60. /// <summary>
  61. /// Initializes and enables debug logs for all devices
  62. /// Note: For application specific logs to work properly
  63. /// (e.g. ANT-FS logs), this needs to be called BEFORE
  64. /// creating an ANT Device.
  65. /// </summary>
  66. public static bool enableDebugLogs()
  67. {
  68. return ANT_EnableDebugLogging() == 1;
  69. }
  70. /// <summary>
  71. /// Initializes and enables debug logs for all devices,
  72. /// and stores the log in the specified path.
  73. /// Note: For application specific logs to work properly
  74. /// (e.g. ANT-FS logs), this needs to be called BEFORE
  75. /// creating an ANT Device.
  76. /// </summary>
  77. /// <param name="debugPath">Debug log directory</param>
  78. public static void enableDebugLogs(string debugPath)
  79. {
  80. enableDebugLogs();
  81. setDebugLogDirectory(debugPath);
  82. }
  83. /// <summary>
  84. /// Disables and closes the debug logs
  85. /// </summary>
  86. public static void disableDebugLogs()
  87. {
  88. ANT_DisableDebugLogging();
  89. }
  90. /// <summary>
  91. /// Set the directory the log files are saved to.
  92. /// This string will prefix the file name so must end with a slash or will be part of the name.
  93. /// ie: directoryPath='c:\ANT\logs' will result in files being saved to the \ANT directory named logsdevice0.txt.
  94. /// Throws an exception if directory does not exist.
  95. /// </summary>
  96. /// <param name="directoryPath">
  97. /// Path to directory to save log files in. Default is the running directory.
  98. /// This string will prefix the file name so must end with a slash or will be part of the name.
  99. /// ie: directoryPath='c:\ANT\logs' will result in files being saved to the \ANT directory named logsdevice0.txt.
  100. /// </param>
  101. public static bool setDebugLogDirectory(string directoryPath)
  102. {
  103. if (!System.IO.Directory.Exists(directoryPath))
  104. throw new ANT_Exception("Path does not exist");
  105. return ANT_SetDebugLogDirectory(directoryPath) == 1;
  106. }
  107. //
  108. //Note: Access to the following logging functions is required by the current ANT-FS implementation
  109. //
  110. /// <summary>
  111. /// Creates a debug log for the currently executing thread
  112. /// </summary>
  113. /// <param name="name">Name of file (will result in ao_debug_name)</param>
  114. /// <returns>True if successful</returns>
  115. internal static bool initDebugLogThread(string name)
  116. {
  117. return ANT_DebugThreadInit(name) == 1;
  118. }
  119. /// <summary>
  120. /// Adds an application specific message to the log for the current thread
  121. /// </summary>
  122. /// <param name="message">Message to write to the log</param>
  123. /// <returns>True on success</returns>
  124. internal static bool writeToDebugLog(string message)
  125. {
  126. return ANT_DebugThreadWrite(message) == 1;
  127. }
  128. }
  129. }