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

Slash lightning #2058

Merged
merged 10 commits into from
Feb 12, 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
158 changes: 110 additions & 48 deletions app/AnimeMatrix/AniMatrixControl.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using NAudio.CoreAudioApi;
using NAudio.Wave;
using Starlight.AnimeMatrix;
using System.Diagnostics;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
Expand All @@ -15,14 +14,17 @@ public class AniMatrixControl : NAudio.CoreAudioApi.Interfaces.IMMNotificationCl
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;
public bool IsSlash => deviceSlash != null;

private long lastPresent;
private List<double> maxes = new List<double>();
Expand All @@ -33,37 +35,81 @@ public AniMatrixControl(SettingsForm settingsForm)

try
{
device = new AnimeMatrixDevice();
Task.Run(device.WakeUp);
if (AppConfig.IsSlash())
deviceSlash = new SlashDevice();
else
deviceMatrix = new AnimeMatrixDevice();

matrixTimer = new System.Timers.Timer(100);
matrixTimer.Elapsed += MatrixTimer_Elapsed;
}
catch
catch (Exception ex)
{
device = null;
Logger.WriteLine(ex.Message);
}

}

public void SetMatrix(bool wakeUp = false)
public void SetDevice(bool wakeUp = false)
{
if (deviceMatrix is not null) SetMatrix(wakeUp);
if (deviceSlash is not null) SetSlash(wakeUp);
}

if (!IsValid) return;

int brightness = AppConfig.Get("matrix_brightness");
int running = AppConfig.Get("matrix_running");
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);
int inteval = AppConfig.Get("matrix_interval", 0);

bool auto = AppConfig.Is("matrix_auto");

if (brightness < 0) brightness = 0;
if (running < 0) running = 0;
Task.Run(() =>
{
try
{
deviceSlash.SetProvider();
}
catch (Exception ex)
{
Logger.WriteLine(ex.Message);
return;
}

BuiltInAnimation animation = new BuiltInAnimation(
(BuiltInAnimation.Running)running,
BuiltInAnimation.Sleeping.Starfield,
BuiltInAnimation.Shutdown.SeeYa,
BuiltInAnimation.Startup.StaticEmergence
);
if (wakeUp) deviceSlash.WakeUp();

if (brightness == 0 || (auto && SystemInformation.PowerStatus.PowerLineStatus != PowerLineStatus.Online))
{
deviceSlash.Init();
deviceSlash.SetOptions(false, 0, 0);
}
else
{
deviceSlash.Init();
deviceSlash.SetMode((SlashMode)running);
deviceSlash.SetOptions(true, brightness, inteval);
deviceSlash.Save();
}
});
}

public void SetBatteryAuto()
{
if (deviceSlash is not null) deviceSlash.SetBatterySaver(AppConfig.Is("matrix_auto"));
if (deviceMatrix is not null) SetMatrix();
}

