code generator that automatically switches endianness
This commit is contained in:
parent
e603c154b9
commit
5532c4f5a8
8 changed files with 291 additions and 34 deletions
|
@ -22,6 +22,10 @@
|
|||
<PackageReference Include="System.Text.Encoding.CodePages" Version="8.0.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1"/>
|
||||
|
||||
<ProjectReference Include="../EndiannessSourceGenerator/EndiannessSourceGenerator.csproj"
|
||||
OutputItemType="Analyzer"
|
||||
ReferenceOutputAssembly="false"/>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -14,7 +14,7 @@ internal sealed class DisplayConnection(IOptions<DisplayConfiguration> options)
|
|||
|
||||
public ValueTask SendClearAsync()
|
||||
{
|
||||
var header = new HeaderWindow { Command = DisplayCommand.Clear };
|
||||
var header = new HeaderWindow { Command = (ushort)DisplayCommand.Clear };
|
||||
|
||||
return SendAsync(header, Memory<byte>.Empty);
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ internal sealed class DisplayConnection(IOptions<DisplayConfiguration> options)
|
|||
{
|
||||
var header = new HeaderWindow
|
||||
{
|
||||
Command = DisplayCommand.Cp437Data,
|
||||
Command = (ushort)DisplayCommand.Cp437Data,
|
||||
Height = grid.Height,
|
||||
Width = grid.Width,
|
||||
PosX = x,
|
||||
|
@ -37,7 +37,7 @@ internal sealed class DisplayConnection(IOptions<DisplayConfiguration> options)
|
|||
{
|
||||
var header = new HeaderWindow
|
||||
{
|
||||
Command = DisplayCommand.CharBrightness,
|
||||
Command = (ushort)DisplayCommand.CharBrightness,
|
||||
PosX = x,
|
||||
PosY = y,
|
||||
Height = luma.Height,
|
||||
|
@ -49,7 +49,7 @@ internal sealed class DisplayConnection(IOptions<DisplayConfiguration> options)
|
|||
|
||||
public async ValueTask SendBrightnessAsync(byte brightness)
|
||||
{
|
||||
var header = new HeaderWindow { Command = DisplayCommand.Brightness };
|
||||
var header = new HeaderWindow { Command = (ushort)DisplayCommand.Brightness };
|
||||
|
||||
var payloadBuffer = _arrayPool.Rent(1);
|
||||
var payload = payloadBuffer.AsMemory(0, 1);
|
||||
|
@ -61,13 +61,13 @@ internal sealed class DisplayConnection(IOptions<DisplayConfiguration> options)
|
|||
|
||||
public ValueTask SendHardResetAsync()
|
||||
{
|
||||
var header = new HeaderWindow { Command = DisplayCommand.HardReset };
|
||||
var header = new HeaderWindow { Command = (ushort)DisplayCommand.HardReset };
|
||||
return SendAsync(header, Memory<byte>.Empty);
|
||||
}
|
||||
|
||||
public async ValueTask SendFadeOutAsync(byte loops)
|
||||
{
|
||||
var header = new HeaderWindow { Command = DisplayCommand.FadeOut };
|
||||
var header = new HeaderWindow { Command = (ushort)DisplayCommand.FadeOut };
|
||||
|
||||
var payloadBuffer = _arrayPool.Rent(1);
|
||||
var payload = payloadBuffer.AsMemory(0, 1);
|
||||
|
@ -81,8 +81,9 @@ internal sealed class DisplayConnection(IOptions<DisplayConfiguration> options)
|
|||
{
|
||||
var header = new HeaderWindow
|
||||
{
|
||||
Command = DisplayCommand.BitmapLinearWin,
|
||||
PosX = x, PosY = y,
|
||||
Command = (ushort)DisplayCommand.BitmapLinearWin,
|
||||
PosX = x,
|
||||
PosY = y,
|
||||
Width = (ushort)(pixels.Width / 8),
|
||||
Height = pixels.Height
|
||||
};
|
||||
|
@ -113,7 +114,6 @@ internal sealed class DisplayConnection(IOptions<DisplayConfiguration> options)
|
|||
var buffer = _arrayPool.Rent(messageSize);
|
||||
var message = buffer.AsMemory(0, messageSize);
|
||||
|
||||
header.ChangeToNetworkOrder();
|
||||
MemoryMarshal.Write(message.Span, header);
|
||||
payload.CopyTo(message[headerSize..]);
|
||||
|
||||
|
|
|
@ -1,17 +1,19 @@
|
|||
using System.Runtime.InteropServices;
|
||||
using EndiannessSourceGenerator;
|
||||
|
||||
namespace DisplayCommands.Internals;
|
||||
|
||||
[StructEndianness(IsLittleEndian = false)]
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 16, Size = 10)]
|
||||
internal struct HeaderBitmap
|
||||
internal partial struct HeaderBitmap
|
||||
{
|
||||
public DisplayCommand Command;
|
||||
private ushort _command;
|
||||
|
||||
public ushort Offset;
|
||||
private ushort _offset;
|
||||
|
||||
public ushort Length;
|
||||
private ushort _length;
|
||||
|
||||
public DisplaySubCommand SubCommand;
|
||||
private ushort _subCommand;
|
||||
|
||||
public ushort Reserved;
|
||||
}
|
||||
private ushort _reserved;
|
||||
}
|
||||
|
|
|
@ -1,29 +1,19 @@
|
|||
using System.Buffers.Binary;
|
||||
using System.Runtime.InteropServices;
|
||||
using EndiannessSourceGenerator;
|
||||
|
||||
namespace DisplayCommands.Internals;
|
||||
|
||||
[StructEndianness(IsLittleEndian = false)]
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 16, Size = 10)]
|
||||
internal struct HeaderWindow
|
||||
internal partial struct HeaderWindow
|
||||
{
|
||||
public DisplayCommand Command;
|
||||
private ushort _command;
|
||||
|
||||
public ushort PosX;
|
||||
private ushort _posX;
|
||||
|
||||
public ushort PosY;
|
||||
private ushort _posY;
|
||||
|
||||
public ushort Width;
|
||||
private ushort _width;
|
||||
|
||||
public ushort Height;
|
||||
|
||||
public void ChangeToNetworkOrder()
|
||||
{
|
||||
if (!BitConverter.IsLittleEndian)
|
||||
return;
|
||||
Command = (DisplayCommand)BinaryPrimitives.ReverseEndianness((ushort)Command);
|
||||
PosX = BinaryPrimitives.ReverseEndianness(PosX);
|
||||
PosY = BinaryPrimitives.ReverseEndianness(PosY);
|
||||
Width = BinaryPrimitives.ReverseEndianness(Width);
|
||||
Height = BinaryPrimitives.ReverseEndianness(Height);
|
||||
}
|
||||
private ushort _height;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue