From 6a8457dee1f7dd4014eab13c6a3ae24995f48793 Mon Sep 17 00:00:00 2001 From: Justin Berger Date: Sat, 29 Aug 2020 19:04:35 -0600 Subject: [PATCH] Fixed C# bindings --- bindings/cs/Demo/Demo.csproj | 98 +---- bindings/cs/Demo/Program.cs | 18 +- bindings/cs/Demo/Properties/AssemblyInfo.cs | 36 -- bindings/cs/LibSurviveBinding.sln | 115 +++--- bindings/cs/libsurvive.net/LibSurViveAPI.cs | 349 ------------------ bindings/cs/libsurvive.net/SurviveContext.cs | 121 +++--- bindings/cs/libsurvive.net/cfunctions.cs | 106 +++--- .../cs/libsurvive.net/libsurvive.net.csproj | 14 +- 8 files changed, 183 insertions(+), 674 deletions(-) delete mode 100644 bindings/cs/Demo/Properties/AssemblyInfo.cs delete mode 100644 bindings/cs/libsurvive.net/LibSurViveAPI.cs diff --git a/bindings/cs/Demo/Demo.csproj b/bindings/cs/Demo/Demo.csproj index f347a5eb..bb617fca 100644 --- a/bindings/cs/Demo/Demo.csproj +++ b/bindings/cs/Demo/Demo.csproj @@ -1,86 +1,12 @@ - - - - - Debug - AnyCPU - {D3A43A2F-03B7-4A10-928F-686790036838} - Exe - Demo - Demo - v4.6.1 - 512 - true - publish\ - true - Disk - false - Foreground - 7 - Days - false - false - true - 0 - 1.0.0.%2a - false - false - true - - - AnyCPU - true - full - false - ..\x64\Debug\ - DEBUG;TRACE - prompt - 4 - false - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - - - - - - - - - - - - - - - {da1b12d7-a121-4d40-b363-4893e4f88b8a} - libsurvive.net - - - - - False - Microsoft .NET Framework 4.6.1 %28x86 and x64%29 - true - - - False - .NET Framework 3.5 SP1 - false - - - - \ No newline at end of file + + + + + + + + Exe + netcoreapp3.1 + + + diff --git a/bindings/cs/Demo/Program.cs b/bindings/cs/Demo/Program.cs index c838a4e0..5e729031 100644 --- a/bindings/cs/Demo/Program.cs +++ b/bindings/cs/Demo/Program.cs @@ -12,16 +12,10 @@ class Program { static void Main(string[] args) { - LibSurViveAPI api = LibSurViveAPI.Instance; - - var so = api.GetSurviveObjectByName("HMD"); - - } - - public static void HMDUpdate(int ObjectID, Vector3 pos) - { - - } - - } + SurviveContext api = new SurviveContext(args); + while (api.Poll() == 0) { + } + api.Close(); + } + } } diff --git a/bindings/cs/Demo/Properties/AssemblyInfo.cs b/bindings/cs/Demo/Properties/AssemblyInfo.cs deleted file mode 100644 index 2c6a6c74..00000000 --- a/bindings/cs/Demo/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("Demo")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("Demo")] -[assembly: AssemblyCopyright("Copyright © 2018")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("d3a43a2f-03b7-4a10-928f-686790036838")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/bindings/cs/LibSurviveBinding.sln b/bindings/cs/LibSurviveBinding.sln index b2e2f11c..d3920f3c 100644 --- a/bindings/cs/LibSurviveBinding.sln +++ b/bindings/cs/LibSurviveBinding.sln @@ -1,67 +1,48 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.27130.2003 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo", "Demo\Demo.csproj", "{D3A43A2F-03B7-4A10-928F-686790036838}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "libsurvive.net", "libsurvive.net\libsurvive.net.csproj", "{DA1B12D7-A121-4D40-B363-4893E4F88B8A}" - ProjectSection(ProjectDependencies) = postProject - {435CFD2C-8724-42EE-8FDE-71EF7D2C6B2F} = {435CFD2C-8724-42EE-8FDE-71EF7D2C6B2F} - EndProjectSection -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libsurvive", "..\..\winbuild\libsurvive\libsurvive.vcxproj", "{435CFD2C-8724-42EE-8FDE-71EF7D2C6B2F}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Debug|x64 = Debug|x64 - Debug|x86 = Debug|x86 - Release|Any CPU = Release|Any CPU - Release|x64 = Release|x64 - Release|x86 = Release|x86 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {D3A43A2F-03B7-4A10-928F-686790036838}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {D3A43A2F-03B7-4A10-928F-686790036838}.Debug|Any CPU.Build.0 = Debug|Any CPU - {D3A43A2F-03B7-4A10-928F-686790036838}.Debug|x64.ActiveCfg = Debug|Any CPU - {D3A43A2F-03B7-4A10-928F-686790036838}.Debug|x64.Build.0 = Debug|Any CPU - {D3A43A2F-03B7-4A10-928F-686790036838}.Debug|x86.ActiveCfg = Debug|Any CPU - {D3A43A2F-03B7-4A10-928F-686790036838}.Debug|x86.Build.0 = Debug|Any CPU - {D3A43A2F-03B7-4A10-928F-686790036838}.Release|Any CPU.ActiveCfg = Release|Any CPU - {D3A43A2F-03B7-4A10-928F-686790036838}.Release|Any CPU.Build.0 = Release|Any CPU - {D3A43A2F-03B7-4A10-928F-686790036838}.Release|x64.ActiveCfg = Release|Any CPU - {D3A43A2F-03B7-4A10-928F-686790036838}.Release|x64.Build.0 = Release|Any CPU - {D3A43A2F-03B7-4A10-928F-686790036838}.Release|x86.ActiveCfg = Release|Any CPU - {D3A43A2F-03B7-4A10-928F-686790036838}.Release|x86.Build.0 = Release|Any CPU - {DA1B12D7-A121-4D40-B363-4893E4F88B8A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {DA1B12D7-A121-4D40-B363-4893E4F88B8A}.Debug|Any CPU.Build.0 = Debug|Any CPU - {DA1B12D7-A121-4D40-B363-4893E4F88B8A}.Debug|x64.ActiveCfg = Debug|Any CPU - {DA1B12D7-A121-4D40-B363-4893E4F88B8A}.Debug|x64.Build.0 = Debug|Any CPU - {DA1B12D7-A121-4D40-B363-4893E4F88B8A}.Debug|x86.ActiveCfg = Debug|Any CPU - {DA1B12D7-A121-4D40-B363-4893E4F88B8A}.Debug|x86.Build.0 = Debug|Any CPU - {DA1B12D7-A121-4D40-B363-4893E4F88B8A}.Release|Any CPU.ActiveCfg = Release|Any CPU - {DA1B12D7-A121-4D40-B363-4893E4F88B8A}.Release|Any CPU.Build.0 = Release|Any CPU - {DA1B12D7-A121-4D40-B363-4893E4F88B8A}.Release|x64.ActiveCfg = Release|Any CPU - {DA1B12D7-A121-4D40-B363-4893E4F88B8A}.Release|x64.Build.0 = Release|Any CPU - {DA1B12D7-A121-4D40-B363-4893E4F88B8A}.Release|x86.ActiveCfg = Release|Any CPU - {DA1B12D7-A121-4D40-B363-4893E4F88B8A}.Release|x86.Build.0 = Release|Any CPU - {435CFD2C-8724-42EE-8FDE-71EF7D2C6B2F}.Debug|Any CPU.ActiveCfg = Debug|x64 - {435CFD2C-8724-42EE-8FDE-71EF7D2C6B2F}.Debug|Any CPU.Build.0 = Debug|x64 - {435CFD2C-8724-42EE-8FDE-71EF7D2C6B2F}.Debug|x64.ActiveCfg = Debug|x64 - {435CFD2C-8724-42EE-8FDE-71EF7D2C6B2F}.Debug|x64.Build.0 = Debug|x64 - {435CFD2C-8724-42EE-8FDE-71EF7D2C6B2F}.Debug|x86.ActiveCfg = Debug|Win32 - {435CFD2C-8724-42EE-8FDE-71EF7D2C6B2F}.Debug|x86.Build.0 = Debug|Win32 - {435CFD2C-8724-42EE-8FDE-71EF7D2C6B2F}.Release|Any CPU.ActiveCfg = Release|Win32 - {435CFD2C-8724-42EE-8FDE-71EF7D2C6B2F}.Release|x64.ActiveCfg = Release|x64 - {435CFD2C-8724-42EE-8FDE-71EF7D2C6B2F}.Release|x64.Build.0 = Release|x64 - {435CFD2C-8724-42EE-8FDE-71EF7D2C6B2F}.Release|x86.ActiveCfg = Release|Win32 - {435CFD2C-8724-42EE-8FDE-71EF7D2C6B2F}.Release|x86.Build.0 = Release|Win32 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {CA47A8D6-3379-4ABA-927E-86BBDDE1D304} - EndGlobalSection -EndGlobal + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26124.0 +MinimumVisualStudioVersion = 15.0.26124.0 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "libsurvive.net", "libsurvive.net\libsurvive.net.csproj", "{EF963623-9F00-40BC-8FC7-1BA5015070A5}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo", "Demo\Demo.csproj", "{D1ADFCE0-613D-4BD7-B28A-9DB0259F9633}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {EF963623-9F00-40BC-8FC7-1BA5015070A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EF963623-9F00-40BC-8FC7-1BA5015070A5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EF963623-9F00-40BC-8FC7-1BA5015070A5}.Debug|x64.ActiveCfg = Debug|Any CPU + {EF963623-9F00-40BC-8FC7-1BA5015070A5}.Debug|x64.Build.0 = Debug|Any CPU + {EF963623-9F00-40BC-8FC7-1BA5015070A5}.Debug|x86.ActiveCfg = Debug|Any CPU + {EF963623-9F00-40BC-8FC7-1BA5015070A5}.Debug|x86.Build.0 = Debug|Any CPU + {EF963623-9F00-40BC-8FC7-1BA5015070A5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EF963623-9F00-40BC-8FC7-1BA5015070A5}.Release|Any CPU.Build.0 = Release|Any CPU + {EF963623-9F00-40BC-8FC7-1BA5015070A5}.Release|x64.ActiveCfg = Release|Any CPU + {EF963623-9F00-40BC-8FC7-1BA5015070A5}.Release|x64.Build.0 = Release|Any CPU + {EF963623-9F00-40BC-8FC7-1BA5015070A5}.Release|x86.ActiveCfg = Release|Any CPU + {EF963623-9F00-40BC-8FC7-1BA5015070A5}.Release|x86.Build.0 = Release|Any CPU + {D1ADFCE0-613D-4BD7-B28A-9DB0259F9633}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D1ADFCE0-613D-4BD7-B28A-9DB0259F9633}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D1ADFCE0-613D-4BD7-B28A-9DB0259F9633}.Debug|x64.ActiveCfg = Debug|Any CPU + {D1ADFCE0-613D-4BD7-B28A-9DB0259F9633}.Debug|x64.Build.0 = Debug|Any CPU + {D1ADFCE0-613D-4BD7-B28A-9DB0259F9633}.Debug|x86.ActiveCfg = Debug|Any CPU + {D1ADFCE0-613D-4BD7-B28A-9DB0259F9633}.Debug|x86.Build.0 = Debug|Any CPU + {D1ADFCE0-613D-4BD7-B28A-9DB0259F9633}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D1ADFCE0-613D-4BD7-B28A-9DB0259F9633}.Release|Any CPU.Build.0 = Release|Any CPU + {D1ADFCE0-613D-4BD7-B28A-9DB0259F9633}.Release|x64.ActiveCfg = Release|Any CPU + {D1ADFCE0-613D-4BD7-B28A-9DB0259F9633}.Release|x64.Build.0 = Release|Any CPU + {D1ADFCE0-613D-4BD7-B28A-9DB0259F9633}.Release|x86.ActiveCfg = Release|Any CPU + {D1ADFCE0-613D-4BD7-B28A-9DB0259F9633}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/bindings/cs/libsurvive.net/LibSurViveAPI.cs b/bindings/cs/libsurvive.net/LibSurViveAPI.cs deleted file mode 100644 index 1fb8caea..00000000 --- a/bindings/cs/libsurvive.net/LibSurViveAPI.cs +++ /dev/null @@ -1,349 +0,0 @@ -using System.Collections; -using System.Collections.Generic; - -using libsurvive; -using System; -using System.Threading; -using System.Runtime.InteropServices; - -public class LibSurViveAPI -{ - private static LibSurViveAPI _instance; - public static LibSurViveAPI Instance - { - get - { - if (_instance == null) - { - _instance = new LibSurViveAPI(); - } - - return _instance; - } - } - - bool running = true; - Thread internalPollTread; - - public IntPtr context; - - light_process_func light_Process_Func; - raw_pose_func raw_Pose_Func; - lighthouse_pose_func lighthouse_Pose_Func; - angle_process_func angle_Process_Func; - button_process_func button_Process_Func; - htc_config_func htc_Config_Func; - imu_process_func imu_Process_Func; - text_feedback_func error_func; - text_feedback_func info_func; - - public delegate void Log(string message); - Log LogInfo = delegate { }; - - public delegate void ErrorLog(string message); - ErrorLog LogError = delegate { }; - - LibSurViveAPI() - { - CreateContext(); - - CreateTread(); - } - - ~LibSurViveAPI() - { - running = false; - } - - private void CreateTread() - { - internalPollTread = new Thread(InternalPoll); - internalPollTread.Start(); - } - - void InternalPoll() - { - while (running) - { - int code = Cfunctions.Survive_poll(context); - - if (code != 0) - { - running = false; - } - } - } - - internal void CreateContext() - { - LogInfo("Start Init"); - - SetupConfigs configs = new SetupConfigs - { - configFile = "survive_conf.json" - }; - - string[] args = CreateStartParameters(configs); - - //string[] vs = new[] { "--playback", "P:/c/libsurvive-data/lightcap-reformat/lightcap-reformat.log", "--disambiguator", "StateBased", "--calibrate" }; - - context = Cfunctions.Survive_init_internal(args.Length, args); - - if (context == IntPtr.Zero) - { - throw new Exception("There was a problem initializing the lib!"); - } - - light_Process_Func = LightEvent; - raw_Pose_Func = PoseEvent; - lighthouse_Pose_Func = LightHouseEvent; - angle_Process_Func = AngleEvent; - button_Process_Func = ButtonEvent; - htc_Config_Func = HTCConfigEvent; - imu_Process_Func = IMUEvent; - error_func = ErrorEvent; - info_func = InfoEvent; - - Cfunctions.Survive_install_raw_pose_fn(context, raw_Pose_Func); - Cfunctions.Survive_install_light_fn(context, light_Process_Func); - Cfunctions.Survive_install_lighthouse_pose_fn(context, lighthouse_Pose_Func); - Cfunctions.Survive_install_angle_fn(context, angle_Process_Func); - Cfunctions.Survive_install_button_fn(context, button_Process_Func); - Cfunctions.Survive_install_htc_config_fn(context, htc_Config_Func); - Cfunctions.Survive_install_imu_fn(context, imu_Process_Func); - Cfunctions.Survive_install_error_fn(context, error_func); - Cfunctions.Survive_install_info_fn(context, info_func); - - LogInfo("Finished Init"); - - LogInfo("Start Startup"); - - int a = 0; - try - { - a = Cfunctions.Survive_startup(context); - } - catch (Exception) - { - throw; - } - - if (a != 0) - { - throw new Exception("Error in startup"); - } - - LogInfo("Finished Startup"); - - } - - - static public string[] CreateStartParameters(SetupConfigs configs) - { - List args = new List - { - "unity" - }; - - if (configs.playbackFile != "" && configs.playbackFile != null) - { - args.AddRange(new[] { "--playback", configs.playbackFile }); - args.AddRange(new[] { "--playback-factor", configs.playbackFactor.ToString() }); - } - - if(configs.disambiguator != Disambiguator.Default) - args.AddRange(new[] { "--disambiguator", Enum.GetName(typeof(Disambiguator), configs.disambiguator) }); - - if (configs.calibrate != BoolConfig.Default) - args.Add(configs.calibrate == BoolConfig.Yes ? "--calibrate" : "--no-calibrate"); - - if (configs.configFile != "") - args.AddRange(new[] { "-c", configs.configFile }); - - //args.AddRange(new[] { "--disambiguator", Enum.GetName(typeof(Poser), disambiguator) }); - - - - return args.ToArray(); - } - - - - virtual protected void InfoEvent(IntPtr ctx, string fault) - { - LogInfo(fault); - } - - virtual protected void ErrorEvent(IntPtr ctx, string fault) - { - LogError(fault); - } - - virtual protected void IMUEvent(IntPtr so, int mask, double[] accelgyro, uint timecode, int id) - { - Cfunctions.Survive_default_imu_process(so, mask, accelgyro, timecode, id); - } - - virtual protected int HTCConfigEvent(IntPtr so, string ct0conf, int len) - { - return Cfunctions.Survive_default_htc_config_process(so, ct0conf, len); - } - - virtual protected void ButtonEvent(IntPtr so, byte eventType, byte buttonId, byte axis1Id, ushort axis1Val, byte axis2Id, ushort axis2Val) - { - Cfunctions.Survive_default_button_process(so, eventType, buttonId, axis1Id, axis1Val, axis2Id, axis2Val); - } - - virtual protected void AngleEvent(IntPtr so, int sensor_id, int acode, uint timecode, double length, double angle, uint lh) - { - Cfunctions.Survive_default_angle_process(so, sensor_id, acode, timecode, length, angle, lh); - - //Debug.Log("AngleEvent"); - } - - protected void LightHouseEvent(IntPtr ctx, byte lighthouse, SurvivePose lighthouse_pose, SurvivePose object_pose) - { - Cfunctions.Survive_default_lighthouse_pose_process(ctx, lighthouse, lighthouse_pose, object_pose); - - //Debug.Log("LightHouseEvent"); - } - - virtual protected void LightEvent(IntPtr so, int sensor_id, int acode, int timeinsweep, UInt32 timecode, UInt32 length, UInt32 lighthouse) - { - Cfunctions.Survive_default_light_process(so, sensor_id, acode, timeinsweep, timecode, length, lighthouse); - - //Console.WriteLine("LightEvent"); - //Debug.Log("LightEvent"); - } - - virtual protected void PoseEvent(IntPtr so, byte lighthouse, SurvivePose pose) - { - Cfunctions.Survive_default_raw_pose_process(so, lighthouse, pose); - - //Debug.Log("PoseEvent"); - } - - /* - public delegate void PoseUpdate(SurviveVector3 pos, SurviveQuaternion quat); - - public Dictionary updates = new Dictionary(); - - public void AddCalback(string ID, PoseUpdate update) - { - if (!updates.ContainsKey(ID)) - { - updates.Add(ID, update); - } - else - { - updates[ID] += update; - } - } - */ - - - public SurviveObject GetSurviveObjectByName(string name) - { - if (name == "") - { - throw new Exception("Empty string is not accepted"); - } - - if (context == IntPtr.Zero) - throw new Exception("The context hasn't been initialsied yet"); - - return new SurviveObject( Cfunctions.Survive_get_so_by_name(context, name)); - } -} - - -public struct SetupConfigs -{ - public string playbackFile; - public int playbackFactor; - public Disambiguator disambiguator; - public Poser poser; - public BoolConfig calibrate; - public string configFile; -} - -public enum Disambiguator -{ - StateBased, - Charles, - Turvey, - Default -} - -public enum Poser -{ - CharlesSlow, - DaveOrtho, - Dummy, - EPNP, - SBA -} - -public enum BoolConfig -{ - Yes, - No, - Default -} - - -public class SurviveObject -{ - private SurvivePose _pose; - public SurvivePose Pose - { - get - { - var ptr1 = Cfunctions.Survive_object_pose(ptr); - return (SurvivePose)Marshal.PtrToStructure(ptr1, typeof(SurvivePose)); - } - } - - private int _charge; - public int Charge - { - get - { - return Cfunctions.Survive_object_charge(ptr); - } - } - public bool Charging - { - get - { - return Cfunctions.Survive_object_charging(ptr); - } - } - - IntPtr ptr; - private bool _charging; - - public SurviveObject(IntPtr obj_ptr) - { - if (obj_ptr == IntPtr.Zero) - { - throw new Exception("Can't create SurviveObject with 0 pointer"); - } - ptr = obj_ptr; - } -} - -public class SurviveVector3 -{ - double x; - double y; - double z; -} - -public class SurviveQuaternion -{ - double x; - double y; - double z; - double w; -} diff --git a/bindings/cs/libsurvive.net/SurviveContext.cs b/bindings/cs/libsurvive.net/SurviveContext.cs index b1eb7112..0e85c9b5 100644 --- a/bindings/cs/libsurvive.net/SurviveContext.cs +++ b/bindings/cs/libsurvive.net/SurviveContext.cs @@ -11,73 +11,65 @@ public class SurviveContext : IDisposable { public delegate void PoseUpdate(int ObjectID, Vector3 pos); - protected IntPtr ctx; - - public void Dispose() - { - Cfunctions.Survive_close(ctx); - ctx = IntPtr.Zero; - } - /** + public void Dispose() { Close(); } + + public void Close() { + if (ctx != IntPtr.Zero) { + Cfunctions.Survive_close(ctx); + } + ctx = IntPtr.Zero; + } + + /** * We need to keep these delegates around for the lifespan of this object. * if we just pass the functions in, it creates temporary delegates that * disappear if we don't keep managed ref around */ light_process_func light_Process_Func; - raw_pose_func raw_Pose_Func; - lighthouse_pose_func lighthouse_Pose_Func; - angle_process_func angle_Process_Func; + pose_process_func raw_Pose_Func; + lighthouse_pose_process_func lighthouse_pose_process_func; + angle_process_func angle_Process_Func; button_process_func button_Process_Func; - htc_config_func htc_Config_Func; - imu_process_func imu_Process_Func; - text_feedback_func error_func; - text_feedback_func info_func; + config_func Config_Func; + imu_process_func imu_Process_Func; + log_process_func log_func; - public SurviveContext(string[] args) + public SurviveContext(string[] args) { Init(args); } - public SurviveContext(string replaydata) - { - string[] vs = new[] { "--playback", "P:/c/libsurvive-data/lightcap-reformat/lightcap-reformat.log", "--disambiguator", "StateBased", "--calibrate" }; - Init(vs); - } - internal void Init(string[] args) { string[] newArgs = new string[args.Length + 1]; newArgs[0] = System.Reflection.Assembly.GetEntryAssembly().FullName; Array.Copy(args, 0, newArgs, 1, args.Length); - ctx = Cfunctions.Survive_init_internal(newArgs.Length, newArgs); + ctx = Cfunctions.Survive_init_internal(newArgs.Length, newArgs, IntPtr.Zero, log_func); - if (ctx == IntPtr.Zero) + if (ctx == IntPtr.Zero) { throw new Exception("There was a problem initializing the lib!"); } light_Process_Func = LightEvent; raw_Pose_Func = PoseEvent; - lighthouse_Pose_Func = LightHouseEvent; - angle_Process_Func = AngleEvent; + lighthouse_pose_process_func = LightHouseEvent; + angle_Process_Func = AngleEvent; button_Process_Func = ButtonEvent; - htc_Config_Func = HTCConfigEvent; - imu_Process_Func = IMUEvent; - error_func = ErrorEvent; - info_func = InfoEvent; - - Cfunctions.Survive_install_raw_pose_fn(ctx, raw_Pose_Func); - Cfunctions.Survive_install_light_fn(ctx, light_Process_Func); - Cfunctions.Survive_install_lighthouse_pose_fn(ctx, lighthouse_Pose_Func); - Cfunctions.Survive_install_angle_fn(ctx, angle_Process_Func); + Config_Func = ConfigEvent; + imu_Process_Func = IMUEvent; + log_func = InfoEvent; + + Cfunctions.Survive_install_pose_fn(ctx, raw_Pose_Func); + Cfunctions.Survive_install_light_fn(ctx, light_Process_Func); + Cfunctions.Survive_install_lighthouse_pose_fn(ctx, lighthouse_pose_process_func); + Cfunctions.Survive_install_angle_fn(ctx, angle_Process_Func); Cfunctions.Survive_install_button_fn(ctx, button_Process_Func); - Cfunctions.Survive_install_htc_config_fn(ctx, htc_Config_Func); - Cfunctions.Survive_install_imu_fn(ctx, imu_Process_Func); - Cfunctions.Survive_install_error_fn(ctx, error_func); - Cfunctions.Survive_install_info_fn(ctx, info_func); + Cfunctions.Survive_install_config_fn(ctx, Config_Func); + Cfunctions.Survive_install_imu_fn(ctx, imu_Process_Func); } @@ -90,29 +82,25 @@ public void AddPoseUpdateCallback(PoseUpdate update, int objectID) poseUpdate += update; } + virtual protected void InfoEvent(SurviveObjectPtr ctx, UInt32 loglevl, string fault) { + Console.Out.WriteLine(fault); + } - - virtual protected void InfoEvent(SurviveObjectPtr ctx, string fault) - { - Console.Out.WriteLine(fault); - } - - virtual protected void ErrorEvent(SurviveObjectPtr ctx, string fault) + virtual protected void ErrorEvent(SurviveObjectPtr ctx, string fault) { Console.Error.WriteLine(fault); } - virtual protected void IMUEvent(SurviveObjectPtr so, int mask, double[] accelgyro, uint timecode, int id) - { - Cfunctions.Survive_default_imu_process(so, mask, accelgyro, timecode, id); - } + virtual protected void IMUEvent(SurviveObjectPtr so, int mask, SurviveIMUData accelgyro, uint timecode, + int id) { + Cfunctions.Survive_default_imu_process(so, mask, accelgyro, timecode, id); + } - virtual protected int HTCConfigEvent(SurviveObjectPtr so, string ct0conf, int len) - { - return Cfunctions.Survive_default_htc_config_process(so, ct0conf, len); - } + virtual protected int ConfigEvent(SurviveObjectPtr so, string ct0conf, int len) { + return Cfunctions.Survive_default_config_process(so, ct0conf, len); + } - virtual protected void ButtonEvent(SurviveObjectPtr so, byte eventType, byte buttonId, byte axis1Id, ushort axis1Val, byte axis2Id, ushort axis2Val) + virtual protected void ButtonEvent(SurviveObjectPtr so, byte eventType, byte buttonId, byte axis1Id, ushort axis1Val, byte axis2Id, ushort axis2Val) { Cfunctions.Survive_default_button_process(so, eventType, buttonId, axis1Id, axis1Val, axis2Id, axis2Val); } @@ -122,26 +110,21 @@ virtual protected void AngleEvent(SurviveObjectPtr so, int sensor_id, int acode, Cfunctions.Survive_default_angle_process(so, sensor_id, acode, timecode, length, angle, lh); } - protected void LightHouseEvent(SurviveObjectPtr ctx, byte lighthouse, SurvivePose lighthouse_pose, SurvivePose object_pose) - { - Cfunctions.Survive_default_lighthouse_pose_process(ctx, lighthouse, lighthouse_pose, object_pose); - } + protected void LightHouseEvent(SurviveObjectPtr ctx, byte lighthouse, SurvivePose lighthouse_pose) { + Cfunctions.Survive_default_lighthouse_pose_process(ctx, lighthouse, lighthouse_pose); + } - virtual protected void LightEvent(SurviveObjectPtr so, int sensor_id, int acode, int timeinsweep, UInt32 timecode, UInt32 length, UInt32 lighthouse) + virtual protected void LightEvent(SurviveObjectPtr so, int sensor_id, int acode, int timeinsweep, UInt32 timecode, UInt32 length, UInt32 lighthouse) { Cfunctions.Survive_default_light_process(so, sensor_id, acode, timeinsweep, timecode, length, lighthouse); - //Console.WriteLine("LightEvent"); } - virtual protected void PoseEvent(SurviveObjectPtr so, byte lighthouse, SurvivePose pose) - { - Cfunctions.Survive_default_raw_pose_process(so, lighthouse, pose); + virtual protected void PoseEvent(SurviveObjectPtr so, UInt32 timecode, SurvivePose pose) { + Cfunctions.Survive_default_pose_process(so, timecode, pose); + } - Console.WriteLine("PoseEvent"); - } - - public int Poll() + public int Poll() { return Cfunctions.Survive_poll(ctx); } @@ -161,4 +144,4 @@ public struct Qutarnion public float z; public float w; } -} \ No newline at end of file + } diff --git a/bindings/cs/libsurvive.net/cfunctions.cs b/bindings/cs/libsurvive.net/cfunctions.cs index d2416f6d..ab05b695 100644 --- a/bindings/cs/libsurvive.net/cfunctions.cs +++ b/bindings/cs/libsurvive.net/cfunctions.cs @@ -18,13 +18,24 @@ public class SurvivePose public double[] Rot; // Quaternion in the form wxyz } - class Cfunctions + [StructLayout(LayoutKind.Sequential)] + public class SurviveIMUData { + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] + public double[] Accel; // Position in the form xyz + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] + public double[] Gyro; // Position in the form xyz + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] + public double[] Mag; // Position in the form xyz + } + + class Cfunctions { //#pragma warning disable IDE1006 // Naming Styles - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_init_internal")] - public static extern SurviveContextPtr Survive_init_internal(int argc, string[] args); + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_init_internal")] + public static extern SurviveContextPtr Survive_init_internal(int argc, string[] args, IntPtr user, + log_process_func fbp); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_close")] + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_close")] public static extern SurviveContextPtr Survive_close(SurviveContextPtr ctx); [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_poll")] @@ -33,17 +44,14 @@ class Cfunctions [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_startup")] public static extern int Survive_startup(SurviveContextPtr ctx); + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, + EntryPoint = "survive_install_config_fn")] + public static extern void Survive_install_config_fn(SurviveContextPtr ctx, config_func fbp); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_install_htc_config_fn")] - public static extern void Survive_install_htc_config_fn(SurviveContextPtr ctx, htc_config_func fbp); + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_install_log_fn")] + public static extern void Survive_install_log_fn(SurviveContextPtr ctx, log_process_func fbp); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_install_info_fn")] - public static extern void Survive_install_info_fn(SurviveContextPtr ctx, text_feedback_func fbp); - - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_install_error_fn")] - public static extern void Survive_install_error_fn(SurviveContextPtr ctx, text_feedback_func fbp); - - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_install_imu_fn")] + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_install_imu_fn")] public static extern void Survive_install_imu_fn(SurviveContextPtr ctx, imu_process_func fbp); [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_install_angle_fn")] @@ -52,45 +60,50 @@ class Cfunctions [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_install_button_fn")] public static extern void Survive_install_button_fn(SurviveContextPtr ctx, button_process_func fbp); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_install_raw_pose_fn")] - public static extern void Survive_install_raw_pose_fn(SurviveContextPtr ctx, raw_pose_func fbp); + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_install_pose_fn")] + public static extern void Survive_install_pose_fn(SurviveContextPtr ctx, pose_process_func fbp); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_install_lighthouse_pose_fn")] - public static extern void Survive_install_lighthouse_pose_fn(SurviveContextPtr ctx, lighthouse_pose_func fbp); + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, + EntryPoint = "survive_install_lighthouse_pose_fn")] + public static extern void Survive_install_lighthouse_pose_fn(SurviveContextPtr ctx, + lighthouse_pose_process_func fbp); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_install_light_fn")] + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_install_light_fn")] public static extern void Survive_install_light_fn(SurviveContextPtr ctx, light_process_func fbp); [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_cal_install")] public static extern void Survive_cal_install(SurviveContextPtr ctx); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_default_light_process")] public static extern void Survive_default_light_process(SurviveObjectPtr so, int sensor_id, int acode, int timeinsweep, UInt32 timecode, UInt32 length, UInt32 lh); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_default_imu_process")] - public static extern void Survive_default_imu_process(SurviveObjectPtr so, int mode, double[] accelgyro, UInt32 timecode, int id); + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, + EntryPoint = "survive_default_imu_process")] + public static extern void Survive_default_imu_process(SurviveObjectPtr so, int mode, SurviveIMUData accelgyro, + UInt32 timecode, int id); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_default_angle_process")] + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_default_angle_process")] public static extern void Survive_default_angle_process(SurviveObjectPtr so, int sensor_id, int acode, UInt32 timecode, double length, double angle, UInt32 lh); [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_default_button_process")] public static extern void Survive_default_button_process(SurviveObjectPtr so, byte eventType, byte buttonId, byte axis1Id, UInt16 axis1Val, byte axis2Id, UInt16 axis2Val); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_default_pose_process")] - public static extern void Survive_default_raw_pose_process(SurviveObjectPtr so, byte lighthouse, SurvivePose pose); + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, + EntryPoint = "survive_default_pose_process")] + public static extern void Survive_default_pose_process(SurviveObjectPtr so, UInt32 timecode, SurvivePose pose); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_default_lighthouse_pose_process")] - public static extern void Survive_default_lighthouse_pose_process(SurviveContextPtr ctx, byte lighthouse, SurvivePose lh_pose, SurvivePose obj_pose); + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, + EntryPoint = "survive_default_lighthouse_pose_process")] + public static extern void Survive_default_lighthouse_pose_process(SurviveContextPtr ctx, byte lighthouse, + SurvivePose lh_pose); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_default_htc_config_process")] - public static extern int Survive_default_htc_config_process(SurviveObjectPtr so, string ct0conf, int len); + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, + EntryPoint = "survive_default_config_process")] + public static extern int Survive_default_config_process(SurviveObjectPtr so, string ct0conf, int len); - - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_object_codename")] + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_object_codename")] public static extern string Survive_object_codename(SurviveObjectPtr so); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_object_drivername")] public static extern char Survive_object_drivername(SurviveObjectPtr so); @@ -111,38 +124,35 @@ class Cfunctions public static extern double[] Survive_object_sensor_normals(SurviveObjectPtr so); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_get_so_by_name")] public static extern IntPtr Survive_get_so_by_name(IntPtr ctx, string name); //#pragma warning restore IDE1006 // Naming Styles } + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + public delegate int config_func(SurviveObjectPtr so, string ct0conf, int len); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - public delegate int htc_config_func(SurviveObjectPtr so, string ct0conf, int len); - - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - public delegate void text_feedback_func(SurviveContextPtr ctx, string fault); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + public delegate void log_process_func(SurviveContextPtr ctx, UInt32 loglevel, string fault); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] + [UnmanagedFunctionPointer(CallingConvention.StdCall)] public delegate void light_process_func(SurviveObjectPtr so, int sensor_id, int acode, int timeinsweep, UInt32 timecode, UInt32 length, UInt32 lighthouse); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - public delegate void imu_process_func(SurviveObjectPtr so, int mask, double[] accelgyro, UInt32 timecode, int id); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + public delegate void imu_process_func(SurviveObjectPtr so, int mask, SurviveIMUData accelgyro, UInt32 timecode, + int id); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] + [UnmanagedFunctionPointer(CallingConvention.StdCall)] public delegate void angle_process_func(SurviveObjectPtr so, int sensor_id, int acode, UInt32 timecode, double length, double angle, UInt32 lh); [UnmanagedFunctionPointer(CallingConvention.StdCall)] public delegate void button_process_func(SurviveObjectPtr so, byte eventType, byte buttonId, byte axis1Id, UInt16 axis1Val, byte axis2Id, UInt16 axis2Val); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - public delegate void raw_pose_func(SurviveObjectPtr so, byte lighthouse, SurvivePose pose); - - - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - public delegate void lighthouse_pose_func(SurviveContextPtr ctx, byte lighthouse, SurvivePose lighthouse_pose, SurvivePose object_pose); - + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + public delegate void pose_process_func(SurviveObjectPtr so, UInt32 timecode, SurvivePose pose); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + public delegate void lighthouse_pose_process_func(SurviveContextPtr ctx, byte lighthouse, + SurvivePose lighthouse_pose); } diff --git a/bindings/cs/libsurvive.net/libsurvive.net.csproj b/bindings/cs/libsurvive.net/libsurvive.net.csproj index 9f5c4f4a..72764a66 100644 --- a/bindings/cs/libsurvive.net/libsurvive.net.csproj +++ b/bindings/cs/libsurvive.net/libsurvive.net.csproj @@ -1,7 +1,7 @@ - - - - netstandard2.0 - - - + + + + netstandard2.0 + + +