Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/seerge/g-helper
Browse files Browse the repository at this point in the history
  • Loading branch information
seerge committed Jun 4, 2024
2 parents d47990c + c2a68d3 commit f3723e9
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 5 deletions.
59 changes: 55 additions & 4 deletions app/AnimeMatrix/AniMatrixControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class AniMatrixControl : NAudio.CoreAudioApi.Interfaces.IMMNotificationCl
SettingsForm settings;

System.Timers.Timer matrixTimer = default!;
System.Timers.Timer slashTimer = default!;

public AnimeMatrixDevice? deviceMatrix;
public SlashDevice? deviceSlash;
Expand Down Expand Up @@ -45,6 +46,7 @@ public AniMatrixControl(SettingsForm settingsForm)

matrixTimer = new System.Timers.Timer(100);
matrixTimer.Elapsed += MatrixTimer_Elapsed;

}
catch (Exception ex)
{
Expand Down Expand Up @@ -101,7 +103,7 @@ public void SetSlash(bool wakeUp = false)

deviceSlash.SetEnabled(true);
deviceSlash.Init();

switch ((SlashMode)running)
{
case SlashMode.Static:
Expand All @@ -114,12 +116,18 @@ public void SetSlash(bool wakeUp = false)
deviceSlash.SetStatic(brightness);
}
break;
case SlashMode.BatteryLevel:
// call tick to immediately update the pattern
SlashTimer_start();
SlashTimer_tick();
break;
default:
deviceSlash.SetMode((SlashMode)running);
deviceSlash.SetOptions(true, brightness, inteval);
deviceSlash.Save();
break;
}
// kill the timer if we are not displaying battery pattern

deviceSlash.SetSleepActive(true);
}
Expand Down Expand Up @@ -237,7 +245,6 @@ private void StopMatrixTimer()
matrixTimer.Stop();
}


private void MatrixTimer_Elapsed(object? sender, ElapsedEventArgs e)
{

Expand All @@ -255,13 +262,58 @@ private void MatrixTimer_Elapsed(object? sender, ElapsedEventArgs e)

}


public void SetMatrixClock()
{
deviceMatrix.SetBuiltInAnimation(false);
StartMatrixTimer(1000);
Logger.WriteLine("Matrix Clock");
}


private void SlashTimer_start(int interval = 60000)
{
// 100% to 0% in 1hr = 1% every 36 seconds
// 1 bracket every 14.2857 * 36s = 514s ~ 8m 30s
// only ~5 actually distinguishable levels, so refresh every <= 514/5 ~ 100s
// default is 60s

// create the timer if first call
// this way, the timer only spawns if user tries to use battery pattern
if(slashTimer == default(System.Timers.Timer))
{
slashTimer = new System.Timers.Timer(interval);
slashTimer.Elapsed += SlashTimer_elapsed;
slashTimer.AutoReset = true;
}
// only write if interval changed
if(slashTimer.Interval != interval)
{
slashTimer.Interval = interval;
}

slashTimer.Start();
}

private void SlashTimer_elapsed(object? sender, ElapsedEventArgs e)
{
SlashTimer_tick();
}

private void SlashTimer_tick()
{
if (deviceSlash is null) return;

//kill timer if called but not in battery pattern mode
if((SlashMode)AppConfig.Get("matrix_running", 0) != SlashMode.BatteryLevel)
{
slashTimer.Stop();
slashTimer.Dispose();
return;
}

deviceSlash.SetBatteryPattern(AppConfig.Get("matrix_brightness", 0));
}


public void Dispose()
{
Expand Down Expand Up @@ -397,7 +449,6 @@ void PresentAudio(double[] audio)
deviceMatrix.Present();
}


public void OpenMatrixPicture()
{
string fileName = null;
Expand Down
50 changes: 49 additions & 1 deletion app/AnimeMatrix/SlashDevice.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using GHelper.AnimeMatrix.Communication;
using System.Management;
using System.Text;
using System.Timers;

namespace GHelper.AnimeMatrix
{
Expand All @@ -20,7 +22,8 @@ public enum SlashMode
GameOver,
Start,
Buzzer,
Static
Static,
BatteryLevel,
}

internal class SlashPacket : Packet
Expand Down Expand Up @@ -54,7 +57,9 @@ public class SlashDevice : Device
{ SlashMode.GameOver, "Game Over"},
{ SlashMode.Start, "Start"},
{ SlashMode.Buzzer, "Buzzer"},

{ SlashMode.Static, "Static"},
{ SlashMode.BatteryLevel, "Battery Level"}
};

private static Dictionary<SlashMode, byte> modeCodes = new Dictionary<SlashMode, byte>
Expand Down Expand Up @@ -127,7 +132,50 @@ public void SetMode(SlashMode mode)
public void SetStatic(int brightness = 0)
{
SetCustom(Enumerable.Repeat((byte)(brightness * 85.333), 7).ToArray());
}

public static double GetBatteryChargePercentage()
{
double batteryCharge = 0;
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Battery");
foreach (ManagementObject battery in searcher.Get())
{
batteryCharge = Convert.ToDouble(battery["EstimatedChargeRemaining"]);
break; // Assuming only one battery
}
}
catch (ManagementException e)
{
Console.WriteLine("An error occurred while querying for WMI data: " + e.Message);
}
return batteryCharge;
}

private byte[] GetBatteryPattern(int brightness, double percentage)
{
// because 7 segments, within each led segment represents a percentage bracket of (100/7 = 14.2857%)
// set brightness to reflect battery's percentage within that range

int bracket = (int)Math.Floor(percentage / 14.2857);
if(bracket >= 7) return Enumerable.Repeat((byte)(brightness * 85.333), 7).ToArray();

byte[] batteryPattern = Enumerable.Repeat((byte)(0x00), 7).ToArray();
for (int i = 6; i > 6-bracket; i--)
{
batteryPattern[i] = (byte)(brightness * 85.333);
}

//set the "selected" bracket to the percentage of that bracket filled from 0 to 255 as a hex
batteryPattern[6-bracket] = (byte)(((percentage % 14.2857) * brightness * 85.333) / 14.2857);

return batteryPattern;
}

public void SetBatteryPattern(int brightness)
{
SetCustom(GetBatteryPattern(brightness, 100*(GetBatteryChargePercentage()/AppConfig.Get("charge_limit",100))));
}

public void SetCustom(byte[] data)
Expand Down

0 comments on commit f3723e9

Please sign in to comment.