Skip to content

Commit

Permalink
Add most of the DS class
Browse files Browse the repository at this point in the history
  • Loading branch information
ThadHouse committed Mar 19, 2024
1 parent 34b202a commit 7e22e01
Show file tree
Hide file tree
Showing 15 changed files with 1,102 additions and 64 deletions.
19 changes: 17 additions & 2 deletions src/hal/JoystickAxis.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;

namespace WPIHal;

[StructLayout(LayoutKind.Sequential)]
public readonly struct JoystickAxes
{
[System.Runtime.CompilerServices.InlineArray(12)]
public const int NumJoystickAxes = 12;

[System.Runtime.CompilerServices.InlineArray(NumJoystickAxes)]
public struct AxesBuffer
{
private float _element0;
}

[System.Runtime.CompilerServices.InlineArray(12)]
[System.Runtime.CompilerServices.InlineArray(NumJoystickAxes)]
public struct RawBuffer
{
private byte _element0;
Expand All @@ -22,6 +25,12 @@ public struct RawBuffer

public readonly int Count => m_count;

[UnscopedRef]
public ReadOnlySpan<float> AxesSpan => m_axes[..Count];

[UnscopedRef]
public ReadOnlySpan<byte> RawSpan => m_raw[..Count];

public readonly double? this[int index]
{
get
Expand Down Expand Up @@ -49,4 +58,10 @@ public readonly double? this[int index]
}

}

public bool IsEqual(ref readonly JoystickAxes other)
{
// Assumed that if raw is equal, floats are equal
return m_count == other.Count && RawSpan.SequenceEqual(other.RawSpan);
}
}
26 changes: 20 additions & 6 deletions src/hal/JoystickButtons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,39 @@ namespace WPIHal;
[StructLayout(LayoutKind.Sequential)]
public readonly struct JoystickButtons
{
private readonly uint buttons;
private readonly byte count;
private readonly uint m_buttons;
private readonly byte m_count;

public int Count => count;
public int Count => m_count;

public uint Buttons => buttons;
public uint Buttons => m_buttons;

public ReadOnlySpan<bool> ToSpan(Span<bool> storage)
{
for (int i = 0; i < m_count; i++)
{
storage[i] = (m_buttons & i) != 0;
}
return storage[..m_count];
}

public bool? this[int index]
{
get
{
if (index < count)
if (index < m_count)
{
return (buttons & index) != 0;
return (m_buttons & index) != 0;
}
else
{
return null;
}
}
}

public bool IsEqual(JoystickButtons other)
{
return m_count == other.m_count && m_buttons == other.m_buttons;
}
}
11 changes: 10 additions & 1 deletion src/hal/JoystickPOVs.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;

namespace WPIHal;

[StructLayout(LayoutKind.Sequential)]
public readonly struct JoystickPOVs
{
public const int NumJoystickPOVs = 12;

[System.Runtime.CompilerServices.InlineArray(12)]
[System.Runtime.CompilerServices.InlineArray(NumJoystickPOVs)]
public struct PovsBuffer
{
private short _element0;
Expand All @@ -17,6 +19,9 @@ public struct PovsBuffer

public readonly int Count => m_count;

[UnscopedRef]
public ReadOnlySpan<short> PovsSpan => m_povs[..Count];

public readonly int? this[int index]
{
get
Expand All @@ -32,4 +37,8 @@ public readonly int? this[int index]
}
}

public bool IsEqual(ref readonly JoystickPOVs other)
{
return m_count == other.Count && PovsSpan.SequenceEqual(other.PovsSpan);
}
}
13 changes: 10 additions & 3 deletions src/hal/MatchInfo.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using System.Text;

namespace WPIHal;

[NativeMarshalling(typeof(MatchInfoMarshaller))]
[StructLayout(LayoutKind.Auto)]
public record struct MatchInfo(string EventName, MatchType MatchType, int MatchNumber, int ReplayNumber, byte[] GameSpecificMessage);
public record struct MatchInfo(string EventName, MatchType MatchType, int MatchNumber, int ReplayNumber, string GameSpecificMessage);

