From 833b7502275686b1efa6bab3eb7ba6120a8839c8 Mon Sep 17 00:00:00 2001 From: Serge <5920850+seerge@users.noreply.github.com> Date: Sat, 10 Feb 2024 17:19:04 +0100 Subject: [PATCH] Anime Matrix cleanup --- app/AnimeMatrix/AnimeMatrixDevice.cs | 146 ++++++++++----------------- 1 file changed, 52 insertions(+), 94 deletions(-) diff --git a/app/AnimeMatrix/AnimeMatrixDevice.cs b/app/AnimeMatrix/AnimeMatrixDevice.cs index 3b4c1740..5c35b4d2 100644 --- a/app/AnimeMatrix/AnimeMatrixDevice.cs +++ b/app/AnimeMatrix/AnimeMatrixDevice.cs @@ -71,7 +71,6 @@ namespace Starlight.AnimeMatrix } - public enum BrightnessMode : byte { Off = 0, @@ -81,7 +80,6 @@ namespace Starlight.AnimeMatrix } - public class AnimeMatrixDevice : Device { int UpdatePageLength = 490; @@ -139,6 +137,56 @@ namespace Starlight.AnimeMatrix } + public void WakeUp() + { + Set(Packet(Encoding.ASCII.GetBytes("ASUS Tech.Inc."))); + } + + public void SetBrightness(BrightnessMode mode) + { + Set(Packet(0xC0, 0x04, (byte)mode)); + } + + public void SetDisplayState(bool enable) + { + Set(Packet(0xC3, 0x01, enable ? (byte)0x00 : (byte)0x80)); + } + + public void SetBuiltInAnimation(bool enable) + { + Set(Packet(0xC4, 0x01, enable ? (byte)0x00 : (byte)0x80)); + } + + public void SetBuiltInAnimation(bool enable, BuiltInAnimation animation) + { + SetBuiltInAnimation(enable); + Set(Packet(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(0xC0, 0x02) + .AppendData(BitConverter.GetBytes((ushort)(start + 1))) + .AppendData(BitConverter.GetBytes((ushort)(end - start))) + .AppendData(_displayBuffer[start..end]) + ); + + page++; + } + + Set(Packet(0xC0, 0x03)); + } + + private void LoadMFont() { byte[] fontData = GHelper.Properties.Resources.MFont; @@ -151,10 +199,6 @@ namespace Starlight.AnimeMatrix System.Runtime.InteropServices.Marshal.FreeCoTaskMem(fontPtr); } - public byte[] GetBuffer() - { - return _displayBuffer; - } public void PresentNextFrame() { @@ -175,12 +219,6 @@ namespace Starlight.AnimeMatrix frames.Add(_displayBuffer.ToArray()); } - public void SendRaw(params byte[] data) - { - Set(Packet(data)); - } - - public int Width() { switch (_model) @@ -294,99 +332,19 @@ namespace Starlight.AnimeMatrix } - public void WakeUp() - { - Set(Packet(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(0xC0, 0x02) - .AppendData(BitConverter.GetBytes((ushort)(address + 1))) - .AppendData(BitConverter.GetBytes((ushort)0x0001)) - .AppendData(value) - ); - - Set(Packet(0xC0, 0x03)); - } - - public void Clear(bool present = false) { - for (var i = 0; i < _displayBuffer.Length; i++) - _displayBuffer[i] = 0; - - if (present) - Present(); + 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(0xC0, 0x02) - .AppendData(BitConverter.GetBytes((ushort)(start + 1))) - .AppendData(BitConverter.GetBytes((ushort)(end - start))) - .AppendData(_displayBuffer[start..end]) - ); - - page++; - } - - Set(Packet(0xC0, 0x03)); - } - - public void SetDisplayState(bool enable) - { - if (enable) - { - Set(Packet(0xC3, 0x01) - .AppendData(0x00)); - } - else - { - Set(Packet(0xC3, 0x01) - .AppendData(0x80)); - } - } - - public void SetBrightness(BrightnessMode mode) - { - Set(Packet(0xC0, 0x04) - .AppendData((byte)mode) - ); - } - - public void SetBuiltInAnimation(bool enable) - { - var enabled = enable ? (byte)0x00 : (byte)0x80; - Set(Packet(0xC4, 0x01, enabled)); - } - - public void SetBuiltInAnimation(bool enable, BuiltInAnimation animation) - { - SetBuiltInAnimation(enable); - Set(Packet(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++)