From 4856dd8d4121f23750baace5fd8bd81869f5ac81 Mon Sep 17 00:00:00 2001 From: Justin Berger Date: Thu, 29 Mar 2018 00:17:07 -0600 Subject: Filled out OO struct; most needed functions are there --- bindings/cs/Demo/Program.cs | 28 +++++++++++- bindings/cs/libsurvive.net/SurviveContext.cs | 65 ++++++++++++++++++--------- bindings/cs/libsurvive.net/cfunctions.cs | 67 +++++++++++++++------------- include/libsurvive/survive_types.h | 3 +- winbuild/libsurvive/libsurvive.vcxproj | 5 ++- 5 files changed, 114 insertions(+), 54 deletions(-) diff --git a/bindings/cs/Demo/Program.cs b/bindings/cs/Demo/Program.cs index 1644360..c03d83c 100644 --- a/bindings/cs/Demo/Program.cs +++ b/bindings/cs/Demo/Program.cs @@ -9,13 +9,39 @@ namespace Demo { internal class MyHandler : SurviveContext { - public MyHandler() + private static void WritePose(string name, SurvivePose pose) + { + 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(); + } + + public MyHandler() : base() { } public MyHandler(string[] args) : base(args) { } + + 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 { diff --git a/bindings/cs/libsurvive.net/SurviveContext.cs b/bindings/cs/libsurvive.net/SurviveContext.cs index 1fb8c11..bafcf2b 100644 --- a/bindings/cs/libsurvive.net/SurviveContext.cs +++ b/bindings/cs/libsurvive.net/SurviveContext.cs @@ -18,6 +18,21 @@ namespace libsurvive 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 + * 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; + 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 SurviveContext(string[] args) { string[] newArgs = new string[args.Length + 1]; @@ -26,60 +41,70 @@ namespace libsurvive ctx = cfunctions.survive_init_internal(newArgs.Length, newArgs); - cfunctions.survive_install_raw_pose_fn(ctx, PoseEvent); - cfunctions.survive_install_light_fn(ctx, LightEvent); - cfunctions.survive_install_lighthouse_pose_fn(ctx, LightHouseEvent); - cfunctions.survive_install_angle_fn(ctx, AngleEvent); - cfunctions.survive_install_button_fn(ctx, ButtonEvent); - cfunctions.survive_install_htc_config_fn(ctx, HTCConfigEvent); - cfunctions.survive_install_imu_fn(ctx, IMUEvent); - cfunctions.survive_install_error_fn(ctx, ErrorEvent); - cfunctions.survive_install_info_fn(ctx, InfoEvent); + 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(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); } - protected void InfoEvent(SurviveObjectPtr ctx, string fault) + virtual protected void InfoEvent(SurviveObjectPtr ctx, string fault) { Console.Out.WriteLine(fault); } - protected void ErrorEvent(SurviveObjectPtr ctx, string fault) + virtual protected void ErrorEvent(SurviveObjectPtr ctx, string fault) { Console.Error.WriteLine(fault); } - protected void IMUEvent(SurviveObjectPtr so, int mask, double[] accelgyro, uint timecode, int id) + virtual protected void IMUEvent(SurviveObjectPtr so, int mask, double[] accelgyro, uint timecode, int id) { cfunctions.survive_default_imu_process(so, mask, accelgyro, timecode, id); } - protected int HTCConfigEvent(SurviveObjectPtr so, string ct0conf, int len) + virtual protected int HTCConfigEvent(SurviveObjectPtr so, string ct0conf, int len) { return cfunctions.survive_default_htc_config_process(so, ct0conf, len); } - 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); } - protected void AngleEvent(SurviveObjectPtr so, int sensor_id, int acode, uint timecode, double length, double angle, uint lh) + 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); } - protected void LightHouseEvent(SurviveObjectPtr ctx, byte lighthouse, ref SurvivePose lighthouse_pose, ref SurvivePose object_pose) + protected void LightHouseEvent(SurviveObjectPtr ctx, byte lighthouse, SurvivePose lighthouse_pose, SurvivePose object_pose) { - cfunctions.survive_default_lighthouse_pose_process(ctx, lighthouse, ref lighthouse_pose, ref object_pose); + cfunctions.survive_default_lighthouse_pose_process(ctx, lighthouse, lighthouse_pose, object_pose); } - 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); } - protected void PoseEvent(IntPtr so, byte lighthouse, ref SurvivePose pose) + virtual protected void PoseEvent(IntPtr so, byte lighthouse, SurvivePose pose) { - cfunctions.survive_default_raw_pose_process(so, lighthouse, ref pose); + cfunctions.survive_default_raw_pose_process(so, lighthouse, pose); } public int Poll() diff --git a/bindings/cs/libsurvive.net/cfunctions.cs b/bindings/cs/libsurvive.net/cfunctions.cs index f9d88f0..760681e 100644 --- a/bindings/cs/libsurvive.net/cfunctions.cs +++ b/bindings/cs/libsurvive.net/cfunctions.cs @@ -10,7 +10,7 @@ namespace libsurvive using SurvivePosePtr = IntPtr; [StructLayout(LayoutKind.Sequential)] - public struct SurvivePose + public class SurvivePose { [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] public double[] Pos; // Position in the form xyz @@ -20,65 +20,72 @@ namespace libsurvive class cfunctions { - #pragma warning disable IDE1006 // Naming Styles - [DllImport("libsurvive")] + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall) ] public static extern SurviveContextPtr survive_init_internal(int argc, string[] args); - [DllImport("libsurvive")] + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] public static extern SurviveContextPtr survive_close(SurviveContextPtr ctx); - [DllImport("libsurvive")] + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] public static extern int survive_poll(SurviveContextPtr ctx); - [DllImport("libsurvive")] + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] public static extern int survive_startup(SurviveContextPtr ctx); - [DllImport("libsurvive")] + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] public static extern void survive_install_htc_config_fn(SurviveContextPtr ctx, htc_config_func fbp); - [DllImport("libsurvive")] + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] public static extern void survive_install_info_fn(SurviveContextPtr ctx, text_feedback_func fbp); - [DllImport("libsurvive")] + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] public static extern void survive_install_error_fn(SurviveContextPtr ctx, text_feedback_func fbp); - [DllImport("libsurvive")] + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] public static extern void survive_install_imu_fn(SurviveContextPtr ctx, imu_process_func fbp); - [DllImport("libsurvive")] + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] public static extern void survive_install_angle_fn(SurviveContextPtr ctx, angle_process_func fbp); - [DllImport("libsurvive")] + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] public static extern void survive_install_button_fn(SurviveContextPtr ctx, button_process_func fbp); - [DllImport("libsurvive")] + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] public static extern void survive_install_raw_pose_fn(SurviveContextPtr ctx, raw_pose_func fbp); - [DllImport("libsurvive")] + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] public static extern void survive_install_lighthouse_pose_fn(SurviveContextPtr ctx, lighthouse_pose_func fbp); - [DllImport("libsurvive")] + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] public static extern void survive_install_light_fn(SurviveContextPtr ctx, light_process_func fbp); - [DllImport("libsurvive")] + [DllImport("libsurvive", CallingConvention = CallingConvention.StdCall)] public static extern void survive_cal_install(SurviveContextPtr ctx); - [DllImport("libsurvive")] + [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")] + [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")] + [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")] + [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")] - public static extern void survive_default_raw_pose_process(SurviveObjectPtr so, byte lighthouse, ref SurvivePose pose); - [DllImport("libsurvive")] - public static extern void survive_default_lighthouse_pose_process(SurviveContextPtr ctx, byte lighthouse, ref SurvivePose lh_pose, - ref SurvivePose obj_pose); - [DllImport("libsurvive")] + [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 } - + [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); - public delegate void raw_pose_func(SurviveObjectPtr so, byte lighthouse, ref SurvivePose pose); - public delegate void lighthouse_pose_func(SurviveContextPtr ctx, byte lighthouse, ref SurvivePose lighthouse_pose, - ref SurvivePose object_pose); + [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/include/libsurvive/survive_types.h b/include/libsurvive/survive_types.h index 160adda..7a7dbf1 100644 --- a/include/libsurvive/survive_types.h +++ b/include/libsurvive/survive_types.h @@ -50,8 +50,7 @@ typedef void (*imu_process_func)( SurviveObject * so, int mask, FLT * accelgyro, typedef void (*angle_process_func)( SurviveObject * so, int sensor_id, int acode, uint32_t timecode, FLT length, FLT angle, uint32_t lh); typedef void(*button_process_func)(SurviveObject * so, uint8_t eventType, uint8_t buttonId, uint8_t axis1Id, uint16_t axis1Val, uint8_t axis2Id, uint16_t axis2Val); typedef void (*raw_pose_func)(SurviveObject *so, uint8_t lighthouse, SurvivePose *pose); -typedef void (*lighthouse_pose_func)(SurviveContext *ctx, uint8_t lighthouse, SurvivePose *lighthouse_pose, - SurvivePose *object_pose); +typedef void (*lighthouse_pose_func)(SurviveContext *ctx, uint8_t lighthouse, SurvivePose *lighthouse_pose, SurvivePose *object_pose); // For lightcap, etc. Don't change this structure at all. Regular vive is dependent on it being exactly as-is. // When you write drivers, you can use this to send survive lightcap data. diff --git a/winbuild/libsurvive/libsurvive.vcxproj b/winbuild/libsurvive/libsurvive.vcxproj index 2451c22..5caa159 100644 --- a/winbuild/libsurvive/libsurvive.vcxproj +++ b/winbuild/libsurvive/libsurvive.vcxproj @@ -83,6 +83,7 @@ Disabled FLT=double;USE_DOUBLE;MANUAL_REGISTRATION;NOZLIB;_CRT_SECURE_NO_WARNINGS;HIDAPI;WINDOWS;_DEBUG;_LIB;HAVE_LAPACK_CONFIG_H;LAPACK_COMPLEX_STRUCTURE;%(PreprocessorDefinitions) ..\..\winbuild;..\..\include\libsurvive;..\..\redist;%(AdditionalIncludeDirectories) + Caret Windows @@ -213,7 +214,9 @@ - + + Designer + -- cgit v1.2.3