Skip to content

Commit

Permalink
Anime Matrix cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
seerge committed Feb 10, 2024
1 parent 103c65a commit 833b750
Showing 1 changed file with 52 additions and 94 deletions.
146 changes: 52 additions & 94 deletions app/AnimeMatrix/AnimeMatrixDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public enum AnimeType
}



public enum BrightnessMode : byte
{
Off = 0,
Expand All @@ -81,7 +80,6 @@ public enum BrightnessMode : byte
}



public class AnimeMatrixDevice : Device
{
int UpdatePageLength = 490;
Expand Down Expand Up @@ -139,6 +137,56 @@ public AnimeMatrixDevice() : base(0x0B05, 0x193B, 640)

}

public void WakeUp()
{
Set(Packet<AnimeMatrixPacket>(Encoding.ASCII.GetBytes("ASUS Tech.Inc.")));
}

public void SetBrightness(BrightnessMode mode)
{
Set(Packet<AnimeMatrixPacket>(0xC0, 0x04, (byte)mode));
}

public void SetDisplayState(bool enable)
{
Set(Packet<AnimeMatrixPacket>(0xC3, 0x01, enable ? (byte)0x00 : (byte)0x80));
}

public void SetBuiltInAnimation(bool enable)
{
Set(Packet<AnimeMatrixPacket>(0xC4, 0x01, enable ? (byte)0x00 : (byte)0x80));
}

public void SetBuiltInAnimation(bool enable, BuiltInAnimation animation)
{
SetBuiltInAnimation(enable);
Set(Packet<AnimeMatrixPacket>(0xC5, animation.AsByte));
}

public void Present()
{

int page = 0;
int start, end;

while (page * UpdatePageLength < LedCount)
{
start = page * UpdatePageLength;
end = Math.Min(LedCount, (page + 1) * UpdatePageLength);

Set(Packet<AnimeMatrixPacket>(0xC0, 0x02)
.AppendData(BitConverter.GetBytes((ushort)(start + 1)))
.AppendData(BitConverter.GetBytes((ushort)(end - start)))
.AppendData(_displayBuffer[start..end])
);

page++;
}

Set(Packet<AnimeMatrixPacket>(0xC0, 0x03));
}


private void LoadMFont()
{
byte[] fontData = GHelper.Properties.Resources.MFont;
Expand All @@ -151,10 +199,6 @@ private void LoadMFont()
System.Runtime.InteropServices.Marshal.FreeCoTaskMem(fontPtr);
}

public byte[] GetBuffer()
{
return _displayBuffer;
}

public void PresentNextFrame()
{
Expand All @@ -175,12 +219,6 @@ public void AddFrame()
frames.Add(_displayBuffer.ToArray());
}

public void SendRaw(params byte[] data)
{
Set(Packet<AnimeMatrixPacket>(data));
}


public int Width()
{
switch (_model)
Expand Down Expand Up @@ -294,99 +332,19 @@ public void SetLedDiagonal(int x, int y, byte color, int deltaX = 0, int deltaY
}


public void WakeUp()
{
Set(Packet<AnimeMatrixPacket>(Encoding.ASCII.GetBytes("ASUS Tech.Inc.")));
}

public void SetLedLinear(int address, byte value)
{
if (!IsAddressableLed(address)) return;
_displayBuffer[address] = value;
}

public void SetLedLinearImmediate(int address, byte value)
{
if (!IsAddressableLed(address)) return;
_displayBuffer[address] = value;

Set(Packet<AnimeMatrixPacket>(0xC0, 0x02)
.AppendData(BitConverter.GetBytes((ushort)(address + 1)))
.AppendData(BitConverter.GetBytes((ushort)0x0001))
.AppendData(value)
);

Set(Packet<AnimeMatrixPacket>(0xC0, 0x03));
}



public void Clear(bool present = false)
{
for (var i = 0; i < _displayBuffer.Length; i++)
_displayBuffer[i] = 0;

if (present)
Present();
}

public void Present()
{

int page = 0;
int start, end;

while (page * UpdatePageLength < LedCount)
{
start = page * UpdatePageLength;
end = Math.Min(LedCount, (page + 1) * UpdatePageLength);

Set(Packet<AnimeMatrixPacket>(0xC0, 0x02)
.AppendData(BitConverter.GetBytes((ushort)(start + 1)))
.AppendData(BitConverter.GetBytes((ushort)(end - start)))
.AppendData(_displayBuffer[start..end])
);

page++;
}

Set(Packet<AnimeMatrixPacket>(0xC0, 0x03));
}

public void SetDisplayState(bool enable)
{
if (enable)
{
Set(Packet<AnimeMatrixPacket>(0xC3, 0x01)
.AppendData(0x00));
}
else
{
Set(Packet<AnimeMatrixPacket>(0xC3, 0x01)
.AppendData(0x80));
}
for (var i = 0; i < _displayBuffer.Length; i++) _displayBuffer[i] = 0;
if (present) Present();
}

public void SetBrightness(BrightnessMode mode)
{
Set(Packet<AnimeMatrixPacket>(0xC0, 0x04)
.AppendData((byte)mode)
);
}

public void SetBuiltInAnimation(bool enable)
{
var enabled = enable ? (byte)0x00 : (byte)0x80;
Set(Packet<AnimeMatrixPacket>(0xC4, 0x01, enabled));
}

public void SetBuiltInAnimation(bool enable, BuiltInAnimation animation)
{
SetBuiltInAnimation(enable);
Set(Packet<AnimeMatrixPacket>(0xC5, animation.AsByte));
}


private void SetBitmapDiagonal(Bitmap bmp, int deltaX = 0, int deltaY = 0, int contrast = 100)
{
for (int y = 0; y < bmp.Height; y++)
Expand Down

0 comments on commit 833b750

Please sign in to comment.