Skip to content

Commit

Permalink
Interops (structures and DllImports) for "Sensor" part of API
Browse files Browse the repository at this point in the history
  • Loading branch information
bibigone committed Jul 7, 2019
1 parent c03a960 commit f541d91
Show file tree
Hide file tree
Showing 46 changed files with 4,811 additions and 1 deletion.
41 changes: 41 additions & 0 deletions K4AdotNet.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.572
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "K4AdotNet", "K4AdotNet\K4AdotNet.csproj", "{0646EAF8-8941-4CBE-9D1A-59C643171F3E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "0 - Solution Items", "0 - Solution Items", "{4214990D-F0AF-4206-9A3B-47919B99AB1C}"
ProjectSection(SolutionItems) = preProject
.gitignore = .gitignore
LICENSE = LICENSE
README.md = README.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "1 - Libraries", "1 - Libraries", "{8A43FD41-5FDD-4106-B41B-FF9C3135D12A}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "2 - Samples", "2 - Samples", "{11E945CD-4D46-4856-8467-72CE65DD4DF7}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "3 - Tests", "3 - Tests", "{B227E80C-8E74-486D-887E-AC31D7A70F96}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0646EAF8-8941-4CBE-9D1A-59C643171F3E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0646EAF8-8941-4CBE-9D1A-59C643171F3E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0646EAF8-8941-4CBE-9D1A-59C643171F3E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0646EAF8-8941-4CBE-9D1A-59C643171F3E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{0646EAF8-8941-4CBE-9D1A-59C643171F3E} = {8A43FD41-5FDD-4106-B41B-FF9C3135D12A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E2012927-1A97-4801-B3C9-76EBC40D34F4}
EndGlobalSection
EndGlobal
3 changes: 3 additions & 0 deletions K4AdotNet/Assembly.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System;

[assembly: CLSCompliant(isCompliant: true)]
178 changes: 178 additions & 0 deletions K4AdotNet/Delay.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
using System;

namespace K4AdotNet
{
public struct Delay :
IEquatable<Delay>, IEquatable<TimeSpan>, IEquatable<int>,
IComparable<Delay>, IComparable<TimeSpan>, IComparable<int>, IComparable,
IFormattable
{
public int ValueUsec;

public Delay(int valueUsec)
=> ValueUsec = valueUsec;

public Delay(TimeSpan value)
=> ValueUsec = checked((int)(value.Ticks / TimeStamp.UsecToTimeSpanTicksFactor));

public TimeSpan ToTimeSpan()
=> TimeSpan.FromTicks(ValueUsec * TimeStamp.UsecToTimeSpanTicksFactor);

public bool Equals(Delay other)
=> ValueUsec.Equals(other.ValueUsec);

public bool Equals(TimeSpan other)
=> Equals(new Delay(other));

public bool Equals(int otherUsec)
=> ValueUsec.Equals(otherUsec);

public int CompareTo(Delay other)
=> ValueUsec.CompareTo(other.ValueUsec);

public int CompareTo(TimeSpan other)
=> CompareTo(new Delay(other));

public int CompareTo(int otherUsec)
=> ValueUsec.CompareTo(otherUsec);

public int CompareTo(object obj)
{
if (obj is null)
return 1;
if (obj is Delay)
return CompareTo((Delay)obj);
if (obj is TimeSpan)
return CompareTo((TimeSpan)obj);
if (obj is IConvertible)
return CompareTo(Convert.ToInt32(obj));
throw new ArgumentException("Object is not a Delay or TimeSpan or integer number", nameof(obj));
}

public string ToString(string format, IFormatProvider formatProvider)
=> ValueUsec.ToString(format, formatProvider) + " usec";

public override bool Equals(object obj)
{
if (obj is null)
return false;
if (obj is Delay)
return Equals((Delay)obj);
if (obj is TimeSpan)
return Equals((TimeSpan)obj);
if (obj is IConvertible)
return Equals(Convert.ToInt32(obj));
return false;
}

public override int GetHashCode()
=> ValueUsec.GetHashCode();

public override string ToString()
=> ValueUsec.ToString() + " usec";

public static bool operator ==(Delay left, Delay right)
=> left.Equals(right);

public static bool operator !=(Delay left, Delay right)
=> !left.Equals(right);

public static bool operator <(Delay left, Delay right)
=> left.CompareTo(right) < 0;

public static bool operator >(Delay left, Delay right)
=> left.CompareTo(right) > 0;

public static bool operator <=(Delay left, Delay right)
=> left.CompareTo(right) <= 0;

public static bool operator >=(Delay left, Delay right)
=> left.CompareTo(right) >= 0;

public static bool operator ==(Delay left, TimeSpan right)
=> left.Equals(right);

public static bool operator !=(Delay left, TimeSpan right)
=> !left.Equals(right);

public static bool operator <(Delay left, TimeSpan right)
=> left.CompareTo(right) < 0;

public static bool operator >(Delay left, TimeSpan right)
=> left.CompareTo(right) > 0;

public static bool operator <=(Delay left, TimeSpan right)
=> left.CompareTo(right) <= 0;

public static bool operator >=(Delay left, TimeSpan right)
=> left.CompareTo(right) >= 0;

public static bool operator ==(TimeSpan left, Delay right)
=> new Delay(left).Equals(right);

public static bool operator !=(TimeSpan left, Delay right)
=> !new Delay(left).Equals(right);

public static bool operator <(TimeSpan left, Delay right)
=> new Delay(left).CompareTo(right) < 0;

public static bool operator >(TimeSpan left, Delay right)
=> new Delay(left).CompareTo(right) > 0;

public static bool operator <=(TimeSpan left, Delay right)
=> new Delay(left).CompareTo(right) <= 0;

public static bool operator >=(TimeSpan left, Delay right)
=> new Delay(left).CompareTo(right) >= 0;

public static bool operator ==(Delay left, int rightUsec)
=> left.Equals(rightUsec);

public static bool operator !=(Delay left, int rightUsec)
=> !left.Equals(rightUsec);

public static bool operator <(Delay left, int rightUsec)
=> left.CompareTo(rightUsec) < 0;

public static bool operator >(Delay left, int rightUsec)
=> left.CompareTo(rightUsec) > 0;

public static bool operator <=(Delay left, int rightUsec)
=> left.CompareTo(rightUsec) <= 0;

public static bool operator >=(Delay left, int rightUsec)
=> left.CompareTo(rightUsec) >= 0;

public static bool operator ==(int leftUsec, Delay right)
=> new Delay(leftUsec).Equals(right);

public static bool operator !=(int leftUsec, Delay right)
=> !new Delay(leftUsec).Equals(right);

public static bool operator <(int leftUsec, Delay right)
=> new Delay(leftUsec).CompareTo(right) < 0;

public static bool operator >(int leftUsec, Delay right)
=> new Delay(leftUsec).CompareTo(right) > 0;

public static bool operator <=(int leftUsec, Delay right)
=> new Delay(leftUsec).CompareTo(right) <= 0;

public static bool operator >=(int leftUsec, Delay right)
=> new Delay(leftUsec).CompareTo(right) >= 0;

public static implicit operator TimeSpan(Delay value)
=> value.ToTimeSpan();

public static implicit operator Delay(TimeSpan value)
=> new Delay(value);

public static implicit operator int(Delay value)
=> value.ValueUsec;

public static implicit operator Delay(int valueUsec)
=> new Delay(valueUsec);

public static readonly Delay Zero = new Delay(0);
}
}
115 changes: 115 additions & 0 deletions K4AdotNet/Float2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using System;
using System.Runtime.InteropServices;