public void SetMatrix(bool wakeUp = false)
{

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");

StopMatrixTimer();
StopMatrixAudio();
Expand All @@ -72,26 +118,26 @@ public void SetMatrix(bool wakeUp = false)
{
try
{
device.SetProvider();
deviceMatrix.SetProvider();
}
catch (Exception ex)
{
Logger.WriteLine(ex.Message);
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)
{
Expand All @@ -105,8 +151,7 @@ public void SetMatrix(bool wakeUp = false)
SetMatrixAudio();
break;
default:
device.SetBuiltInAnimation(true, animation);
Logger.WriteLine("Matrix builtin " + animation.AsByte);
SetBuiltIn(running);
break;
}

Expand All @@ -115,6 +160,19 @@ public void SetMatrix(bool wakeUp = false)


}

private void SetBuiltIn(int running)
{
BuiltInAnimation animation = new BuiltInAnimation(
(BuiltInAnimation.Running)running,
BuiltInAnimation.Sleeping.Starfield,
BuiltInAnimation.Shutdown.SeeYa,
BuiltInAnimation.Startup.StaticEmergence
);
deviceMatrix.SetBuiltInAnimation(true, animation);
Logger.WriteLine("Matrix builtin: " + animation.AsByte);
}

private void StartMatrixTimer(int interval = 100)
{
matrixTimer.Interval = interval;
Expand All @@ -129,15 +187,16 @@ private void StopMatrixTimer()

private void MatrixTimer_Elapsed(object? sender, ElapsedEventArgs e)
{
//if (!IsValid) return;

if (deviceMatrix is null) return;

switch (AppConfig.Get("matrix_running"))
{
case 2:
device.PresentNextFrame();
deviceMatrix.PresentNextFrame();
break;
case 3:
device.PresentClock();
deviceMatrix.PresentClock();
break;
}

Expand All @@ -146,7 +205,7 @@ private void MatrixTimer_Elapsed(object? sender, ElapsedEventArgs e)

public void SetMatrixClock()
{
device.SetBuiltInAnimation(false);
deviceMatrix.SetBuiltInAnimation(false);
StartMatrixTimer(1000);
Logger.WriteLine("Matrix Clock");
}
Expand Down Expand Up @@ -177,9 +236,9 @@ void StopMatrixAudio()

void SetMatrixAudio()
{
if (!IsValid) return;
if (deviceMatrix is null) return;

device.SetBuiltInAnimation(false);
deviceMatrix.SetBuiltInAnimation(false);
StopMatrixTimer();
StopMatrixAudio();

Expand Down Expand Up @@ -251,18 +310,20 @@ private void DrawBar(int pos, double h)
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);
}
}

void PresentAudio(double[] audio)
{

if (deviceMatrix is null) return;

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];
Expand All @@ -280,7 +341,7 @@ void PresentAudio(double[] audio)

for (int i = 0; i < size; i++) DrawBar(20 - i, bars[i] * 20 / maxAverage);

device.Present();
deviceMatrix.Present();
}


Expand Down Expand Up @@ -309,7 +370,7 @@ public void OpenMatrixPicture()
AppConfig.Set("matrix_running", 2);

SetMatrixPicture(fileName);
settings.SetMatrixRunning(2);
settings.VisualiseMatrixRunning(2);

}

Expand All @@ -318,7 +379,8 @@ public void OpenMatrixPicture()
public void SetMatrixPicture(string fileName, bool visualise = true)
{

if (!IsValid) return;
if (deviceMatrix is null) return;

StopMatrixTimer();

try
Expand All @@ -338,7 +400,7 @@ public void SetMatrixPicture(string fileName, bool visualise = true)
}

fs.Close();
if (visualise) settings.VisualiseMatrix(fileName);
if (visualise) settings.VisualiseMatrixPicture(fileName);
}
}
catch
Expand All @@ -351,8 +413,8 @@ public void SetMatrixPicture(string fileName, bool visualise = true)

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);
Expand Down Expand Up @@ -380,11 +442,11 @@ protected void ProcessPicture(Image image)
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();
}


Expand All @@ -397,11 +459,11 @@ protected void ProcessPicture(Image image)
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();
}

}
Expand Down
3 changes: 1 addition & 2 deletions app/AnimeMatrix/AnimeMatrixDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Drawing.Text;
using System.Text;

namespace Starlight.AnimeMatrix
namespace GHelper.AnimeMatrix
{
public class BuiltInAnimation
{
Expand Down Expand Up @@ -91,7 +91,6 @@ public class AnimeMatrixDevice : Device
public int MaxRows = 61;
public int MaxColumns = 34;
public int LedStart = 0;

public int FullRows = 11;

private int frameIndex = 0;
Expand Down
8 changes: 3 additions & 5 deletions app/AnimeMatrix/Communication/Platform/WindowsUsbProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,12 @@ public WindowsUsbProvider(ushort vendorId, ushort productId, int maxFeatureRepor
{
HidDevice = DeviceList.Local
.GetHidDevices(vendorId, productId)
.First(x => x.GetMaxFeatureReportLength() == maxFeatureReportLength);

Logger.WriteLine("Matrix Device: " + HidDevice.DevicePath);

.First(x => x.GetMaxFeatureReportLength() >= maxFeatureReportLength);
Logger.WriteLine("Matrix Device: " + HidDevice.DevicePath + " " + HidDevice.GetMaxFeatureReportLength());
}
catch
{
throw new IOException("AniMe Matrix control device was not found on your machine.");
throw new IOException("Matrix control device was not found on your machine.");
}

var config = new OpenConfiguration();
Expand Down
Loading
Loading