[CustomMarshaller(typeof(MatchInfo), MarshalMode.ManagedToUnmanagedOut, typeof(MatchInfoMarshaller))]
[CustomMarshaller(typeof(MatchInfo), MarshalMode.ManagedToUnmanagedIn, typeof(MatchInfoMarshaller))]
Expand All @@ -19,7 +20,7 @@ public static unsafe MatchInfo ConvertToManaged(NativeMatchInfo unmanaged)
MatchType = unmanaged.matchType,
MatchNumber = unmanaged.matchNumber,
ReplayNumber = unmanaged.replayNumber,
GameSpecificMessage = unmanaged.gameSpecificMessage.FromRawBytes(unmanaged.gameSpecificMessageSize)
GameSpecificMessage = unmanaged.gameSpecificMessage.FromByteString(unmanaged.gameSpecificMessageSize)
};
}

Expand Down Expand Up @@ -49,9 +50,15 @@ public readonly unsafe byte[] FromRawBytes(int length)
{
byte[] ret = new byte[int.Min(length, 64)];
ReadOnlySpan<byte> thisSpan = this;
thisSpan[ret.Length..].CopyTo(ret.AsSpan());
thisSpan[..ret.Length].CopyTo(ret.AsSpan());
return ret;
}

public readonly unsafe string FromByteString(int length)
{
ReadOnlySpan<byte> thisSpan = this;
return Encoding.UTF8.GetString(thisSpan[..length]);
}
}