namespace K4AdotNet
{
// Defined in k4atypes.h:
// typedef union
// {
// struct _xy
// {
// float x; /**< X component of a vector. */
// float y; /**< Y component of a vector. */
// } xy; /**< X, Y representation of a vector. */
// float v[2]; /**< Array representation of a vector. */
// } k4a_float2_t;
/// <summary>Two dimensional floating point vector.</summary>
[StructLayout(LayoutKind.Sequential)]
public struct Float2 : IEquatable<Float2>, IFormattable
{
/// <summary>X component of a vector. Corresponds to <c>0</c> index in array representation.</summary>
public float X;

/// <summary>Y component of a vector. Corresponds to <c>1</c> index in array representation.</summary>
public float Y;

/// <summary>Constructs vector with given components.</summary>
/// <param name="x">X component</param>
/// <param name="y">Y component</param>
public Float2(float x, float y)
{
X = x;
Y = y;
}

public Float2(float[] values)
{
if (values is null)
throw new ArgumentNullException(nameof(values));
if (values.Length != 2)
throw new ArgumentOutOfRangeException(nameof(values) + "." + nameof(values.Length));
X = values[0];
Y = values[1];
}

public float[] ToArray()
=> new[] { X, Y };

/// <summary>Indexed access to vector components.</summary>
/// <param name="index">Index of component: <c>X</c> - <c>0</c>, <c>Y</c> - <c>1</c>.</param>
/// <returns>Value of appropriate component.</returns>
public float this[int index]
{
get
{
switch (index)
{
case 0: return X;
case 1: return Y;
default: throw new ArgumentOutOfRangeException(nameof(index));
}
}

set
{
switch (index)
{
case 0: X = value; break;
case 1: Y = value; break;
default: throw new ArgumentOutOfRangeException(nameof(index));
}
}
}

/// <summary>Per-component comparison.</summary>
/// <param name="other">Other vector to be compared to this one.</param>
/// <returns><c>true</c> if all components are equal.</returns>
public bool Equals(Float2 other)
=> X.Equals(other.X) && Y.Equals(other.Y);

public override bool Equals(object obj)
{
if (obj is null || !(obj is Float2))
return false;
return Equals((Float2)obj);
}

public static bool operator ==(Float2 left, Float2 right)
=> left.Equals(right);

public static bool operator !=(Float2 left, Float2 right)
=> !left.Equals(right);

public override int GetHashCode()
=> X.GetHashCode() ^ Y.GetHashCode();

/// <summary>Formats vector as <c>[X Y]</c> string.</summary>
/// <param name="format">Format string for each individual component in string representation.</param>
/// <param name="formatProvider">Culture for formatting numbers to strings.</param>
/// <returns>String representation of vector in a given Culture.</returns>
public string ToString(string format, IFormatProvider formatProvider)
=> $"[{X.ToString(format, formatProvider)} {Y.ToString(format, formatProvider)}]";

public override string ToString()
=> $"[{X} {Y}]";

/// <summary>Zero vector.</summary>
public static readonly Float2 Zero = new Float2();

/// <summary>Unit vector in +X direction.</summary>
public static readonly Float2 UnitX = new Float2(1, 0);

/// <summary>Unit vector in +Y direction.</summary>
public static readonly Float2 UnitY = new Float2(0, 1);
}
}
Loading

0 comments on commit f541d91

Please sign in to comment.