Skip to content

Commit 314dcc9

Browse files
committed
Optimize Emulator
1 parent 55880a0 commit 314dcc9

File tree

1 file changed

+81
-36
lines changed

1 file changed

+81
-36
lines changed

LaserGRBL/GrblEmulator/EmulatorUI.cs

Lines changed: 81 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public partial class EmulatorUI : Form
1919
{
2020
private static EmulatorUI istance;
2121
private bool canclose = false;
22-
22+
RollingBuffer rb = new RollingBuffer(30);
2323
private const int CP_NOCLOSE_BUTTON = 0x200;
2424
protected override CreateParams CreateParams
2525
{
@@ -55,63 +55,108 @@ public EmulatorUI(string initmessage)
5555
{
5656
InitializeComponent();
5757
Grblv11Emulator.EmulatorMessage += Grblv11Emulator_EmulatorMessage;
58-
5958
Grblv11Emulator_EmulatorMessage(initmessage);
6059
}
6160

62-
StringBuilder sb = new StringBuilder();
63-
6461
void Grblv11Emulator_EmulatorMessage(string message)
6562
{
6663
if (message == null)
67-
{
68-
if (InvokeRequired)
69-
Invoke(new Grblv11Emulator.SendMessage(ManageClearMessage), new object [] {null});
70-
else
71-
ManageClearMessage(null);
72-
}
64+
rb.Clear();
7365
else
74-
{
75-
lock (sb)
76-
{ sb.AppendLine(message); }
77-
}
66+
rb.Add(message);
7867
}
7968

8069

81-
void ManageClearMessage(string message)
70+
private void RT_Tick(object sender, EventArgs e)
8271
{
83-
lock (sb)
84-
{ sb.Length = 0; }
85-
RTB.Text = "";
72+
string buff = "";
73+
74+
string[] arr = rb.ToArray();
75+
76+
foreach (string s in arr)
77+
buff = buff + s + "\r\n";
78+
79+
RTB.Text = buff;
80+
81+
82+
RTB.SelectionStart = RTB.TextLength;
83+
RTB.ScrollToCaret();
8684
}
8785

88-
private void RT_Tick(object sender, EventArgs e)
86+
private void EmulatorUI_FormClosing(object sender, FormClosingEventArgs e)
8987
{
90-
string buff = null;
91-
lock (sb)
92-
{
93-
if (sb.Length > 0)
88+
e.Cancel = !canclose;
89+
}
90+
}
91+
92+
93+
public class RollingBuffer //one reader, one writer... no need to sync?!
94+
{
95+
private string[] buffer;
96+
private int head;
97+
private int tail;
98+
private int count;
99+
private int capacity;
100+
101+
public RollingBuffer(int size)
102+
{
103+
lock(this)
104+
{
105+
buffer = new string[size];
106+
capacity = size;
107+
head = 0;
108+
tail = 0;
109+
count = 0;
110+
}
111+
}
112+
113+
public void Add(string item)
114+
{
115+
//lock (this)
116+
//{
117+
buffer[head] = item;
118+
head = (head + 1) % capacity;
119+
120+
if (count == capacity)
94121
{
95-
buff = sb.ToString();
96-
sb.Length = 0;
122+
tail = (tail + 1) % capacity; // Overwrite oldest element
97123
}
98-
}
124+
else
125+
{
126+
count++;
127+
}
128+
//}
129+
}
99130

100-
if (buff != null)
101-
{
102-
RTB.Text = RTB.Text + buff;
131+
public string[] ToArray()
132+
{
133+
//lock(this)
134+
//{
135+
string[] result = new string[count];
136+
for (int i = 0; i < count; i++)
137+
result[i] = buffer[(tail + i) % capacity];
138+
return result;
139+
//}
140+
}
103141

104-
if (RTB.TextLength > 10000)
105-
RTB.Text = RTB.Text.Substring(RTB.Text.Length - 10000);
142+
internal void Clear()
143+
{
144+
//lock (this)
145+
//{
146+
head = 0;
147+
tail = 0;
148+
count = 0;
149+
//}
150+
}
106151

107-
RTB.SelectionStart = RTB.TextLength;
108-
RTB.ScrollToCaret();
109-
}
152+
public int Count
153+
{
154+
get { return count; }
110155
}
111156

112-
private void EmulatorUI_FormClosing(object sender, FormClosingEventArgs e)
157+
public int Capacity
113158
{
114-
e.Cancel = !canclose;
159+
get { return capacity; }
115160
}
116161
}
117162
}

0 commit comments

Comments
 (0)