Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DMX support, "Advanced" tab, Stagekit toggle #670

Merged
merged 8 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Assets/Plugins/Haukcode.sACN.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

111 changes: 111 additions & 0 deletions Assets/Plugins/Haukcode.sACN/BigEndianBinaryWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System;
using System.Linq;
using System.Text;

namespace Haukcode.sACN
{
internal class BigEndianBinaryWriter
{
private int writePosition = 0;
private readonly Memory<byte> buffer;

public BigEndianBinaryWriter(Memory<byte> buffer)
{
this.buffer = buffer;
}

public int WrittenBytes => writePosition;

public Memory<byte> Memory => buffer.Slice(writePosition);

public void WriteByte(byte value)
{
var span = buffer.Span;

span[writePosition++] = (byte)value;
}

public void WriteShort(short value)
{
var span = buffer.Span;

span[writePosition++] = (byte)(value >> 8);
span[writePosition++] = (byte)value;
}

public void WriteUShort(ushort value)
{
var span = buffer.Span;

span[writePosition++] = (byte)(value >> 8);
span[writePosition++] = (byte)value;
}

public void WriteInt32(int value)
{
var span = buffer.Span;

span[writePosition++] = (byte)(value >> 24);
span[writePosition++] = (byte)(value >> 16);
span[writePosition++] = (byte)(value >> 8);
span[writePosition++] = (byte)value;
}

public void WriteBytes(byte[] bytes)
{
bytes.CopyTo(buffer[writePosition..].Span);

writePosition += bytes.Length;
}

public void WriteBytes(ReadOnlyMemory<byte> bytes)
{
bytes.Span.CopyTo(buffer[writePosition..].Span);

writePosition += bytes.Length;
}

public void WriteString(string value, int length)
{
//FIXME
WriteBytes(Encoding.UTF8.GetBytes(value));
WriteBytes(Enumerable.Repeat((byte)0, length - value.Length).ToArray());
}

private byte[] GuidToByteArray(Guid input)
{
var bytes = input.ToByteArray();

return new byte[] {
bytes[3],
bytes[2],
bytes[1],
bytes[0],

bytes[5],
bytes[4],

bytes[7],
bytes[6],

bytes[8],
bytes[9],

bytes[10],
bytes[11],
bytes[12],
bytes[13],
bytes[14],
bytes[15]
};
}

public void WriteGuid(Guid value)
{
// Fixme
var bytes = GuidToByteArray(value);

WriteBytes(bytes);
}
}
}
11 changes: 11 additions & 0 deletions Assets/Plugins/Haukcode.sACN/BigEndianBinaryWriter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/Plugins/Haukcode.sACN/Model.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions Assets/Plugins/Haukcode.sACN/Model/DMPLayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;

namespace Haukcode.sACN.Model
{
public class DMPLayer
{
public const byte DMP_VECTOR = 2;
public const byte ADDRESS_TYPE_AND_DATA_TYPE = 0xA1;
public const short FIRST_PROPERTY_ADDRESS = 0x00;
public const short ADDRESS_INCREMENT = 1;

public byte StartCode { get; set; }

public short Length { get { return (short)(11 + Data.Length); } }

public ReadOnlyMemory<byte> Data { get; set; }

public DMPLayer(ReadOnlyMemory<byte> data, byte startCode = 0x00)
{
Data = data;
StartCode = startCode;
}

public int WriteToBuffer(Memory<byte> buffer)
{
var writer = new BigEndianBinaryWriter(buffer);

ushort flagsAndDMPLength = (ushort)(SACNDataPacket.FLAGS | (ushort)Length);

writer.WriteUShort(flagsAndDMPLength);
writer.WriteByte(DMP_VECTOR);
writer.WriteByte(ADDRESS_TYPE_AND_DATA_TYPE);
writer.WriteShort(FIRST_PROPERTY_ADDRESS);
writer.WriteShort(ADDRESS_INCREMENT);
writer.WriteShort((short)(Data.Length + 1));
writer.WriteByte(StartCode);
writer.WriteBytes(Data);

return writer.WrittenBytes;
}
}
}
11 changes: 11 additions & 0 deletions Assets/Plugins/Haukcode.sACN/Model/DMPLayer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions Assets/Plugins/Haukcode.sACN/Model/DataFramingLayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;


