Battery Percentage Pattern for Slash Lighting (#2685)

* quicksave / testing

* implemented battery charge pattern

* changed defaults

* immediately refresh pattern

* cleaned up comments
This commit is contained in:
Luca
2024-06-04 13:29:14 -07:00
committed by GitHub
parent 26e26b9701
commit c2a68d3022
2 changed files with 104 additions and 5 deletions

View File

@@ -14,6 +14,7 @@ namespace GHelper.AnimeMatrix
SettingsForm settings; SettingsForm settings;
System.Timers.Timer matrixTimer = default!; System.Timers.Timer matrixTimer = default!;
System.Timers.Timer slashTimer = default!;
public AnimeMatrixDevice? deviceMatrix; public AnimeMatrixDevice? deviceMatrix;
public SlashDevice? deviceSlash; public SlashDevice? deviceSlash;
@@ -45,6 +46,7 @@ namespace GHelper.AnimeMatrix
matrixTimer = new System.Timers.Timer(100); matrixTimer = new System.Timers.Timer(100);
matrixTimer.Elapsed += MatrixTimer_Elapsed; matrixTimer.Elapsed += MatrixTimer_Elapsed;
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -101,7 +103,7 @@ namespace GHelper.AnimeMatrix
deviceSlash.SetEnabled(true); deviceSlash.SetEnabled(true);
deviceSlash.Init(); deviceSlash.Init();
switch ((SlashMode)running) switch ((SlashMode)running)
{ {
case SlashMode.Static: case SlashMode.Static:
@@ -114,12 +116,18 @@ namespace GHelper.AnimeMatrix
deviceSlash.SetStatic(brightness); deviceSlash.SetStatic(brightness);
} }
break; break;
case SlashMode.BatteryLevel:
// call tick to immediately update the pattern
SlashTimer_start();
SlashTimer_tick();
break;
default: default:
deviceSlash.SetMode((SlashMode)running); deviceSlash.SetMode((SlashMode)running);
deviceSlash.SetOptions(true, brightness, inteval); deviceSlash.SetOptions(true, brightness, inteval);
deviceSlash.Save(); deviceSlash.Save();
break; break;
} }
// kill the timer if we are not displaying battery pattern
deviceSlash.SetSleepActive(true); deviceSlash.SetSleepActive(true);
} }
@@ -237,7 +245,6 @@ namespace GHelper.AnimeMatrix
matrixTimer.Stop(); matrixTimer.Stop();
} }
private void MatrixTimer_Elapsed(object? sender, ElapsedEventArgs e) private void MatrixTimer_Elapsed(object? sender, ElapsedEventArgs e)
{ {
@@ -255,13 +262,58 @@ namespace GHelper.AnimeMatrix
} }
public void SetMatrixClock() public void SetMatrixClock()
{ {
deviceMatrix.SetBuiltInAnimation(false); deviceMatrix.SetBuiltInAnimation(false);
StartMatrixTimer(1000); StartMatrixTimer(1000);
Logger.WriteLine("Matrix Clock"); 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() public void Dispose()
{ {
@@ -397,7 +449,6 @@ namespace GHelper.AnimeMatrix
deviceMatrix.Present(); deviceMatrix.Present();
} }
public void OpenMatrixPicture() public void OpenMatrixPicture()
{ {
string fileName = null; string fileName = null;

View File

@@ -1,5 +1,7 @@
using GHelper.AnimeMatrix.Communication; using GHelper.AnimeMatrix.Communication;
using System.Management;
using System.Text; using System.Text;
using System.Timers;
namespace GHelper.AnimeMatrix namespace GHelper.AnimeMatrix
{ {
@@ -20,7 +22,8 @@ namespace GHelper.AnimeMatrix
GameOver, GameOver,
Start, Start,
Buzzer, Buzzer,
Static Static,
BatteryLevel,
} }
internal class SlashPacket : Packet internal class SlashPacket : Packet
@@ -54,7 +57,9 @@ namespace GHelper.AnimeMatrix
{ SlashMode.GameOver, "Game Over"}, { SlashMode.GameOver, "Game Over"},
{ SlashMode.Start, "Start"}, { SlashMode.Start, "Start"},
{ SlashMode.Buzzer, "Buzzer"}, { SlashMode.Buzzer, "Buzzer"},
{ SlashMode.Static, "Static"}, { SlashMode.Static, "Static"},
{ SlashMode.BatteryLevel, "Battery Level"}
}; };
private static Dictionary<SlashMode, byte> modeCodes = new Dictionary<SlashMode, byte> private static Dictionary<SlashMode, byte> modeCodes = new Dictionary<SlashMode, byte>
@@ -127,7 +132,50 @@ namespace GHelper.AnimeMatrix
public void SetStatic(int brightness = 0) public void SetStatic(int brightness = 0)
{ {
SetCustom(Enumerable.Repeat((byte)(brightness * 85.333), 7).ToArray()); 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) public void SetCustom(byte[] data)