From 91f0cab811e983da63ea49f6e24afae283138a1c Mon Sep 17 00:00:00 2001 From: Justin Berger Date: Wed, 28 Mar 2018 20:36:05 -0600 Subject: Functional C# in windows --- bindings/cs/libsurvive.net/SurviveContext.cs | 90 ++++++++++++++++++++++++ bindings/cs/libsurvive.net/cfunctions.cs | 84 ++++++++++++++++++++++ bindings/cs/libsurvive.net/libsurvive.net.csproj | 7 ++ 3 files changed, 181 insertions(+) create mode 100644 bindings/cs/libsurvive.net/SurviveContext.cs create mode 100644 bindings/cs/libsurvive.net/cfunctions.cs create mode 100644 bindings/cs/libsurvive.net/libsurvive.net.csproj (limited to 'bindings/cs/libsurvive.net') diff --git a/bindings/cs/libsurvive.net/SurviveContext.cs b/bindings/cs/libsurvive.net/SurviveContext.cs new file mode 100644 index 0000000..1fb8c11 --- /dev/null +++ b/bindings/cs/libsurvive.net/SurviveContext.cs @@ -0,0 +1,90 @@ +using System; + +namespace libsurvive +{ + using SurviveContextPtr = IntPtr; + using SurviveObjectPtr = IntPtr; + using SurvivePosePtr = IntPtr; + + public class SurviveContext : IDisposable + { + protected IntPtr ctx; + + public void Dispose() + { + cfunctions.survive_close(ctx); + ctx = IntPtr.Zero; + } + + public SurviveContext() : this(null) { } + + public SurviveContext(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); + + 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); + } + + protected void InfoEvent(SurviveObjectPtr ctx, string fault) + { + Console.Out.WriteLine(fault); + } + + protected void ErrorEvent(SurviveObjectPtr ctx, string fault) + { + Console.Error.WriteLine(fault); + } + + 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) + { + 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) + { + 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) + { + 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) + { + cfunctions.survive_default_lighthouse_pose_process(ctx, lighthouse, ref lighthouse_pose, ref object_pose); + } + + 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) + { + cfunctions.survive_default_raw_pose_process(so, lighthouse, ref pose); + } + + public int Poll() + { + return cfunctions.survive_poll(ctx); + } + } +} \ No newline at end of file diff --git a/bindings/cs/libsurvive.net/cfunctions.cs b/bindings/cs/libsurvive.net/cfunctions.cs new file mode 100644 index 0000000..f9d88f0 --- /dev/null +++ b/bindings/cs/libsurvive.net/cfunctions.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Text; + +namespace libsurvive +{ + using SurviveContextPtr = IntPtr; + using SurviveObjectPtr = IntPtr; + using SurvivePosePtr = IntPtr; + + [StructLayout(LayoutKind.Sequential)] + public struct SurvivePose + { + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] + public double[] Pos; // Position in the form xyz + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] + public double[] Rot; // Quaternion in the form wxyz + } + + class cfunctions + { + +#pragma warning disable IDE1006 // Naming Styles + [DllImport("libsurvive")] + public static extern SurviveContextPtr survive_init_internal(int argc, string[] args); + [DllImport("libsurvive")] + public static extern SurviveContextPtr survive_close(SurviveContextPtr ctx); + [DllImport("libsurvive")] + public static extern int survive_poll(SurviveContextPtr ctx); + [DllImport("libsurvive")] + public static extern int survive_startup(SurviveContextPtr ctx); + + [DllImport("libsurvive")] + public static extern void survive_install_htc_config_fn(SurviveContextPtr ctx, htc_config_func fbp); + [DllImport("libsurvive")] + public static extern void survive_install_info_fn(SurviveContextPtr ctx, text_feedback_func fbp); + [DllImport("libsurvive")] + public static extern void survive_install_error_fn(SurviveContextPtr ctx, text_feedback_func fbp); + [DllImport("libsurvive")] + public static extern void survive_install_imu_fn(SurviveContextPtr ctx, imu_process_func fbp); + [DllImport("libsurvive")] + public static extern void survive_install_angle_fn(SurviveContextPtr ctx, angle_process_func fbp); + [DllImport("libsurvive")] + public static extern void survive_install_button_fn(SurviveContextPtr ctx, button_process_func fbp); + [DllImport("libsurvive")] + public static extern void survive_install_raw_pose_fn(SurviveContextPtr ctx, raw_pose_func fbp); + [DllImport("libsurvive")] + public static extern void survive_install_lighthouse_pose_fn(SurviveContextPtr ctx, lighthouse_pose_func fbp); + [DllImport("libsurvive")] + public static extern void survive_install_light_fn(SurviveContextPtr ctx, light_process_func fbp); + [DllImport("libsurvive")] + public static extern void survive_cal_install(SurviveContextPtr ctx); + + [DllImport("libsurvive")] + 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")] + public static extern void survive_default_imu_process(SurviveObjectPtr so, int mode, double[] accelgyro, UInt32 timecode, int id); + [DllImport("libsurvive")] + 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")] + 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")] + public static extern int survive_default_htc_config_process(SurviveObjectPtr so, string ct0conf, int len); + +#pragma warning restore IDE1006 // Naming Styles + } + + public delegate int htc_config_func(SurviveObjectPtr so, string ct0conf, int len); + public delegate void text_feedback_func(SurviveContextPtr ctx, string fault); + public delegate void light_process_func(SurviveObjectPtr so, int sensor_id, int acode, int timeinsweep, UInt32 timecode, UInt32 length, UInt32 lighthouse); + public delegate void imu_process_func(SurviveObjectPtr so, int mask, double[] accelgyro, UInt32 timecode, int id); + public delegate void angle_process_func(SurviveObjectPtr so, int sensor_id, int acode, UInt32 timecode, double length, double angle, UInt32 lh); + 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); + +} diff --git a/bindings/cs/libsurvive.net/libsurvive.net.csproj b/bindings/cs/libsurvive.net/libsurvive.net.csproj new file mode 100644 index 0000000..eac3503 --- /dev/null +++ b/bindings/cs/libsurvive.net/libsurvive.net.csproj @@ -0,0 +1,7 @@ + + + + netstandard1.6 + + + -- cgit v1.2.3