Skip to content

Commit

Permalink
HAL Work
Browse files Browse the repository at this point in the history
  • Loading branch information
ThadHouse committed Feb 27, 2024
1 parent ab9cc2b commit 1cd5d5c
Show file tree
Hide file tree
Showing 16 changed files with 260 additions and 68 deletions.
1 change: 1 addition & 0 deletions codehelp/CodeHelpers/StatusCheckGenerator/MethodModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ internal static class MethodModelExtensions

var returnTypeFmt = new SymbolDisplayFormat(
memberOptions: SymbolDisplayMemberOptions.IncludeType | SymbolDisplayMemberOptions.IncludeRef,
genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters,
typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces,
globalNamespaceStyle: SymbolDisplayGlobalNamespaceStyle.Included,
miscellaneousOptions: SymbolDisplayMiscellaneousOptions.UseSpecialTypes
Expand Down
8 changes: 0 additions & 8 deletions src/hal/AccelerometerRange.cs

This file was deleted.

17 changes: 0 additions & 17 deletions src/hal/AddressableLEDData.cs

This file was deleted.

46 changes: 45 additions & 1 deletion src/hal/CAMStreamMessage.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace WPIHal;

public struct CANStreamMessage;
[StructLayout(LayoutKind.Sequential)]
public struct CANStreamMessage
{
public uint MessageID;
public uint TimeStamp;
public DataBuffer Data;
public byte DataSize;

[System.Runtime.CompilerServices.InlineArray(8)]
public struct DataBuffer
{
private byte _element0;
}
}

public readonly struct CANMessage
{
private readonly ulong timestamp;
private readonly DataBuffer data;
private readonly int dataSize;

public CANMessage(ReadOnlySpan<byte> buffer, ulong timestamp)
{
Debug.Assert(buffer.Length <= 8);
buffer.CopyTo(data);
this.timestamp = timestamp;
this.dataSize = buffer.Length;
}

[UnscopedRef]
public ReadOnlySpan<byte> Data => data[..dataSize];

public ulong Timestamp => timestamp;

[InlineArray(8)]
public struct DataBuffer
{
private byte _element0;
}
}
16 changes: 16 additions & 0 deletions src/hal/HalStatus.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
using System.Runtime.CompilerServices;

namespace WPIHal;

public enum HalStatus : int
{
Ok = 0,
}

public static class HalStatusExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowIfFailed(this HalStatus status)
{
if (status == HalStatus.Ok)
{
return;
}
// TODO Throw Exception
throw new InvalidOperationException();
}
}
13 changes: 10 additions & 3 deletions src/hal/Natives/HalAccelerometer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ namespace WPIHal.Natives;

