Skip to content

Commit

Permalink
Increased performance.
Browse files Browse the repository at this point in the history
NEW Smoothing for text.
Adjustable sample rate.
Use of a statusbar.
  • Loading branch information
COMBudda committed Dec 9, 2023
1 parent efdbff3 commit b4dc0d4
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 46 deletions.
19 changes: 17 additions & 2 deletions Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

115 changes: 71 additions & 44 deletions Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System;
using System.Collections.Generic;
using SaleaeDeviceSdkDotNet;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;


namespace CGATest
Expand All @@ -34,7 +35,7 @@ public partial class Form1 : Form

public volatile StreamReader vIn;
public volatile ConcurrentQueue<int> bufferedData = new ConcurrentQueue<int>();

static int maxBuffer = 100000000;

bool endofstream = false;
Expand Down Expand Up @@ -99,15 +100,16 @@ private void TShowInfo(object myObject, EventArgs myEventArgs)
toolStripStatusLabel4.Text = TB1.ToString();
toolStripStatusLabel5.Text = TB2.ToString();
toolStripStatusLabel6.Text = screens.ToString();
if (screens < 2) reset();
screens = 0;
}


private async void TShowPic(object? obj)
{
Bitmap? resized = null;
CancellationToken ct = (CancellationToken)obj;

await Task.Run(() =>
{
while (true && !ct.IsCancellationRequested)
Expand All @@ -121,25 +123,24 @@ await Task.Run(() =>
else // or do we scale up
{
resized = new Bitmap((Bitmap)newframe, new Size(xResize, yResize));
pictureBox1.Image = (Bitmap)resized.Clone();
resized.Dispose();
}
newframe.Dispose();
newframeisready = false;
}
}
// Wait for a refresh
Thread.Sleep(1);
Thread.Sleep(10);
}
}
);
}







