Skip to content

Commit

Permalink
small cleanup and optimization
Browse files Browse the repository at this point in the history
removed some small memory overhead by closing open connections and removing keypress handlers when necessary
  • Loading branch information
SmokeyMcBong committed Mar 13, 2018
1 parent 0fa3768 commit 1ac8abf
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 30 deletions.
53 changes: 34 additions & 19 deletions StreamDeckMonitor/ImageMgr/ImageMgr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Accord.Video.FFMPEG;
using System.IO;
using System.Drawing.Imaging;
using System.Collections.Generic;

namespace StreamDeckMonitor
{
Expand Down Expand Up @@ -76,15 +77,15 @@ void CreateImage(string text, string filename, int textSize, Single x, Single y)
}
}

//extract and resize video frames for animation
//process video frames for animation
public static void StartAnimation()
{
while (true)
{
//create instance of video reader and open video file
VideoFileReader vidReader = new VideoFileReader();
string fileExt = ".mp4";
vidReader.Open(SettingsMgr.animationImgDir + SettingsMgr.animName + fileExt);
string vidFile = SettingsMgr.animationImgDir + SettingsMgr.animName + ".mp4";
vidReader.Open(vidFile);

int frameCount = Convert.ToInt32(vidReader.FrameCount);
int adjustedCount;
Expand All @@ -109,7 +110,7 @@ public static void StartAnimation()
//dispose the video frame
videoFrame.Dispose();

//process animation from stream
//display animation from stream
vidStream.Seek(0, SeekOrigin.Begin);
var animStream = StreamDeckKeyBitmap.FromStream(vidStream);
ShowAnim(animStream);
Expand All @@ -120,20 +121,27 @@ public static void StartAnimation()
vidReader.Close();
}

//send images to the deck in the sequence the frames are received
void ShowAnim(StreamDeckKeyBitmap anim)
//display animation
void ShowAnim(StreamDeckKeyBitmap animStream)
{
List<int> buttonList = new List<int>
{
SettingsMgr.KeyLocBgImg1,
SettingsMgr.KeyLocBgImg2,
SettingsMgr.KeyLocBgImg3,
SettingsMgr.KeyLocBgImg4,
SettingsMgr.KeyLocBgImg5,
SettingsMgr.KeyLocBgImg6,
SettingsMgr.KeyLocBgImg7
};

foreach (var button in buttonList)
{
deck.SetKeyBitmap(button, animStream);
}

//frametime delay
int frametime = SettingsMgr.FrametimeValue();

deck.SetKeyBitmap(SettingsMgr.KeyLocBgImg1, anim);
deck.SetKeyBitmap(SettingsMgr.KeyLocBgImg2, anim);
deck.SetKeyBitmap(SettingsMgr.KeyLocBgImg3, anim);
deck.SetKeyBitmap(SettingsMgr.KeyLocBgImg4, anim);
deck.SetKeyBitmap(SettingsMgr.KeyLocBgImg5, anim);
deck.SetKeyBitmap(SettingsMgr.KeyLocBgImg6, anim);
deck.SetKeyBitmap(SettingsMgr.KeyLocBgImg7, anim);

System.Threading.Thread.Sleep(frametime);
}
}
Expand Down Expand Up @@ -204,10 +212,17 @@ void ProcessImage(string imagefilepath, string tempimagefilepath)
}
}

bitmap.Save(tempimagefilepath);//save the image file

var tempBitmap = StreamDeckKeyBitmap.FromFile(tempimagefilepath);
deck.SetKeyBitmap(location, tempBitmap);
using (var valuesStream = new MemoryStream())
{
bitmap.Save(valuesStream, ImageFormat.Png);
bitmap.Dispose();

//display values using stream
valuesStream.Seek(0, SeekOrigin.Begin);
var valStream = StreamDeckKeyBitmap.FromStream(valuesStream);
deck.SetKeyBitmap(location, valStream);
valuesStream.Close();
}
}
}
}
Expand Down
36 changes: 25 additions & 11 deletions StreamDeckMonitor/SDMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ static void Main(string[] args)
//make sure only one instance is running
SettingsMgr.CheckForTwins();

//define openhardwaremonitor sensors (CPU temp data requires 'highestAvailable' requestedExecutionLevel !!)
//MSI Afterburner MACM shared memory
HardwareMonitor mahm = new HardwareMonitor();

//define openhardwaremonitor sensors and connect (CPU temp data requires 'highestAvailable' requestedExecutionLevel !!)
Computer computer = new Computer() { CPUEnabled = true, GPUEnabled = true };
computer.Open();

Expand Down Expand Up @@ -56,16 +59,18 @@ void StartMe()
//start loop
while (true)
{
try
{
//connect to MSI Afterburner MACM shared memory
HardwareMonitor mahm = new HardwareMonitor();
//add key press handler, if pressed send exit command
ImageMgr.deck.KeyPressed += DeckKeyPressed;

//Get monitoring data
HardwareMonitorEntry framerate = mahm.GetEntry(HardwareMonitor.GPU_GLOBAL_INDEX, MONITORING_SOURCE_ID.FRAMERATE);
//connect to msi afterburner and reload values
var framerateEntry = mahm.GetEntry(HardwareMonitor.GPU_GLOBAL_INDEX, MONITORING_SOURCE_ID.FRAMERATE);
mahm.Connect();
mahm.ReloadEntry(framerateEntry);

try
{
//get values for framerate and pass to process
int framerateInt = (int)Math.Round(framerate.Data);
int framerateInt = (int)Math.Round(framerateEntry.Data);
string dataValue = framerateInt.ToString();
string type = "f";
ImageMgr.ProcessValueImg(dataValue, type, SettingsMgr.KeyLocFps);
Expand Down Expand Up @@ -156,6 +161,7 @@ void StartMe()
}
}
}

//search for load sensor
if (sensor.SensorType == SensorType.Load)
{
Expand Down Expand Up @@ -185,8 +191,11 @@ void StartMe()
//wait 1 second before restarting loop
System.Threading.Thread.Sleep(1000);

//check for key presses, if pressed send exit command
ImageMgr.deck.KeyPressed += DeckKeyPressed;
//close msi afturburner monitoring connection
mahm.Disconnect();

//remove handler
ImageMgr.deck.KeyPressed -= DeckKeyPressed;
}
}

Expand All @@ -196,7 +205,12 @@ void DeckKeyPressed(object sender, StreamDeckKeyEventArgs e)
//clean display and set brightness back to rough default of 60%
ImageMgr.deck.ClearKeys();
ImageMgr.deck.SetBrightness(60);
//exit

//close monitoring connections
mahm.Disconnect();
computer.Close();

//exit
Environment.Exit(Environment.ExitCode);
}
}
Expand Down

0 comments on commit 1ac8abf

Please sign in to comment.