From 6a44c42cbfd3ca50fc01e5ce7d723426df871c48 Mon Sep 17 00:00:00 2001 From: Serge <5920850+seerge@users.noreply.github.com> Date: Sat, 10 Feb 2024 22:04:45 +0100 Subject: [PATCH] Slash Lightning --- app/AnimeMatrix/AniMatrixControl.cs | 132 +++++++++++++----- .../Platform/WindowsUsbProvider.cs | 3 +- app/AnimeMatrix/SlashDevice.cs | 62 ++++++++ app/Matrix.cs | 4 +- app/Program.cs | 2 +- app/Settings.cs | 17 ++- 6 files changed, 175 insertions(+), 45 deletions(-) create mode 100644 app/AnimeMatrix/SlashDevice.cs diff --git a/app/AnimeMatrix/AniMatrixControl.cs b/app/AnimeMatrix/AniMatrixControl.cs index f433a8bc..0e0ee65d 100644 --- a/app/AnimeMatrix/AniMatrixControl.cs +++ b/app/AnimeMatrix/AniMatrixControl.cs @@ -15,14 +15,16 @@ namespace GHelper.AnimeMatrix SettingsForm settings; System.Timers.Timer matrixTimer = default!; - public AnimeMatrixDevice? device; + + public AnimeMatrixDevice? deviceMatrix; + public SlashDevice? deviceSlash; double[]? AudioValues; WasapiCapture? AudioDevice; string? AudioDeviceId; private MMDeviceEnumerator? AudioDeviceEnum; - public bool IsValid => device != null; + public bool IsValid => deviceMatrix != null || deviceSlash != null; private long lastPresent; private List maxes = new List(); @@ -33,31 +35,87 @@ namespace GHelper.AnimeMatrix try { - device = new AnimeMatrixDevice(); - Task.Run(device.WakeUp); - matrixTimer = new System.Timers.Timer(100); - matrixTimer.Elapsed += MatrixTimer_Elapsed; + deviceMatrix = new AnimeMatrixDevice(); } catch { - device = null; + deviceMatrix = null; } + if (AppConfig.ContainsModel("GA403")) + { + try + { + deviceSlash = new SlashDevice(); + } + catch + { + deviceSlash = null; + } + } + + if (IsValid) + { + matrixTimer = new System.Timers.Timer(100); + matrixTimer.Elapsed += MatrixTimer_Elapsed; + } + + } + + public void SetDevice(bool wakeUp = false) + { + if (deviceMatrix is not null) SetMatrix(wakeUp); + if (deviceSlash is not null) SetSlash(wakeUp); + } + + + public void SetSlash(bool wakeUp = false) + { + if (deviceSlash is null) return; + + int brightness = AppConfig.Get("matrix_brightness", 0); + int running = AppConfig.Get("matrix_running", 0); + bool auto = AppConfig.Is("matrix_auto"); + + Task.Run(() => + { + try + { + deviceSlash.SetProvider(); + } + catch (Exception ex) + { + Logger.WriteLine(ex.Message); + return; + } + + if (wakeUp) deviceSlash.WakeUp(); + + if (brightness == 0 || (auto && SystemInformation.PowerStatus.PowerLineStatus != PowerLineStatus.Online)) + { + deviceSlash.Init(); + deviceSlash.SetOptions(false); + } else + { + deviceSlash.Init(); + deviceSlash.SetMode((SlashMode)running); + deviceSlash.SetOptions(false, (byte)(brightness*85.333), 0); + deviceSlash.Save(); + } + + + }); } public void SetMatrix(bool wakeUp = false) { - if (!IsValid) return; - - int brightness = AppConfig.Get("matrix_brightness"); - int running = AppConfig.Get("matrix_running"); + if (deviceMatrix is null) return; + int brightness = AppConfig.Get("matrix_brightness", 0); + int running = AppConfig.Get("matrix_running", 0); bool auto = AppConfig.Is("matrix_auto"); - if (brightness < 0) brightness = 0; - if (running < 0) running = 0; - BuiltInAnimation animation = new BuiltInAnimation( (BuiltInAnimation.Running)running, BuiltInAnimation.Sleeping.Starfield, @@ -72,7 +130,7 @@ namespace GHelper.AnimeMatrix { try { - device.SetProvider(); + deviceMatrix.SetProvider(); } catch (Exception ex) { @@ -80,18 +138,18 @@ namespace GHelper.AnimeMatrix return; } - if (wakeUp) device.WakeUp(); + if (wakeUp) deviceMatrix.WakeUp(); if (brightness == 0 || (auto && SystemInformation.PowerStatus.PowerLineStatus != PowerLineStatus.Online)) { - device.SetDisplayState(false); - device.SetDisplayState(false); // some devices are dumb + deviceMatrix.SetDisplayState(false); + deviceMatrix.SetDisplayState(false); // some devices are dumb Logger.WriteLine("Matrix Off"); } else { - device.SetDisplayState(true); - device.SetBrightness((BrightnessMode)brightness); + deviceMatrix.SetDisplayState(true); + deviceMatrix.SetBrightness((BrightnessMode)brightness); switch (running) { @@ -105,7 +163,7 @@ namespace GHelper.AnimeMatrix SetMatrixAudio(); break; default: - device.SetBuiltInAnimation(true, animation); + deviceMatrix.SetBuiltInAnimation(true, animation); Logger.WriteLine("Matrix builtin " + animation.AsByte); break; } @@ -134,10 +192,10 @@ namespace GHelper.AnimeMatrix switch (AppConfig.Get("matrix_running")) { case 2: - device.PresentNextFrame(); + deviceMatrix.PresentNextFrame(); break; case 3: - device.PresentClock(); + deviceMatrix.PresentClock(); break; } @@ -146,7 +204,7 @@ namespace GHelper.AnimeMatrix public void SetMatrixClock() { - device.SetBuiltInAnimation(false); + deviceMatrix.SetBuiltInAnimation(false); StartMatrixTimer(1000); Logger.WriteLine("Matrix Clock"); } @@ -179,7 +237,7 @@ namespace GHelper.AnimeMatrix { if (!IsValid) return; - device.SetBuiltInAnimation(false); + deviceMatrix.SetBuiltInAnimation(false); StopMatrixTimer(); StopMatrixAudio(); @@ -251,8 +309,8 @@ namespace GHelper.AnimeMatrix for (int x = 0; x < 2 - (y % 2); x++) { //color = (byte)(Math.Min(1,(h - y - 2)*2) * 255); - device.SetLedPlanar(x + dx, dy + y, (byte)(h * 255 / 30)); - device.SetLedPlanar(x + dx, dy - y, 255); + deviceMatrix.SetLedPlanar(x + dx, dy + y, (byte)(h * 255 / 30)); + deviceMatrix.SetLedPlanar(x + dx, dy - y, 255); } } @@ -262,7 +320,7 @@ namespace GHelper.AnimeMatrix if (Math.Abs(DateTimeOffset.Now.ToUnixTimeMilliseconds() - lastPresent) < 70) return; lastPresent = DateTimeOffset.Now.ToUnixTimeMilliseconds(); - device.Clear(); + deviceMatrix.Clear(); int size = 20; double[] bars = new double[size]; @@ -280,7 +338,7 @@ namespace GHelper.AnimeMatrix for (int i = 0; i < size; i++) DrawBar(20 - i, bars[i] * 20 / maxAverage); - device.Present(); + deviceMatrix.Present(); } @@ -351,8 +409,8 @@ namespace GHelper.AnimeMatrix protected void ProcessPicture(Image image) { - device.SetBuiltInAnimation(false); - device.ClearFrames(); + deviceMatrix.SetBuiltInAnimation(false); + deviceMatrix.ClearFrames(); int matrixX = AppConfig.Get("matrix_x", 0); int matrixY = AppConfig.Get("matrix_y", 0); @@ -380,11 +438,11 @@ namespace GHelper.AnimeMatrix image.SelectActiveFrame(dimension, i); if (rotation == MatrixRotation.Planar) - device.GenerateFrame(image, matrixZoom, matrixX, matrixY, matrixQuality, matrixContrast); + deviceMatrix.GenerateFrame(image, matrixZoom, matrixX, matrixY, matrixQuality, matrixContrast); else - device.GenerateFrameDiagonal(image, matrixZoom, matrixX, matrixY, matrixQuality, matrixContrast); + deviceMatrix.GenerateFrameDiagonal(image, matrixZoom, matrixX, matrixY, matrixQuality, matrixContrast); - device.AddFrame(); + deviceMatrix.AddFrame(); } @@ -397,11 +455,11 @@ namespace GHelper.AnimeMatrix else { if (rotation == MatrixRotation.Planar) - device.GenerateFrame(image, matrixZoom, matrixX, matrixY, matrixQuality, matrixContrast); + deviceMatrix.GenerateFrame(image, matrixZoom, matrixX, matrixY, matrixQuality, matrixContrast); else - device.GenerateFrameDiagonal(image, matrixZoom, matrixX, matrixY, matrixQuality, matrixContrast); + deviceMatrix.GenerateFrameDiagonal(image, matrixZoom, matrixX, matrixY, matrixQuality, matrixContrast); - device.Present(); + deviceMatrix.Present(); } } diff --git a/app/AnimeMatrix/Communication/Platform/WindowsUsbProvider.cs b/app/AnimeMatrix/Communication/Platform/WindowsUsbProvider.cs index 2db6e1d8..0a43b96c 100644 --- a/app/AnimeMatrix/Communication/Platform/WindowsUsbProvider.cs +++ b/app/AnimeMatrix/Communication/Platform/WindowsUsbProvider.cs @@ -36,9 +36,10 @@ namespace GHelper.AnimeMatrix.Communication.Platform { HidDevice = DeviceList.Local .GetHidDevices(vendorId, productId) - .First(x => x.GetMaxFeatureReportLength() == maxFeatureReportLength); + .First(x => x.GetMaxFeatureReportLength() >= maxFeatureReportLength); Logger.WriteLine("Matrix Device: " + HidDevice.DevicePath); + Logger.WriteLine("Matrix Features: " + HidDevice.GetMaxFeatureReportLength()); } catch diff --git a/app/AnimeMatrix/SlashDevice.cs b/app/AnimeMatrix/SlashDevice.cs new file mode 100644 index 00000000..f0f7e207 --- /dev/null +++ b/app/AnimeMatrix/SlashDevice.cs @@ -0,0 +1,62 @@ +using GHelper.AnimeMatrix.Communication; +using System.Text; + +namespace GHelper.AnimeMatrix +{ + public enum SlashMode + { + Transmission, + Bitstream + } + + internal class SlashPacket : Packet + { + public SlashPacket(byte[] command) : base(0x5E, 128, command) + { + } + } + + public class SlashDevice : Device + { + public SlashDevice() : base(0x0B05, 0x193B, 640) // 128 + { + } + + public void WakeUp() + { + Set(Packet(Encoding.ASCII.GetBytes("ASUS Tech.Inc."))); + } + + public void Init() + { + Set(Packet(0xD7, 0x00, 0x00, 0x01, 0xAC)); + Set(Packet(0xD2, 0x02, 0x01, 0x08, 0xAB)); + } + + public void Save() + { + Set(Packet(0xD4, 0x00, 0x00, 0x01, 0xAB)); + } + + public void SetMode(SlashMode mode) + { + Set(Packet(0xD2, 0x03, 0x00, 0x0C)); + Set(Packet(0xD3, 0x04, 0x00, 0x0C, 0x01, (mode == SlashMode.Bitstream) ? (byte)0x1D : (byte)0x1A, 0x02, 0x19, 0x03, 0x13, 0x04, 0x11, 0x05, 0x12, 0x06, 0x13)); + } + + public void SetOptions(bool status, byte brightness = 0xFF, byte interval = 0x00) + { + Set(Packet(0xD3, 0x03, 0x01, 0x08, 0xAB, 0xFF, 0x01, status ? (byte)0x01 : (byte)0x00, 0x06, brightness, 0xFF, interval)); + Save(); + Save(); + } + + public void Set(Packet packet) + { + _usbProvider?.Set(packet.Data); + Logger.WriteLine("Slash:" + BitConverter.ToString(packet.Data)); + } + + + } +} \ No newline at end of file diff --git a/app/Matrix.cs b/app/Matrix.cs index c87d61cc..78c2a4a5 100644 --- a/app/Matrix.cs +++ b/app/Matrix.cs @@ -62,8 +62,8 @@ namespace GHelper comboRotation.SelectedValueChanged += ComboRotation_SelectedValueChanged; ; - uiScale = panelPicture.Width / matrixControl.device.MaxColumns / 3; - panelPicture.Height = (int)(matrixControl.device.MaxRows * uiScale); + uiScale = panelPicture.Width / matrixControl.deviceMatrix.MaxColumns / 3; + panelPicture.Height = (int)(matrixControl.deviceMatrix.MaxRows * uiScale); } diff --git a/app/Program.cs b/app/Program.cs index ecf1d2e7..7cd18809 100644 --- a/app/Program.cs +++ b/app/Program.cs @@ -229,7 +229,7 @@ namespace GHelper BatteryControl.AutoBattery(init); - settingsForm.matrixControl.SetMatrix(true); + settingsForm.matrixControl.SetDevice(true); if (AppConfig.IsAlly()) { diff --git a/app/Settings.cs b/app/Settings.cs index bbaec21c..650ac2b7 100644 --- a/app/Settings.cs +++ b/app/Settings.cs @@ -686,7 +686,7 @@ namespace GHelper private void CheckMatrix_CheckedChanged(object? sender, EventArgs e) { AppConfig.Set("matrix_auto", checkMatrix.Checked ? 1 : 0); - matrixControl.SetMatrix(); + matrixControl.SetDevice(); } @@ -724,14 +724,14 @@ namespace GHelper private void ComboMatrixRunning_SelectedValueChanged(object? sender, EventArgs e) { AppConfig.Set("matrix_running", comboMatrixRunning.SelectedIndex); - matrixControl.SetMatrix(); + matrixControl.SetDevice(); } private void ComboMatrix_SelectedValueChanged(object? sender, EventArgs e) { AppConfig.Set("matrix_brightness", comboMatrix.SelectedIndex); - matrixControl.SetMatrix(); + matrixControl.SetDevice(); } @@ -896,6 +896,15 @@ namespace GHelper return; } + if (matrixControl.deviceSlash is not null) + { + labelMatrix.Text = "Slash Lightning"; + comboMatrixRunning.Items.Clear(); + comboMatrixRunning.Items.Add("Transmission"); + comboMatrixRunning.Items.Add("Bitstream"); + buttonMatrix.Visible = false; + } + comboMatrix.SelectedIndex = Math.Min(AppConfig.Get("matrix_brightness", 0), comboMatrix.Items.Count - 1); comboMatrixRunning.SelectedIndex = Math.Min(AppConfig.Get("matrix_running", 0), comboMatrixRunning.Items.Count - 1); @@ -909,7 +918,7 @@ namespace GHelper { comboMatrix.SelectedIndex = Math.Min(Math.Max(0, comboMatrix.SelectedIndex + delta), comboMatrix.Items.Count - 1); AppConfig.Set("matrix_brightness", comboMatrix.SelectedIndex); - matrixControl.SetMatrix(); + matrixControl.SetDevice(); Program.toast.RunToast(comboMatrix.GetItemText(comboMatrix.SelectedItem), delta > 0 ? ToastIcon.BacklightUp : ToastIcon.BacklightDown); }