// Read input stream to buffer
private async void TBufferInput(object? o)
// Read input stream to buffer
private async void TBufferInput(object? o)
{
int buffSize = 32;
bool loop = true;
Expand All @@ -152,9 +153,7 @@ private async void TBufferInput(object? o)
while (loop && vIn != null && !ct.IsCancellationRequested)
{
readchar = await vIn.ReadAsync(c, 0, buffSize);
TB3 = bufferedData.Count;
int i = 0;
// if (TB3<maxBuff)
try
{
// Delay input since the file is much faster than the drawing
Expand All @@ -180,8 +179,8 @@ await Task.Run(() =>
{
if (restartRequired)
{
mLogic.ReadStart();
toolStripStatusLabel8.Text = "Connected to Logic with " + mSampleRateHz + "Hz";
mLogic.ReadStart();
toolStripStatusLabel8.Text = "Connected to Logic with " + mLogic.SampleRateHz + "Hz";
}
restartRequired = false;
Thread.Sleep(10);
Expand All @@ -201,7 +200,6 @@ private async void Form1_Shown(object sender, EventArgs e)
CancellationTokenSource CTSTSaleaeWatchdog = new CancellationTokenSource();
CancellationToken CTTSaleaeWatchdog = CTSTSaleaeWatchdog.Token;


Thread Handler_TShowPic = null;
CancellationTokenSource CTSshowpic = new CancellationTokenSource();
CancellationToken CTShowPic = CTSshowpic.Token;
Expand Down Expand Up @@ -246,6 +244,22 @@ await Task.Run(() =>
}
});

List<uint> sample_rates = new List<uint>();
if (mLogic != null)
sample_rates = mLogic.GetSupportedSampleRates();
if (mLogic16 != null)
sample_rates = mLogic16.GetSupportedSampleRates();


//Sample Rates
for (int i = 0; i < sample_rates.Count; ++i)
{
uint newSampleRate = sample_rates[i];
ToolStripItem item = toolStripDropDownButton1.DropDownItems.Add(string.Format(sample_rates[i].ToString(), newSampleRate ), null, new EventHandler(SetSamplingRate));
item.Tag = newSampleRate;
}


toolStripStatusLabel8.ForeColor = Color.Green;
toolStripStatusLabel8.Text = "Connected to Logic with " + mSampleRateHz + "Hz";

Expand All @@ -270,7 +284,7 @@ await Task.Run(() =>
// If finished, clean up threads
if (Handler_TBufferInput != null) CTSTBufferInput.Cancel();
if (Handler_TSaleaeWatchdog != null) CTSTSaleaeWatchdog.Cancel();
if (Handler_TShowPic != null) CTSshowpic.Cancel();
//if (Handler_TShowPic != null) CTSshowpic.Cancel();
}

//Saleae
Expand Down Expand Up @@ -352,14 +366,17 @@ public async Task Main(object sender, EventArgs e)
await Task.Run(async () =>
{
newpic = await CP;
CP.Dispose();
});

if (xposmax >= width) xposmax = width;
if (yposmax >= height) yposmax = height;

if (newpic != null) // Valid picture received, extract only the area which has data in it
{
RectangleF cloneRect = new RectangleF(0, 0, xposmax_b, yposmax_b);
if (newpic.Height > 5) newframe = (Bitmap)newpic.Clone(cloneRect, PixelFormat.DontCare);
//if (newpic.Height > 5) newframe = (Bitmap)newpic.Clone(cloneRect, PixelFormat.DontCare);
newframe = (Bitmap)newpic.Clone();
newpic.Dispose();

newframeisready = true;
Expand All @@ -381,8 +398,8 @@ await Task.Run(async () =>

private async Task<Bitmap> CreatePicture(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap((xposmax_b > 10 ? xposmax : 10), (yposmax_b > 10 ? yposmax_b : 10));
Bitmap bmp = new Bitmap((xposmax_b > 10 ? xposmax : 10), (yposmax_b > 10 ? yposmax_b : 10));

int readchar, readFail;
int rawdata = 0;
bool hsync_raw = false, vsync_raw = false;
Expand All @@ -404,15 +421,15 @@ private async Task<Bitmap> CreatePicture(object sender, EventArgs e)

bool loop = true;
bool vcompositeflag = false, armed = false;

//experiment
BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = data.Stride;
//experiment

await Task.Run(() =>
{
while (loop && !endofstream)
while (loop && !endofstream)
{
// A timeout in case no data is added to the queue. Multibyte read-async can get stuck then
readFail = 0;
Expand All @@ -427,7 +444,7 @@ await Task.Run(() =>
// Give it some time
Thread.Sleep(1);
}
// Catch standard case of file end
if (readchar == -1)
{
Expand Down Expand Up @@ -582,10 +599,10 @@ await Task.Run(() =>
if (!hsync) // Outside hsync cyle
{
int xoffset = 0 ;
int xoffset = 0;
if (hsyncc) // Just coming from a hsync signal, new line
{
// expectation setting and limiter
xposmax = xpos;
// new line and reset horizontal
Expand All @@ -598,45 +615,45 @@ await Task.Run(() =>
// Find the shortest hsync pulse
if (hsync_p < hduration) hduration = hsync_p;
// If there is jitter, skip pixels
xoffset = (hsync_p-hduration);
xoffset = (hsync_p - hduration);
xpos = xoffset;
hsync_p = 0;
}
else // No sync and no recent changes: Set horizontal pixels
{
if (xpos>0 && xpos < width && xpos < xposmax_b && ypos < yposmax_b) // Within defined bitmap ?
if (xpos > 0 && xpos < width && xpos < xposmax_b && ypos < yposmax_b ) // Within defined bitmap ?
{
byte setblue = blue;
byte setgreen = green;
byte setred = red;
unsafe
{
byte* ptr = (byte*)data.Scan0;
if (smoothing)
{
if (!(blue == bline[xpos, ypos])) setblue = bline[xpos-xoffset, ypos];
if (!(green == gline[xpos, ypos])) setgreen = gline[xpos-xoffset, ypos];
if (!(red == rline[xpos, ypos])) setred = rline[xpos-xoffset, ypos];
if (!(blue == bline[xpos, ypos])) setblue = bline[xpos - xoffset, ypos];
if (!(green == gline[xpos, ypos])) setgreen = gline[xpos - xoffset, ypos];
if (!(red == rline[xpos, ypos])) setred = rline[xpos - xoffset, ypos];
}
ptr[(xpos * 3) + ypos * stride] = setblue;
ptr[(xpos * 3) + ypos * stride + 1] = setgreen;
ptr[(xpos * 3) + ypos * stride + 2] = setred;
};
if (smoothing)
{
bline[xpos, ypos] = (byte)((setblue*2 + blue) / 3);
gline[xpos, ypos] = (byte)((setgreen *2+ green) / 3);
rline[xpos, ypos] = (byte)((setred *2 + red) / 3);
bline[xpos, ypos] = (byte)((setblue * 2 + blue) / 3);
gline[xpos, ypos] = (byte)((setgreen * 2 + green) / 3);
rline[xpos, ypos] = (byte)((setred * 2 + red) / 3);
}
xpos++;
}
else xpos++; // If no pixel are set, we are still interested in the high value
}
}
}
else hsync_p++;
}
Expand All @@ -651,20 +668,21 @@ await Task.Run(() =>
// Visualize parameters
TB1 = xposmax_b;
TB2 = yposmax_b;

if (xposmax_b < 10 || yposmax_b < 10 || endofstream) return null; // received frame is too small or stream stopped, discard
else
{
//experiment
bmp.UnlockBits(data);
//experiment

return bmp;
}


}

private void reset()
private void reset()
{
// Reset
syncpulses = 0; sync_long = 0;
Expand All @@ -681,7 +699,6 @@ private void Form1_FormClosing(object sender, FormClosingEventArgs e)
Console.WriteLine("Sorry, the device is not currently streaming.");
else
mLogic.Stop();

}


Expand Down Expand Up @@ -782,8 +799,18 @@ private void button1_Click(object sender, EventArgs e)
private void checkBox5_CheckedChanged(object sender, EventArgs e)
{
smoothing = !smoothing;
checkBox5.Checked= smoothing;
checkBox5.Checked = smoothing;
}
}

private void SetSamplingRate(object sender, EventArgs e)
{
ToolStripItem item = (ToolStripItem)sender;
uint newRate = (uint)item.Tag;
mLogic.Stop();
Thread.Sleep(100);
mLogic.SampleRateHz = newRate;
restartRequired = true;
}
}

}
12 changes: 12 additions & 0 deletions Form1.resx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@
<metadata name="statusStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>135, 17</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="toolStripDropDownButton1.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8
YQUAAAAJcEhZcwAAFiUAABYlAUlSJPAAAAEKSURBVEhL3ZG9DsFQHMXvczDZvIOtXsHObuhqkViI3Quw
6CYmNoMYJJ0NBiFFIoIytOuf0+TeXP3yde+iyS+3OcP53Z4y3/dJJ4HAsiwyTVMp6BQCBIZhKAWdEcHV
vSlBmeB82NFy1KLluEWOPRC5MoHdMWhazwi4RJlALgf4EuT6BI+5kCsTrGddUY658E+QvyXYHq9UnRyC
U87f4aUApcXhnrI9Jzg/laQKFntXlHM+lSQK5psL5fvbp/JvJLGCQqmSWM5JkiCT84igXGtSrruKLQ0T
luAdmZxHBG37FFuWBC/j5XKOmX8WAH7rcI6ZMffPgjQwN2bXJgDo/COBTpjneQ2dML0PY3cISreGe8HM
qgAAAABJRU5ErkJggg==
</value>
</data>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>25</value>
</metadata>
Expand Down

0 comments on commit b4dc0d4

Please sign in to comment.