Skip to content

Commit

Permalink
Optimize Emulator
Browse files Browse the repository at this point in the history
  • Loading branch information
arkypita committed Jun 22, 2024
1 parent 55880a0 commit 314dcc9
Showing 1 changed file with 81 additions and 36 deletions.
117 changes: 81 additions & 36 deletions LaserGRBL/GrblEmulator/EmulatorUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public partial class EmulatorUI : Form
{
private static EmulatorUI istance;
private bool canclose = false;

RollingBuffer rb = new RollingBuffer(30);
private const int CP_NOCLOSE_BUTTON = 0x200;
protected override CreateParams CreateParams
{
Expand Down Expand Up @@ -55,63 +55,108 @@ public EmulatorUI(string initmessage)
{
InitializeComponent();
Grblv11Emulator.EmulatorMessage += Grblv11Emulator_EmulatorMessage;

Grblv11Emulator_EmulatorMessage(initmessage);
}

StringBuilder sb = new StringBuilder();

void Grblv11Emulator_EmulatorMessage(string message)
{
if (message == null)
{
if (InvokeRequired)
Invoke(new Grblv11Emulator.SendMessage(ManageClearMessage), new object [] {null});
else
ManageClearMessage(null);
}
rb.Clear();
else
{
lock (sb)
{ sb.AppendLine(message); }
}
rb.Add(message);
}


void ManageClearMessage(string message)
private void RT_Tick(object sender, EventArgs e)
{
lock (sb)
{ sb.Length = 0; }
RTB.Text = "";
string buff = "";

string[] arr = rb.ToArray();

foreach (string s in arr)
buff = buff + s + "\r\n";

RTB.Text = buff;


RTB.SelectionStart = RTB.TextLength;
RTB.ScrollToCaret();
}

private void RT_Tick(object sender, EventArgs e)
private void EmulatorUI_FormClosing(object sender, FormClosingEventArgs e)
{
string buff = null;
lock (sb)
{
if (sb.Length > 0)
e.Cancel = !canclose;
}
}


public class RollingBuffer //one reader, one writer... no need to sync?!
{
private string[] buffer;
private int head;
private int tail;
private int count;
private int capacity;

public RollingBuffer(int size)
{
lock(this)
{
buffer = new string[size];
capacity = size;
head = 0;
tail = 0;
count = 0;
}
}

public void Add(string item)
{
//lock (this)
//{
buffer[head] = item;
head = (head + 1) % capacity;

if (count == capacity)
{
buff = sb.ToString();
sb.Length = 0;
tail = (tail + 1) % capacity; // Overwrite oldest element
}
}
else
{
count++;
}
//}
}

if (buff != null)
{
RTB.Text = RTB.Text + buff;
public string[] ToArray()
{
//lock(this)
//{
string[] result = new string[count];
for (int i = 0; i < count; i++)
result[i] = buffer[(tail + i) % capacity];
return result;
//}
}

if (RTB.TextLength > 10000)
RTB.Text = RTB.Text.Substring(RTB.Text.Length - 10000);
internal void Clear()
{
//lock (this)
//{
head = 0;
tail = 0;
count = 0;
//}
}

RTB.SelectionStart = RTB.TextLength;
RTB.ScrollToCaret();
}
public int Count
{
get { return count; }
}

private void EmulatorUI_FormClosing(object sender, FormClosingEventArgs e)
public int Capacity
{
e.Cancel = !canclose;
get { return capacity; }
}
}
}

0 comments on commit 314dcc9

Please sign in to comment.