public static partial class HalAccelerometer
{
public enum Range : int
{
#pragma warning disable CA1712 // Do not prefix enum values with type name
Range2G = 0,
Range4G = 1,
Range8G = 2,
#pragma warning restore CA1712 // Do not prefix enum values with type name
}

[LibraryImport("wpiHal", EntryPoint = "HAL_GetAccelerometerX")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial double GetAccelerometerX();
Expand All @@ -23,7 +32,5 @@ public static partial class HalAccelerometer

[LibraryImport("wpiHal", EntryPoint = "HAL_SetAccelerometerRange")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void SetAccelerometerRange(AccelerometerRange range);


public static partial void SetAccelerometerRange(Range range);
}
24 changes: 20 additions & 4 deletions src/hal/Natives/HalAddressableLED.cs
Original file line number Diff line number Diff line change
@@ -1,51 +1,67 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using WPIHal.Handles;
using WPIUtil;

namespace WPIHal.Natives;

public static partial class HalAddressableLED
{
[StructLayout(LayoutKind.Sequential)]
public struct LedData
{
public byte B;
public byte G;
public byte R;
public byte Padding;
}

[LibraryImport("wpiHal", EntryPoint = "HAL_FreeAddressableLED")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void FreeAddressableLED(HalAddressableLEDHandle handle);

[AutomateStatusCheck(StatusCheckMethod = HalBase.StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_InitializeAddressableLED")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial HalAddressableLEDHandle InitializeAddressableLED(HalDigitalHandle outputPort, out HalStatus status);

[AutomateStatusCheck(StatusCheckMethod = HalBase.StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_SetAddressableLEDBitTiming")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void SetAddressableLEDBitTiming(HalAddressableLEDHandle handle, int highTime0NanoSeconds, int lowTime0NanoSeconds, int highTime1NanoSeconds, int lowTime1NanoSeconds, out HalStatus status);

[AutomateStatusCheck(StatusCheckMethod = HalBase.StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_SetAddressableLEDLength")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void SetAddressableLEDLength(HalAddressableLEDHandle handle, int length, out HalStatus status);

[AutomateStatusCheck(StatusCheckMethod = HalBase.StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_SetAddressableLEDOutputPort")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void SetAddressableLEDOutputPort(HalAddressableLEDHandle handle, HalDigitalHandle outputPort, out HalStatus status);

[AutomateStatusCheck(StatusCheckMethod = HalBase.StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_SetAddressableLEDSyncTime")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void SetAddressableLEDSyncTime(HalAddressableLEDHandle handle, int syncTimeMicroSeconds, out HalStatus status);

[AutomateStatusCheck(StatusCheckMethod = HalBase.StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_StartAddressableLEDOutput")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void StartAddressableLEDOutput(HalAddressableLEDHandle handle, out HalStatus status);

[AutomateStatusCheck(StatusCheckMethod = HalBase.StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_StopAddressableLEDOutput")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void StopAddressableLEDOutput(HalAddressableLEDHandle handle, out HalStatus status);

[LibraryImport("wpiHal", EntryPoint = "HAL_WriteAddressableLEDData")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void WriteAddressableLEDData(HalAddressableLEDHandle handle, ReadOnlySpan<AddressableLEDData> data, int length, out HalStatus status);
public static partial void WriteAddressableLEDData(HalAddressableLEDHandle handle, ReadOnlySpan<LedData> data, int length, out HalStatus status);

public static void WriteAddressableLEDData(HalAddressableLEDHandle handle, ReadOnlySpan<AddressableLEDData> data, out HalStatus status)
[AutomateStatusCheck(StatusCheckMethod = HalBase.StatusCheckCall)]
public static void WriteAddressableLEDData(HalAddressableLEDHandle handle, ReadOnlySpan<LedData> data, out HalStatus status)
{
WriteAddressableLEDData(handle, data, data.Length, out status);
}


}
11 changes: 9 additions & 2 deletions src/hal/Natives/HalAnalogAccumulator.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,49 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using WPIHal.Handles;
using WPIUtil;

namespace WPIHal.Natives;

public static partial class HalAnalogAccumulator
{
[AutomateStatusCheck(StatusCheckMethod = HalBase.StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_GetAccumulatorCount")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial long GetAccumulatorCount(HalAnalogInputHandle analogPortHandle, out HalStatus status);

[AutomateStatusCheck(StatusCheckMethod = HalBase.StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_GetAccumulatorOutput")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void GetAccumulatorOutput(HalAnalogInputHandle analogPortHandle, out long value, out long count, out HalStatus status);

[AutomateStatusCheck(StatusCheckMethod = HalBase.StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_GetAccumulatorValue")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial long GetAccumulatorValue(HalAnalogInputHandle analogPortHandle, out HalStatus status);

[AutomateStatusCheck(StatusCheckMethod = HalBase.StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_InitAccumulator")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void InitAccumulator(HalAnalogInputHandle analogPortHandle, out HalStatus status);

[AutomateStatusCheck(StatusCheckMethod = HalBase.StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_IsAccumulatorChannel")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial int IsAccumulatorChannel(HalAnalogInputHandle analogPortHandle, out HalStatus status);

[AutomateStatusCheck(StatusCheckMethod = HalBase.StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_ResetAccumulator")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void ResetAccumulator(HalAnalogInputHandle analogPortHandle, out HalStatus status);

[AutomateStatusCheck(StatusCheckMethod = HalBase.StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_SetAccumulatorCenter")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void SetAccumulatorCenter(HalAnalogInputHandle analogPortHandle, int center, out HalStatus status);

[AutomateStatusCheck(StatusCheckMethod = HalBase.StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_SetAccumulatorDeadband")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void SetAccumulatorDeadband(HalAnalogInputHandle analogPortHandle, int deadband, out HalStatus status);


}
14 changes: 12 additions & 2 deletions src/hal/Natives/HalAnalogGyro.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using WPIHal.Handles;
using WPIUtil;

namespace WPIHal.Natives;

public static partial class HalAnalogGyro
{
[AutomateStatusCheck(StatusCheckMethod = HalBase.StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_CalibrateAnalogGyro")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void CalibrateAnalogGyro(HalGyroHandle handle, out HalStatus status);
Expand All @@ -14,45 +16,53 @@ public static partial class HalAnalogGyro
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void FreeAnalogGyro(HalGyroHandle handle);

[AutomateStatusCheck(StatusCheckMethod = HalBase.StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_GetAnalogGyroAngle")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial double GetAnalogGyroAngle(HalGyroHandle handle, out HalStatus status);

[AutomateStatusCheck(StatusCheckMethod = HalBase.StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_GetAnalogGyroCenter")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial int GetAnalogGyroCenter(HalGyroHandle handle, out HalStatus status);

[AutomateStatusCheck(StatusCheckMethod = HalBase.StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_GetAnalogGyroOffset")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial double GetAnalogGyroOffset(HalGyroHandle handle, out HalStatus status);

[AutomateStatusCheck(StatusCheckMethod = HalBase.StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_GetAnalogGyroRate")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial double GetAnalogGyroRate(HalGyroHandle handle, out HalStatus status);

[AutomateStatusCheck(StatusCheckMethod = HalBase.StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_InitializeAnalogGyro", StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial HalGyroHandle InitializeAnalogGyro(HalAnalogInputHandle handle, string allocationLocation, out HalStatus status);

[AutomateStatusCheck(StatusCheckMethod = HalBase.StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_ResetAnalogGyro")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void ResetAnalogGyro(HalGyroHandle handle, out HalStatus status);

[AutomateStatusCheck(StatusCheckMethod = HalBase.StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_SetAnalogGyroDeadband")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void SetAnalogGyroDeadband(HalGyroHandle handle, double volts, out HalStatus status);

[AutomateStatusCheck(StatusCheckMethod = HalBase.StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_SetAnalogGyroParameters")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void SetAnalogGyroParameters(HalGyroHandle handle, double voltsPerDegreePerSecond, double offset, int center, out HalStatus status);

[AutomateStatusCheck(StatusCheckMethod = HalBase.StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_SetAnalogGyroVoltsPerDegreePerSecond")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void SetAnalogGyroVoltsPerDegreePerSecond(HalGyroHandle handle, double voltsPerDegreePerSecond, out HalStatus status);

[AutomateStatusCheck(StatusCheckMethod = HalBase.StatusCheckCall)]
[LibraryImport("wpiHal", EntryPoint = "HAL_SetupAnalogGyro")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void SetupAnalogGyro(HalGyroHandle handle, out HalStatus status);


}
Loading

0 comments on commit 1cd5d5c

Please sign in to comment.