public Utf8StringBuffer eventName;
Expand Down
20 changes: 16 additions & 4 deletions src/hal/Natives/HalBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using System.Text;
using CommunityToolkit.Diagnostics;
using WPIHal.Handles;
using WPIHal.Marshal;
using WPIUtil;
Expand All @@ -16,7 +17,15 @@ public static partial class HalBase
[LibraryImport("wpiHal", EntryPoint = "HAL_Initialize")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
[return: MarshalAs(UnmanagedType.I4)]
public static partial bool Initialize(int timeout, int mode);
internal static partial bool Initialize(int timeout, int mode);

public static void Initialize()
{
if (!Initialize(500, 0))
{
ThrowHelper.ThrowInvalidOperationException("HAL failed to initialize");
}
}

[AutomateStatusCheck(StatusCheckMethod = StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_GetFPGATime")]
Expand All @@ -31,7 +40,8 @@ public static partial class HalBase
[AutomateStatusCheck(StatusCheckMethod = StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_GetBrownedOut")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial int GetBrownedOut(out HalStatus status);
[return: MarshalAs(UnmanagedType.I4)]
public static partial bool GetBrownedOut(out HalStatus status);

[LibraryImport("wpiHal", EntryPoint = "HAL_GetErrorMessage")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
Expand All @@ -41,7 +51,8 @@ public static partial class HalBase
[AutomateStatusCheck(StatusCheckMethod = StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_GetFPGAButton")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial int GetFPGAButton(out HalStatus status);
[return: MarshalAs(UnmanagedType.I4)]
public static partial bool GetFPGAButton(out HalStatus status);

[AutomateStatusCheck(StatusCheckMethod = StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_GetFPGARevision")]
Expand All @@ -68,7 +79,8 @@ public static partial class HalBase
[AutomateStatusCheck(StatusCheckMethod = StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_GetSystemActive")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial int GetSystemActive(out HalStatus status);
[return: MarshalAs(UnmanagedType.I4)]
public static partial bool GetSystemActive(out HalStatus status);

[LibraryImport("wpiHal", EntryPoint = "HAL_GetLastError")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
Expand Down
11 changes: 8 additions & 3 deletions src/hal/Natives/HalDriverStation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ public static partial class HalDriverStation
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial HalStatus GetControlWord(out ControlWord controlWord);

[LibraryImport("wpiHal", EntryPoint = "HAL_GetControlWord")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static unsafe partial HalStatus GetControlWordNative(uint* controlWord);

[LibraryImport("wpiHal", EntryPoint = "HAL_GetJoystickAxes")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial HalStatus GetJoystickAxes(int joystickNum, out JoystickAxes axes);

[LibraryImport("wpiHal", EntryPoint = "HAL_GetJoystickAxisType")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial HalStatus GetJoystickAxisType(int joystickNum, int axis);
public static partial int GetJoystickAxisType(int joystickNum, int axis);

[LibraryImport("wpiHal", EntryPoint = "HAL_GetJoystickButtons")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
Expand All @@ -36,7 +40,8 @@ public static partial class HalDriverStation

[LibraryImport("wpiHal", EntryPoint = "HAL_GetJoystickIsXbox")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial HalStatus GetJoystickIsXbox(int joystickNum);
[return: MarshalAs(UnmanagedType.I4)]
public static partial bool GetJoystickIsXbox(int joystickNum);

[LibraryImport("wpiHal", EntryPoint = "HAL_GetJoystickName")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
Expand All @@ -48,7 +53,7 @@ public static partial class HalDriverStation

[LibraryImport("wpiHal", EntryPoint = "HAL_GetJoystickType")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial HalStatus GetJoystickType(int joystickNum);
public static partial int GetJoystickType(int joystickNum);

[LibraryImport("wpiHal", EntryPoint = "HAL_GetMatchInfo")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
Expand Down
22 changes: 11 additions & 11 deletions src/ntcore/Generated/BooleanTopic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public BooleanTopic(NetworkTableInstance inst, NtTopic handle) : base(inst, hand
/// <returns>subscriber</returns>
public IBooleanSubscriber Subscribe(
bool defaultValue,
PubSubOptions options)
PubSubOptions options = default)
{
return new BooleanEntryImpl<NtSubscriber>(
this,
Expand Down Expand Up @@ -83,7 +83,7 @@ public IBooleanSubscriber Subscribe(
public IBooleanSubscriber SubscribeEx(
string typeString,
bool defaultValue,
PubSubOptions options)
PubSubOptions options = default)
{
return new BooleanEntryImpl<NtSubscriber>(
this,
Expand Down Expand Up @@ -112,7 +112,7 @@ public IBooleanSubscriber SubscribeEx(
public IBooleanSubscriber SubscribeEx(
ReadOnlySpan<byte> typeString,
bool defaultValue,
PubSubOptions options)
PubSubOptions options = default)
{
return new BooleanEntryImpl<NtSubscriber>(
this,
Expand All @@ -137,7 +137,7 @@ public IBooleanSubscriber SubscribeEx(
/// <param name="options">publish options</param>
/// <returns>publisher</returns>
public IBooleanPublisher Publish(
PubSubOptions options)
PubSubOptions options = default)
{
return new BooleanEntryImpl<NtPublisher>(
this,
Expand Down Expand Up @@ -165,7 +165,7 @@ public IBooleanPublisher Publish(
/// <returns>publisher</returns>
public IBooleanPublisher PublishEx(
string typeString, string properties,
PubSubOptions options)
PubSubOptions options = default)
{
return new BooleanEntryImpl<NtPublisher>(
this,
Expand Down Expand Up @@ -194,7 +194,7 @@ public IBooleanPublisher PublishEx(
public IBooleanPublisher PublishEx(
ReadOnlySpan<byte> typeString,
string properties,
PubSubOptions options)
PubSubOptions options = default)
{
return new BooleanEntryImpl<NtPublisher>(
this,
Expand Down Expand Up @@ -223,7 +223,7 @@ public IBooleanPublisher PublishEx(
public IBooleanPublisher PublishEx(
string typeString,
ReadOnlySpan<byte> properties,
PubSubOptions options)
PubSubOptions options = default)
{
return new BooleanEntryImpl<NtPublisher>(
this,
Expand Down Expand Up @@ -252,7 +252,7 @@ public IBooleanPublisher PublishEx(
public IBooleanPublisher PublishEx(
ReadOnlySpan<byte> typeString,
ReadOnlySpan<byte> properties,
PubSubOptions options)
PubSubOptions options = default)
{
return new BooleanEntryImpl<NtPublisher>(
this,
Expand Down Expand Up @@ -284,7 +284,7 @@ public IBooleanPublisher PublishEx(
/// <returns>entry</returns>
public IBooleanEntry GetEntry(
bool defaultValue,
PubSubOptions options)
PubSubOptions options = default)
{
return new BooleanEntryImpl<NtEntry>(
this,
Expand Down Expand Up @@ -318,7 +318,7 @@ public IBooleanEntry GetEntry(
public IBooleanEntry GetEntryEx(
string typeString,
bool defaultValue,
PubSubOptions options)
PubSubOptions options = default)
{
return new BooleanEntryImpl<NtEntry>(
this,
Expand Down Expand Up @@ -352,7 +352,7 @@ public IBooleanEntry GetEntryEx(
public IBooleanEntry GetEntryEx(
ReadOnlySpan<byte> typeString,
bool defaultValue,
PubSubOptions options)
PubSubOptions options = default)
{
return new BooleanEntryImpl<NtEntry>(
this,
Expand Down
Loading

0 comments on commit 7e22e01

Please sign in to comment.