From cf4c2c50e75e6b21d7aa65211031417619e4dacd Mon Sep 17 00:00:00 2001 From: dpeter99 Date: Fri, 30 Mar 2018 20:54:08 +0200 Subject: Some tests --- bindings/cs/Demo/Program.cs | 46 ++------ bindings/cs/libsurvive.net/SurviveContext.cs | 93 ++++++++++++---- bindings/cs/libsurvive.net/cfunctions.cs | 132 ++++++++++++++--------- bindings/cs/libsurvive.net/libsurvive.net.csproj | 2 +- 4 files changed, 165 insertions(+), 108 deletions(-) (limited to 'bindings/cs') diff --git a/bindings/cs/Demo/Program.cs b/bindings/cs/Demo/Program.cs index c03d83c..aa98b6c 100644 --- a/bindings/cs/Demo/Program.cs +++ b/bindings/cs/Demo/Program.cs @@ -7,50 +7,22 @@ using System.Threading.Tasks; namespace Demo { - internal class MyHandler : SurviveContext + + class Program { - private static void WritePose(string name, SurvivePose pose) + static void Main(string[] args) { - Console.Out.WriteLine(name); - Console.Out.Write(" [ "); - for (int i = 0; i < 3; i++) - Console.Out.Write("{0} ", pose.Pos[i]); - Console.Out.Write(" ] [ "); - for (int i = 0; i < 4; i++) - Console.Out.Write("{0} ", pose.Rot[i]); - Console.Out.Write(" ] "); - Console.Out.WriteLine(); - } + SurviveContext context = new SurviveContext(args); + context.AddPoseUpdateCallback(HMDUpdate, -1); - public MyHandler() : base() - { - } - public MyHandler(string[] args) : base(args) - { - } + while (context.Poll() == 0) { + } - protected void LightHouseEvent1(IntPtr ctx, byte lighthouse, SurvivePose lighthouse_pose, SurvivePose object_pose) - { - base.LightHouseEvent(ctx, lighthouse, lighthouse_pose, object_pose); - WritePose("Lighthouse", lighthouse_pose); - WritePose("Object", object_pose); } - - protected override void PoseEvent(IntPtr so, byte lighthouse, SurvivePose pose) - { - WritePose("Pose", pose); - base.PoseEvent(so, lighthouse, pose); - } - } - class Program - { - static void Main(string[] args) + + public static void HMDUpdate(int ObjectID, Vector3 pos) { - MyHandler handler = new MyHandler(args); - - while (handler.Poll() == 0) { - } } diff --git a/bindings/cs/libsurvive.net/SurviveContext.cs b/bindings/cs/libsurvive.net/SurviveContext.cs index bafcf2b..37f67e4 100644 --- a/bindings/cs/libsurvive.net/SurviveContext.cs +++ b/bindings/cs/libsurvive.net/SurviveContext.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; namespace libsurvive { @@ -8,16 +9,17 @@ namespace libsurvive public class SurviveContext : IDisposable { + public delegate void PoseUpdate(int ObjectID, Vector3 pos); + + protected IntPtr ctx; public void Dispose() { - cfunctions.survive_close(ctx); + cfunctions.Survive_close(ctx); ctx = IntPtr.Zero; } - public SurviveContext() : this(null) { } - /** * 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 @@ -34,12 +36,28 @@ namespace libsurvive text_feedback_func info_func; public SurviveContext(string[] args) + { + Init(args); + } + + public SurviveContext(string replaydata) + { + string[] vs = new[] { "--playback", "lightcap-reformat.log"}; + 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); + + if (ctx == IntPtr.Zero) + { + throw new Exception("There was a problem initializing the lib!"); + } light_Process_Func = LightEvent; raw_Pose_Func = PoseEvent; @@ -51,17 +69,29 @@ namespace libsurvive 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); - 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_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); + 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); } + + //Dictionary PoseUpdateCallbacks = new Dictionary(); + PoseUpdate poseUpdate; + + public void AddPoseUpdateCallback(PoseUpdate update, int objectID) + { + //PoseUpdateCallbacks[objectID] += update; + poseUpdate += update; + } + + + virtual protected void InfoEvent(SurviveObjectPtr ctx, string fault) { Console.Out.WriteLine(fault); @@ -74,42 +104,61 @@ namespace libsurvive virtual protected void IMUEvent(SurviveObjectPtr so, int mask, double[] accelgyro, uint timecode, int id) { - cfunctions.survive_default_imu_process(so, mask, accelgyro, timecode, 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); + return cfunctions.Survive_default_htc_config_process(so, ct0conf, len); } 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); + cfunctions.Survive_default_button_process(so, eventType, buttonId, axis1Id, axis1Val, axis2Id, axis2Val); } virtual protected void AngleEvent(SurviveObjectPtr 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); + 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); + cfunctions.Survive_default_lighthouse_pose_process(ctx, lighthouse, lighthouse_pose, object_pose); } 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); + cfunctions.Survive_default_light_process(so, sensor_id, acode, timeinsweep, timecode, length, lighthouse); + + //Console.WriteLine("LightEvent"); } - virtual protected void PoseEvent(IntPtr so, byte lighthouse, SurvivePose pose) + virtual protected void PoseEvent(SurviveObjectPtr so, byte lighthouse, SurvivePose pose) { - cfunctions.survive_default_raw_pose_process(so, lighthouse, pose); + cfunctions.Survive_default_raw_pose_process(so, lighthouse, pose); + + Console.WriteLine("PoseEvent"); } public int Poll() { - return cfunctions.survive_poll(ctx); + return cfunctions.Survive_poll(ctx); } } + + public struct Vector3 + { + public float x; + public float y; + public float z; + } + + public struct Qutarnion + { + public float x; + public float y; + 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 760681e..6715a36 100644 --- a/bindings/cs/libsurvive.net/cfunctions.cs +++ b/bindings/cs/libsurvive.net/cfunctions.cs @@ -18,74 +18,110 @@ namespace libsurvive public double[] Rot; // Quaternion in the form wxyz } + [StructLayout(LayoutKind.Sequential)] + public class SurviveObject + { + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] + public char[] codename; //3 letters, null-terminated. Currently HMD, WM0, WM1. + } + class cfunctions { -#pragma warning disable IDE1006 // Naming Styles - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall) ] - public static extern SurviveContextPtr survive_init_internal(int argc, string[] args); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] - public static extern SurviveContextPtr survive_close(SurviveContextPtr ctx); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] - public static extern int survive_poll(SurviveContextPtr ctx); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] - public static extern int survive_startup(SurviveContextPtr ctx); - - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] - public static extern void survive_install_htc_config_fn(SurviveContextPtr ctx, htc_config_func fbp); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] - public static extern void survive_install_info_fn(SurviveContextPtr ctx, text_feedback_func fbp); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] - public static extern void survive_install_error_fn(SurviveContextPtr ctx, text_feedback_func fbp); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] - public static extern void survive_install_imu_fn(SurviveContextPtr ctx, imu_process_func fbp); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] - public static extern void survive_install_angle_fn(SurviveContextPtr ctx, angle_process_func fbp); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] - public static extern void survive_install_button_fn(SurviveContextPtr ctx, button_process_func fbp); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] - public static extern void survive_install_raw_pose_fn(SurviveContextPtr ctx, raw_pose_func fbp); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] - public static extern void survive_install_lighthouse_pose_fn(SurviveContextPtr ctx, lighthouse_pose_func fbp); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] - public static extern void survive_install_light_fn(SurviveContextPtr ctx, light_process_func fbp); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] - public static extern void survive_cal_install(SurviveContextPtr ctx); - - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] - 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)] - public static extern void survive_default_imu_process(SurviveObjectPtr so, int mode, double[] accelgyro, UInt32 timecode, int id); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] - 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)] - 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)] - public static extern void survive_default_raw_pose_process(SurviveObjectPtr so, byte lighthouse, SurvivePose pose); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] - public static extern void survive_default_lighthouse_pose_process(SurviveContextPtr ctx, byte lighthouse, SurvivePose lh_pose, - SurvivePose obj_pose); - [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] - public static extern int survive_default_htc_config_process(SurviveObjectPtr so, string ct0conf, int len); - -#pragma warning restore IDE1006 // Naming Styles +//#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_close")] + public static extern SurviveContextPtr Survive_close(SurviveContextPtr ctx); + + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_poll")] + public static extern int Survive_poll(SurviveContextPtr ctx); + + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_startup")] + public static extern int Survive_startup(SurviveContextPtr ctx); + + + [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_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")] + public static extern void Survive_install_imu_fn(SurviveContextPtr ctx, imu_process_func fbp); + + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall, EntryPoint = "survive_install_angle_fn")] + public static extern void Survive_install_angle_fn(SurviveContextPtr ctx, angle_process_func fbp); + + [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_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_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_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_raw_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_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_htc_config_process")] + public static extern int Survive_default_htc_config_process(SurviveObjectPtr so, string ct0conf, int len); + + +//#pragma warning restore IDE1006 // Naming Styles } + + [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 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 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); + } diff --git a/bindings/cs/libsurvive.net/libsurvive.net.csproj b/bindings/cs/libsurvive.net/libsurvive.net.csproj index eac3503..9f5c4f4 100644 --- a/bindings/cs/libsurvive.net/libsurvive.net.csproj +++ b/bindings/cs/libsurvive.net/libsurvive.net.csproj @@ -1,7 +1,7 @@ - netstandard1.6 + netstandard2.0 -- cgit v1.2.3