namespace Haukcode.sACN.Model
{
public class DataFramingLayer : FramingLayer
{
public const int SourceNameLength = 64;

public DMPLayer DMPLayer { get; set; }

public override ushort Length { get { return (ushort)(13 + SourceNameLength + DMPLayer.Length); } }

public string SourceName { get; set; }

public ushort UniverseId { get; set; }

public byte Priority { get; set; }

public ushort SyncAddress { get; set; }

public FramingOptions Options { get; set; }

public override int RootVector => RootLayer.VECTOR_ROOT_E131_DATA;

public DataFramingLayer(string sourceName, ushort universeId, byte sequenceId, ReadOnlyMemory<byte> data, byte priority, ushort syncAddress = 0, byte startCode = 0)
: base(sequenceId)
{
SourceName = sourceName;
UniverseId = universeId;
DMPLayer = new DMPLayer(data, startCode);
Priority = priority;
SyncAddress = syncAddress;
Options = new FramingOptions();
}

public DataFramingLayer()
{
}

public override int WriteToBuffer(Memory<byte> buffer)
{
var writer = new BigEndianBinaryWriter(buffer);

ushort flagsAndFramingLength = (ushort)(SACNPacket.FLAGS | Length);
writer.WriteUShort(flagsAndFramingLength);
writer.WriteInt32(VECTOR_E131_DATA_PACKET);
writer.WriteString(SourceName, 64);
writer.WriteByte(Priority);
writer.WriteUShort(SyncAddress);
writer.WriteByte(SequenceId);
writer.WriteByte(Options.ToByte());
writer.WriteUShort(UniverseId);

return writer.WrittenBytes + DMPLayer.WriteToBuffer(writer.Memory);
}
}
}
11 changes: 11 additions & 0 deletions Assets/Plugins/Haukcode.sACN/Model/DataFramingLayer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions Assets/Plugins/Haukcode.sACN/Model/FramingLayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;

namespace Haukcode.sACN.Model
{
public abstract class FramingLayer
{
public const int VECTOR_E131_DATA_PACKET = 0x00000002;
public const int VECTOR_E131_EXTENDED_SYNCHRONIZATION = 0x00000001;

public abstract ushort Length { get; }

public byte SequenceId { get; set; }

public abstract int RootVector { get; }

public FramingLayer(byte sequenceId)
{
SequenceId = sequenceId;
}

public FramingLayer()
{
}

public abstract int WriteToBuffer(Memory<byte> buffer);
}
}
11 changes: 11 additions & 0 deletions Assets/Plugins/Haukcode.sACN/Model/FramingLayer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions Assets/Plugins/Haukcode.sACN/Model/FramingOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

namespace Haukcode.sACN.Model
{
public class FramingOptions
{
public const byte FORCE_SYNCHRONIZATION = 0b0000_1000;
public const byte STREAM_TERMINATED = 0b0000_0100;
public const byte PREVIEW_DATA = 0b0000_0010;

public bool PreviewData { get; set; }

public bool StreamTerminated { get; set; }

public bool ForceSynchronization { get; set; }

public FramingOptions()
{
}

public static FramingOptions Parse(byte optionsByte)
{
var options = new FramingOptions();

if ((optionsByte & FORCE_SYNCHRONIZATION) != 0)
{
options.ForceSynchronization = true;
}
if ((optionsByte & STREAM_TERMINATED) != 0)
{
options.StreamTerminated = true;
}
if ((optionsByte & PREVIEW_DATA) != 0)
{
options.PreviewData = true;
}

return options;
}

public byte ToByte()
{
byte returnVal = 0;

if (PreviewData)
{
returnVal = (byte)(returnVal | PREVIEW_DATA);
}
if (StreamTerminated)
{
returnVal = (byte)(returnVal | STREAM_TERMINATED);
}
if (ForceSynchronization)
{
returnVal = (byte)(returnVal | FORCE_SYNCHRONIZATION);
}

return returnVal;
}
}
}
11 changes: 11 additions & 0 deletions Assets/Plugins/Haukcode.sACN/Model/FramingOptions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading